massa_db_exports/
controller.rs

1use crate::{DBBatch, Key, MassaDBError, StreamBatch, Value};
2use massa_hash::{HashXof, HASH_XOF_SIZE_BYTES};
3use massa_models::{error::ModelsError, slot::Slot, streaming_step::StreamingStep};
4use parking_lot::RwLock;
5use std::path::PathBuf;
6use std::{fmt::Debug, sync::Arc};
7
8#[cfg(feature = "test-exports")]
9use std::collections::BTreeMap;
10
11pub type ShareableMassaDBController = Arc<RwLock<Box<dyn MassaDBController>>>;
12
13/// Controller trait for the MassaDB
14/// TODO: MOCK IT WITH MOCKALL. HAVING LIFETIMES ERRORS WITH AUTO MOCK
15pub trait MassaDBController: Send + Sync + Debug {
16    /// Creates a new hard copy of the DB, for the given slot
17    fn backup_db(&self, slot: Slot) -> PathBuf;
18
19    /// Get the current change_id attached to the database.
20    fn get_change_id(&self) -> Result<Slot, ModelsError>;
21
22    /// Set the initial change_id. This function should only be called at startup/reset, as it does not batch this set with other changes.
23    fn set_initial_change_id(&self, change_id: Slot);
24
25    /// Writes the batch to the DB
26    fn write_batch(&mut self, batch: DBBatch, versioning_batch: DBBatch, change_id: Option<Slot>);
27
28    /// Utility function to put / update a key & value in the batch
29    fn put_or_update_entry_value(&self, batch: &mut DBBatch, key: Vec<u8>, value: &[u8]);
30
31    /// Utility function to delete a key & value in the batch
32    fn delete_key(&self, batch: &mut DBBatch, key: Vec<u8>);
33
34    /// Utility function to delete all keys in a prefix
35    fn delete_prefix(&mut self, prefix: &str, handle_str: &str, change_id: Option<Slot>);
36
37    /// Reset the database, and attach it to the given slot.
38    /// Note that this does not clear the db's content, only the change_id and the history.
39    fn reset_slot_and_history(&mut self, slot: Slot);
40
41    /// Exposes RocksDB's "get_cf" function
42    fn get_cf(&self, handle_cf: &str, key: Key) -> Result<Option<Value>, MassaDBError>;
43
44    /// Exposes RocksDB's "multi_get_cf" function
45    fn multi_get_cf(&self, query: Vec<(&str, Key)>) -> Vec<Result<Option<Value>, MassaDBError>>;
46
47    /// Exposes RocksDB's "iterator_cf" function, without filling up the read cache
48    fn iterator_cf_for_full_db_traversal(
49        &self,
50        handle_cf: &str,
51        mode: MassaIteratorMode,
52    ) -> Box<dyn Iterator<Item = (Key, Value)> + '_>;
53
54    /// Exposes RocksDB's "iterator_cf" function
55    fn iterator_cf(
56        &self,
57        handle_cf: &str,
58        mode: MassaIteratorMode,
59    ) -> Box<dyn Iterator<Item = (Key, Value)> + '_>;
60
61    /// Exposes RocksDB's "prefix_iterator_cf" function
62    fn prefix_iterator_cf(
63        &self,
64        handle_cf: &str,
65        prefix: &[u8],
66    ) -> Box<dyn Iterator<Item = (Key, Value)> + '_>;
67
68    /// Get the current extended state hash of the database
69    fn get_xof_db_hash(&self) -> HashXof<HASH_XOF_SIZE_BYTES>;
70
71    /// Flushes the underlying db.
72    fn flush(&self) -> Result<(), MassaDBError>;
73
74    /// Write a stream_batch of database entries received from a bootstrap server
75    fn write_batch_bootstrap_client(
76        &mut self,
77        stream_changes: StreamBatch<Slot>,
78        stream_changes_versioning: StreamBatch<Slot>,
79    ) -> Result<(StreamingStep<Key>, StreamingStep<Key>), MassaDBError>;
80
81    /// Used for bootstrap servers (get a new batch of data from STATE_CF to stream to the client)
82    ///
83    /// Returns a StreamBatch<Slot>
84    fn get_batch_to_stream(
85        &self,
86        last_state_step: &StreamingStep<Vec<u8>>,
87        last_change_id: Option<Slot>,
88    ) -> Result<StreamBatch<Slot>, MassaDBError>;
89
90    /// Used for bootstrap servers (get a new batch of data from VERSIONING_CF to stream to the client)
91    ///
92    /// Returns a StreamBatch<Slot>
93    fn get_versioning_batch_to_stream(
94        &self,
95        last_versioning_step: &StreamingStep<Vec<u8>>,
96        last_change_id: Option<Slot>,
97    ) -> Result<StreamBatch<Slot>, MassaDBError>;
98
99    /// Get the total size of the change history and the change versioning history respectively
100    fn get_change_history_sizes(&self) -> (usize, usize);
101
102    /// Used in test to compare a prebuilt ledger with a ledger that has been built by the code
103    #[cfg(feature = "test-exports")]
104    fn get_entire_database(&self) -> Vec<BTreeMap<Vec<u8>, Vec<u8>>>;
105}
106
107/// Similar to RocksDB's IteratorMode
108pub enum MassaIteratorMode<'a> {
109    Start,
110    End,
111    From(&'a [u8], MassaDirection),
112}
113
114/// Similar to RocksDB's Direction
115pub enum MassaDirection {
116    Forward,
117    Reverse,
118}