From 769ed5ab85f00988572bb043c13c245d8fd834dc Mon Sep 17 00:00:00 2001 From: Przemyslaw Hugh Kaznowski Date: Tue, 18 Jul 2023 19:32:38 +0100 Subject: [PATCH] Sur 253 Change Cl->Nd, rework key/mod (#2257) Co-authored-by: Tobie Morgan Hitchcock --- lib/src/dbs/mod.rs | 2 +- lib/src/dbs/{cl.rs => node.rs} | 0 lib/src/dbs/processor.rs | 10 +- lib/src/err/mod.rs | 2 +- lib/src/idx/mod.rs | 22 +- lib/src/key/bc.rs | 106 -------- lib/src/key/bd.rs | 115 --------- lib/src/key/bf.rs | 122 --------- lib/src/key/bi.rs | 109 -------- lib/src/key/bk.rs | 105 -------- lib/src/key/bl.rs | 116 --------- lib/src/key/bp.rs | 116 --------- lib/src/key/bt.rs | 115 --------- lib/src/key/bu.rs | 105 -------- lib/src/key/{cf.rs => change/mod.rs} | 17 +- lib/src/key/{database.rs => database/all.rs} | 15 +- lib/src/key/{ => database}/az.rs | 5 +- lib/src/key/{ => database}/fc.rs | 5 +- lib/src/key/{dl.rs => database/lg.rs} | 29 ++- lib/src/key/database/mod.rs | 9 + lib/src/key/{ => database}/pa.rs | 5 +- lib/src/key/{ => database}/sc.rs | 5 +- lib/src/key/{ => database}/tb.rs | 5 +- lib/src/key/{dt.rs => database/tk.rs} | 33 +-- lib/src/key/{dv.rs => database/vs.rs} | 23 +- lib/src/key/debug.rs | 1 + lib/src/key/{graph.rs => graph/mod.rs} | 1 + lib/src/key/index/all.rs | 56 ++++ lib/src/key/index/bc.rs | 63 +++++ lib/src/key/index/bd.rs | 67 +++++ lib/src/key/index/bf.rs | 77 ++++++ lib/src/key/index/bi.rs | 62 +++++ lib/src/key/index/bk.rs | 62 +++++ lib/src/key/index/bl.rs | 68 +++++ lib/src/key/{ => index}/bo.rs | 57 +--- lib/src/key/index/bp.rs | 68 +++++ lib/src/key/{ => index}/bs.rs | 1 + lib/src/key/index/bt.rs | 67 +++++ lib/src/key/index/bu.rs | 62 +++++ lib/src/key/{index.rs => index/mod.rs} | 29 ++- lib/src/key/mod.rs | 141 ++++------ .../key/{namespace.rs => namespace/all.rs} | 15 +- lib/src/key/{ => namespace}/db.rs | 5 +- lib/src/key/{nl.rs => namespace/lg.rs} | 29 ++- lib/src/key/namespace/mod.rs | 4 + lib/src/key/{nt.rs => namespace/tk.rs} | 29 ++- lib/src/key/node/all.rs | 45 ++++ lib/src/key/{ => node}/lq.rs | 5 +- lib/src/key/node/mod.rs | 2 + lib/src/key/{kv.rs => root/all.rs} | 1 + lib/src/key/{ => root}/hb.rs | 11 +- lib/src/key/root/mod.rs | 4 + lib/src/key/{cl.rs => root/nd.rs} | 31 +-- lib/src/key/{ => root}/ns.rs | 11 +- lib/src/key/{scope.rs => scope/all.rs} | 4 +- lib/src/key/scope/mod.rs | 4 + lib/src/key/{st.rs => scope/tk.rs} | 32 +-- lib/src/key/{table.rs => table/all.rs} | 1 + lib/src/key/{ => table}/ev.rs | 5 +- lib/src/key/{ => table}/fd.rs | 5 +- lib/src/key/{ => table}/ft.rs | 5 +- lib/src/key/{ => table}/ix.rs | 5 +- lib/src/key/{lv.rs => table/lq.rs} | 37 +-- lib/src/key/table/mod.rs | 6 + lib/src/key/{thing.rs => thing/mod.rs} | 5 +- lib/src/kvs/ds.rs | 24 +- lib/src/kvs/tests/helper.rs | 2 +- lib/src/kvs/tests/lq.rs | 4 +- lib/src/kvs/tests/mod.rs | 10 +- lib/src/kvs/tests/{lv.rs => nq.rs} | 6 +- lib/src/kvs/tests/tb.rs | 4 +- lib/src/kvs/tx.rs | 244 +++++++++--------- lib/src/sql/statements/define.rs | 51 ++-- lib/src/sql/statements/kill.rs | 6 +- lib/src/sql/statements/live.rs | 4 +- lib/src/sql/statements/remove.rs | 84 ++---- lib/tests/remove.rs | 42 +-- lib/tests/util.rs | 23 ++ 78 files changed, 1265 insertions(+), 1618 deletions(-) rename lib/src/dbs/{cl.rs => node.rs} (100%) delete mode 100644 lib/src/key/bc.rs delete mode 100644 lib/src/key/bd.rs delete mode 100644 lib/src/key/bf.rs delete mode 100644 lib/src/key/bi.rs delete mode 100644 lib/src/key/bk.rs delete mode 100644 lib/src/key/bl.rs delete mode 100644 lib/src/key/bp.rs delete mode 100644 lib/src/key/bt.rs delete mode 100644 lib/src/key/bu.rs rename lib/src/key/{cf.rs => change/mod.rs} (85%) rename lib/src/key/{database.rs => database/all.rs} (65%) rename lib/src/key/{ => database}/az.rs (89%) rename lib/src/key/{ => database}/fc.rs (89%) rename lib/src/key/{dl.rs => database/lg.rs} (53%) create mode 100644 lib/src/key/database/mod.rs rename lib/src/key/{ => database}/pa.rs (87%) rename lib/src/key/{ => database}/sc.rs (87%) rename lib/src/key/{ => database}/tb.rs (87%) rename lib/src/key/{dt.rs => database/tk.rs} (55%) rename lib/src/key/{dv.rs => database/vs.rs} (61%) rename lib/src/key/{graph.rs => graph/mod.rs} (99%) create mode 100644 lib/src/key/index/all.rs create mode 100644 lib/src/key/index/bc.rs create mode 100644 lib/src/key/index/bd.rs create mode 100644 lib/src/key/index/bf.rs create mode 100644 lib/src/key/index/bi.rs create mode 100644 lib/src/key/index/bk.rs create mode 100644 lib/src/key/index/bl.rs rename lib/src/key/{ => index}/bo.rs (52%) create mode 100644 lib/src/key/index/bp.rs rename lib/src/key/{ => index}/bs.rs (96%) create mode 100644 lib/src/key/index/bt.rs create mode 100644 lib/src/key/index/bu.rs rename lib/src/key/{index.rs => index/mod.rs} (86%) rename lib/src/key/{namespace.rs => namespace/all.rs} (63%) rename lib/src/key/{ => namespace}/db.rs (88%) rename lib/src/key/{nl.rs => namespace/lg.rs} (51%) create mode 100644 lib/src/key/namespace/mod.rs rename lib/src/key/{nt.rs => namespace/tk.rs} (51%) create mode 100644 lib/src/key/node/all.rs rename lib/src/key/{ => node}/lq.rs (94%) create mode 100644 lib/src/key/node/mod.rs rename lib/src/key/{kv.rs => root/all.rs} (93%) rename lib/src/key/{ => root}/hb.rs (81%) create mode 100644 lib/src/key/root/mod.rs rename lib/src/key/{cl.rs => root/nd.rs} (54%) rename lib/src/key/{ => root}/ns.rs (78%) rename lib/src/key/{scope.rs => scope/all.rs} (91%) create mode 100644 lib/src/key/scope/mod.rs rename lib/src/key/{st.rs => scope/tk.rs} (59%) rename lib/src/key/{table.rs => table/all.rs} (94%) rename lib/src/key/{ => table}/ev.rs (90%) rename lib/src/key/{ => table}/fd.rs (90%) rename lib/src/key/{ => table}/ft.rs (89%) rename lib/src/key/{ => table}/ix.rs (87%) rename lib/src/key/{lv.rs => table/lq.rs} (56%) create mode 100644 lib/src/key/table/mod.rs rename lib/src/key/{thing.rs => thing/mod.rs} (92%) rename lib/src/kvs/tests/{lv.rs => nq.rs} (92%) create mode 100644 lib/tests/util.rs diff --git a/lib/src/dbs/mod.rs b/lib/src/dbs/mod.rs index cfedf0b1..c95b7403 100644 --- a/lib/src/dbs/mod.rs +++ b/lib/src/dbs/mod.rs @@ -26,7 +26,7 @@ pub(crate) use self::statement::*; pub(crate) use self::transaction::*; pub(crate) use self::variables::*; -pub mod cl; +pub mod node; mod processor; #[cfg(test)] diff --git a/lib/src/dbs/cl.rs b/lib/src/dbs/node.rs similarity index 100% rename from lib/src/dbs/cl.rs rename to lib/src/dbs/node.rs diff --git a/lib/src/dbs/processor.rs b/lib/src/dbs/processor.rs index cd135796..027dafec 100644 --- a/lib/src/dbs/processor.rs +++ b/lib/src/dbs/processor.rs @@ -236,8 +236,8 @@ impl<'a> Processor<'a> { nxt = Some(k.clone()); } // Parse the data from the store - let key: crate::key::thing::Thing = (&k).into(); - let val: crate::sql::value::Value = (&v).into(); + let key: thing::Thing = (&k).into(); + let val: Value = (&v).into(); let rid = Thing::from((key.tb, key.id)); // Create a new operable value let val = Operable::Value(val); @@ -319,8 +319,8 @@ impl<'a> Processor<'a> { nxt = Some(k.clone()); } // Parse the data from the store - let key: crate::key::thing::Thing = (&k).into(); - let val: crate::sql::value::Value = (&v).into(); + let key: thing::Thing = (&k).into(); + let val: Value = (&v).into(); let rid = Thing::from((key.tb, key.id)); // Create a new operable value let val = Operable::Value(val); @@ -454,7 +454,7 @@ impl<'a> Processor<'a> { nxt = Some(k.clone()); } // Parse the data from the store - let gra: crate::key::graph::Graph = (&k).into(); + let gra: graph::Graph = (&k).into(); // Fetch the data from the store let key = thing::new(opt.ns(), opt.db(), gra.ft, &gra.fk); let val = txn.lock().await.get(key).await?; diff --git a/lib/src/err/mod.rs b/lib/src/err/mod.rs index 9f13003d..db306cc6 100644 --- a/lib/src/err/mod.rs +++ b/lib/src/err/mod.rs @@ -256,7 +256,7 @@ pub enum Error { // The cluster node does not exist #[error("The node '{value}' does not exist")] - ClNotFound { + NdNotFound { value: String, }, diff --git a/lib/src/idx/mod.rs b/lib/src/idx/mod.rs index c7ee9ab5..9ac1fb76 100644 --- a/lib/src/idx/mod.rs +++ b/lib/src/idx/mod.rs @@ -8,17 +8,17 @@ use crate::err::Error; use crate::idx::btree::NodeId; use crate::idx::ft::docids::DocId; use crate::idx::ft::terms::TermId; -use crate::key::bc::Bc; -use crate::key::bd::Bd; -use crate::key::bf::Bf; -use crate::key::bi::Bi; -use crate::key::bk::Bk; -use crate::key::bl::Bl; -use crate::key::bo::Bo; -use crate::key::bp::Bp; -use crate::key::bs::Bs; -use crate::key::bt::Bt; -use crate::key::bu::Bu; +use crate::key::index::bc::Bc; +use crate::key::index::bd::Bd; +use crate::key::index::bf::Bf; +use crate::key::index::bi::Bi; +use crate::key::index::bk::Bk; +use crate::key::index::bl::Bl; +use crate::key::index::bo::Bo; +use crate::key::index::bp::Bp; +use crate::key::index::bs::Bs; +use crate::key::index::bt::Bt; +use crate::key::index::bu::Bu; use crate::kvs::{Key, Val}; use crate::sql::statements::DefineIndexStatement; use roaring::RoaringTreemap; diff --git a/lib/src/key/bc.rs b/lib/src/key/bc.rs deleted file mode 100644 index 82e4291a..00000000 --- a/lib/src/key/bc.rs +++ /dev/null @@ -1,106 +0,0 @@ -use crate::idx::ft::terms::TermId; -use derive::Key; -use serde::{Deserialize, Serialize}; -use std::ops::Range; - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Bc<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, - pub term_id: TermId, -} - -impl<'a> Bc<'a> { - pub fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str, term_id: TermId) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'c', - ix, - _g: b'*', - term_id, - } - } - - pub fn range(ns: &str, db: &str, tb: &str, ix: &str) -> Range> { - let mut beg = Prefix::new(ns, db, tb, ix).encode().unwrap(); - beg.extend_from_slice(&[0x00]); - let mut end = Prefix::new(ns, db, tb, ix).encode().unwrap(); - end.extend_from_slice(&[0xff]); - beg..end - } -} - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -struct Prefix<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, -} - -impl<'a> Prefix<'a> { - fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'c', - ix, - _g: b'*', - } - } -} - -#[cfg(test)] -mod tests { - - #[test] - fn key() { - use super::*; - #[rustfmt::skip] - let val = Bc::new( - "testns", - "testdb", - "testtb", - "testix", - 7 - ); - let enc = Bc::encode(&val).unwrap(); - assert_eq!(enc, b"/*testns\0*testdb\0*testtb\0!bctestix\0*\0\0\0\0\0\0\0\x07"); - - let dec = Bc::decode(&enc).unwrap(); - assert_eq!(val, dec); - } -} diff --git a/lib/src/key/bd.rs b/lib/src/key/bd.rs deleted file mode 100644 index 3e7e68a9..00000000 --- a/lib/src/key/bd.rs +++ /dev/null @@ -1,115 +0,0 @@ -use crate::idx::btree::NodeId; -use derive::Key; -use serde::{Deserialize, Serialize}; -use std::ops::Range; - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Bd<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, - pub node_id: Option, -} - -impl<'a> Bd<'a> { - pub fn new( - ns: &'a str, - db: &'a str, - tb: &'a str, - ix: &'a str, - node_id: Option, - ) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'd', - ix, - _g: b'*', - node_id, - } - } - - pub fn range(ns: &str, db: &str, tb: &str, ix: &str) -> Range> { - let mut beg = Prefix::new(ns, db, tb, ix).encode().unwrap(); - beg.extend_from_slice(&[0x00]); - let mut end = Prefix::new(ns, db, tb, ix).encode().unwrap(); - end.extend_from_slice(&[0xff]); - beg..end - } -} - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -struct Prefix<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, -} - -impl<'a> Prefix<'a> { - fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'd', - ix, - _g: b'*', - } - } -} - -#[cfg(test)] -mod tests { - #[test] - fn key() { - use super::*; - #[rustfmt::skip] - let val = Bd::new( - "testns", - "testdb", - "testtb", - "testix", - Some(7) - ); - let enc = Bd::encode(&val).unwrap(); - assert_eq!( - enc, - b"/*testns\0*testdb\0*testtb\0!bdtestix\0*\ - \x01\ - \0\0\0\0\0\0\0\x07" - ); - let dec = Bd::decode(&enc).unwrap(); - assert_eq!(val, dec); - } -} diff --git a/lib/src/key/bf.rs b/lib/src/key/bf.rs deleted file mode 100644 index 865caeec..00000000 --- a/lib/src/key/bf.rs +++ /dev/null @@ -1,122 +0,0 @@ -use crate::idx::ft::docids::DocId; -use crate::idx::ft::terms::TermId; -use derive::Key; -use serde::{Deserialize, Serialize}; -use std::ops::Range; - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Bf<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, - pub term_id: TermId, - pub doc_id: DocId, -} - -impl<'a> Bf<'a> { - pub fn new( - ns: &'a str, - db: &'a str, - tb: &'a str, - ix: &'a str, - term_id: TermId, - doc_id: DocId, - ) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'f', - ix, - _g: b'*', - term_id, - doc_id, - } - } - - pub fn range(ns: &str, db: &str, tb: &str, ix: &str) -> Range> { - let mut beg = Prefix::new(ns, db, tb, ix).encode().unwrap(); - beg.extend_from_slice(&[0x00]); - let mut end = Prefix::new(ns, db, tb, ix).encode().unwrap(); - end.extend_from_slice(&[0xff]); - beg..end - } -} - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -struct Prefix<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, -} - -impl<'a> Prefix<'a> { - fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'f', - ix, - _g: b'*', - } - } -} - -#[cfg(test)] -mod tests { - use crate::key::bf::Bf; - - #[test] - fn key() { - #[rustfmt::skip] - let val = Bf::new( - "testns", - "testdb", - "testtb", - "testix", - 7, - 13 - ); - let enc = Bf::encode(&val).unwrap(); - assert_eq!( - enc, - b"/*testns\0*testdb\0*testtb\0!bftestix\0*\ - \0\0\0\0\0\0\0\x07\ - \0\0\0\0\0\0\0\x0d" - ); - - let dec = Bf::decode(&enc).unwrap(); - assert_eq!(val, dec); - } -} diff --git a/lib/src/key/bi.rs b/lib/src/key/bi.rs deleted file mode 100644 index 26a602a5..00000000 --- a/lib/src/key/bi.rs +++ /dev/null @@ -1,109 +0,0 @@ -use crate::idx::btree::NodeId; -use derive::Key; -use serde::{Deserialize, Serialize}; -use std::ops::Range; - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Bi<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, - pub node_id: NodeId, -} - -impl<'a> Bi<'a> { - pub fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str, node_id: NodeId) -> Self { - Bi { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'i', - ix, - _g: b'*', - node_id, - } - } - - pub fn range(ns: &str, db: &str, tb: &str, ix: &str) -> Range> { - let mut beg = Prefix::new(ns, db, tb, ix).encode().unwrap(); - beg.extend_from_slice(&[0x00]); - let mut end = Prefix::new(ns, db, tb, ix).encode().unwrap(); - end.extend_from_slice(&[0xff]); - beg..end - } -} - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Prefix<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, -} - -impl<'a> Prefix<'a> { - fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'i', - ix, - _g: b'*', - } - } -} - -#[cfg(test)] -mod tests { - #[test] - fn key() { - use super::*; - #[rustfmt::skip] - let val = Bi::new( - "testns", - "testdb", - "testtb", - "testix", - 7 - ); - let enc = Bi::encode(&val).unwrap(); - assert_eq!( - enc, - b"/*testns\0*testdb\0*testtb\0!bitestix\0*\ - \0\0\0\0\0\0\0\x07" - ); - - let dec = Bi::decode(&enc).unwrap(); - assert_eq!(val, dec); - } -} diff --git a/lib/src/key/bk.rs b/lib/src/key/bk.rs deleted file mode 100644 index 42e7e97f..00000000 --- a/lib/src/key/bk.rs +++ /dev/null @@ -1,105 +0,0 @@ -use crate::idx::ft::docids::DocId; -use derive::Key; -use serde::{Deserialize, Serialize}; -use std::ops::Range; - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Bk<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, - pub doc_id: DocId, -} - -impl<'a> Bk<'a> { - pub fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str, doc_id: DocId) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'k', - ix, - _g: b'*', - doc_id, - } - } - - pub fn range(ns: &str, db: &str, tb: &str, ix: &str) -> Range> { - let mut beg = Prefix::new(ns, db, tb, ix).encode().unwrap(); - beg.extend_from_slice(&[0x00]); - let mut end = Prefix::new(ns, db, tb, ix).encode().unwrap(); - end.extend_from_slice(&[0xff]); - beg..end - } -} - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -struct Prefix<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, -} - -impl<'a> Prefix<'a> { - fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'k', - ix, - _g: b'*', - } - } -} - -#[cfg(test)] -mod tests { - #[test] - fn key() { - use super::*; - #[rustfmt::skip] - let val = Bk::new( - "testns", - "testdb", - "testtb", - "testix", - 7 - ); - let enc = Bk::encode(&val).unwrap(); - assert_eq!(enc, b"/*testns\0*testdb\0*testtb\0!bktestix\0*\0\0\0\0\0\0\0\x07"); - - let dec = Bk::decode(&enc).unwrap(); - assert_eq!(val, dec); - } -} diff --git a/lib/src/key/bl.rs b/lib/src/key/bl.rs deleted file mode 100644 index 10740f27..00000000 --- a/lib/src/key/bl.rs +++ /dev/null @@ -1,116 +0,0 @@ -use crate::idx::btree::NodeId; -use derive::Key; -use serde::{Deserialize, Serialize}; -use std::ops::Range; - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Bl<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, - pub node_id: Option, -} - -impl<'a> Bl<'a> { - pub fn new( - ns: &'a str, - db: &'a str, - tb: &'a str, - ix: &'a str, - node_id: Option, - ) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'l', - ix, - _g: b'*', - node_id, - } - } - - pub fn range(ns: &str, db: &str, tb: &str, ix: &str) -> Range> { - let mut beg = Prefix::new(ns, db, tb, ix).encode().unwrap(); - beg.extend_from_slice(&[0x00]); - let mut end = Prefix::new(ns, db, tb, ix).encode().unwrap(); - end.extend_from_slice(&[0xff]); - beg..end - } -} - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Prefix<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, -} - -impl<'a> Prefix<'a> { - fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'l', - ix, - _g: b'*', - } - } -} - -#[cfg(test)] -mod tests { - #[test] - fn key() { - use super::*; - #[rustfmt::skip] - let val = Bl::new( - "testns", - "testdb", - "testtb", - "testix", - Some(7) - ); - let enc = Bl::encode(&val).unwrap(); - assert_eq!( - enc, - b"/*testns\0*testdb\0*testtb\0!bltestix\0*\ - \x01\ - \0\0\0\0\0\0\0\x07" - ); - - let dec = Bl::decode(&enc).unwrap(); - assert_eq!(val, dec); - } -} diff --git a/lib/src/key/bp.rs b/lib/src/key/bp.rs deleted file mode 100644 index 03d94d1e..00000000 --- a/lib/src/key/bp.rs +++ /dev/null @@ -1,116 +0,0 @@ -use crate::idx::btree::NodeId; -use derive::Key; -use serde::{Deserialize, Serialize}; -use std::ops::Range; - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Bp<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, - pub node_id: Option, -} - -impl<'a> Bp<'a> { - pub fn new( - ns: &'a str, - db: &'a str, - tb: &'a str, - ix: &'a str, - node_id: Option, - ) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'p', - ix, - _g: b'*', - node_id, - } - } - - pub fn range(ns: &str, db: &str, tb: &str, ix: &str) -> Range> { - let mut beg = Prefix::new(ns, db, tb, ix).encode().unwrap(); - beg.extend_from_slice(&[0x00]); - let mut end = Prefix::new(ns, db, tb, ix).encode().unwrap(); - end.extend_from_slice(&[0xff]); - beg..end - } -} - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Prefix<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, -} - -impl<'a> Prefix<'a> { - pub fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'p', - ix, - _g: b'*', - } - } -} - -#[cfg(test)] -mod tests { - #[test] - fn key() { - use super::*; - #[rustfmt::skip] - let val = Bp::new( - "testns", - "testdb", - "testtb", - "testix", - Some(7) - ); - let enc = Bp::encode(&val).unwrap(); - assert_eq!( - enc, - b"/*testns\0*testdb\0*testtb\0!bptestix\0*\ - \x01\ - \0\0\0\0\0\0\0\x07" - ); - - let dec = Bp::decode(&enc).unwrap(); - assert_eq!(val, dec); - } -} diff --git a/lib/src/key/bt.rs b/lib/src/key/bt.rs deleted file mode 100644 index e7d5d437..00000000 --- a/lib/src/key/bt.rs +++ /dev/null @@ -1,115 +0,0 @@ -use crate::idx::btree::NodeId; -use derive::Key; -use serde::{Deserialize, Serialize}; -use std::ops::Range; - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Bt<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, - pub node_id: Option, -} - -impl<'a> Bt<'a> { - pub fn new( - ns: &'a str, - db: &'a str, - tb: &'a str, - ix: &'a str, - node_id: Option, - ) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b't', - ix, - _g: b'*', - node_id, - } - } - - pub fn range(ns: &str, db: &str, tb: &str, ix: &str) -> Range> { - let mut beg = Prefix::new(ns, db, tb, ix).encode().unwrap(); - beg.extend_from_slice(&[0x00]); - let mut end = Prefix::new(ns, db, tb, ix).encode().unwrap(); - end.extend_from_slice(&[0xff]); - beg..end - } -} - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -struct Prefix<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, -} - -impl<'a> Prefix<'a> { - fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b't', - ix, - _g: b'*', - } - } -} - -#[cfg(test)] -mod tests { - #[test] - fn key() { - use super::*; - #[rustfmt::skip] - let val = Bt::new( - "testns", - "testdb", - "testtb", - "testix", - Some(7) - ); - let enc = Bt::encode(&val).unwrap(); - assert_eq!( - enc, - b"/*testns\0*testdb\0*testtb\0!bttestix\0*\ - \x01\ - \0\0\0\0\0\0\0\x07" - ); - let dec = Bt::decode(&enc).unwrap(); - assert_eq!(val, dec); - } -} diff --git a/lib/src/key/bu.rs b/lib/src/key/bu.rs deleted file mode 100644 index f7d9813a..00000000 --- a/lib/src/key/bu.rs +++ /dev/null @@ -1,105 +0,0 @@ -use crate::idx::ft::terms::TermId; -use derive::Key; -use serde::{Deserialize, Serialize}; -use std::ops::Range; - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Bu<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, - pub term_id: TermId, -} - -impl<'a> Bu<'a> { - pub fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str, term_id: TermId) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'u', - ix, - _g: b'*', - term_id, - } - } - - pub fn range(ns: &str, db: &str, tb: &str, ix: &str) -> Range> { - let mut beg = Prefix::new(ns, db, tb, ix).encode().unwrap(); - beg.extend_from_slice(&[0x00]); - let mut end = Prefix::new(ns, db, tb, ix).encode().unwrap(); - end.extend_from_slice(&[0xff]); - beg..end - } -} - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -struct Prefix<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, -} - -impl<'a> Prefix<'a> { - fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'u', - ix, - _g: b'*', - } - } -} - -#[cfg(test)] -mod tests { - #[test] - fn key() { - use super::*; - #[rustfmt::skip] - let val = Bu::new( - "testns", - "testdb", - "testtb", - "testix", - 7 - ); - let enc = Bu::encode(&val).unwrap(); - assert_eq!(enc, b"/*testns\0*testdb\0*testtb\0!butestix\0*\0\0\0\0\0\0\0\x07"); - - let dec = Bu::decode(&enc).unwrap(); - assert_eq!(val, dec); - } -} diff --git a/lib/src/key/cf.rs b/lib/src/key/change/mod.rs similarity index 85% rename from lib/src/key/cf.rs rename to lib/src/key/change/mod.rs index 8697bdb2..3898e752 100644 --- a/lib/src/key/cf.rs +++ b/lib/src/key/change/mod.rs @@ -1,3 +1,4 @@ +/// Stores change feeds use derive::Key; use serde::{Deserialize, Serialize}; @@ -30,8 +31,8 @@ pub fn new<'a>(ns: &'a str, db: &'a str, ts: u64, tb: &'a str) -> Cf<'a> { #[allow(unused)] pub fn versionstamped_key_prefix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); - k.extend_from_slice(&[b'!', b'c', b'f']); + let mut k = crate::key::database::all::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[b'#']); k } @@ -49,8 +50,8 @@ pub fn versionstamped_key_suffix(tb: &str) -> Vec { /// specified versionstamp. #[allow(unused)] pub fn ts_prefix(ns: &str, db: &str, vs: vs::Versionstamp) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); - k.extend_from_slice(&[b'!', b'c', b'f']); + let mut k = crate::key::database::all::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[b'#']); k.extend_from_slice(&vs); k } @@ -58,16 +59,16 @@ pub fn ts_prefix(ns: &str, db: &str, vs: vs::Versionstamp) -> Vec { /// Returns the prefix for the whole database change feeds #[allow(unused)] pub fn prefix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); - k.extend_from_slice(&[b'!', b'c', b'f', 0x00]); + let mut k = crate::key::database::all::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[b'#', 0x00]); k } /// Returns the suffix for the whole database change feeds #[allow(unused)] pub fn suffix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); - k.extend_from_slice(&[b'!', b'c', b'f', 0xff]); + let mut k = crate::key::database::all::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[b'#', 0xff]); k } diff --git a/lib/src/key/database.rs b/lib/src/key/database/all.rs similarity index 65% rename from lib/src/key/database.rs rename to lib/src/key/database/all.rs index 8ab677fc..b2884dc0 100644 --- a/lib/src/key/database.rs +++ b/lib/src/key/database/all.rs @@ -1,8 +1,9 @@ +//! Stores the key prefix for all keys under a database use derive::Key; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Database<'a> { +pub struct All<'a> { __: u8, _a: u8, pub ns: &'a str, @@ -10,11 +11,11 @@ pub struct Database<'a> { pub db: &'a str, } -pub fn new<'a>(ns: &'a str, db: &'a str) -> Database<'a> { - Database::new(ns, db) +pub fn new<'a>(ns: &'a str, db: &'a str) -> All<'a> { + All::new(ns, db) } -impl<'a> Database<'a> { +impl<'a> All<'a> { pub fn new(ns: &'a str, db: &'a str) -> Self { Self { __: b'/', // / @@ -32,14 +33,14 @@ mod tests { fn key() { use super::*; #[rustfmt::skip] - let val = Database::new( + let val = All::new( "testns", "testdb", ); - let enc = Database::encode(&val).unwrap(); + let enc = All::encode(&val).unwrap(); assert_eq!(enc, b"/*testns\0*testdb\0"); - let dec = Database::decode(&enc).unwrap(); + let dec = All::decode(&enc).unwrap(); assert_eq!(val, dec); } } diff --git a/lib/src/key/az.rs b/lib/src/key/database/az.rs similarity index 89% rename from lib/src/key/az.rs rename to lib/src/key/database/az.rs index 9f88e7d4..5845d1fb 100644 --- a/lib/src/key/az.rs +++ b/lib/src/key/database/az.rs @@ -1,3 +1,4 @@ +//! Stores a DEFINE ANALYZER config definition use derive::Key; use serde::{Deserialize, Serialize}; @@ -19,13 +20,13 @@ pub fn new<'a>(ns: &'a str, db: &'a str, tb: &'a str) -> Az<'a> { } pub fn prefix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); + let mut k = super::all::new(ns, db).encode().unwrap(); k.extend_from_slice(&[b'!', b'a', b'z', 0x00]); k } pub fn suffix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); + let mut k = super::all::new(ns, db).encode().unwrap(); k.extend_from_slice(&[b'!', b'a', b'z', 0xff]); k } diff --git a/lib/src/key/fc.rs b/lib/src/key/database/fc.rs similarity index 89% rename from lib/src/key/fc.rs rename to lib/src/key/database/fc.rs index c9310496..859f8904 100644 --- a/lib/src/key/fc.rs +++ b/lib/src/key/database/fc.rs @@ -1,3 +1,4 @@ +/// Stores a DEFINE FUNCTION config definition use derive::Key; use serde::{Deserialize, Serialize}; @@ -19,13 +20,13 @@ pub fn new<'a>(ns: &'a str, db: &'a str, fc: &'a str) -> Fc<'a> { } pub fn prefix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); + let mut k = super::all::new(ns, db).encode().unwrap(); k.extend_from_slice(&[b'!', b'f', b'n', 0x00]); k } pub fn suffix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); + let mut k = super::all::new(ns, db).encode().unwrap(); k.extend_from_slice(&[b'!', b'f', b'n', 0xff]); k } diff --git a/lib/src/key/dl.rs b/lib/src/key/database/lg.rs similarity index 53% rename from lib/src/key/dl.rs rename to lib/src/key/database/lg.rs index 9888b3ea..ddaef438 100644 --- a/lib/src/key/dl.rs +++ b/lib/src/key/database/lg.rs @@ -1,8 +1,9 @@ +//! Stores a DEFINE LOGIN ON DATABASE config definition use derive::Key; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Dl<'a> { +pub struct Lg<'a> { __: u8, _a: u8, pub ns: &'a str, @@ -14,23 +15,23 @@ pub struct Dl<'a> { pub dl: &'a str, } -pub fn new<'a>(ns: &'a str, db: &'a str, dl: &'a str) -> Dl<'a> { - Dl::new(ns, db, dl) +pub fn new<'a>(ns: &'a str, db: &'a str, dl: &'a str) -> Lg<'a> { + Lg::new(ns, db, dl) } pub fn prefix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); - k.extend_from_slice(&[b'!', b'd', b'l', 0x00]); + let mut k = super::all::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[b'!', b'l', b'g', 0x00]); k } pub fn suffix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); - k.extend_from_slice(&[b'!', b'd', b'l', 0xff]); + let mut k = super::all::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[b'!', b'l', b'g', 0xff]); k } -impl<'a> Dl<'a> { +impl<'a> Lg<'a> { pub fn new(ns: &'a str, db: &'a str, dl: &'a str) -> Self { Self { __: b'/', @@ -39,8 +40,8 @@ impl<'a> Dl<'a> { _b: b'*', db, _c: b'!', - _d: b'd', - _e: b'l', + _d: b'l', + _e: b'g', dl, } } @@ -52,15 +53,15 @@ mod tests { fn key() { use super::*; #[rustfmt::skip] - let val = Dl::new( + let val = Lg::new( "testns", "testdb", "testdl", ); - let enc = Dl::encode(&val).unwrap(); - assert_eq!(enc, b"/*testns\0*testdb\0!dltestdl\0"); + let enc = Lg::encode(&val).unwrap(); + assert_eq!(enc, b"/*testns\0*testdb\0!lgtestdl\0"); - let dec = Dl::decode(&enc).unwrap(); + let dec = Lg::decode(&enc).unwrap(); assert_eq!(val, dec); } } diff --git a/lib/src/key/database/mod.rs b/lib/src/key/database/mod.rs new file mode 100644 index 00000000..7e9f622d --- /dev/null +++ b/lib/src/key/database/mod.rs @@ -0,0 +1,9 @@ +pub mod all; +pub mod az; +pub mod fc; +pub mod lg; +pub mod pa; +pub mod sc; +pub mod tb; +pub mod tk; +pub mod vs; diff --git a/lib/src/key/pa.rs b/lib/src/key/database/pa.rs similarity index 87% rename from lib/src/key/pa.rs rename to lib/src/key/database/pa.rs index 7be71bf4..9789041b 100644 --- a/lib/src/key/pa.rs +++ b/lib/src/key/database/pa.rs @@ -1,3 +1,4 @@ +//! Stores a DEFINE PARAM config definition use derive::Key; use serde::{Deserialize, Serialize}; @@ -19,13 +20,13 @@ pub fn new<'a>(ns: &'a str, db: &'a str, pa: &'a str) -> Pa<'a> { } pub fn prefix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); + let mut k = super::all::new(ns, db).encode().unwrap(); k.extend_from_slice(&[b'!', b'p', b'a', 0x00]); k } pub fn suffix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); + let mut k = super::all::new(ns, db).encode().unwrap(); k.extend_from_slice(&[b'!', b'p', b'a', 0xff]); k } diff --git a/lib/src/key/sc.rs b/lib/src/key/database/sc.rs similarity index 87% rename from lib/src/key/sc.rs rename to lib/src/key/database/sc.rs index 08b72ebb..4e5d9c7c 100644 --- a/lib/src/key/sc.rs +++ b/lib/src/key/database/sc.rs @@ -1,3 +1,4 @@ +//! Stores a DEFINE SCOPE config definition use derive::Key; use serde::{Deserialize, Serialize}; @@ -19,13 +20,13 @@ pub fn new<'a>(ns: &'a str, db: &'a str, sc: &'a str) -> Sc<'a> { } pub fn prefix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); + let mut k = super::all::new(ns, db).encode().unwrap(); k.extend_from_slice(&[b'!', b's', b'c', 0x00]); k } pub fn suffix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); + let mut k = super::all::new(ns, db).encode().unwrap(); k.extend_from_slice(&[b'!', b's', b'c', 0xff]); k } diff --git a/lib/src/key/tb.rs b/lib/src/key/database/tb.rs similarity index 87% rename from lib/src/key/tb.rs rename to lib/src/key/database/tb.rs index 17047324..bcb91c9d 100644 --- a/lib/src/key/tb.rs +++ b/lib/src/key/database/tb.rs @@ -1,3 +1,4 @@ +//! Stores a DEFINE TABLE config definition use derive::Key; use serde::{Deserialize, Serialize}; @@ -19,13 +20,13 @@ pub fn new<'a>(ns: &'a str, db: &'a str, tb: &'a str) -> Tb<'a> { } pub fn prefix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); + let mut k = super::all::new(ns, db).encode().unwrap(); k.extend_from_slice(&[b'!', b't', b'b', 0x00]); k } pub fn suffix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); + let mut k = super::all::new(ns, db).encode().unwrap(); k.extend_from_slice(&[b'!', b't', b'b', 0xff]); k } diff --git a/lib/src/key/dt.rs b/lib/src/key/database/tk.rs similarity index 55% rename from lib/src/key/dt.rs rename to lib/src/key/database/tk.rs index 956aae78..15460424 100644 --- a/lib/src/key/dt.rs +++ b/lib/src/key/database/tk.rs @@ -1,8 +1,9 @@ +/// Stores a DEFINE LOGIN ON DATABASE config definition use derive::Key; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Dt<'a> { +pub struct Tk<'a> { __: u8, _a: u8, pub ns: &'a str, @@ -14,23 +15,23 @@ pub struct Dt<'a> { pub tk: &'a str, } -pub fn new<'a>(ns: &'a str, db: &'a str, tb: &'a str) -> Dt<'a> { - Dt::new(ns, db, tb) +pub fn new<'a>(ns: &'a str, db: &'a str, tb: &'a str) -> Tk<'a> { + Tk::new(ns, db, tb) } pub fn prefix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); - k.extend_from_slice(&[b'!', b'd', b't', 0x00]); + let mut k = super::all::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[b'!', b't', b'k', 0x00]); k } pub fn suffix(ns: &str, db: &str) -> Vec { - let mut k = super::database::new(ns, db).encode().unwrap(); - k.extend_from_slice(&[b'!', b'd', b't', 0xff]); + let mut k = super::all::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[b'!', b't', b'k', 0xff]); k } -impl<'a> Dt<'a> { +impl<'a> Tk<'a> { pub fn new(ns: &'a str, db: &'a str, tk: &'a str) -> Self { Self { __: b'/', @@ -39,8 +40,8 @@ impl<'a> Dt<'a> { _b: b'*', db, _c: b'!', - _d: b'd', - _e: b't', + _d: b't', + _e: b'k', tk, } } @@ -52,27 +53,27 @@ mod tests { fn key() { use super::*; #[rustfmt::skip] - let val = Dt::new( + let val = Tk::new( "testns", "testdb", "testtk", ); - let enc = Dt::encode(&val).unwrap(); - assert_eq!(enc, b"/*testns\x00*testdb\x00!dttesttk\x00"); + let enc = Tk::encode(&val).unwrap(); + assert_eq!(enc, b"/*testns\x00*testdb\x00!tktesttk\x00"); - let dec = Dt::decode(&enc).unwrap(); + let dec = Tk::decode(&enc).unwrap(); assert_eq!(val, dec); } #[test] fn test_prefix() { let val = super::prefix("testns", "testdb"); - assert_eq!(val, b"/*testns\0*testdb\0!dt\0"); + assert_eq!(val, b"/*testns\0*testdb\0!tk\0"); } #[test] fn test_suffix() { let val = super::suffix("testns", "testdb"); - assert_eq!(val, b"/*testns\0*testdb\0!dt\xff"); + assert_eq!(val, b"/*testns\0*testdb\0!tk\xff"); } } diff --git a/lib/src/key/dv.rs b/lib/src/key/database/vs.rs similarity index 61% rename from lib/src/key/dv.rs rename to lib/src/key/database/vs.rs index b52db232..1e249875 100644 --- a/lib/src/key/dv.rs +++ b/lib/src/key/database/vs.rs @@ -1,9 +1,10 @@ +//! Stores database versionstamps use derive::Key; use serde::{Deserialize, Serialize}; -// Dv stands for Database Versionstamp +// Vs stands for Database Versionstamp #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Dv<'a> { +pub struct Vs<'a> { __: u8, _a: u8, pub ns: &'a str, @@ -15,21 +16,21 @@ pub struct Dv<'a> { } #[allow(unused)] -pub fn new<'a>(ns: &'a str, db: &'a str) -> Dv<'a> { - Dv::new(ns, db) +pub fn new<'a>(ns: &'a str, db: &'a str) -> Vs<'a> { + Vs::new(ns, db) } -impl<'a> Dv<'a> { +impl<'a> Vs<'a> { pub fn new(ns: &'a str, db: &'a str) -> Self { - Dv { + Vs { __: b'/', _a: b'*', ns, _b: b'*', db, _d: b'!', - _e: b't', - _f: b't', + _e: b'v', + _f: b's', } } } @@ -40,12 +41,12 @@ mod tests { fn key() { use super::*; #[rustfmt::skip] - let val = Dv::new( + let val = Vs::new( "test", "test", ); - let enc = Dv::encode(&val).unwrap(); - let dec = Dv::decode(&enc).unwrap(); + let enc = Vs::encode(&val).unwrap(); + let dec = Vs::decode(&enc).unwrap(); assert_eq!(val, dec); } } diff --git a/lib/src/key/debug.rs b/lib/src/key/debug.rs index d1f1d4f8..e7961502 100644 --- a/lib/src/key/debug.rs +++ b/lib/src/key/debug.rs @@ -1,3 +1,4 @@ +/// Debug purposes only. It may be used in logs. Not for key handling in implementation code. use crate::kvs::Key; /// Helpers for debugging keys diff --git a/lib/src/key/graph.rs b/lib/src/key/graph/mod.rs similarity index 99% rename from lib/src/key/graph.rs rename to lib/src/key/graph/mod.rs index 5a742693..feedfd9b 100644 --- a/lib/src/key/graph.rs +++ b/lib/src/key/graph/mod.rs @@ -1,3 +1,4 @@ +//! Stores a graph edge pointer use crate::sql::dir::Dir; use crate::sql::id::Id; use crate::sql::thing::Thing; diff --git a/lib/src/key/index/all.rs b/lib/src/key/index/all.rs new file mode 100644 index 00000000..fbc0c13e --- /dev/null +++ b/lib/src/key/index/all.rs @@ -0,0 +1,56 @@ +//! Stores the key prefix for all keys under an index +use derive::Key; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] +pub struct All<'a> { + __: u8, + _a: u8, + pub ns: &'a str, + _b: u8, + pub db: &'a str, + _c: u8, + pub tb: &'a str, + _d: u8, + pub ix: &'a str, +} + +pub fn new<'a>(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> All<'a> { + All::new(ns, db, tb, ix) +} + +impl<'a> All<'a> { + pub fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Self { + Self { + __: b'/', + _a: b'*', + ns, + _b: b'*', + db, + _c: b'*', + tb, + _d: b'+', + ix, + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn key() { + use super::*; + #[rustfmt::skip] + let val = All::new( + "testns", + "testdb", + "testtb", + "testix", + ); + let enc = All::encode(&val).unwrap(); + assert_eq!(enc, b"/*testns\0*testdb\0*testtb\0+testix\0"); + + let dec = All::decode(&enc).unwrap(); + assert_eq!(val, dec); + } +} diff --git a/lib/src/key/index/bc.rs b/lib/src/key/index/bc.rs new file mode 100644 index 00000000..600c801a --- /dev/null +++ b/lib/src/key/index/bc.rs @@ -0,0 +1,63 @@ +//! Stores Doc list for each term +use crate::idx::ft::terms::TermId; +use derive::Key; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] +pub struct Bc<'a> { + __: u8, + _a: u8, + pub ns: &'a str, + _b: u8, + pub db: &'a str, + _c: u8, + pub tb: &'a str, + _d: u8, + pub ix: &'a str, + _e: u8, + _f: u8, + _g: u8, + pub term_id: TermId, +} + +impl<'a> Bc<'a> { + pub fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str, term_id: TermId) -> Self { + Self { + __: b'/', + _a: b'*', + ns, + _b: b'*', + db, + _c: b'*', + tb, + _d: b'+', + ix, + _e: b'!', + _f: b'b', + _g: b'c', + term_id, + } + } +} + +#[cfg(test)] +mod tests { + + #[test] + fn key() { + use super::*; + #[rustfmt::skip] + let val = Bc::new( + "testns", + "testdb", + "testtb", + "testix", + 7 + ); + let enc = Bc::encode(&val).unwrap(); + assert_eq!(enc, b"/*testns\0*testdb\0*testtb\0+testix\0!bc\0\0\0\0\0\0\0\x07"); + + let dec = Bc::decode(&enc).unwrap(); + assert_eq!(val, dec); + } +} diff --git a/lib/src/key/index/bd.rs b/lib/src/key/index/bd.rs new file mode 100644 index 00000000..7ecb0116 --- /dev/null +++ b/lib/src/key/index/bd.rs @@ -0,0 +1,67 @@ +//! Stores BTree nodes for doc ids +use crate::idx::btree::NodeId; +use derive::Key; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] +pub struct Bd<'a> { + __: u8, + _a: u8, + pub ns: &'a str, + _b: u8, + pub db: &'a str, + _c: u8, + pub tb: &'a str, + _d: u8, + pub ix: &'a str, + _e: u8, + _f: u8, + _g: u8, + pub node_id: Option, +} + +impl<'a> Bd<'a> { + pub fn new( + ns: &'a str, + db: &'a str, + tb: &'a str, + ix: &'a str, + node_id: Option, + ) -> Self { + Self { + __: b'/', + _a: b'*', + ns, + _b: b'*', + db, + _c: b'*', + tb, + _d: b'+', + ix, + _e: b'!', + _f: b'b', + _g: b'd', + node_id, + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn key() { + use super::*; + #[rustfmt::skip] + let val = Bd::new( + "testns", + "testdb", + "testtb", + "testix", + Some(7) + ); + let enc = Bd::encode(&val).unwrap(); + assert_eq!(enc, b"/*testns\0*testdb\0*testtb\0+testix\0!bd\x01\0\0\0\0\0\0\0\x07"); + let dec = Bd::decode(&enc).unwrap(); + assert_eq!(val, dec); + } +} diff --git a/lib/src/key/index/bf.rs b/lib/src/key/index/bf.rs new file mode 100644 index 00000000..d9a66858 --- /dev/null +++ b/lib/src/key/index/bf.rs @@ -0,0 +1,77 @@ +//! Stores Term/Doc frequency +use crate::idx::ft::docids::DocId; +use crate::idx::ft::terms::TermId; +use derive::Key; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] +pub struct Bf<'a> { + __: u8, + _a: u8, + pub ns: &'a str, + _b: u8, + pub db: &'a str, + _c: u8, + pub tb: &'a str, + _d: u8, + pub ix: &'a str, + _e: u8, + _f: u8, + _g: u8, + pub term_id: TermId, + pub doc_id: DocId, +} + +impl<'a> Bf<'a> { + pub fn new( + ns: &'a str, + db: &'a str, + tb: &'a str, + ix: &'a str, + term_id: TermId, + doc_id: DocId, + ) -> Self { + Self { + __: b'/', + _a: b'*', + ns, + _b: b'*', + db, + _c: b'*', + tb, + _d: b'+', + ix, + _e: b'!', + _f: b'b', + _g: b'f', + term_id, + doc_id, + } + } +} + +#[cfg(test)] +mod tests { + use crate::key::index::bf::Bf; + + #[test] + fn key() { + #[rustfmt::skip] + let val = Bf::new( + "testns", + "testdb", + "testtb", + "testix", + 7, + 13 + ); + let enc = Bf::encode(&val).unwrap(); + assert_eq!( + enc, + b"/*testns\0*testdb\0*testtb\0+testix\0!bf\0\0\0\0\0\0\0\x07\0\0\0\0\0\0\0\x0d" + ); + + let dec = Bf::decode(&enc).unwrap(); + assert_eq!(val, dec); + } +} diff --git a/lib/src/key/index/bi.rs b/lib/src/key/index/bi.rs new file mode 100644 index 00000000..208bc377 --- /dev/null +++ b/lib/src/key/index/bi.rs @@ -0,0 +1,62 @@ +//! Stores doc keys for doc_ids +use crate::idx::btree::NodeId; +use derive::Key; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] +pub struct Bi<'a> { + __: u8, + _a: u8, + pub ns: &'a str, + _b: u8, + pub db: &'a str, + _c: u8, + pub tb: &'a str, + _d: u8, + pub ix: &'a str, + _e: u8, + _f: u8, + _g: u8, + pub node_id: NodeId, +} + +impl<'a> Bi<'a> { + pub fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str, node_id: NodeId) -> Self { + Bi { + __: b'/', + _a: b'*', + ns, + _b: b'*', + db, + _c: b'*', + tb, + _d: b'+', + ix, + _e: b'!', + _f: b'b', + _g: b'i', + node_id, + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn key() { + use super::*; + #[rustfmt::skip] + let val = Bi::new( + "testns", + "testdb", + "testtb", + "testix", + 7 + ); + let enc = Bi::encode(&val).unwrap(); + assert_eq!(enc, b"/*testns\0*testdb\0*testtb\0+testix\0!bi\0\0\0\0\0\0\0\x07"); + + let dec = Bi::decode(&enc).unwrap(); + assert_eq!(val, dec); + } +} diff --git a/lib/src/key/index/bk.rs b/lib/src/key/index/bk.rs new file mode 100644 index 00000000..bb3540d1 --- /dev/null +++ b/lib/src/key/index/bk.rs @@ -0,0 +1,62 @@ +//! Stores the term list for doc_ids +use crate::idx::ft::docids::DocId; +use derive::Key; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] +pub struct Bk<'a> { + __: u8, + _a: u8, + pub ns: &'a str, + _b: u8, + pub db: &'a str, + _c: u8, + pub tb: &'a str, + _d: u8, + pub ix: &'a str, + _e: u8, + _f: u8, + _g: u8, + pub doc_id: DocId, +} + +impl<'a> Bk<'a> { + pub fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str, doc_id: DocId) -> Self { + Self { + __: b'/', + _a: b'*', + ns, + _b: b'*', + db, + _c: b'*', + tb, + _d: b'+', + ix, + _e: b'!', + _f: b'b', + _g: b'k', + doc_id, + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn key() { + use super::*; + #[rustfmt::skip] + let val = Bk::new( + "testns", + "testdb", + "testtb", + "testix", + 7 + ); + let enc = Bk::encode(&val).unwrap(); + assert_eq!(enc, b"/*testns\0*testdb\0*testtb\0+testix\0!bk\0\0\0\0\0\0\0\x07"); + + let dec = Bk::decode(&enc).unwrap(); + assert_eq!(val, dec); + } +} diff --git a/lib/src/key/index/bl.rs b/lib/src/key/index/bl.rs new file mode 100644 index 00000000..3299d296 --- /dev/null +++ b/lib/src/key/index/bl.rs @@ -0,0 +1,68 @@ +//! Stores BTree nodes for doc lengths +use crate::idx::btree::NodeId; +use derive::Key; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] +pub struct Bl<'a> { + __: u8, + _a: u8, + pub ns: &'a str, + _b: u8, + pub db: &'a str, + _c: u8, + pub tb: &'a str, + _d: u8, + pub ix: &'a str, + _e: u8, + _f: u8, + _g: u8, + pub node_id: Option, +} + +impl<'a> Bl<'a> { + pub fn new( + ns: &'a str, + db: &'a str, + tb: &'a str, + ix: &'a str, + node_id: Option, + ) -> Self { + Self { + __: b'/', + _a: b'*', + ns, + _b: b'*', + db, + _c: b'*', + tb, + _d: b'+', + ix, + _e: b'!', + _f: b'b', + _g: b'l', + node_id, + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn key() { + use super::*; + #[rustfmt::skip] + let val = Bl::new( + "testns", + "testdb", + "testtb", + "testix", + Some(7) + ); + let enc = Bl::encode(&val).unwrap(); + assert_eq!(enc, b"/*testns\0*testdb\0*testtb\0+testix\0!bl\x01\0\0\0\0\0\0\0\x07"); + + let dec = Bl::decode(&enc).unwrap(); + assert_eq!(val, dec); + } +} diff --git a/lib/src/key/bo.rs b/lib/src/key/index/bo.rs similarity index 52% rename from lib/src/key/bo.rs rename to lib/src/key/index/bo.rs index 51865826..68d1376e 100644 --- a/lib/src/key/bo.rs +++ b/lib/src/key/index/bo.rs @@ -1,8 +1,8 @@ +//! Stores the offsets use crate::idx::ft::docids::DocId; use crate::idx::ft::terms::TermId; use derive::Key; use serde::{Deserialize, Serialize}; -use std::ops::Range; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] pub struct Bo<'a> { @@ -14,9 +14,9 @@ pub struct Bo<'a> { _c: u8, pub tb: &'a str, _d: u8, + pub ix: &'a str, _e: u8, _f: u8, - pub ix: &'a str, _g: u8, pub doc_id: DocId, pub term_id: TermId, @@ -39,58 +39,15 @@ impl<'a> Bo<'a> { db, _c: b'*', tb, - _d: b'!', - _e: b'b', - _f: b'o', + _d: b'+', ix, - _g: b'*', + _e: b'!', + _f: b'b', + _g: b'o', doc_id, term_id, } } - - pub fn range(ns: &str, db: &str, tb: &str, ix: &str) -> Range> { - let mut beg = Prefix::new(ns, db, tb, ix).encode().unwrap(); - beg.extend_from_slice(&[0x00]); - let mut end = Prefix::new(ns, db, tb, ix).encode().unwrap(); - end.extend_from_slice(&[0xff]); - beg..end - } -} - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Prefix<'a> { - __: u8, - _a: u8, - pub ns: &'a str, - _b: u8, - pub db: &'a str, - _c: u8, - pub tb: &'a str, - _d: u8, - _e: u8, - _f: u8, - pub ix: &'a str, - _g: u8, -} - -impl<'a> Prefix<'a> { - fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Self { - Self { - __: b'/', - _a: b'*', - ns, - _b: b'*', - db, - _c: b'*', - tb, - _d: b'!', - _e: b'b', - _f: b'o', - ix, - _g: b'*', - } - } } #[cfg(test)] @@ -109,7 +66,7 @@ mod tests { let enc = Bo::encode(&val).unwrap(); assert_eq!( enc, - b"/*testns\0*testdb\0*testtb\0!botestix\0*\0\0\0\0\0\0\0\x01\0\0\0\0\0\0\0\x02" + b"/*testns\0*testdb\0*testtb\0+testix\0!bo\0\0\0\0\0\0\0\x01\0\0\0\0\0\0\0\x02" ); let dec = Bo::decode(&enc).unwrap(); diff --git a/lib/src/key/index/bp.rs b/lib/src/key/index/bp.rs new file mode 100644 index 00000000..22336325 --- /dev/null +++ b/lib/src/key/index/bp.rs @@ -0,0 +1,68 @@ +//! Stores BTree nodes for postings +use crate::idx::btree::NodeId; +use derive::Key; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] +pub struct Bp<'a> { + __: u8, + _a: u8, + pub ns: &'a str, + _b: u8, + pub db: &'a str, + _c: u8, + pub tb: &'a str, + _d: u8, + pub ix: &'a str, + _e: u8, + _f: u8, + _g: u8, + pub node_id: Option, +} + +impl<'a> Bp<'a> { + pub fn new( + ns: &'a str, + db: &'a str, + tb: &'a str, + ix: &'a str, + node_id: Option, + ) -> Self { + Self { + __: b'/', + _a: b'*', + ns, + _b: b'*', + db, + _c: b'*', + tb, + _d: b'+', + ix, + _e: b'!', + _f: b'b', + _g: b'p', + node_id, + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn key() { + use super::*; + #[rustfmt::skip] + let val = Bp::new( + "testns", + "testdb", + "testtb", + "testix", + Some(7) + ); + let enc = Bp::encode(&val).unwrap(); + assert_eq!(enc, b"/*testns\0*testdb\0*testtb\0+testix\0!bp\x01\0\0\0\0\0\0\0\x07"); + + let dec = Bp::decode(&enc).unwrap(); + assert_eq!(val, dec); + } +} diff --git a/lib/src/key/bs.rs b/lib/src/key/index/bs.rs similarity index 96% rename from lib/src/key/bs.rs rename to lib/src/key/index/bs.rs index 3baa6caf..5f561229 100644 --- a/lib/src/key/bs.rs +++ b/lib/src/key/index/bs.rs @@ -1,3 +1,4 @@ +//! Stores FullText index states use derive::Key; use serde::{Deserialize, Serialize}; diff --git a/lib/src/key/index/bt.rs b/lib/src/key/index/bt.rs new file mode 100644 index 00000000..360b4940 --- /dev/null +++ b/lib/src/key/index/bt.rs @@ -0,0 +1,67 @@ +//! Stores BTree nodes for terms +use crate::idx::btree::NodeId; +use derive::Key; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] +pub struct Bt<'a> { + __: u8, + _a: u8, + pub ns: &'a str, + _b: u8, + pub db: &'a str, + _c: u8, + pub tb: &'a str, + _d: u8, + pub ix: &'a str, + _e: u8, + _f: u8, + _g: u8, + pub node_id: Option, +} + +impl<'a> Bt<'a> { + pub fn new( + ns: &'a str, + db: &'a str, + tb: &'a str, + ix: &'a str, + node_id: Option, + ) -> Self { + Self { + __: b'/', + _a: b'*', + ns, + _b: b'*', + db, + _c: b'*', + tb, + _d: b'+', + ix, + _e: b'!', + _f: b'b', + _g: b't', + node_id, + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn key() { + use super::*; + #[rustfmt::skip] + let val = Bt::new( + "testns", + "testdb", + "testtb", + "testix", + Some(7) + ); + let enc = Bt::encode(&val).unwrap(); + assert_eq!(enc, b"/*testns\0*testdb\0*testtb\0+testix\0!bt\x01\0\0\0\0\0\0\0\x07"); + let dec = Bt::decode(&enc).unwrap(); + assert_eq!(val, dec); + } +} diff --git a/lib/src/key/index/bu.rs b/lib/src/key/index/bu.rs new file mode 100644 index 00000000..d62e3d7e --- /dev/null +++ b/lib/src/key/index/bu.rs @@ -0,0 +1,62 @@ +//! Stores terms for term_ids +use crate::idx::ft::terms::TermId; +use derive::Key; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] +pub struct Bu<'a> { + __: u8, + _a: u8, + pub ns: &'a str, + _b: u8, + pub db: &'a str, + _c: u8, + pub tb: &'a str, + _d: u8, + pub ix: &'a str, + _e: u8, + _f: u8, + _g: u8, + pub term_id: TermId, +} + +impl<'a> Bu<'a> { + pub fn new(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str, term_id: TermId) -> Self { + Self { + __: b'/', + _a: b'*', + ns, + _b: b'*', + db, + _c: b'*', + tb, + _d: b'+', + ix, + _e: b'!', + _f: b'b', + _g: b'u', + term_id, + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn key() { + use super::*; + #[rustfmt::skip] + let val = Bu::new( + "testns", + "testdb", + "testtb", + "testix", + 7 + ); + let enc = Bu::encode(&val).unwrap(); + assert_eq!(enc, b"/*testns\0*testdb\0*testtb\0+testix\0!bu\0\0\0\0\0\0\0\x07"); + + let dec = Bu::decode(&enc).unwrap(); + assert_eq!(val, dec); + } +} diff --git a/lib/src/key/index.rs b/lib/src/key/index/mod.rs similarity index 86% rename from lib/src/key/index.rs rename to lib/src/key/index/mod.rs index 5a048d60..5d851ae0 100644 --- a/lib/src/key/index.rs +++ b/lib/src/key/index/mod.rs @@ -1,4 +1,17 @@ -use crate::key::CHAR_INDEX; +//! Stores an index entry +pub mod all; +pub mod bc; +pub mod bd; +pub mod bf; +pub mod bi; +pub mod bk; +pub mod bl; +pub mod bo; +pub mod bp; +pub mod bs; +pub mod bt; +pub mod bu; + use crate::sql::array::Array; use crate::sql::id::Id; use derive::Key; @@ -16,6 +29,7 @@ struct Prefix<'a> { pub tb: &'a str, _d: u8, pub ix: &'a str, + _e: u8, } impl<'a> Prefix<'a> { @@ -28,8 +42,9 @@ impl<'a> Prefix<'a> { db, _c: b'*', tb, - _d: CHAR_INDEX, + _d: b'+', ix, + _e: b'*', } } } @@ -45,6 +60,7 @@ struct PrefixIds<'a> { pub tb: &'a str, _d: u8, pub ix: &'a str, + _e: u8, pub fd: Array, } @@ -58,8 +74,9 @@ impl<'a> PrefixIds<'a> { db, _c: b'*', tb, - _d: CHAR_INDEX, + _d: b'+', ix, + _e: b'*', fd: fd.to_owned(), } } @@ -76,6 +93,7 @@ pub struct Index<'a> { pub tb: &'a str, _d: u8, pub ix: &'a str, + _e: u8, pub fd: Array, pub id: Option, } @@ -97,8 +115,9 @@ impl<'a> Index<'a> { db, _c: b'*', tb, - _d: CHAR_INDEX, + _d: b'+', ix, + _e: b'*', fd, id, } @@ -138,7 +157,7 @@ mod tests { let enc = Index::encode(&val).unwrap(); assert_eq!( enc, - b"/*testns\0*testdb\0*testtb\0\xa4testix\0\0\0\0\x04testfd1\0\0\0\0\x04testfd2\0\x01\x01\0\0\0\x01testid\0" + b"/*testns\0*testdb\0*testtb\0+testix\0*\0\0\0\x04testfd1\0\0\0\0\x04testfd2\0\x01\x01\0\0\0\x01testid\0" ); let dec = Index::decode(&enc).unwrap(); diff --git a/lib/src/key/mod.rs b/lib/src/key/mod.rs index 1a960eec..56021565 100644 --- a/lib/src/key/mod.rs +++ b/lib/src/key/mod.rs @@ -1,98 +1,67 @@ //! How the keys are structured in the key value store /// -/// KV / +/// crate::key::root::all / +/// crate::key::root::hb /!hb{ts}/{nd} +/// crate::key::root::nd /!nd{nd} +/// crate::key::root::ns /!ns{ns} /// -/// HB /!hb{ts}/{nd} +/// crate::key::node::all /${nd} +/// crate::key::node::lq /${nd}!lq{lq}{ns}{db} /// -/// ND /!nd{nd} -/// NQ /!nd{nd}*{ns}*{db}!lq{lq} +/// crate::key::namespace::all /*{ns} +/// crate::key::namespace::db /*{ns}!db{db} +/// crate::key::namespace::lg /*{ns}!lg{lg} +/// crate::key::namespace::tk /*{ns}!tk{tk} /// -/// NS /!ns{ns} +/// crate::key::database::all /*{ns}*{db} +/// crate::key::database::az /*{ns}*{db}!az{az} +/// crate::key::database::cf /*{ns}*{db}!cf{ts} +/// crate::key::database::fc /*{ns}*{db}!fn{fc} +/// crate::key::database::lg /*{ns}*{db}!lg{lg} +/// crate::key::database::pa /*{ns}*{db}!pa{pa} +/// crate::key::database::sc /*{ns}*{db}!sc{sc} +/// crate::key::database::tb /*{ns}*{db}!tb{tb} +/// crate::key::database::tk /*{ns}*{db}!tk{tk} +/// crate::key::database::vs /*{ns}*{db}!vs /// -/// Namespace /*{ns} -/// NL /*{ns}!nl{us} -/// NT /*{ns}!nt{tk} -/// DB /*{ns}!db{db} +/// crate::key::scope::all /*{ns}*{db}±{sc} +/// crate::key::scope::tk /*{ns}*{db}±{sc}!tk{tk} /// -/// Database /*{ns}*{db} -/// AZ /*{ns}*{db}!az{az} -/// CF /*{ns}*{db}!cf{ts} -/// DL /*{ns}*{db}!dl{us} -/// DT /*{ns}*{db}!dt{tk} -/// PA /*{ns}*{db}!pa{pa} -/// SC /*{ns}*{db}!sc{sc} -/// TB /*{ns}*{db}!tb{tb} +/// crate::key::table::all /*{ns}*{db}*{tb} +/// crate::key::table::ev /*{ns}*{db}*{tb}!ev{ev} +/// crate::key::table::fd /*{ns}*{db}*{tb}!fd{fd} +/// crate::key::table::ft /*{ns}*{db}*{tb}!ft{ft} +/// crate::key::table::ix /*{ns}*{db}*{tb}!ix{ix} +/// crate::key::table::lq /*{ns}*{db}*{tb}!lq{lq} /// -/// Scope /*{ns}*{db}±{sc} -/// ST /*{ns}*{db}±{sc}!st{tk} +/// crate::key::index::all /*{ns}*{db}*{tb}+{ix} +/// crate::key::index::bc /*{ns}*{db}*{tb}+{ix}!bc{id} +/// crate::key::index::bd /*{ns}*{db}*{tb}+{ix}!bd{id} +/// crate::key::index::bf /*{ns}*{db}*{tb}+{ix}!bf{id} +/// crate::key::index::bi /*{ns}*{db}*{tb}+{ix}!bi{id} +/// crate::key::index::bk /*{ns}*{db}*{tb}+{ix}!bk{id} +/// crate::key::index::bl /*{ns}*{db}*{tb}+{ix}!bl{id} +/// crate::key::index::bo /*{ns}*{db}*{tb}+{ix}!bo{id} +/// crate::key::index::bp /*{ns}*{db}*{tb}+{ix}!bp{id} +/// crate::key::index::bs /*{ns}*{db}*{tb}+{ix}!bs +/// crate::key::index::bt /*{ns}*{db}*{tb}+{ix}!bt{id} +/// crate::key::index::bu /*{ns}*{db}*{tb}+{ix}!bu{id} +/// crate::key::index /*{ns}*{db}*{tb}+{ix}*{fd}{id} /// -/// AZ /*{ns}*{db}!az{az} +/// crate::key::change /*{ns}*{db}#{ts} /// -/// Table /*{ns}*{db}*{tb} -/// EV /*{ns}*{db}*{tb}!ev{ev} -/// FD /*{ns}*{db}*{tb}!fd{fd} -/// FT /*{ns}*{db}*{tb}!ft{ft} -/// IX /*{ns}*{db}*{tb}!ix{ix} -/// LV /*{ns}*{db}*{tb}!lv{lv} +/// crate::key::thing /*{ns}*{db}*{tb}*{id} /// -/// Thing /*{ns}*{db}*{tb}*{id} +/// crate::key::graph /*{ns}*{db}*{tb}~{id}{eg}{fk} /// -/// Graph /*{ns}*{db}*{tb}~{id}{eg}{fk} -/// -/// Index /*{ns}*{db}*{tb}¤{ix}{fd}{id} -/// -/// BC /*{ns}*{db}*{tb}!bc{ix}*{id} -/// BD /*{ns}*{db}*{tb}!bd{ix}*{id} -/// BF /*{ns}*{db}*{tb}!bf{ix}*{id} -/// BI /*{ns}*{db}*{tb}!bi{ix}*{id} -/// BK /*{ns}*{db}*{tb}!bk{ix}*{id} -/// BL /*{ns}*{db}*{tb}!bl{ix}*{id} -/// BP /*{ns}*{db}*{tb}!bp{ix}*{id} -/// BS /*{ns}*{db}*{tb}!bs{ix} -/// BT /*{ns}*{db}*{tb}!bt{ix}*{id} -/// BU /*{ns}*{db}*{tb}!bu{ix}*{id} -pub mod az; // Stores a DEFINE ANALYZER config definition -pub mod bc; // Stores Doc list for each term -pub mod bd; // Stores BTree nodes for doc ids -pub mod bf; // Stores Term/Doc frequency -pub mod bi; // Stores doc keys for doc_ids -pub mod bk; // Stores the term list for doc_ids -pub mod bl; // Stores BTree nodes for doc lengths -pub mod bo; // Stores the offsets -pub mod bp; // Stores BTree nodes for postings -pub mod bs; // Stores FullText index states -pub mod bt; // Stores BTree nodes for terms -pub mod bu; // Stores terms for term_ids -pub mod cf; // Stores change feeds -pub mod cl; // Stores cluster membership information -pub mod database; // Stores the key prefix for all keys under a database -pub mod db; // Stores a DEFINE DATABASE config definition -pub mod debug; // Debug purposes only. It may be used in logs. Not for key handling in implementation code. -pub mod dl; // Stores a DEFINE LOGIN ON DATABASE config definition -pub mod dt; // Stores a DEFINE LOGIN ON DATABASE config definition -pub mod dv; // Stores database versionstamps -pub mod ev; // Stores a DEFINE EVENT config definition -pub mod fc; // Stores a DEFINE FUNCTION config definition -pub mod fd; // Stores a DEFINE FIELD config definition -pub mod ft; // Stores a DEFINE TABLE AS config definition -pub mod graph; // Stores a graph edge pointer -pub mod hb; // Stores a heartbeat per registered cluster node -pub mod index; // Stores an index entry -pub mod ix; // Stores a DEFINE INDEX config definition -pub mod kv; // Stores the key prefix for all keys -pub mod lq; // Stores a LIVE SELECT query definition on the database -pub mod lv; // Stores a LIVE SELECT query definition on the table -pub mod namespace; // Stores the key prefix for all keys under a namespace -pub mod nl; // Stores a DEFINE LOGIN ON NAMESPACE config definition -pub mod ns; // Stores a DEFINE NAMESPACE config definition -pub mod nt; // Stores a DEFINE TOKEN ON NAMESPACE config definition -pub mod pa; // Stores a DEFINE PARAM config definition -pub mod sc; // Stores a DEFINE SCOPE config definition -pub mod scope; // Stores the key prefix for all keys under a scope -pub mod st; // Stores a DEFINE TOKEN ON SCOPE config definition -pub mod table; // Stores the key prefix for all keys under a table -pub mod tb; // Stores a DEFINE TABLE config definition -pub mod thing; // Stores a record id - -const CHAR_PATH: u8 = 0xb1; // ± -const CHAR_INDEX: u8 = 0xa4; // ¤ +pub mod change; +pub mod database; +pub mod debug; +pub mod graph; +pub mod index; +pub mod namespace; +pub mod node; +pub mod root; +pub mod scope; +pub mod table; +pub mod thing; diff --git a/lib/src/key/namespace.rs b/lib/src/key/namespace/all.rs similarity index 63% rename from lib/src/key/namespace.rs rename to lib/src/key/namespace/all.rs index 9ae15a96..e420e2d2 100644 --- a/lib/src/key/namespace.rs +++ b/lib/src/key/namespace/all.rs @@ -1,18 +1,19 @@ +//! Stores the key prefix for all keys under a namespace use derive::Key; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Namespace<'a> { +pub struct All<'a> { __: u8, _a: u8, pub ns: &'a str, } -pub fn new(ns: &str) -> Namespace<'_> { - Namespace::new(ns) +pub fn new(ns: &str) -> All<'_> { + All::new(ns) } -impl<'a> Namespace<'a> { +impl<'a> All<'a> { pub fn new(ns: &'a str) -> Self { Self { __: b'/', @@ -28,13 +29,13 @@ mod tests { fn key() { use super::*; #[rustfmt::skip] - let val = Namespace::new( + let val = All::new( "testns", ); - let enc = Namespace::encode(&val).unwrap(); + let enc = All::encode(&val).unwrap(); assert_eq!(enc, b"/*testns\0"); - let dec = Namespace::decode(&enc).unwrap(); + let dec = All::decode(&enc).unwrap(); assert_eq!(val, dec); } } diff --git a/lib/src/key/db.rs b/lib/src/key/namespace/db.rs similarity index 88% rename from lib/src/key/db.rs rename to lib/src/key/namespace/db.rs index fb4de51f..9476adba 100644 --- a/lib/src/key/db.rs +++ b/lib/src/key/namespace/db.rs @@ -1,3 +1,4 @@ +/// Stores a DEFINE DATABASE config definition use derive::Key; use serde::{Deserialize, Serialize}; @@ -17,13 +18,13 @@ pub fn new<'a>(ns: &'a str, db: &'a str) -> Db<'a> { } pub fn prefix(ns: &str) -> Vec { - let mut k = super::namespace::new(ns).encode().unwrap(); + let mut k = super::all::new(ns).encode().unwrap(); k.extend_from_slice(&[b'!', b'd', b'b', 0x00]); k } pub fn suffix(ns: &str) -> Vec { - let mut k = super::namespace::new(ns).encode().unwrap(); + let mut k = super::all::new(ns).encode().unwrap(); k.extend_from_slice(&[b'!', b'd', b'b', 0xff]); k } diff --git a/lib/src/key/nl.rs b/lib/src/key/namespace/lg.rs similarity index 51% rename from lib/src/key/nl.rs rename to lib/src/key/namespace/lg.rs index ddde749c..a42d3b82 100644 --- a/lib/src/key/nl.rs +++ b/lib/src/key/namespace/lg.rs @@ -1,8 +1,9 @@ +//! Stores a DEFINE LOGIN ON NAMESPACE config definition use derive::Key; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Nl<'a> { +pub struct Lg<'a> { __: u8, _a: u8, pub ns: &'a str, @@ -12,31 +13,31 @@ pub struct Nl<'a> { pub us: &'a str, } -pub fn new<'a>(ns: &'a str, us: &'a str) -> Nl<'a> { - Nl::new(ns, us) +pub fn new<'a>(ns: &'a str, us: &'a str) -> Lg<'a> { + Lg::new(ns, us) } pub fn prefix(ns: &str) -> Vec { - let mut k = super::namespace::new(ns).encode().unwrap(); - k.extend_from_slice(&[b'!', b'n', b'l', 0x00]); + let mut k = super::all::new(ns).encode().unwrap(); + k.extend_from_slice(&[b'!', b'l', b'g', 0x00]); k } pub fn suffix(ns: &str) -> Vec { - let mut k = super::namespace::new(ns).encode().unwrap(); - k.extend_from_slice(&[b'!', b'n', b'l', 0xff]); + let mut k = super::all::new(ns).encode().unwrap(); + k.extend_from_slice(&[b'!', b'l', b'g', 0xff]); k } -impl<'a> Nl<'a> { +impl<'a> Lg<'a> { pub fn new(ns: &'a str, us: &'a str) -> Self { Self { __: b'/', _a: b'*', ns, _b: b'!', - _c: b'n', - _d: b'l', + _c: b'l', + _d: b'g', us, } } @@ -48,14 +49,14 @@ mod tests { fn key() { use super::*; #[rustfmt::skip] - let val = Nl::new( + let val = Lg::new( "testns", "testus", ); - let enc = Nl::encode(&val).unwrap(); - assert_eq!(enc, b"/*testns\0!nltestus\0"); + let enc = Lg::encode(&val).unwrap(); + assert_eq!(enc, b"/*testns\0!lgtestus\0"); - let dec = Nl::decode(&enc).unwrap(); + let dec = Lg::decode(&enc).unwrap(); assert_eq!(val, dec); } } diff --git a/lib/src/key/namespace/mod.rs b/lib/src/key/namespace/mod.rs new file mode 100644 index 00000000..e4efb81a --- /dev/null +++ b/lib/src/key/namespace/mod.rs @@ -0,0 +1,4 @@ +pub mod all; +pub mod db; +pub mod lg; +pub mod tk; diff --git a/lib/src/key/nt.rs b/lib/src/key/namespace/tk.rs similarity index 51% rename from lib/src/key/nt.rs rename to lib/src/key/namespace/tk.rs index 85b62b4b..c1047730 100644 --- a/lib/src/key/nt.rs +++ b/lib/src/key/namespace/tk.rs @@ -1,8 +1,9 @@ +//! Stores a DEFINE TOKEN ON NAMESPACE config definition use derive::Key; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Nt<'a> { +pub struct Tk<'a> { __: u8, _a: u8, pub ns: &'a str, @@ -12,31 +13,31 @@ pub struct Nt<'a> { pub tk: &'a str, } -pub fn new<'a>(ns: &'a str, tk: &'a str) -> Nt<'a> { - Nt::new(ns, tk) +pub fn new<'a>(ns: &'a str, tk: &'a str) -> Tk<'a> { + Tk::new(ns, tk) } pub fn prefix(ns: &str) -> Vec { - let mut k = super::namespace::new(ns).encode().unwrap(); - k.extend_from_slice(&[b'!', b'n', b't', 0x00]); + let mut k = super::all::new(ns).encode().unwrap(); + k.extend_from_slice(&[b'!', b't', b'k', 0x00]); k } pub fn suffix(ns: &str) -> Vec { - let mut k = super::namespace::new(ns).encode().unwrap(); - k.extend_from_slice(&[b'!', b'n', b't', 0xff]); + let mut k = super::all::new(ns).encode().unwrap(); + k.extend_from_slice(&[b'!', b't', b'k', 0xff]); k } -impl<'a> Nt<'a> { +impl<'a> Tk<'a> { pub fn new(ns: &'a str, tk: &'a str) -> Self { Self { __: b'/', _a: b'*', ns, _b: b'!', - _c: b'n', - _d: b't', + _c: b't', + _d: b'k', tk, } } @@ -48,13 +49,13 @@ mod tests { fn key() { use super::*; #[rustfmt::skip] - let val = Nt::new( + let val = Tk::new( "testns", "testtk", ); - let enc = Nt::encode(&val).unwrap(); - assert_eq!(enc, b"/*testns\0!nttesttk\0"); - let dec = Nt::decode(&enc).unwrap(); + let enc = Tk::encode(&val).unwrap(); + assert_eq!(enc, b"/*testns\0!tktesttk\0"); + let dec = Tk::decode(&enc).unwrap(); assert_eq!(val, dec); } } diff --git a/lib/src/key/node/all.rs b/lib/src/key/node/all.rs new file mode 100644 index 00000000..fd24ed42 --- /dev/null +++ b/lib/src/key/node/all.rs @@ -0,0 +1,45 @@ +//! Stores the key prefix for all nodes +use derive::Key; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] +pub struct All { + __: u8, + _a: u8, + #[serde(with = "uuid::serde::compact")] + pub nd: Uuid, +} + +pub fn new(nd: Uuid) -> All { + All::new(nd) +} + +impl All { + pub fn new(nd: Uuid) -> Self { + Self { + __: b'/', + _a: b'$', + nd, + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn key() { + use super::*; + #[rustfmt::skip] + let nd = Uuid::from_bytes([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]); + #[rustfmt::skip] + let val = All::new( + nd, + ); + let enc = All::encode(&val).unwrap(); + assert_eq!(enc, b"/$\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"); + + let dec = All::decode(&enc).unwrap(); + assert_eq!(val, dec); + } +} diff --git a/lib/src/key/lq.rs b/lib/src/key/node/lq.rs similarity index 94% rename from lib/src/key/lq.rs rename to lib/src/key/node/lq.rs index a491081c..ae2182c8 100644 --- a/lib/src/key/lq.rs +++ b/lib/src/key/node/lq.rs @@ -1,3 +1,4 @@ +//! Stores a LIVE SELECT query definition on the cluster use derive::Key; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -53,7 +54,7 @@ impl<'a> Lq<'a> { db, _f: b'!', _g: b'l', - _h: b'v', + _h: b'q', lq, } } @@ -71,7 +72,7 @@ mod tests { let lv = Uuid::from_bytes([0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20]); let val = Lq::new(nd, "testns", "testdb", lv); let enc = Lq::encode(&val).unwrap(); - assert_eq!(enc, b"/!nd\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10*testns\x00*testdb\x00!lv\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20" ); + assert_eq!(enc, b"/!nd\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10*testns\x00*testdb\x00!lq\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20" ); let dec = Lq::decode(&enc).unwrap(); assert_eq!(val, dec); diff --git a/lib/src/key/node/mod.rs b/lib/src/key/node/mod.rs new file mode 100644 index 00000000..450f7677 --- /dev/null +++ b/lib/src/key/node/mod.rs @@ -0,0 +1,2 @@ +pub mod all; +pub mod lq; diff --git a/lib/src/key/kv.rs b/lib/src/key/root/all.rs similarity index 93% rename from lib/src/key/kv.rs rename to lib/src/key/root/all.rs index c576c55f..cd1e306e 100644 --- a/lib/src/key/kv.rs +++ b/lib/src/key/root/all.rs @@ -1,3 +1,4 @@ +//! Stores the key prefix for all keys use derive::Key; use serde::{Deserialize, Serialize}; diff --git a/lib/src/key/hb.rs b/lib/src/key/root/hb.rs similarity index 81% rename from lib/src/key/hb.rs rename to lib/src/key/root/hb.rs index 18f0b698..26873859 100644 --- a/lib/src/key/hb.rs +++ b/lib/src/key/root/hb.rs @@ -1,4 +1,5 @@ -use crate::dbs::cl::{KeyTimestamp, Timestamp}; +//! Stores a heartbeat per registered cluster node +use crate::dbs::node::{KeyTimestamp, Timestamp}; use derive::Key; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -29,7 +30,7 @@ impl Hb { } pub fn prefix() -> Vec { - let mut k = super::kv::new().encode().unwrap(); + let mut k = crate::key::root::all::new().encode().unwrap(); k.extend_from_slice(&[b'!', b'h', b'b', 0x00]); k } @@ -40,7 +41,7 @@ impl Hb { let tskey: KeyTimestamp = KeyTimestamp { value: ts.value + 1, }; - let mut k = super::kv::new().encode().unwrap(); + let mut k = crate::key::root::all::new().encode().unwrap(); k.extend_from_slice(&[b'!', b'h', b'b']); k.extend_from_slice(tskey.encode().unwrap().as_ref()); k @@ -60,14 +61,14 @@ mod tests { fn key() { use super::*; #[rustfmt::skip] - let val = Hb::new( + let val = Hb::new( Timestamp { value: 123 }, Uuid::from_bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) ); let enc = Hb::encode(&val).unwrap(); assert_eq!( enc, - b"/!hb\x00\x00\x00\x00\x00\x00\x00\x7b/\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"); + b"/!hb\x00\x00\x00\x00\x00\x00\x00\x7b/\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"); let dec = Hb::decode(&enc).unwrap(); assert_eq!(val, dec); } diff --git a/lib/src/key/root/mod.rs b/lib/src/key/root/mod.rs new file mode 100644 index 00000000..72204b5f --- /dev/null +++ b/lib/src/key/root/mod.rs @@ -0,0 +1,4 @@ +pub mod all; +pub mod hb; +pub mod nd; +pub mod ns; diff --git a/lib/src/key/cl.rs b/lib/src/key/root/nd.rs similarity index 54% rename from lib/src/key/cl.rs rename to lib/src/key/root/nd.rs index 483ef270..301dccf5 100644 --- a/lib/src/key/cl.rs +++ b/lib/src/key/root/nd.rs @@ -1,3 +1,4 @@ +//! Stores cluster membership information use derive::Key; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -5,7 +6,7 @@ use uuid::Uuid; // Represents cluster information. // In the future, this could also include broadcast addresses and other information. #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Cl { +pub struct Nd { __: u8, _a: u8, _b: u8, @@ -14,26 +15,26 @@ pub struct Cl { pub nd: Uuid, } -impl Cl { +impl Nd { pub fn new(nd: Uuid) -> Self { Self { __: b'/', _a: b'!', - _b: b'c', - _c: b'l', + _b: b'n', + _c: b'd', nd, } } pub fn prefix() -> Vec { - let mut k = super::kv::new().encode().unwrap(); - k.extend_from_slice(&[b'!', b'c', b'l', 0x00]); + let mut k = crate::key::root::all::new().encode().unwrap(); + k.extend_from_slice(&[b'!', b'n', b'd', 0x00]); k } pub fn suffix() -> Vec { - let mut k = super::kv::new().encode().unwrap(); - k.extend_from_slice(&[b'!', b'c', b'l', 0xff]); + let mut k = crate::key::root::all::new().encode().unwrap(); + k.extend_from_slice(&[b'!', b'n', b'd', 0xff]); k } } @@ -43,21 +44,21 @@ mod tests { #[test] fn key() { use super::*; - let val = Cl::new(Uuid::default()); - let enc = Cl::encode(&val).unwrap(); - let dec = Cl::decode(&enc).unwrap(); + let val = Nd::new(Uuid::default()); + let enc = Nd::encode(&val).unwrap(); + let dec = Nd::decode(&enc).unwrap(); assert_eq!(val, dec); } #[test] fn test_prefix() { - let val = super::Cl::prefix(); - assert_eq!(val, b"/!cl\0") + let val = super::Nd::prefix(); + assert_eq!(val, b"/!nd\0") } #[test] fn test_suffix() { - let val = super::Cl::suffix(); - assert_eq!(val, b"/!cl\xff") + let val = super::Nd::suffix(); + assert_eq!(val, b"/!nd\xff") } } diff --git a/lib/src/key/ns.rs b/lib/src/key/root/ns.rs similarity index 78% rename from lib/src/key/ns.rs rename to lib/src/key/root/ns.rs index 50ade88d..8c7b9287 100644 --- a/lib/src/key/ns.rs +++ b/lib/src/key/root/ns.rs @@ -1,3 +1,4 @@ +//! Stores a DEFINE NAMESPACE config definition use derive::Key; use serde::{Deserialize, Serialize}; @@ -15,13 +16,13 @@ pub fn new(ns: &str) -> Ns<'_> { } pub fn prefix() -> Vec { - let mut k = super::kv::new().encode().unwrap(); + let mut k = super::all::new().encode().unwrap(); k.extend_from_slice(&[b'!', b'n', b's', 0x00]); k } pub fn suffix() -> Vec { - let mut k = super::kv::new().encode().unwrap(); + let mut k = super::all::new().encode().unwrap(); k.extend_from_slice(&[b'!', b'n', b's', 0xff]); k } @@ -44,9 +45,9 @@ mod tests { fn key() { use super::*; #[rustfmt::skip] - let val = Ns::new( - "testns", - ); + let val = Ns::new( + "testns", + ); let enc = Ns::encode(&val).unwrap(); assert_eq!(enc, b"/!nstestns\0"); diff --git a/lib/src/key/scope.rs b/lib/src/key/scope/all.rs similarity index 91% rename from lib/src/key/scope.rs rename to lib/src/key/scope/all.rs index 46943ce7..51eea22a 100644 --- a/lib/src/key/scope.rs +++ b/lib/src/key/scope/all.rs @@ -1,4 +1,4 @@ -use crate::key::CHAR_PATH; +//! Stores the key prefix for all keys under a scope use derive::Key; use serde::{Deserialize, Serialize}; @@ -25,7 +25,7 @@ impl<'a> Scope<'a> { ns, _b: b'*', db, - _c: CHAR_PATH, + _c: super::CHAR, sc, } } diff --git a/lib/src/key/scope/mod.rs b/lib/src/key/scope/mod.rs new file mode 100644 index 00000000..4172cf6a --- /dev/null +++ b/lib/src/key/scope/mod.rs @@ -0,0 +1,4 @@ +pub mod all; +pub mod tk; + +pub(self) const CHAR: u8 = 0xb1; // ± diff --git a/lib/src/key/st.rs b/lib/src/key/scope/tk.rs similarity index 59% rename from lib/src/key/st.rs rename to lib/src/key/scope/tk.rs index f7548773..40836b39 100644 --- a/lib/src/key/st.rs +++ b/lib/src/key/scope/tk.rs @@ -1,9 +1,9 @@ -use crate::key::CHAR_PATH; +//! Stores a DEFINE TOKEN ON SCOPE config definition use derive::Key; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct St<'a> { +pub struct Tk<'a> { __: u8, _a: u8, pub ns: &'a str, @@ -17,23 +17,23 @@ pub struct St<'a> { pub tk: &'a str, } -pub fn new<'a>(ns: &'a str, db: &'a str, sc: &'a str, tk: &'a str) -> St<'a> { - St::new(ns, db, sc, tk) +pub fn new<'a>(ns: &'a str, db: &'a str, sc: &'a str, tk: &'a str) -> Tk<'a> { + Tk::new(ns, db, sc, tk) } pub fn prefix(ns: &str, db: &str, sc: &str) -> Vec { - let mut k = super::scope::new(ns, db, sc).encode().unwrap(); - k.extend_from_slice(&[b'!', b's', b't', 0x00]); + let mut k = super::all::new(ns, db, sc).encode().unwrap(); + k.extend_from_slice(&[b'!', b't', b'k', 0x00]); k } pub fn suffix(ns: &str, db: &str, sc: &str) -> Vec { - let mut k = super::scope::new(ns, db, sc).encode().unwrap(); - k.extend_from_slice(&[b'!', b's', b't', 0xff]); + let mut k = super::all::new(ns, db, sc).encode().unwrap(); + k.extend_from_slice(&[b'!', b't', b'k', 0xff]); k } -impl<'a> St<'a> { +impl<'a> Tk<'a> { pub fn new(ns: &'a str, db: &'a str, sc: &'a str, tk: &'a str) -> Self { Self { __: b'/', @@ -41,11 +41,11 @@ impl<'a> St<'a> { ns, _b: b'*', db, - _c: CHAR_PATH, + _c: super::CHAR, sc, _d: b'!', - _e: b's', - _f: b't', + _e: b't', + _f: b'k', tk, } } @@ -57,16 +57,16 @@ mod tests { fn key() { use super::*; #[rustfmt::skip] - let val = St::new( + let val = Tk::new( "testns", "testdb", "testsc", "testtk", ); - let enc = St::encode(&val).unwrap(); - assert_eq!(enc, b"/*testns\0*testdb\0\xb1testsc\0!sttesttk\0"); + let enc = Tk::encode(&val).unwrap(); + assert_eq!(enc, b"/*testns\0*testdb\0\xb1testsc\0!tktesttk\0"); - let dec = St::decode(&enc).unwrap(); + let dec = Tk::decode(&enc).unwrap(); assert_eq!(val, dec); } } diff --git a/lib/src/key/table.rs b/lib/src/key/table/all.rs similarity index 94% rename from lib/src/key/table.rs rename to lib/src/key/table/all.rs index d6fa014a..8040a5ed 100644 --- a/lib/src/key/table.rs +++ b/lib/src/key/table/all.rs @@ -1,3 +1,4 @@ +//! Stores the key prefix for all keys under a table use derive::Key; use serde::{Deserialize, Serialize}; diff --git a/lib/src/key/ev.rs b/lib/src/key/table/ev.rs similarity index 90% rename from lib/src/key/ev.rs rename to lib/src/key/table/ev.rs index 1f7c3fc2..6c119256 100644 --- a/lib/src/key/ev.rs +++ b/lib/src/key/table/ev.rs @@ -1,3 +1,4 @@ +/// Stores a DEFINE EVENT config definition use derive::Key; use serde::{Deserialize, Serialize}; @@ -21,13 +22,13 @@ pub fn new<'a>(ns: &'a str, db: &'a str, tb: &'a str, ev: &'a str) -> Ev<'a> { } pub fn prefix(ns: &str, db: &str, tb: &str) -> Vec { - let mut k = super::table::new(ns, db, tb).encode().unwrap(); + let mut k = super::all::new(ns, db, tb).encode().unwrap(); k.extend_from_slice(&[b'!', b'e', b'v', 0x00]); k } pub fn suffix(ns: &str, db: &str, tb: &str) -> Vec { - let mut k = super::table::new(ns, db, tb).encode().unwrap(); + let mut k = super::all::new(ns, db, tb).encode().unwrap(); k.extend_from_slice(&[b'!', b'e', b'v', 0xff]); k } diff --git a/lib/src/key/fd.rs b/lib/src/key/table/fd.rs similarity index 90% rename from lib/src/key/fd.rs rename to lib/src/key/table/fd.rs index 32fb3963..e545b722 100644 --- a/lib/src/key/fd.rs +++ b/lib/src/key/table/fd.rs @@ -1,3 +1,4 @@ +/// Stores a DEFINE FIELD config definition use derive::Key; use serde::{Deserialize, Serialize}; @@ -21,13 +22,13 @@ pub fn new<'a>(ns: &'a str, db: &'a str, tb: &'a str, fd: &'a str) -> Fd<'a> { } pub fn prefix(ns: &str, db: &str, tb: &str) -> Vec { - let mut k = super::table::new(ns, db, tb).encode().unwrap(); + let mut k = super::all::new(ns, db, tb).encode().unwrap(); k.extend_from_slice(&[b'!', b'f', b'd', 0x00]); k } pub fn suffix(ns: &str, db: &str, tb: &str) -> Vec { - let mut k = super::table::new(ns, db, tb).encode().unwrap(); + let mut k = super::all::new(ns, db, tb).encode().unwrap(); k.extend_from_slice(&[b'!', b'f', b'd', 0xff]); k } diff --git a/lib/src/key/ft.rs b/lib/src/key/table/ft.rs similarity index 89% rename from lib/src/key/ft.rs rename to lib/src/key/table/ft.rs index 12b8c0b1..cdda0e39 100644 --- a/lib/src/key/ft.rs +++ b/lib/src/key/table/ft.rs @@ -1,3 +1,4 @@ +/// Stores a DEFINE TABLE AS config definition use derive::Key; use serde::{Deserialize, Serialize}; @@ -21,13 +22,13 @@ pub fn new<'a>(ns: &'a str, db: &'a str, tb: &'a str, ft: &'a str) -> Ft<'a> { } pub fn prefix(ns: &str, db: &str, tb: &str) -> Vec { - let mut k = super::table::new(ns, db, tb).encode().unwrap(); + let mut k = super::all::new(ns, db, tb).encode().unwrap(); k.extend_from_slice(&[b'!', b'f', b't', 0x00]); k } pub fn suffix(ns: &str, db: &str, tb: &str) -> Vec { - let mut k = super::table::new(ns, db, tb).encode().unwrap(); + let mut k = super::all::new(ns, db, tb).encode().unwrap(); k.extend_from_slice(&[b'!', b'f', b't', 0xff]); k } diff --git a/lib/src/key/ix.rs b/lib/src/key/table/ix.rs similarity index 87% rename from lib/src/key/ix.rs rename to lib/src/key/table/ix.rs index 5119f06f..a01d516c 100644 --- a/lib/src/key/ix.rs +++ b/lib/src/key/table/ix.rs @@ -1,3 +1,4 @@ +/// Stores a DEFINE INDEX config definition use derive::Key; use serde::{Deserialize, Serialize}; @@ -21,13 +22,13 @@ pub fn new<'a>(ns: &'a str, db: &'a str, tb: &'a str, ix: &'a str) -> Ix<'a> { } pub fn prefix(ns: &str, db: &str, tb: &str) -> Vec { - let mut k = super::table::new(ns, db, tb).encode().unwrap(); + let mut k = super::all::new(ns, db, tb).encode().unwrap(); k.extend_from_slice(&[b'!', b'i', b'x', 0x00]); k } pub fn suffix(ns: &str, db: &str, tb: &str) -> Vec { - let mut k = super::table::new(ns, db, tb).encode().unwrap(); + let mut k = super::all::new(ns, db, tb).encode().unwrap(); k.extend_from_slice(&[b'!', b'i', b'x', 0xff]); k } diff --git a/lib/src/key/lv.rs b/lib/src/key/table/lq.rs similarity index 56% rename from lib/src/key/lv.rs rename to lib/src/key/table/lq.rs index cb52f6ec..4e9babca 100644 --- a/lib/src/key/lv.rs +++ b/lib/src/key/table/lq.rs @@ -1,9 +1,10 @@ +//! Stores a LIVE SELECT query definition on the table use derive::Key; use serde::{Deserialize, Serialize}; use uuid::Uuid; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Key)] -pub struct Lv<'a> { +pub struct Lq<'a> { __: u8, _a: u8, pub ns: &'a str, @@ -15,27 +16,27 @@ pub struct Lv<'a> { _e: u8, _f: u8, #[serde(with = "uuid::serde::compact")] - pub lv: Uuid, + pub lq: Uuid, } -pub fn new<'a>(ns: &'a str, db: &'a str, tb: &'a str, lv: Uuid) -> Lv<'a> { - Lv::new(ns, db, tb, lv) +pub fn new<'a>(ns: &'a str, db: &'a str, tb: &'a str, lq: Uuid) -> Lq<'a> { + Lq::new(ns, db, tb, lq) } pub fn prefix(ns: &str, db: &str, tb: &str) -> Vec { - let mut k = super::table::new(ns, db, tb).encode().unwrap(); - k.extend_from_slice(&[b'!', b'l', b'v', 0x00]); + let mut k = super::all::new(ns, db, tb).encode().unwrap(); + k.extend_from_slice(&[b'!', b'l', b'q', 0x00]); k } pub fn suffix(ns: &str, db: &str, tb: &str) -> Vec { - let mut k = super::table::new(ns, db, tb).encode().unwrap(); - k.extend_from_slice(&[b'!', b'l', b'v', 0xff]); + let mut k = super::all::new(ns, db, tb).encode().unwrap(); + k.extend_from_slice(&[b'!', b'l', b'q', 0xff]); k } -impl<'a> Lv<'a> { - pub fn new(ns: &'a str, db: &'a str, tb: &'a str, lv: Uuid) -> Self { +impl<'a> Lq<'a> { + pub fn new(ns: &'a str, db: &'a str, tb: &'a str, lq: Uuid) -> Self { Self { __: b'/', _a: b'*', @@ -46,8 +47,8 @@ impl<'a> Lv<'a> { tb, _d: b'!', _e: b'l', - _f: b'v', - lv, + _f: b'q', + lq, } } } @@ -61,27 +62,27 @@ mod tests { use super::*; #[rustfmt::skip] let live_query_id = Uuid::from_bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); - let val = Lv::new("testns", "testdb", "testtb", live_query_id); - let enc = Lv::encode(&val).unwrap(); + let val = Lq::new("testns", "testdb", "testtb", live_query_id); + let enc = Lq::encode(&val).unwrap(); println!("{:?}", debug::sprint_key(&enc)); assert_eq!( enc, - b"/*testns\x00*testdb\x00*testtb\x00!lv\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10" + b"/*testns\x00*testdb\x00*testtb\x00!lq\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10" ); - let dec = Lv::decode(&enc).unwrap(); + let dec = Lq::decode(&enc).unwrap(); assert_eq!(val, dec); } #[test] fn prefix() { let val = super::prefix("testns", "testdb", "testtb"); - assert_eq!(val, b"/*testns\x00*testdb\x00*testtb\x00!lv\x00") + assert_eq!(val, b"/*testns\x00*testdb\x00*testtb\x00!lq\x00") } #[test] fn suffix() { let val = super::suffix("testns", "testdb", "testtb"); - assert_eq!(val, b"/*testns\x00*testdb\x00*testtb\x00!lv\xff") + assert_eq!(val, b"/*testns\x00*testdb\x00*testtb\x00!lq\xff") } } diff --git a/lib/src/key/table/mod.rs b/lib/src/key/table/mod.rs new file mode 100644 index 00000000..7c690978 --- /dev/null +++ b/lib/src/key/table/mod.rs @@ -0,0 +1,6 @@ +pub mod all; +pub mod ev; +pub mod fd; +pub mod ft; +pub mod ix; +pub mod lq; diff --git a/lib/src/key/thing.rs b/lib/src/key/thing/mod.rs similarity index 92% rename from lib/src/key/thing.rs rename to lib/src/key/thing/mod.rs index 0edf3daf..fc307fa5 100644 --- a/lib/src/key/thing.rs +++ b/lib/src/key/thing/mod.rs @@ -1,3 +1,4 @@ +//! Stores a record document use crate::sql::id::Id; use derive::Key; use serde::{Deserialize, Serialize}; @@ -20,13 +21,13 @@ pub fn new<'a>(ns: &'a str, db: &'a str, tb: &'a str, id: &Id) -> Thing<'a> { } pub fn prefix(ns: &str, db: &str, tb: &str) -> Vec { - let mut k = super::table::new(ns, db, tb).encode().unwrap(); + let mut k = crate::key::table::all::new(ns, db, tb).encode().unwrap(); k.extend_from_slice(&[b'*', 0x00]); k } pub fn suffix(ns: &str, db: &str, tb: &str) -> Vec { - let mut k = super::table::new(ns, db, tb).encode().unwrap(); + let mut k = crate::key::table::all::new(ns, db, tb).encode().unwrap(); k.extend_from_slice(&[b'*', 0xff]); k } diff --git a/lib/src/kvs/ds.rs b/lib/src/kvs/ds.rs index 34ba15e3..59638b3d 100644 --- a/lib/src/kvs/ds.rs +++ b/lib/src/kvs/ds.rs @@ -1,5 +1,5 @@ use crate::ctx::Context; -use crate::dbs::cl::Timestamp; +use crate::dbs::node::Timestamp; use crate::dbs::Attach; use crate::dbs::Executor; use crate::dbs::Notification; @@ -8,9 +8,7 @@ use crate::dbs::Response; use crate::dbs::Session; use crate::dbs::Variables; use crate::err::Error; -use crate::key::hb::Hb; -use crate::key::lq; -use crate::key::lv::Lv; +use crate::key::root::hb::Hb; use crate::sql; use crate::sql::Query; use crate::sql::Value; @@ -30,7 +28,7 @@ use super::tx::Transaction; /// Not a stored struct; Used only in this module #[derive(Debug, Clone, Eq, PartialEq)] pub struct LqValue { - pub cl: Uuid, + pub nd: Uuid, pub ns: String, pub db: String, pub tb: String, @@ -324,7 +322,7 @@ impl Datastore { node_id: &Uuid, timestamp: &Timestamp, ) -> Result<(), Error> { - tx.set_cl(*node_id).await?; + tx.set_nd(*node_id).await?; tx.set_hb(timestamp.clone(), *node_id).await?; Ok(()) } @@ -340,7 +338,7 @@ impl Datastore { let mut nodes = vec![]; for hb in hbs { trace!("Deleting node {}", &hb.nd); - tx.del_cl(hb.nd).await?; + tx.del_nd(hb.nd).await?; nodes.push(hb.nd); } Ok(nodes) @@ -366,7 +364,7 @@ impl Datastore { trace!("Found {} LQ entries for {:?}", node_lqs.len(), nd); for lq in node_lqs { trace!("Archiving query {:?}", &lq); - let node_archived_lqs = self.archive_lv_for_node(tx, &lq.cl, this_node_id).await?; + let node_archived_lqs = self.archive_lv_for_node(tx, &lq.nd, this_node_id).await?; for lq_value in node_archived_lqs { archived.push(lq_value); } @@ -382,9 +380,11 @@ impl Datastore { ) -> Result<(), Error> { for lq in archived { // Delete the cluster key, used for finding LQ associated with a node - tx.del(lq::new(lq.cl, lq.ns.as_str(), lq.db.as_str(), lq.lq)).await?; + let key = crate::key::node::lq::new(lq.nd, &lq.ns, &lq.db, lq.lq); + tx.del(key).await?; // Delete the table key, used for finding LQ associated with a table - tx.del(Lv::new(lq.ns.as_str(), lq.db.as_str(), lq.tb.as_str(), lq.lq)).await?; + let key = crate::key::table::lq::new(&lq.ns, &lq.db, &lq.tb, lq.lq); + tx.del(key).await?; } Ok(()) } @@ -402,7 +402,7 @@ impl Datastore { let mut archived: Vec = vec![]; for hb in dead_heartbeats { let new_archived = self.archive_lv_for_node(tx, &hb.nd, this_node_id).await?; - tx.del_cl(hb.nd).await?; + tx.del_nd(hb.nd).await?; trace!("Deleted node {}", hb.nd); for lq_value in new_archived { archived.push(lq_value); @@ -441,7 +441,7 @@ impl Datastore { let dead = tx.scan_hb(ts, limit).await?; tx.delr_hb(dead.clone(), 1000).await?; for dead_node in dead.clone() { - tx.del_cl(dead_node.nd).await?; + tx.del_nd(dead_node.nd).await?; } Ok::, Error>(dead) } diff --git a/lib/src/kvs/tests/helper.rs b/lib/src/kvs/tests/helper.rs index 0fa902c1..ba0769e7 100644 --- a/lib/src/kvs/tests/helper.rs +++ b/lib/src/kvs/tests/helper.rs @@ -1,4 +1,4 @@ -use crate::dbs::cl::Timestamp; +use crate::dbs::node::Timestamp; use crate::err::Error; pub struct TestContext { diff --git a/lib/src/kvs/tests/lq.rs b/lib/src/kvs/tests/lq.rs index 178d53d2..6758e303 100644 --- a/lib/src/kvs/tests/lq.rs +++ b/lib/src/kvs/tests/lq.rs @@ -15,7 +15,7 @@ async fn scan_node_lq() { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, ]); - let key = crate::key::lq::new(node_id, namespace, database, live_query_id); + let key = crate::key::node::lq::new(node_id, namespace, database, live_query_id); trace!( "Inserting key: {}", key.encode() @@ -32,7 +32,7 @@ async fn scan_node_lq() { let res = tx.scan_lq(&node_id, 100).await.unwrap(); assert_eq!(res.len(), 1); for val in res { - assert_eq!(val.cl, node_id); + assert_eq!(val.nd, node_id); assert_eq!(val.ns, namespace); assert_eq!(val.db, database); assert_eq!(val.lq, live_query_id); diff --git a/lib/src/kvs/tests/mod.rs b/lib/src/kvs/tests/mod.rs index deeb16e9..fda13454 100644 --- a/lib/src/kvs/tests/mod.rs +++ b/lib/src/kvs/tests/mod.rs @@ -16,7 +16,7 @@ mod mem { include!("helper.rs"); include!("cluster_init.rs"); include!("lq.rs"); - include!("lv.rs"); + include!("nq.rs"); include!("raw.rs"); include!("snapshot.rs"); include!("tb.rs"); @@ -43,7 +43,7 @@ mod rocksdb { include!("helper.rs"); include!("cluster_init.rs"); include!("lq.rs"); - include!("lv.rs"); + include!("nq.rs"); include!("raw.rs"); include!("snapshot.rs"); include!("tb.rs"); @@ -72,7 +72,7 @@ mod speedb { include!("helper.rs"); include!("cluster_init.rs"); include!("lq.rs"); - include!("lv.rs"); + include!("nq.rs"); include!("raw.rs"); include!("snapshot.rs"); include!("tb.rs"); @@ -105,7 +105,7 @@ mod tikv { include!("cluster_init.rs"); include!("helper.rs"); include!("lq.rs"); - include!("lv.rs"); + include!("nq.rs"); include!("raw.rs"); include!("snapshot.rs"); include!("tb.rs"); @@ -138,7 +138,7 @@ mod fdb { include!("cluster_init.rs"); include!("helper.rs"); include!("lq.rs"); - include!("lv.rs"); + include!("nq.rs"); include!("raw.rs"); include!("snapshot.rs"); include!("tb.rs"); diff --git a/lib/src/kvs/tests/lv.rs b/lib/src/kvs/tests/nq.rs similarity index 92% rename from lib/src/kvs/tests/lv.rs rename to lib/src/kvs/tests/nq.rs index 0d57a14f..544fb3f8 100644 --- a/lib/src/kvs/tests/lv.rs +++ b/lib/src/kvs/tests/nq.rs @@ -12,14 +12,14 @@ async fn archive_lv_for_node_archives() { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, ]); - tx.set_cl(node_id).await.unwrap(); + tx.set_nd(node_id).await.unwrap(); let lv_id = Uuid::from_bytes([ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, ]); - let key = crate::key::lq::new(node_id, namespace, database, lv_id); + let key = crate::key::node::lq::new(node_id, namespace, database, lv_id); tx.putc(key, table, None).await.unwrap(); let (_, mut stm) = live(format!("LIVE SELECT * FROM {}", table).as_str()).unwrap(); @@ -37,7 +37,7 @@ async fn archive_lv_for_node_archives() { let mut tx = test.db.transaction(true, false).await.unwrap(); let results = test.db.archive_lv_for_node(&mut tx, &node_id, &this_node_id).await.unwrap(); assert_eq!(results.len(), 1); - assert_eq!(results[0].cl, node_id); + assert_eq!(results[0].nd, node_id); assert_eq!(results[0].ns, namespace); assert_eq!(results[0].db, database); assert_eq!(results[0].tb, table); diff --git a/lib/src/kvs/tests/tb.rs b/lib/src/kvs/tests/tb.rs index 91c51f50..9006c2e4 100644 --- a/lib/src/kvs/tests/tb.rs +++ b/lib/src/kvs/tests/tb.rs @@ -1,5 +1,5 @@ -use crate::key::tb; -use crate::key::tb::Tb; +use crate::key::database::tb; +use crate::key::database::tb::Tb; use crate::sql::statements::DefineTableStatement; #[tokio::test] diff --git a/lib/src/kvs/tx.rs b/lib/src/kvs/tx.rs index e461fc8b..c0113091 100644 --- a/lib/src/kvs/tx.rs +++ b/lib/src/kvs/tx.rs @@ -2,13 +2,9 @@ use super::kv::Add; use super::kv::Convert; use super::Key; use super::Val; -use crate::dbs::cl::ClusterMembership; -use crate::dbs::cl::Timestamp; +use crate::dbs::node::ClusterMembership; +use crate::dbs::node::Timestamp; use crate::err::Error; -use crate::key::hb::Hb; -use crate::key::lq::Lq; -use crate::key::lv::Lv; -use crate::key::{lq, thing}; use crate::kvs::cache::Cache; use crate::kvs::cache::Entry; use crate::kvs::LqValue; @@ -798,9 +794,9 @@ impl Transaction { // Register cluster membership // NOTE: Setting cluster membership sets the heartbeat // Remember to set the heartbeat as well - pub async fn set_cl(&mut self, id: Uuid) -> Result<(), Error> { - let key = crate::key::cl::Cl::new(id); - match self.get_cl(id).await? { + pub async fn set_nd(&mut self, id: Uuid) -> Result<(), Error> { + let key = crate::key::root::nd::Nd::new(id); + match self.get_nd(id).await? { Some(_) => Err(Error::ClAlreadyExists { value: id.to_string(), }), @@ -816,8 +812,8 @@ impl Transaction { } // Retrieve cluster information - pub async fn get_cl(&mut self, id: Uuid) -> Result, Error> { - let key = crate::key::cl::Cl::new(id); + pub async fn get_nd(&mut self, id: Uuid) -> Result, Error> { + let key = crate::key::root::nd::Nd::new(id); let val = self.get(key).await?; match val { Some(v) => Ok(Some::(v.into())), @@ -835,7 +831,7 @@ impl Transaction { // Set heartbeat pub async fn set_hb(&mut self, timestamp: Timestamp, id: Uuid) -> Result<(), Error> { - let key = Hb::new(timestamp.clone(), id); + let key = crate::key::root::hb::Hb::new(timestamp.clone(), id); // We do not need to do a read, we always want to overwrite self.put( key, @@ -849,30 +845,34 @@ impl Transaction { } // Delete a cluster registration entry - pub async fn del_cl(&mut self, node: uuid::Uuid) -> Result<(), Error> { - let key = crate::key::cl::Cl::new(node); + pub async fn del_nd(&mut self, node: Uuid) -> Result<(), Error> { + let key = crate::key::root::nd::Nd::new(node); self.del(key).await } // Delete the live query notification registry on the table // Return the Table ID - pub async fn del_cllv(&mut self, cl: &Uuid) -> Result { - // This isn't implemented because it is covered by del_cl + pub async fn del_ndlv(&mut self, nd: &Uuid) -> Result { + // This isn't implemented because it is covered by del_nd // Will add later for remote node kill - Err(Error::ClNotFound { - value: format!("Missing cluster {:?}", cl), + Err(Error::NdNotFound { + value: format!("Missing cluster node {:?}", nd), }) } // Scans up until the heartbeat timestamp and returns the discovered nodes - pub async fn scan_hb(&mut self, time_to: &Timestamp, limit: u32) -> Result, Error> { - let beg = crate::key::hb::Hb::prefix(); - let end = crate::key::hb::Hb::suffix(time_to); + pub async fn scan_hb( + &mut self, + time_to: &Timestamp, + limit: u32, + ) -> Result, Error> { + let beg = crate::key::root::hb::Hb::prefix(); + let end = crate::key::root::hb::Hb::suffix(time_to); trace!("Scan start: {} ({:?})", String::from_utf8_lossy(&beg).to_string(), &beg); trace!("Scan end: {} ({:?})", String::from_utf8_lossy(&end).to_string(), &end); let mut nxt: Option = None; let mut num = limit; - let mut out: Vec = vec![]; + let mut out: Vec = vec![]; // Start processing while num > 0 { // Get records batch @@ -903,7 +903,7 @@ impl Transaction { if n == i + 1 { nxt = Some(k.clone()); } - out.push(Hb::decode(k.as_slice())?); + out.push(crate::key::root::hb::Hb::decode(k.as_slice())?); // Count num -= 1; } @@ -913,8 +913,8 @@ impl Transaction { } pub async fn scan_cl(&mut self, limit: u32) -> Result, Error> { - let beg = crate::key::cl::Cl::prefix(); - let end = crate::key::cl::Cl::suffix(); + let beg = crate::key::root::nd::Nd::prefix(); + let end = crate::key::root::nd::Nd::suffix(); trace!("Scan start: {} ({:?})", String::from_utf8_lossy(&beg).to_string(), &beg); trace!("Scan end: {} ({:?})", String::from_utf8_lossy(&end).to_string(), &end); let mut nxt: Option = None; @@ -959,7 +959,11 @@ impl Transaction { Ok(out) } - pub async fn delr_hb(&mut self, ts: Vec, limit: u32) -> Result<(), Error> { + pub async fn delr_hb( + &mut self, + ts: Vec, + limit: u32, + ) -> Result<(), Error> { trace!("delr_hb: ts={:?} limit={:?}", ts, limit); for hb in ts.into_iter() { self.del(hb).await?; @@ -969,18 +973,14 @@ impl Transaction { pub async fn del_lv(&mut self, ns: &str, db: &str, tb: &str, lv: Uuid) -> Result<(), Error> { trace!("del_lv: ns={:?} db={:?} tb={:?} lv={:?}", ns, db, tb, lv); - let key = crate::key::lv::new(ns, db, tb, lv); + let key = crate::key::table::lq::new(ns, db, tb, lv); self.cache.del(&key.clone().into()); self.del(key).await } - pub async fn scan_lq<'a>( - &mut self, - node: &uuid::Uuid, - limit: u32, - ) -> Result, Error> { - let pref = lq::prefix_nd(node); - let suff = lq::suffix_nd(node); + pub async fn scan_lq<'a>(&mut self, node: &Uuid, limit: u32) -> Result, Error> { + let pref = crate::key::node::lq::prefix_nd(node); + let suff = crate::key::node::lq::suffix_nd(node); trace!( "Scanning range from pref={}, suff={}", crate::key::debug::sprint_key(&pref), @@ -991,10 +991,10 @@ impl Transaction { let mut res: Vec = vec![]; for (key, value) in scanned { trace!("scan_lq: key={:?} value={:?}", &key, &value); - let lq = Lq::decode(key.as_slice())?; + let lq = crate::key::node::lq::Lq::decode(key.as_slice())?; let tb: String = String::from_utf8(value).unwrap(); res.push(LqValue { - cl: lq.nd, + nd: lq.nd, ns: lq.ns.to_string(), db: lq.db.to_string(), tb, @@ -1012,15 +1012,15 @@ impl Transaction { live_stm: LiveStatement, expected: Option, ) -> Result<(), Error> { - let key = crate::key::lv::new(ns, db, tb, live_stm.id.0); - let key_enc = Lv::encode(&key)?; + let key = crate::key::table::lq::new(ns, db, tb, live_stm.id.0); + let key_enc = crate::key::table::lq::Lq::encode(&key)?; trace!("putc_lv ({:?}): key={:?}", &live_stm.id, crate::key::debug::sprint_key(&key_enc)); self.putc(key_enc, live_stm, expected).await } /// Retrieve all namespace definitions in a datastore. pub async fn all_ns(&mut self) -> Result, Error> { - let key = crate::key::ns::prefix(); + let key = crate::key::root::ns::prefix(); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Nss(v) = e { v @@ -1028,8 +1028,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::ns::prefix(); - let end = crate::key::ns::suffix(); + let beg = crate::key::root::ns::prefix(); + let end = crate::key::root::ns::suffix(); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Nss(Arc::clone(&val))); @@ -1039,7 +1039,7 @@ impl Transaction { /// Retrieve all namespace login definitions for a specific namespace. pub async fn all_nl(&mut self, ns: &str) -> Result, Error> { - let key = crate::key::nl::prefix(ns); + let key = crate::key::namespace::lg::prefix(ns); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Nls(v) = e { v @@ -1047,8 +1047,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::nl::prefix(ns); - let end = crate::key::nl::suffix(ns); + let beg = crate::key::namespace::lg::prefix(ns); + let end = crate::key::namespace::lg::suffix(ns); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Nls(Arc::clone(&val))); @@ -1058,7 +1058,7 @@ impl Transaction { /// Retrieve all namespace token definitions for a specific namespace. pub async fn all_nt(&mut self, ns: &str) -> Result, Error> { - let key = crate::key::nt::prefix(ns); + let key = crate::key::namespace::tk::prefix(ns); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Nts(v) = e { v @@ -1066,8 +1066,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::nt::prefix(ns); - let end = crate::key::nt::suffix(ns); + let beg = crate::key::namespace::tk::prefix(ns); + let end = crate::key::namespace::tk::suffix(ns); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Nts(Arc::clone(&val))); @@ -1077,7 +1077,7 @@ impl Transaction { /// Retrieve all database definitions for a specific namespace. pub async fn all_db(&mut self, ns: &str) -> Result, Error> { - let key = crate::key::db::prefix(ns); + let key = crate::key::namespace::db::prefix(ns); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Dbs(v) = e { v @@ -1085,8 +1085,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::db::prefix(ns); - let end = crate::key::db::suffix(ns); + let beg = crate::key::namespace::db::prefix(ns); + let end = crate::key::namespace::db::suffix(ns); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Dbs(Arc::clone(&val))); @@ -1100,7 +1100,7 @@ impl Transaction { ns: &str, db: &str, ) -> Result, Error> { - let key = crate::key::dl::prefix(ns, db); + let key = crate::key::database::lg::prefix(ns, db); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Dls(v) = e { v @@ -1108,8 +1108,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::dl::prefix(ns, db); - let end = crate::key::dl::suffix(ns, db); + let beg = crate::key::database::lg::prefix(ns, db); + let end = crate::key::database::lg::suffix(ns, db); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Dls(Arc::clone(&val))); @@ -1123,7 +1123,7 @@ impl Transaction { ns: &str, db: &str, ) -> Result, Error> { - let key = crate::key::dt::prefix(ns, db); + let key = crate::key::database::tk::prefix(ns, db); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Dts(v) = e { v @@ -1131,8 +1131,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::dt::prefix(ns, db); - let end = crate::key::dt::suffix(ns, db); + let beg = crate::key::database::tk::prefix(ns, db); + let end = crate::key::database::tk::suffix(ns, db); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Dts(Arc::clone(&val))); @@ -1146,7 +1146,7 @@ impl Transaction { ns: &str, db: &str, ) -> Result, Error> { - let key = crate::key::fc::prefix(ns, db); + let key = crate::key::database::fc::prefix(ns, db); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Fcs(v) = e { v @@ -1154,8 +1154,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::fc::prefix(ns, db); - let end = crate::key::fc::suffix(ns, db); + let beg = crate::key::database::fc::prefix(ns, db); + let end = crate::key::database::fc::suffix(ns, db); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Fcs(Arc::clone(&val))); @@ -1169,7 +1169,7 @@ impl Transaction { ns: &str, db: &str, ) -> Result, Error> { - let key = crate::key::sc::prefix(ns, db); + let key = crate::key::database::sc::prefix(ns, db); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Scs(v) = e { v @@ -1177,8 +1177,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::sc::prefix(ns, db); - let end = crate::key::sc::suffix(ns, db); + let beg = crate::key::database::sc::prefix(ns, db); + let end = crate::key::database::sc::suffix(ns, db); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Scs(Arc::clone(&val))); @@ -1193,7 +1193,7 @@ impl Transaction { db: &str, sc: &str, ) -> Result, Error> { - let key = crate::key::st::prefix(ns, db, sc); + let key = crate::key::scope::tk::prefix(ns, db, sc); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Sts(v) = e { v @@ -1201,8 +1201,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::st::prefix(ns, db, sc); - let end = crate::key::st::suffix(ns, db, sc); + let beg = crate::key::scope::tk::prefix(ns, db, sc); + let end = crate::key::scope::tk::suffix(ns, db, sc); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Sts(Arc::clone(&val))); @@ -1216,7 +1216,7 @@ impl Transaction { ns: &str, db: &str, ) -> Result, Error> { - let key = crate::key::pa::prefix(ns, db); + let key = crate::key::database::pa::prefix(ns, db); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Pas(v) = e { v @@ -1224,8 +1224,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::pa::prefix(ns, db); - let end = crate::key::pa::suffix(ns, db); + let beg = crate::key::database::pa::prefix(ns, db); + let end = crate::key::database::pa::suffix(ns, db); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Pas(Arc::clone(&val))); @@ -1239,7 +1239,7 @@ impl Transaction { ns: &str, db: &str, ) -> Result, Error> { - let key = crate::key::tb::prefix(ns, db); + let key = crate::key::database::tb::prefix(ns, db); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Tbs(v) = e { v @@ -1247,8 +1247,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::tb::prefix(ns, db); - let end = crate::key::tb::suffix(ns, db); + let beg = crate::key::database::tb::prefix(ns, db); + let end = crate::key::database::tb::suffix(ns, db); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Tbs(Arc::clone(&val))); @@ -1263,7 +1263,7 @@ impl Transaction { db: &str, tb: &str, ) -> Result, Error> { - let key = crate::key::ev::prefix(ns, db, tb); + let key = crate::key::table::ev::prefix(ns, db, tb); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Evs(v) = e { v @@ -1271,8 +1271,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::ev::prefix(ns, db, tb); - let end = crate::key::ev::suffix(ns, db, tb); + let beg = crate::key::table::ev::prefix(ns, db, tb); + let end = crate::key::table::ev::suffix(ns, db, tb); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Evs(Arc::clone(&val))); @@ -1287,7 +1287,7 @@ impl Transaction { db: &str, tb: &str, ) -> Result, Error> { - let key = crate::key::fd::prefix(ns, db, tb); + let key = crate::key::table::fd::prefix(ns, db, tb); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Fds(v) = e { v @@ -1295,8 +1295,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::fd::prefix(ns, db, tb); - let end = crate::key::fd::suffix(ns, db, tb); + let beg = crate::key::table::fd::prefix(ns, db, tb); + let end = crate::key::table::fd::suffix(ns, db, tb); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Fds(Arc::clone(&val))); @@ -1311,7 +1311,7 @@ impl Transaction { db: &str, tb: &str, ) -> Result, Error> { - let key = crate::key::ix::prefix(ns, db, tb); + let key = crate::key::table::ix::prefix(ns, db, tb); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Ixs(v) = e { v @@ -1319,8 +1319,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::ix::prefix(ns, db, tb); - let end = crate::key::ix::suffix(ns, db, tb); + let beg = crate::key::table::ix::prefix(ns, db, tb); + let end = crate::key::table::ix::suffix(ns, db, tb); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Ixs(Arc::clone(&val))); @@ -1335,7 +1335,7 @@ impl Transaction { db: &str, tb: &str, ) -> Result, Error> { - let key = crate::key::ft::prefix(ns, db, tb); + let key = crate::key::table::ft::prefix(ns, db, tb); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Fts(v) = e { v @@ -1343,8 +1343,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::ft::prefix(ns, db, tb); - let end = crate::key::ft::suffix(ns, db, tb); + let beg = crate::key::table::ft::prefix(ns, db, tb); + let end = crate::key::table::ft::suffix(ns, db, tb); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Fts(Arc::clone(&val))); @@ -1359,7 +1359,7 @@ impl Transaction { db: &str, tb: &str, ) -> Result, Error> { - let key = crate::key::lv::prefix(ns, db, tb); + let key = crate::key::table::lq::prefix(ns, db, tb); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Lvs(v) = e { v @@ -1367,8 +1367,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::lv::prefix(ns, db, tb); - let end = crate::key::lv::suffix(ns, db, tb); + let beg = crate::key::table::lq::prefix(ns, db, tb); + let end = crate::key::table::lq::suffix(ns, db, tb); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Lvs(Arc::clone(&val))); @@ -1377,18 +1377,18 @@ impl Transaction { } pub async fn all_lq(&mut self, nd: &uuid::Uuid) -> Result, Error> { - let beg = crate::key::lq::prefix_nd(nd); - let end = crate::key::lq::suffix_nd(nd); + let beg = crate::key::node::lq::prefix_nd(nd); + let end = crate::key::node::lq::suffix_nd(nd); let lq_pairs = self.getr(beg..end, u32::MAX).await?; let mut lqs = vec![]; for (key, value) in lq_pairs { - let lq_key = Lq::decode(key.as_slice())?; + let lq_key = crate::key::node::lq::Lq::decode(key.as_slice())?; trace!("Value is {:?}", &value); let lq_value = String::from_utf8(value).map_err(|e| { Error::Internal(format!("Failed to decode a value while reading LQ: {}", e)) })?; let lqv = LqValue { - cl: *nd, + nd: *nd, ns: lq_key.ns.to_string(), db: lq_key.db.to_string(), tb: lq_value, @@ -1405,7 +1405,7 @@ impl Transaction { ns: &str, db: &str, ) -> Result, Error> { - let key = crate::key::az::prefix(ns, db); + let key = crate::key::database::az::prefix(ns, db); Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Azs(v) = e { v @@ -1413,8 +1413,8 @@ impl Transaction { unreachable!(); } } else { - let beg = crate::key::az::prefix(ns, db); - let end = crate::key::az::suffix(ns, db); + let beg = crate::key::database::az::prefix(ns, db); + let end = crate::key::database::az::suffix(ns, db); let val = self.getr(beg..end, u32::MAX).await?; let val = val.convert().into(); self.cache.set(key, Entry::Azs(Arc::clone(&val))); @@ -1424,7 +1424,7 @@ impl Transaction { /// Retrieve a specific namespace definition. pub async fn get_ns(&mut self, ns: &str) -> Result { - let key = crate::key::ns::new(ns); + let key = crate::key::root::ns::new(ns); let val = self.get(key).await?.ok_or(Error::NsNotFound { value: ns.to_owned(), })?; @@ -1433,7 +1433,7 @@ impl Transaction { /// Retrieve a specific namespace login definition. pub async fn get_nl(&mut self, ns: &str, nl: &str) -> Result { - let key = crate::key::nl::new(ns, nl); + let key = crate::key::namespace::lg::new(ns, nl); let val = self.get(key).await?.ok_or(Error::NlNotFound { value: nl.to_owned(), })?; @@ -1442,7 +1442,7 @@ impl Transaction { /// Retrieve a specific namespace token definition. pub async fn get_nt(&mut self, ns: &str, nt: &str) -> Result { - let key = crate::key::nt::new(ns, nt); + let key = crate::key::namespace::tk::new(ns, nt); let val = self.get(key).await?.ok_or(Error::NtNotFound { value: nt.to_owned(), })?; @@ -1451,7 +1451,7 @@ impl Transaction { /// Retrieve a specific database definition. pub async fn get_db(&mut self, ns: &str, db: &str) -> Result { - let key = crate::key::db::new(ns, db); + let key = crate::key::namespace::db::new(ns, db); let val = self.get(key).await?.ok_or(Error::DbNotFound { value: db.to_owned(), })?; @@ -1465,7 +1465,7 @@ impl Transaction { db: &str, dl: &str, ) -> Result { - let key = crate::key::dl::new(ns, db, dl); + let key = crate::key::database::lg::new(ns, db, dl); let val = self.get(key).await?.ok_or(Error::DlNotFound { value: dl.to_owned(), })?; @@ -1479,7 +1479,7 @@ impl Transaction { db: &str, dt: &str, ) -> Result { - let key = crate::key::dt::new(ns, db, dt); + let key = crate::key::database::tk::new(ns, db, dt); let val = self.get(key).await?.ok_or(Error::DtNotFound { value: dt.to_owned(), })?; @@ -1493,7 +1493,7 @@ impl Transaction { db: &str, sc: &str, ) -> Result { - let key = crate::key::sc::new(ns, db, sc); + let key = crate::key::database::sc::new(ns, db, sc); let val = self.get(key).await?.ok_or(Error::ScNotFound { value: sc.to_owned(), })?; @@ -1508,7 +1508,7 @@ impl Transaction { sc: &str, st: &str, ) -> Result { - let key = crate::key::st::new(ns, db, sc, st); + let key = crate::key::scope::tk::new(ns, db, sc, st); let val = self.get(key).await?.ok_or(Error::StNotFound { value: st.to_owned(), })?; @@ -1522,7 +1522,7 @@ impl Transaction { db: &str, fc: &str, ) -> Result { - let key = crate::key::fc::new(ns, db, fc); + let key = crate::key::database::fc::new(ns, db, fc); let val = self.get(key).await?.ok_or(Error::FcNotFound { value: fc.to_owned(), })?; @@ -1537,7 +1537,7 @@ impl Transaction { db: &str, lq: Uuid, ) -> Result { - let key = lq::new(nd, ns, db, lq); + let key = crate::key::node::lq::new(nd, ns, db, lq); let val = self.get(key).await?.ok_or(Error::LqNotFound { value: lq.to_string(), })?; @@ -1551,8 +1551,8 @@ impl Transaction { tb: &str, lv: &Uuid, ) -> Result { - let key = crate::key::lv::new(ns, db, tb, *lv); - let key_enc = Lv::encode(&key)?; + let key = crate::key::table::lq::new(ns, db, tb, *lv); + let key_enc = crate::key::table::lq::Lq::encode(&key)?; trace!("Getting lv ({:?}) {:?}", lv, crate::key::debug::sprint_key(&key_enc)); let val = self.get(key_enc).await?.ok_or(Error::LvNotFound { value: lv.to_string(), @@ -1567,7 +1567,7 @@ impl Transaction { db: &str, pa: &str, ) -> Result { - let key = crate::key::pa::new(ns, db, pa); + let key = crate::key::database::pa::new(ns, db, pa); let val = self.get(key).await?.ok_or(Error::PaNotFound { value: pa.to_owned(), })?; @@ -1581,7 +1581,7 @@ impl Transaction { db: &str, tb: &str, ) -> Result { - let key = crate::key::tb::new(ns, db, tb); + let key = crate::key::database::tb::new(ns, db, tb); let val = self.get(key).await?.ok_or(Error::TbNotFound { value: tb.to_owned(), })?; @@ -1594,7 +1594,7 @@ impl Transaction { db: &str, az: &str, ) -> Result { - let key = crate::key::az::new(ns, db, az); + let key = crate::key::database::az::new(ns, db, az); let val = self.get(key).await?.ok_or(Error::AzNotFound { value: az.to_owned(), })?; @@ -1608,7 +1608,7 @@ impl Transaction { tb: &str, ix: &str, ) -> Result { - let key = crate::key::ix::new(ns, db, tb, ix); + let key = crate::key::table::ix::new(ns, db, tb, ix); let val = self.get(key).await?.ok_or(Error::IxNotFound { value: ix.to_owned(), })?; @@ -1625,7 +1625,7 @@ impl Transaction { value, }) => match strict { false => { - let key = crate::key::ns::new(ns); + let key = crate::key::root::ns::new(ns); let val = DefineNamespaceStatement { name: ns.to_owned().into(), }; @@ -1653,7 +1653,7 @@ impl Transaction { value, }) => match strict { false => { - let key = crate::key::db::new(ns, db); + let key = crate::key::namespace::db::new(ns, db); let val = DefineDatabaseStatement { name: db.to_owned().into(), changefeed: None, @@ -1683,7 +1683,7 @@ impl Transaction { value, }) => match strict { false => { - let key = crate::key::sc::new(ns, db, sc); + let key = crate::key::database::sc::new(ns, db, sc); let val = DefineScopeStatement { name: sc.to_owned().into(), ..DefineScopeStatement::default() @@ -1713,7 +1713,7 @@ impl Transaction { value, }) => match strict { false => { - let key = crate::key::tb::new(ns, db, tb); + let key = crate::key::database::tb::new(ns, db, tb); let val = DefineTableStatement { name: tb.to_owned().into(), permissions: Permissions::none(), @@ -1736,7 +1736,7 @@ impl Transaction { &mut self, ns: &str, ) -> Result, Error> { - let key = crate::key::ns::new(ns).encode()?; + let key = crate::key::root::ns::new(ns).encode()?; Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Ns(v) = e { v @@ -1759,7 +1759,7 @@ impl Transaction { ns: &str, db: &str, ) -> Result, Error> { - let key = crate::key::db::new(ns, db).encode()?; + let key = crate::key::namespace::db::new(ns, db).encode()?; Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Db(v) = e { v @@ -1783,7 +1783,7 @@ impl Transaction { db: &str, tb: &str, ) -> Result, Error> { - let key = crate::key::tb::new(ns, db, tb).encode()?; + let key = crate::key::database::tb::new(ns, db, tb).encode()?; Ok(if let Some(e) = self.cache.get(&key) { if let Entry::Tb(v) = e { v @@ -1811,7 +1811,7 @@ impl Transaction { value, }) => match strict { false => { - let key = crate::key::ns::new(ns); + let key = crate::key::root::ns::new(ns); let val = DefineNamespaceStatement { name: ns.to_owned().into(), }; @@ -1839,7 +1839,7 @@ impl Transaction { value, }) => match strict { false => { - let key = crate::key::db::new(ns, db); + let key = crate::key::namespace::db::new(ns, db); let val = DefineDatabaseStatement { name: db.to_owned().into(), changefeed: None, @@ -1869,7 +1869,7 @@ impl Transaction { value, }) => match strict { false => { - let key = crate::key::tb::new(ns, db, tb); + let key = crate::key::database::tb::new(ns, db, tb); let val = DefineTableStatement { name: tb.to_owned().into(), permissions: Permissions::none(), @@ -2072,8 +2072,8 @@ impl Transaction { chn.send(bytes!("-- ------------------------------")).await?; chn.send(bytes!("")).await?; // Fetch records - let beg = thing::prefix(ns, db, &tb.name); - let end = thing::suffix(ns, db, &tb.name); + let beg = crate::key::thing::prefix(ns, db, &tb.name); + let end = crate::key::thing::suffix(ns, db, &tb.name); let mut nxt: Option> = None; loop { let res = match nxt { @@ -2104,7 +2104,7 @@ impl Transaction { } // Parse the key and the value let k: crate::key::thing::Thing = (&k).into(); - let v: crate::sql::value::Value = (&v).into(); + let v: Value = (&v).into(); let t = Thing::from((k.tb, k.id)); // Check if this is a graph edge match (v.pick(&*EDGE), v.pick(&*IN), v.pick(&*OUT)) { diff --git a/lib/src/sql/statements/define.rs b/lib/src/sql/statements/define.rs index c0ad0eb7..ddd43898 100644 --- a/lib/src/sql/statements/define.rs +++ b/lib/src/sql/statements/define.rs @@ -21,7 +21,7 @@ use crate::sql::idiom::{Idiom, Idioms}; use crate::sql::index::Index; use crate::sql::kind::{kind, Kind}; use crate::sql::permission::{permissions, Permissions}; -use crate::sql::statements::{RemoveIndexStatement, UpdateStatement}; +use crate::sql::statements::UpdateStatement; use crate::sql::strand::strand_raw; use crate::sql::tokenizer::{tokenizers, Tokenizer}; use crate::sql::value::{value, values, Value, Values}; @@ -145,7 +145,7 @@ impl DefineNamespaceStatement { // Allowed to run? opt.check(Level::Kv)?; // Process the statement - let key = crate::key::ns::new(&self.name); + let key = crate::key::root::ns::new(&self.name); // Claim transaction let mut run = txn.lock().await; run.set(key, self).await?; @@ -200,7 +200,7 @@ impl DefineDatabaseStatement { // Claim transaction let mut run = txn.lock().await; // Process the statement - let key = crate::key::db::new(opt.ns(), &self.name); + let key = crate::key::namespace::db::new(opt.ns(), &self.name); run.add_ns(opt.ns(), opt.strict).await?; run.set(key, self).await?; // Ok all good @@ -281,7 +281,7 @@ impl DefineFunctionStatement { // Claim transaction let mut run = txn.lock().await; // Process the statement - let key = crate::key::fc::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::database::fc::new(opt.ns(), opt.db(), &self.name); run.add_ns(opt.ns(), opt.strict).await?; run.add_db(opt.ns(), opt.db(), opt.strict).await?; run.set(key, self).await?; @@ -363,7 +363,7 @@ impl DefineAnalyzerStatement { // Claim transaction let mut run = txn.lock().await; // Process the statement - let key = crate::key::az::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::database::az::new(opt.ns(), opt.db(), &self.name); run.add_ns(opt.ns(), opt.strict).await?; run.add_db(opt.ns(), opt.db(), opt.strict).await?; run.set(key, self).await?; @@ -439,7 +439,7 @@ impl DefineLoginStatement { // Claim transaction let mut run = txn.lock().await; // Process the statement - let key = crate::key::nl::new(opt.ns(), &self.name); + let key = crate::key::namespace::lg::new(opt.ns(), &self.name); run.add_ns(opt.ns(), opt.strict).await?; run.set(key, self).await?; // Ok all good @@ -453,7 +453,7 @@ impl DefineLoginStatement { // Claim transaction let mut run = txn.lock().await; // Process the statement - let key = crate::key::dl::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::database::lg::new(opt.ns(), opt.db(), &self.name); run.add_ns(opt.ns(), opt.strict).await?; run.add_db(opt.ns(), opt.db(), opt.strict).await?; run.set(key, self).await?; @@ -559,7 +559,7 @@ impl DefineTokenStatement { // Claim transaction let mut run = txn.lock().await; // Process the statement - let key = crate::key::nt::new(opt.ns(), &self.name); + let key = crate::key::namespace::tk::new(opt.ns(), &self.name); run.add_ns(opt.ns(), opt.strict).await?; run.set(key, self).await?; // Ok all good @@ -573,7 +573,7 @@ impl DefineTokenStatement { // Claim transaction let mut run = txn.lock().await; // Process the statement - let key = crate::key::dt::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::database::tk::new(opt.ns(), opt.db(), &self.name); run.add_ns(opt.ns(), opt.strict).await?; run.add_db(opt.ns(), opt.db(), opt.strict).await?; run.set(key, self).await?; @@ -588,7 +588,7 @@ impl DefineTokenStatement { // Claim transaction let mut run = txn.lock().await; // Process the statement - let key = crate::key::st::new(opt.ns(), opt.db(), sc, &self.name); + let key = crate::key::scope::tk::new(opt.ns(), opt.db(), sc, &self.name); run.add_ns(opt.ns(), opt.strict).await?; run.add_db(opt.ns(), opt.db(), opt.strict).await?; run.add_sc(opt.ns(), opt.db(), sc, opt.strict).await?; @@ -672,7 +672,7 @@ impl DefineScopeStatement { // Claim transaction let mut run = txn.lock().await; // Process the statement - let key = crate::key::sc::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::database::sc::new(opt.ns(), opt.db(), &self.name); run.add_ns(opt.ns(), opt.strict).await?; run.add_db(opt.ns(), opt.db(), opt.strict).await?; run.set(key, self).await?; @@ -790,7 +790,7 @@ impl DefineParamStatement { // Claim transaction let mut run = txn.lock().await; // Process the statement - let key = crate::key::pa::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::database::pa::new(opt.ns(), opt.db(), &self.name); run.add_ns(opt.ns(), opt.strict).await?; run.add_db(opt.ns(), opt.db(), opt.strict).await?; run.set(key, self).await?; @@ -854,22 +854,22 @@ impl DefineTableStatement { // Claim transaction let mut run = txn.lock().await; // Process the statement - let key = crate::key::tb::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::database::tb::new(opt.ns(), opt.db(), &self.name); run.add_ns(opt.ns(), opt.strict).await?; run.add_db(opt.ns(), opt.db(), opt.strict).await?; run.set(key, self).await?; // Check if table is a view if let Some(view) = &self.view { // Remove the table data - let key = crate::key::table::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::table::all::new(opt.ns(), opt.db(), &self.name); run.delp(key, u32::MAX).await?; // Process each foreign table for v in view.what.0.iter() { // Save the view config - let key = crate::key::ft::new(opt.ns(), opt.db(), v, &self.name); + let key = crate::key::table::ft::new(opt.ns(), opt.db(), v, &self.name); run.set(key, self).await?; // Clear the cache - let key = crate::key::ft::prefix(opt.ns(), opt.db(), v); + let key = crate::key::table::ft::prefix(opt.ns(), opt.db(), v); run.clr(key).await?; } // Release the transaction @@ -1057,13 +1057,13 @@ impl DefineEventStatement { // Claim transaction let mut run = txn.lock().await; // Process the statement - let key = crate::key::ev::new(opt.ns(), opt.db(), &self.what, &self.name); + let key = crate::key::table::ev::new(opt.ns(), opt.db(), &self.what, &self.name); 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(), &self.what, opt.strict).await?; run.set(key, self).await?; // Clear the cache - let key = crate::key::ev::prefix(opt.ns(), opt.db(), &self.what); + let key = crate::key::table::ev::prefix(opt.ns(), opt.db(), &self.what); run.clr(key).await?; // Ok all good Ok(Value::None) @@ -1142,13 +1142,13 @@ impl DefineFieldStatement { let mut run = txn.lock().await; // Process the statement let fd = self.name.to_string(); - let key = crate::key::fd::new(opt.ns(), opt.db(), &self.what, &fd); + let key = crate::key::table::fd::new(opt.ns(), opt.db(), &self.what, &fd); 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(), &self.what, opt.strict).await?; run.set(key, self).await?; // Clear the cache - let key = crate::key::fd::prefix(opt.ns(), opt.db(), &self.what); + let key = crate::key::table::fd::prefix(opt.ns(), opt.db(), &self.what); run.clr(key).await?; // Ok all good Ok(Value::None) @@ -1301,16 +1301,17 @@ impl DefineIndexStatement { // Claim transaction let mut run = txn.lock().await; // Process the statement - let key = crate::key::ix::new(opt.ns(), opt.db(), &self.what, &self.name); + let key = crate::key::table::ix::new(opt.ns(), opt.db(), &self.what, &self.name); 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(), &self.what, opt.strict).await?; run.set(key, self).await?; - // Clear the cache - let key = crate::key::ix::prefix(opt.ns(), opt.db(), &self.what); - run.clr(key).await?; // Remove the index data - RemoveIndexStatement::delete_resources(&mut run, opt, &self.what, &self.name).await?; + let key = crate::key::index::all::new(opt.ns(), opt.db(), &self.what, &self.name); + run.delp(key, u32::MAX).await?; + // Clear the cache + let key = crate::key::table::ix::prefix(opt.ns(), opt.db(), &self.what); + run.clr(key).await?; // Release the transaction drop(run); // Force queries to run diff --git a/lib/src/sql/statements/kill.rs b/lib/src/sql/statements/kill.rs index 3e36e3c2..1b5adf34 100644 --- a/lib/src/sql/statements/kill.rs +++ b/lib/src/sql/statements/kill.rs @@ -35,16 +35,16 @@ impl KillStatement { // Claim transaction let mut run = txn.lock().await; // Fetch the live query key - let key = crate::key::lq::new(opt.id()?, opt.ns(), opt.db(), self.id.0); + let key = crate::key::node::lq::new(opt.id()?, opt.ns(), opt.db(), self.id.0); // Fetch the live query key if it exists match run.get(key).await? { Some(val) => match std::str::from_utf8(&val) { Ok(tb) => { // Delete the node live query - let key = crate::key::lq::new(opt.id()?, opt.ns(), opt.db(), self.id.0); + let key = crate::key::node::lq::new(opt.id()?, opt.ns(), opt.db(), self.id.0); run.del(key).await?; // Delete the table live query - let key = crate::key::lv::new(opt.ns(), opt.db(), tb, self.id.0); + let key = crate::key::table::lq::new(opt.ns(), opt.db(), tb, self.id.0); run.del(key).await?; } _ => { diff --git a/lib/src/sql/statements/live.rs b/lib/src/sql/statements/live.rs index 2616009d..0fdf106c 100644 --- a/lib/src/sql/statements/live.rs +++ b/lib/src/sql/statements/live.rs @@ -64,10 +64,10 @@ impl LiveStatement { } stm.node = opt.id()?; // Insert the node live query - let key = crate::key::lq::new(opt.id()?, opt.ns(), opt.db(), self.id.0); + let key = crate::key::node::lq::new(opt.id()?, opt.ns(), opt.db(), self.id.0); run.putc(key, tb.as_str(), None).await?; // Insert the table live query - let key = crate::key::lv::new(opt.ns(), opt.db(), &tb, self.id.0); + let key = crate::key::table::lq::new(opt.ns(), opt.db(), &tb, self.id.0); run.putc(key, stm, None).await?; } v => { diff --git a/lib/src/sql/statements/remove.rs b/lib/src/sql/statements/remove.rs index 04eeed97..5b033558 100644 --- a/lib/src/sql/statements/remove.rs +++ b/lib/src/sql/statements/remove.rs @@ -3,7 +3,6 @@ use crate::dbs::Options; use crate::dbs::{Level, Transaction}; use crate::doc::CursorDoc; use crate::err::Error; -use crate::kvs; use crate::sql::base::{base, base_or_scope, Base}; use crate::sql::comment::{mightbespace, shouldbespace}; use crate::sql::error::IResult; @@ -124,10 +123,10 @@ impl RemoveNamespaceStatement { // Claim transaction let mut run = txn.lock().await; // Delete the definition - let key = crate::key::ns::new(&self.name); + let key = crate::key::root::ns::new(&self.name); run.del(key).await?; // Delete the resource data - let key = crate::key::namespace::new(&self.name); + let key = crate::key::namespace::all::new(&self.name); run.delp(key, u32::MAX).await?; // Ok all good Ok(Value::None) @@ -178,10 +177,10 @@ impl RemoveDatabaseStatement { // Claim transaction let mut run = txn.lock().await; // Delete the definition - let key = crate::key::db::new(opt.ns(), &self.name); + let key = crate::key::namespace::db::new(opt.ns(), &self.name); run.del(key).await?; // Delete the resource data - let key = crate::key::database::new(opt.ns(), &self.name); + let key = crate::key::database::all::new(opt.ns(), &self.name); run.delp(key, u32::MAX).await?; // Ok all good Ok(Value::None) @@ -232,7 +231,7 @@ impl RemoveFunctionStatement { // Claim transaction let mut run = txn.lock().await; // Delete the definition - let key = crate::key::fc::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::database::fc::new(opt.ns(), opt.db(), &self.name); run.del(key).await?; // Ok all good Ok(Value::None) @@ -290,7 +289,7 @@ impl RemoveAnalyzerStatement { // Claim transaction let mut run = txn.lock().await; // Delete the definition - let key = crate::key::az::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::database::az::new(opt.ns(), opt.db(), &self.name); run.del(key).await?; // TODO Check that the analyzer is not used in any schema // Ok all good @@ -345,7 +344,7 @@ impl RemoveLoginStatement { // Claim transaction let mut run = txn.lock().await; // Delete the definition - let key = crate::key::nl::new(opt.ns(), &self.name); + let key = crate::key::namespace::lg::new(opt.ns(), &self.name); run.del(key).await?; // Ok all good Ok(Value::None) @@ -358,7 +357,7 @@ impl RemoveLoginStatement { // Claim transaction let mut run = txn.lock().await; // Delete the definition - let key = crate::key::dl::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::database::lg::new(opt.ns(), opt.db(), &self.name); run.del(key).await?; // Ok all good Ok(Value::None) @@ -420,7 +419,7 @@ impl RemoveTokenStatement { // Claim transaction let mut run = txn.lock().await; // Delete the definition - let key = crate::key::nt::new(opt.ns(), &self.name); + let key = crate::key::namespace::tk::new(opt.ns(), &self.name); run.del(key).await?; // Ok all good Ok(Value::None) @@ -433,7 +432,7 @@ impl RemoveTokenStatement { // Claim transaction let mut run = txn.lock().await; // Delete the definition - let key = crate::key::dt::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::database::tk::new(opt.ns(), opt.db(), &self.name); run.del(key).await?; // Ok all good Ok(Value::None) @@ -446,7 +445,7 @@ impl RemoveTokenStatement { // Claim transaction let mut run = txn.lock().await; // Delete the definition - let key = crate::key::st::new(opt.ns(), opt.db(), sc, &self.name); + let key = crate::key::scope::tk::new(opt.ns(), opt.db(), sc, &self.name); run.del(key).await?; // Ok all good Ok(Value::None) @@ -505,10 +504,10 @@ impl RemoveScopeStatement { // Claim transaction let mut run = txn.lock().await; // Delete the definition - let key = crate::key::sc::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::database::sc::new(opt.ns(), opt.db(), &self.name); run.del(key).await?; // Remove the resource data - let key = crate::key::scope::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::scope::all::new(opt.ns(), opt.db(), &self.name); run.delp(key, u32::MAX).await?; // Ok all good Ok(Value::None) @@ -559,7 +558,7 @@ impl RemoveParamStatement { // Claim transaction let mut run = txn.lock().await; // Delete the definition - let key = crate::key::pa::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::database::pa::new(opt.ns(), opt.db(), &self.name); run.del(key).await?; // Ok all good Ok(Value::None) @@ -611,10 +610,10 @@ impl RemoveTableStatement { // Claim transaction let mut run = txn.lock().await; // Delete the definition - let key = crate::key::tb::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::database::tb::new(opt.ns(), opt.db(), &self.name); run.del(key).await?; // Remove the resource data - let key = crate::key::table::new(opt.ns(), opt.db(), &self.name); + let key = crate::key::table::all::new(opt.ns(), opt.db(), &self.name); run.delp(key, u32::MAX).await?; // Ok all good Ok(Value::None) @@ -666,10 +665,10 @@ impl RemoveEventStatement { // Claim transaction let mut run = txn.lock().await; // Delete the definition - let key = crate::key::ev::new(opt.ns(), opt.db(), &self.what, &self.name); + let key = crate::key::table::ev::new(opt.ns(), opt.db(), &self.what, &self.name); run.del(key).await?; // Clear the cache - let key = crate::key::ev::prefix(opt.ns(), opt.db(), &self.what); + let key = crate::key::table::ev::prefix(opt.ns(), opt.db(), &self.what); run.clr(key).await?; // Ok all good Ok(Value::None) @@ -728,10 +727,10 @@ impl RemoveFieldStatement { let mut run = txn.lock().await; // Delete the definition let fd = self.name.to_string(); - let key = crate::key::fd::new(opt.ns(), opt.db(), &self.what, &fd); + let key = crate::key::table::fd::new(opt.ns(), opt.db(), &self.what, &fd); run.del(key).await?; // Clear the cache - let key = crate::key::fd::prefix(opt.ns(), opt.db(), &self.what); + let key = crate::key::table::fd::prefix(opt.ns(), opt.db(), &self.what); run.clr(key).await?; // Ok all good Ok(Value::None) @@ -789,50 +788,17 @@ impl RemoveIndexStatement { // Claim transaction let mut run = txn.lock().await; // Delete the definition - let key = crate::key::ix::new(opt.ns(), opt.db(), &self.what, &self.name); + let key = crate::key::table::ix::new(opt.ns(), opt.db(), &self.what, &self.name); run.del(key).await?; + // Remove the index data + let key = crate::key::index::all::new(opt.ns(), opt.db(), &self.what, &self.name); + run.delp(key, u32::MAX).await?; // Clear the cache - let key = crate::key::ix::prefix(opt.ns(), opt.db(), &self.what); + let key = crate::key::table::ix::prefix(opt.ns(), opt.db(), &self.what); run.clr(key).await?; - // Delete resource - Self::delete_resources(&mut run, opt, &self.what, &self.name).await?; // Ok all good Ok(Value::None) } - - // Remove the resource data (whatever the type of the index) - pub(crate) async fn delete_resources( - run: &mut kvs::Transaction, - opt: &Options, - tb: &str, - ix: &str, - ) -> Result<(), Error> { - let rng = crate::key::index::Index::range(opt.ns(), opt.db(), tb, ix); - run.delr(rng, u32::MAX).await?; - let rng = crate::key::bc::Bc::range(opt.ns(), opt.db(), tb, ix); - run.delr(rng, u32::MAX).await?; - let rng = crate::key::bd::Bd::range(opt.ns(), opt.db(), tb, ix); - run.delr(rng, u32::MAX).await?; - let rng = crate::key::bf::Bf::range(opt.ns(), opt.db(), tb, ix); - run.delr(rng, u32::MAX).await?; - let rng = crate::key::bi::Bi::range(opt.ns(), opt.db(), tb, ix); - run.delr(rng, u32::MAX).await?; - let rng = crate::key::bk::Bk::range(opt.ns(), opt.db(), tb, ix); - run.delr(rng, u32::MAX).await?; - let rng = crate::key::bl::Bl::range(opt.ns(), opt.db(), tb, ix); - run.delr(rng, u32::MAX).await?; - let rng = crate::key::bo::Bo::range(opt.ns(), opt.db(), tb, ix); - run.delr(rng, u32::MAX).await?; - let rng = crate::key::bp::Bp::range(opt.ns(), opt.db(), tb, ix); - run.delr(rng, u32::MAX).await?; - let key = crate::key::bs::Bs::new(opt.ns(), opt.db(), tb, ix); - run.del(key).await?; - let rng = crate::key::bt::Bt::range(opt.ns(), opt.db(), tb, ix); - run.delr(rng, u32::MAX).await?; - let rng = crate::key::bu::Bu::range(opt.ns(), opt.db(), tb, ix); - run.delr(rng, u32::MAX).await?; - Ok(()) - } } impl Display for RemoveIndexStatement { diff --git a/lib/tests/remove.rs b/lib/tests/remove.rs index 382056eb..0c91a519 100644 --- a/lib/tests/remove.rs +++ b/lib/tests/remove.rs @@ -1,20 +1,11 @@ mod parse; +#[macro_use] +mod util; + use parse::Parse; use surrealdb::dbs::Session; use surrealdb::err::Error; -use surrealdb::key::bc::Bc; -use surrealdb::key::bd::Bd; -use surrealdb::key::bf::Bf; -use surrealdb::key::bi::Bi; -use surrealdb::key::bk::Bk; -use surrealdb::key::bl::Bl; -use surrealdb::key::bo::Bo; -use surrealdb::key::bp::Bp; -use surrealdb::key::bs::Bs; -use surrealdb::key::bt::Bt; -use surrealdb::key::bu::Bu; -use surrealdb::key::index::Index; use surrealdb::kvs::Datastore; use surrealdb::sql::Value; @@ -86,20 +77,6 @@ async fn remove_statement_analyzer() -> Result<(), Error> { Ok(()) } -macro_rules! check_empty_range { - ($tx:expr, $rng:expr) => {{ - let r = $tx.getr($rng, 1).await?; - assert!(r.is_empty()); - }}; -} - -macro_rules! check_none_val { - ($tx:expr, $key:expr) => {{ - let r = $tx.get($key).await?; - assert!(r.is_none()); - }}; -} - #[tokio::test] async fn remove_statement_index() -> Result<(), Error> { let sql = " @@ -135,18 +112,7 @@ async fn remove_statement_index() -> Result<(), Error> { let mut tx = dbs.transaction(false, false).await?; for ix in ["uniq_isbn", "idx_author", "ft_title"] { - check_empty_range!(&mut tx, Bc::range("test", "test", "book", ix)); - check_empty_range!(&mut tx, Bd::range("test", "test", "book", ix)); - check_empty_range!(&mut tx, Bf::range("test", "test", "book", ix)); - check_empty_range!(&mut tx, Bi::range("test", "test", "book", ix)); - check_empty_range!(&mut tx, Bk::range("test", "test", "book", ix)); - check_empty_range!(&mut tx, Bl::range("test", "test", "book", ix)); - check_empty_range!(&mut tx, Bo::range("test", "test", "book", ix)); - check_empty_range!(&mut tx, Bp::range("test", "test", "book", ix)); - check_none_val!(&mut tx, Bs::new("test", "test", "book", ix)); - check_empty_range!(&mut tx, Bt::range("test", "test", "book", ix)); - check_empty_range!(&mut tx, Bu::range("test", "test", "book", ix)); - check_empty_range!(&mut tx, Index::range("test", "test", "book", ix)); + assert_empty_prefix!(&mut tx, surrealdb::key::index::all::new("test", "test", "book", ix)); } Ok(()) } diff --git a/lib/tests/util.rs b/lib/tests/util.rs new file mode 100644 index 00000000..0d529b2e --- /dev/null +++ b/lib/tests/util.rs @@ -0,0 +1,23 @@ +#[allow(unused_macros)] +macro_rules! assert_empty_val { + ($tx:expr, $key:expr) => {{ + let r = $tx.get($key).await?; + assert!(r.is_none()); + }}; +} + +#[allow(unused_macros)] +macro_rules! assert_empty_prefix { + ($tx:expr, $rng:expr) => {{ + let r = $tx.getp($rng, 1).await?; + assert!(r.is_empty()); + }}; +} + +#[allow(unused_macros)] +macro_rules! assert_empty_range { + ($tx:expr, $rng:expr) => {{ + let r = $tx.getr($rng, 1).await?; + assert!(r.is_empty()); + }}; +}