From 4c07e6d5d6246ef2c6d50e84eed70ee763d8139e Mon Sep 17 00:00:00 2001 From: Dennis Schwerdel Date: Sat, 1 Apr 2017 18:43:36 +0200 Subject: [PATCH] Local bundle cache --- src/bundledb/db.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/bundledb/db.rs b/src/bundledb/db.rs index 4de6852..0f6009a 100644 --- a/src/bundledb/db.rs +++ b/src/bundledb/db.rs @@ -113,6 +113,7 @@ pub struct BundleDb { local_bundles_path: PathBuf, temp_path: PathBuf, remote_cache_path: PathBuf, + local_cache_path: PathBuf, crypto: Arc>, local_bundles: HashMap, remote_bundles: HashMap, @@ -123,7 +124,8 @@ pub struct BundleDb { impl BundleDb { fn new(remote_path: PathBuf, local_path: PathBuf, crypto: Arc>) -> Self { BundleDb { - remote_cache_path: local_path.join("bundle_info.cache"), + remote_cache_path: local_path.join("remote.cache"), + local_cache_path: local_path.join("local.cache"), local_bundles_path: local_path.join("cached"), temp_path: local_path.join("temp"), remote_path: remote_path, @@ -135,22 +137,34 @@ impl BundleDb { } fn load_bundle_list(&mut self) -> Result<(Vec, Vec), BundleDbError> { - let bundle_info_cache = &self.remote_cache_path; - if let Ok(list) = StoredBundle::read_list_from(&bundle_info_cache) { + let local_cache_path = &self.local_cache_path; + if let Ok(list) = StoredBundle::read_list_from(&local_cache_path) { + for bundle in list { + self.local_bundles.insert(bundle.id(), bundle); + } + } + let remote_cache_path = &self.remote_cache_path; + if let Ok(list) = StoredBundle::read_list_from(&remote_cache_path) { for bundle in list { self.remote_bundles.insert(bundle.id(), bundle); } } - try!(load_bundles(&self.local_bundles_path, &mut self.local_bundles)); + let (new, gone) = try!(load_bundles(&self.local_bundles_path, &mut self.local_bundles)); + if !new.is_empty() || !gone.is_empty() { + let bundles: Vec<_> = self.local_bundles.values().cloned().collect(); + try!(StoredBundle::save_list_to(&bundles, &local_cache_path)); + } let (new, gone) = try!(load_bundles(&self.remote_path, &mut self.remote_bundles)); if !new.is_empty() || !gone.is_empty() { let bundles: Vec<_> = self.remote_bundles.values().cloned().collect(); - try!(StoredBundle::save_list_to(&bundles, &bundle_info_cache)); + try!(StoredBundle::save_list_to(&bundles, &remote_cache_path)); } Ok((new, gone)) } pub fn save_cache(&self) -> Result<(), BundleDbError> { + let bundles: Vec<_> = self.local_bundles.values().cloned().collect(); + try!(StoredBundle::save_list_to(&bundles, &self.local_cache_path)); let bundles: Vec<_> = self.remote_bundles.values().cloned().collect(); Ok(try!(StoredBundle::save_list_to(&bundles, &self.remote_cache_path))) }