Auto merge of #39 - matthiasbeyer:fix-store-cache, r=matthiasbeyer

Fix store cache

Ensure the store always honors the cache.
This commit is contained in:
Homu 2016-01-02 07:46:09 +09:00
commit dca295d8fd
2 changed files with 63 additions and 62 deletions

View file

@ -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)

View file

@ -51,32 +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<HP>(&self, m: &Module, parser: &Parser<HP>, id: FileID)
-> Option<Rc<RefCell<File>>>
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);
});
self.load(&id)
}
/**
* Generate a new file for a module.
*
@ -228,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
*
* 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<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);
self.cache.borrow().get(id).cloned()
}
@ -286,11 +291,36 @@ 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)
}
/**
* Load all files for a module
*/
pub fn load_for_module<HP>(&self, m: &Module, parser: &Parser<HP>)
-> Vec<Rc<RefCell<File>>>
where HP: FileHeaderParser
{
use glob::{glob, Paths, PatternError};
let globstr = format!("{}/{}-*.imag", self.storepath, m.name());
let mut res = vec![];
glob(&globstr[..]).map(|paths| {
for path in paths {
if let Ok(pathbuf) = path {
let fname = pathbuf.file_name().and_then(|s| s.to_str());
fname.map(|s| {
FileID::parse(&String::from(s)).map(|id| {
self.load(m, parser, &id).map(|file| {
res.push(file);
})
});
});
}
}
});
res
}
/**
@ -316,35 +346,6 @@ impl Store {
.unwrap_or(false)
}
/**
* Load all files for a module
*/
pub fn load_for_module<HP>(&self, m: &Module, parser: &Parser<HP>)
-> Vec<Rc<RefCell<File>>>
where HP: FileHeaderParser
{
use glob::{glob, Paths, PatternError};
let globstr = format!("{}/{}-*.imag", self.storepath, m.name());
let mut res = vec![];
glob(&globstr[..]).map(|paths| {
for path in paths {
if let Ok(pathbuf) = path {
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| {
res.push(file);
})
});
});
}
}
});
res
}
/**
* Helper to generate a new FileID object
*/