···1313 let did = Auth.get_authed_did_exn ctx.auth in
1414 let bytes_stream = Dream.body_stream ctx.req in
1515 let car_stream = stream_to_seq bytes_stream in
1616- let%lwt repo = Repository.load did ~ds:ctx.db ~ensure_active:true in
1616+ let%lwt repo = Repository.load did ~ensure_active:true in
1717 let%lwt result = Repository.import_car repo car_stream in
1818 match result with
1919 | Ok _ ->
+1-1
pegasus/lib/api/server/checkAccountStatus.ml
···1818 Errors.internal_error ~msg:"actor not found" ()
1919 | Some actor -> (
2020 let%lwt {db= us; commit; _} =
2121- Repository.load ~ds:db did
2121+ Repository.load did
2222 in
2323 let%lwt cid, commit =
2424 match commit with
+1-1
pegasus/lib/api/server/createAccount.ml
···120120 ~perm:0o644
121121 in
122122 let%lwt repo =
123123- Repository.load ~create:true ~ds:db did
123123+ Repository.load ~create:true did
124124 in
125125 let%lwt _ = Repository.put_initial_commit repo in
126126 let%lwt _ = Sequencer.sequence_identity db ~did ~handle () in
+1-3
pegasus/lib/api/sync/getBlob.ml
···44 Xrpc.handler (fun ctx ->
55 let {did; cid} = Xrpc.parse_query ctx.req query_of_yojson in
66 let cid_parsed = Cid.as_cid cid in
77- let%lwt {db; _} =
88- Repository.load did ~ensure_active:true ~ds:ctx.db
99- in
77+ let%lwt {db; _} = Repository.load did ~ensure_active:true in
108 let%lwt blob_meta = User_store.get_blob_metadata db cid_parsed in
119 match blob_meta with
1210 | None ->
+1-3
pegasus/lib/api/sync/getBlocks.ml
···44let handler =
55 Xrpc.handler (fun ctx ->
66 let {did; cids} : query = Xrpc.parse_query ctx.req query_of_yojson in
77- let%lwt {db; commit; _} =
88- Repository.load did ~ensure_active:true ~ds:ctx.db
99- in
77+ let%lwt {db; commit; _} = Repository.load did ~ensure_active:true in
108 let commit_cid, commit_signed = Option.get commit in
119 let commit_block =
1210 commit_signed |> User_store.Types.signed_commit_to_yojson
+1-3
pegasus/lib/api/sync/getLatestCommit.ml
···55let handler =
66 Xrpc.handler (fun ctx ->
77 let {did} : query = Xrpc.parse_query ctx.req query_of_yojson in
88- match%lwt
99- Repository.load did ~ensure_active:true ~ds:ctx.db
1010- with
88+ match%lwt Repository.load did ~ensure_active:true with
119 | {commit= Some (cid, {rev; _}); _} ->
1210 let cid = Cid.to_string cid in
1311 Dream.json @@ Yojson.Safe.to_string @@ response_to_yojson {cid; rev}
+1-3
pegasus/lib/api/sync/getRecord.ml
···99 Xrpc.parse_query ctx.req query_of_yojson
1010 in
1111 let path = collection ^ "/" ^ rkey in
1212- let%lwt repo =
1313- Repository.load did ~ensure_active:true ~ds:ctx.db
1414- in
1212+ let%lwt repo = Repository.load did ~ensure_active:true in
1513 match%lwt Repository.get_record repo path with
1614 | None ->
1715 Printf.ksprintf
+1-1
pegasus/lib/api/sync/getRepoStatus.ml
···1515 Errors.invalid_request ~name:"RepoNotFound"
1616 "couldn't find a repo with that did"
1717 in
1818- let%lwt {db= user_db; _} = Repository.load did ~ds:ctx.db in
1818+ let%lwt {db= user_db; _} = Repository.load did in
1919 let%lwt _, commit =
2020 match%lwt User_store.get_commit user_db with
2121 | Some c ->
+1-3
pegasus/lib/api/sync/listBlobs.ml
···2121 | _ ->
2222 1000
2323 in
2424- let%lwt {db; _} =
2525- Repository.load did ~ensure_active:true ~ds:ctx.db
2626- in
2424+ let%lwt {db; _} = Repository.load did ~ensure_active:true in
2725 let%lwt cids = User_store.list_blobs db ~limit ~cursor ?since in
2826 let cids = List.map Cid.to_string cids in
2927 let cursor =
+19-14
pegasus/lib/data_store.ml
···323323let pool_mutex = Lwt_mutex.create ()
324324325325let connect ?create () : t Lwt.t =
326326- Lwt_mutex.with_lock pool_mutex (fun () ->
327327- match !pool with
328328- | Some pool ->
329329- Lwt.return pool
330330- | None ->
331331- if create = Some true then
332332- Util.mkfile_p Util.Constants.pegasus_db_filepath ~perm:0o644 ;
333333- let%lwt db =
334334- Util.connect_sqlite ?create ~write:true
335335- Util.Constants.pegasus_db_location
336336- in
337337- let%lwt () = Migrations.run_migrations Data_store db in
338338- pool := Some db ;
339339- Lwt.return db )
326326+ match !pool with
327327+ | Some pool ->
328328+ Lwt.return pool
329329+ | None ->
330330+ Lwt_mutex.with_lock pool_mutex (fun () ->
331331+ (* pool might've been initialized while we were waiting for the mutex *)
332332+ match !pool with
333333+ | Some pool ->
334334+ Lwt.return pool
335335+ | None ->
336336+ if create = Some true then
337337+ Util.mkfile_p Util.Constants.pegasus_db_filepath ~perm:0o644 ;
338338+ let%lwt db =
339339+ Util.connect_sqlite ?create ~write:true
340340+ Util.Constants.pegasus_db_location
341341+ in
342342+ let%lwt () = Migrations.run_migrations Data_store db in
343343+ pool := Some db ;
344344+ Lwt.return db )
340345341346let create_actor ~did ~handle ~email ~password ~signing_key conn =
342347 let password_hash = Bcrypt.hash password |> Bcrypt.string_of_hash in
+3-5
pegasus/lib/repository.ml
···502502 Dream.debug (fun l -> l "commit sequenced") ;
503503 Lwt.return {commit= new_commit; results} )
504504505505-let load ?create ?(ensure_active = false) ?ds did : t Lwt.t =
506506- let%lwt data_store_conn =
507507- match ds with Some ds -> Lwt.return ds | None -> Data_store.connect ()
508508- in
505505+let load ?create ?(ensure_active = false) did : t Lwt.t =
506506+ let%lwt ds_conn = Data_store.connect () in
509507 let%lwt user_db =
510508 try%lwt User_store.connect ?create did
511509 with _ ->
···513511 "your princess is in another castle"
514512 in
515513 let%lwt {signing_key; _} =
516516- match%lwt Data_store.get_actor_by_identifier did data_store_conn with
514514+ match%lwt Data_store.get_actor_by_identifier did ds_conn with
517515 | Some actor when ensure_active = false || actor.deactivated_at = None ->
518516 Lwt.return actor
519517 | Some _ ->
+18-13
pegasus/lib/user_store.ml
···315315let pool_cache_mutex = Lwt_mutex.create ()
316316317317let connect ?create did : t Lwt.t =
318318- Lwt_mutex.with_lock pool_cache_mutex (fun () ->
319319- match Hashtbl.find_opt pool_cache did with
320320- | Some cached ->
321321- Lwt.return cached
322322- | None ->
323323- let%lwt db =
324324- Util.connect_sqlite ?create ~write:true
325325- (Util.Constants.user_db_location did)
326326- in
327327- let%lwt () = Migrations.run_migrations User_store db in
328328- let t = {did; db} in
329329- Hashtbl.replace pool_cache did t ;
330330- Lwt.return t )
318318+ match Hashtbl.find_opt pool_cache did with
319319+ | Some cached ->
320320+ Lwt.return cached
321321+ | None ->
322322+ Lwt_mutex.with_lock pool_cache_mutex (fun () ->
323323+ (* pool might've been initialized while we were waiting for the mutex *)
324324+ match Hashtbl.find_opt pool_cache did with
325325+ | Some cached ->
326326+ Lwt.return cached
327327+ | None ->
328328+ let%lwt db =
329329+ Util.connect_sqlite ?create ~write:true
330330+ (Util.Constants.user_db_location did)
331331+ in
332332+ let%lwt () = Migrations.run_migrations User_store db in
333333+ let t = {did; db} in
334334+ Hashtbl.replace pool_cache did t ;
335335+ Lwt.return t )
331336332337(* mst blocks; implements Writable_blockstore *)
333338