From 14e7df995ae0844c2220484a69d036061549c45f Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Thu, 17 Dec 2020 23:03:07 -0800 Subject: [PATCH] index: move static functions from Index to IndexFile and delete it The Index struct no longer has any state, so it's not needed. --- lib/src/index.rs | 50 ++++++++++++++++++++++-------------------------- lib/src/repo.rs | 8 ++++---- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/lib/src/index.rs b/lib/src/index.rs index f017d80de..69f5f75bf 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -908,6 +908,29 @@ impl IndexEntry<'_> { } impl IndexFile { + 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(); + IndexFile::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); + let index_file = if op_id_file.exists() { + let op_id = OperationId(hex::decode(op_id_hex).unwrap()); + IndexFile::load_at_operation(&dir, repo.store().hash_length(), &op_id).unwrap() + } else { + let op = repo.view().get_operation(&op_id).unwrap(); + IndexFile::index(repo.store(), &dir, &op).unwrap() + }; + + Arc::new(index_file) + } + fn load_from( file: &mut dyn Read, dir: &Path, @@ -1103,33 +1126,6 @@ impl IndexFile { } } -pub struct Index; - -impl Index { - 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(); - Index::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); - let index_file = if op_id_file.exists() { - let op_id = OperationId(hex::decode(op_id_hex).unwrap()); - IndexFile::load_at_operation(&dir, repo.store().hash_length(), &op_id).unwrap() - } else { - let op = repo.view().get_operation(&op_id).unwrap(); - IndexFile::index(repo.store(), &dir, &op).unwrap() - }; - - Arc::new(index_file) - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 683a783ff..d69afa909 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -24,7 +24,7 @@ use thiserror::Error; use crate::commit_builder::{new_change_id, signature}; use crate::evolution::{Evolution, ReadonlyEvolution}; use crate::git_store::GitStore; -use crate::index::{Index, IndexFile}; +use crate::index::IndexFile; use crate::local_store::LocalStore; use crate::operation::Operation; use crate::settings::{RepoSettings, UserSettings}; @@ -155,7 +155,7 @@ impl ReadonlyRepo { let static_lifetime_repo: &'static ReadonlyRepo = unsafe { std::mem::transmute(repo_ref) }; fs::create_dir(repo_path.join("index")).unwrap(); - Index::init(repo_path.join("index")); + IndexFile::init(repo_path.join("index")); let evolution = ReadonlyEvolution::new(static_lifetime_repo); @@ -221,13 +221,13 @@ impl ReadonlyRepo { let mut locked_index = self.index.lock().unwrap(); if locked_index.is_none() { let op_id = self.view.base_op_head_id().clone(); - locked_index.replace(Index::load(self, self.repo_path.join("index"), op_id)); + locked_index.replace(IndexFile::load(self, self.repo_path.join("index"), op_id)); } locked_index.as_ref().unwrap().clone() } pub fn reindex(&mut self) -> Arc { - Index::reinit(self.repo_path.join("index")); + IndexFile::reinit(self.repo_path.join("index")); { let mut locked_index = self.index.lock().unwrap(); locked_index.take();