Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions src/query/datalog/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ use super::rules::RuleRegistry;
use super::types::{AttributeSpec, EdnValue, Pattern, Rule, WhereClause};
use crate::graph::FactStorage;
use crate::graph::types::{Fact, Value};
use crate::storage::index::encode_value;
use anyhow::{Result, anyhow};
use std::collections::BTreeSet;
use std::sync::{Arc, RwLock};

/// Default maximum iterations for recursive evaluation
Expand Down Expand Up @@ -114,11 +116,13 @@ impl RecursiveEvaluator {
)?;
}

// Track facts we've seen (for delta computation)
// Note: Using Vec instead of HashSet because Value contains Float which can't Hash
let mut seen_facts: Vec<(uuid::Uuid, String, Value)> = base_facts
// Track facts we've seen (for delta computation) using BTreeSet with canonical encoding.
let mut seen_facts: BTreeSet<(uuid::Uuid, String, Vec<u8>)> = base_facts
.iter()
.map(|f| (f.entity, f.attribute.clone(), f.value.clone()))
.map(|f| {
let encoded = encode_value(&f.value);
(f.entity, f.attribute.clone(), encoded)
})
.collect();

let mut iteration = 0;
Expand Down Expand Up @@ -148,16 +152,17 @@ impl RecursiveEvaluator {
// Compute delta: facts not yet seen
let mut delta = Vec::new();
for fact in new_facts {
let key = (fact.entity, fact.attribute.clone(), fact.value.clone());
if !self.contains_fact(&seen_facts, &key) {
let encoded = encode_value(&fact.value);
let key = (fact.entity, fact.attribute.clone(), encoded);
if !seen_facts.contains(&key) {
// Check total result limit
if seen_facts.len() >= self.max_results {
return Err(anyhow!(
"Max query results ({}) exceeded.",
self.max_results
));
}
seen_facts.push(key);
seen_facts.insert(key);
delta.push(fact);
}
}
Expand Down Expand Up @@ -358,19 +363,6 @@ impl RecursiveEvaluator {
pub fn instantiate_head_public(&self, head: &[EdnValue], binding: &Bindings) -> Result<Fact> {
self.instantiate_head(head, binding)
}

/// Check if a fact tuple exists in the seen_facts vector.
///
/// Manual containment check since Value can't implement Hash (contains Float).
fn contains_fact(
&self,
seen_facts: &[(uuid::Uuid, String, Value)],
key: &(uuid::Uuid, String, Value),
) -> bool {
seen_facts
.iter()
.any(|(e, a, v)| e == &key.0 && a == &key.1 && v == &key.2)
}
}

/// Convert a stored Value back to EdnValue.
Expand Down
Loading