use bytes::Bytes;
use smallvec::SmallVec;
use std::io;
mod io_stats;
pub const PREFIX_LEN: usize = 12;
pub type DBValue = Vec<u8>;
pub type DBKey = SmallVec<[u8; 32]>;
pub use io_stats::{IoStats, Kind as IoStatsKind};
#[derive(Default, Clone, PartialEq)]
pub struct DBTransaction {
pub ops: Vec<DBOp>,
}
#[derive(Clone, PartialEq)]
pub enum DBOp {
Insert { col: u32, key: DBKey, value: DBValue },
Delete { col: u32, key: DBKey },
}
impl DBOp {
pub fn key(&self) -> &[u8] {
match *self {
DBOp::Insert { ref key, .. } => key,
DBOp::Delete { ref key, .. } => key,
}
}
pub fn col(&self) -> u32 {
match *self {
DBOp::Insert { col, .. } => col,
DBOp::Delete { col, .. } => col,
}
}
}
impl DBTransaction {
pub fn new() -> DBTransaction {
DBTransaction::with_capacity(256)
}
pub fn with_capacity(cap: usize) -> DBTransaction {
DBTransaction { ops: Vec::with_capacity(cap) }
}
pub fn put(&mut self, col: u32, key: &[u8], value: &[u8]) {
self.ops.push(DBOp::Insert { col, key: DBKey::from_slice(key), value: value.to_vec() })
}
pub fn put_vec(&mut self, col: u32, key: &[u8], value: Bytes) {
self.ops.push(DBOp::Insert { col, key: DBKey::from_slice(key), value });
}
pub fn delete(&mut self, col: u32, key: &[u8]) {
self.ops.push(DBOp::Delete { col, key: DBKey::from_slice(key) });
}
}
pub trait KeyValueDB: Sync + Send + parity_util_mem::MallocSizeOf {
fn transaction(&self) -> DBTransaction {
DBTransaction::new()
}
fn get(&self, col: u32, key: &[u8]) -> io::Result<Option<DBValue>>;
fn get_by_prefix(&self, col: u32, prefix: &[u8]) -> Option<Box<[u8]>>;
fn write_buffered(&self, transaction: DBTransaction);
fn write(&self, transaction: DBTransaction) -> io::Result<()> {
self.write_buffered(transaction);
self.flush()
}
fn flush(&self) -> io::Result<()>;
fn iter<'a>(&'a self, col: u32) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + 'a>;
fn iter_from_prefix<'a>(
&'a self,
col: u32,
prefix: &'a [u8],
) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + 'a>;
fn restore(&self, new_db: &str) -> io::Result<()>;
fn io_stats(&self, _kind: IoStatsKind) -> IoStats {
IoStats::empty()
}
}