Merge pull request #1222 from matthiasbeyer/libimagstore/improve-iterator-extensions

libimagstore: improve iterator extensions
This commit is contained in:
Matthias Beyer 2018-02-01 20:22:02 +01:00 committed by GitHub
commit 4c9add9deb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 99 additions and 22 deletions

View file

@ -45,7 +45,6 @@ use libimagrt::setup::generate_runtime_setup;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
use libimagstore::iter::get::StoreIdGetIteratorExtension;
use libimagstore::error::StoreError as Error; use libimagstore::error::StoreError as Error;
use libimagentrylink::internal::*; use libimagentrylink::internal::*;
@ -101,7 +100,7 @@ fn main() {
let diags = rt.store() let diags = rt.store()
.entries() .entries()
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.into_get_iter(rt.store()) .into_get_iter()
.map(|e| { .map(|e| {
e.map_err_trace_exit_unwrap(1) e.map_err_trace_exit_unwrap(1)
.ok_or(Error::from("Unable to get entry".to_owned())) .ok_or(Error::from("Unable to get entry".to_owned()))

View file

@ -42,7 +42,6 @@ extern crate libimagerror;
use regex::Regex; use regex::Regex;
use libimagrt::setup::generate_runtime_setup; use libimagrt::setup::generate_runtime_setup;
use libimagstore::iter::get::StoreIdGetIteratorExtension;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
@ -77,7 +76,7 @@ fn main() {
.store() .store()
.entries() .entries()
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.into_get_iter(rt.store()) .into_get_iter()
.filter_map(|res| res.map_err_trace_exit_unwrap(1)) .filter_map(|res| res.map_err_trace_exit_unwrap(1))
.filter(|entry| pattern.is_match(entry.get_content())) .filter(|entry| pattern.is_match(entry.get_content()))
.map(|entry| show(&entry, &pattern, &opts, &mut count)) .map(|entry| show(&entry, &pattern, &opts, &mut count))

View file

@ -34,6 +34,12 @@ macro_rules! mk_iterator {
pub struct $itername<'a>(Box<Iterator<Item = StoreId>>, &'a Store); pub struct $itername<'a>(Box<Iterator<Item = StoreId>>, &'a Store);
impl<'a> $itername<'a> {
pub fn new(inner: Box<Iterator<Item = StoreId>>, store: &'a Store) -> Self {
$itername(inner, store)
}
}
impl<'a> Iterator for $itername<'a> { impl<'a> Iterator for $itername<'a> {
type Item = Result<$yield>; type Item = Result<$yield>;
@ -196,18 +202,14 @@ mod compile_test {
} }
fn test_compile_get() { fn test_compile_get() {
use super::get::StoreIdGetIteratorExtension;
let store = store(); let store = store();
let _ = store let _ = store
.entries() .entries()
.unwrap() .unwrap()
.into_get_iter(&store); .into_get_iter();
} }
fn test_compile_get_result() { fn test_compile_get_result() {
use super::get::StoreIdGetResultIteratorExtension;
fn to_result(e: StoreId) -> Result<StoreId, ()> { fn to_result(e: StoreId) -> Result<StoreId, ()> {
Ok(e) Ok(e)
} }
@ -216,8 +218,7 @@ mod compile_test {
let _ = store let _ = store
.entries() .entries()
.unwrap() .unwrap()
.map(to_result) .into_get_iter();
.into_get_iter(&store);
} }
} }

View file

@ -40,7 +40,7 @@ use toml_query::read::TomlValueReadTypeExt;
use error::{StoreError as SE, StoreErrorKind as SEK}; use error::{StoreError as SE, StoreErrorKind as SEK};
use error::ResultExt; use error::ResultExt;
use storeid::{IntoStoreId, StoreId, StoreIdIterator}; use storeid::{IntoStoreId, StoreId, StoreIdIterator, StoreIdIteratorWithStore};
use file_abstraction::FileAbstractionInstance; use file_abstraction::FileAbstractionInstance;
// We re-export the following things so tests can use them // We re-export the following things so tests can use them
@ -721,7 +721,7 @@ impl Store {
} }
/// Get _all_ entries in the store (by id as iterator) /// Get _all_ entries in the store (by id as iterator)
pub fn entries(&self) -> Result<StoreIdIterator> { pub fn entries<'a>(&'a self) -> Result<StoreIdIteratorWithStore<'a>> {
self.backend self.backend
.pathes_recursively(self.path().clone()) .pathes_recursively(self.path().clone())
.and_then(|iter| { .and_then(|iter| {
@ -738,9 +738,8 @@ impl Store {
elems.push(sid); elems.push(sid);
} }
} }
Ok(StoreIdIterator::new(Box::new(elems.into_iter()))) Ok(StoreIdIteratorWithStore::new(Box::new(elems.into_iter()), self))
}) })
} }
/// Gets the path where this store is on the disk /// Gets the path where this store is on the disk

