From eb3843e0cf5cb7bf9ffcc3db906ac2721d468ee6 Mon Sep 17 00:00:00 2001 From: Eric Tu Date: Tue, 10 Sep 2024 10:12:30 -0400 Subject: [PATCH 1/3] for_each_checkpoint --- shardtree/src/store.rs | 13 +++++++++++++ shardtree/src/store/caching.rs | 6 ++++++ shardtree/src/store/memory.rs | 11 +++++++++++ 3 files changed, 30 insertions(+) diff --git a/shardtree/src/store.rs b/shardtree/src/store.rs index eadd168..2a9affb 100644 --- a/shardtree/src/store.rs +++ b/shardtree/src/store.rs @@ -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(&self, limit: usize, callback: F) -> Result<(), Self::Error> + where + F: Fn(&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. /// @@ -218,6 +224,13 @@ impl ShardStore for &mut S { S::with_checkpoints(self, limit, callback) } + fn for_each_checkpoint(&self, limit: usize, callback: F) -> Result<(), Self::Error> + where + F: Fn(&Self::CheckpointId, &Checkpoint) -> Result<(), Self::Error>, + { + S::for_each_checkpoint(self, limit, callback) + } + fn update_checkpoint_with( &mut self, checkpoint_id: &Self::CheckpointId, diff --git a/shardtree/src/store/caching.rs b/shardtree/src/store/caching.rs index 104106d..6b26345 100644 --- a/shardtree/src/store/caching.rs +++ b/shardtree/src/store/caching.rs @@ -184,6 +184,12 @@ where { self.cache.with_checkpoints(limit, callback) } + fn for_each_checkpoint(&self, limit: usize, callback: F) -> Result<(), Self::Error> + where + F: Fn(&Self::CheckpointId, &Checkpoint) -> Result<(), Self::Error>, + { + self.cache.for_each_checkpoint(limit, callback) + } fn update_checkpoint_with( &mut self, diff --git a/shardtree/src/store/memory.rs b/shardtree/src/store/memory.rs index bf3d2fd..d8f1f6d 100644 --- a/shardtree/src/store/memory.rs +++ b/shardtree/src/store/memory.rs @@ -137,6 +137,17 @@ impl ShardStore for MemoryShardStore { Ok(()) } + fn for_each_checkpoint(&self, limit: usize, callback: F) -> Result<(), Self::Error> + where + F: Fn(&C, &Checkpoint) -> Result<(), Self::Error>, + { + for (cid, checkpoint) in self.checkpoints.iter().take(limit) { + callback(cid, checkpoint)? + } + + Ok(()) + } + fn update_checkpoint_with( &mut self, checkpoint_id: &C, From 16eff253ad2575d48feec04f7387e6507a7dd698 Mon Sep 17 00:00:00 2001 From: Eric Tu Date: Tue, 10 Sep 2024 15:57:50 -0400 Subject: [PATCH 2/3] FnMut instead of Fn --- shardtree/src/store.rs | 4 ++-- shardtree/src/store/caching.rs | 2 +- shardtree/src/store/memory.rs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/shardtree/src/store.rs b/shardtree/src/store.rs index 2a9affb..3fa68fd 100644 --- a/shardtree/src/store.rs +++ b/shardtree/src/store.rs @@ -120,7 +120,7 @@ pub trait ShardStore { /// essentially the immutable version of `with_checkpoints`. fn for_each_checkpoint(&self, limit: usize, callback: F) -> Result<(), Self::Error> where - F: Fn(&Self::CheckpointId, &Checkpoint) -> Result<(), Self::Error>; + 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. @@ -226,7 +226,7 @@ impl ShardStore for &mut S { fn for_each_checkpoint(&self, limit: usize, callback: F) -> Result<(), Self::Error> where - F: Fn(&Self::CheckpointId, &Checkpoint) -> Result<(), Self::Error>, + F: FnMut(&Self::CheckpointId, &Checkpoint) -> Result<(), Self::Error>, { S::for_each_checkpoint(self, limit, callback) } diff --git a/shardtree/src/store/caching.rs b/shardtree/src/store/caching.rs index 6b26345..9078094 100644 --- a/shardtree/src/store/caching.rs +++ b/shardtree/src/store/caching.rs @@ -186,7 +186,7 @@ where } fn for_each_checkpoint(&self, limit: usize, callback: F) -> Result<(), Self::Error> where - F: Fn(&Self::CheckpointId, &Checkpoint) -> Result<(), Self::Error>, + F: FnMut(&Self::CheckpointId, &Checkpoint) -> Result<(), Self::Error>, { self.cache.for_each_checkpoint(limit, callback) } diff --git a/shardtree/src/store/memory.rs b/shardtree/src/store/memory.rs index d8f1f6d..c76a5d1 100644 --- a/shardtree/src/store/memory.rs +++ b/shardtree/src/store/memory.rs @@ -137,9 +137,9 @@ impl ShardStore for MemoryShardStore { Ok(()) } - fn for_each_checkpoint(&self, limit: usize, callback: F) -> Result<(), Self::Error> + fn for_each_checkpoint(&self, limit: usize, mut callback: F) -> Result<(), Self::Error> where - F: Fn(&C, &Checkpoint) -> Result<(), Self::Error>, + F: FnMut(&C, &Checkpoint) -> Result<(), Self::Error>, { for (cid, checkpoint) in self.checkpoints.iter().take(limit) { callback(cid, checkpoint)? From 8ccb40d088aa54947fb3be778cc6f4bfd2f172b0 Mon Sep 17 00:00:00 2001 From: Eric Tu Date: Thu, 12 Sep 2024 11:48:22 -0400 Subject: [PATCH 3/3] CHANGELOG --- shardtree/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/shardtree/CHANGELOG.md b/shardtree/CHANGELOG.md index 956b0af..9330901 100644 --- a/shardtree/CHANGELOG.md +++ b/shardtree/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to Rust's notion of ## Unreleased +### Added +- `shardtree::store::ShardStore::for_each_checkpoint` + ## [0.4.0] - 2024-08-12 This is a bugfix release that fixes a couple of subtle problems related to