seemingly working, zero warnings

main
Ziyang Hu 2 years ago
parent 123a775388
commit c220d68e4b

@ -145,7 +145,7 @@ class PredicateClass:
def __call__(self, *args):
if self._pred_name is None:
raise Exception("you need to set the predicate name first")
ret = {'pred': self._pred_name, 'args': list(args)}
ret = {'op': self._pred_name, 'args': list(args)}
return ret

@ -2,9 +2,9 @@ use std::borrow::Cow;
use cxx::*;
use crate::{IterBuilder, PinSlice};
use crate::bridge::ffi::*;
use crate::bridge::tx::TxBuilder;
use crate::{IterBuilder, PinSlice};
#[derive(Default)]
pub struct DbBuilder<'a> {
@ -188,7 +188,7 @@ impl RawRocksDb {
IterBuilder {
inner: self.inner.iterator_with_snapshot(snapshot),
}
.auto_prefix_mode(true)
.auto_prefix_mode(true)
}
#[inline]
pub fn put(&self, key: &[u8], val: &[u8]) -> Result<(), RocksDbStatus> {

@ -12,7 +12,7 @@ use crate::data::id::{AttrId, EntityId, TxId};
use crate::data::json::JsonValue;
use crate::data::keyword::Keyword;
use crate::data::triple::StoreOp;
use crate::data::value::{DataValue, INLINE_VAL_SIZE_LIMIT};
use crate::data::value::DataValue;
use crate::parse::triple::TempIdCtx;
#[repr(u8)]
@ -278,32 +278,4 @@ impl Attribute {
}
self.val_type.coerce_value(value)
}
pub fn encode(&self) -> EncodedVec<INLINE_VAL_SIZE_LIMIT> {
let mut ret = SmallVec::<[u8; INLINE_VAL_SIZE_LIMIT]>::new();
self.serialize(&mut Serializer::new(&mut ret)).unwrap();
ret.into()
}
}
#[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),
keyword: Keyword::from("01234567890123456789012/01234567890123456789012"),
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());
}
}

