Skip to content

Commit

Permalink
Implement config definition caching within a transaction
Browse files Browse the repository at this point in the history
Closes #21
  • Loading branch information
tobiemh committed Aug 8, 2022
1 parent 9ed50a9 commit bac8aa3
Show file tree
Hide file tree
Showing 6 changed files with 508 additions and 100 deletions.
19 changes: 10 additions & 9 deletions lib/src/doc/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::sql::statements::define::DefineTableStatement;
use crate::sql::thing::Thing;
use crate::sql::value::Value;
use std::borrow::Cow;
use std::sync::Arc;

pub struct Document<'a> {
pub(super) id: Option<Thing>,
Expand Down Expand Up @@ -49,24 +50,24 @@ impl<'a> Document<'a> {
&self,
opt: &Options,
txn: &Transaction,
) -> Result<DefineTableStatement, Error> {
) -> Result<Arc<DefineTableStatement>, Error> {
// Clone transaction
let run = txn.clone();
// Claim transaction
let mut run = run.lock().await;
// Get the record id
let rid = self.id.as_ref().unwrap();
// Get the table definition
let tb = run.get_tb(opt.ns(), opt.db(), &rid.tb).await;
let tb = run.get_and_cache_tb(opt.ns(), opt.db(), &rid.tb).await;
// Return the table or attempt to define it
match tb {
// The table doesn't exist
Err(Error::TbNotFound) => match opt.auth.check(Level::Db) {
// We can create the table automatically
true => {
run.add_ns(opt.ns(), opt.strict).await?;
run.add_db(opt.ns(), opt.db(), opt.strict).await?;
run.add_tb(opt.ns(), opt.db(), &rid.tb, opt.strict).await
run.add_and_cache_ns(opt.ns(), opt.strict).await?;
run.add_and_cache_db(opt.ns(), opt.db(), opt.strict).await?;
run.add_and_cache_tb(opt.ns(), opt.db(), &rid.tb, opt.strict).await
}
// We can't create the table so error
false => Err(Error::TbNotFound),
Expand All @@ -82,7 +83,7 @@ impl<'a> Document<'a> {
&self,
opt: &Options,
txn: &Transaction,
) -> Result<Vec<DefineTableStatement>, Error> {
) -> Result<Arc<Vec<DefineTableStatement>>, Error> {
// Get the record id
let id = self.id.as_ref().unwrap();
// Get the table definitions
Expand All @@ -93,7 +94,7 @@ impl<'a> Document<'a> {
&self,
opt: &Options,
txn: &Transaction,
) -> Result<Vec<DefineEventStatement>, Error> {
) -> Result<Arc<Vec<DefineEventStatement>>, Error> {
// Get the record id
let id = self.id.as_ref().unwrap();
// Get the event definitions
Expand All @@ -104,7 +105,7 @@ impl<'a> Document<'a> {
&self,
opt: &Options,
txn: &Transaction,
) -> Result<Vec<DefineFieldStatement>, Error> {
) -> Result<Arc<Vec<DefineFieldStatement>>, Error> {
// Get the record id
let id = self.id.as_ref().unwrap();
// Get the field definitions
Expand All @@ -115,7 +116,7 @@ impl<'a> Document<'a> {
&self,
opt: &Options,
txn: &Transaction,
) -> Result<Vec<DefineIndexStatement>, Error> {
) -> Result<Arc<Vec<DefineIndexStatement>>, Error> {
// Get the record id
let id = self.id.as_ref().unwrap();
// Get the index definitions
Expand Down
52 changes: 52 additions & 0 deletions lib/src/kvs/cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::kvs::kv::Key;
use crate::sql::statements::DefineDatabaseStatement;
use crate::sql::statements::DefineEventStatement;
use crate::sql::statements::DefineFieldStatement;
use crate::sql::statements::DefineIndexStatement;
use crate::sql::statements::DefineLoginStatement;
use crate::sql::statements::DefineNamespaceStatement;
use crate::sql::statements::DefineScopeStatement;
use crate::sql::statements::DefineTableStatement;
use crate::sql::statements::DefineTokenStatement;
use crate::sql::statements::LiveStatement;
use std::collections::HashMap;
use std::sync::Arc;

#[derive(Clone)]
pub enum Entry {
Ns(Arc<DefineNamespaceStatement>),
Db(Arc<DefineDatabaseStatement>),
Tb(Arc<DefineTableStatement>),
Nss(Arc<Vec<DefineNamespaceStatement>>),
Nls(Arc<Vec<DefineLoginStatement>>),
Nts(Arc<Vec<DefineTokenStatement>>),
Dbs(Arc<Vec<DefineDatabaseStatement>>),
Dls(Arc<Vec<DefineLoginStatement>>),
Dts(Arc<Vec<DefineTokenStatement>>),
Scs(Arc<Vec<DefineScopeStatement>>),
Sts(Arc<Vec<DefineTokenStatement>>),
Tbs(Arc<Vec<DefineTableStatement>>),
Evs(Arc<Vec<DefineEventStatement>>),
Fds(Arc<Vec<DefineFieldStatement>>),
Ixs(Arc<Vec<DefineIndexStatement>>),
Fts(Arc<Vec<DefineTableStatement>>),
Lvs(Arc<Vec<LiveStatement>>),
}

#[derive(Default)]
pub struct Cache(pub HashMap<Key, Entry>);

impl Cache {
// Check if key exists
pub fn exi(&mut self, key: &Key) -> bool {
self.0.contains_key(key)
}
// Set a key in the cache
pub fn set(&mut self, key: Key, val: Entry) {
self.0.insert(key, val);
}
// get a key from the cache
pub fn get(&mut self, key: &Key) -> Option<Entry> {
self.0.get(key).cloned()
}
}
4 changes: 4 additions & 0 deletions lib/src/kvs/ds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,31 @@ impl Datastore {
let tx = v.transaction(write, lock).await?;
Ok(Transaction {
inner: super::tx::Inner::Mem(tx),
cache: super::cache::Cache::default(),
})
}
#[cfg(feature = "kv-indxdb")]
Inner::IxDB(v) => {
let tx = v.transaction(write, lock).await?;
Ok(Transaction {
inner: super::tx::Inner::IxDB(tx),
cache: super::cache::Cache::default(),
})
}
#[cfg(feature = "kv-yokudb")]
Inner::File(v) => {
let tx = v.transaction(write, lock).await?;
Ok(Transaction {
inner: super::tx::Inner::File(tx),
cache: super::cache::Cache::default(),
})
}
#[cfg(feature = "kv-tikv")]
Inner::TiKV(v) => {
let tx = v.transaction(write, lock).await?;
Ok(Transaction {
inner: super::tx::Inner::TiKV(tx),
cache: super::cache::Cache::default(),
})
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/kvs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod cache;
mod ds;
mod file;
mod ixdb;
Expand Down

0 comments on commit bac8aa3

Please sign in to comment.