From c4fe7aab106738e27863eb4986be4977462be11d Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Tue, 2 Mar 2021 22:48:08 -0800 Subject: [PATCH] index: move ReadonlyRepo::load_at_operation() to IndexStore --- lib/src/index.rs | 19 +------------------ lib/src/index_store.rs | 32 ++++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/lib/src/index.rs b/lib/src/index.rs index 86a316f15..d16d95bca 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -1283,7 +1283,7 @@ impl IndexEntry<'_> { } impl ReadonlyIndex { - fn load_from( + pub(crate) fn load_from( file: &mut dyn Read, dir: PathBuf, name: String, @@ -1344,23 +1344,6 @@ impl ReadonlyIndex { })) } - pub fn load_at_operation( - dir: PathBuf, - hash_length: usize, - op_id: &OperationId, - ) -> io::Result> { - let op_id_file = dir.join("operations").join(op_id.hex()); - let mut buf = vec![]; - File::open(op_id_file) - .unwrap() - .read_to_end(&mut buf) - .unwrap(); - let index_file_id_hex = String::from_utf8(buf).unwrap(); - let index_file_path = dir.join(&index_file_id_hex); - let mut index_file = File::open(&index_file_path).unwrap(); - ReadonlyIndex::load_from(&mut index_file, dir, index_file_id_hex, hash_length) - } - /// Records a link from the given operation to the this index version. pub fn associate_with_operation(&self, op_id: &OperationId) -> io::Result<()> { let mut temp_file = NamedTempFile::new_in(&self.dir)?; diff --git a/lib/src/index_store.rs b/lib/src/index_store.rs index 1f3ade893..7a506d185 100644 --- a/lib/src/index_store.rs +++ b/lib/src/index_store.rs @@ -21,7 +21,9 @@ use crate::repo::ReadonlyRepo; use crate::store::CommitId; use crate::store_wrapper::StoreWrapper; use std::collections::{HashMap, HashSet}; +use std::fs::File; use std::io; +use std::io::Read; use std::path::PathBuf; use std::sync::Arc; @@ -49,7 +51,7 @@ impl IndexStore { let op_id_file = self.dir.join("operations").join(&op_id_hex); if op_id_file.exists() { let op_id = OperationId(hex::decode(op_id_hex).unwrap()); - ReadonlyIndex::load_at_operation(self.dir.clone(), repo.store().hash_length(), &op_id) + self.load_index_at_operation(repo.store().hash_length(), &op_id) .unwrap() } else { let op = repo.view().as_view_ref().get_operation(&op_id).unwrap(); @@ -57,6 +59,28 @@ impl IndexStore { } } + fn load_index_at_operation( + &self, + hash_length: usize, + op_id: &OperationId, + ) -> io::Result> { + let op_id_file = self.dir.join("operations").join(op_id.hex()); + let mut buf = vec![]; + File::open(op_id_file) + .unwrap() + .read_to_end(&mut buf) + .unwrap(); + let index_file_id_hex = String::from_utf8(buf).unwrap(); + let index_file_path = self.dir.join(&index_file_id_hex); + let mut index_file = File::open(&index_file_path).unwrap(); + ReadonlyIndex::load_from( + &mut index_file, + self.dir.clone(), + index_file_id_hex, + hash_length, + ) + } + fn index_at_operation( &self, store: &StoreWrapper, @@ -90,9 +114,9 @@ impl IndexStore { data = MutableIndex::full(self.dir.clone(), hash_length); } Some(parent_op_id) => { - let parent_file = - ReadonlyIndex::load_at_operation(self.dir.clone(), hash_length, &parent_op_id) - .unwrap(); + let parent_file = self + .load_index_at_operation(hash_length, &parent_op_id) + .unwrap(); maybe_parent_file = Some(parent_file.clone()); data = MutableIndex::incremental(parent_file) }