From 364412b437121776699fb0c839179006c8965342 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Fri, 18 Mar 2022 07:21:22 +0000 Subject: [PATCH] Improve datastore key creation functionality --- lib/src/dbs/channel.rs | 11 ++-- lib/src/dbs/iterate.rs | 11 ++-- lib/src/key/database.rs | 12 +++++ lib/src/key/db.rs | 12 +++++ lib/src/key/dl.rs | 12 +++++ lib/src/key/dt.rs | 12 +++++ lib/src/key/ev.rs | 12 +++++ lib/src/key/fd.rs | 12 +++++ lib/src/key/ft.rs | 12 +++++ lib/src/key/guide.rs | 84 ++++++++++++++++++++++++++++++ lib/src/key/index.rs | 12 +++++ lib/src/key/ix.rs | 12 +++++ lib/src/key/key.rs | 110 --------------------------------------- lib/src/key/kv.rs | 51 ++++++++++++++++++ lib/src/key/lv.rs | 12 +++++ lib/src/key/mod.rs | 33 ++++++++++-- lib/src/key/namespace.rs | 12 +++++ lib/src/key/nl.rs | 12 +++++ lib/src/key/ns.rs | 12 +++++ lib/src/key/nt.rs | 12 +++++ lib/src/key/point.rs | 12 +++++ lib/src/key/sc.rs | 12 +++++ lib/src/key/st.rs | 12 +++++ lib/src/key/table.rs | 12 +++++ lib/src/key/tb.rs | 12 +++++ lib/src/key/thing.rs | 12 +++++ lib/src/kvs/ex.rs | 56 ++++++++++---------- 27 files changed, 443 insertions(+), 153 deletions(-) create mode 100644 lib/src/key/guide.rs delete mode 100644 lib/src/key/key.rs create mode 100644 lib/src/key/kv.rs diff --git a/lib/src/dbs/channel.rs b/lib/src/dbs/channel.rs index b3e505ac..45b1cdd6 100644 --- a/lib/src/dbs/channel.rs +++ b/lib/src/dbs/channel.rs @@ -3,7 +3,6 @@ use crate::dbs::Options; use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; -use crate::key; use crate::key::thing; use crate::sql::array::Array; use crate::sql::model::Model; @@ -123,21 +122,21 @@ impl Table { chn: &Sender<(Option, Value)>, ) -> Result<(), Error> { if ctx.is_ok() { - let beg = thing::new(opt.ns(), opt.db(), &self.name, key::PREFIX); - let end = thing::new(opt.ns(), opt.db(), &self.name, key::SUFFIX); + let beg = thing::prefix(opt.ns(), opt.db(), &self.name); + let end = thing::suffix(opt.ns(), opt.db(), &self.name); let mut nxt: Option> = None; loop { if ctx.is_ok() { let res = match nxt { None => { - let min = beg.encode()?; - let max = end.encode()?; + let min = beg.clone(); + let max = end.clone(); txn.clone().lock().await.scan(min..max, 1000).await? } Some(ref mut beg) => { beg.push(0x00); let min = beg.clone(); - let max = end.encode()?; + let max = end.clone(); txn.clone().lock().await.scan(min..max, 1000).await? } }; diff --git a/lib/src/dbs/iterate.rs b/lib/src/dbs/iterate.rs index 7bac0e92..70993fac 100644 --- a/lib/src/dbs/iterate.rs +++ b/lib/src/dbs/iterate.rs @@ -4,7 +4,6 @@ use crate::dbs::Options; use crate::dbs::Runtime; use crate::dbs::Transaction; use crate::err::Error; -use crate::key; use crate::key::thing; use crate::sql::array::Array; use crate::sql::model::Model; @@ -126,21 +125,21 @@ impl Table { ite: &mut Iterator, ) -> Result<(), Error> { if ctx.is_ok() { - let beg = thing::new(opt.ns(), opt.db(), &self.name, key::PREFIX); - let end = thing::new(opt.ns(), opt.db(), &self.name, key::SUFFIX); + let beg = thing::prefix(opt.ns(), opt.db(), &self.name); + let end = thing::suffix(opt.ns(), opt.db(), &self.name); let mut nxt: Option> = None; loop { if ctx.is_ok() { let res = match nxt { None => { - let min = beg.encode()?; - let max = end.encode()?; + let min = beg.clone(); + let max = end.clone(); txn.clone().lock().await.scan(min..max, 1000).await? } Some(ref mut beg) => { beg.push(0x00); let min = beg.clone(); - let max = end.encode()?; + let max = end.clone(); txn.clone().lock().await.scan(min..max, 1000).await? } }; diff --git a/lib/src/key/database.rs b/lib/src/key/database.rs index a160eba0..fb2daf9c 100644 --- a/lib/src/key/database.rs +++ b/lib/src/key/database.rs @@ -27,6 +27,18 @@ pub fn new(ns: &str, db: &str) -> Database { Database::new(ns.to_string(), db.to_string()) } +pub fn prefix(ns: &str) -> Vec { + let mut k = super::namespace::new(ns).encode().unwrap(); + k.extend_from_slice(&[0x2a, 0x00]); + k +} + +pub fn suffix(ns: &str) -> Vec { + let mut k = super::namespace::new(ns).encode().unwrap(); + k.extend_from_slice(&[0x2a, 0xff]); + k +} + impl Database { pub fn new(ns: String, db: String) -> Database { Database { diff --git a/lib/src/key/db.rs b/lib/src/key/db.rs index ebc9a63e..2cfa94bf 100644 --- a/lib/src/key/db.rs +++ b/lib/src/key/db.rs @@ -29,6 +29,18 @@ pub fn new(ns: &str, db: &str) -> Db { Db::new(ns.to_string(), db.to_string()) } +pub fn prefix(ns: &str) -> Vec { + let mut k = super::ns::new(ns).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x64, 0x02, 0x00]); + k +} + +pub fn suffix(ns: &str) -> Vec { + let mut k = super::ns::new(ns).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x64, 0x02, 0xff]); + k +} + impl Db { pub fn new(ns: String, db: String) -> Db { Db { diff --git a/lib/src/key/dl.rs b/lib/src/key/dl.rs index 3c099c3c..1246bba7 100644 --- a/lib/src/key/dl.rs +++ b/lib/src/key/dl.rs @@ -31,6 +31,18 @@ pub fn new(ns: &str, db: &str, dl: &str) -> Dl { Dl::new(ns.to_string(), db.to_string(), dl.to_string()) } +pub fn prefix(ns: &str, db: &str) -> Vec { + let mut k = super::database::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x64, 0x6c, 0x00]); + k +} + +pub fn suffix(ns: &str, db: &str) -> Vec { + let mut k = super::database::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x64, 0x6c, 0xff]); + k +} + impl Dl { pub fn new(ns: String, db: String, dl: String) -> Dl { Dl { diff --git a/lib/src/key/dt.rs b/lib/src/key/dt.rs index 4193d498..d49f0191 100644 --- a/lib/src/key/dt.rs +++ b/lib/src/key/dt.rs @@ -31,6 +31,18 @@ pub fn new(ns: &str, db: &str, tb: &str) -> Dt { Dt::new(ns.to_string(), db.to_string(), tb.to_string()) } +pub fn prefix(ns: &str, db: &str) -> Vec { + let mut k = super::database::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x64, 0x74, 0x00]); + k +} + +pub fn suffix(ns: &str, db: &str) -> Vec { + let mut k = super::database::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x64, 0x74, 0xff]); + k +} + impl Dt { pub fn new(ns: String, db: String, tk: String) -> Dt { Dt { diff --git a/lib/src/key/ev.rs b/lib/src/key/ev.rs index 9a4798ad..d28d3aa6 100644 --- a/lib/src/key/ev.rs +++ b/lib/src/key/ev.rs @@ -33,6 +33,18 @@ pub fn new(ns: &str, db: &str, tb: &str, ev: &str) -> Ev { Ev::new(ns.to_string(), db.to_string(), tb.to_string(), ev.to_string()) } +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(&[0x21, 0x65, 0x76, 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(&[0x21, 0x65, 0x76, 0xff]); + k +} + impl Ev { pub fn new(ns: String, db: String, tb: String, ev: String) -> Ev { Ev { diff --git a/lib/src/key/fd.rs b/lib/src/key/fd.rs index 52c49085..18914dc6 100644 --- a/lib/src/key/fd.rs +++ b/lib/src/key/fd.rs @@ -33,6 +33,18 @@ pub fn new(ns: &str, db: &str, tb: &str, fd: &str) -> Fd { Fd::new(ns.to_string(), db.to_string(), tb.to_string(), fd.to_string()) } +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(&[0x21, 0x66, 0x64, 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(&[0x21, 0x66, 0x64, 0xff]); + k +} + impl Fd { pub fn new(ns: String, db: String, tb: String, fd: String) -> Fd { Fd { diff --git a/lib/src/key/ft.rs b/lib/src/key/ft.rs index 4b4990f2..740bd262 100644 --- a/lib/src/key/ft.rs +++ b/lib/src/key/ft.rs @@ -33,6 +33,18 @@ pub fn new(ns: &str, db: &str, tb: &str, ft: &str) -> Ft { Ft::new(ns.to_string(), db.to_string(), tb.to_string(), ft.to_string()) } +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(&[0x21, 0x66, 0x74, 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(&[0x21, 0x66, 0x74, 0xff]); + k +} + impl Ft { pub fn new(ns: String, db: String, tb: String, ft: String) -> Ft { Ft { diff --git a/lib/src/key/guide.rs b/lib/src/key/guide.rs new file mode 100644 index 00000000..1b976acc --- /dev/null +++ b/lib/src/key/guide.rs @@ -0,0 +1,84 @@ +use crate::err::Error; +use serde::{Deserialize, Serialize}; +use storekey::{deserialize, serialize}; + +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] +pub struct Guide { + __: u8, + _a: u8, + pub ns: String, + _b: u8, + pub db: String, + _c: u8, + pub tb: String, + _d: u8, + pub ix: String, +} + +impl From for Vec { + fn from(val: Guide) -> Vec { + val.encode().unwrap() + } +} + +impl From> for Guide { + fn from(val: Vec) -> Self { + Guide::decode(&val).unwrap() + } +} + +pub fn new(ns: &str, db: &str, tb: &str, ix: &str) -> Guide { + Guide::new(ns.to_string(), db.to_string(), tb.to_string(), ix.to_string()) +} + +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(&[0xa4, 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(&[0xa4, 0xff]); + k +} + +impl Guide { + pub fn new(ns: String, db: String, tb: String, ix: String) -> Guide { + Guide { + __: 0x2f, // / + _a: 0x2a, // * + ns, + _b: 0x2a, // * + db, + _c: 0x2a, // * + tb, + _d: 0xa4, // ¤ + ix, + } + } + pub fn encode(&self) -> Result, Error> { + Ok(serialize(self)?) + } + pub fn decode(v: &[u8]) -> Result { + Ok(deserialize(v)?) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn key() { + use super::*; + #[rustfmt::skip] + let val = Guide::new( + "test".to_string(), + "test".to_string(), + "test".to_string(), + "test".to_string(), + ); + let enc = Guide::encode(&val).unwrap(); + let dec = Guide::decode(&enc).unwrap(); + assert_eq!(val, dec); + } +} diff --git a/lib/src/key/index.rs b/lib/src/key/index.rs index 23adccc7..261acbe4 100644 --- a/lib/src/key/index.rs +++ b/lib/src/key/index.rs @@ -33,6 +33,18 @@ pub fn new(ns: &str, db: &str, tb: &str, ix: &str, fd: Value) -> Index { Index::new(ns.to_string(), db.to_string(), tb.to_string(), ix.to_string(), fd) } +pub fn prefix(ns: &str, db: &str, tb: &str, ix: &str) -> Vec { + let mut k = super::guide::new(ns, db, tb, ix).encode().unwrap(); + k.extend_from_slice(&[0x00]); + k +} + +pub fn suffix(ns: &str, db: &str, tb: &str, ix: &str) -> Vec { + let mut k = super::guide::new(ns, db, tb, ix).encode().unwrap(); + k.extend_from_slice(&[0xff]); + k +} + impl Index { pub fn new(ns: String, db: String, tb: String, ix: String, fd: Value) -> Index { Index { diff --git a/lib/src/key/ix.rs b/lib/src/key/ix.rs index 0fcbb7a9..6ebe4970 100644 --- a/lib/src/key/ix.rs +++ b/lib/src/key/ix.rs @@ -33,6 +33,18 @@ pub fn new(ns: &str, db: &str, tb: &str, ix: &str) -> Ix { Ix::new(ns.to_string(), db.to_string(), tb.to_string(), ix.to_string()) } +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(&[0x21, 0x69, 0x78, 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(&[0x21, 0x69, 0x78, 0xff]); + k +} + impl Ix { pub fn new(ns: String, db: String, tb: String, ix: String) -> Ix { Ix { diff --git a/lib/src/key/key.rs b/lib/src/key/key.rs deleted file mode 100644 index d9d66842..00000000 --- a/lib/src/key/key.rs +++ /dev/null @@ -1,110 +0,0 @@ -use super::*; -use crate::err::Error; -use serde::{Deserialize, Serialize}; -use storekey::{deserialize, serialize}; - -// Ignore specifies an ignored field -pub const IGNORE: &str = "\x00"; -// Prefix is the lowest char found in a key -pub const PREFIX: &str = "\x01"; -// Suffix is the highest char found in a key -pub const SUFFIX: &str = "\x7f"; - -/// KV {$kv} -/// NS {$kv}!ns{$ns} -/// -/// Namespace {$kv}*{$ns} -/// NT {$kv}*{$ns}!tk{$tk} -/// NU {$kv}*{$ns}!us{$us} -/// DB {$kv}*{$ns}!db{$db} -/// -/// Database {$kv}*{$ns}*{$db} -/// DT {$kv}*{$ns}*{$db}!tk{$tk} -/// DU {$kv}*{$ns}*{$db}!us{$us} -/// SC {$kv}*{$ns}*{$db}!sc{$sc} -/// ST {$kv}*{$ns}*{$db}!st{$sc}!tk{$tk} -/// -/// TB {$kv}*{$ns}*{$db}!tb{$tb} -/// -/// Table {$kv}*{$ns}*{$db}*{$tb} -/// FT {$kv}*{$ns}*{$db}*{$tb}!ft{$ft} -/// FD {$kv}*{$ns}*{$db}*{$tb}!fd{$fd} -/// EV {$kv}*{$ns}*{$db}*{$tb}!ev{$ev} -/// IX {$kv}*{$ns}*{$db}*{$tb}!ix{$ix} -/// LV {$kv}*{$ns}*{$db}*{$tb}!lv{$lv} -/// -/// Thing {$kv}*{$ns}*{$db}*{$tb}*{$id} -/// -/// Patch {$kv}*{$ns}*{$db}*{$tb}~{$id}{$at} -/// -/// Index {$kv}*{$ns}*{$db}*{$tb}¤{$ix}{$fd} -/// Point {$kv}*{$ns}*{$db}*{$tb}¤{$ix}{$fd}{$id} - -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] -pub enum Key { - Ns(ns::Ns), // Namespace definition key - Nt(nt::Nt), // Namespace token definition key - Nu(nl::Nl), // Namespace login definition key - Db(db::Db), // Database definition key - Dt(dt::Dt), // Database token definition key - Du(dl::Dl), // Database login definition key - Sc(sc::Sc), // Scope definition key - St(st::St), // Scope token definition key - Tb(tb::Tb), // Table definition key - Ft(ft::Ft), // Foreign table definition key - Ev(ev::Ev), // Event definition key - Fd(fd::Fd), // Field definition key - Ix(ix::Ix), // Index definition key - Lv(lv::Lv), // Live definition key - Namespace, // Namespace resource data key - Database, // Database resource data key - Table, // Table resource data key - Thing, // Thing resource data key - Index, // Index resource data key - Point, // Index resource data key - Patch, // Patch resource data key - Edge, // Edge resource data key -} - -impl From for Vec { - fn from(val: Key) -> Vec { - val.encode().unwrap() - } -} - -impl From> for Key { - fn from(val: Vec) -> Self { - Key::decode(&val).unwrap() - } -} - -impl Key { - pub fn encode(&self) -> Result, Error> { - Ok(serialize(self)?) - } - pub fn decode(v: &[u8]) -> Result { - Ok(deserialize(v)?) - } -} - -#[cfg(test)] -mod tests { - #[test] - fn key() { - use super::*; - #[rustfmt::skip] - let val = Key::Tb(tb::new("test", "test", "test")); - let enc = Key::encode(&val).unwrap(); - let dec = Key::decode(&enc).unwrap(); - assert_eq!(val, dec); - } - #[test] - fn sort() { - use super::*; - let less = Key::Tb(tb::new("test", "test", "")); - let item = Key::Tb(tb::new("test", "test", "item")); - let more = Key::Tb(tb::new("test", "test", "test")); - assert!(less.encode().unwrap() < item.encode().unwrap()); - assert!(item.encode().unwrap() < more.encode().unwrap()); - } -} diff --git a/lib/src/key/kv.rs b/lib/src/key/kv.rs new file mode 100644 index 00000000..dc58f8d0 --- /dev/null +++ b/lib/src/key/kv.rs @@ -0,0 +1,51 @@ +use crate::err::Error; +use serde::{Deserialize, Serialize}; +use storekey::{deserialize, serialize}; + +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] +pub struct Kv { + __: u8, +} + +impl From for Vec { + fn from(val: Kv) -> Vec { + val.encode().unwrap() + } +} + +impl From> for Kv { + fn from(val: Vec) -> Self { + Kv::decode(&val).unwrap() + } +} + +pub fn new() -> Kv { + Kv::new() +} + +impl Kv { + pub fn new() -> Kv { + Kv { + __: 0x2f, // / + } + } + pub fn encode(&self) -> Result, Error> { + Ok(serialize(self)?) + } + pub fn decode(v: &[u8]) -> Result { + Ok(deserialize::(v)?) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn key() { + use super::*; + #[rustfmt::skip] + let val = Kv::new(); + let enc = Kv::encode(&val).unwrap(); + let dec = Kv::decode(&enc).unwrap(); + assert_eq!(val, dec); + } +} diff --git a/lib/src/key/lv.rs b/lib/src/key/lv.rs index 7a6df4b4..a436389f 100644 --- a/lib/src/key/lv.rs +++ b/lib/src/key/lv.rs @@ -33,6 +33,18 @@ pub fn new(ns: &str, db: &str, tb: &str, lv: &str) -> Lv { Lv::new(ns.to_string(), db.to_string(), tb.to_string(), lv.to_string()) } +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(&[0x21, 0x6c, 0x76, 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(&[0x21, 0x6c, 0x76, 0xff]); + k +} + impl Lv { pub fn new(ns: String, db: String, tb: String, lv: String) -> Lv { Lv { diff --git a/lib/src/key/mod.rs b/lib/src/key/mod.rs index 5fe49f75..baf722f8 100644 --- a/lib/src/key/mod.rs +++ b/lib/src/key/mod.rs @@ -1,5 +1,31 @@ -pub use self::key::*; - +/// KV / +/// NS /!ns{$ns} +/// +/// Namespace /*{$ns} +/// NL /*{$ns}!nl{$us} +/// NT /*{$ns}!nt{$tk} +/// DB /*{$ns}!db{$db} +/// +/// Database /*{$ns}*{$db} +/// DL /*{$ns}*{$db}!dl{$us} +/// DT /*{$ns}*{$db}!dt{$tk} +/// SC /*{$ns}*{$db}!sc{$sc} +/// ST /*{$ns}*{$db}!st{$sc}!tk{$tk} +/// TB /*{$ns}*{$db}!tb{$tb} +/// +/// Table /*{$ns}*{$db}*{$tb} +/// FT /*{$ns}*{$db}*{$tb}!ft{$ft} +/// FD /*{$ns}*{$db}*{$tb}!fd{$fd} +/// EV /*{$ns}*{$db}*{$tb}!ev{$ev} +/// IX /*{$ns}*{$db}*{$tb}!ix{$ix} +/// LV /*{$ns}*{$db}*{$tb}!lv{$lv} +/// +/// Thing /*{$ns}*{$db}*{$tb}*{$id} +/// +/// Guide /*{$ns}*{$db}*{$tb}¤{$ix} +/// Index /*{$ns}*{$db}*{$tb}¤{$ix}{$fd} +/// Point /*{$ns}*{$db}*{$tb}¤{$ix}{$fd}{$id} +/// pub mod database; pub mod db; pub mod dl; @@ -7,9 +33,10 @@ pub mod dt; pub mod ev; pub mod fd; pub mod ft; +pub mod guide; pub mod index; pub mod ix; -pub mod key; +pub mod kv; pub mod lv; pub mod namespace; pub mod nl; diff --git a/lib/src/key/namespace.rs b/lib/src/key/namespace.rs index dab6b378..891a2662 100644 --- a/lib/src/key/namespace.rs +++ b/lib/src/key/namespace.rs @@ -25,6 +25,18 @@ pub fn new(ns: &str) -> Namespace { Namespace::new(ns.to_string()) } +pub fn prefix() -> Vec { + let mut k = super::kv::new().encode().unwrap(); + k.extend_from_slice(&[0x2a, 0x00]); + k +} + +pub fn suffix() -> Vec { + let mut k = super::kv::new().encode().unwrap(); + k.extend_from_slice(&[0x2a, 0xff]); + k +} + impl Namespace { pub fn new(ns: String) -> Namespace { Namespace { diff --git a/lib/src/key/nl.rs b/lib/src/key/nl.rs index 55d370bf..bf32d442 100644 --- a/lib/src/key/nl.rs +++ b/lib/src/key/nl.rs @@ -29,6 +29,18 @@ pub fn new(ns: &str, us: &str) -> Nl { Nl::new(ns.to_string(), us.to_string()) } +pub fn prefix(ns: &str) -> Vec { + let mut k = super::namespace::new(ns).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x6e, 0x6c, 0x00]); + k +} + +pub fn suffix(ns: &str) -> Vec { + let mut k = super::namespace::new(ns).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x6e, 0x6c, 0xff]); + k +} + impl Nl { pub fn new(ns: String, us: String) -> Nl { Nl { diff --git a/lib/src/key/ns.rs b/lib/src/key/ns.rs index e645ac10..ab481fde 100644 --- a/lib/src/key/ns.rs +++ b/lib/src/key/ns.rs @@ -27,6 +27,18 @@ pub fn new(ns: &str) -> Ns { Ns::new(ns.to_string()) } +pub fn prefix() -> Vec { + let mut k = super::kv::new().encode().unwrap(); + k.extend_from_slice(&[0x21, 0x6e, 0x73, 0x00]); + k +} + +pub fn suffix() -> Vec { + let mut k = super::kv::new().encode().unwrap(); + k.extend_from_slice(&[0x21, 0x6e, 0x73, 0xff]); + k +} + impl Ns { pub fn new(ns: String) -> Ns { Ns { diff --git a/lib/src/key/nt.rs b/lib/src/key/nt.rs index 81249cd4..cf677e2c 100644 --- a/lib/src/key/nt.rs +++ b/lib/src/key/nt.rs @@ -29,6 +29,18 @@ pub fn new(ns: &str, tk: &str) -> Nt { Nt::new(ns.to_string(), tk.to_string()) } +pub fn prefix(ns: &str) -> Vec { + let mut k = super::namespace::new(ns).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x6e, 0x74, 0x00]); + k +} + +pub fn suffix(ns: &str) -> Vec { + let mut k = super::namespace::new(ns).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x6e, 0x74, 0xff]); + k +} + impl Nt { pub fn new(ns: String, tk: String) -> Nt { Nt { diff --git a/lib/src/key/point.rs b/lib/src/key/point.rs index 2e4ebb59..c5c2012f 100644 --- a/lib/src/key/point.rs +++ b/lib/src/key/point.rs @@ -34,6 +34,18 @@ pub fn new(ns: &str, db: &str, tb: &str, ix: &str, fd: Value, id: &str) -> Point Point::new(ns.to_string(), db.to_string(), tb.to_string(), ix.to_string(), fd, id.to_string()) } +pub fn prefix(ns: &str, db: &str, tb: &str, ix: &str) -> Vec { + let mut k = super::guide::new(ns, db, tb, ix).encode().unwrap(); + k.extend_from_slice(&[0x00]); + k +} + +pub fn suffix(ns: &str, db: &str, tb: &str, ix: &str) -> Vec { + let mut k = super::guide::new(ns, db, tb, ix).encode().unwrap(); + k.extend_from_slice(&[0xff]); + k +} + impl Point { pub fn new(ns: String, db: String, tb: String, ix: String, fd: Value, id: String) -> Point { Point { diff --git a/lib/src/key/sc.rs b/lib/src/key/sc.rs index 97e09a4c..baa5e45d 100644 --- a/lib/src/key/sc.rs +++ b/lib/src/key/sc.rs @@ -31,6 +31,18 @@ pub fn new(ns: &str, db: &str, sc: &str) -> Sc { Sc::new(ns.to_string(), db.to_string(), sc.to_string()) } +pub fn prefix(ns: &str, db: &str) -> Vec { + let mut k = super::database::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x73, 0x63, 0x00]); + k +} + +pub fn suffix(ns: &str, db: &str) -> Vec { + let mut k = super::database::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x73, 0x63, 0xff]); + k +} + impl Sc { pub fn new(ns: String, db: String, sc: String) -> Sc { Sc { diff --git a/lib/src/key/st.rs b/lib/src/key/st.rs index 90e9e588..892c5880 100644 --- a/lib/src/key/st.rs +++ b/lib/src/key/st.rs @@ -35,6 +35,18 @@ pub fn new(ns: &str, db: &str, sc: &str, tk: &str) -> St { St::new(ns.to_string(), db.to_string(), sc.to_string(), tk.to_string()) } +pub fn prefix(ns: &str, db: &str, sc: &str) -> Vec { + let mut k = super::sc::new(ns, db, sc).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x74, 0x6b, 0x00]); + k +} + +pub fn suffix(ns: &str, db: &str, sc: &str) -> Vec { + let mut k = super::sc::new(ns, db, sc).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x74, 0x6b, 0xff]); + k +} + impl St { pub fn new(ns: String, db: String, sc: String, tk: String) -> St { St { diff --git a/lib/src/key/table.rs b/lib/src/key/table.rs index 1e66f653..13587d09 100644 --- a/lib/src/key/table.rs +++ b/lib/src/key/table.rs @@ -29,6 +29,18 @@ pub fn new(ns: &str, db: &str, tb: &str) -> Table { Table::new(ns.to_string(), db.to_string(), tb.to_string()) } +pub fn prefix(ns: &str, db: &str) -> Vec { + let mut k = super::database::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[0x2a, 0x00]); + k +} + +pub fn suffix(ns: &str, db: &str) -> Vec { + let mut k = super::database::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[0x2a, 0xff]); + k +} + impl Table { pub fn new(ns: String, db: String, tb: String) -> Table { Table { diff --git a/lib/src/key/tb.rs b/lib/src/key/tb.rs index 59f4a0ee..9671fbde 100644 --- a/lib/src/key/tb.rs +++ b/lib/src/key/tb.rs @@ -31,6 +31,18 @@ pub fn new(ns: &str, db: &str, tb: &str) -> Tb { Tb::new(ns.to_string(), db.to_string(), tb.to_string()) } +pub fn prefix(ns: &str, db: &str) -> Vec { + let mut k = super::database::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x74, 0x62, 0x00]); + k +} + +pub fn suffix(ns: &str, db: &str) -> Vec { + let mut k = super::database::new(ns, db).encode().unwrap(); + k.extend_from_slice(&[0x21, 0x74, 0x62, 0xff]); + k +} + impl Tb { pub fn new(ns: String, db: String, tb: String) -> Tb { Tb { diff --git a/lib/src/key/thing.rs b/lib/src/key/thing.rs index 7e48f3e8..0f76322f 100644 --- a/lib/src/key/thing.rs +++ b/lib/src/key/thing.rs @@ -37,6 +37,18 @@ pub fn new(ns: &str, db: &str, tb: &str, id: &str) -> Thing { Thing::new(ns.to_string(), db.to_string(), tb.to_string(), id.to_string()) } +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(&[0x2a, 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(&[0x2a, 0xff]); + k +} + impl Thing { pub fn new(ns: String, db: String, tb: String, id: String) -> Thing { Thing { diff --git a/lib/src/kvs/ex.rs b/lib/src/kvs/ex.rs index a593d15e..cde51a40 100644 --- a/lib/src/kvs/ex.rs +++ b/lib/src/kvs/ex.rs @@ -30,50 +30,50 @@ where impl Transaction { // Get all namespaces pub async fn all_ns(&mut self) -> Result, Error> { - let beg = crate::key::ns::new(crate::key::PREFIX); - let end = crate::key::ns::new(crate::key::SUFFIX); + let beg = crate::key::ns::prefix(); + let end = crate::key::ns::suffix(); let val = self.getr(beg..end, u32::MAX).await?; Ok(val.convert()) } // Get all namespace logins pub async fn all_nl(&mut self, ns: &str) -> Result, Error> { - let beg = crate::key::nl::new(ns, crate::key::PREFIX); - let end = crate::key::nl::new(ns, crate::key::SUFFIX); + let beg = crate::key::nl::prefix(ns); + let end = crate::key::nl::suffix(ns); let val = self.getr(beg..end, u32::MAX).await?; Ok(val.convert()) } // Get all namespace tokens pub async fn all_nt(&mut self, ns: &str) -> Result, Error> { - let beg = crate::key::nt::new(ns, crate::key::PREFIX); - let end = crate::key::nt::new(ns, crate::key::SUFFIX); + let beg = crate::key::nt::prefix(ns); + let end = crate::key::nt::suffix(ns); let val = self.getr(beg..end, u32::MAX).await?; Ok(val.convert()) } // Get all databases pub async fn all_db(&mut self, ns: &str) -> Result, Error> { - let beg = crate::key::db::new(ns, crate::key::PREFIX); - let end = crate::key::db::new(ns, crate::key::SUFFIX); + let beg = crate::key::db::prefix(ns); + let end = crate::key::db::suffix(ns); let val = self.getr(beg..end, u32::MAX).await?; Ok(val.convert()) } // Get all database logins pub async fn all_dl(&mut self, ns: &str, db: &str) -> Result, Error> { - let beg = crate::key::dl::new(ns, db, crate::key::PREFIX); - let end = crate::key::dl::new(ns, db, crate::key::SUFFIX); + let beg = crate::key::dl::prefix(ns, db); + let end = crate::key::dl::suffix(ns, db); let val = self.getr(beg..end, u32::MAX).await?; Ok(val.convert()) } // Get all database tokens pub async fn all_dt(&mut self, ns: &str, db: &str) -> Result, Error> { - let beg = crate::key::dt::new(ns, db, crate::key::PREFIX); - let end = crate::key::dt::new(ns, db, crate::key::SUFFIX); + let beg = crate::key::dt::prefix(ns, db); + let end = crate::key::dt::suffix(ns, db); let val = self.getr(beg..end, u32::MAX).await?; Ok(val.convert()) } // Get all scopes pub async fn all_sc(&mut self, ns: &str, db: &str) -> Result, Error> { - let beg = crate::key::sc::new(ns, db, crate::key::PREFIX); - let end = crate::key::sc::new(ns, db, crate::key::SUFFIX); + let beg = crate::key::sc::prefix(ns, db); + let end = crate::key::sc::suffix(ns, db); let val = self.getr(beg..end, u32::MAX).await?; Ok(val.convert()) } @@ -84,15 +84,15 @@ impl Transaction { db: &str, sc: &str, ) -> Result, Error> { - let beg = crate::key::st::new(ns, db, sc, crate::key::PREFIX); - let end = crate::key::st::new(ns, db, sc, crate::key::SUFFIX); + let beg = crate::key::st::prefix(ns, db, sc); + let end = crate::key::st::suffix(ns, db, sc); let val = self.getr(beg..end, u32::MAX).await?; Ok(val.convert()) } // Get all tables pub async fn all_tb(&mut self, ns: &str, db: &str) -> Result, Error> { - let beg = crate::key::tb::new(ns, db, crate::key::PREFIX); - let end = crate::key::tb::new(ns, db, crate::key::SUFFIX); + let beg = crate::key::tb::prefix(ns, db); + let end = crate::key::tb::suffix(ns, db); let val = self.getr(beg..end, u32::MAX).await?; Ok(val.convert()) } @@ -103,8 +103,8 @@ impl Transaction { db: &str, tb: &str, ) -> Result, Error> { - let beg = crate::key::ev::new(ns, db, tb, crate::key::PREFIX); - let end = crate::key::ev::new(ns, db, tb, crate::key::SUFFIX); + let beg = crate::key::ev::prefix(ns, db, tb); + let end = crate::key::ev::suffix(ns, db, tb); let val = self.getr(beg..end, u32::MAX).await?; Ok(val.convert()) } @@ -115,8 +115,8 @@ impl Transaction { db: &str, tb: &str, ) -> Result, Error> { - let beg = crate::key::fd::new(ns, db, tb, crate::key::PREFIX); - let end = crate::key::fd::new(ns, db, tb, crate::key::SUFFIX); + let beg = crate::key::fd::prefix(ns, db, tb); + let end = crate::key::fd::suffix(ns, db, tb); let val = self.getr(beg..end, u32::MAX).await?; Ok(val.convert()) } @@ -127,8 +127,8 @@ impl Transaction { db: &str, tb: &str, ) -> Result, Error> { - let beg = crate::key::ix::new(ns, db, tb, crate::key::PREFIX); - let end = crate::key::ix::new(ns, db, tb, crate::key::SUFFIX); + let beg = crate::key::ix::prefix(ns, db, tb); + let end = crate::key::ix::suffix(ns, db, tb); let val = self.getr(beg..end, u32::MAX).await?; Ok(val.convert()) } @@ -139,8 +139,8 @@ impl Transaction { db: &str, tb: &str, ) -> Result, Error> { - let beg = crate::key::ft::new(ns, db, tb, crate::key::PREFIX); - let end = crate::key::ft::new(ns, db, tb, crate::key::SUFFIX); + let beg = crate::key::ft::prefix(ns, db, tb); + let end = crate::key::ft::suffix(ns, db, tb); let val = self.getr(beg..end, u32::MAX).await?; Ok(val.convert()) } @@ -151,8 +151,8 @@ impl Transaction { db: &str, tb: &str, ) -> Result, Error> { - let beg = crate::key::lv::new(ns, db, tb, crate::key::PREFIX); - let end = crate::key::lv::new(ns, db, tb, crate::key::SUFFIX); + let beg = crate::key::lv::prefix(ns, db, tb); + let end = crate::key::lv::suffix(ns, db, tb); let val = self.getr(beg..end, u32::MAX).await?; Ok(val.convert()) }