Add Store::get_all_versions() for getting all versions of a StoreId entry

This commit is contained in:
Matthias Beyer 2016-05-12 15:04:18 +02:00
parent df33dec0c3
commit 839ec051a4

View file

@ -387,6 +387,45 @@ impl Store {
} }
} }
/// Same as `Store::get()` but also tries older versions of the entry, returning an iterator
/// over all versions of the entry.
pub fn get_all_versions<'a, S: IntoStoreId>(&'a self, id: S) -> Result<StoreIdIterator>
{
// get PathBuf component from storeid, but not version component
fn path_component<S: IntoStoreId>(id: S) -> Result<PathBuf> {
let p : PathBuf = id.into_storeid().into();
match p.to_str() {
Some(s) => {
let mut split = s.split("~");
let path_element = match split.next() {
Some(s) => s,
None => return Err(SE::new(SEK::StorePathError, None)),
};
Ok(PathBuf::from(path_element))
},
None => Err(SE::new(SEK::StorePathError, None)),
}
}
fn build_glob_pattern(mut pb: PathBuf) -> Option<String> {
pb.push("~*.*.*");
pb.to_str().map(String::from)
}
match path_component(id).map(build_glob_pattern) {
Err(e) => Err(SE::new(SEK::StorePathError, Some(Box::new(e)))),
Ok(None) => Err(SE::new(SEK::StorePathError, None)),
Ok(Some(pattern)) => {
glob(&pattern[..])
.map(|paths| GlobStoreIdIterator::new(paths).into())
.map_err(|e| SE::new(SEK::GlobError, Some(Box::new(e))))
}
}
}
/// Iterate over all StoreIds for one module name /// Iterate over all StoreIds for one module name
pub fn retrieve_for_module(&self, mod_name: &str) -> Result<StoreIdIterator> { pub fn retrieve_for_module(&self, mod_name: &str) -> Result<StoreIdIterator> {
let mut path = self.path().clone(); let mut path = self.path().clone();