#![cfg(feature = "kv-mem")]
use crate::err::Error;
use crate::kvs::Check;
use crate::kvs::Key;
use crate::kvs::Val;
use crate::vs::{try_to_u64_be, u64_to_versionstamp, Versionstamp};
use std::ops::Range;
pub struct Datastore {
db: echodb::Db<Key, Val>,
}
pub struct Transaction {
done: bool,
write: bool,
check: Check,
inner: echodb::Tx<Key, Val>,
}
impl Drop for Transaction {
fn drop(&mut self) {
if !self.done && self.write {
if std::thread::panicking() {
return;
}
match self.check {
Check::None => {
trace!("A transaction was dropped without being committed or cancelled");
}
Check::Warn => {
warn!("A transaction was dropped without being committed or cancelled");
}
Check::Panic => {
#[cfg(debug_assertions)]
{
let backtrace = std::backtrace::Backtrace::force_capture();
if let std::backtrace::BacktraceStatus::Captured = backtrace.status() {
println!("{}", backtrace);
}
}
panic!("A transaction was dropped without being committed or cancelled");
}
}
}
}
}
impl Datastore {
pub(crate) async fn new() -> Result<Datastore, Error> {
Ok(Datastore {
db: echodb::db::new(),
})
}
pub(crate) async fn transaction(&self, write: bool, _: bool) -> Result<Transaction, Error> {
#[cfg(not(debug_assertions))]
let check = Check::Warn;
#[cfg(debug_assertions)]
let check = Check::Panic;
match self.db.begin(write).await {
Ok(inner) => Ok(Transaction {
done: false,
check,
write,
inner,
}),
Err(e) => Err(Error::Tx(e.to_string())),
}
}
}
impl Transaction {
pub(crate) fn check_level(&mut self, check: Check) {
self.check = check;
}
pub(crate) fn closed(&self) -> bool {
self.done
}
pub(crate) fn cancel(&mut self) -> Result<(), Error> {
if self.done {
return Err(Error::TxFinished);
}
self.done = true;
self.inner.cancel()?;
Ok(())
}
pub(crate) fn commit(&mut self) -> Result<(), Error> {
if self.done {
return Err(Error::TxFinished);
}
if !self.write {
return Err(Error::TxReadonly);
}
self.done = true;
self.inner.commit()?;
Ok(())
}
pub(crate) fn exi<K>(&mut self, key: K) -> Result<bool, Error>
where
K: Into<Key>,
{
if self.done {
return Err(Error::TxFinished);
}
let res = self.inner.exi(key.into())?;
Ok(res)
}
pub(crate) fn get<K>(&mut self, key: K) -> Result<Option<Val>, Error>
where
K: Into<Key>,
{
if self.done {
return Err(Error::TxFinished);
}
let res = self.inner.get(key.into())?;
Ok(res)
}
#[allow(unused)]
pub(crate) fn get_timestamp<K>(&mut self, key: K) -> Result<Versionstamp, Error>
where
K: Into<Key>,
{
if self.done {
return Err(Error::TxFinished);
}
let k: Key = key.into();
let prev = self.inner.get(k.clone())?;
let ver = match prev {
Some(prev) => {
let slice = prev.as_slice();
let res: Result<[u8; 10], Error> = match slice.try_into() {
Ok(ba) => Ok(ba),
Err(e) => Err(Error::Ds(e.to_string())),
};
let array = res?;
let prev = try_to_u64_be(array)?;
prev + 1
}
None => 1,
};
let verbytes = u64_to_versionstamp(ver);
self.inner.set(k, verbytes.to_vec())?;
Ok(verbytes)
}
pub(crate) async fn get_versionstamped_key<K>(
&mut self,
ts_key: K,
prefix: K,
suffix: K,
) -> Result<Vec<u8>, Error>
where
K: Into<Key>,
{
if self.done {
return Err(Error::TxFinished);
}
if !self.write {
return Err(Error::TxReadonly);
}
let ts_key: Key = ts_key.into();
let prefix: Key = prefix.into();
let suffix: Key = suffix.into();
let ts = self.get_timestamp(ts_key.clone())?;
let mut k: Vec<u8> = prefix.clone();
k.append(&mut ts.to_vec());
k.append(&mut suffix.clone());
trace!("get_versionstamped_key; {ts_key:?} {prefix:?} {ts:?} {suffix:?} {k:?}",);
Ok(k)
}
pub(crate) fn set<K, V>(&mut self, key: K, val: V) -> Result<(), Error>
where
K: Into<Key>,
V: Into<Val>,
{
if self.done {
return Err(Error::TxFinished);
}
if !self.write {
return Err(Error::TxReadonly);
}
self.inner.set(key.into(), val.into())?;
Ok(())
}
pub(crate) fn put<K, V>(&mut self, key: K, val: V) -> Result<(), Error>
where
K: Into<Key>,
V: Into<Val>,
{
if self.done {
return Err(Error::TxFinished);
}
if !self.write {
return Err(Error::TxReadonly);
}
self.inner.put(key.into(), val.into())?;
Ok(())
}
pub(crate) fn putc<K, V>(&mut self, key: K, val: V, chk: Option<V>) -> Result<(), Error>
where
K: Into<Key>,
V: Into<Val>,
{
if self.done {
return Err(Error::TxFinished);
}
if !self.write {
return Err(Error::TxReadonly);
}
self.inner.putc(key.into(), val.into(), chk.map(Into::into))?;
Ok(())
}
pub(crate) fn del<K>(&mut self, key: K) -> Result<(), Error>
where
K: Into<Key>,
{
if self.done {
return Err(Error::TxFinished);
}
if !self.write {
return Err(Error::TxReadonly);
}
self.inner.del(key.into())?;
Ok(())
}
pub(crate) fn delc<K, V>(&mut self, key: K, chk: Option<V>) -> Result<(), Error>
where
K: Into<Key>,
V: Into<Val>,
{
if self.done {
return Err(Error::TxFinished);
}
if !self.write {
return Err(Error::TxReadonly);
}
self.inner.delc(key.into(), chk.map(Into::into))?;
Ok(())
}
pub(crate) fn scan<K>(&mut self, rng: Range<K>, limit: u32) -> Result<Vec<(Key, Val)>, Error>
where
K: Into<Key>,
{
if self.done {
return Err(Error::TxFinished);
}
let rng: Range<Key> = Range {
start: rng.start.into(),
end: rng.end.into(),
};
let res = self.inner.scan(rng, limit)?;
Ok(res)
}
}