@@ -592,20 +592,9 @@ let import_car t (stream : Car.stream) : (t, exn) Lwt_result.t =
592592 failwith (" invalid commit: " ^ e)
593593 in
594594 if commit.did <> t.did then failwith " did does not match commit did" ;
595- (* create in-memory mst to walk *)
596- let mem_bs = Mist.Storage.Memory_blockstore. create ~blocks: all_blocks () in
597- let mem_mst : Mem_mst.t = {blockstore= mem_bs; root= commit.data} in
598- let % lwt leaves = Mem_mst. leaves_of_root mem_mst in
599- let leaf_cids =
600- List. fold_left
601- (fun acc (_ , cid ) -> Cid.Set. add cid acc)
602- Cid.Set. empty leaves
603- in
604- (* get mst nodes by filtering out leaves and commit from all blocks *)
595+ let leaves = Mist.Mst. leaves_from_blocks all_blocks commit.data in
605596 let mst_node_cids =
606- Block_map. keys all_blocks
607- |> List. filter (fun cid ->
608- (not (Cid. equal cid root)) && not (Cid.Set. mem cid leaf_cids) )
597+ Mist.Mst. mst_node_cids_from_blocks all_blocks commit.data
609598 in
610599 (* collect mst node blocks for insert *)
611600 let mst_blocks =
@@ -620,23 +609,23 @@ let import_car t (stream : Car.stream) : (t, exn) Lwt_result.t =
620609 in
621610 (* collect record data for insert *)
622611 let since = Tid. now () in
623- let blob_refs : (string * Cid.t) list ref = ref [] in
624- let record_data =
625- List. map
626- (fun (path , cid ) ->
612+ let record_data, blob_refs =
613+ List. fold_left
614+ (fun (acc_data , acc_refs ) (path , cid ) ->
627615 match Block_map. get cid all_blocks with
628616 | Some data ->
629617 let record = Lex. of_cbor data in
630618 let record_refs =
631619 Util. find_blob_refs record
632620 |> List. map (fun (br : Mist.Blob_ref.t ) -> (path, br.ref ))
633621 in
634- blob_refs := record_refs @ ! blob_refs ;
635- (path, cid, data, since )
622+ ( (path, cid, data, since) :: acc_data
623+ , List. rev_append record_refs acc_refs )
636624 | None ->
637625 failwith (" missing record block: " ^ Cid. to_string cid) )
638- leaves
626+ ( [] , [] ) leaves
639627 in
628+ let record_data = List. rev record_data in
640629 let % lwt _ =
641630 Util. use_pool t.db.db (fun conn ->
642631 Util. transact conn (fun () ->
@@ -645,7 +634,7 @@ let import_car t (stream : Car.stream) : (t, exn) Lwt_result.t =
645634 let $! () = User_store.Bulk. put_blocks mst_blocks conn in
646635 let $! () =
647636 [% rapper execute {sql| DELETE FROM records | sql}] () conn
648- in
637+ in
649638 let $! () = User_store.Bulk. put_records record_data conn in
650639 let $! () = User_store.Bulk. put_blob_refs ! blob_refs conn in
651640 Lwt. return_ok () ) )
0 commit comments