view: move locking of .jj/view/op_heads/ to OpHeadsStore

This commit is contained in:
Martin von Zweigbergk 2021-03-10 21:24:54 -08:00
parent 4bd121dab5
commit b4d4cd143a
2 changed files with 9 additions and 21 deletions

View file

@ -57,8 +57,12 @@ impl OpHeadsStore {
op_heads op_heads
} }
pub fn lock(&self) -> FileLock {
FileLock::lock(self.dir.join("lock"))
}
pub fn update_op_heads(&self, op: &Operation) { pub fn update_op_heads(&self, op: &Operation) {
let _op_heads_lock = FileLock::lock(self.dir.join("lock")); let _op_heads_lock = self.lock();
self.add_op_head(op.id()); self.add_op_head(op.id());
for old_parent_id in op.parent_ids() { for old_parent_id in op.parent_ids() {
self.remove_op_head(old_parent_id); self.remove_op_head(old_parent_id);

View file

@ -14,7 +14,6 @@
use std::cmp::min; use std::cmp::min;
use std::collections::{BTreeMap, HashSet}; use std::collections::{BTreeMap, HashSet};
use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
@ -24,7 +23,6 @@ use crate::commit::Commit;
use crate::dag_walk; use crate::dag_walk;
use crate::index::MutableIndex; use crate::index::MutableIndex;
use crate::index_store::IndexStore; use crate::index_store::IndexStore;
use crate::lock::FileLock;
use crate::op_heads_store::OpHeadsStore; use crate::op_heads_store::OpHeadsStore;
use crate::op_store; use crate::op_store;
use crate::op_store::{OpStore, OpStoreResult, OperationId, OperationMetadata}; use crate::op_store::{OpStore, OpStoreResult, OperationId, OperationMetadata};
@ -92,7 +90,6 @@ impl<'a> ViewRef<'a> {
pub struct ReadonlyView { pub struct ReadonlyView {
store: Arc<StoreWrapper>, store: Arc<StoreWrapper>,
path: PathBuf,
op_store: Arc<dyn OpStore>, op_store: Arc<dyn OpStore>,
op_heads_store: Arc<OpHeadsStore>, op_heads_store: Arc<OpHeadsStore>,
op_id: OperationId, op_id: OperationId,
@ -231,7 +228,6 @@ fn get_single_op_head(
op_store: &Arc<dyn OpStore>, op_store: &Arc<dyn OpStore>,
index_store: &Arc<IndexStore>, index_store: &Arc<IndexStore>,
op_head_store: &Arc<OpHeadsStore>, op_head_store: &Arc<OpHeadsStore>,
op_heads_dir: &Path,
) -> Result<(OperationId, op_store::Operation, op_store::View), OpHeadResolutionError> { ) -> Result<(OperationId, op_store::Operation, op_store::View), OpHeadResolutionError> {
let mut op_heads = op_head_store.get_op_heads(); let mut op_heads = op_head_store.get_op_heads();
@ -254,8 +250,7 @@ fn get_single_op_head(
// Note that the locking isn't necessary for correctness; we take the lock // Note that the locking isn't necessary for correctness; we take the lock
// only to avoid other concurrent processes from doing the same work (and // only to avoid other concurrent processes from doing the same work (and
// producing another set of divergent heads). // producing another set of divergent heads).
// TODO: Add a function to OpHeadsStore for taking the lock? let _lock = op_head_store.lock();
let _lock = FileLock::lock(op_heads_dir.join("lock"));
let op_heads = op_head_store.get_op_heads(); let op_heads = op_head_store.get_op_heads();
if op_heads.is_empty() { if op_heads.is_empty() {
@ -376,7 +371,6 @@ impl ReadonlyView {
ReadonlyView { ReadonlyView {
store, store,
path,
op_store, op_store,
op_heads_store, op_heads_store,
op_id: init_operation_id, op_id: init_operation_id,
@ -392,18 +386,11 @@ impl ReadonlyView {
path: PathBuf, path: PathBuf,
) -> Self { ) -> Self {
let op_heads_dir = path.join("op_heads"); let op_heads_dir = path.join("op_heads");
let op_heads_store = Arc::new(OpHeadsStore::load(op_heads_dir.clone())); let op_heads_store = Arc::new(OpHeadsStore::load(op_heads_dir));
let (op_id, _operation, view) = get_single_op_head( let (op_id, _operation, view) =
&store, get_single_op_head(&store, &op_store, &index_store, &op_heads_store).unwrap();
&op_store,
&index_store,
&op_heads_store,
&op_heads_dir,
)
.unwrap();
ReadonlyView { ReadonlyView {
store, store,
path,
op_store, op_store,
op_heads_store, op_heads_store,
op_id, op_id,
@ -423,7 +410,6 @@ impl ReadonlyView {
let op_heads_store = Arc::new(OpHeadsStore::load(op_heads_dir)); let op_heads_store = Arc::new(OpHeadsStore::load(op_heads_dir));
ReadonlyView { ReadonlyView {
store, store,
path,
op_store, op_store,
op_heads_store, op_heads_store,
op_id: operation.id().clone(), op_id: operation.id().clone(),
@ -433,13 +419,11 @@ impl ReadonlyView {
} }
pub fn reload(&mut self) -> OperationId { pub fn reload(&mut self) -> OperationId {
let op_heads_dir = self.path.join("op_heads");
let (op_id, _operation, view) = get_single_op_head( let (op_id, _operation, view) = get_single_op_head(
&self.store, &self.store,
&self.op_store, &self.op_store,
&self.index_store, &self.index_store,
&self.op_heads_store, &self.op_heads_store,
&op_heads_dir,
) )
.unwrap(); .unwrap();
self.op_id = op_id; self.op_id = op_id;