···7777 let encoded = Dag_cbor.encode ipld in
7878 let cid = Cid.create Dcbor encoded in
7979 (cid, encoded)
8080+8181+let of_cbor encoded : repo_record =
8282+ let decoded = Dag_cbor.decode encoded in
8383+ match of_ipld decoded with
8484+ | `LexMap m ->
8585+ m
8686+ | _ ->
8787+ raise (Failure "Decoded non-record value")
+6-6
mist/lib/storage/block_map.ml
···6677let empty = Cid_map.empty
8899-let add m value =
99+let add value m =
1010 let cid, bytes = Lex.to_cbor_block value in
1111 (Cid_map.add cid bytes m, cid)
12121313-let set m cid bytes = Cid_map.add cid bytes m
1313+let set = Cid_map.add
14141515-let get m cid = Cid_map.find_opt cid m
1515+let get = Cid_map.find_opt
16161717-let remove m cid = Cid_map.remove cid m
1717+let remove = Cid_map.remove
18181919-let get_many m cids =
1919+let get_many cids m =
2020 let blocks, missing =
2121 List.fold_left
2222 (fun (b, mis) cid ->
2323- match get m cid with
2323+ match get cid m with
2424 | Some bytes ->
2525 (Cid_map.add cid bytes b, mis)
2626 | None ->
+60
mist/lib/storage/memory_store.ml
···11+open Lwt.Infix
22+33+module Make () = struct
44+ type t =
55+ { mutable blocks: Block_map.t
66+ ; mutable root: Cid.t option
77+ ; mutable rev: string option }
88+99+ let create ?(blocks = Block_map.empty) () = {blocks; root= None; rev= None}
1010+1111+ let get_bytes s cid = Lwt.return (Block_map.get cid s.blocks)
1212+1313+ let has s cid = Lwt.return (Block_map.has cid s.blocks)
1414+1515+ let get_blocks s cids = Lwt.return (Block_map.get_many cids s.blocks)
1616+1717+ let read_obj_and_bytes s cid =
1818+ get_bytes s cid
1919+ >|= function
2020+ | Some b ->
2121+ let v = Dag_cbor.decode b in
2222+ Some (v, b)
2323+ | None ->
2424+ None
2525+2626+ let read_obj s cid = read_obj_and_bytes s cid >|= Option.map fst
2727+2828+ let read_record s cid =
2929+ match Block_map.get cid s.blocks with
3030+ | Some b ->
3131+ Lwt.return (Lex.of_cbor b)
3232+ | None ->
3333+ raise (Failure "Missing block")
3434+3535+ let get_root s = Lwt.return s.root
3636+3737+ let put_block s cid bytes ~rev =
3838+ s.blocks <- Block_map.set cid bytes s.blocks ;
3939+ s.rev <- Some rev ;
4040+ Lwt.return_unit
4141+4242+ let put_many s blocks =
4343+ s.blocks <- Block_map.merge s.blocks blocks ;
4444+ Lwt.return_unit
4545+4646+ let update_root s cid ~rev =
4747+ s.root <- Some cid ;
4848+ s.rev <- Some rev ;
4949+ Lwt.return_unit
5050+5151+ let apply_commit s (c : Repo_store.commit_data) =
5252+ let with_removed =
5353+ Cid.Set.fold
5454+ (fun cid blocks -> Block_map.remove cid blocks)
5555+ c.removed_cids s.blocks
5656+ in
5757+ s.blocks <- Block_map.merge with_removed c.relevant_blocks ;
5858+ s.root <- Some c.cid ;
5959+ Lwt.return_unit
6060+end
+54
mist/lib/storage/overlay_store.ml
···11+let ( let* ) = Lwt.bind
22+33+module Make (Top : Repo_store.Readable) (Bottom : Repo_store.Readable) : sig
44+ include Repo_store.Readable
55+66+ val create : Top.t -> Bottom.t -> t
77+end = struct
88+ type t = {top: Top.t; bottom: Bottom.t}
99+1010+ let create top bottom = {top; bottom}
1111+1212+ let get_bytes {top; bottom} cid =
1313+ let* from_top = Top.get_bytes top cid in
1414+ match from_top with
1515+ | Some _ as res ->
1616+ Lwt.return res
1717+ | None ->
1818+ Bottom.get_bytes bottom cid
1919+2020+ let has {top; bottom} cid =
2121+ let* from_top = Top.has top cid in
2222+ match from_top with
2323+ | true ->
2424+ Lwt.return_true
2525+ | false ->
2626+ Bottom.has bottom cid
2727+2828+ let get_blocks {top; bottom} cids =
2929+ let* from_top = Top.get_blocks top cids in
3030+ let* from_bottom = Bottom.get_blocks bottom from_top.missing in
3131+ let merged_blocks = Block_map.merge from_top.blocks from_bottom.blocks in
3232+ Lwt.return {Block_map.blocks= merged_blocks; missing= from_bottom.missing}
3333+3434+ let read_obj_and_bytes {top; bottom} cid =
3535+ let* from_top = Top.read_obj_and_bytes top cid in
3636+ match from_top with
3737+ | Some _ as res ->
3838+ Lwt.return res
3939+ | None ->
4040+ Bottom.read_obj_and_bytes bottom cid
4141+4242+ let read_obj {top; bottom} cid =
4343+ let* from_top = Top.read_obj top cid in
4444+ match from_top with
4545+ | Some _ as res ->
4646+ Lwt.return res
4747+ | None ->
4848+ Bottom.read_obj bottom cid
4949+5050+ let read_record {top; bottom} cid =
5151+ Lwt.catch
5252+ (fun () -> Top.read_record top cid)
5353+ (fun _ -> Bottom.read_record bottom cid)
5454+end
+39
mist/lib/storage/repo_store.ml
···11+type commit_data =
22+ { cid: Cid.t
33+ ; rev: string
44+ ; since: string option
55+ ; prev: Cid.t option
66+ ; relevant_blocks: Block_map.t
77+ ; removed_cids: Cid.Set.t }
88+99+module type Readable = sig
1010+ type t
1111+1212+ val get_bytes : t -> Cid.t -> bytes option Lwt.t
1313+1414+ val has : t -> Cid.t -> bool Lwt.t
1515+1616+ val get_blocks : t -> Cid.t list -> Block_map.with_missing Lwt.t
1717+1818+ val read_obj_and_bytes : t -> Cid.t -> (Dag_cbor.value * bytes) option Lwt.t
1919+2020+ val read_obj : t -> Cid.t -> Dag_cbor.value option Lwt.t
2121+2222+ val read_record : t -> Cid.t -> Lex.repo_record Lwt.t
2323+end
2424+2525+module type Writable = sig
2626+ type t
2727+2828+ include Readable with type t := t
2929+3030+ val get_root : t -> Cid.t option Lwt.t
3131+3232+ val put_block : t -> Cid.t -> bytes -> rev:string -> unit Lwt.t
3333+3434+ val put_many : t -> Block_map.t -> unit Lwt.t
3535+3636+ val update_root : t -> Cid.t -> rev:string -> unit Lwt.t
3737+3838+ val apply_commit : t -> commit_data -> unit Lwt.t
3939+end
+18-26
mist/lib/storage/storage.ml
···11module Block_map = Block_map
22-33-type commit_data =
44- { cid: Cid.t
55- ; rev: string
66- ; since: string option
77- ; prev: Cid.t option
88- ; relevant_blocks: Block_map.t
99- ; removed_cids: Cid.Set.t }
1010-1111-module type S = sig
1212- type t
1313-1414- val get_root : t -> Cid.t option Lwt.t
1515-1616- val put_block : t -> Cid.t -> bytes -> rev:string -> unit Lwt.t
22+module Blob_store = Blob_store
1731818- val put_many : t -> Block_map.t -> unit Lwt.t
1919-2020- val update_root : t -> Cid.t -> rev:string -> unit Lwt.t
2121-2222- val apply_commit : t -> commit_data -> unit Lwt.t
44+module type Readable_blockstore = Repo_store.Readable
2352424- val get_bytes : t -> Cid.t -> bytes option Lwt.t
66+module type Writable_blockstore = Repo_store.Writable
2572626- val has : t -> Cid.t -> bool Lwt.t
88+module Memory_blockstore = struct
99+ module Impl = Memory_store.Make ()
1010+ include Impl
27112828- val get_blocks : t -> Cid.t list -> Block_map.with_missing Lwt.t
1212+ module Readable : Repo_store.Readable with type t = Impl.t = Impl
29133030- val read_obj_and_bytes : t -> Cid.t -> (Dag_cbor.value * bytes) option Lwt.t
1414+ module Writable : Repo_store.Writable with type t = Impl.t = Impl
1515+end
31163232- val read_obj : t -> Cid.t -> Dag_cbor.value option Lwt.t
1717+module Overlay_blockstore
1818+ (Top : Repo_store.Readable)
1919+ (Bottom : Repo_store.Readable) =
2020+struct
2121+ module Impl = Overlay_store.Make (Top) (Bottom)
2222+ include Impl
33233434- val read_record : t -> Cid.t -> Lex.repo_record
2424+ module Readable : Repo_store.Readable with type t = Impl.t = Impl
3525end
2626+2727+type commit_data = Repo_store.commit_data