From ec071041268d5aa855e7c447d5f0abd3519f0a9d Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Wed, 10 Mar 2021 22:45:56 -0800 Subject: [PATCH] view: move creation of initial operation to OpHeadsStore This is a step towards getting rid of `MutableView::update_op_heads()`. --- lib/src/op_heads_store.rs | 22 ++++++++++++++++++++-- lib/src/view.rs | 19 ++++--------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/src/op_heads_store.rs b/lib/src/op_heads_store.rs index 71d0d5ad5..f27cffb36 100644 --- a/lib/src/op_heads_store.rs +++ b/lib/src/op_heads_store.rs @@ -24,6 +24,7 @@ use crate::view; use std::path::PathBuf; use std::sync::Arc; +use crate::store::CommitId; use thiserror::Error; /// Manages the very set of current heads of the operation log. The store is @@ -40,8 +41,25 @@ pub enum OpHeadResolutionError { } impl OpHeadsStore { - pub fn init(dir: PathBuf) -> Self { - OpHeadsStore { dir } + pub fn init( + dir: PathBuf, + op_store: &Arc, + checkout: CommitId, + ) -> (Self, OperationId, op_store::View) { + let mut root_view = op_store::View::new(checkout.clone()); + root_view.head_ids.insert(checkout); + let root_view_id = op_store.write_view(&root_view).unwrap(); + let operation_metadata = OperationMetadata::new("initialize repo".to_string()); + let init_operation = op_store::Operation { + view_id: root_view_id, + parents: vec![], + metadata: operation_metadata, + }; + let init_operation_id = op_store.write_operation(&init_operation).unwrap(); + + let op_heads_store = OpHeadsStore { dir }; + op_heads_store.add_op_head(&init_operation_id); + (op_heads_store, init_operation_id, root_view) } pub fn load(dir: PathBuf) -> OpHeadsStore { diff --git a/lib/src/view.rs b/lib/src/view.rs index 85dd6af36..2e0a0c443 100644 --- a/lib/src/view.rs +++ b/lib/src/view.rs @@ -219,27 +219,16 @@ impl ReadonlyView { path: PathBuf, checkout: CommitId, ) -> Self { - let mut root_view = op_store::View::new(checkout.clone()); - root_view.head_ids.insert(checkout); - let root_view_id = op_store.write_view(&root_view).unwrap(); - let operation_metadata = OperationMetadata::new("initialize repo".to_string()); - let init_operation = op_store::Operation { - view_id: root_view_id, - parents: vec![], - metadata: operation_metadata, - }; - let init_operation_id = op_store.write_operation(&init_operation).unwrap(); - let op_heads_dir = path.join("op_heads"); std::fs::create_dir(&op_heads_dir).unwrap(); - let op_heads_store = Arc::new(OpHeadsStore::init(op_heads_dir)); - op_heads_store.add_op_head(&init_operation_id); + let (op_heads_store, init_op_id, root_view) = + OpHeadsStore::init(op_heads_dir, &op_store, checkout); ReadonlyView { store, op_store, - op_heads_store, - op_id: init_operation_id, + op_heads_store: Arc::new(op_heads_store), + op_id: init_op_id, index_store, data: root_view, }