Explorar el Código

blockstore: Implement get_range() to retrieve a range iterator.

parazyd hace 4 años
padre
commit
8864a4c564
Se han modificado 1 ficheros con 19 adiciones y 0 borrados
  1. 19 0
      src/blockchain2/blockstore.rs

+ 19 - 0
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)
+    }
 }