From 8864a4c564520882ac7cf6ec9dd2cfbd631cef5f Mon Sep 17 00:00:00 2001 From: parazyd Date: Sun, 3 Apr 2022 16:08:29 +0200 Subject: [PATCH] blockstore: Implement get_range() to retrieve a range iterator. --- src/blockchain2/blockstore.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/blockchain2/blockstore.rs b/src/blockchain2/blockstore.rs index a1083a797..35695bb97 100644 --- a/src/blockchain2/blockstore.rs +++ b/src/blockchain2/blockstore.rs @@ -112,4 +112,23 @@ impl BlockStore { Ok(None) } + + /// Retrieve an iterator over a range of blockhashes. + /// Usage: + /// ``` + /// let mut r = get_range(foo, bar); + /// while let Some((k, v)) = r.next() { + /// let hash_bytes: [u8; 32] = k.as_ref().try_into().unwrap(); + /// let block = deserialize(&v)?; + /// } + /// ``` + /// When iterating, take care of potential memory limitations if you're + /// storing results in memory. For blockchain sync, it should probably + /// be done in chunks. + pub fn get_range(&self, start: blake3::Hash, end: blake3::Hash) -> sled::Iter { + let start: &[u8] = start.as_bytes().as_ref(); + let end: &[u8] = end.as_bytes().as_ref(); + + self.0.range(start..end) + } }