From 030e32e44f054519517c1d491f170b6687f848ba Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 28 Jan 2018 21:10:12 +0100 Subject: [PATCH 1/6] Provide StoreId Iterator which has a ref to the Store This change is needed so we can refactor the "get" iterator to not take an argument (the store) later, which improves the API. --- bin/core/imag-diagnostics/src/main.rs | 1 - lib/core/libimagstore/src/store.rs | 7 +++---- lib/core/libimagstore/src/storeid.rs | 29 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/bin/core/imag-diagnostics/src/main.rs b/bin/core/imag-diagnostics/src/main.rs index 7d3b06da..391bcfa1 100644 --- a/bin/core/imag-diagnostics/src/main.rs +++ b/bin/core/imag-diagnostics/src/main.rs @@ -45,7 +45,6 @@ use libimagrt::setup::generate_runtime_setup; use libimagerror::trace::MapErrTrace; use libimagstore::store::FileLockEntry; use libimagstore::storeid::StoreId; -use libimagstore::iter::get::StoreIdGetIteratorExtension; use libimagstore::error::StoreError as Error; use libimagentrylink::internal::*; diff --git a/lib/core/libimagstore/src/store.rs b/lib/core/libimagstore/src/store.rs index 365576e0..0a292ef2 100644 --- a/lib/core/libimagstore/src/store.rs +++ b/lib/core/libimagstore/src/store.rs @@ -40,7 +40,7 @@ use toml_query::read::TomlValueReadTypeExt; use error::{StoreError as SE, StoreErrorKind as SEK}; use error::ResultExt; -use storeid::{IntoStoreId, StoreId, StoreIdIterator}; +use storeid::{IntoStoreId, StoreId, StoreIdIterator, StoreIdIteratorWithStore}; use file_abstraction::FileAbstractionInstance; // 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) - pub fn entries(&self) -> Result { + pub fn entries<'a>(&'a self) -> Result> { self.backend .pathes_recursively(self.path().clone()) .and_then(|iter| { @@ -738,9 +738,8 @@ impl Store { 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 diff --git a/lib/core/libimagstore/src/storeid.rs b/lib/core/libimagstore/src/storeid.rs index dde25a2e..1585d754 100644 --- a/lib/core/libimagstore/src/storeid.rs +++ b/lib/core/libimagstore/src/storeid.rs @@ -30,6 +30,7 @@ use error::StoreErrorKind as SEK; use error::StoreError as SE; use error::ResultExt; use store::Result; +use store::Store; /// The Index into the Store #[derive(Debug, Clone, Hash, Eq, PartialOrd, Ord)] @@ -260,6 +261,34 @@ 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 { + self.0.next() + } +} + +impl<'a> StoreIdIteratorWithStore<'a> { + pub fn new(iter: Box>, store: &'a Store) -> Self { + StoreIdIteratorWithStore(StoreIdIterator::new(iter), store) + } + + pub fn get_store(&self) -> &Store { + &self.1 + } +} + #[cfg(test)] mod test { use std::path::PathBuf; From 3b01a9eb2fc34af47a1b789b08d96c231ac05cab Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 28 Jan 2018 21:21:44 +0100 Subject: [PATCH 2/6] Add API in StoreIdIteratorWithStore to get acting iterators This patch adds API functions in the StoreIdIteratorWithStore iterator type to transform it into a iterator which _does_ something (as in the `libimagstore::iter` API). It mimics the API which is offered by `libimagstore::iter`. --- lib/core/libimagstore/src/iter.rs | 6 +++++ lib/core/libimagstore/src/storeid.rs | 38 ++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/core/libimagstore/src/iter.rs b/lib/core/libimagstore/src/iter.rs index d42c92fc..67c0f0ba 100644 --- a/lib/core/libimagstore/src/iter.rs +++ b/lib/core/libimagstore/src/iter.rs @@ -34,6 +34,12 @@ macro_rules! mk_iterator { pub struct $itername<'a>(Box>, &'a Store); + impl<'a> $itername<'a> { + pub fn new(inner: Box>, store: &'a Store) -> Self { + $itername(inner, store) + } + } + impl<'a> Iterator for $itername<'a> { type Item = Result<$yield>; diff --git a/lib/core/libimagstore/src/storeid.rs b/lib/core/libimagstore/src/storeid.rs index 1585d754..04e80099 100644 --- a/lib/core/libimagstore/src/storeid.rs +++ b/lib/core/libimagstore/src/storeid.rs @@ -32,6 +32,11 @@ use error::ResultExt; 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 #[derive(Debug, Clone, Hash, Eq, PartialOrd, Ord)] pub struct StoreId { @@ -280,13 +285,42 @@ impl<'a> Iterator for StoreIdIteratorWithStore<'a> { } impl<'a> StoreIdIteratorWithStore<'a> { + pub fn new(iter: Box>, store: &'a Store) -> Self { StoreIdIteratorWithStore(StoreIdIterator::new(iter), store) } - pub fn get_store(&self) -> &Store { - &self.1 + /// 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)] From 8e931cd79f000d2b9f6826c38ed11e79635fd9cd Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 28 Jan 2018 21:24:02 +0100 Subject: [PATCH 3/6] Change calls to into_get_iter() to not take argument --- bin/core/imag-diagnostics/src/main.rs | 2 +- bin/core/imag-grep/src/main.rs | 3 +-- lib/core/libimagstore/src/iter.rs | 9 ++------- lib/entry/libimagentrylink/src/internal.rs | 3 +-- 4 files changed, 5 insertions(+), 12 deletions(-) diff --git a/bin/core/imag-diagnostics/src/main.rs b/bin/core/imag-diagnostics/src/main.rs index 391bcfa1..3f746cb1 100644 --- a/bin/core/imag-diagnostics/src/main.rs +++ b/bin/core/imag-diagnostics/src/main.rs @@ -100,7 +100,7 @@ fn main() { let diags = rt.store() .entries() .map_err_trace_exit_unwrap(1) - .into_get_iter(rt.store()) + .into_get_iter() .map(|e| { e.map_err_trace_exit_unwrap(1) .ok_or(Error::from("Unable to get entry".to_owned())) diff --git a/bin/core/imag-grep/src/main.rs b/bin/core/imag-grep/src/main.rs index 6e92b601..e51f1cf5 100644 --- a/bin/core/imag-grep/src/main.rs +++ b/bin/core/imag-grep/src/main.rs @@ -42,7 +42,6 @@ extern crate libimagerror; use regex::Regex; use libimagrt::setup::generate_runtime_setup; -use libimagstore::iter::get::StoreIdGetIteratorExtension; use libimagstore::store::Entry; use libimagerror::trace::MapErrTrace; @@ -77,7 +76,7 @@ fn main() { .store() .entries() .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(|entry| pattern.is_match(entry.get_content())) .map(|entry| show(&entry, &pattern, &opts, &mut count)) diff --git a/lib/core/libimagstore/src/iter.rs b/lib/core/libimagstore/src/iter.rs index 67c0f0ba..460ebf0e 100644 --- a/lib/core/libimagstore/src/iter.rs +++ b/lib/core/libimagstore/src/iter.rs @@ -202,18 +202,14 @@ mod compile_test { } fn test_compile_get() { - use super::get::StoreIdGetIteratorExtension; - let store = store(); let _ = store .entries() .unwrap() - .into_get_iter(&store); + .into_get_iter(); } fn test_compile_get_result() { - use super::get::StoreIdGetResultIteratorExtension; - fn to_result(e: StoreId) -> Result { Ok(e) } @@ -222,8 +218,7 @@ mod compile_test { let _ = store .entries() .unwrap() - .map(to_result) - .into_get_iter(&store); + .into_get_iter(); } } diff --git a/lib/entry/libimagentrylink/src/internal.rs b/lib/entry/libimagentrylink/src/internal.rs index d7cddb69..371419cb 100644 --- a/lib/entry/libimagentrylink/src/internal.rs +++ b/lib/entry/libimagentrylink/src/internal.rs @@ -616,7 +616,6 @@ pub mod store_check { use internal::InternalLinker; use libimagstore::storeid::StoreId; - use libimagstore::iter::get::StoreIdGetIteratorExtension; use libimagutil::debug_result::DebugResult; // 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> { store .entries()? - .into_get_iter(store) + .into_get_iter() .fold(Ok(HashMap::new()), |map, element| { map.and_then(|mut map| { debug!("Checking element = {:?}", element); From b3f546129a5291a684159f00bca88e2eab1feb27 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 29 Jan 2018 20:42:02 +0100 Subject: [PATCH 4/6] Add StoreIdIteratorWithStore::without_store() --- lib/core/libimagstore/src/storeid.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/core/libimagstore/src/storeid.rs b/lib/core/libimagstore/src/storeid.rs index 04e80099..b97c4c7f 100644 --- a/lib/core/libimagstore/src/storeid.rs +++ b/lib/core/libimagstore/src/storeid.rs @@ -290,6 +290,10 @@ impl<'a> StoreIdIteratorWithStore<'a> { 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`. From 3c1a0a51c90a130d8cf28cc3d0d8a674548f7ea8 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 29 Jan 2018 20:42:15 +0100 Subject: [PATCH 5/6] Transform iterator to forget Store reference here --- lib/entry/libimagentrymarkdown/src/processor.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/entry/libimagentrymarkdown/src/processor.rs b/lib/entry/libimagentrymarkdown/src/processor.rs index 965635c5..cdc7f3e4 100644 --- a/lib/entry/libimagentrymarkdown/src/processor.rs +++ b/lib/entry/libimagentrymarkdown/src/processor.rs @@ -381,7 +381,7 @@ mod tests { let entries = store.entries(); 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); @@ -420,7 +420,7 @@ mod tests { let entries = store.entries(); 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); println!("{:?}", entries); @@ -453,7 +453,7 @@ mod tests { let entries = store.entries(); 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); println!("{:?}", entries); @@ -486,7 +486,7 @@ mod tests { let entries = store.entries(); 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); println!("{:?}", entries); @@ -514,7 +514,7 @@ mod tests { let entries = store.entries(); 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); } From da0c1fa5948a1174b4d01494531e05abba631166 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 31 Jan 2018 18:34:37 +0100 Subject: [PATCH 6/6] Rewrite From<> implementations for habit iterators to be generic over all iterators over StoreIds, so they can be build directly from them. --- lib/domain/libimaghabit/src/iter.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/domain/libimaghabit/src/iter.rs b/lib/domain/libimaghabit/src/iter.rs index 9d5d740f..71f7220b 100644 --- a/lib/domain/libimaghabit/src/iter.rs +++ b/lib/domain/libimaghabit/src/iter.rs @@ -18,6 +18,7 @@ // use libimagstore::storeid::StoreIdIterator; +use libimagstore::storeid::StoreIdIteratorWithStore; use libimagstore::storeid::StoreId; use util::IsHabitCheck; @@ -43,6 +44,12 @@ impl From for HabitTemplateStoreIdIterator { } } +impl<'a> From> for HabitTemplateStoreIdIterator { + fn from(sii: StoreIdIteratorWithStore<'a>) -> Self { + HabitTemplateStoreIdIterator(sii.without_store()) + } +} + pub struct HabitInstanceStoreIdIterator(StoreIdIterator); impl HabitInstanceStoreIdIterator { @@ -70,3 +77,9 @@ impl From for HabitInstanceStoreIdIterator { } } +impl<'a> From> for HabitInstanceStoreIdIterator { + fn from(sii: StoreIdIteratorWithStore<'a>) -> Self { + HabitInstanceStoreIdIterator(sii.without_store()) + } +} +