use alloc::vec::Vec;
use crate::Result;
pub type Key = Vec<u8>;
pub type Value = Vec<u8>;
pub type QueueIndex = u32;
pub struct KvTransaction {
pub accessed_keys: Vec<Key>,
pub version_updates: Vec<Key>,
pub value_updates: Vec<(Key, Option<Value>)>,
pub queue_head: Option<QueueIndex>,
pub queue_lock: Option<Key>,
}
pub trait BumpVersion {
fn bump_version(&self, version: Option<Value>) -> Result<Value>;
}
pub trait KvSnapshot {
fn snapshot_id(&self) -> Result<Value>;
fn get(&self, key: &[u8]) -> Result<Option<Value>>;
#[allow(clippy::type_complexity)]
fn batch_get(&self, keys: &[Key]) -> Result<Vec<(Key, Option<Value>)>> {
keys.iter()
.map(|key| {
let key = key.clone();
Ok((key.clone(), self.get(&key)?))
})
.collect()
}
}
pub trait KvSession {
fn get(&mut self, key: &[u8]) -> Result<Option<Value>>;
fn put(&mut self, key: &[u8], value: Value);
fn delete(&mut self, key: &[u8]);
}
pub trait QueueSession {
fn pop(&mut self) -> Result<Option<Value>>;
}
pub trait QueueIndexCodec {
fn encode(number: QueueIndex) -> Vec<u8>;
fn decode(raw: impl AsRef<[u8]>) -> Result<QueueIndex>;
}
pub trait AccessTracking {
fn read(&mut self, key: &[u8]);
fn write(&mut self, key: &[u8]);
fn collect_into(self) -> (Vec<Key>, Vec<Key>);
}