zvault/src/repository/vacuum.rs

58 lines
2.0 KiB
Rust
Raw Normal View History

2017-07-21 09:21:59 +00:00
use prelude::*;
2017-03-21 10:28:11 +00:00
2018-03-10 15:35:40 +00:00
use std::collections::HashMap;
2017-03-20 13:03:29 +00:00
impl Repository {
2018-03-10 15:35:40 +00:00
pub fn delete_bundle(&mut self, id: u32) -> Result<(), RepositoryError> {
2017-03-20 13:03:29 +00:00
if let Some(bundle) = self.bundle_map.remove(id) {
2017-03-22 11:27:17 +00:00
try!(self.bundles.delete_bundle(&bundle));
2017-03-20 13:03:29 +00:00
Ok(())
} else {
Err(IntegrityError::MissingBundleId(id).into())
2017-03-20 13:03:29 +00:00
}
}
2018-03-10 15:35:40 +00:00
pub fn rewrite_bundles(&mut self, rewrite_bundles: &[u32], usage: &HashMap<u32, BundleAnalysis>) -> Result<(), RepositoryError> {
for &id in ProgressIter::new(
2018-02-24 12:19:51 +00:00
tr!("rewriting bundles"),
2017-07-21 09:21:59 +00:00
rewrite_bundles.len(),
rewrite_bundles.iter()
)
2018-03-10 15:35:40 +00:00
{
let bundle = &usage[&id];
let bundle_id = self.bundle_map.get(id).unwrap();
let chunks = try!(self.bundles.get_chunk_list(&bundle_id));
let mode = usage[&id].info.mode;
for (chunk, &(hash, _len)) in chunks.into_iter().enumerate() {
if !bundle.chunk_usage.get(chunk) {
try!(self.index.delete(&hash));
continue;
}
let data = try!(self.bundles.get_chunk(&bundle_id, chunk));
try!(self.put_chunk_override(mode, hash, &data));
2017-03-20 13:03:29 +00:00
}
}
try!(self.flush());
2018-02-24 12:19:51 +00:00
tr_info!("Checking index");
for (hash, location) in self.index.iter() {
2018-02-19 21:30:59 +00:00
let loc_bundle = location.bundle;
let loc_chunk = location.chunk;
if rewrite_bundles.contains(&loc_bundle) {
2018-02-24 12:19:51 +00:00
tr_panic!(
2017-07-21 09:21:59 +00:00
"Removed bundle is still referenced in index: hash:{}, bundle:{}, chunk:{}",
hash,
2018-02-19 21:30:59 +00:00
loc_bundle,
loc_chunk
2017-07-21 09:21:59 +00:00
);
2017-03-20 13:03:29 +00:00
}
}
2018-02-24 12:19:51 +00:00
tr_info!("Deleting {} bundles", rewrite_bundles.len());
2018-03-10 15:35:40 +00:00
for &id in rewrite_bundles {
2017-03-20 13:03:29 +00:00
try!(self.delete_bundle(id));
}
2017-04-08 12:35:10 +00:00
try!(self.save_bundle_map());
2017-03-20 13:03:29 +00:00
Ok(())
}
}