add more encodings

main
Ziyang Hu 2 years ago
parent dd7a59d258
commit 45522ee043

@ -53,6 +53,8 @@ fn compare_key(a: &[u8], b: &[u8]) -> Ordering {
Tx => compare_key_tx(a, b),
UniqueEntity => compare_key_unique_entity(a, b),
UniqueAttrValue => compare_key_unique_attr_val(a, b),
UniqueAttrById => compare_key_unique_attr_by_id(a, b),
UniqueAttrByKeyword => compare_key_unique_attr_by_kw(a, b),
}
}
@ -145,9 +147,7 @@ fn compare_key_tx(a: &[u8], b: &[u8]) -> Ordering {
#[inline]
fn compare_key_unique_entity(a: &[u8], b: &[u8]) -> Ordering {
let a_e = EntityId::from_bytes(a);
let b_e = EntityId::from_bytes(b);
a_e.cmp(&b_e)
a.cmp(b)
}
#[inline]
@ -157,3 +157,13 @@ fn compare_key_unique_attr_val(a: &[u8], b: &[u8]) -> Ordering {
return_if_resolved!(a_a.cmp(&b_a));
a_v.cmp(&b_v)
}
#[inline]
fn compare_key_unique_attr_by_id(a: &[u8], b: &[u8]) -> Ordering {
a.cmp(b)
}
#[inline]
fn compare_key_unique_attr_by_kw(a: &[u8], b: &[u8]) -> Ordering {
a.cmp(b)
}

@ -1,4 +1,3 @@
use crate::data::encode::StorageTag::Tx;
use crate::data::id::{AttrId, EntityId, TxId};
use crate::data::keyword::Keyword;
use crate::data::triple::StoreOp;
@ -21,6 +20,8 @@ pub(crate) enum StorageTag {
Tx = 7,
UniqueEntity = 8,
UniqueAttrValue = 9,
UniqueAttrById = 10,
UniqueAttrByKeyword = 11,
}
#[derive(Debug, thiserror::Error)]
@ -289,3 +290,31 @@ pub(crate) fn decode_unique_attr_val(src: &[u8]) -> Result<(AttrId, Value)> {
let val = rmp_serde::from_slice(&src[4..])?;
Ok((a_id, val))
}
#[inline]
pub(crate) fn encode_unique_attr_by_id(aid: AttrId) -> impl Deref<Target = [u8]> {
let mut ret = SmallVec::<[u8; 4]>::new();
ret.extend(aid.0.to_be_bytes());
ret[0] = StorageTag::UniqueAttrById as u8;
debug_assert_eq!(ret.len(), 4);
ret
}
pub(crate) fn decode_unique_attr_by_id(src: &[u8]) -> Result<AttrId> {
Ok(AttrId::from_bytes(src))
}
#[inline]
pub(crate) fn encode_unique_attr_by_kw(kw: Keyword) -> impl Deref<Target = [u8]> {
let mut ret = SmallVec::<[u8; 60]>::new();
ret.push(StorageTag::UniqueAttrByKeyword as u8);
ret.extend_from_slice(kw.ns.as_bytes());
ret.push(b'/');
ret.extend_from_slice(kw.ident.as_bytes());
ret
}
#[inline]
pub(crate) fn decode_unique_attr_by_kw(src: &[u8]) -> Result<Keyword> {
Ok(Keyword::try_from(&src[1..])?)
}

@ -13,7 +13,9 @@ pub enum KeywordError {
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Debug, Deserialize, Serialize)]
pub struct Keyword {
#[serde(rename = "n")]
pub(crate) ns: SmartString<LazyCompact>,
#[serde(rename = "i")]
pub(crate) ident: SmartString<LazyCompact>,
}

Loading…
Cancel
Save