From 4c95db254a36f16825782fff5fbd20ebfc314325 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 23 Mar 2022 14:01:04 +0000 Subject: [PATCH] Add datastore key type for graph edges --- lib/src/key/graph.rs | 101 +++++++++++++++++++++++++++++++++++++++++++ lib/src/key/mod.rs | 1 + lib/src/sql/test.rs | 7 +++ 3 files changed, 109 insertions(+) create mode 100644 lib/src/key/graph.rs diff --git a/lib/src/key/graph.rs b/lib/src/key/graph.rs new file mode 100644 index 00000000..e26d631f --- /dev/null +++ b/lib/src/key/graph.rs @@ -0,0 +1,101 @@ +use crate::err::Error; +use crate::sql::graph::Dir; +use crate::sql::id::Id; +use crate::sql::thing::Thing; +use serde::{Deserialize, Serialize}; +use storekey::{deserialize, serialize}; + +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] +pub struct Graph { + __: u8, + _a: u8, + pub ns: String, + _b: u8, + pub db: String, + _c: u8, + pub tb: String, + _d: u8, + pub id: Id, + pub eg: Dir, + pub fk: Thing, +} + +impl From for Vec { + fn from(val: Graph) -> Vec { + val.encode().unwrap() + } +} + +impl From> for Graph { + fn from(val: Vec) -> Self { + Graph::decode(&val).unwrap() + } +} + +pub fn new(ns: &str, db: &str, tb: &str, id: &Id, eg: &Dir, fk: &Thing) -> Graph { + Graph::new( + ns.to_string(), + db.to_string(), + tb.to_string(), + id.to_owned(), + eg.to_owned(), + fk.to_owned(), + ) +} + +pub fn prefix(ns: &str, db: &str, tb: &str, id: &Id) -> Vec { + let mut k = super::thing::new(ns, db, tb, id).encode().unwrap(); + k.extend_from_slice(&[0x00]); + k +} + +pub fn suffix(ns: &str, db: &str, tb: &str, id: &Id) -> Vec { + let mut k = super::thing::new(ns, db, tb, id).encode().unwrap(); + k.extend_from_slice(&[0xff]); + k +} + +impl Graph { + pub fn new(ns: String, db: String, tb: String, id: Id, eg: Dir, fk: Thing) -> Graph { + Graph { + __: 0x2f, // / + _a: 0x2a, // * + ns, + _b: 0x2a, // * + db, + _c: 0x2a, // * + tb, + _d: 0x7e, // ~ + id, + eg, + fk, + } + } + 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::*; + use crate::sql::test::Parse; + #[rustfmt::skip] + let val = Graph::new( + "test".to_string(), + "test".to_string(), + "test".to_string(), + "test".into(), + Dir::Out, + Thing::parse("other:test"), + ); + let enc = Graph::encode(&val).unwrap(); + let dec = Graph::decode(&enc).unwrap(); + assert_eq!(val, dec); + } +} diff --git a/lib/src/key/mod.rs b/lib/src/key/mod.rs index b7171c9b..cf709fe9 100644 --- a/lib/src/key/mod.rs +++ b/lib/src/key/mod.rs @@ -35,6 +35,7 @@ pub mod dt; pub mod ev; pub mod fd; pub mod ft; +pub mod graph; pub mod guide; pub mod index; pub mod ix; diff --git a/lib/src/sql/test.rs b/lib/src/sql/test.rs index 6249ff11..e5664618 100644 --- a/lib/src/sql/test.rs +++ b/lib/src/sql/test.rs @@ -3,6 +3,7 @@ use crate::sql::expression::{expression, Expression}; use crate::sql::idiom::{idiom, Idiom}; use crate::sql::param::{param, Param}; use crate::sql::script::{script, Script}; +use crate::sql::thing::{thing, Thing}; use crate::sql::value::{value, Value}; pub trait Parse { @@ -39,6 +40,12 @@ impl Parse