···44 Xrpc.handler (fun ctx ->
55 let {did; cid} = Xrpc.parse_query ctx.req query_of_yojson in
66 let cid = Cid.as_cid cid in
77- let%lwt db = User_store.connect did in
77+ let%lwt {db; _} = Repository.load did ~write:false ~ds:ctx.db in
88 let%lwt blob =
99 match%lwt User_store.get_blob db cid with
1010 | Some blob ->
+33
pegasus/lib/api/sync/getBlocks.ml
···11+type query = {did: string; cids: string list} [@@deriving yojson]
22+33+let handler =
44+ Xrpc.handler (fun ctx ->
55+ let {did; cids} : query = Xrpc.parse_query ctx.req query_of_yojson in
66+ let%lwt {db; commit; _} = Repository.load did ~write:false ~ds:ctx.db in
77+ let commit_cid, commit_signed = Option.get commit in
88+ let commit_block =
99+ commit_signed |> User_store.Types.signed_commit_to_yojson
1010+ |> Dag_cbor.encode_yojson
1111+ in
1212+ let cids = List.map Cid.as_cid cids in
1313+ match%lwt User_store.get_blocks db cids with
1414+ | {blocks; missing= []} ->
1515+ let blocks_stream =
1616+ Repository.BlockMap.entries blocks |> Lwt_seq.of_list
1717+ in
1818+ let car_stream =
1919+ Lwt_seq.cons (commit_cid, commit_block) blocks_stream
2020+ |> Car.blocks_to_stream commit_cid
2121+ in
2222+ Dream.stream
2323+ ~headers:[("Content-Type", "application/vnd.ipld.car")]
2424+ (fun res_stream ->
2525+ Lwt_seq.iter_s
2626+ (fun chunk -> Dream.write res_stream (Bytes.to_string chunk))
2727+ car_stream )
2828+ | {missing; _} ->
2929+ let missing_cids =
3030+ List.map Cid.to_string missing |> String.concat ", "
3131+ in
3232+ Errors.invalid_request ~name:"BlockNotFound"
3333+ ("missing the following blocks: " ^ missing_cids) )
+13
pegasus/lib/api/sync/getLatestCommit.ml
···11+type query = {did: string} [@@deriving yojson]
22+33+type response = {cid: string; rev: string} [@@deriving yojson]
44+55+let handler =
66+ Xrpc.handler (fun ctx ->
77+ let {did} : query = Xrpc.parse_query ctx.req query_of_yojson in
88+ match%lwt Repository.load did ~write:false ~ds:ctx.db with
99+ | {commit= Some (cid, {rev; _}); _} ->
1010+ let cid = Cid.to_string cid in
1111+ Dream.json @@ Yojson.Safe.to_string @@ response_to_yojson {cid; rev}
1212+ | _ ->
1313+ failwith ("couldn't resolve commit for " ^ did) )
+16
pegasus/lib/api/sync/getRecord.ml
···11+type query = {did: string; collection: string; rkey: string} [@@deriving yojson]
22+33+type response = Mist.Lex.repo_record [@@deriving yojson]
44+55+let handler =
66+ Xrpc.handler (fun ctx ->
77+ let {did; collection; rkey} : query =
88+ Xrpc.parse_query ctx.req query_of_yojson
99+ in
1010+ let path = collection ^ "/" ^ rkey in
1111+ let%lwt {db; _} = Repository.load did ~write:false ~ds:ctx.db in
1212+ match%lwt User_store.get_record_by_path db path with
1313+ | Some {value; _} ->
1414+ Dream.json @@ Yojson.Safe.to_string @@ response_to_yojson value
1515+ | None ->
1616+ Errors.invalid_request ~name:"RecordNotFound" "record not found" )
···1515 Errors.invalid_request ~name:"RepoNotFound"
1616 "couldn't find a repo with that did"
1717 in
1818- let%lwt user_db = User_store.connect actor.did in
1818+ let%lwt {db= user_db; _} = Repository.load did ~write:false ~ds:ctx.db in
1919 let%lwt _, commit =
2020 match%lwt User_store.get_commit user_db with
2121 | Some c ->
+1-1
pegasus/lib/api/sync/listBlobs.ml
···1717 | _ ->
1818 1000
1919 in
2020- let%lwt db = User_store.connect did in
2020+ let%lwt {db; _} = Repository.load did ~write:false ~ds:ctx.db in
2121 let%lwt cids = User_store.list_blobs db ~limit ~cursor ?since in
2222 let cids = List.map Cid.to_string cids in
2323 let cursor =
+24-16
pegasus/lib/repository.ml
···141141 ; did: string
142142 ; db: User_store.t
143143 ; mutable block_map: Cid.t StringMap.t option
144144- ; mutable commit: Cid.t option }
144144+ ; mutable commit: (Cid.t * signed_commit) option }
145145146146let get_map t : Cid.t StringMap.t Lwt.t =
147147 let%lwt root, commit =
···151151 | None ->
152152 failwith ("failed to retrieve commit for " ^ t.did)
153153 in
154154- if t.commit <> Some root then t.commit <- Some root ;
154154+ t.commit <- Some (root, commit) ;
155155 match t.block_map with
156156 | Some map ->
157157 Lwt.return map
···225225 let%lwt commit_cid =
226226 User_store.put_commit t.db signed |> Lwt_result.get_exn
227227 in
228228- t.commit <- Some commit_cid ;
228228+ t.commit <- Some (commit_cid, signed) ;
229229 Lwt.return (commit_cid, signed)
230230231231let put_initial_commit t : (Cid.t * signed_commit) Lwt.t =
···243243 | None ->
244244 failwith ("failed to retrieve commit for " ^ t.did)
245245 in
246246- if swap_commit <> None && swap_commit <> t.commit then
246246+ if swap_commit <> None && swap_commit <> Option.map fst t.commit then
247247 Errors.invalid_request ~name:"InvalidSwap"
248248 (Format.sprintf "swapCommit cid %s did not match last commit cid %s"
249249 (Cid.to_string (Option.get swap_commit))
250250- (match t.commit with Some c -> Cid.to_string c | None -> "null") ) ;
250250+ (match t.commit with Some (c, _) -> Cid.to_string c | None -> "null") ) ;
251251 let%lwt block_map = Lwt.map ref (get_map t) in
252252 (* need to cache this so in the end, we only emit new blocks *)
253253 let prev_blocks =
···426426 in
427427 Lwt.return {commit= new_commit; results}
428428429429-let load did : t Lwt.t =
430430- let%lwt data_store_conn = Data_store.connect () in
431431- let%lwt user_db = User_store.connect did in
429429+let load ?write ?(ensure_active = false) ?ds did : t Lwt.t =
430430+ let%lwt data_store_conn =
431431+ match ds with
432432+ | Some ds ->
433433+ Lwt.return ds
434434+ | None ->
435435+ Data_store.connect ?write ()
436436+ in
437437+ let%lwt user_db =
438438+ try%lwt User_store.connect did
439439+ with _ ->
440440+ Errors.invalid_request ~name:"RepoNotFound"
441441+ "your princess is in another castle"
442442+ in
432443 let%lwt () = User_store.init user_db in
433444 let%lwt {signing_key; _} =
434445 match%lwt Data_store.get_actor_by_identifier did data_store_conn with
435435- | Some actor ->
446446+ | Some actor when ensure_active = false || actor.deactivated_at = None ->
436447 Lwt.return actor
448448+ | Some _ ->
449449+ Errors.invalid_request ~name:"RepoDeactivated"
450450+ ("repository " ^ did ^ " is deactivated")
437451 | None ->
438452 failwith ("failed to retrieve actor for " ^ did)
439453 in
440454 let key = Kleidos.parse_multikey_str signing_key in
441441- let%lwt commit =
442442- match%lwt User_store.get_commit user_db with
443443- | Some (cid, _) ->
444444- Lwt.return_some cid
445445- | None ->
446446- Lwt.return_none
447447- in
455455+ let%lwt commit = User_store.get_commit user_db in
448456 Lwt.return {key; did; db= user_db; block_map= None; commit}
449457450458let export_car t : Car.stream Lwt.t =