@ -401,13 +401,6 @@ pub(crate) fn encode_sentinel_entity_attr(eid: EntityId, aid: AttrId) -> Encoded
ret.into()
}
#[inline]
pub(crate) fn decode_sentinel_entity_attr(src: &[u8]) -> Result<(EntityId, AttrId)> {
let eid = EntityId::from_bytes(&src[..VEC_SIZE_8]);
let aid = AttrId::from_bytes(&src[VEC_SIZE_8..VEC_SIZE_16]);
Ok((eid, aid))
}
#[inline]
pub(crate) fn encode_sentinel_attr_val(aid: AttrId, val: &DataValue) -> EncodedVec<LARGE_VEC_SIZE> {
let mut ret = SmallVec::<[u8; LARGE_VEC_SIZE]>::new();
@ -433,10 +426,6 @@ pub(crate) fn encode_sentinel_attr_by_id(aid: AttrId) -> EncodedVec<VEC_SIZE_8>
ret.into()
}
pub(crate) fn decode_sentinel_attr_by_id(src: &[u8]) -> Result<AttrId> {
Ok(AttrId::from_bytes(src))
}
#[inline]
pub(crate) fn encode_sentinel_attr_by_kw(kw: &Keyword) -> EncodedVec<LARGE_VEC_SIZE> {
let mut ret = SmallVec::<[u8; LARGE_VEC_SIZE]>::new();

@ -207,12 +207,6 @@ pub(crate) enum InputAtom {
Unification(Unification),
}
impl InputAtom {
pub(crate) fn is_negation(&self) -> bool {
matches!(self, InputAtom::Negation(_))
}
}
#[derive(Debug, Clone)]
pub(crate) enum NormalFormAtom {
AttrTriple(NormalFormAttrTripleAtom),

@ -10,18 +10,6 @@ pub enum StoreOp {
Assert = 1,
}
impl StoreOp {
pub(crate) fn bytes(&self) -> [u8; 1] {
[*self as u8]
}
pub(crate) fn name(&self) -> &'static str {
match self {
Self::Retract => "retract",
Self::Assert => "put",
}
}
}
impl Display for StoreOp {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {

@ -34,9 +34,6 @@ impl Tuple {
pub(crate) fn arity(&self) -> usize {
self.0.len()
}
pub(crate) fn encode_as_key(&self, prefix: TempStoreId) -> Vec<u8> {
self.encode_as_key_for_epoch(prefix, 0)
}
pub(crate) fn encode_as_key_for_epoch(&self, prefix: TempStoreId, epoch: u32) -> Vec<u8> {
let len = self.arity();
let mut ret = Vec::with_capacity(4 + 4 * len + 10 * len);
@ -230,37 +227,3 @@ pub(crate) fn rusty_scratch_cmp(a: &[u8], b: &[u8]) -> i8 {
Ordering::Less => -1,
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::data::tuple::{EncodedTuple, Tuple};
use crate::data::value::DataValue;
use crate::runtime::temp_store::TempStoreId;
#[test]
fn test_serde() {
let val: Vec<DataValue> = vec![
json!(1).into(),
json!(2.0).into(),
json!("my_name_is").into(),
];
let val = Tuple(val);
let encoded = val.encode_as_key(TempStoreId(123));
println!("{:x?}", encoded);
let encoded_tuple: EncodedTuple<'_> = (&encoded as &[u8]).into();
println!("{:?}", encoded_tuple.prefix());
println!("{:?}", encoded_tuple.arity());
println!("{:?}", encoded_tuple.get(0));
println!("{:?}", encoded_tuple.get(1));
println!("{:?}", encoded_tuple.get(2));
println!("{:?}", encoded_tuple.get(3));
println!(
"{:?}",
encoded_tuple
.iter()
.collect::<anyhow::Result<Vec<DataValue>>>()
)
}
}

@ -11,6 +11,7 @@ use crate::data::json::JsonValue;
use crate::data::keyword::{Keyword, PROG_ENTRY};
use crate::data::program::{
InputAtom, InputAttrTripleAtom, InputProgram, InputRule, InputRuleApplyAtom, InputTerm,
Unification,
};
use crate::data::value::DataValue;
use crate::query::pull::PullSpecs;
@ -177,9 +178,27 @@ impl SessionTx {
pred.partial_eval()?;
Ok(InputAtom::Predicate(pred))
}
fn parse_unification(payload: &Map<String, JsonValue>) -> Result<InputAtom> {
let binding = payload
.get("unify")
.ok_or_else(|| anyhow!("expect expression to have field 'unify'"))?
.as_str()
.ok_or_else(|| anyhow!("expect field 'unify' to be a keyword"))?;
let binding = Keyword::from(binding);
ensure!(
!binding.is_reserved(),
"binding for unification {} is reserved",
binding
);
let expr = payload
.get("expr")
.ok_or_else(|| anyhow!("expect unify map to have field 'expr'"))?;
let expr = Self::parse_expr_arg(expr)?;
Ok(InputAtom::Unification(Unification { binding, expr }))
}
fn parse_expr(payload: &Map<String, JsonValue>) -> Result<Expr> {
let name = payload
.get("pred")
.get("op")
.ok_or_else(|| anyhow!("expect expression to have key 'pred'"))?
.as_str()
.ok_or_else(|| anyhow!("expect key 'pred' to be a string referring to a predicate"))?;
@ -228,7 +247,7 @@ impl SessionTx {
JsonValue::Object(map) => {
if let Some(v) = map.get("const") {
Ok(Expr::Const(v.into()))
} else if map.contains_key("pred") {
} else if map.contains_key("op") {
Self::parse_expr(map)
} else {
bail!("expression object must contain either 'const' or 'pred' key");
@ -352,8 +371,10 @@ impl SessionTx {
JsonValue::Object(map) => {
if map.contains_key("rule") {
self.parse_input_rule_atom(map, vld)
} else if map.contains_key("pred") {
} else if map.contains_key("op") {
Self::parse_input_predicate_atom(map)
} else if map.contains_key("unify") {
Self::parse_unification(map)
} else if map.contains_key("conj")
|| map.contains_key("disj")
|| map.contains_key("not_exists")

@ -179,57 +179,3 @@ pub(crate) fn generalized_kahn(
debug_assert_eq!(in_degree.iter().sum::<usize>(), 0);
ret
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use crate::query::graph::{
generalized_kahn, reachable_components, strongly_connected_components, StratifiedGraph,
};
#[test]
fn test_scc() {
// let graph = BTreeMap::from([
// ("a", vec!["b"]),
// ("b", vec!["a", "c"]),
// ("c", vec!["a", "d", "e"]),
// ("d", vec!["e", "e", "e"]),
// ("e", vec![]),
// ("f", vec![]),
// ]);
let graph = BTreeMap::from([
(0, vec![1, 5, 9]),
(1, vec![6, 10]),
(2, vec![4]),
(3, vec![9, 10]),
(4, vec![]),
(5, vec![2]),
(6, vec![3]),
(7, vec![2]),
(8, vec![3]),
(9, vec![7]),
(10, vec![8]),
(11, vec![0, 4]),
]);
let scc = strongly_connected_components(&graph);
println!("scc found: {:?}", scc);
// let reachable = reachable_components(&graph, &11);
// dbg!(reachable);
//
// let s_graph: StratifiedGraph<usize> = BTreeMap::from([
// (
// 0,
// BTreeMap::from([(1, false), (2, false), (3, false), (4, true), (5, true)]),
// ),
// (1, BTreeMap::from([(6, false)])),
// (2, BTreeMap::from([(6, false)])),
// (3, BTreeMap::from([(6, true)])),
// (4, BTreeMap::from([(6, true)])),
// (5, BTreeMap::from([(6, false)])),
// ]);
// dbg!(generalized_kahn(&s_graph, 7));
}
}

@ -293,16 +293,6 @@ impl Relation {
bindings: [e_binding, v_binding],
})
}
pub(crate) fn singlet(bindings: Vec<Keyword>, data: Vec<DataValue>) -> Self {
Self::fixed(bindings, vec![data])
}
pub(crate) fn fixed(bindings: Vec<Keyword>, data: Vec<Vec<DataValue>>) -> Self {
Self::Fixed(InlineFixedRelation {
bindings,
data,
to_eliminate: Default::default(),
})
}
pub(crate) fn reorder(self, new_order: Vec<Keyword>) -> Self {
Self::Reorder(ReorderRelation {
relation: Box::new(self),

@ -6,12 +6,7 @@ use cozorocks::{DbIter, IterBuilder};
use crate::data::attr::{Attribute, AttributeTyping};
use crate::data::compare::compare_key;
use crate::data::encode::{
decode_ae_key, decode_ea_key, decode_vae_key, decode_value, decode_value_from_key,
decode_value_from_val, encode_aev_key, encode_ave_key, encode_ave_key_for_unique_v,
encode_eav_key, encode_sentinel_attr_val, encode_sentinel_entity_attr, encode_vae_key,
EncodedVec, LARGE_VEC_SIZE,
};
use crate::data::encode::{decode_ae_key, decode_ea_key, decode_vae_key, decode_value, decode_value_from_key, decode_value_from_val, encode_aev_key, encode_ave_key, encode_ave_key_for_unique_v, encode_eav_key, encode_sentinel_attr_val, encode_sentinel_entity_attr, encode_vae_key, EncodedVec, LARGE_VEC_SIZE};
use crate::data::id::{AttrId, EntityId, Validity};
use crate::data::triple::StoreOp;
use crate::data::value::{DataValue, INLINE_VAL_SIZE_LIMIT};
@ -407,28 +402,6 @@ impl SessionTx {
},
)
}
pub(crate) fn restore_bottom_value(
&self,
eid: EntityId,
aid: AttrId,
vld: Validity,
) -> Result<DataValue> {
let encoded = encode_eav_key(eid, aid, &DataValue::Bottom, vld);
let res = self
.tx
.get(&encoded, false)?
.ok_or_else(|| anyhow!("triple not found with {:?} and {:?}", eid, aid))?;
decode_value(&res.as_ref()[1..])
}
pub(crate) fn triple_ea_scan(
&self,
eid: EntityId,
aid: AttrId,
) -> impl Iterator<Item = Result<(EntityId, AttrId, DataValue, Validity, StoreOp)>> {
let lower = encode_eav_key(eid, aid, &DataValue::Null, Validity::MAX);
let upper = encode_eav_key(eid, aid, &DataValue::Bottom, Validity::MIN);
TripleEntityAttrIter::new(self.tx.iterator(), lower, upper)
}
pub(crate) fn triple_ea_before_scan(
&self,
eid: EntityId,
@ -454,34 +427,6 @@ impl SessionTx {
}
Ok(false)
}
pub(crate) fn triple_ae_scan(
&self,
aid: AttrId,
eid: EntityId,
) -> impl Iterator<Item = Result<(AttrId, EntityId, DataValue, Validity, StoreOp)>> {
let lower = encode_aev_key(aid, eid, &DataValue::Null, Validity::MAX);
let upper = encode_aev_key(aid, eid, &DataValue::Bottom, Validity::MIN);
TripleAttrEntityIter::new(self.tx.iterator(), lower, upper)
}
pub(crate) fn triple_ae_before_scan(
&self,
aid: AttrId,
eid: EntityId,
before: Validity,
) -> impl Iterator<Item = Result<(AttrId, EntityId, DataValue)>> {
let lower = encode_aev_key(aid, eid, &DataValue::Null, Validity::MAX);
let upper = encode_aev_key(aid, eid, &DataValue::Bottom, Validity::MIN);
TripleAttrEntityBeforeIter::new(self.tx.iterator(), lower, upper, before)
}
pub(crate) fn triple_av_scan(
&self,
aid: AttrId,
v: &DataValue,
) -> impl Iterator<Item = Result<(AttrId, DataValue, EntityId, Validity, StoreOp)>> {
let lower = encode_ave_key(aid, v, EntityId::MIN_PERM, Validity::MAX);
let upper = encode_ave_key(aid, v, EntityId::MAX_PERM, Validity::MIN);
TripleAttrValueIter::new(self.tx.iterator(), lower, upper)
}
pub(crate) fn triple_av_before_scan(
&self,
aid: AttrId,
@ -502,15 +447,6 @@ impl SessionTx {
let upper = encode_ave_key(aid, v, EntityId::MAX_PERM, Validity::MIN);
TripleAttrValueAfterIter::new(self.tx.iterator(), lower, upper, after)
}
pub(crate) fn triple_vref_a_scan(
&self,
v_eid: EntityId,
aid: AttrId,
) -> impl Iterator<Item = Result<(EntityId, AttrId, EntityId, Validity, StoreOp)>> {
let lower = encode_vae_key(v_eid, aid, EntityId::MIN_PERM, Validity::MAX);
let upper = encode_vae_key(v_eid, aid, EntityId::MAX_PERM, Validity::MIN);
TripleValueRefAttrIter::new(self.tx.iterator(), lower, upper)
}
pub(crate) fn triple_vref_a_before_scan(
&self,
v_eid: EntityId,
@ -521,31 +457,6 @@ impl SessionTx {
let upper = encode_vae_key(v_eid, aid, EntityId::MAX_PERM, Validity::MIN);
TripleValueRefAttrBeforeIter::new(self.tx.iterator(), lower, upper, before)
}
pub(crate) fn triple_e_scan(
&self,
eid: EntityId,
) -> impl Iterator<Item = Result<(EntityId, AttrId, DataValue, Validity, StoreOp)>> {
let lower = encode_eav_key(eid, AttrId::MIN_PERM, &DataValue::Null, Validity::MAX);
let upper = encode_eav_key(eid, AttrId::MAX_PERM, &DataValue::Bottom, Validity::MIN);
TripleEntityAttrIter::new(self.tx.iterator(), lower, upper)
}
pub(crate) fn triple_e_before_scan(
&self,
eid: EntityId,
before: Validity,
) -> impl Iterator<Item = Result<(EntityId, AttrId, DataValue)>> {
let lower = encode_eav_key(eid, AttrId::MIN_PERM, &DataValue::Null, Validity::MAX);
let upper = encode_eav_key(eid, AttrId::MAX_PERM, &DataValue::Bottom, Validity::MIN);
TripleEntityAttrBeforeIter::new(self.tx.iterator(), lower, upper, before)
}
pub(crate) fn triple_a_scan(
&self,
aid: AttrId,
) -> impl Iterator<Item = Result<(AttrId, EntityId, DataValue, Validity, StoreOp)>> {
let lower = encode_aev_key(aid, EntityId::MIN_PERM, &DataValue::Null, Validity::MAX);
let upper = encode_aev_key(aid, EntityId::MAX_PERM, &DataValue::Bottom, Validity::MIN);
TripleAttrEntityIter::new(self.tx.iterator(), lower, upper)
}
pub(crate) fn triple_a_before_scan(
&self,
aid: AttrId,
@ -555,64 +466,6 @@ impl SessionTx {
let upper = encode_aev_key(aid, EntityId::MAX_PERM, &DataValue::Bottom, Validity::MIN);
TripleAttrEntityBeforeIter::new(self.tx.iterator(), lower, upper, before)
}
pub(crate) fn triple_a_scan_all(
&self,
) -> impl Iterator<Item = Result<(AttrId, EntityId, DataValue, Validity, StoreOp)>> {
let lower = encode_aev_key(
AttrId::MIN_PERM,
EntityId::MIN_PERM,
&DataValue::Null,
Validity::MAX,
);
let upper = encode_aev_key(
AttrId::MAX_PERM,
EntityId::MAX_PERM,
&DataValue::Bottom,
Validity::MIN,
);
TripleAttrEntityIter::new(self.tx.iterator(), lower, upper)
}
pub(crate) fn triple_a_before_scan_all(
&self,
before: Validity,
) -> impl Iterator<Item = Result<(AttrId, EntityId, DataValue)>> {
let lower = encode_aev_key(
AttrId::MIN_PERM,
EntityId::MIN_PERM,
&DataValue::Null,
Validity::MAX,
);
let upper = encode_aev_key(
AttrId::MAX_PERM,
EntityId::MAX_PERM,
&DataValue::Bottom,
Validity::MIN,
);
TripleAttrEntityBeforeIter::new(self.tx.iterator(), lower, upper, before)
}
pub(crate) fn triple_vref_scan(
&self,
v_eid: EntityId,
) -> impl Iterator<Item = Result<(EntityId, AttrId, EntityId, Validity, StoreOp)>> {
let lower = encode_vae_key(v_eid, AttrId::MIN_PERM, EntityId::MIN_PERM, Validity::MAX);
let upper = encode_vae_key(v_eid, AttrId::MAX_PERM, EntityId::MAX_PERM, Validity::MIN);
TripleValueRefAttrIter::new(self.tx.iterator(), lower, upper)
}
pub(crate) fn triple_vref_before_scan(
&self,
v_eid: EntityId,
before: Validity,
) -> impl Iterator<Item = Result<(EntityId, AttrId, EntityId)>> {
let lower = encode_vae_key(v_eid, AttrId::MIN_PERM, EntityId::MIN_PERM, Validity::MAX);
let upper = encode_vae_key(v_eid, AttrId::MAX_PERM, EntityId::MAX_PERM, Validity::MIN);
TripleValueRefAttrBeforeIter::new(self.tx.iterator(), lower, upper, before)
}
}
enum LatestTripleExistence {
Asserted,
Retracted,
NotFound,
}
// normal version
@ -623,17 +476,6 @@ struct TripleEntityAttrIter {
}
impl TripleEntityAttrIter {
fn new(
builder: IterBuilder,
lower_bound: EncodedVec<LARGE_VEC_SIZE>,
upper_bound: EncodedVec<LARGE_VEC_SIZE>,
) -> Self {
let it = builder.upper_bound(&upper_bound).start();
Self {
it,
current: lower_bound,
}
}
fn next_inner(&mut self) -> Result<Option<(EntityId, AttrId, DataValue, Validity, StoreOp)>> {
self.it.seek(&self.current);
return match self.it.pair()? {
@ -723,17 +565,6 @@ struct TripleAttrEntityIter {
}
impl TripleAttrEntityIter {
fn new(
builder: IterBuilder,
lower_bound: EncodedVec<LARGE_VEC_SIZE>,
upper_bound: EncodedVec<LARGE_VEC_SIZE>,
) -> Self {
let it = builder.upper_bound(&upper_bound).start();
Self {
it,
current: lower_bound,
}
}
fn next_inner(&mut self) -> Result<Option<(AttrId, EntityId, DataValue, Validity, StoreOp)>> {
loop {
self.it.seek(&self.current);
@ -827,17 +658,6 @@ struct TripleAttrValueIter {
}
impl TripleAttrValueIter {
fn new(
builder: IterBuilder,
lower_bound: EncodedVec<LARGE_VEC_SIZE>,
upper_bound: EncodedVec<LARGE_VEC_SIZE>,
) -> Self {
let it = builder.upper_bound(&upper_bound).start();
Self {
it,
current: lower_bound,
}
}
fn next_inner(&mut self) -> Result<Option<(AttrId, DataValue, EntityId, Validity, StoreOp)>> {
self.it.seek(&self.current);
return match self.it.pair()? {
@ -983,17 +803,6 @@ struct TripleValueRefAttrIter {
}
impl TripleValueRefAttrIter {
fn new(
builder: IterBuilder,
lower_bound: EncodedVec<LARGE_VEC_SIZE>,
upper_bound: EncodedVec<LARGE_VEC_SIZE>,
) -> Self {
let it = builder.upper_bound(&upper_bound).start();
Self {
it,
current: lower_bound,
}
}
fn next_inner(&mut self) -> Result<Option<(EntityId, AttrId, EntityId, Validity, StoreOp)>> {
self.it.seek(&self.current);
return match self.it.pair()? {

@ -1,4 +1,3 @@
use itertools::Itertools;
use log::info;
use serde_json::{json, to_string_pretty};

Loading…
Cancel
Save