use std::cell::RefCell;
use wasm_dbms_api::prelude::{DbmsResult, TransactionId};
use wasm_dbms_memory::prelude::{
AccessControl, AccessControlList, MemoryManager, MemoryProvider, SchemaRegistry,
TableRegistryPage,
};
use crate::transaction::journal::Journal;
use crate::transaction::session::TransactionSession;
pub struct DbmsContext<M, A = AccessControlList>
where
M: MemoryProvider,
A: AccessControl,
{
pub(crate) mm: RefCell<MemoryManager<M>>,
pub(crate) schema_registry: RefCell<SchemaRegistry>,
pub(crate) acl: RefCell<A>,
pub(crate) transaction_session: RefCell<TransactionSession>,
pub(crate) journal: RefCell<Option<Journal>>,
}
impl<M> DbmsContext<M>
where
M: MemoryProvider,
{
pub fn new(memory: M) -> Self {
let mut mm = MemoryManager::init(memory);
let schema_registry = SchemaRegistry::load(&mut mm).unwrap_or_default();
let acl = AccessControlList::load(&mut mm).unwrap_or_default();
Self {
mm: RefCell::new(mm),
schema_registry: RefCell::new(schema_registry),
acl: RefCell::new(acl),
transaction_session: RefCell::new(TransactionSession::default()),
journal: RefCell::new(None),
}
}
}
impl<M, A> DbmsContext<M, A>
where
M: MemoryProvider,
A: AccessControl,
{
pub fn with_acl(memory: M) -> Self {
let mut mm = MemoryManager::init(memory);
let schema_registry = SchemaRegistry::load(&mut mm).unwrap_or_default();
let acl = A::load(&mut mm).unwrap_or_default();
Self {
mm: RefCell::new(mm),
schema_registry: RefCell::new(schema_registry),
acl: RefCell::new(acl),
transaction_session: RefCell::new(TransactionSession::default()),
journal: RefCell::new(None),
}
}
pub fn register_table<T: wasm_dbms_api::prelude::TableSchema>(
&self,
) -> DbmsResult<TableRegistryPage> {
let mut sr = self.schema_registry.borrow_mut();
let mut mm = self.mm.borrow_mut();
sr.register_table::<T>(&mut mm).map_err(Into::into)
}
pub fn acl_add(&self, identity: A::Id) -> DbmsResult<()> {
let mut acl = self.acl.borrow_mut();
let mut mm = self.mm.borrow_mut();
acl.add_identity(identity, &mut mm).map_err(Into::into)
}
pub fn acl_remove(&self, identity: &A::Id) -> DbmsResult<()> {
let mut acl = self.acl.borrow_mut();
let mut mm = self.mm.borrow_mut();
acl.remove_identity(identity, &mut mm).map_err(Into::into)
}
pub fn acl_allowed(&self) -> Vec<A::Id> {
let acl = self.acl.borrow();
acl.allowed_identities()
}
pub fn acl_is_allowed(&self, identity: &A::Id) -> bool {
let acl = self.acl.borrow();
acl.is_allowed(identity)
}
pub fn begin_transaction(&self, owner: Vec<u8>) -> TransactionId {
let mut ts = self.transaction_session.borrow_mut();
ts.begin_transaction(owner)
}
pub fn has_transaction(&self, tx_id: &TransactionId, caller: &[u8]) -> bool {
let ts = self.transaction_session.borrow();
ts.has_transaction(tx_id, caller)
}
}
impl<M, A> std::fmt::Debug for DbmsContext<M, A>
where
M: MemoryProvider,
A: AccessControl + std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DbmsContext")
.field("schema_registry", &self.schema_registry)
.field("acl", &self.acl)
.field("transaction_session", &self.transaction_session)
.finish_non_exhaustive()
}
}
#[cfg(test)]
mod tests {
use wasm_dbms_memory::prelude::HeapMemoryProvider;
use super::*;
#[test]
fn test_should_create_context() {
let ctx = DbmsContext::new(HeapMemoryProvider::default());
assert!(ctx.acl_allowed().is_empty());
}
#[test]
fn test_should_add_acl_identity() {
let ctx = DbmsContext::new(HeapMemoryProvider::default());
ctx.acl_add(vec![1, 2, 3]).unwrap();
assert!(ctx.acl_is_allowed(&vec![1, 2, 3]));
assert!(!ctx.acl_is_allowed(&vec![4, 5, 6]));
}
#[test]
fn test_should_remove_acl_identity() {
let ctx = DbmsContext::new(HeapMemoryProvider::default());
ctx.acl_add(vec![1, 2, 3]).unwrap();
ctx.acl_add(vec![4, 5, 6]).unwrap();
ctx.acl_remove(&vec![1, 2, 3]).unwrap();
assert!(!ctx.acl_is_allowed(&vec![1, 2, 3]));
assert!(ctx.acl_is_allowed(&vec![4, 5, 6]));
}
#[test]
fn test_should_begin_transaction() {
let ctx = DbmsContext::new(HeapMemoryProvider::default());
let owner = vec![1, 2, 3];
let tx_id = ctx.begin_transaction(owner.clone());
assert!(ctx.has_transaction(&tx_id, &owner));
assert!(!ctx.has_transaction(&tx_id, &[4, 5, 6]));
}
#[test]
fn test_should_debug_context() {
let ctx = DbmsContext::new(HeapMemoryProvider::default());
let debug = format!("{ctx:?}");
assert!(debug.contains("DbmsContext"));
}
}