summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2018-04-30 15:46:39 +0200
committerMatthias Beyer <mail@beyermatthias.de>2018-05-01 17:44:00 +0200
commita2ff298e672694a4204d0989c364d5ecb3599f55 (patch)
treec06917f4058cecd27a289dd5026c0f447f669318 /lib
parentf4556f39839aa19e687767b8ab7e17cb92c4ba63 (diff)
Rewrite Store::entries()
This patch rewrites the Store::entries() function to not be collecting the iterator. It therefore introduces a new, internal, iterator type which creates the StoreId objects from the pathes the PathIterator yields internally. With this patch, the Store iterator interface changes, as the iterators now yield `Result<StoreId, StoreError>` instead of `StoreId`. This is necessary, as the internal conversion errors shouldn't be hidden. Of course, the iterator types (like the StoreGetIterator and so on) should hold a Result<StoreId> internally as well, and also yield appropritely. This was changed in this commit, too.
Diffstat (limited to 'lib')
-rw-r--r--lib/core/libimagstore/src/file_abstraction/iter.rs31
-rw-r--r--lib/core/libimagstore/src/file_abstraction/mod.rs2
-rw-r--r--lib/core/libimagstore/src/iter.rs8
-rw-r--r--lib/core/libimagstore/src/store.rs21
-rw-r--r--lib/core/libimagstore/src/storeid.rs14
5 files changed, 47 insertions, 29 deletions
diff --git a/lib/core/libimagstore/src/file_abstraction/iter.rs b/lib/core/libimagstore/src/file_abstraction/iter.rs
index c0bd8b5f..d7ae82d9 100644
--- a/lib/core/libimagstore/src/file_abstraction/iter.rs
+++ b/lib/core/libimagstore/src/file_abstraction/iter.rs
@@ -20,6 +20,7 @@
use std::path::PathBuf;
use error::Result;
+use storeid::StoreId;
/// A wrapper for an iterator over `PathBuf`s
pub struct PathIterator(Box<Iterator<Item = Result<PathBuf>>>);
@@ -30,6 +31,10 @@ impl PathIterator {
PathIterator(iter)
}
+ pub fn store_id_constructing(self, storepath: PathBuf) -> StoreIdConstructingIterator {
+ StoreIdConstructingIterator(self, storepath)
+ }
+
}
impl Iterator for PathIterator {
@@ -41,3 +46,29 @@ impl Iterator for PathIterator {
}
+
+/// Helper type for constructing StoreIds from a PathIterator.
+///
+/// Automatically ignores non-files.
+pub struct StoreIdConstructingIterator(PathIterator, PathBuf);
+
+impl Iterator for StoreIdConstructingIterator {
+ type Item = Result<StoreId>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ while let Some(next) = self.0.next() {
+ match next {
+ Err(e) => return Some(Err(e)),
+ Ok(next) => if next.is_file() {
+ return Some(StoreId::from_full_path(&self.1, next))
+ } else {
+ continue
+ }
+ }
+ }
+
+ None
+ }
+
+}
+
diff --git a/lib/core/libimagstore/src/file_abstraction/mod.rs b/lib/core/libimagstore/src/file_abstraction/mod.rs
index 893b8ca2..a3172735 100644
--- a/lib/core/libimagstore/src/file_abstraction/mod.rs
+++ b/lib/core/libimagstore/src/file_abstraction/mod.rs
@@ -27,7 +27,7 @@ use storeid::StoreId;
mod fs;
mod inmemory;
-mod iter;
+pub(crate) mod iter;
pub use self::fs::FSFileAbstraction;
pub use self::fs::FSFileAbstractionInstance;
diff --git a/lib/core/libimagstore/src/iter.rs b/lib/core/libimagstore/src/iter.rs
index 0171a93b..63100470 100644
--- a/lib/core/libimagstore/src/iter.rs
+++ b/lib/core/libimagstore/src/iter.rs
@@ -32,10 +32,10 @@ macro_rules! mk_iterator {
use store::Store;
use error::Result;
- pub struct $itername<'a>(Box<Iterator<Item = StoreId> + 'a>, &'a Store);
+ pub struct $itername<'a>(Box<Iterator<Item = Result<StoreId>> + 'a>, &'a Store);
impl<'a> $itername<'a> {
- pub fn new(inner: Box<Iterator<Item = StoreId> + 'a>, store: &'a Store) -> Self {
+ pub fn new(inner: Box<Iterator<Item = Result<StoreId>> + 'a>, store: &'a Store) -> Self {
$itername(inner, store)
}
}
@@ -44,7 +44,7 @@ macro_rules! mk_iterator {
type Item = Result<$yield>;
fn next(&mut self) -> Option<Self::Item> {
- self.0.next().map(|id| $fun(id, self.1))
+ self.0.next().map(|id| $fun(id?, self.1))
}
}
@@ -53,7 +53,7 @@ macro_rules! mk_iterator {
}
impl<'a, I> $extname<'a> for I
- where I: Iterator<Item = StoreId> + 'a
+ where I: Iterator<Item = Result<StoreId>> + 'a
{
fn $extfnname(self, store: &'a Store) -> $itername<'a> {
$itername(Box::new(self), store)
diff --git a/lib/core/libimagstore/src/store.rs b/lib/core/libimagstore/src/store.rs
index 7edf3f05..c401594a 100644
--- a/lib/core/libimagstore/src/store.rs
+++ b/lib/core/libimagstore/src/store.rs
@@ -741,25 +741,12 @@ impl Store {
}
/// Get _all_ entries in the store (by id as iterator)
- pub fn entries<'a>(&'a self) -> Result<StoreIdIteratorWithStore<'a>> {
+ pub fn entries(&self) -> Result<StoreIdIteratorWithStore> {
self.backend
.pathes_recursively(self.path().clone())
- .and_then(|iter| {
- let mut elems = vec![];
- for element in iter {
- let is_file = {
- let mut base = self.path().clone();
- base.push(element.clone());
- self.backend.is_file(&base)?
- };
-
- if is_file {
- let sid = StoreId::from_full_path(self.path(), element)?;
- elems.push(sid);
- }
- }
- Ok(StoreIdIteratorWithStore::new(Box::new(elems.into_iter()), self))
- })
+ .map(|i| i.store_id_constructing(self.path().clone()))
+ .map(Box::new)
+ .map(|it| StoreIdIteratorWithStore::new(it, 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 2bcd9ba7..80e1b35d 100644
--- a/lib/core/libimagstore/src/storeid.rs
+++ b/lib/core/libimagstore/src/storeid.rs
@@ -241,7 +241,7 @@ macro_rules! module_entry_path_mod {
}
pub struct StoreIdIterator {
- iter: Box<Iterator<Item = StoreId>>,
+ iter: Box<Iterator<Item = Result<StoreId>>>,
}
impl Debug for StoreIdIterator {
@@ -254,16 +254,16 @@ impl Debug for StoreIdIterator {
impl StoreIdIterator {
- pub fn new(iter: Box<Iterator<Item = StoreId>>) -> StoreIdIterator {
+ pub fn new(iter: Box<Iterator<Item = Result<StoreId>>>) -> StoreIdIterator {
StoreIdIterator { iter }
}
}
impl Iterator for StoreIdIterator {
- type Item = StoreId;
+ type Item = Result<StoreId>;
- fn next(&mut self) -> Option<StoreId> {
+ fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
@@ -280,16 +280,16 @@ impl<'a> Deref for StoreIdIteratorWithStore<'a> {
}
impl<'a> Iterator for StoreIdIteratorWithStore<'a> {
- type Item = StoreId;
+ type Item = Result<StoreId>;
- fn next(&mut self) -> Option<StoreId> {
+ fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl<'a> StoreIdIteratorWithStore<'a> {
- pub fn new(iter: Box<Iterator<Item = StoreId>>, store: &'a Store) -> Self {
+ pub fn new(iter: Box<Iterator<Item = Result<StoreId>>>, store: &'a Store) -> Self {
StoreIdIteratorWithStore(StoreIdIterator::new(iter), store)
}