serai_db/
lib.rs

1mod create_db;
2pub use create_db::*;
3
4mod mem;
5pub use mem::*;
6
7#[cfg(feature = "rocksdb")]
8mod rocks;
9#[cfg(feature = "rocksdb")]
10pub use rocks::{RocksDB, new_rocksdb};
11
12#[cfg(feature = "parity-db")]
13mod parity_db;
14#[cfg(feature = "parity-db")]
15pub use parity_db::{ParityDb, new_parity_db};
16
17/// An object implementing `get`.
18pub trait Get {
19  /// Get a value from the database.
20  fn get(&self, key: impl AsRef<[u8]>) -> Option<Vec<u8>>;
21}
22
23/// An atomic database transaction.
24///
25/// A transaction is only required to atomically commit. It is not required that two `Get` calls
26/// made with the same transaction return the same result, if another transaction wrote to that
27/// key.
28///
29/// If two transactions are created, and both write (including deletions) to the same key, behavior
30/// is undefined. The transaction may block, deadlock, panic, overwrite one of the two values
31/// randomly, or any other action, at time of write or at time of commit.
32#[must_use]
33pub trait DbTxn: Send + Get {
34  /// Write a value to this key.
35  fn put(&mut self, key: impl AsRef<[u8]>, value: impl AsRef<[u8]>);
36  /// Delete the value from this key.
37  fn del(&mut self, key: impl AsRef<[u8]>);
38  /// Commit this transaction.
39  fn commit(self);
40}
41
42/// A database supporting atomic transaction.
43pub trait Db: 'static + Send + Sync + Clone + Get {
44  /// The type representing a database transaction.
45  type Transaction<'a>: DbTxn;
46  /// Calculate a key for a database entry.
47  ///
48  /// Keys are separated by the database, the item within the database, and the item's key itself.
49  fn key(db_dst: &'static [u8], item_dst: &'static [u8], key: impl AsRef<[u8]>) -> Vec<u8> {
50    let db_len = u8::try_from(db_dst.len()).unwrap();
51    let dst_len = u8::try_from(item_dst.len()).unwrap();
52    [[db_len].as_ref(), db_dst, [dst_len].as_ref(), item_dst, key.as_ref()].concat()
53  }
54  /// Open a new transaction.
55  fn txn(&mut self) -> Self::Transaction<'_>;
56}