Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Immutable For Each on ShardStore Checkpoints #112

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions shardtree/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ pub trait ShardStore {
where
F: FnMut(&Self::CheckpointId, &Checkpoint) -> Result<(), Self::Error>;

/// Calls the given callback for each checkpoint in `CheckpointId` order. This is
/// essentially the immutable version of `with_checkpoints`.
fn for_each_checkpoint<F>(&self, limit: usize, callback: F) -> Result<(), Self::Error>
where
F: FnMut(&Self::CheckpointId, &Checkpoint) -> Result<(), Self::Error>;

/// Update the checkpoint having the given identifier by mutating it with the provided
/// function, and persist the updated checkpoint to the data store.
///
Expand Down Expand Up @@ -218,6 +224,13 @@ impl<S: ShardStore> ShardStore for &mut S {
S::with_checkpoints(self, limit, callback)
}

fn for_each_checkpoint<F>(&self, limit: usize, callback: F) -> Result<(), Self::Error>
where
F: FnMut(&Self::CheckpointId, &Checkpoint) -> Result<(), Self::Error>,
{
S::for_each_checkpoint(self, limit, callback)
}

fn update_checkpoint_with<F>(
&mut self,
checkpoint_id: &Self::CheckpointId,
Expand Down
6 changes: 6 additions & 0 deletions shardtree/src/store/caching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ where
{
self.cache.with_checkpoints(limit, callback)
}
fn for_each_checkpoint<F>(&self, limit: usize, callback: F) -> Result<(), Self::Error>
where
F: FnMut(&Self::CheckpointId, &Checkpoint) -> Result<(), Self::Error>,
{
self.cache.for_each_checkpoint(limit, callback)
}

fn update_checkpoint_with<F>(
&mut self,
Expand Down
11 changes: 11 additions & 0 deletions shardtree/src/store/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ impl<H: Clone, C: Clone + Ord> ShardStore for MemoryShardStore<H, C> {
Ok(())
}

fn for_each_checkpoint<F>(&self, limit: usize, mut callback: F) -> Result<(), Self::Error>
where
F: FnMut(&C, &Checkpoint) -> Result<(), Self::Error>,
{
for (cid, checkpoint) in self.checkpoints.iter().take(limit) {
callback(cid, checkpoint)?
}

Ok(())
}

fn update_checkpoint_with<F>(
&mut self,
checkpoint_id: &C,
Expand Down
Loading