2023-11-05 18:47:24 +04:00
|
|
|
mod create_db;
|
|
|
|
|
pub use create_db::*;
|
|
|
|
|
|
2023-07-13 19:09:11 -04:00
|
|
|
mod mem;
|
|
|
|
|
pub use mem::*;
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "rocksdb")]
|
|
|
|
|
mod rocks;
|
2023-07-25 21:39:29 -04:00
|
|
|
#[cfg(feature = "rocksdb")]
|
|
|
|
|
pub use rocks::{RocksDB, new_rocksdb};
|
2023-04-14 11:41:01 -04:00
|
|
|
|
2023-11-30 04:22:37 -05:00
|
|
|
#[cfg(feature = "parity-db")]
|
|
|
|
|
mod parity_db;
|
|
|
|
|
#[cfg(feature = "parity-db")]
|
|
|
|
|
pub use parity_db::{ParityDb, new_parity_db};
|
|
|
|
|
|
2024-12-26 02:32:14 -05:00
|
|
|
/// An object implementing `get`.
|
2023-07-13 19:09:11 -04:00
|
|
|
pub trait Get {
|
2024-12-26 02:32:14 -05:00
|
|
|
/// Get a value from the database.
|
2023-04-14 11:41:01 -04:00
|
|
|
fn get(&self, key: impl AsRef<[u8]>) -> Option<Vec<u8>>;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-26 02:32:14 -05:00
|
|
|
/// An atomic database transaction.
|
|
|
|
|
///
|
|
|
|
|
/// A transaction is only required to atomically commit. It is not required that two `Get` calls
|
|
|
|
|
/// made with the same transaction return the same result, if another transaction wrote to that
|
|
|
|
|
/// key.
|
|
|
|
|
///
|
|
|
|
|
/// If two transactions are created, and both write (including deletions) to the same key, behavior
|
|
|
|
|
/// is undefined. The transaction may block, deadlock, panic, overwrite one of the two values
|
|
|
|
|
/// randomly, or any other action, at time of write or at time of commit.
|
2023-04-14 15:04:26 -04:00
|
|
|
#[must_use]
|
2023-07-13 19:09:11 -04:00
|
|
|
pub trait DbTxn: Send + Get {
|
2024-12-26 02:32:14 -05:00
|
|
|
/// Write a value to this key.
|
2023-04-14 11:41:01 -04:00
|
|
|
fn put(&mut self, key: impl AsRef<[u8]>, value: impl AsRef<[u8]>);
|
2024-12-26 02:32:14 -05:00
|
|
|
/// Delete the value from this key.
|
2023-04-14 11:41:01 -04:00
|
|
|
fn del(&mut self, key: impl AsRef<[u8]>);
|
2024-12-26 02:32:14 -05:00
|
|
|
/// Commit this transaction.
|
2023-04-14 11:41:01 -04:00
|
|
|
fn commit(self);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-26 02:32:14 -05:00
|
|
|
/// A database supporting atomic transaction.
|
2023-07-13 19:09:11 -04:00
|
|
|
pub trait Db: 'static + Send + Sync + Clone + Get {
|
2024-12-26 02:32:14 -05:00
|
|
|
/// The type representing a database transaction.
|
2023-04-14 11:41:01 -04:00
|
|
|
type Transaction<'a>: DbTxn;
|
2024-12-26 02:32:14 -05:00
|
|
|
/// Calculate a key for a database entry.
|
|
|
|
|
///
|
|
|
|
|
/// Keys are separated by the database, the item within the database, and the item's key itself.
|
2023-04-14 11:41:01 -04:00
|
|
|
fn key(db_dst: &'static [u8], item_dst: &'static [u8], key: impl AsRef<[u8]>) -> Vec<u8> {
|
|
|
|
|
let db_len = u8::try_from(db_dst.len()).unwrap();
|
|
|
|
|
let dst_len = u8::try_from(item_dst.len()).unwrap();
|
2023-07-08 11:29:05 -04:00
|
|
|
[[db_len].as_ref(), db_dst, [dst_len].as_ref(), item_dst, key.as_ref()].concat()
|
2023-04-14 11:41:01 -04:00
|
|
|
}
|
2024-12-26 02:32:14 -05:00
|
|
|
/// Open a new transaction.
|
2023-04-14 11:41:01 -04:00
|
|
|
fn txn(&mut self) -> Self::Transaction<'_>;
|
|
|
|
|
}
|