use crate::{
err::Error,
interface::{
kv::{Key, Val},
KeyValuePair,
},
util::now,
};
use async_trait::async_trait;
use futures::lock::Mutex;
use std::{pin::Pin, sync::Arc};
pub type CF = Option<Vec<u8>>;
#[derive(Debug)]
pub struct DBTransaction<D, T>
where
D: 'static,
T: 'static,
{
pub tx: Arc<Mutex<Option<T>>>,
pub ok: bool,
pub writable: bool,
pub readable: bool,
pub timestamp: i64,
pub _db: Pin<Arc<D>>,
}
impl<DBType, TxType> DBTransaction<DBType, TxType>
where
DBType: 'static,
TxType: 'static,
{
pub fn new(tx: TxType, db: Pin<Arc<DBType>>, w: bool) -> Result<Self, Error> {
Ok(DBTransaction {
tx: Arc::new(Mutex::new(Some(tx))),
ok: false,
writable: w,
readable: true,
timestamp: now(),
_db: db,
})
}
}
#[async_trait(?Send)]
pub trait SimpleTransaction {
fn closed(&self) -> bool;
async fn cancel(&mut self) -> Result<(), Error>;
async fn count(&mut self, cf: CF) -> Result<usize, Error>;
async fn commit(&mut self) -> Result<(), Error>;
async fn exi<K: Into<Key> + Send>(&self, cf: CF, key: K) -> Result<bool, Error>;
async fn get<K: Into<Key> + Send>(&self, cf: CF, key: K) -> Result<Option<Val>, Error>;
async fn set<K: Into<Key> + Send, V: Into<Key> + Send>(
&mut self,
cf: CF,
key: K,
val: V,
) -> Result<(), Error>;
async fn put<K: Into<Key> + Send, V: Into<Key> + Send>(
&mut self,
cf: CF,
key: K,
val: V,
) -> Result<(), Error>;
async fn del<K: Into<Key> + Send>(&mut self, cf: CF, key: K) -> Result<(), Error>;
async fn multi_get<K: Into<Key> + Send + AsRef<[u8]>>(
&self,
_cf: CF,
_keys: Vec<K>,
) -> Result<Vec<Option<Val>>, Error> {
todo!();
}
async fn iterate(&self, cf: CF) -> Result<Vec<Result<KeyValuePair, Error>>, Error>;
async fn prefix_iterate<P: Into<Key> + Send>(
&self,
cf: CF,
prefix: P,
) -> Result<Vec<Result<KeyValuePair, Error>>, Error>;
async fn suffix_iterate<S: Into<Key> + Send>(
&self,
cf: CF,
suffix: S,
) -> Result<Vec<Result<KeyValuePair, Error>>, Error>;
}