From f802b0097422955fa62cad20f035bb8a86ad056c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 1 Jan 2016 23:26:04 +0100 Subject: [PATCH 1/3] Rewrite Store::load_in_cache() to check cache before loading --- src/storage/mod.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/storage/mod.rs b/src/storage/mod.rs index 8f2a3707..ba1cc5fa 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -60,21 +60,22 @@ impl Store { -> Option>> where HP: FileHeaderParser { - let idstr : String = id.clone().into(); - let path = format!("{}/{}-{}.imag", self.storepath, m.name(), idstr); - debug!("Loading path = '{}'", path); - let mut string = String::new(); + self.load(&id).or({ + let idstr : String = id.clone().into(); + let path = format!("{}/{}-{}.imag", self.storepath, m.name(), idstr); + debug!("Loading path = '{}'", path); + let mut string = String::new(); - FSFile::open(&path).map(|mut file| { - file.read_to_string(&mut string) - .map_err(|e| error!("Failed reading file: '{}'", path)); - }); + FSFile::open(&path).map(|mut file| { + file.read_to_string(&mut string) + .map_err(|e| error!("Failed reading file: '{}'", path)); + }); - parser.read(string).map(|(header, data)| { - self.new_file_from_parser_result(m, id.clone(), header, data); - }); - - self.load(&id) + parser.read(string).map(|(header, data)| { + self.new_file_from_parser_result(m, id.clone(), header, data); + self.load(&id) + }).unwrap_or(None) + }) } /** From c681b24ef4c4f54840fb126ac8aa39fa33eb0eac Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 1 Jan 2016 23:38:27 +0100 Subject: [PATCH 2/3] Store::load() should load, not Store::put_in_cache() Store::put_in_cache (not put_into_cache) is now called inside ::load() if the key is not yet in the cache. The Store::load() function cached its interface for this. It can also be used by the other functions in the Store object to load content. --- src/module/bm/mod.rs | 2 +- src/storage/mod.rs | 68 ++++++++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/module/bm/mod.rs b/src/module/bm/mod.rs index a2b880ae..369ee59b 100644 --- a/src/module/bm/mod.rs +++ b/src/module/bm/mod.rs @@ -70,7 +70,7 @@ impl<'a> BM<'a> { let header = build_header(url, tags); let fileid = self.rt.store().new_file_with_header(self, header); - self.rt.store().load(&fileid).and_then(|file| { + self.rt.store().load(self, &parser, &fileid).and_then(|file| { info!("Created file in memory: {}", fileid); Some(self.rt.store().persist(&parser, file)) }).unwrap_or(false) diff --git a/src/storage/mod.rs b/src/storage/mod.rs index ba1cc5fa..3e6a08ff 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -51,33 +51,6 @@ impl Store { res } - /** - * Load a file by ID into the cache and return it afterwards - * - * Returns None if the file could be loaded from the Filesystem - */ - pub fn load_in_cache(&self, m: &Module, parser: &Parser, id: FileID) - -> Option>> - where HP: FileHeaderParser - { - self.load(&id).or({ - let idstr : String = id.clone().into(); - let path = format!("{}/{}-{}.imag", self.storepath, m.name(), idstr); - debug!("Loading path = '{}'", path); - let mut string = String::new(); - - FSFile::open(&path).map(|mut file| { - file.read_to_string(&mut string) - .map_err(|e| error!("Failed reading file: '{}'", path)); - }); - - parser.read(string).map(|(header, data)| { - self.new_file_from_parser_result(m, id.clone(), header, data); - self.load(&id) - }).unwrap_or(None) - }) - } - /** * Generate a new file for a module. * @@ -229,13 +202,44 @@ impl Store { }) } + /** + * Load a file by ID into the cache and return it afterwards + * + * Returns None if the file could be loaded from the Filesystem + */ + fn load_into_cache(&self, m: &Module, parser: &Parser, id: &FileID) + -> bool + where HP: FileHeaderParser + { + let idstr : String = id.clone().into(); + let path = format!("{}/{}-{}.imag", self.storepath, m.name(), idstr); + debug!("Loading path = '{}'", path); + let mut string = String::new(); + + FSFile::open(&path).map(|mut file| { + file.read_to_string(&mut string) + .map_err(|e| error!("Failed reading file: '{}'", path)); + }); + + parser.read(string).map(|(header, data)| { + self.new_file_from_parser_result(m, id.clone(), header, data); + true + }).unwrap_or(false) + } + /** * Load a file from the cache by FileID * * TODO: Semantics: This function should load from FS if the file is not in the cache yet or * fail if the file is not available. */ - pub fn load(&self, id: &FileID) -> Option>> { + pub fn load(&self, m: &Module, parser: &Parser, id: &FileID) + -> Option>> + where HP: FileHeaderParser + { + if !self.cache.borrow().contains_key(id) { + self.load_into_cache(m, parser, id); + } debug!("Loading '{:?}'", id); self.cache.borrow().get(id).cloned() } @@ -287,11 +291,7 @@ impl Store { debug!("Loaded ID = '{:?}'", id); - self.load_in_cache(m, parser, id) - .map(|file| { - debug!("Loaded File = '{:?}'", file); - Some(file) - }).unwrap_or(None) + self.load(m, parser, &id) } /** @@ -335,7 +335,7 @@ impl Store { let fname = pathbuf.file_name().and_then(|s| s.to_str()); fname.map(|s| { FileID::parse(&String::from(s)).map(|id| { - self.load_in_cache(m, parser, id).map(|file| { + self.load(m, parser, &id).map(|file| { res.push(file); }) }); From 911c248c39f15c4e2fc3e3c03d390573415a6bba Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 1 Jan 2016 23:40:37 +0100 Subject: [PATCH 3/3] Store::remove(): Move function so we have a better order --- src/storage/mod.rs | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/storage/mod.rs b/src/storage/mod.rs index 3e6a08ff..3e33c681 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -294,29 +294,6 @@ impl Store { self.load(m, parser, &id) } - /** - * Remove a file from the filesystem by FileID - * - * Returns true if this works. - */ - pub fn remove(&self, id: FileID) -> bool { - use std::fs::remove_file; - - self.cache - .borrow_mut() - .remove(&id) - .map(|file| { - let idstr : String = id.into(); - let path = format!("{}/{}-{}.imag", - self.storepath, - file.deref().borrow().owner_name(), - idstr); - debug!("Removing file NOW: '{}'", path); - remove_file(path).is_ok() - }) - .unwrap_or(false) - } - /** * Load all files for a module */ @@ -346,6 +323,29 @@ impl Store { res } + /** + * Remove a file from the filesystem by FileID + * + * Returns true if this works. + */ + pub fn remove(&self, id: FileID) -> bool { + use std::fs::remove_file; + + self.cache + .borrow_mut() + .remove(&id) + .map(|file| { + let idstr : String = id.into(); + let path = format!("{}/{}-{}.imag", + self.storepath, + file.deref().borrow().owner_name(), + idstr); + debug!("Removing file NOW: '{}'", path); + remove_file(path).is_ok() + }) + .unwrap_or(false) + } + /** * Helper to generate a new FileID object */