objective categorical abstract machine language personal data server
at main 92 lines 2.0 kB view raw view rendered
1# mist 2 3is a [Merkle Search Tree](https://atproto.com/specs/repository#mst-structure) implementation for atproto data repositories. 4 5## installation 6 7Add to your `dune-project`: 8 9```lisp 10(depends 11 mist 12 ipld) ; required dependency 13``` 14 15## usage 16 17### working with TIDs 18 19TIDs are 13-character base32-encoded identifiers that combine a microsecond timestamp with a clock ID for ordering and uniqueness. 20 21```ocaml 22open Mist 23 24(* Generate a TID from current timestamp *) 25let tid = Tid.now () 26(* => "3jzfcijpj2z23" *) 27 28(* Create TID from timestamp *) 29let tid = Tid.of_timestamp_ms 1609459200000L 30 ~clockid:123 31 32(* Parse TID from string *) 33let tid = Tid.of_string "3jzfcijpj2z23" 34 35(* Extract timestamp *) 36let (timestamp_us, clockid) = Tid.to_timestamp_us tid 37 38(* TIDs are comparable for ordering *) 39let is_later = Tid.compare tid1 tid2 > 0 40``` 41 42### working with MSTs 43 44```ocaml 45open Lwt.Syntax 46 47(* Create a new MST with a blockstore and an empty root *) 48let blockstore = Mist.Storage.Memory_blockstore.create () in 49let* mst = Mst.create blockstore (Cid.of_string "") 50 51(* Add an entry *) 52let key = "app.bsky.feed.post/3jzfcijpj2z23" in 53let cid = Cid.of_string "bafy2bzaceb3z2z23" in 54let* mst = Mst.add mst key cid blockstore 55 56(* Get an entry *) 57let* value_opt = Mst.retrieve_node mst cid in 58match value_opt with 59| Some node -> (* found *) 60| None -> (* not found *) 61 62(* Delete an entry *) 63let* mst = Mst.delete mst key 64 65(* Get the root CID *) 66let root_cid = Cid.to_string mst.root 67``` 68 69### inductive proof 70 71```ocaml 72(* Generate a map of all blocks needed to prove a given key *) 73let* proof = Mst.proof_for_key mst cid key in 74``` 75 76### working with blob references 77 78```ocaml 79(* Parse blob reference from JSON *) 80let blob = Blob_ref.of_yojson json 81 82(* Access blob properties *) 83let cid = blob.ref in 84let mime_type = blob.mime_type in 85let size = blob.size 86 87(* Convert to IPLD representation *) 88let ipld = Blob_ref.to_ipld blob 89 90(* Convert back to JSON *) 91let json = Blob_ref.to_yojson blob 92```