avalanche_types/subnet/rpc/database/
iterator.rs

1use std::io::Result;
2
3/// ref. <https://pkg.go.dev/github.com/ava-labs/avalanchego/database#Iterator>
4#[tonic::async_trait]
5pub trait Iterator {
6    /// Attempts to move the iterator to the next key/value pair. It returns whether
7    /// the iterator successfully moved to a new key/value pair.
8    /// The iterator may return false if the underlying database has been closed
9    /// before the iteration has completed, in which case future calls to Error()
10    /// must return ErrorKind::Other, "database closed")
11    async fn next(&mut self) -> Result<bool>;
12
13    /// Returns any accumulated error. Exhausting all the key/value pairs
14    /// is not considered to be an error.
15    /// Error should be called after all key/value pairs have been exhausted ie.
16    /// after Next() has returned false.
17    async fn error(&mut self) -> Result<()>;
18
19    /// Returns the key of the current key/value pair, or empty slice if done.
20    async fn key(&self) -> Result<&[u8]>;
21
22    /// Returns the key of the current k&ey/value pair, or empty slice if done.
23    async fn value(&self) -> Result<&[u8]>;
24
25    /// Releases associated resources. Release should always succeed and
26    /// can be called multiple times without causing error.
27    async fn release(&mut self);
28}
29
30/// Helper type which defines a thread safe boxed Iterator interface.
31pub type BoxedIterator = Box<dyn Iterator + Send + Sync + 'static>;
32
33/// ref. <https://pkg.go.dev/github.com/ava-labs/avalanchego/database#Iteratee>
34#[tonic::async_trait]
35pub trait Iteratee {
36    /// Creates an iterator over the entire keyspace contained within
37    /// the key-value database.
38    async fn new_iterator(&self) -> Result<BoxedIterator>;
39
40    /// Creates an iterator over a subset of database content starting
41    /// at a particular initial key.
42    async fn new_iterator_with_start(&self, start: &[u8]) -> Result<BoxedIterator>;
43
44    /// Creates an iterator over a subset of database content with a
45    /// particular key prefix.
46    async fn new_iterator_with_prefix(&self, prefix: &[u8]) -> Result<BoxedIterator>;
47
48    /// Creates an iterator over a subset of database content with a
49    /// particular key prefix starting at a specified key.
50    async fn new_iterator_with_start_and_prefix(
51        &self,
52        start: &[u8],
53        prefix: &[u8],
54    ) -> Result<BoxedIterator>;
55}