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); }) });