cita_database/
database.rs

1use crate::error::DatabaseError;
2use rocksdb::DBIterator;
3use std::result;
4
5pub type Result<T> = result::Result<T, DatabaseError>;
6
7/// Specify the category of data stored, and users can store the data in a
8/// decentralized manner.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum DataCategory {
11    // State
12    State,
13    // Block headers
14    Headers,
15    // Block bodies
16    Bodies,
17    // Extras: Block hash, receipt, and so on
18    Extra,
19    // TBD. Traces
20    Trace,
21    // TBD. Empty accounts bloom filter
22    AccountBloom,
23    // Keep it for compatibility
24    Other,
25}
26
27pub trait Database: Send + Sync {
28    fn get(&self, category: Option<DataCategory>, key: &[u8]) -> Result<Option<Vec<u8>>>;
29
30    fn get_batch(
31        &self,
32        category: Option<DataCategory>,
33        keys: &[Vec<u8>],
34    ) -> Result<Vec<Option<Vec<u8>>>>;
35
36    fn insert(&self, category: Option<DataCategory>, key: Vec<u8>, value: Vec<u8>) -> Result<()>;
37
38    fn insert_batch(
39        &self,
40        category: Option<DataCategory>,
41        keys: Vec<Vec<u8>>,
42        values: Vec<Vec<u8>>,
43    ) -> Result<()>;
44
45    fn contains(&self, category: Option<DataCategory>, key: &[u8]) -> Result<bool>;
46
47    fn remove(&self, category: Option<DataCategory>, key: &[u8]) -> Result<()>;
48
49    fn remove_batch(&self, category: Option<DataCategory>, keys: &[Vec<u8>]) -> Result<()>;
50
51    fn restore(&mut self, new_db: &str) -> Result<()>;
52
53    // TODO Replace the DBIterator
54    fn iterator(&self, category: Option<DataCategory>) -> Option<DBIterator>;
55
56    fn close(&mut self);
57    fn flush(&self) -> Result<()>;
58}