From fff799f44ea362dbaf9bb4e202da038f9aad4091 Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Mon, 4 Jul 2022 19:12:24 +0800 Subject: [PATCH] attribute and json --- src/data/attr.rs | 29 ++++++++++++++++++++++++++--- src/data/keyword.rs | 10 ++++++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/data/attr.rs b/src/data/attr.rs index 7cf8cbb6..b0569b96 100644 --- a/src/data/attr.rs +++ b/src/data/attr.rs @@ -238,11 +238,11 @@ pub(crate) struct Attribute { pub(crate) with_history: bool, } -const LARGE_VEC_SIZE: usize = 60; +const ATTR_VEC_SIZE: usize = 80; impl Attribute { - pub(crate) fn encode(&self) -> Encoded { - let mut inner = SmallVec::<[u8; LARGE_VEC_SIZE]>::new(); + pub(crate) fn encode(&self) -> Encoded { + let mut inner = SmallVec::<[u8; ATTR_VEC_SIZE]>::new(); self.serialize(&mut Serializer::new(&mut inner)).unwrap(); Encoded { inner } } @@ -250,3 +250,26 @@ impl Attribute { Ok(rmp_serde::from_slice(data)?) } } + +#[cfg(test)] +mod tests { + use crate::data::attr::{Attribute, AttributeCardinality, AttributeIndex, AttributeTyping}; + use crate::data::id::AttrId; + use crate::data::keyword::Keyword; + + #[test] + fn show_sizes() { + let attr = Attribute { + id: AttrId(0), + alias: Keyword::try_from("01234567890123456789012/01234567890123456789012").unwrap(), + cardinality: AttributeCardinality::One, + val_type: AttributeTyping::Ref, + indexing: AttributeIndex::None, + with_history: false, + }; + let encoded = attr.encode(); + dbg!(encoded.len()); + dbg!("01234567890123456789012".as_bytes().len()); + dbg!(Attribute::decode(&encoded).unwrap()); + } +} diff --git a/src/data/keyword.rs b/src/data/keyword.rs index c7bfd562..29bb1fe6 100644 --- a/src/data/keyword.rs +++ b/src/data/keyword.rs @@ -1,6 +1,6 @@ use serde_derive::{Deserialize, Serialize}; use smartstring::{LazyCompact, SmartString}; -use std::fmt::{Display, Formatter}; +use std::fmt::{Debug, Display, Formatter}; use std::str::Utf8Error; #[derive(Debug, thiserror::Error)] @@ -12,7 +12,7 @@ pub enum KeywordError { Utf8(#[from] Utf8Error), } -#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Debug, Deserialize, Serialize)] +#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Deserialize, Serialize)] pub struct Keyword { #[serde(rename = "n")] pub(crate) ns: SmartString, @@ -26,6 +26,12 @@ impl Display for Keyword { } } +impl Debug for Keyword { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, ":{}", self) + } +} + impl TryFrom<&str> for Keyword { type Error = KeywordError; fn try_from(value: &str) -> Result {