avalanche_types/subnet/rpc/database/
mod.rs

1pub mod batch;
2pub mod corruptabledb;
3pub mod iterator;
4pub mod manager;
5pub mod memdb;
6pub mod nodb;
7pub mod rpcdb;
8pub mod versiondb;
9
10use std::io::Result;
11
12use crate::subnet::rpc::health::Checkable;
13
14use self::batch::BoxedBatch;
15
16pub const MAX_BATCH_SIZE: usize = 128 * 1000;
17
18#[tonic::async_trait]
19pub trait Closer {
20    async fn close(&self) -> Result<()>;
21}
22
23#[tonic::async_trait]
24pub trait Database:
25    batch::Batcher + CloneBox + KeyValueReaderWriterDeleter + Closer + Checkable + iterator::Iteratee
26{
27}
28
29/// Helper type which defines a thread safe boxed Database trait.
30pub type BoxedDatabase = Box<dyn Database + Send + Sync + 'static>;
31
32/// ref. <https://pkg.go.dev/github.com/ava-labs/avalanchego/database#KeyValueReaderWriterDeleter>
33#[tonic::async_trait]
34pub trait KeyValueReaderWriterDeleter {
35    async fn has(&self, key: &[u8]) -> Result<bool>;
36    async fn get(&self, key: &[u8]) -> Result<Vec<u8>>;
37    async fn put(&mut self, key: &[u8], value: &[u8]) -> Result<()>;
38    async fn delete(&mut self, key: &[u8]) -> Result<()>;
39}
40
41// Trait that specifies that something may be
42// committed.
43#[tonic::async_trait]
44trait Commitable {
45    /// Writes all the operations of this database to the underlying database.
46    async fn commit(&mut self) -> Result<()>;
47    /// Abort all changes to the underlying database.
48    async fn abort(&self) -> Result<()>;
49    /// Returns a batch that contains all uncommitted puts/deletes.  Calling
50    /// write() on the returned batch causes the puts/deletes to be written to
51    /// the underlying database. The returned batch should be written before
52    /// future calls to this DB unless the batch will never be written.
53    async fn commit_batch(&mut self) -> Result<BoxedBatch>;
54}
55
56pub trait CloneBox {
57    /// Returns a Boxed clone of the underlying Database.
58    fn clone_box(&self) -> BoxedDatabase;
59}
60
61impl<T> CloneBox for T
62where
63    T: 'static + Database + Clone + Send + Sync,
64{
65    fn clone_box(&self) -> BoxedDatabase {
66        Box::new(self.clone())
67    }
68}
69
70impl Clone for BoxedDatabase {
71    fn clone(&self) -> BoxedDatabase {
72        self.clone_box()
73    }
74}
75
76#[tonic::async_trait]
77pub trait VersionedDatabase {
78    async fn close(&mut self) -> Result<()>;
79}
80
81#[tokio::test]
82async fn clone_box_test() {
83    // create box and mutate underlying hashmap
84    let mut db = memdb::Database::new_boxed();
85    let resp = db.put("foo".as_bytes(), "bar".as_bytes()).await;
86    assert!(resp.is_ok());
87
88    // clone and mutate
89    let mut cloned_db = db.clone();
90    let resp = cloned_db.delete("foo".as_bytes()).await;
91    assert!(resp.is_ok());
92
93    // verify mutation
94    let resp = cloned_db.get("foo".as_bytes()).await;
95    assert!(resp.is_err());
96}