From 976b80120885c1ff8650df28dc0a012c96987988 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Tue, 30 Jan 2024 18:13:04 +0900 Subject: [PATCH] index: on reinit(), delete all segment files to save disk space Perhaps, reinit() will evolve to gc() function? It's basically a gc() with empty operation set. --- lib/src/default_index/store.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/src/default_index/store.rs b/lib/src/default_index/store.rs index c25cb45d1..f6ec39d40 100644 --- a/lib/src/default_index/store.rs +++ b/lib/src/default_index/store.rs @@ -39,6 +39,9 @@ use crate::op_store::{OpStoreError, OperationId}; use crate::operation::Operation; use crate::store::Store; +// BLAKE2b-512 hash length in hex string +const SEGMENT_FILE_NAME_LENGTH: usize = 64 * 2; + /// Error that may occur during `DefaultIndexStore` initialization. #[derive(Debug, Error)] #[error("Failed to initialize index store: {0}")] @@ -100,9 +103,21 @@ impl DefaultIndexStore { } pub fn reinit(&self) -> Result<(), DefaultIndexStoreInitError> { + // Remove all operation links to trigger rebuilding. let op_dir = self.dir.join("operations"); std::fs::remove_dir_all(&op_dir).context(&op_dir)?; std::fs::create_dir(&op_dir).context(&op_dir)?; + // Remove index segments to save disk space. If raced, new segment file + // will be created by the other process. + for entry in self.dir.read_dir().context(&self.dir)? { + let entry = entry.context(&self.dir)?; + let path = entry.path(); + if path.file_name().unwrap().len() != SEGMENT_FILE_NAME_LENGTH { + // Skip "type" file, "operations" directory, etc. + continue; + } + fs::remove_file(&path).context(&path)?; + } Ok(()) }