Skip to content

Commit 5885442

Browse files
committed
Don't traverse mst just to list records/collections
1 parent 2a8ed6b commit 5885442

File tree

2 files changed

+14
-50
lines changed

2 files changed

+14
-50
lines changed

pegasus/lib/repository.ml

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -147,57 +147,17 @@ type t =
147147
{ key: Kleidos.key
148148
; did: string
149149
; db: User_store.t
150-
; mutable block_map: Cid.t String_map.t option
151150
; mutable commit: (Cid.t * signed_commit) option }
152151

153-
let get_map t : Cid.t String_map.t Lwt.t =
154-
let%lwt root, commit =
155-
match%lwt User_store.get_commit t.db with
156-
| Some (r, c) ->
157-
Lwt.return (r, c)
158-
| None ->
159-
failwith ("failed to retrieve commit for " ^ t.did)
160-
in
161-
t.commit <- Some (root, commit) ;
162-
match t.block_map with
163-
| Some map ->
164-
Lwt.return map
165-
| _ ->
166-
let%lwt map = Mst.build_map {blockstore= t.db; root= commit.data} in
167-
t.block_map <- Some map ;
168-
Lwt.return map
169-
170152
let get_record_cid t path : Cid.t option Lwt.t =
171-
let%lwt map = get_map t in
172-
Lwt.return @@ String_map.find_opt path map
153+
User_store.get_record_cid t.db path
173154

174155
let get_record t path : record option Lwt.t = User_store.get_record t.db path
175156

176-
let list_collections t : string list Lwt.t =
177-
let module Set = Set.Make (String) in
178-
let%lwt map = get_map t in
179-
String_map.bindings map
180-
|> List.fold_left
181-
(fun (acc : Set.t) (path, _) ->
182-
let collection = String.split_on_char '/' path |> List.hd in
183-
Set.add collection acc )
184-
Set.empty
185-
|> Set.to_list |> Lwt.return
157+
let list_collections t : string list Lwt.t = User_store.list_collections t.db
186158

187-
let list_all_records t collection : (string * Cid.t * record) list Lwt.t =
188-
let%lwt map = get_map t in
189-
String_map.bindings map
190-
|> List.filter (fun (path, _) ->
191-
String.starts_with ~prefix:(path ^ "/") collection )
192-
|> Lwt_list.fold_left_s
193-
(fun acc (path, cid) ->
194-
match%lwt User_store.get_record t.db path with
195-
| Some record ->
196-
Lwt.return
197-
((Format.sprintf "at://%s/%s" t.did path, cid, record) :: acc)
198-
| None ->
199-
Lwt.return acc )
200-
[]
159+
let list_records t ?limit ?cursor ?reverse collection =
160+
User_store.list_records t.db ?limit ?cursor ?reverse collection
201161

202162
let sign_commit t commit : signed_commit =
203163
let msg = commit |> commit_to_yojson |> Dag_cbor.encode_yojson in
@@ -232,8 +192,6 @@ let put_commit t ?(previous : signed_commit option = None) mst_root :
232192
User_store.put_commit t.db signed |> Lwt_result.get_exn
233193
in
234194
t.commit <- Some (commit_cid, signed) ;
235-
(* clear cached blocks so next get_map call rebuilds from the new commit *)
236-
t.block_map <- None ;
237195
Lwt.return (commit_cid, signed)
238196

239197
let put_initial_commit t : (Cid.t * signed_commit) Lwt.t =
@@ -261,7 +219,6 @@ let apply_writes (t : t) (writes : repo_write list) (swap_commit : Cid.t option)
261219
let mst : Cached_mst.t ref =
262220
ref (Cached_mst.create cached_store prev_commit.data)
263221
in
264-
t.block_map <- None ;
265222
(* ops to emit, built in loop because prev_data (previous cid) is otherwise inaccessible *)
266223
let commit_ops : commit_evt_op list ref = ref [] in
267224
let added_leaves = ref Block_map.empty in
@@ -477,7 +434,7 @@ let load ?create ?(ensure_active = false) did : t Lwt.t =
477434
in
478435
let key = Kleidos.parse_multikey_str signing_key in
479436
let%lwt commit = User_store.get_commit user_db in
480-
Lwt.return {key; did; db= user_db; block_map= None; commit}
437+
Lwt.return {key; did; db= user_db; commit}
481438

482439
let export_car t : Car.stream Lwt.t =
483440
let%lwt root, commit =
@@ -629,8 +586,6 @@ let import_car t (stream : Car.stream) : (t, exn) Lwt_result.t =
629586
let$! () = User_store.Bulk.put_blob_refs blob_refs conn in
630587
Lwt.return_ok () ) )
631588
in
632-
(* clear cached block_map so it's rebuilt on next access *)
633-
t.block_map <- None ;
634589
t.commit <- Some (root, commit) ;
635590
Lwt.return_ok t
636591
with exn -> Lwt.return_error exn

pegasus/lib/user_store.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ module Queries = struct
162162
let count_records =
163163
[%rapper get_one {sql| SELECT @int{COUNT(*)} FROM records |sql}]
164164
165+
let list_collections =
166+
[%rapper
167+
get_many
168+
{sql| SELECT DISTINCT SUBSTR(path, 1, INSTR(path, '/') - 1) AS @string{collection} FROM records |sql}]
169+
()
170+
165171
let list_records_reverse =
166172
[%rapper
167173
get_many
@@ -440,6 +446,9 @@ let list_records t ?(limit = 100) ?(cursor = "") ?(reverse = false) collection :
440446
441447
let count_records t : int Lwt.t = Util.use_pool t.db @@ Queries.count_records ()
442448
449+
let list_collections t : string list Lwt.t =
450+
Util.use_pool t.db @@ Queries.list_collections
451+
443452
let put_record t record path : (Cid.t * bytes) Lwt.t =
444453
let cid, data = Lex.to_cbor_block record in
445454
let since = Tid.now () in

0 commit comments

Comments
 (0)