View file

@ -30,6 +30,12 @@ use error::StoreErrorKind as SEK;
use error::StoreError as SE; use error::StoreError as SE;
use error::ResultExt; use error::ResultExt;
use store::Result; use store::Result;
use store::Store;
use iter::create::StoreCreateIterator;
use iter::delete::StoreDeleteIterator;
use iter::get::StoreGetIterator;
use iter::retrieve::StoreRetrieveIterator;
/// The Index into the Store /// The Index into the Store
#[derive(Debug, Clone, Hash, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Hash, Eq, PartialOrd, Ord)]
@ -260,6 +266,67 @@ impl Iterator for StoreIdIterator {
} }
pub struct StoreIdIteratorWithStore<'a>(StoreIdIterator, &'a Store);
impl<'a> Deref for StoreIdIteratorWithStore<'a> {
type Target = StoreIdIterator;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a> Iterator for StoreIdIteratorWithStore<'a> {
type Item = StoreId;
fn next(&mut self) -> Option<StoreId> {
self.0.next()
}
}
impl<'a> StoreIdIteratorWithStore<'a> {
pub fn new(iter: Box<Iterator<Item = StoreId>>, store: &'a Store) -> Self {
StoreIdIteratorWithStore(StoreIdIterator::new(iter), store)
}
pub fn without_store(self) -> StoreIdIterator {
self.0
}
/// Transform the iterator into a StoreCreateIterator
///
/// This immitates the API from `libimagstore::iter`.
pub fn into_create_iter(self) -> StoreCreateIterator<'a> {
StoreCreateIterator::new(Box::new(self.0), self.1)
}
/// Transform the iterator into a StoreDeleteIterator
///
///
/// This immitates the API from `libimagstore::iter`.
pub fn into_delete_iter(self) -> StoreDeleteIterator<'a> {
StoreDeleteIterator::new(Box::new(self.0), self.1)
}
/// Transform the iterator into a StoreGetIterator
///
///
/// This immitates the API from `libimagstore::iter`.
pub fn into_get_iter(self) -> StoreGetIterator<'a> {
StoreGetIterator::new(Box::new(self.0), self.1)
}
/// Transform the iterator into a StoreRetrieveIterator
///
///
/// This immitates the API from `libimagstore::iter`.
pub fn into_retrieve_iter(self) -> StoreRetrieveIterator<'a> {
StoreRetrieveIterator::new(Box::new(self.0), self.1)
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::path::PathBuf; use std::path::PathBuf;

View file

@ -18,6 +18,7 @@
// //
use libimagstore::storeid::StoreIdIterator; use libimagstore::storeid::StoreIdIterator;
use libimagstore::storeid::StoreIdIteratorWithStore;
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
use util::IsHabitCheck; use util::IsHabitCheck;
@ -43,6 +44,12 @@ impl From<StoreIdIterator> for HabitTemplateStoreIdIterator {
} }
} }
impl<'a> From<StoreIdIteratorWithStore<'a>> for HabitTemplateStoreIdIterator {
fn from(sii: StoreIdIteratorWithStore<'a>) -> Self {
HabitTemplateStoreIdIterator(sii.without_store())
}
}
pub struct HabitInstanceStoreIdIterator(StoreIdIterator); pub struct HabitInstanceStoreIdIterator(StoreIdIterator);
impl HabitInstanceStoreIdIterator { impl HabitInstanceStoreIdIterator {
@ -70,3 +77,9 @@ impl From<StoreIdIterator> for HabitInstanceStoreIdIterator {
} }
} }
impl<'a> From<StoreIdIteratorWithStore<'a>> for HabitInstanceStoreIdIterator {
fn from(sii: StoreIdIteratorWithStore<'a>) -> Self {
HabitInstanceStoreIdIterator(sii.without_store())
}
}

