avalanche_types/subnet/rpc/database/
mod.rs1pub 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
29pub type BoxedDatabase = Box<dyn Database + Send + Sync + 'static>;
31
32#[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#[tonic::async_trait]
44trait Commitable {
45 async fn commit(&mut self) -> Result<()>;
47 async fn abort(&self) -> Result<()>;
49 async fn commit_batch(&mut self) -> Result<BoxedBatch>;
54}
55
56pub trait CloneBox {
57 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 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 let mut cloned_db = db.clone();
90 let resp = cloned_db.delete("foo".as_bytes()).await;
91 assert!(resp.is_ok());
92
93 let resp = cloned_db.get("foo".as_bytes()).await;
95 assert!(resp.is_err());
96}