|
1 | 1 | open Core_kernel |
2 | | -module Filename = Mina_stdlib.Bounded_types.String |
| 2 | + |
| 3 | +(** Buffer size for writing: 128 KB *) |
| 4 | +let buffer_size = 131072 |
| 5 | + |
| 6 | +module type S = Intf.S |
3 | 7 |
|
4 | 8 | module Tag = struct |
5 | 9 | [%%versioned |
6 | 10 | module Stable = struct |
7 | 11 | module V1 = struct |
8 | | - type 'a t = |
9 | | - { filename : Filename.Stable.V1.t; offset : int64; size : int } |
| 12 | + type ('filename_key, 'a) t = |
| 13 | + { filename_key : 'filename_key; offset : int64; size : int } |
10 | 14 | end |
11 | 15 | end] |
12 | 16 | end |
13 | 17 |
|
14 | | -type 'a tag = 'a Tag.t |
| 18 | +module Make_custom (Inputs : sig |
| 19 | + type filename_key |
| 20 | + |
| 21 | + val filename : filename_key -> string |
| 22 | +end) : |
| 23 | + S |
| 24 | + with type 'a tag = (Inputs.filename_key, 'a) Tag.t |
| 25 | + and type filename_key = Inputs.filename_key = struct |
| 26 | + type 'a tag = (Inputs.filename_key, 'a) Tag.t |
| 27 | + |
| 28 | + type filename_key = Inputs.filename_key |
| 29 | + |
| 30 | + type writer_t = |
| 31 | + { f : 'a. (module Bin_prot.Binable.S with type t = 'a) -> 'a -> 'a tag } |
| 32 | + |
| 33 | + let write_value { f } = f |
| 34 | + |
| 35 | + (* Flush buffer to file when it exceeds threshold *) |
| 36 | + let flush_buffer oc buffer = |
| 37 | + Out_channel.output_string oc (Buffer.contents buffer) |
| 38 | + |
| 39 | + (* Write key function provided to the callback *) |
| 40 | + let make_writer ~oc ~filename_key ~buffer : writer_t = |
| 41 | + { f = |
| 42 | + (fun (type a) (module B : Bin_prot.Binable.S with type t = a) |
| 43 | + (value : a) -> |
| 44 | + (* Serialize the value to a bigstring *) |
| 45 | + let serialized_size = B.bin_size_t value in |
| 46 | + let buf = Bigstring.create serialized_size in |
| 47 | + let written = B.bin_write_t buf ~pos:0 value in |
| 48 | + assert (written = serialized_size) ; |
| 49 | + |
| 50 | + (* Convert bigstring to string for writing *) |
| 51 | + let data = Bigstring.to_string buf in |
| 52 | + |
| 53 | + (* Create tag before writing *) |
| 54 | + let tag = |
| 55 | + { Tag.filename_key |
| 56 | + ; offset = Int64.of_int @@ Buffer.length buffer |
| 57 | + ; size = serialized_size |
| 58 | + } |
| 59 | + in |
| 60 | + |
| 61 | + (* Add to buffer *) |
| 62 | + Buffer.add_string buffer data ; |
| 63 | + |
| 64 | + (* Flush if buffer is large enough *) |
| 65 | + if Buffer.length buffer >= buffer_size then ( |
| 66 | + flush_buffer oc buffer ; Buffer.clear buffer ) ; |
| 67 | + |
| 68 | + tag ) |
| 69 | + } |
| 70 | + |
| 71 | + (** Write multiple keys to a database file with buffered I/O *) |
| 72 | + let write_values_exn ~f filename_key = |
| 73 | + let do_writing oc = |
| 74 | + (* Buffer for accumulating writes *) |
| 75 | + let buffer = Buffer.create buffer_size in |
| 76 | + let writer = make_writer ~oc ~filename_key ~buffer in |
| 77 | + |
| 78 | + (* Call user function with write_value *) |
| 79 | + let result = f writer in |
| 80 | + |
| 81 | + (* Flush any remaining data *) |
| 82 | + if Buffer.length buffer > 0 then flush_buffer oc buffer ; |
| 83 | + |
| 84 | + result |
| 85 | + in |
| 86 | + Out_channel.with_file |
| 87 | + (Inputs.filename filename_key) |
| 88 | + ~binary:true ~f:do_writing |
| 89 | + |
| 90 | + (** Read a value from the database using a tag *) |
| 91 | + let read : |
| 92 | + type a. |
| 93 | + (module Bin_prot.Binable.S with type t = a) -> a tag -> a Or_error.t = |
| 94 | + fun (module B : Bin_prot.Binable.S with type t = a) tag -> |
| 95 | + let do_reading ic = |
| 96 | + (* Seek to the specified offset *) |
| 97 | + In_channel.seek ic tag.offset ; |
| 98 | + |
| 99 | + (* Read the exact number of bytes *) |
| 100 | + let buffer = Bytes.create tag.size in |
| 101 | + In_channel.really_input_exn ic ~buf:buffer ~pos:0 ~len:tag.size ; |
| 102 | + |
| 103 | + (* Deserialize using bin_prot *) |
| 104 | + let bigstring = Bigstring.of_bytes buffer in |
| 105 | + let pos_ref = ref 0 in |
| 106 | + let%bind.Or_error value = |
| 107 | + Or_error.try_with ~backtrace:true |
| 108 | + @@ fun () -> B.bin_read_t bigstring ~pos_ref |
| 109 | + in |
| 110 | + if !pos_ref <> tag.size then |
| 111 | + Or_error.error_string |
| 112 | + (sprintf "Size mismatch: expected %d bytes, read %d bytes" tag.size |
| 113 | + !pos_ref ) |
| 114 | + else Ok value |
| 115 | + in |
| 116 | + Or_error.tag ~tag:(Inputs.filename tag.filename_key) |
| 117 | + @@ Or_error.try_with_join ~backtrace:true |
| 118 | + @@ fun () -> |
| 119 | + In_channel.with_file |
| 120 | + (Inputs.filename tag.filename_key) |
| 121 | + ~binary:true ~f:do_reading |
| 122 | +end |
15 | 123 |
|
16 | | -(** Buffer size for writing: 128 KB *) |
17 | | -let buffer_size = 131072 |
| 124 | +include Make_custom (struct |
| 125 | + type filename_key = string |
18 | 126 |
|
19 | | -(* Flush buffer to file when it exceeds threshold *) |
20 | | -let flush_buffer oc buffer = |
21 | | - Out_channel.output_string oc (Buffer.contents buffer) |
22 | | - |
23 | | -type writer_t = |
24 | | - { f : 'a. (module Bin_prot.Binable.S with type t = 'a) -> 'a -> 'a tag } |
25 | | - |
26 | | -let write_value : |
27 | | - writer_t -> (module Bin_prot.Binable.S with type t = 'a) -> 'a -> 'a tag = |
28 | | - fun { f } -> f |
29 | | - |
30 | | -(* Write key function provided to the callback *) |
31 | | -let make_writer ~oc ~filename ~buffer : writer_t = |
32 | | - { f = |
33 | | - (fun (type a) (module B : Bin_prot.Binable.S with type t = a) (value : a) -> |
34 | | - (* Serialize the value to a bigstring *) |
35 | | - let serialized_size = B.bin_size_t value in |
36 | | - let buf = Bigstring.create serialized_size in |
37 | | - let written = B.bin_write_t buf ~pos:0 value in |
38 | | - assert (written = serialized_size) ; |
39 | | - |
40 | | - (* Convert bigstring to string for writing *) |
41 | | - let data = Bigstring.to_string buf in |
42 | | - |
43 | | - (* Create tag before writing *) |
44 | | - let tag = |
45 | | - { Tag.filename |
46 | | - ; offset = Int64.of_int @@ Buffer.length buffer |
47 | | - ; size = serialized_size |
48 | | - } |
49 | | - in |
50 | | - |
51 | | - (* Add to buffer *) |
52 | | - Buffer.add_string buffer data ; |
53 | | - |
54 | | - (* Flush if buffer is large enough *) |
55 | | - if Buffer.length buffer >= buffer_size then ( |
56 | | - flush_buffer oc buffer ; Buffer.clear buffer ) ; |
57 | | - |
58 | | - tag ) |
59 | | - } |
60 | | - |
61 | | -(** Write multiple keys to a database file with buffered I/O *) |
62 | | -let write_values_exn ~f filename = |
63 | | - let do_writing oc = |
64 | | - (* Buffer for accumulating writes *) |
65 | | - let buffer = Buffer.create buffer_size in |
66 | | - let writer = make_writer ~oc ~filename ~buffer in |
67 | | - |
68 | | - (* Call user function with write_value *) |
69 | | - let result = f writer in |
70 | | - |
71 | | - (* Flush any remaining data *) |
72 | | - if Buffer.length buffer > 0 then flush_buffer oc buffer ; |
73 | | - |
74 | | - result |
75 | | - in |
76 | | - Out_channel.with_file filename ~binary:true ~f:do_writing |
77 | | - |
78 | | -(** Read a value from the database using a tag *) |
79 | | -let read : |
80 | | - type a. (module Bin_prot.Binable.S with type t = a) -> a tag -> a Or_error.t |
81 | | - = |
82 | | - fun (module B : Bin_prot.Binable.S with type t = a) tag -> |
83 | | - let do_reading ic = |
84 | | - (* Seek to the specified offset *) |
85 | | - In_channel.seek ic tag.offset ; |
86 | | - |
87 | | - (* Read the exact number of bytes *) |
88 | | - let buffer = Bytes.create tag.size in |
89 | | - In_channel.really_input_exn ic ~buf:buffer ~pos:0 ~len:tag.size ; |
90 | | - |
91 | | - (* Deserialize using bin_prot *) |
92 | | - let bigstring = Bigstring.of_bytes buffer in |
93 | | - let pos_ref = ref 0 in |
94 | | - let%bind.Or_error value = |
95 | | - Or_error.try_with ~backtrace:true |
96 | | - @@ fun () -> B.bin_read_t bigstring ~pos_ref |
97 | | - in |
98 | | - if !pos_ref <> tag.size then |
99 | | - Or_error.error_string |
100 | | - (sprintf "Size mismatch: expected %d bytes, read %d bytes" tag.size |
101 | | - !pos_ref ) |
102 | | - else Ok value |
103 | | - in |
104 | | - Or_error.tag ~tag:tag.filename |
105 | | - @@ Or_error.try_with_join ~backtrace:true |
106 | | - @@ fun () -> In_channel.with_file tag.filename ~binary:true ~f:do_reading |
| 127 | + let filename = ident |
| 128 | +end) |
0 commit comments