···1-type 'bs data = {mutable reads: Cid.Set.t; mutable cache: Block_map.t; bs: 'bs}
000023-module Make
4- (Bs : Blockstore.Writable) : sig
5 include Blockstore.Writable
67 val create : Bs.t -> t
···9 val get_reads : t -> Cid.Set.t
1011 val get_cache : t -> Block_map.t
000012end
13with type t = Bs.t data = struct
14 type t = Bs.t data
1516- let create bs = {reads= Cid.Set.empty; cache= Block_map.empty; bs}
01718 let get_reads t = t.reads
1920 let get_cache t = t.cache
2100000000000022 let get_bytes t cid =
23 match Block_map.get cid t.cache with
24 | Some _ as cached ->
···5859 let put_block t cid bytes =
60 t.cache <- Block_map.set cid bytes t.cache ;
61- Bs.put_block t.bs cid bytes
006263 let put_many t blocks =
64 Block_map.iter
65- (fun cid data -> t.cache <- Block_map.set cid data t.cache)
0066 blocks ;
67- Bs.put_many t.bs blocks
06869 let delete_block t cid =
70 t.cache <- Block_map.remove cid t.cache ;
071 Bs.delete_block t.bs cid
7273 let delete_many t cids =
74- List.iter (fun cid -> t.cache <- Block_map.remove cid t.cache) cids ;
000075 Bs.delete_many t.bs cids
76end
···1+type 'bs data =
2+ { mutable reads: Cid.Set.t
3+ ; mutable cache: Block_map.t
4+ ; mutable pending_writes: Block_map.t
5+ ; bs: 'bs }
67+module Make (Bs : Blockstore.Writable) : sig
08 include Blockstore.Writable
910 val create : Bs.t -> t
···12 val get_reads : t -> Cid.Set.t
1314 val get_cache : t -> Block_map.t
15+16+ val get_pending_writes : t -> Block_map.t
17+18+ val flush_writes : t -> (unit, exn) Lwt_result.t
19end
20with type t = Bs.t data = struct
21 type t = Bs.t data
2223+ let create bs =
24+ {reads= Cid.Set.empty; cache= Block_map.empty; pending_writes= Block_map.empty; bs}
2526 let get_reads t = t.reads
2728 let get_cache t = t.cache
2930+ let get_pending_writes t = t.pending_writes
31+32+ let flush_writes t =
33+ if Block_map.is_empty t.pending_writes then Lwt_result.return ()
34+ else
35+ match%lwt Bs.put_many t.bs t.pending_writes with
36+ | Ok _ ->
37+ t.pending_writes <- Block_map.empty ;
38+ Lwt_result.return ()
39+ | Error e ->
40+ Lwt_result.fail e
41+42 let get_bytes t cid =
43 match Block_map.get cid t.cache with
44 | Some _ as cached ->
···7879 let put_block t cid bytes =
80 t.cache <- Block_map.set cid bytes t.cache ;
81+ t.pending_writes <- Block_map.set cid bytes t.pending_writes ;
82+ (* defer actual write to flush_writes *)
83+ Lwt_result.return true
8485 let put_many t blocks =
86 Block_map.iter
87+ (fun cid data ->
88+ t.cache <- Block_map.set cid data t.cache ;
89+ t.pending_writes <- Block_map.set cid data t.pending_writes )
90 blocks ;
91+ (* defer actual write to flush_writes *)
92+ Lwt_result.return (Block_map.length blocks)
9394 let delete_block t cid =
95 t.cache <- Block_map.remove cid t.cache ;
96+ t.pending_writes <- Block_map.remove cid t.pending_writes ;
97 Bs.delete_block t.bs cid
9899 let delete_many t cids =
100+ List.iter
101+ (fun cid ->
102+ t.cache <- Block_map.remove cid t.cache ;
103+ t.pending_writes <- Block_map.remove cid t.pending_writes )
104+ cids ;
105 Bs.delete_many t.bs cids
106end
+8
pegasus/lib/repository.ml
···419 writes
420 in
421 let new_mst = !mst in
00000000422 let%lwt new_commit = put_commit t new_mst.root ~previous:(Some prev_commit) in
423 let new_commit_cid, new_commit_signed = new_commit in
424 let commit_block =
···419 writes
420 in
421 let new_mst = !mst in
422+ (* flush all writes, ensuring all blocks are written or none are *)
423+ let%lwt () =
424+ match%lwt Cached_store.flush_writes cached_store with
425+ | Ok () ->
426+ Lwt.return_unit
427+ | Error e ->
428+ raise e
429+ in
430 let%lwt new_commit = put_commit t new_mst.root ~previous:(Some prev_commit) in
431 let new_commit_cid, new_commit_signed = new_commit in
432 let commit_block =
+8-2
pegasus/lib/util.ml
···279 | Error e ->
280 Lwt.return_error e
281282-(* runs a bunch of queries and catches duplicate insertion, returning how many succeeded *)
283let multi_query pool
284 (queries : (Caqti_lwt.connection -> ('a, Caqti_error.t) Lwt_result.t) list)
285 : (int, exn) Lwt_result.t =
···317 else Lwt.return_error e ) )
318 in
319 let%lwt result = aux (Ok 0) queries in
320- Lwt.return result ) )
000000321322let minute = 60 * 1000
323
···279 | Error e ->
280 Lwt.return_error e
281282+(* runs a bunch of queries in a transaction, catches duplicate insertion, returning how many succeeded *)
283let multi_query pool
284 (queries : (Caqti_lwt.connection -> ('a, Caqti_error.t) Lwt_result.t) list)
285 : (int, exn) Lwt_result.t =
···317 else Lwt.return_error e ) )
318 in
319 let%lwt result = aux (Ok 0) queries in
320+ match result with
321+ | Ok count ->
322+ let$! () = C.commit () in
323+ Lwt.return_ok count
324+ | Error e ->
325+ let%lwt _ = C.rollback () in
326+ Lwt.return_error e ) )
327328let minute = 60 * 1000
329