1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
open Utils
open Node
open Cursor
open Result_lwt.Syntax
type t =
{ commit_db : Commit_db.t
; context : Context.t
; auto_flush_seconds : int
}
let commit_db { commit_db ; _ } = commit_db
let context { context ; _ } = context
let empty t = Cursor.empty t.context
let check_prefix s =
if s = "" then failwith "Path prefix cannot be empty"
else if s.[String.length s - 1] = '/' then
failwith "Path prefix cannot be a directory"
else ()
let default_auto_flush_seconds =
try
let n = int_of_string @@ Sys.getenv "PLEBEIA_SYNC_SECONDS" in
Log.fatal "Configuration: synchronize_s=%d" n;
n
with
| Not_found -> 20
| _ -> Stdlib.failwith "PLEBEIA_SYNC_SECONDS=<int>"
let create ?hashcons ?node_cache ?(lock=true)
?bytes_per_cell ?hash_func ?bytes_per_hash ?resize_step_bytes
?(auto_flush_seconds=default_auto_flush_seconds)
path_prefix =
check_prefix path_prefix;
let*= () =
if lock then
let+= _ = Lock.lock @@ path_prefix ^ ".lock" in ()
else Lwt.return_unit
in
let*= context =
Context.create ?hashcons ?node_cache
?bytes_per_cell ?hash_func ?bytes_per_hash ?resize_step_bytes
(path_prefix ^ ".context")
in
let storage_context = Context.get_storage context in
let*= commit_tree = Commit_tree.create ?resize_step_bytes (path_prefix ^ ".commit_tree") in
let+=? commit_db = Commit_db.create ~hash_func:context.hash.hash_func ~storage_context ~commit_tree in
{ commit_db ; context; auto_flush_seconds }
let open_ ~mode ?hashcons ?node_cache
?bytes_per_cell ?hash_func ?bytes_per_hash ?resize_step_bytes
?(auto_flush_seconds=default_auto_flush_seconds)
path_prefix =
check_prefix path_prefix;
let*= () =
if mode = Storage.Writer then
let+= _ = Lock.lock @@ path_prefix ^ ".lock" in ()
else Lwt.return_unit in
let*= context =
Context.open_ ~mode
?hashcons ?node_cache
?bytes_per_cell ?hash_func ?bytes_per_hash ?resize_step_bytes
(path_prefix ^ ".context")
in
let storage_context = Context.get_storage context in
let*= commit_tree =
let fn = path_prefix ^ ".commit_tree" in
let*= res = Commit_tree.open_ ~mode fn ?resize_step_bytes in
match res with
| Some ct -> Lwt.return ct
| None ->
if mode = Storage.Writer then Commit_tree.create fn
else failwithf "%s does not exist" fn
in
let+=? commit_db = Commit_db.create ~hash_func:context.hash.hash_func ~storage_context ~commit_tree in
{ commit_db ; context; auto_flush_seconds }
let enable_process_sync t =
Storage.enable_process_sync t.context.storage;
Storage.enable_process_sync (Commit_tree.Internal.get_storage (Commit_db.commit_tree t.commit_db))
let close { commit_db ; context; _ } =
let*= () = Context.close context in
Commit_tree.close (Commit_db.commit_tree commit_db)
let compute_commit_hash { commit_db ; _ } ~parent c =
let c = Cursor.go_top c in
let (c, (hp, s)) = Cursor.compute_hash c in
assert (s = "");
c, Commit_db.compute_hash commit_db ~parent hp
let since_last_sync = ref (Mtime_clock.counter ())
let commit vc =
let context = context vc in
let*= () = Storage.commit context.storage in
Commit_db.commit vc.commit_db
let flush vc =
let context = context vc in
let*= () = Storage.flush context.storage in
Commit_db.flush vc.commit_db
let commit
?(allow_missing_parent=false)
({ commit_db ; context ; auto_flush_seconds } as vc)
~parent
~hash_override
c =
let c = Cursor.go_top c in
let () =
let Cursor (_, _, context', _) = c in
assert (context == context');
in
begin match parent with
| None -> ()
| Some ph ->
match Commit_db.find commit_db ph with
| None when allow_missing_parent ->
Log.warning "Vc.commit: parent:%a : parent is not in the storage"
(Format.option Commit_hash.pp) parent
| None ->
failwithf "parent does not exist %a" Commit_hash.pp ph
| Some _ -> ()
end;
let*=? (c, i, hp) = Lwt.return @@ Cursor_storage.write_top_cursor c in
let*=? iinfo =
let Cursor.Cursor (_,_,_,info) = c in
if List.length info.Info.copies <> 0 then
Log.debug "Vc: COPIES=%d" (List.length info.Info.copies);
let storage = context.Context.storage in
Lwt.return @@ Info.write storage info
in
let commit_entry = Commit_db.make_commit commit_db
~parent
~index: i
~info: iinfo
?hash_override
hp
in
Log.notice "Vc.commit: %a" Commit.pp commit_entry;
begin match Commit_db.find commit_db commit_entry.hash with
| Some commit_entry' ->
if commit_entry'.parent = commit_entry.parent then
Log.notice "Vc.commit: Overriding hash %a (old index %a, new index %a)"
Commit_hash.pp commit_entry.hash
Index.pp commit_entry'.index
Index.pp commit_entry.index
else
failwithf "hash collision %a" Commit_hash.pp commit_entry.hash
| None -> ()
end;
let*=? () = Commit_db.add commit_db commit_entry in
let+= () =
if
auto_flush_seconds = 0
|| Mtime.Span.to_s (Mtime_clock.count !since_last_sync) > float auto_flush_seconds
then begin
if auto_flush_seconds <> 0 then Log.notice "Vc: flushing...";
let+= () = flush vc in
if auto_flush_seconds <> 0 then Log.notice "Vc: flushed";
since_last_sync := Mtime_clock.counter ()
end else begin
commit vc
end
in
Ok (c, hp, commit_entry)
let update_reader t =
Log.debug "Vc: updating storage and commit_db";
let+= (), secs = with_time_lwt (fun () -> Commit_db.update_reader t.commit_db) in
Log.debug "Vc: updated storage and commit_db in %a" Mtime.Span.pp secs
let load_info context i_info =
let storage = context.Context.storage in
Info.read storage i_info
let checkout' ?(keep_info=false) ({ commit_db ; context ; _ } as t) hash =
let find () =
Option.map
(fun ({ Commit.index; info; _ } as root) ->
let info = Result.from_Ok @@ load_info context info in
(root,
_Cursor (_Top,
Disk (index, Not_Extender),
context,
if keep_info then info else Info.empty)))
(Commit_db.find commit_db hash)
in
match find () with
| Some _ as res -> Lwt.return res
| None when Context.mode context = Writer ->
Lwt.return_none
| None ->
Log.notice "checkout: update_reader...";
let+= () = update_reader t in
Log.notice "retry checkout...";
find ()
let checkout ?keep_info roots hash =
let+= res = checkout' ?keep_info roots hash in
Option.map snd res
let mem ({ commit_db ; context ; _ } as t) hash =
match Commit_db.find commit_db hash with
| Some _ -> Lwt.return_true
| None when Context.mode context = Writer ->
Lwt.return_false
| None ->
let+= () = update_reader t in
match Commit_db.find commit_db hash with
| Some _ -> true
| None -> false