Rewrite backend to not collect on pathes_recursively()

This commit is contained in:
Matthias Beyer 2018-04-30 15:42:20 +02:00
parent d45eef299e
commit f4556f3983
3 changed files with 13 additions and 16 deletions

View file

@ -168,20 +168,15 @@ impl FileAbstraction for FSFileAbstraction {
fn pathes_recursively(&self, basepath: PathBuf) -> Result<PathIterator, SE> {
use walkdir::WalkDir;
let i : Result<Vec<PathBuf>, SE> = WalkDir::new(basepath)
let i = WalkDir::new(basepath)
.min_depth(1)
.max_open(100)
.into_iter()
.map(|r| {
r.map(|e| PathBuf::from(e.path()))
.chain_err(|| SE::from_kind(SEK::FileError))
})
.fold(Ok(vec![]), |acc, e| {
acc.and_then(move |mut a| {
a.push(e?);
Ok(a)
})
r.map(|e| PathBuf::from(e.path())).chain_err(|| SE::from_kind(SEK::FileError))
});
Ok(PathIterator::new(Box::new(i?.into_iter())))
Ok(PathIterator::new(Box::new(i)))
}
}

View file

@ -183,15 +183,15 @@ impl FileAbstraction for InMemoryFileAbstraction {
fn pathes_recursively(&self, _basepath: PathBuf) -> Result<PathIterator, SE> {
debug!("Getting all pathes");
let keys : Vec<PathBuf> = self
let keys : Vec<Result<PathBuf, SE>> = self
.backend()
.lock()
.map_err(|_| SE::from_kind(SEK::FileError))?
.get_mut()
.keys()
.map(PathBuf::from)
.collect();
// we collect here as this happens only in tests and in memory anyways, so no problem
.map(Ok)
.collect(); // we have to collect() because of the lock() above.
Ok(PathIterator::new(Box::new(keys.into_iter())))
}

View file

@ -19,19 +19,21 @@
use std::path::PathBuf;
use error::Result;
/// A wrapper for an iterator over `PathBuf`s
pub struct PathIterator(Box<Iterator<Item = PathBuf>>);
pub struct PathIterator(Box<Iterator<Item = Result<PathBuf>>>);
impl PathIterator {
pub fn new(iter: Box<Iterator<Item = PathBuf>>) -> PathIterator {
pub fn new(iter: Box<Iterator<Item = Result<PathBuf>>>) -> PathIterator {
PathIterator(iter)
}
}
impl Iterator for PathIterator {
type Item = PathBuf;
type Item = Result<PathBuf>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()