···11-type 'bs data = {mutable reads: Cid.Set.t; mutable cache: Block_map.t; bs: 'bs}
11+type 'bs data =
22+ { mutable reads: Cid.Set.t
33+ ; mutable cache: Block_map.t
44+ ; mutable pending_writes: Block_map.t
55+ ; bs: 'bs }
2633-module Make
44- (Bs : Blockstore.Writable) : sig
77+module Make (Bs : Blockstore.Writable) : sig
58 include Blockstore.Writable
69710 val create : Bs.t -> t
···912 val get_reads : t -> Cid.Set.t
10131114 val get_cache : t -> Block_map.t
1515+1616+ val get_pending_writes : t -> Block_map.t
1717+1818+ val flush_writes : t -> (unit, exn) Lwt_result.t
1219end
1320with type t = Bs.t data = struct
1421 type t = Bs.t data
15221616- let create bs = {reads= Cid.Set.empty; cache= Block_map.empty; bs}
2323+ let create bs =
2424+ {reads= Cid.Set.empty; cache= Block_map.empty; pending_writes= Block_map.empty; bs}
17251826 let get_reads t = t.reads
19272028 let get_cache t = t.cache
21293030+ let get_pending_writes t = t.pending_writes
3131+3232+ let flush_writes t =
3333+ if Block_map.is_empty t.pending_writes then Lwt_result.return ()
3434+ else
3535+ match%lwt Bs.put_many t.bs t.pending_writes with
3636+ | Ok _ ->
3737+ t.pending_writes <- Block_map.empty ;
3838+ Lwt_result.return ()
3939+ | Error e ->
4040+ Lwt_result.fail e
4141+2242 let get_bytes t cid =
2343 match Block_map.get cid t.cache with
2444 | Some _ as cached ->
···58785979 let put_block t cid bytes =
6080 t.cache <- Block_map.set cid bytes t.cache ;
6161- Bs.put_block t.bs cid bytes
8181+ t.pending_writes <- Block_map.set cid bytes t.pending_writes ;
8282+ (* defer actual write to flush_writes *)
8383+ Lwt_result.return true
62846385 let put_many t blocks =
6486 Block_map.iter
6565- (fun cid data -> t.cache <- Block_map.set cid data t.cache)
8787+ (fun cid data ->
8888+ t.cache <- Block_map.set cid data t.cache ;
8989+ t.pending_writes <- Block_map.set cid data t.pending_writes )
6690 blocks ;
6767- Bs.put_many t.bs blocks
9191+ (* defer actual write to flush_writes *)
9292+ Lwt_result.return (Block_map.length blocks)
68936994 let delete_block t cid =
7095 t.cache <- Block_map.remove cid t.cache ;
9696+ t.pending_writes <- Block_map.remove cid t.pending_writes ;
7197 Bs.delete_block t.bs cid
72987399 let delete_many t cids =
7474- List.iter (fun cid -> t.cache <- Block_map.remove cid t.cache) cids ;
100100+ List.iter
101101+ (fun cid ->
102102+ t.cache <- Block_map.remove cid t.cache ;
103103+ t.pending_writes <- Block_map.remove cid t.pending_writes )
104104+ cids ;
75105 Bs.delete_many t.bs cids
76106end
+8
pegasus/lib/repository.ml
···419419 writes
420420 in
421421 let new_mst = !mst in
422422+ (* flush all writes, ensuring all blocks are written or none are *)
423423+ let%lwt () =
424424+ match%lwt Cached_store.flush_writes cached_store with
425425+ | Ok () ->
426426+ Lwt.return_unit
427427+ | Error e ->
428428+ raise e
429429+ in
422430 let%lwt new_commit = put_commit t new_mst.root ~previous:(Some prev_commit) in
423431 let new_commit_cid, new_commit_signed = new_commit in
424432 let commit_block =
+8-2
pegasus/lib/util.ml
···279279 | Error e ->
280280 Lwt.return_error e
281281282282-(* runs a bunch of queries and catches duplicate insertion, returning how many succeeded *)
282282+(* runs a bunch of queries in a transaction, catches duplicate insertion, returning how many succeeded *)
283283let multi_query pool
284284 (queries : (Caqti_lwt.connection -> ('a, Caqti_error.t) Lwt_result.t) list)
285285 : (int, exn) Lwt_result.t =
···317317 else Lwt.return_error e ) )
318318 in
319319 let%lwt result = aux (Ok 0) queries in
320320- Lwt.return result ) )
320320+ match result with
321321+ | Ok count ->
322322+ let$! () = C.commit () in
323323+ Lwt.return_ok count
324324+ | Error e ->
325325+ let%lwt _ = C.rollback () in
326326+ Lwt.return_error e ) )
321327322328let minute = 60 * 1000
323329