forked from mirrors/jj
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.
This commit is contained in:
parent
3d68601c01
commit
976b801208
1 changed files with 15 additions and 0 deletions
|
@ -39,6 +39,9 @@ use crate::op_store::{OpStoreError, OperationId};
|
||||||
use crate::operation::Operation;
|
use crate::operation::Operation;
|
||||||
use crate::store::Store;
|
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.
|
/// Error that may occur during `DefaultIndexStore` initialization.
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
#[error("Failed to initialize index store: {0}")]
|
#[error("Failed to initialize index store: {0}")]
|
||||||
|
@ -100,9 +103,21 @@ impl DefaultIndexStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reinit(&self) -> Result<(), DefaultIndexStoreInitError> {
|
pub fn reinit(&self) -> Result<(), DefaultIndexStoreInitError> {
|
||||||
|
// Remove all operation links to trigger rebuilding.
|
||||||
let op_dir = self.dir.join("operations");
|
let op_dir = self.dir.join("operations");
|
||||||
std::fs::remove_dir_all(&op_dir).context(&op_dir)?;
|
std::fs::remove_dir_all(&op_dir).context(&op_dir)?;
|
||||||
std::fs::create_dir(&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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue