Skip to content

Commit

Permalink
refactor(store): add iter_versioned_changesets
Browse files Browse the repository at this point in the history
This allows the use of one iterator for unversioned changesets and the
other iterator for the versioned ones.
  • Loading branch information
nymius committed Aug 2, 2024
1 parent d07be74 commit 4891b28
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions crates/file_store/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ where
}
}

/// Iterates over the stored **versioned** changesets from first to last, changing the seek
/// position at each iteration.
///
/// The iterator may fail to read an entry and therefore return an error. However, the first time
/// it returns an error will be the last. After doing so, the iterator will always yield `None`.
///
/// **WARNING**: This method changes the write position in the underlying file. You should
/// always iterate over all entries until `None` is returned if you want your next write to go
/// at the end; otherwise, you will write over existing entries.
pub fn iter_versioned_changesets(&mut self) -> EntryIter<V> {
EntryIter::new(self.magic_len as u64, &mut self.db_file)
}

/// Iterates over the stored changeset from first to last, changing the seek position at each
/// iteration.
///
Expand All @@ -126,7 +139,7 @@ where
/// **WARNING**: This method changes the write position in the underlying file. You should
/// always iterate over all entries until `None` is returned if you want your next write to go
/// at the end; otherwise, you will write over existing entries.
pub fn iter_changesets(&mut self) -> EntryIter<V> {
pub fn iter_changesets(&mut self) -> EntryIter<C> {
EntryIter::new(self.magic_len as u64, &mut self.db_file)
}

Expand Down Expand Up @@ -158,13 +171,13 @@ where
}),
};
// Prepare C changeset deserializer
let unversioned_parsed_changeset =
EntryIter::<C>::new(self.magic_len as u64, &mut self.db_file)
.try_fold(Option::<C>::None, changeset_aggregator);
let unversioned_parsed_changeset = self
.iter_changesets()
.try_fold(Option::<C>::None, changeset_aggregator);

// But try first versioned V changeset deserializer and if it fails, proceed with C
// unversioned deserializer
self.iter_changesets()
self.iter_versioned_changesets()
.try_fold(Option::<C>::None, |acc, x| {
changeset_aggregator(acc, x.map(|v| v.into()))
})
Expand Down

0 comments on commit 4891b28

Please sign in to comment.