View file

@ -616,7 +616,6 @@ pub mod store_check {
use internal::InternalLinker; use internal::InternalLinker;
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
use libimagstore::iter::get::StoreIdGetIteratorExtension;
use libimagutil::debug_result::DebugResult; use libimagutil::debug_result::DebugResult;
// Helper data structure to collect incoming and outgoing links for each StoreId // Helper data structure to collect incoming and outgoing links for each StoreId
@ -636,7 +635,7 @@ pub mod store_check {
let aggregate_link_network = |store: &Store| -> Result<HashMap<StoreId, Linking>> { let aggregate_link_network = |store: &Store| -> Result<HashMap<StoreId, Linking>> {
store store
.entries()? .entries()?
.into_get_iter(store) .into_get_iter()
.fold(Ok(HashMap::new()), |map, element| { .fold(Ok(HashMap::new()), |map, element| {
map.and_then(|mut map| { map.and_then(|mut map| {
debug!("Checking element = {:?}", element); debug!("Checking element = {:?}", element);

View file

@ -381,7 +381,7 @@ mod tests {
let entries = store.entries(); let entries = store.entries();
assert!(entries.is_ok()); assert!(entries.is_ok());
let entries : Vec<_> = entries.unwrap().collect(); let entries : Vec<_> = entries.unwrap().without_store().collect();
assert_eq!(2, entries.len(), "Expected 2 links, got: {:?}", entries); assert_eq!(2, entries.len(), "Expected 2 links, got: {:?}", entries);
@ -420,7 +420,7 @@ mod tests {
let entries = store.entries(); let entries = store.entries();
assert!(entries.is_ok()); assert!(entries.is_ok());
let entries : Vec<_> = entries.unwrap().collect(); let entries : Vec<_> = entries.unwrap().without_store().collect();
assert_eq!(2, entries.len(), "Expected 2 links, got: {:?}", entries); assert_eq!(2, entries.len(), "Expected 2 links, got: {:?}", entries);
println!("{:?}", entries); println!("{:?}", entries);
@ -453,7 +453,7 @@ mod tests {
let entries = store.entries(); let entries = store.entries();
assert!(entries.is_ok()); assert!(entries.is_ok());
let entries : Vec<_> = entries.unwrap().collect(); let entries : Vec<_> = entries.unwrap().without_store().collect();
assert_eq!(3, entries.len(), "Expected 3 links, got: {:?}", entries); assert_eq!(3, entries.len(), "Expected 3 links, got: {:?}", entries);
println!("{:?}", entries); println!("{:?}", entries);
@ -486,7 +486,7 @@ mod tests {
let entries = store.entries(); let entries = store.entries();
assert!(entries.is_ok()); assert!(entries.is_ok());
let entries : Vec<_> = entries.unwrap().collect(); let entries : Vec<_> = entries.unwrap().without_store().collect();
assert_eq!(1, entries.len(), "Expected 1 entries, got: {:?}", entries); assert_eq!(1, entries.len(), "Expected 1 entries, got: {:?}", entries);
println!("{:?}", entries); println!("{:?}", entries);
@ -514,7 +514,7 @@ mod tests {
let entries = store.entries(); let entries = store.entries();
assert!(entries.is_ok()); assert!(entries.is_ok());
let entries : Vec<_> = entries.unwrap().collect(); let entries : Vec<_> = entries.unwrap().without_store().collect();
assert_eq!(1, entries.len(), "Expected 1 entries, got: {:?}", entries); assert_eq!(1, entries.len(), "Expected 1 entries, got: {:?}", entries);
} }