Skip to content

Commit

Permalink
Band::iter_entries need not take exclusions
Browse files Browse the repository at this point in the history
These can be separately added to the iter
when needed.
  • Loading branch information
sourcefrog committed Jan 20, 2020
1 parent e7033b0 commit 57ea8da
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/archive.rs
Expand Up @@ -134,7 +134,7 @@ impl Archive {
let mut hs = BTreeSet::<String>::new();
for band_id in self.list_bands()? {
let band = Band::open(&self, &band_id)?;
for ie in band.iter_entries(&excludes::excludes_nothing(), &self.report)? {
for ie in band.iter_entries(&self.report)? {
for a in ie.addrs {
hs.insert(a.hash);
}
Expand Down
5 changes: 1 addition & 4 deletions src/backup.rs
Expand Up @@ -131,10 +131,7 @@ mod tests {
let band = Band::open(&af, &band_ids[0]).unwrap();
assert!(band.is_closed().unwrap());

let index_entries = band
.iter_entries(&excludes::excludes_nothing(), &report)
.unwrap()
.collect::<Vec<Entry>>();
let index_entries = band.iter_entries(&report).unwrap().collect::<Vec<Entry>>();
assert_eq!(2, index_entries.len());

let e2 = &index_entries[1];
Expand Down
8 changes: 2 additions & 6 deletions src/band.rs
Expand Up @@ -139,12 +139,8 @@ impl Band {
}

/// Return an iterator through entries in this band.
pub fn iter_entries(
&self,
excludes: &GlobSet,
report: &Report,
) -> Result<index::IndexEntryIter> {
index::IndexEntryIter::open(&self.index_dir_path, excludes, report)
pub fn iter_entries(&self, report: &Report) -> Result<index::IndexEntryIter> {
index::IndexEntryIter::open(&self.index_dir_path, report)
}

fn read_head(&self, report: &Report) -> Result<Head> {
Expand Down
29 changes: 18 additions & 11 deletions src/index.rs
Expand Up @@ -147,8 +147,8 @@ impl ReadIndex {
}

/// Make an iterator that will return all entries in this band.
pub fn iter(&self, excludes: &GlobSet, report: &Report) -> Result<IndexEntryIter> {
IndexEntryIter::open(&self.dir, excludes, report)
pub fn iter(&self, report: &Report) -> Result<IndexEntryIter> {
IndexEntryIter::open(&self.dir, report)
}
}

Expand Down Expand Up @@ -196,16 +196,21 @@ impl IndexEntryIter {
/// Create an iterator that will read all entires from an existing index.
///
/// Prefer to use `Band::index_iter` instead.
pub fn open(index_dir: &Path, excludes: &GlobSet, report: &Report) -> Result<IndexEntryIter> {
pub fn open(index_dir: &Path, report: &Report) -> Result<IndexEntryIter> {
Ok(IndexEntryIter {
dir: index_dir.to_path_buf(),
buffered_entries: Vec::<Entry>::new().into_iter().peekable(),
next_hunk_number: 0,
report: report.clone(),
excludes: excludes.clone(),
excludes: excludes::excludes_nothing(),
})
}

/// Consume this iterator and return a new one with exclusions.
pub fn with_excludes(self, excludes: globset::GlobSet) -> IndexEntryIter {
IndexEntryIter { excludes, ..self }
}

/// Return the entry for given apath, if it is present, otherwise None.
/// It follows this will also return None at the end of the index.
///
Expand Down Expand Up @@ -413,7 +418,7 @@ mod tests {
\"kind\":\"File\"}]"
);

let mut it = IndexEntryIter::open(&ib.dir, &excludes::excludes_nothing(), &report).unwrap();
let mut it = IndexEntryIter::open(&ib.dir, &report).unwrap();
let entry = it.next().expect("Get first entry");
assert_eq!(&entry.apath, "/apple");
let entry = it.next().expect("Get second entry");
Expand All @@ -432,7 +437,7 @@ mod tests {
add_an_entry(&mut ib, "/2.2");
ib.finish_hunk(&report).unwrap();

let it = IndexEntryIter::open(&ib.dir, &excludes::excludes_nothing(), &report).unwrap();
let it = IndexEntryIter::open(&ib.dir, &report).unwrap();
assert_eq!(
format!("{:?}", &it),
format!(
Expand Down Expand Up @@ -474,7 +479,9 @@ mod tests {
ib.finish_hunk(&report).unwrap();

let excludes = excludes::from_strings(&["/fo*"]).unwrap();
let it = IndexEntryIter::open(&ib.dir, &excludes, &report).unwrap();
let it = IndexEntryIter::open(&ib.dir, &report)
.unwrap()
.with_excludes(excludes);
assert_eq!(
format!("{:?}", &it),
format!(
Expand Down Expand Up @@ -502,25 +509,25 @@ mod tests {
ib.finish_hunk(&report).unwrap();

// Advance to /foo and read on from there.
let mut it = IndexEntryIter::open(&ib.dir, &excludes::excludes_nothing(), &report).unwrap();
let mut it = IndexEntryIter::open(&ib.dir, &report).unwrap();
assert_eq!(it.advance_to(&Apath::from("/foo")).unwrap().apath, "/foo");
assert_eq!(it.next().unwrap().apath, "/foobar");
assert_eq!(it.next().unwrap().apath, "/g01");

// Advance to before /g01
let mut it = IndexEntryIter::open(&ib.dir, &excludes::excludes_nothing(), &report).unwrap();
let mut it = IndexEntryIter::open(&ib.dir, &report).unwrap();
assert_eq!(it.advance_to(&Apath::from("/fxxx")), None);
assert_eq!(it.next().unwrap().apath, "/g01");
assert_eq!(it.next().unwrap().apath, "/g02");

// Advance to before the first entry
let mut it = IndexEntryIter::open(&ib.dir, &excludes::excludes_nothing(), &report).unwrap();
let mut it = IndexEntryIter::open(&ib.dir, &report).unwrap();
assert_eq!(it.advance_to(&Apath::from("/aaaa")), None);
assert_eq!(it.next().unwrap().apath, "/bar");
assert_eq!(it.next().unwrap().apath, "/foo");

// Advance to after the last entry
let mut it = IndexEntryIter::open(&ib.dir, &excludes::excludes_nothing(), &report).unwrap();
let mut it = IndexEntryIter::open(&ib.dir, &report).unwrap();
assert_eq!(it.advance_to(&Apath::from("/zz")), None);
assert_eq!(it.next(), None);
}
Expand Down
6 changes: 1 addition & 5 deletions src/output.rs
Expand Up @@ -84,11 +84,7 @@ impl<'a> IndexDump<'a> {
impl<'a> ShowArchive for IndexDump<'a> {
fn show_archive(&self, archive: &Archive) -> Result<()> {
let report = archive.report();
let index_entries = self
.band
.iter_entries(&excludes::excludes_nothing(), &report)
.unwrap()
.collect::<Vec<Entry>>();
let index_entries = self.band.iter_entries(&report)?.collect::<Vec<Entry>>();
let output = serde_json::to_string_pretty(&index_entries)
.context(errors::SerializeIndex { path: "-" })?;
report.print(&output);
Expand Down
5 changes: 4 additions & 1 deletion src/stored_tree.rs
Expand Up @@ -121,7 +121,10 @@ impl ReadTree for StoredTree {

/// Return an iter of index entries in this stored tree.
fn iter_entries(&self, report: &Report) -> Result<index::IndexEntryIter> {
self.band.iter_entries(&self.excludes, report)
Ok(self
.band
.iter_entries(report)?
.with_excludes(self.excludes.clone()))
}

fn file_contents(&self, entry: &Entry) -> Result<Self::R> {
Expand Down
6 changes: 1 addition & 5 deletions tests/integration.rs
Expand Up @@ -64,11 +64,7 @@ fn check_backup(af: &ScratchArchive, report: &Report) {
let band = Band::open(&af, &band_ids[0]).unwrap();
assert!(band.is_closed().unwrap());

let index_entries = band
.index()
.iter(&excludes::excludes_nothing(), &report)
.unwrap()
.collect::<Vec<Entry>>();
let index_entries = band.iter_entries(&report).unwrap().collect::<Vec<Entry>>();
assert_eq!(2, index_entries.len());

let root_entry = &index_entries[0];
Expand Down

0 comments on commit 57ea8da

Please sign in to comment.