From f802b0097422955fa62cad20f035bb8a86ad056c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 1 Jan 2016 23:26:04 +0100 Subject: [PATCH] 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) + }) } /**