From c1de8b0f3a16839ad8e47e078d588659d3a32961 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 13 Mar 2021 21:23:30 -0800 Subject: [PATCH] View: move creation of Operation to Transaction This continues the work to make the `View` types be only about the state of the current view and not about operations in general (which has been moved out `OpStore` and qOpHeadsStore`). --- lib/src/op_heads_store.rs | 6 +++--- lib/src/op_store.rs | 8 ++++---- lib/src/transaction.rs | 18 ++++++++++++++---- lib/src/view.rs | 17 ++++------------- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/lib/src/op_heads_store.rs b/lib/src/op_heads_store.rs index 80796bd1e..25fcc0215 100644 --- a/lib/src/op_heads_store.rs +++ b/lib/src/op_heads_store.rs @@ -25,7 +25,7 @@ use std::path::PathBuf; use std::sync::Arc; use crate::repo::RepoLoader; -use crate::store::CommitId; +use crate::store::{CommitId, Timestamp}; use std::collections::HashSet; use thiserror::Error; @@ -51,7 +51,7 @@ impl OpHeadsStore { 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 operation_metadata = OperationMetadata::new("initialize repo".to_string(), Timestamp::now()); let init_operation = op_store::Operation { view_id: root_view_id, parents: vec![], @@ -216,7 +216,7 @@ fn merge_op_heads( } let merged_index = index_store.write_index(index).unwrap(); let merged_view_id = op_store.write_view(&merged_view).unwrap(); - let operation_metadata = OperationMetadata::new("resolve concurrent operations".to_string()); + let operation_metadata = OperationMetadata::new("resolve concurrent operations".to_string(), Timestamp::now()); let op_parent_ids = op_heads.iter().map(|op| op.id().clone()).collect(); let merge_operation = op_store::Operation { view_id: merged_view_id, diff --git a/lib/src/op_store.rs b/lib/src/op_store.rs index 9307ab281..7505ee61e 100644 --- a/lib/src/op_store.rs +++ b/lib/src/op_store.rs @@ -103,13 +103,13 @@ pub struct OperationMetadata { } impl OperationMetadata { - pub fn new(description: String) -> Self { - let timestamp = Timestamp::now(); + pub fn new(description: String, start_time: Timestamp) -> Self { + let end_time = Timestamp::now(); let hostname = whoami::hostname(); let username = whoami::username(); OperationMetadata { - start_time: timestamp.clone(), - end_time: timestamp, + start_time, + end_time, description, hostname, username, diff --git a/lib/src/transaction.rs b/lib/src/transaction.rs index b416fbc2b..13916ecb1 100644 --- a/lib/src/transaction.rs +++ b/lib/src/transaction.rs @@ -25,6 +25,7 @@ use crate::store::{CommitId, Timestamp}; use crate::store_wrapper::StoreWrapper; use crate::view::MutableView; use std::sync::Arc; +use crate::op_store::OperationMetadata; pub struct Transaction<'r> { repo: Option>>, @@ -132,11 +133,20 @@ impl<'r> Transaction<'r> { pub fn write(mut self) -> UnpublishedOperation { let mut_repo = Arc::try_unwrap(self.repo.take().unwrap()).ok().unwrap(); let base_repo = mut_repo.base_repo(); - let index_store = base_repo.index_store(); let (mut_index, mut_view) = mut_repo.consume(); - let index = index_store.write_index(mut_index).unwrap(); - let operation = mut_view.save(self.description.clone(), self.start_time.clone()); - index_store + let index = base_repo.index_store().write_index(mut_index).unwrap(); + + let view_id = base_repo.op_store().write_view(mut_view.store_view()).unwrap(); + let operation_metadata = OperationMetadata::new(self.description.clone(),self.start_time.clone()); + let store_operation = op_store::Operation { + view_id, + parents: vec![base_repo.view().op_id().clone()], + metadata: operation_metadata, + }; + let new_op_id = base_repo.op_store().write_operation(&store_operation).unwrap(); + let operation = Operation::new(base_repo.op_store().clone(), new_op_id, store_operation); + + base_repo.index_store() .associate_file_with_operation(&index, operation.id()) .unwrap(); self.closed = true; diff --git a/lib/src/view.rs b/lib/src/view.rs index b0c6837b9..f48596106 100644 --- a/lib/src/view.rs +++ b/lib/src/view.rs @@ -18,9 +18,9 @@ use std::sync::Arc; use crate::commit::Commit; use crate::op_store; -use crate::op_store::{OpStore, OpStoreResult, OperationId, OperationMetadata}; +use crate::op_store::{OpStore, OpStoreResult, OperationId}; use crate::operation::Operation; -use crate::store::{CommitId, Timestamp}; +use crate::store::{CommitId}; use crate::store_wrapper::StoreWrapper; pub enum ViewRef<'a> { @@ -319,16 +319,7 @@ impl MutableView { enforce_invariants(&self.store, &mut self.data); } - pub fn save(&self, description: String, operation_start_time: Timestamp) -> Operation { - let view_id = self.op_store.write_view(&self.data).unwrap(); - let mut operation_metadata = OperationMetadata::new(description); - operation_metadata.start_time = operation_start_time; - let operation = op_store::Operation { - view_id, - parents: vec![self.base_op_id.clone()], - metadata: operation_metadata, - }; - let new_op_id = self.op_store.write_operation(&operation).unwrap(); - Operation::new(self.op_store.clone(), new_op_id, operation) + pub fn store_view(&self) -> &op_store::View { + &self.data } }