From 88f3085bb134d7c1a2e12bf46376d26f298bd99d Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sun, 17 Dec 2023 13:44:21 +0900 Subject: [PATCH] index: extract function that opens file and loads index segments --- lib/src/default_index/readonly.rs | 16 ++++++++++++---- lib/src/default_index/store.rs | 8 +------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/src/default_index/readonly.rs b/lib/src/default_index/readonly.rs index 40d6df8f7..5e33254ba 100644 --- a/lib/src/default_index/readonly.rs +++ b/lib/src/default_index/readonly.rs @@ -169,6 +169,17 @@ impl Debug for ReadonlyIndexSegment { } impl ReadonlyIndexSegment { + /// Loads both parent segments and local entries from the given file `name`. + pub(super) fn load( + dir: &Path, + name: String, + commit_id_length: usize, + change_id_length: usize, + ) -> Result, ReadonlyIndexLoadError> { + let mut file = File::open(dir.join(&name))?; + Self::load_from(&mut file, dir, name, commit_id_length, change_id_length) + } + /// Loads both parent segments and local entries from the given `file`. pub(super) fn load_from( file: &mut dyn Read, @@ -183,10 +194,7 @@ impl ReadonlyIndexSegment { file.read_exact(&mut parent_filename_bytes)?; let parent_filename = String::from_utf8(parent_filename_bytes) .map_err(|_| ReadonlyIndexLoadError::IndexCorrupt(name.to_owned()))?; - let parent_file_path = dir.join(&parent_filename); - let mut index_file = File::open(parent_file_path)?; - let parent_file = ReadonlyIndexSegment::load_from( - &mut index_file, + let parent_file = ReadonlyIndexSegment::load( dir, parent_filename, commit_id_length, diff --git a/lib/src/default_index/store.rs b/lib/src/default_index/store.rs index b0748e38f..df274562b 100644 --- a/lib/src/default_index/store.rs +++ b/lib/src/default_index/store.rs @@ -15,7 +15,6 @@ #![allow(missing_docs)] use std::any::Any; -use std::fs::File; use std::io::Write; use std::path::{Path, PathBuf}; use std::sync::Arc; @@ -93,12 +92,7 @@ impl DefaultIndexStore { let op_id_file = self.dir.join("operations").join(op_id.hex()); let index_file_id_hex = fs::read_to_string(op_id_file).map_err(DefaultIndexStoreError::LoadAssociation)?; - let index_file_path = self.dir.join(&index_file_id_hex); - let mut index_file = File::open(index_file_path).map_err(|err| { - DefaultIndexStoreError::LoadIndex(ReadonlyIndexLoadError::IoError(err)) - })?; - ReadonlyIndexSegment::load_from( - &mut index_file, + ReadonlyIndexSegment::load( &self.dir, index_file_id_hex, commit_id_length,