objective categorical abstract machine language personal data server
65
fork

Configure Feed

Select the types of activity you want to include in your feed.

Don't traverse mst just to list records/collections

futurGH 5885442d 2a8ed6b2

+14 -50
+5 -50
pegasus/lib/repository.ml
··· 147 147 { key: Kleidos.key 148 148 ; did: string 149 149 ; db: User_store.t 150 - ; mutable block_map: Cid.t String_map.t option 151 150 ; mutable commit: (Cid.t * signed_commit) option } 152 151 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 - 170 152 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 173 154 174 155 let get_record t path : record option Lwt.t = User_store.get_record t.db path 175 156 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 186 158 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 201 161 202 162 let sign_commit t commit : signed_commit = 203 163 let msg = commit |> commit_to_yojson |> Dag_cbor.encode_yojson in ··· 232 192 User_store.put_commit t.db signed |> Lwt_result.get_exn 233 193 in 234 194 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 ; 237 195 Lwt.return (commit_cid, signed) 238 196 239 197 let put_initial_commit t : (Cid.t * signed_commit) Lwt.t = ··· 261 219 let mst : Cached_mst.t ref = 262 220 ref (Cached_mst.create cached_store prev_commit.data) 263 221 in 264 - t.block_map <- None ; 265 222 (* ops to emit, built in loop because prev_data (previous cid) is otherwise inaccessible *) 266 223 let commit_ops : commit_evt_op list ref = ref [] in 267 224 let added_leaves = ref Block_map.empty in ··· 477 434 in 478 435 let key = Kleidos.parse_multikey_str signing_key in 479 436 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} 481 438 482 439 let export_car t : Car.stream Lwt.t = 483 440 let%lwt root, commit = ··· 629 586 let$! () = User_store.Bulk.put_blob_refs blob_refs conn in 630 587 Lwt.return_ok () ) ) 631 588 in 632 - (* clear cached block_map so it's rebuilt on next access *) 633 - t.block_map <- None ; 634 589 t.commit <- Some (root, commit) ; 635 590 Lwt.return_ok t 636 591 with exn -> Lwt.return_error exn
+9
pegasus/lib/user_store.ml
··· 162 162 let count_records = 163 163 [%rapper get_one {sql| SELECT @int{COUNT(*)} FROM records |sql}] 164 164 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 + 165 171 let list_records_reverse = 166 172 [%rapper 167 173 get_many ··· 439 445 {path; cid; value= Lex.of_cbor data; since} ) 440 446 441 447 let count_records t : int Lwt.t = Util.use_pool t.db @@ Queries.count_records () 448 + 449 + let list_collections t : string list Lwt.t = 450 + Util.use_pool t.db @@ Queries.list_collections 442 451 443 452 let put_record t record path : (Cid.t * bytes) Lwt.t = 444 453 let cid, data = Lex.to_cbor_block record in