From dd39abffc2b504c001e99ca4f89765d2033b6531 Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Sun, 17 Jul 2022 23:59:34 +0800 Subject: [PATCH] cleanup --- src/data/attr.rs | 8 -------- src/data/id.rs | 7 +------ src/data/value.rs | 6 ------ src/preprocess/attr.rs | 4 ++-- src/preprocess/pull.rs | 15 +++++++++------ src/preprocess/triple.rs | 41 ++++++++++++++-------------------------- src/runtime/db.rs | 8 ++++---- src/transact/pull.rs | 26 ++++++++++++------------- src/transact/triple.rs | 2 -- 9 files changed, 43 insertions(+), 74 deletions(-) diff --git a/src/data/attr.rs b/src/data/attr.rs index f438b487..7b46e1ae 100644 --- a/src/data/attr.rs +++ b/src/data/attr.rs @@ -217,9 +217,6 @@ impl AttributeIndex { pub(crate) fn is_unique_index(&self) -> bool { matches!(self, AttributeIndex::Identity | AttributeIndex::Unique) } - pub(crate) fn is_non_unique_index(&self) -> bool { - *self == AttributeIndex::Indexed - } pub(crate) fn should_index(&self) -> bool { *self != AttributeIndex::None } @@ -285,11 +282,6 @@ impl Attribute { self.serialize(&mut Serializer::new(&mut inner)).unwrap(); EncodedVec { inner } } - pub(crate) fn encode(&self) -> EncodedVec { - let mut inner = SmallVec::<[u8; ATTR_VEC_SIZE]>::new(); - self.serialize(&mut Serializer::new(&mut inner)).unwrap(); - EncodedVec { inner } - } pub(crate) fn decode(data: &[u8]) -> Result { Ok(rmp_serde::from_slice(data)?) } diff --git a/src/data/id.rs b/src/data/id.rs index 19cc44ac..a7081ca2 100644 --- a/src/data/id.rs +++ b/src/data/id.rs @@ -3,8 +3,8 @@ use std::time::{SystemTime, UNIX_EPOCH}; use chrono::{DateTime, TimeZone, Utc}; use serde_derive::{Deserialize, Serialize}; -use crate::data::json::JsonValue; +use crate::data::json::JsonValue; use crate::data::triple::StoreOp; #[derive(Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Deserialize, Serialize, Hash)] @@ -88,7 +88,6 @@ impl Debug for Validity { pub struct EntityId(pub u64); impl EntityId { - pub(crate) const MAX_SYS: EntityId = EntityId(1000); pub(crate) const MAX_TEMP: EntityId = EntityId(10_000_000); pub const MIN_PERM: EntityId = EntityId(10_000_001); pub const MAX_PERM: EntityId = EntityId(0x00ff_ffff_ff00_0000); @@ -122,7 +121,6 @@ impl Debug for EntityId { pub struct AttrId(pub u64); impl AttrId { - pub(crate) const MAX_SYS: AttrId = AttrId(1000); pub(crate) const MAX_TEMP: AttrId = AttrId(10_000_000); pub(crate) const MIN_PERM: AttrId = AttrId(10_000_001); pub(crate) const MAX_PERM: AttrId = AttrId(0x00ff_ffff_ff00_0000); @@ -157,10 +155,7 @@ impl Debug for AttrId { pub struct TxId(pub u64); impl TxId { - pub(crate) const ZERO: TxId = TxId(0); - pub(crate) const NO_HISTORY: TxId = TxId(1000); pub(crate) const MAX_SYS: TxId = TxId(10000); - pub(crate) const MIN_USER: TxId = TxId(10001); pub(crate) const MAX_USER: TxId = TxId(0x00ff_ffff_ffff_ffff); pub(crate) fn from_bytes(b: &[u8]) -> Self { diff --git a/src/data/value.rs b/src/data/value.rs index 015f8438..d2f73901 100644 --- a/src/data/value.rs +++ b/src/data/value.rs @@ -93,12 +93,6 @@ impl<'a> Value<'a> { ret.into() } - pub(crate) fn encode(&self) -> EncodedVec { - let mut ret = SmallVec::<[u8; INLINE_VAL_SIZE_LIMIT]>::new(); - self.serialize(&mut Serializer::new(&mut ret)).unwrap(); - ret.into() - } - pub(crate) fn get_entity_id(&self) -> Result { match self { Value::EnId(id) => Ok(*id), diff --git a/src/preprocess/attr.rs b/src/preprocess/attr.rs index 6aa63c9d..430cba27 100644 --- a/src/preprocess/attr.rs +++ b/src/preprocess/attr.rs @@ -31,7 +31,7 @@ impl AttrTxItem { req.clone(), "'attrs' cannot be empty".to_string(), ) - .into()); + .into()); } let res = items.iter().map(AttrTxItem::try_from).try_collect()?; Ok((res, comment)) @@ -56,7 +56,7 @@ impl TryFrom<&'_ JsonValue> for AttrTxItem { value.clone(), "object must have exactly one field".to_string(), ) - .into()); + .into()); } let (k, v) = map.into_iter().next().unwrap(); let op = match k as &str { diff --git a/src/preprocess/pull.rs b/src/preprocess/pull.rs index eae61b15..c29505a9 100644 --- a/src/preprocess/pull.rs +++ b/src/preprocess/pull.rs @@ -21,10 +21,13 @@ pub enum PullError { impl SessionTx { pub(crate) fn parse_pull(&mut self, desc: &JsonValue, depth: usize) -> Result { if let Some(inner) = desc.as_array() { - inner + let mut ret: PullSpecs = inner .iter() .map(|v| self.parse_pull_element(v, depth)) - .try_collect() + .try_collect()?; + // the sort is necessary to put recursive queries last + ret.sort(); + Ok(ret) } else { Err(PullError::InvalidFormat(desc.clone(), "expect array".to_string()).into()) } @@ -123,7 +126,7 @@ impl SessionTx { JsonValue::Object(desc.clone()), "expect boolean or number".to_string(), ) - .into()); + .into()); } recursive = true; } @@ -141,7 +144,7 @@ impl SessionTx { JsonValue::Object(desc.clone()), "expect array".to_string(), ) - .into()); + .into()); } }; } @@ -150,7 +153,7 @@ impl SessionTx { v.into(), "unexpected spec key".to_string(), ) - .into()) + .into()); } } } @@ -166,7 +169,7 @@ impl SessionTx { JsonValue::Object(desc.clone()), "expect target key".to_string(), ) - .into()); + .into()); } let input_kw = input_kw.unwrap(); diff --git a/src/preprocess/triple.rs b/src/preprocess/triple.rs index 386757ab..c66d61af 100644 --- a/src/preprocess/triple.rs +++ b/src/preprocess/triple.rs @@ -179,7 +179,7 @@ impl SessionTx { JsonValue::Object(item.clone()), "expect any of the keys 'put', 'retract', 'erase', 'ensure'".to_string(), ) - .into()); + .into()); } }; let since = match item.get("since") { @@ -229,7 +229,7 @@ impl SessionTx { action, "using temp id instead of perm id".to_string(), ) - .into()); + .into()); } let v = if let JsonValue::Object(inner) = value { @@ -264,7 +264,7 @@ impl SessionTx { action, "component shorthand cannot be used".to_string(), ) - .into()); + .into()); } let (eid, has_unique_attr) = self.parse_tx_request_obj(comp, true, action, since, temp_id_ctx, collected)?; @@ -289,7 +289,7 @@ impl SessionTx { action, "singlet only allowed for 'retract'".to_string(), ) - .into()); + .into()); } let eid = eid.as_u64().ok_or_else(|| { TxError::Decoding(eid.clone(), "cannot parse as entity id".to_string()) @@ -317,7 +317,7 @@ impl SessionTx { action, "doublet only allowed for 'retract'".to_string(), ) - .into()); + .into()); } let kw: Keyword = attr.try_into()?; let attr = self.attr_by_kw(&kw)?.ok_or(TxError::AttrNotFound(kw))?; @@ -399,7 +399,7 @@ impl SessionTx { id.0, "conflicting id for identity value".into(), ) - .into()); + .into()); } id } else if eid.is_string() { @@ -407,7 +407,7 @@ impl SessionTx { existing_id.0, "specifying temp_id string together with unique constraint".into(), ) - .into()); + .into()); } else { existing_id } @@ -479,7 +479,7 @@ impl SessionTx { existing_eid.0, "conflicting entity id given".to_string(), ) - .into()); + .into()); } } eid = Some(existing_eid) @@ -502,7 +502,7 @@ impl SessionTx { given_id.0, "temp id given where perm id is required".to_string(), ) - .into()); + .into()); } if let Some(prev_id) = eid { if prev_id != given_id { @@ -510,7 +510,7 @@ impl SessionTx { given_id.0, "conflicting entity id given".to_string(), ) - .into()); + .into()); } } eid = Some(given_id); @@ -521,7 +521,7 @@ impl SessionTx { eid_inner.0, "conflicting entity id given".to_string(), ) - .into()); + .into()); } let temp_id_str = temp_id.as_str().ok_or_else(|| { TxError::Decoding( @@ -544,7 +544,7 @@ impl SessionTx { action, "upsert requires identity attribute present".to_string(), ) - .into()); + .into()); } for (attr, v) in pairs { self.parse_tx_request_inner(eid, &attr, v, action, since, temp_id_ctx, collected)?; @@ -560,23 +560,10 @@ impl SessionTx { action, "cannot use non-unique fields to specify entity".to_string(), ) - .into()); + .into()); } } } Ok((eid, has_unique_attr)) } -} - -fn assert_absence_of_keys(m: &Map, keys: &[&str]) -> Result<()> { - for k in keys { - if m.contains_key(*k) { - return Err(TxError::Decoding( - JsonValue::Object(m.clone()), - format!("object must not contain key {}", k), - ) - .into()); - } - } - Ok(()) -} +} \ No newline at end of file diff --git a/src/runtime/db.rs b/src/runtime/db.rs index f33fb744..31df88c2 100644 --- a/src/runtime/db.rs +++ b/src/runtime/db.rs @@ -1,7 +1,7 @@ use std::collections::BTreeMap; use std::fmt::{Debug, Formatter}; -use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; use std::sync::Arc; +use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; use anyhow::Result; use itertools::Itertools; @@ -9,7 +9,8 @@ use serde_json::json; use cozorocks::{DbBuilder, DbIter, RocksDb}; -use crate::data::compare::{rusty_cmp, DB_KEY_PREFIX_LEN}; +use crate::AttrTxItem; +use crate::data::compare::{DB_KEY_PREFIX_LEN, rusty_cmp}; use crate::data::encode::{ decode_ea_key, decode_value_from_key, decode_value_from_val, encode_eav_key, StorageTag, }; @@ -19,7 +20,6 @@ use crate::data::triple::StoreOp; use crate::data::value::Value; use crate::runtime::transact::SessionTx; use crate::transact::pull::CurrentPath; -use crate::AttrTxItem; pub struct Db { db: RocksDb, @@ -132,7 +132,7 @@ impl Db { spec, 0, &specs, - CurrentPath::new(idx), + CurrentPath::new(idx)?, &mut collected, &mut recursive_seen, )?; diff --git a/src/transact/pull.rs b/src/transact/pull.rs index be4a2dfe..7e3519ef 100644 --- a/src/transact/pull.rs +++ b/src/transact/pull.rs @@ -17,7 +17,7 @@ use crate::runtime::transact::SessionTx; pub(crate) type PullSpecs = Vec; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)] pub(crate) enum PullSpec { PullAll, PullId(Keyword), @@ -33,40 +33,40 @@ impl PullSpec { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)] pub(crate) struct AttrPullSpec { + pub(crate) recursive: bool, + pub(crate) reverse: bool, pub(crate) attr: Attribute, pub(crate) default_val: StaticValue, - pub(crate) reverse: bool, pub(crate) name: Keyword, pub(crate) cardinality: AttributeCardinality, pub(crate) take: Option, pub(crate) nested: PullSpecs, - pub(crate) recursive: bool, pub(crate) recursion_limit: Option, pub(crate) recursion_depth: usize, } #[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Debug)] -pub(crate) struct CurrentPath(SmallVec<[usize; 8]>); +pub(crate) struct CurrentPath(SmallVec<[u16; 8]>); impl CurrentPath { - pub(crate) fn new(idx: usize) -> Self { - Self(smallvec![idx]) + pub(crate) fn new(idx: usize) -> Result { + Ok(Self(smallvec![idx.try_into()?])) } fn get_from_root<'a>(&self, depth: usize, root: &'a PullSpecs) -> &'a PullSpecs { let mut current = root; let indices = &self.0[..self.0.len() - depth]; for i in indices { - current = ¤t[*i].as_attr_spec().unwrap().nested; + current = ¤t[*i as usize].as_attr_spec().unwrap().nested; } current } - fn push(&self, idx: usize) -> Self { + fn push(&self, idx: usize) -> Result { let mut ret = CurrentPath(Default::default()); ret.0.clone_from(&self.0); - ret.0.push(idx); - ret + ret.0.push(idx.try_into()?); + Ok(ret) } fn recurse_pop(&self, depth: usize) -> Self { Self(self.0[..self.0.len() + 1 - depth].to_smallvec()) @@ -255,7 +255,7 @@ impl SessionTx { sub_spec, depth, root, - path.push(idx), + path.push(idx)?, &mut sub_collector, recursive_seen, )?; @@ -334,7 +334,7 @@ impl SessionTx { sub_spec, depth, root, - path.push(idx), + path.push(idx)?, &mut sub_collector, recursive_seen, )?; diff --git a/src/transact/triple.rs b/src/transact/triple.rs index 8aa9bc52..f3aad9a1 100644 --- a/src/transact/triple.rs +++ b/src/transact/triple.rs @@ -24,8 +24,6 @@ use crate::utils::swap_option_result; enum TripleError { #[error("use of temp entity id: {0:?}")] TempEid(EntityId), - #[error("use of non-existent entity: {0:?}")] - EidNotFound(EntityId), #[error("unique constraint violated: {0} {1}")] UniqueConstraintViolated(Keyword, String), #[error("triple not found for {0:?} {1:?} {2:?}")]