index: extract function that opens file and loads index segments

This commit is contained in:
Yuya Nishihara 2023-12-17 13:44:21 +09:00
parent eccb9b7a44
commit 88f3085bb1
2 changed files with 13 additions and 11 deletions

View file

@ -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<Arc<ReadonlyIndexSegment>, 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,

View file

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