pub mod error;
mod mem;
mod tetsy_kvdb;
pub use mem::MemDb;
pub use crate::tetsy_kvdb::as_database;
pub type ColumnId = u32;
#[derive(Clone)]
pub enum Change<H> {
Set(ColumnId, Vec<u8>, Vec<u8>),
Remove(ColumnId, Vec<u8>),
Store(H, Vec<u8>),
Release(H),
}
pub enum ChangeRef<'a, H> {
Set(ColumnId, &'a [u8], &'a [u8]),
Remove(ColumnId, &'a [u8]),
Store(H, &'a [u8]),
Release(H),
}
#[derive(Default, Clone)]
pub struct Transaction<H>(pub Vec<Change<H>>);
impl<H> Transaction<H> {
pub fn new() -> Self {
Transaction(Vec::new())
}
pub fn set(&mut self, col: ColumnId, key: &[u8], value: &[u8]) {
self.0.push(Change::Set(col, key.to_vec(), value.to_vec()))
}
pub fn set_from_vec(&mut self, col: ColumnId, key: &[u8], value: Vec<u8>) {
self.0.push(Change::Set(col, key.to_vec(), value))
}
pub fn remove(&mut self, col: ColumnId, key: &[u8]) {
self.0.push(Change::Remove(col, key.to_vec()))
}
pub fn store(&mut self, hash: H, preimage: &[u8]) {
self.0.push(Change::Store(hash, preimage.to_vec()))
}
pub fn release(&mut self, hash: H) {
self.0.push(Change::Release(hash))
}
}
pub trait Database<H: Clone>: Send + Sync {
fn commit(&self, transaction: Transaction<H>) -> error::Result<()> {
for change in transaction.0.into_iter() {
match change {
Change::Set(col, key, value) => self.set(col, &key, &value),
Change::Remove(col, key) => self.remove(col, &key),
Change::Store(hash, preimage) => self.store(&hash, &preimage),
Change::Release(hash) => self.release(&hash),
}?;
}
Ok(())
}
fn commit_ref<'a>(&self, transaction: &mut dyn Iterator<Item=ChangeRef<'a, H>>) -> error::Result<()> {
let mut tx = Transaction::new();
for change in transaction {
match change {
ChangeRef::Set(col, key, value) => tx.set(col, key, value),
ChangeRef::Remove(col, key) => tx.remove(col, key),
ChangeRef::Store(hash, preimage) => tx.store(hash, preimage),
ChangeRef::Release(hash) => tx.release(hash),
}
}
self.commit(tx)
}
fn get(&self, col: ColumnId, key: &[u8]) -> Option<Vec<u8>>;
fn with_get(&self, col: ColumnId, key: &[u8], f: &mut dyn FnMut(&[u8])) {
self.get(col, key).map(|v| f(&v));
}
fn set(&self, col: ColumnId, key: &[u8], value: &[u8]) -> error::Result<()> {
let mut t = Transaction::new();
t.set(col, key, value);
self.commit(t)
}
fn remove(&self, col: ColumnId, key: &[u8]) -> error::Result<()> {
let mut t = Transaction::new();
t.remove(col, key);
self.commit(t)
}
fn lookup(&self, hash: &H) -> Option<Vec<u8>>;
fn with_lookup(&self, hash: &H, f: &mut dyn FnMut(&[u8])) {
self.lookup(hash).map(|v| f(&v));
}
fn store(&self, hash: &H, preimage: &[u8]) -> error::Result<()> {
let mut t = Transaction::new();
t.store(hash.clone(), preimage);
self.commit(t)
}
fn release(&self, hash: &H) -> error::Result<()> {
let mut t = Transaction::new();
t.release(hash.clone());
self.commit(t)
}
}
impl<H> std::fmt::Debug for dyn Database<H> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Database")
}
}
pub fn with_get<R, H: Clone>(db: &dyn Database<H>, col: ColumnId, key: &[u8], mut f: impl FnMut(&[u8]) -> R) -> Option<R> {
let mut result: Option<R> = None;
let mut adapter = |k: &_| { result = Some(f(k)); };
db.with_get(col, key, &mut adapter);
result
}
pub fn with_lookup<R, H: Clone>(db: &dyn Database<H>, hash: &H, mut f: impl FnMut(&[u8]) -> R) -> Option<R> {
let mut result: Option<R> = None;
let mut adapter = |k: &_| { result = Some(f(k)); };
db.with_lookup(hash, &mut adapter);
result
}