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.
This commit is contained in:
Matthias Beyer 2016-01-01 23:38:27 +01:00
parent f802b00974
commit c681b24ef4
2 changed files with 35 additions and 35 deletions

View file

@ -70,7 +70,7 @@ impl<'a> BM<'a> {
let header = build_header(url, tags); let header = build_header(url, tags);
let fileid = self.rt.store().new_file_with_header(self, header); 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); info!("Created file in memory: {}", fileid);
Some(self.rt.store().persist(&parser, file)) Some(self.rt.store().persist(&parser, file))
}).unwrap_or(false) }).unwrap_or(false)

View file

@ -51,33 +51,6 @@ impl Store {
res 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<HP>(&self, m: &Module, parser: &Parser<HP>, id: FileID)
-> Option<Rc<RefCell<File>>>
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. * 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<HP>(&self, m: &Module, parser: &Parser<HP>, 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 * 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 * 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. * fail if the file is not available.
*/ */
pub fn load(&self, id: &FileID) -> Option<Rc<RefCell<File>>> { pub fn load<HP>(&self, m: &Module, parser: &Parser<HP>, id: &FileID)
-> Option<Rc<RefCell<File>>>
where HP: FileHeaderParser
{
if !self.cache.borrow().contains_key(id) {
self.load_into_cache(m, parser, id);
}
debug!("Loading '{:?}'", id); debug!("Loading '{:?}'", id);
self.cache.borrow().get(id).cloned() self.cache.borrow().get(id).cloned()
} }
@ -287,11 +291,7 @@ impl Store {
debug!("Loaded ID = '{:?}'", id); debug!("Loaded ID = '{:?}'", id);
self.load_in_cache(m, parser, id) self.load(m, parser, &id)
.map(|file| {
debug!("Loaded File = '{:?}'", file);
Some(file)
}).unwrap_or(None)
} }
/** /**
@ -335,7 +335,7 @@ impl Store {
let fname = pathbuf.file_name().and_then(|s| s.to_str()); let fname = pathbuf.file_name().and_then(|s| s.to_str());
fname.map(|s| { fname.map(|s| {
FileID::parse(&String::from(s)).map(|id| { 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); res.push(file);
}) })
}); });