From 403e86c138cec814d1a8c0f570b160f201ab6ea6 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Tue, 2 Mar 2021 21:43:13 -0800 Subject: [PATCH] index: introduce IndexStore, which owns ReadonlyIndex files This patch introduces a new `IndexStore` struct. The idea is that it will know about the directory in which the index files are stored, the associations with operations. It may also cache `Arc` instances so if multiple `ReadonlyIndex` instances are loaded, they can be returned from the cache. That may be useful when merging operations because the operations are likely to share a large parent index file. For now, however, all the new type has is `init()`, `load()`, and `reinit()`. --- lib/src/index.rs | 9 --------- lib/src/index_store.rs | 35 +++++++++++++++++++++++++++++++++++ lib/src/lib.rs | 1 + lib/src/repo.rs | 17 ++++++++++++----- 4 files changed, 48 insertions(+), 14 deletions(-) create mode 100644 lib/src/index_store.rs diff --git a/lib/src/index.rs b/lib/src/index.rs index f88296963..fa0bf8877 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -1352,15 +1352,6 @@ impl IndexEntry<'_> { } impl ReadonlyIndex { - pub fn init(dir: PathBuf) { - std::fs::create_dir(dir.join("operations")).unwrap(); - } - - pub fn reinit(dir: PathBuf) { - std::fs::remove_dir_all(dir.join("operations")).unwrap(); - ReadonlyIndex::init(dir); - } - pub fn load(repo: &ReadonlyRepo, dir: PathBuf, op_id: OperationId) -> Arc { let op_id_hex = op_id.hex(); let op_id_file = dir.join("operations").join(&op_id_hex); diff --git a/lib/src/index_store.rs b/lib/src/index_store.rs new file mode 100644 index 000000000..22989e63e --- /dev/null +++ b/lib/src/index_store.rs @@ -0,0 +1,35 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::path::PathBuf; + +pub struct IndexStore { + dir: PathBuf, +} + +impl IndexStore { + pub fn init(dir: PathBuf) -> Self { + std::fs::create_dir(dir.join("operations")).unwrap(); + IndexStore { dir } + } + + pub fn reinit(&self) { + std::fs::remove_dir_all(self.dir.join("operations")).unwrap(); + IndexStore::init(self.dir.clone()); + } + + pub fn load(dir: PathBuf) -> IndexStore { + IndexStore { dir } + } +} diff --git a/lib/src/lib.rs b/lib/src/lib.rs index e5e7ff3f4..db2d4b7a0 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -28,6 +28,7 @@ pub mod files; pub mod git; pub mod git_store; pub mod index; +pub mod index_store; pub mod local_store; pub mod lock; pub mod matchers; diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 4a9b43acc..99731d613 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -25,6 +25,7 @@ use crate::commit_builder::{new_change_id, signature}; use crate::evolution::{EvolutionRef, MutableEvolution, ReadonlyEvolution}; use crate::git_store::GitStore; use crate::index::{IndexRef, MutableIndex, ReadonlyIndex}; +use crate::index_store::IndexStore; use crate::local_store::LocalStore; use crate::op_store::OpStore; use crate::operation::Operation; @@ -99,6 +100,7 @@ pub struct ReadonlyRepo { wc_path: PathBuf, store: Arc, settings: RepoSettings, + index_store: IndexStore, index: Mutex>>, working_copy: Arc>, view: ReadonlyView, @@ -203,11 +205,15 @@ impl ReadonlyRepo { checkout_commit.id().clone(), ); + fs::create_dir(repo_path.join("index")).unwrap(); + let index_store = IndexStore::init(repo_path.join("index")); + let repo = ReadonlyRepo { - repo_path: repo_path.clone(), + repo_path, wc_path, store, settings: repo_settings, + index_store, index: Mutex::new(None), working_copy: Arc::new(Mutex::new(working_copy)), view, @@ -217,9 +223,6 @@ impl ReadonlyRepo { let repo_ref: &ReadonlyRepo = repo.as_ref(); let static_lifetime_repo: &'static ReadonlyRepo = unsafe { std::mem::transmute(repo_ref) }; - fs::create_dir(repo_path.join("index")).unwrap(); - ReadonlyIndex::init(repo_path.join("index")); - let evolution = ReadonlyEvolution::new(static_lifetime_repo); Arc::get_mut(&mut repo).unwrap().evolution = Some(evolution); @@ -279,7 +282,7 @@ impl ReadonlyRepo { } pub fn reindex(&mut self) -> Arc { - ReadonlyIndex::reinit(self.repo_path.join("index")); + self.index_store.reinit(); { let mut locked_index = self.index.lock().unwrap(); locked_index.take(); @@ -342,6 +345,7 @@ pub struct RepoLoader { repo_settings: RepoSettings, store: Arc, op_store: Arc, + index_store: IndexStore, } impl RepoLoader { @@ -354,12 +358,14 @@ impl RepoLoader { let store = RepoLoader::load_store(&repo_path); let repo_settings = user_settings.with_repo(&repo_path).unwrap(); let op_store: Arc = Arc::new(SimpleOpStore::load(repo_path.join("op_store"))); + let index_store = IndexStore::load(repo_path.join("index")); Ok(RepoLoader { wc_path, repo_path, repo_settings, store, op_store, + index_store, }) } @@ -418,6 +424,7 @@ impl RepoLoader { wc_path: self.wc_path, store: self.store, settings: self.repo_settings, + index_store: self.index_store, index: Mutex::new(None), working_copy: Arc::new(Mutex::new(working_copy)), view,