use crate::tools;
use super::DataChunk;
+use crate::server::WorkerTask;
#[derive(Clone, Serialize)]
pub struct GarbageCollectionStatus {
pub fn get_chunk_iterator(
&self,
- print_percentage: bool,
) -> Result<
- impl Iterator<Item = Result<tools::fs::ReadDirEntry, Error>> + std::iter::FusedIterator,
+ impl Iterator<Item = (Result<tools::fs::ReadDirEntry, Error>, usize)> + std::iter::FusedIterator,
Error
> {
use nix::dir::Dir;
};
let mut verbose = true;
- let mut last_percentage = 0;
Ok((0..0x10000).filter_map(move |index| {
- if print_percentage {
- let percentage = (index * 100) / 0x10000;
- if last_percentage != percentage {
- last_percentage = percentage;
- eprintln!("percentage done: {}", percentage);
- }
- }
+ let percentage = (index * 100) / 0x10000;
let subdir: &str = &format!("{:04x}", index);
match tools::fs::read_subdir(base_handle.as_raw_fd(), subdir) {
Err(e) => {
}
None
}
- Ok(iter) => Some(iter),
+ Ok(iter) => Some(iter.map(move |item| (item, percentage))),
}
})
.flatten()
- .filter(|entry| {
+ .filter(|(entry, _percentage)| {
// Check that the file name is actually a hash! (64 hex digits)
let entry = match entry {
Err(_) => return true, // pass errors onwards
pub fn sweep_unused_chunks(
&self,
oldest_writer: Option<i64>,
- status: &mut GarbageCollectionStatus
+ status: &mut GarbageCollectionStatus,
+ worker: Arc<WorkerTask>,
) -> Result<(), Error> {
use nix::sys::stat::fstatat;
min_atime -= 300; // add 5 mins gap for safety
- for entry in self.get_chunk_iterator(true)? {
+ let mut last_percentage = 0;
+ for (entry, percentage) in self.get_chunk_iterator()? {
+ if last_percentage != percentage {
+ last_percentage = percentage;
+ worker.log(format!("percentage done: {}", percentage));
+ }
tools::fail_on_shutdown()?;
}
drop(lock);
}
+
Ok(())
}
pub fn get_chunk_iterator(
&self,
- print_percentage: bool,
) -> Result<
- impl Iterator<Item = Result<tools::fs::ReadDirEntry, Error>>,
+ impl Iterator<Item = (Result<tools::fs::ReadDirEntry, Error>, usize)>,
Error
> {
- self.chunk_store.get_chunk_iterator(print_percentage)
+ self.chunk_store.get_chunk_iterator()
}
pub fn create_fixed_writer<P: AsRef<Path>>(&self, filename: P, size: usize, chunk_size: usize) -> Result<FixedIndexWriter, Error> {
self.mark_used_chunks(&mut gc_status)?;
worker.log("Start GC phase2 (sweep unused chunks)");
- self.chunk_store.sweep_unused_chunks(oldest_writer, &mut gc_status)?;
+ self.chunk_store.sweep_unused_chunks(oldest_writer, &mut gc_status, worker.clone())?;
worker.log(&format!("Removed bytes: {}", gc_status.removed_bytes));
worker.log(&format!("Removed chunks: {}", gc_status.removed_chunks));