use std::ops::Bound;
use std::sync::Arc;
use super::keypath::KeyPath;
pub type StoreId = u64;
pub type IndexId = u64;
pub type Value = Arc<Vec<u8>>;
#[derive(Clone, Debug, PartialEq)]
pub struct IndexMeta {
pub id: IndexId,
pub name: Vec<u16>,
pub key_path: KeyPath,
pub unique: bool,
pub multi_entry: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub struct StoreMeta {
pub id: StoreId,
pub name: Vec<u16>,
pub key_path: Option<KeyPath>,
pub auto_increment: bool,
pub indexes: Vec<IndexMeta>,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct DbMeta {
pub name: Vec<u16>,
pub version: u64,
pub stores: Vec<StoreMeta>,
}
impl DbMeta {
pub fn store(&self, id: StoreId) -> Option<&StoreMeta> {
self.stores.iter().find(|s| s.id == id)
}
pub fn store_by_name(&self, name: &[u16]) -> Option<&StoreMeta> {
self.stores.iter().find(|s| s.name == name)
}
pub fn next_id(&self) -> u64 {
self.stores.iter().flat_map(|s| core::iter::once(s.id).chain(s.indexes.iter().map(|i| i.id))).max().unwrap_or(0) + 1
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Durability {
Default,
Strict,
Relaxed,
}
#[derive(Clone, Debug, PartialEq)]
pub enum BackendError {
Locked(String),
Quota { quota: u64, requested: u64 },
Io(String),
}
pub type R<T> = Result<T, BackendError>;
pub trait Backend: Send {
fn describe(&self) -> String;
fn persistent(&self) -> bool;
fn databases(&mut self) -> R<Vec<(Vec<u16>, u64)>>;
fn open(&mut self, name: &[u16]) -> R<Box<dyn Database>>;
fn inspect_existing(&mut self, _name: &[u16]) -> R<Box<dyn Database>> {
Err(BackendError::Io("This backend cannot inspect a closed database without opening it".into()))
}
fn inspect_databases(&mut self) -> R<Vec<(Vec<u16>, u64)>> { self.databases() }
fn delete(&mut self, name: &[u16]) -> R<u64>;
fn usage(&mut self) -> u64;
fn quota(&mut self) -> u64;
}
pub trait Database: Send {
fn meta(&self) -> &DbMeta;
fn begin_write(&mut self, durability: Durability) -> R<()>;
fn commit_write(&mut self) -> R<()>;
fn rollback_write(&mut self);
fn savepoint(&mut self) -> usize;
fn rollback_to(&mut self, sp: usize);
fn set_version(&mut self, v: u64) -> R<()>;
fn create_store(&mut self, meta: StoreMeta) -> R<()>;
fn delete_store(&mut self, id: StoreId) -> R<()>;
fn rename_store(&mut self, id: StoreId, name: Vec<u16>) -> R<()>;
fn create_index(&mut self, store: StoreId, meta: IndexMeta) -> R<()>;
fn delete_index(&mut self, store: StoreId, id: IndexId) -> R<()>;
fn rename_index(&mut self, store: StoreId, id: IndexId, name: Vec<u16>) -> R<()>;
fn key_generator(&mut self, store: StoreId) -> R<f64>;
fn set_key_generator(&mut self, store: StoreId, v: f64) -> R<()>;
fn get(&mut self, store: StoreId, key: &[u8]) -> R<Option<Value>>;
fn inspect_value(&mut self, store: StoreId, key: &[u8], maximum: usize) -> R<Option<(u64, Vec<u8>)>> {
Ok(self.get(store, key)?.map(|value| (value.len() as u64, value[..value.len().min(maximum)].to_vec())))
}
fn inspection_budget(&mut self, _instructions: Option<u32>) {}
fn put(&mut self, store: StoreId, key: &[u8], value: Value) -> R<()>;
fn delete(&mut self, store: StoreId, key: &[u8]) -> R<()>;
fn keys(&mut self, store: StoreId, lo: Bound<&[u8]>, hi: Bound<&[u8]>, reverse: bool, limit: usize) -> R<Vec<Vec<u8>>>;
fn count(&mut self, store: StoreId, lo: Bound<&[u8]>, hi: Bound<&[u8]>) -> R<u64>;
fn clear(&mut self, store: StoreId) -> R<()>;
fn index_add(&mut self, index: IndexId, entry: Vec<u8>) -> R<()>;
fn index_remove(&mut self, index: IndexId, entry: &[u8]) -> R<()>;
fn index_scan(&mut self, index: IndexId, lo: Bound<&[u8]>, hi: Bound<&[u8]>, reverse: bool, limit: usize) -> R<Vec<Vec<u8>>>;
fn index_count(&mut self, index: IndexId, lo: Bound<&[u8]>, hi: Bound<&[u8]>) -> R<u64>;
fn index_clear(&mut self, index: IndexId) -> R<()>;
}