ok/jj
1
0
Fork 0
forked from mirrors/jj

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<ReadonlyIndex>`
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()`.
This commit is contained in:
Martin von Zweigbergk 2021-03-02 21:43:13 -08:00
parent 0a4ef1030f
commit 403e86c138
4 changed files with 48 additions and 14 deletions

View file

@ -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<ReadonlyIndex> {
let op_id_hex = op_id.hex();
let op_id_file = dir.join("operations").join(&op_id_hex);

35
lib/src/index_store.rs Normal file
View file

@ -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 }
}
}

View file

@ -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;

View file

@ -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<StoreWrapper>,
settings: RepoSettings,
index_store: IndexStore,
index: Mutex<Option<Arc<ReadonlyIndex>>>,
working_copy: Arc<Mutex<WorkingCopy>>,
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> {
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<StoreWrapper>,
op_store: Arc<dyn OpStore>,
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<dyn OpStore> = 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,