From 3665e7750a1e14c6fdfc3f700940dbeb1edbdb5e Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Mon, 25 Jul 2022 17:24:54 +0800 Subject: [PATCH] formatting --- src/data/value.rs | 52 +++++++++++++++++++++++++- src/preprocess/query.rs | 35 +++++++++++++++--- src/transact/query.rs | 78 ++++++++++++++++++++++++++++++++++++--- src/transact/throwaway.rs | 8 +++- 4 files changed, 158 insertions(+), 15 deletions(-) diff --git a/src/data/value.rs b/src/data/value.rs index c38379b7..fbf3f80a 100644 --- a/src/data/value.rs +++ b/src/data/value.rs @@ -1,5 +1,5 @@ use std::cmp::Reverse; -use std::fmt::Debug; +use std::fmt::{Binary, Debug, Display, Formatter, Pointer}; use anyhow::Result; use ordered_float::OrderedFloat; @@ -23,7 +23,7 @@ pub enum ValueError { TypeMismatch(String, String), } -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Deserialize, Serialize)] +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)] pub enum DataValue { #[serde(rename = "n")] Null, @@ -54,6 +54,54 @@ pub enum DataValue { Bottom, } +impl Debug for DataValue { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + DataValue::Null => { + write!(f, "null") + } + DataValue::Bool(b) => { + write!(f, "{}", b) + } + DataValue::EnId(id) => { + id.fmt(f) + } + DataValue::Int(i) => { + write!(f, "{}", i) + } + DataValue::Float(n) => { + write!(f, "{}", n.0) + } + DataValue::Keyword(k) => { + write!(f, "{:?}", k) + } + DataValue::String(s) => { + write!(f, "{:?}", s) + } + DataValue::Uuid(u) => { + write!(f, "{}", u) + } + DataValue::Timestamp(ts) => { + write!(f, "ts@{}", ts) + } + DataValue::Bytes(b) => { + write!(f, "bytes(len={})", b.len()) + } + DataValue::Tuple(t) => { + f.debug_list() + .entries(t.iter()) + .finish() + } + DataValue::DescVal(v) => { + write!(f, "desc<{:?}>", v) + } + DataValue::Bottom => { + write!(f, "bottom") + } + } + } +} + pub(crate) const INLINE_VAL_SIZE_LIMIT: usize = 60; impl DataValue { diff --git a/src/preprocess/query.rs b/src/preprocess/query.rs index fcfeefc9..9e3b1411 100644 --- a/src/preprocess/query.rs +++ b/src/preprocess/query.rs @@ -1,5 +1,6 @@ use std::collections::btree_map::Entry; use std::collections::{BTreeMap, BTreeSet}; +use std::fmt::{Debug, Formatter}; use std::mem; use std::ops::Sub; @@ -137,11 +138,26 @@ pub enum Aggregation { #[derive(Clone, Debug)] pub(crate) struct Rule { - pub(crate) head: Vec<(Keyword, Aggregation)>, + pub(crate) head: Vec, pub(crate) body: Vec, pub(crate) vld: Validity, } +#[derive(Clone, Debug)] +pub(crate) struct BindingHeadTerm { + name: Keyword, + aggr: Aggregation +} + +struct BindingHeadFormatter<'a>(&'a [BindingHeadTerm]); + +impl Debug for BindingHeadFormatter<'_> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let s = self.0.iter().map(|h| h.name.to_string_no_prefix()).join(", "); + write!(f, "[{}]", s) + } +} + impl SessionTx { pub fn semi_naive_evaluate(&mut self, prog: &DatalogProgram) -> Result { let stores = prog @@ -158,11 +174,11 @@ impl SessionTx { .map( |(k, body)| -> Result<( Keyword, - Vec<(Vec<(Keyword, Aggregation)>, BTreeSet, Relation)>, + Vec<(Vec, BTreeSet, Relation)>, )> { let mut collected = Vec::with_capacity(body.sets.len()); for rule in &body.sets { - let header = rule.head.iter().map(|(k, v)| k).cloned().collect_vec(); + let header = rule.head.iter().map(|t| &t.name).cloned().collect_vec(); let relation = self.compile_rule_body(&rule.body, rule.vld, &stores, &header)?; collected.push((rule.head.clone(), rule.contained_rules(), relation)); @@ -172,7 +188,11 @@ impl SessionTx { ) .try_collect()?; - // dbg!(&compiled); + for (k, vs) in compiled.iter() { + for (i, (binding, _, rel)) in vs.iter().enumerate() { + eprintln!("{}.{} {:?}: {:#?}", k, i, BindingHeadFormatter(binding), rel) + } + } let mut changed: BTreeMap<_, _> = compiled.keys().map(|k| (k, false)).collect(); let mut prev_changed = changed.clone(); @@ -384,9 +404,12 @@ impl SessionTx { })?; let rule_head = rule_head .iter() - .map(|el| -> Result<(Keyword, Aggregation)> { + .map(|el| -> Result { if let Some(s) = el.as_str() { - Ok((Keyword::from(s), Default::default())) + Ok(BindingHeadTerm { + name: Keyword::from(s), + aggr: Default::default() + }) } else { todo!() } diff --git a/src/transact/query.rs b/src/transact/query.rs index 94d1f55d..e53664d8 100644 --- a/src/transact/query.rs +++ b/src/transact/query.rs @@ -1,4 +1,5 @@ use std::collections::{BTreeMap, BTreeSet}; +use std::fmt::{Debug, Formatter}; use std::iter; use anyhow::Result; @@ -13,7 +14,6 @@ use crate::runtime::transact::SessionTx; use crate::transact::throwaway::{ThrowawayArea, ThrowawayId}; use crate::Validity; -#[derive(Debug)] pub enum Relation { Fixed(InlineFixedRelation), Triple(TripleRelation), @@ -22,6 +22,65 @@ pub enum Relation { Reorder(ReorderRelation), } +struct BindingFormatter<'a>(&'a [Keyword]); + +impl Debug for BindingFormatter<'_> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let s = self.0.iter().map(|f| f.to_string_no_prefix()).join(", "); + write!(f, "[{}]", s) + } +} + +impl Debug for Relation { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + Relation::Fixed(r) => { + if r.bindings.is_empty() && r.data.len() == 1 { + f.write_str("Unit") + } else if r.data.len() == 1 { + f.debug_tuple("Singlet") + .field(&BindingFormatter(&r.bindings)) + .field(r.data.get(0).unwrap()) + .finish() + } else { + f.debug_tuple("Fixed") + .field(&BindingFormatter(&r.bindings)) + .field(&["..."]) + .finish() + } + } + Relation::Triple(r) => f + .debug_tuple("Triple") + .field(&BindingFormatter(&r.bindings)) + .field(&r.attr.keyword) + .finish(), + Relation::Derived(r) => f + .debug_tuple("Derived") + .field(&BindingFormatter(&r.bindings)) + .field(&r.storage.id) + .finish(), + Relation::Join(r) => { + if r.left.is_unit() { + r.right.fmt(f) + } else { + f + .debug_tuple("Join") + .field(&BindingFormatter(&r.bindings())) + .field(&r.joiner) + .field(&r.left) + .field(&r.right) + .finish() + } + }, + Relation::Reorder(r) => f + .debug_tuple("Reorder") + .field(&r.new_order) + .field(&r.relation) + .finish(), + } + } +} + impl Relation { pub(crate) fn unit() -> Self { Self::Fixed(InlineFixedRelation::unit()) @@ -37,10 +96,7 @@ impl Relation { self.join(right, vec![], vec![]) } pub(crate) fn derived(bindings: Vec, storage: ThrowawayArea) -> Self { - Self::Derived(StoredDerivedRelation { - bindings, - storage - }) + Self::Derived(StoredDerivedRelation { bindings, storage }) } pub(crate) fn triple( attr: Attribute, @@ -653,13 +709,23 @@ impl StoredDerivedRelation { } } -#[derive(Debug)] pub(crate) struct Joiner { // invariant: these are of the same lengths pub(crate) left_keys: Vec, pub(crate) right_keys: Vec, } +impl Debug for Joiner { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{:?}<->{:?}", + BindingFormatter(&self.left_keys), + BindingFormatter(&self.right_keys) + ) + } +} + impl Joiner { pub(crate) fn join_indices( &self, diff --git a/src/transact/throwaway.rs b/src/transact/throwaway.rs index 633cf6e3..5fe91793 100644 --- a/src/transact/throwaway.rs +++ b/src/transact/throwaway.rs @@ -5,9 +5,15 @@ use cozorocks::{DbIter, RawRocksDb, RocksDbStatus}; use crate::data::tuple::{EncodedTuple, Tuple}; use crate::data::value::DataValue; -#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)] +#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] pub struct ThrowawayId(pub(crate) u32); +impl Debug for ThrowawayId { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "t{}", self.0) + } +} + #[derive(Clone)] pub struct ThrowawayArea { pub(crate) db: RawRocksDb,