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> { fn pathes_recursively(&self, basepath: PathBuf) -> Result<PathIterator, SE> {
use walkdir::WalkDir; use walkdir::WalkDir;
let i : Result<Vec<PathBuf>, SE> = WalkDir::new(basepath) let i = WalkDir::new(basepath)
.min_depth(1) .min_depth(1)
.max_open(100)
.into_iter() .into_iter()
.map(|r| { .map(|r| {
r.map(|e| PathBuf::from(e.path())) r.map(|e| PathBuf::from(e.path())).chain_err(|| SE::from_kind(SEK::FileError))
.chain_err(|| SE::from_kind(SEK::FileError))
})
.fold(Ok(vec![]), |acc, e| {
acc.and_then(move |mut a| {
a.push(e?);
Ok(a)
})
}); });
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> { fn pathes_recursively(&self, _basepath: PathBuf) -> Result<PathIterator, SE> {
debug!("Getting all pathes"); debug!("Getting all pathes");
let keys : Vec<PathBuf> = self let keys : Vec<Result<PathBuf, SE>> = self
.backend() .backend()
.lock() .lock()
.map_err(|_| SE::from_kind(SEK::FileError))? .map_err(|_| SE::from_kind(SEK::FileError))?
.get_mut() .get_mut()
.keys() .keys()
.map(PathBuf::from) .map(PathBuf::from)
.collect(); .map(Ok)
// we collect here as this happens only in tests and in memory anyways, so no problem .collect(); // we have to collect() because of the lock() above.
Ok(PathIterator::new(Box::new(keys.into_iter()))) Ok(PathIterator::new(Box::new(keys.into_iter())))
} }

View file

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