Improve datastore key creation functionality

This commit is contained in:
Tobie Morgan Hitchcock 2022-03-18 07:21:22 +00:00
parent f509b88109
commit 364412b437
27 changed files with 443 additions and 153 deletions

View file

@ -3,7 +3,6 @@ use crate::dbs::Options;
use crate::dbs::Runtime; use crate::dbs::Runtime;
use crate::dbs::Transaction; use crate::dbs::Transaction;
use crate::err::Error; use crate::err::Error;
use crate::key;
use crate::key::thing; use crate::key::thing;
use crate::sql::array::Array; use crate::sql::array::Array;
use crate::sql::model::Model; use crate::sql::model::Model;
@ -123,21 +122,21 @@ impl Table {
chn: &Sender<(Option<Thing>, Value)>, chn: &Sender<(Option<Thing>, Value)>,
) -> Result<(), Error> { ) -> Result<(), Error> {
if ctx.is_ok() { if ctx.is_ok() {
let beg = thing::new(opt.ns(), opt.db(), &self.name, key::PREFIX); let beg = thing::prefix(opt.ns(), opt.db(), &self.name);
let end = thing::new(opt.ns(), opt.db(), &self.name, key::SUFFIX); let end = thing::suffix(opt.ns(), opt.db(), &self.name);
let mut nxt: Option<Vec<u8>> = None; let mut nxt: Option<Vec<u8>> = None;
loop { loop {
if ctx.is_ok() { if ctx.is_ok() {
let res = match nxt { let res = match nxt {
None => { None => {
let min = beg.encode()?; let min = beg.clone();
let max = end.encode()?; let max = end.clone();
txn.clone().lock().await.scan(min..max, 1000).await? txn.clone().lock().await.scan(min..max, 1000).await?
} }
Some(ref mut beg) => { Some(ref mut beg) => {
beg.push(0x00); beg.push(0x00);
let min = beg.clone(); let min = beg.clone();
let max = end.encode()?; let max = end.clone();
txn.clone().lock().await.scan(min..max, 1000).await? txn.clone().lock().await.scan(min..max, 1000).await?
} }
}; };

View file

@ -4,7 +4,6 @@ use crate::dbs::Options;
use crate::dbs::Runtime; use crate::dbs::Runtime;
use crate::dbs::Transaction; use crate::dbs::Transaction;
use crate::err::Error; use crate::err::Error;
use crate::key;
use crate::key::thing; use crate::key::thing;
use crate::sql::array::Array; use crate::sql::array::Array;
use crate::sql::model::Model; use crate::sql::model::Model;
@ -126,21 +125,21 @@ impl Table {
ite: &mut Iterator, ite: &mut Iterator,
) -> Result<(), Error> { ) -> Result<(), Error> {
if ctx.is_ok() { if ctx.is_ok() {
let beg = thing::new(opt.ns(), opt.db(), &self.name, key::PREFIX); let beg = thing::prefix(opt.ns(), opt.db(), &self.name);
let end = thing::new(opt.ns(), opt.db(), &self.name, key::SUFFIX); let end = thing::suffix(opt.ns(), opt.db(), &self.name);
let mut nxt: Option<Vec<u8>> = None; let mut nxt: Option<Vec<u8>> = None;
loop { loop {
if ctx.is_ok() { if ctx.is_ok() {
let res = match nxt { let res = match nxt {
None => { None => {
let min = beg.encode()?; let min = beg.clone();
let max = end.encode()?; let max = end.clone();
txn.clone().lock().await.scan(min..max, 1000).await? txn.clone().lock().await.scan(min..max, 1000).await?
} }
Some(ref mut beg) => { Some(ref mut beg) => {
beg.push(0x00); beg.push(0x00);
let min = beg.clone(); let min = beg.clone();
let max = end.encode()?; let max = end.clone();
txn.clone().lock().await.scan(min..max, 1000).await? txn.clone().lock().await.scan(min..max, 1000).await?
} }
}; };

View file

@ -27,6 +27,18 @@ pub fn new(ns: &str, db: &str) -> Database {
Database::new(ns.to_string(), db.to_string()) Database::new(ns.to_string(), db.to_string())
} }
pub fn prefix(ns: &str) -> Vec<u8> {
let mut k = super::namespace::new(ns).encode().unwrap();
k.extend_from_slice(&[0x2a, 0x00]);
k
}
pub fn suffix(ns: &str) -> Vec<u8> {
let mut k = super::namespace::new(ns).encode().unwrap();
k.extend_from_slice(&[0x2a, 0xff]);
k
}
impl Database { impl Database {
pub fn new(ns: String, db: String) -> Database { pub fn new(ns: String, db: String) -> Database {
Database { Database {

View file

@ -29,6 +29,18 @@ pub fn new(ns: &str, db: &str) -> Db {
Db::new(ns.to_string(), db.to_string()) Db::new(ns.to_string(), db.to_string())
} }
pub fn prefix(ns: &str) -> Vec<u8> {
let mut k = super::ns::new(ns).encode().unwrap();
k.extend_from_slice(&[0x21, 0x64, 0x02, 0x00]);
k
}
pub fn suffix(ns: &str) -> Vec<u8> {
let mut k = super::ns::new(ns).encode().unwrap();
k.extend_from_slice(&[0x21, 0x64, 0x02, 0xff]);
k
}
impl Db { impl Db {
pub fn new(ns: String, db: String) -> Db { pub fn new(ns: String, db: String) -> Db {
Db { Db {

View file

@ -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()) Dl::new(ns.to_string(), db.to_string(), dl.to_string())
} }
pub fn prefix(ns: &str, db: &str) -> Vec<u8> {
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<u8> {
let mut k = super::database::new(ns, db).encode().unwrap();
k.extend_from_slice(&[0x21, 0x64, 0x6c, 0xff]);
k
}
impl Dl { impl Dl {
pub fn new(ns: String, db: String, dl: String) -> Dl { pub fn new(ns: String, db: String, dl: String) -> Dl {
Dl { Dl {

View file

@ -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()) Dt::new(ns.to_string(), db.to_string(), tb.to_string())
} }
pub fn prefix(ns: &str, db: &str) -> Vec<u8> {
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<u8> {
let mut k = super::database::new(ns, db).encode().unwrap();
k.extend_from_slice(&[0x21, 0x64, 0x74, 0xff]);
k
}
impl Dt { impl Dt {
pub fn new(ns: String, db: String, tk: String) -> Dt { pub fn new(ns: String, db: String, tk: String) -> Dt {
Dt { Dt {

View file

@ -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()) Ev::new(ns.to_string(), db.to_string(), tb.to_string(), ev.to_string())
} }
pub fn prefix(ns: &str, db: &str, tb: &str) -> Vec<u8> {
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<u8> {
let mut k = super::table::new(ns, db, tb).encode().unwrap();
k.extend_from_slice(&[0x21, 0x65, 0x76, 0xff]);
k
}
impl Ev { impl Ev {
pub fn new(ns: String, db: String, tb: String, ev: String) -> Ev { pub fn new(ns: String, db: String, tb: String, ev: String) -> Ev {
Ev { Ev {

View file

@ -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()) Fd::new(ns.to_string(), db.to_string(), tb.to_string(), fd.to_string())
} }
pub fn prefix(ns: &str, db: &str, tb: &str) -> Vec<u8> {
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<u8> {
let mut k = super::table::new(ns, db, tb).encode().unwrap();
k.extend_from_slice(&[0x21, 0x66, 0x64, 0xff]);
k
}
impl Fd { impl Fd {
pub fn new(ns: String, db: String, tb: String, fd: String) -> Fd { pub fn new(ns: String, db: String, tb: String, fd: String) -> Fd {
Fd { Fd {

View file

@ -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()) Ft::new(ns.to_string(), db.to_string(), tb.to_string(), ft.to_string())
} }
pub fn prefix(ns: &str, db: &str, tb: &str) -> Vec<u8> {
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<u8> {
let mut k = super::table::new(ns, db, tb).encode().unwrap();
k.extend_from_slice(&[0x21, 0x66, 0x74, 0xff]);
k
}
impl Ft { impl Ft {
pub fn new(ns: String, db: String, tb: String, ft: String) -> Ft { pub fn new(ns: String, db: String, tb: String, ft: String) -> Ft {
Ft { Ft {

84
lib/src/key/guide.rs Normal file
View file

@ -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<Guide> for Vec<u8> {
fn from(val: Guide) -> Vec<u8> {
val.encode().unwrap()
}
}
impl From<Vec<u8>> for Guide {
fn from(val: Vec<u8>) -> 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<u8> {
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<u8> {
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<Vec<u8>, Error> {
Ok(serialize(self)?)
}
pub fn decode(v: &[u8]) -> Result<Guide, Error> {
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);
}
}

View file

@ -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) 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<u8> {
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<u8> {
let mut k = super::guide::new(ns, db, tb, ix).encode().unwrap();
k.extend_from_slice(&[0xff]);
k
}
impl Index { impl Index {
pub fn new(ns: String, db: String, tb: String, ix: String, fd: Value) -> Index { pub fn new(ns: String, db: String, tb: String, ix: String, fd: Value) -> Index {
Index { Index {

View file

@ -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()) Ix::new(ns.to_string(), db.to_string(), tb.to_string(), ix.to_string())
} }
pub fn prefix(ns: &str, db: &str, tb: &str) -> Vec<u8> {
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<u8> {
let mut k = super::table::new(ns, db, tb).encode().unwrap();
k.extend_from_slice(&[0x21, 0x69, 0x78, 0xff]);
k
}
impl Ix { impl Ix {
pub fn new(ns: String, db: String, tb: String, ix: String) -> Ix { pub fn new(ns: String, db: String, tb: String, ix: String) -> Ix {
Ix { Ix {

View file

@ -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<Key> for Vec<u8> {
fn from(val: Key) -> Vec<u8> {
val.encode().unwrap()
}
}
impl From<Vec<u8>> for Key {
fn from(val: Vec<u8>) -> Self {
Key::decode(&val).unwrap()
}
}
impl Key {
pub fn encode(&self) -> Result<Vec<u8>, Error> {
Ok(serialize(self)?)
}
pub fn decode(v: &[u8]) -> Result<Key, Error> {
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());
}
}

51
lib/src/key/kv.rs Normal file
View file

@ -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<Kv> for Vec<u8> {
fn from(val: Kv) -> Vec<u8> {
val.encode().unwrap()
}
}
impl From<Vec<u8>> for Kv {
fn from(val: Vec<u8>) -> Self {
Kv::decode(&val).unwrap()
}
}
pub fn new() -> Kv {
Kv::new()
}
impl Kv {
pub fn new() -> Kv {
Kv {
__: 0x2f, // /
}
}
pub fn encode(&self) -> Result<Vec<u8>, Error> {
Ok(serialize(self)?)
}
pub fn decode(v: &[u8]) -> Result<Kv, Error> {
Ok(deserialize::<Kv>(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);
}
}

View file

@ -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()) Lv::new(ns.to_string(), db.to_string(), tb.to_string(), lv.to_string())
} }
pub fn prefix(ns: &str, db: &str, tb: &str) -> Vec<u8> {
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<u8> {
let mut k = super::table::new(ns, db, tb).encode().unwrap();
k.extend_from_slice(&[0x21, 0x6c, 0x76, 0xff]);
k
}
impl Lv { impl Lv {
pub fn new(ns: String, db: String, tb: String, lv: String) -> Lv { pub fn new(ns: String, db: String, tb: String, lv: String) -> Lv {
Lv { Lv {

View file

@ -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 database;
pub mod db; pub mod db;
pub mod dl; pub mod dl;
@ -7,9 +33,10 @@ pub mod dt;
pub mod ev; pub mod ev;
pub mod fd; pub mod fd;
pub mod ft; pub mod ft;
pub mod guide;
pub mod index; pub mod index;
pub mod ix; pub mod ix;
pub mod key; pub mod kv;
pub mod lv; pub mod lv;
pub mod namespace; pub mod namespace;
pub mod nl; pub mod nl;

View file

@ -25,6 +25,18 @@ pub fn new(ns: &str) -> Namespace {
Namespace::new(ns.to_string()) Namespace::new(ns.to_string())
} }
pub fn prefix() -> Vec<u8> {
let mut k = super::kv::new().encode().unwrap();
k.extend_from_slice(&[0x2a, 0x00]);
k
}
pub fn suffix() -> Vec<u8> {
let mut k = super::kv::new().encode().unwrap();
k.extend_from_slice(&[0x2a, 0xff]);
k
}
impl Namespace { impl Namespace {
pub fn new(ns: String) -> Namespace { pub fn new(ns: String) -> Namespace {
Namespace { Namespace {

View file

@ -29,6 +29,18 @@ pub fn new(ns: &str, us: &str) -> Nl {
Nl::new(ns.to_string(), us.to_string()) Nl::new(ns.to_string(), us.to_string())
} }
pub fn prefix(ns: &str) -> Vec<u8> {
let mut k = super::namespace::new(ns).encode().unwrap();
k.extend_from_slice(&[0x21, 0x6e, 0x6c, 0x00]);
k
}
pub fn suffix(ns: &str) -> Vec<u8> {
let mut k = super::namespace::new(ns).encode().unwrap();
k.extend_from_slice(&[0x21, 0x6e, 0x6c, 0xff]);
k
}
impl Nl { impl Nl {
pub fn new(ns: String, us: String) -> Nl { pub fn new(ns: String, us: String) -> Nl {
Nl { Nl {

View file

@ -27,6 +27,18 @@ pub fn new(ns: &str) -> Ns {
Ns::new(ns.to_string()) Ns::new(ns.to_string())
} }
pub fn prefix() -> Vec<u8> {
let mut k = super::kv::new().encode().unwrap();
k.extend_from_slice(&[0x21, 0x6e, 0x73, 0x00]);
k
}
pub fn suffix() -> Vec<u8> {
let mut k = super::kv::new().encode().unwrap();
k.extend_from_slice(&[0x21, 0x6e, 0x73, 0xff]);
k
}
impl Ns { impl Ns {
pub fn new(ns: String) -> Ns { pub fn new(ns: String) -> Ns {
Ns { Ns {

View file

@ -29,6 +29,18 @@ pub fn new(ns: &str, tk: &str) -> Nt {
Nt::new(ns.to_string(), tk.to_string()) Nt::new(ns.to_string(), tk.to_string())
} }
pub fn prefix(ns: &str) -> Vec<u8> {
let mut k = super::namespace::new(ns).encode().unwrap();
k.extend_from_slice(&[0x21, 0x6e, 0x74, 0x00]);
k
}
pub fn suffix(ns: &str) -> Vec<u8> {
let mut k = super::namespace::new(ns).encode().unwrap();
k.extend_from_slice(&[0x21, 0x6e, 0x74, 0xff]);
k
}
impl Nt { impl Nt {
pub fn new(ns: String, tk: String) -> Nt { pub fn new(ns: String, tk: String) -> Nt {
Nt { Nt {

View file

@ -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()) 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<u8> {
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<u8> {
let mut k = super::guide::new(ns, db, tb, ix).encode().unwrap();
k.extend_from_slice(&[0xff]);
k
}
impl Point { impl Point {
pub fn new(ns: String, db: String, tb: String, ix: String, fd: Value, id: String) -> Point { pub fn new(ns: String, db: String, tb: String, ix: String, fd: Value, id: String) -> Point {
Point { Point {

View file

@ -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()) Sc::new(ns.to_string(), db.to_string(), sc.to_string())
} }
pub fn prefix(ns: &str, db: &str) -> Vec<u8> {
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<u8> {
let mut k = super::database::new(ns, db).encode().unwrap();
k.extend_from_slice(&[0x21, 0x73, 0x63, 0xff]);
k
}
impl Sc { impl Sc {
pub fn new(ns: String, db: String, sc: String) -> Sc { pub fn new(ns: String, db: String, sc: String) -> Sc {
Sc { Sc {

View file

@ -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()) St::new(ns.to_string(), db.to_string(), sc.to_string(), tk.to_string())
} }
pub fn prefix(ns: &str, db: &str, sc: &str) -> Vec<u8> {
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<u8> {
let mut k = super::sc::new(ns, db, sc).encode().unwrap();
k.extend_from_slice(&[0x21, 0x74, 0x6b, 0xff]);
k
}
impl St { impl St {
pub fn new(ns: String, db: String, sc: String, tk: String) -> St { pub fn new(ns: String, db: String, sc: String, tk: String) -> St {
St { St {

View file

@ -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()) Table::new(ns.to_string(), db.to_string(), tb.to_string())
} }
pub fn prefix(ns: &str, db: &str) -> Vec<u8> {
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<u8> {
let mut k = super::database::new(ns, db).encode().unwrap();
k.extend_from_slice(&[0x2a, 0xff]);
k
}
impl Table { impl Table {
pub fn new(ns: String, db: String, tb: String) -> Table { pub fn new(ns: String, db: String, tb: String) -> Table {
Table { Table {

View file

@ -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()) Tb::new(ns.to_string(), db.to_string(), tb.to_string())
} }
pub fn prefix(ns: &str, db: &str) -> Vec<u8> {
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<u8> {
let mut k = super::database::new(ns, db).encode().unwrap();
k.extend_from_slice(&[0x21, 0x74, 0x62, 0xff]);
k
}
impl Tb { impl Tb {
pub fn new(ns: String, db: String, tb: String) -> Tb { pub fn new(ns: String, db: String, tb: String) -> Tb {
Tb { Tb {

View file

@ -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()) Thing::new(ns.to_string(), db.to_string(), tb.to_string(), id.to_string())
} }
pub fn prefix(ns: &str, db: &str, tb: &str) -> Vec<u8> {
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<u8> {
let mut k = super::table::new(ns, db, tb).encode().unwrap();
k.extend_from_slice(&[0x2a, 0xff]);
k
}
impl Thing { impl Thing {
pub fn new(ns: String, db: String, tb: String, id: String) -> Thing { pub fn new(ns: String, db: String, tb: String, id: String) -> Thing {
Thing { Thing {

View file

@ -30,50 +30,50 @@ where
impl Transaction { impl Transaction {
// Get all namespaces // Get all namespaces
pub async fn all_ns(&mut self) -> Result<Vec<DefineNamespaceStatement>, Error> { pub async fn all_ns(&mut self) -> Result<Vec<DefineNamespaceStatement>, Error> {
let beg = crate::key::ns::new(crate::key::PREFIX); let beg = crate::key::ns::prefix();
let end = crate::key::ns::new(crate::key::SUFFIX); let end = crate::key::ns::suffix();
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
Ok(val.convert()) Ok(val.convert())
} }
// Get all namespace logins // Get all namespace logins
pub async fn all_nl(&mut self, ns: &str) -> Result<Vec<DefineLoginStatement>, Error> { pub async fn all_nl(&mut self, ns: &str) -> Result<Vec<DefineLoginStatement>, Error> {
let beg = crate::key::nl::new(ns, crate::key::PREFIX); let beg = crate::key::nl::prefix(ns);
let end = crate::key::nl::new(ns, crate::key::SUFFIX); let end = crate::key::nl::suffix(ns);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
Ok(val.convert()) Ok(val.convert())
} }
// Get all namespace tokens // Get all namespace tokens
pub async fn all_nt(&mut self, ns: &str) -> Result<Vec<DefineTokenStatement>, Error> { pub async fn all_nt(&mut self, ns: &str) -> Result<Vec<DefineTokenStatement>, Error> {
let beg = crate::key::nt::new(ns, crate::key::PREFIX); let beg = crate::key::nt::prefix(ns);
let end = crate::key::nt::new(ns, crate::key::SUFFIX); let end = crate::key::nt::suffix(ns);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
Ok(val.convert()) Ok(val.convert())
} }
// Get all databases // Get all databases
pub async fn all_db(&mut self, ns: &str) -> Result<Vec<DefineDatabaseStatement>, Error> { pub async fn all_db(&mut self, ns: &str) -> Result<Vec<DefineDatabaseStatement>, Error> {
let beg = crate::key::db::new(ns, crate::key::PREFIX); let beg = crate::key::db::prefix(ns);
let end = crate::key::db::new(ns, crate::key::SUFFIX); let end = crate::key::db::suffix(ns);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
Ok(val.convert()) Ok(val.convert())
} }
// Get all database logins // Get all database logins
pub async fn all_dl(&mut self, ns: &str, db: &str) -> Result<Vec<DefineLoginStatement>, Error> { pub async fn all_dl(&mut self, ns: &str, db: &str) -> Result<Vec<DefineLoginStatement>, Error> {
let beg = crate::key::dl::new(ns, db, crate::key::PREFIX); let beg = crate::key::dl::prefix(ns, db);
let end = crate::key::dl::new(ns, db, crate::key::SUFFIX); let end = crate::key::dl::suffix(ns, db);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
Ok(val.convert()) Ok(val.convert())
} }
// Get all database tokens // Get all database tokens
pub async fn all_dt(&mut self, ns: &str, db: &str) -> Result<Vec<DefineTokenStatement>, Error> { pub async fn all_dt(&mut self, ns: &str, db: &str) -> Result<Vec<DefineTokenStatement>, Error> {
let beg = crate::key::dt::new(ns, db, crate::key::PREFIX); let beg = crate::key::dt::prefix(ns, db);
let end = crate::key::dt::new(ns, db, crate::key::SUFFIX); let end = crate::key::dt::suffix(ns, db);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
Ok(val.convert()) Ok(val.convert())
} }
// Get all scopes // Get all scopes
pub async fn all_sc(&mut self, ns: &str, db: &str) -> Result<Vec<DefineScopeStatement>, Error> { pub async fn all_sc(&mut self, ns: &str, db: &str) -> Result<Vec<DefineScopeStatement>, Error> {
let beg = crate::key::sc::new(ns, db, crate::key::PREFIX); let beg = crate::key::sc::prefix(ns, db);
let end = crate::key::sc::new(ns, db, crate::key::SUFFIX); let end = crate::key::sc::suffix(ns, db);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
Ok(val.convert()) Ok(val.convert())
} }
@ -84,15 +84,15 @@ impl Transaction {
db: &str, db: &str,
sc: &str, sc: &str,
) -> Result<Vec<DefineTokenStatement>, Error> { ) -> Result<Vec<DefineTokenStatement>, Error> {
let beg = crate::key::st::new(ns, db, sc, crate::key::PREFIX); let beg = crate::key::st::prefix(ns, db, sc);
let end = crate::key::st::new(ns, db, sc, crate::key::SUFFIX); let end = crate::key::st::suffix(ns, db, sc);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
Ok(val.convert()) Ok(val.convert())
} }
// Get all tables // Get all tables
pub async fn all_tb(&mut self, ns: &str, db: &str) -> Result<Vec<DefineTableStatement>, Error> { pub async fn all_tb(&mut self, ns: &str, db: &str) -> Result<Vec<DefineTableStatement>, Error> {
let beg = crate::key::tb::new(ns, db, crate::key::PREFIX); let beg = crate::key::tb::prefix(ns, db);
let end = crate::key::tb::new(ns, db, crate::key::SUFFIX); let end = crate::key::tb::suffix(ns, db);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
Ok(val.convert()) Ok(val.convert())
} }
@ -103,8 +103,8 @@ impl Transaction {
db: &str, db: &str,
tb: &str, tb: &str,
) -> Result<Vec<DefineEventStatement>, Error> { ) -> Result<Vec<DefineEventStatement>, Error> {
let beg = crate::key::ev::new(ns, db, tb, crate::key::PREFIX); let beg = crate::key::ev::prefix(ns, db, tb);
let end = crate::key::ev::new(ns, db, tb, crate::key::SUFFIX); let end = crate::key::ev::suffix(ns, db, tb);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
Ok(val.convert()) Ok(val.convert())
} }
@ -115,8 +115,8 @@ impl Transaction {
db: &str, db: &str,
tb: &str, tb: &str,
) -> Result<Vec<DefineFieldStatement>, Error> { ) -> Result<Vec<DefineFieldStatement>, Error> {
let beg = crate::key::fd::new(ns, db, tb, crate::key::PREFIX); let beg = crate::key::fd::prefix(ns, db, tb);
let end = crate::key::fd::new(ns, db, tb, crate::key::SUFFIX); let end = crate::key::fd::suffix(ns, db, tb);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
Ok(val.convert()) Ok(val.convert())
} }
@ -127,8 +127,8 @@ impl Transaction {
db: &str, db: &str,
tb: &str, tb: &str,
) -> Result<Vec<DefineIndexStatement>, Error> { ) -> Result<Vec<DefineIndexStatement>, Error> {
let beg = crate::key::ix::new(ns, db, tb, crate::key::PREFIX); let beg = crate::key::ix::prefix(ns, db, tb);
let end = crate::key::ix::new(ns, db, tb, crate::key::SUFFIX); let end = crate::key::ix::suffix(ns, db, tb);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
Ok(val.convert()) Ok(val.convert())
} }
@ -139,8 +139,8 @@ impl Transaction {
db: &str, db: &str,
tb: &str, tb: &str,
) -> Result<Vec<DefineTableStatement>, Error> { ) -> Result<Vec<DefineTableStatement>, Error> {
let beg = crate::key::ft::new(ns, db, tb, crate::key::PREFIX); let beg = crate::key::ft::prefix(ns, db, tb);
let end = crate::key::ft::new(ns, db, tb, crate::key::SUFFIX); let end = crate::key::ft::suffix(ns, db, tb);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
Ok(val.convert()) Ok(val.convert())
} }
@ -151,8 +151,8 @@ impl Transaction {
db: &str, db: &str,
tb: &str, tb: &str,
) -> Result<Vec<LiveStatement>, Error> { ) -> Result<Vec<LiveStatement>, Error> {
let beg = crate::key::lv::new(ns, db, tb, crate::key::PREFIX); let beg = crate::key::lv::prefix(ns, db, tb);
let end = crate::key::lv::new(ns, db, tb, crate::key::SUFFIX); let end = crate::key::lv::suffix(ns, db, tb);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
Ok(val.convert()) Ok(val.convert())
} }