pub enum ViewTree {
Leaf(Leaf),
And(Box<Tree>, Box<Tree>),
Or(Box<Tree>, Box<Tree>),
Diff(Box<Tree>, Box<Tree>),
}Expand description
The composition tree. Depth ≤ 3, leaves ≤ 4 (declarative caps, enforced at CREATE).
use kevy_index::{IndexValue, Leaf, Segment, Tree};
let mut age = Segment::new();
age.set_key_dir(true);
age.apply(b"u:1", None, Some(IndexValue::I64(30)));
age.apply(b"u:2", None, Some(IndexValue::I64(70)));
let mut city = Segment::new();
city.set_key_dir(true);
city.apply(b"u:1", None, Some(IndexValue::Str(b"kyoto".to_vec())));
city.apply(b"u:2", None, Some(IndexValue::Str(b"kyoto".to_vec())));
let kyoto = IndexValue::Str(b"kyoto".to_vec());
let t = Tree::And(
Box::new(Tree::Leaf(Leaf::new("city", kyoto.clone(), kyoto))),
Box::new(Tree::Leaf(Leaf::new("age", IndexValue::I64(18), IndexValue::I64(65)))),
);
let seg = |name: &[u8]| match name {
b"age" => Some(&age),
b"city" => Some(&city),
_ => None,
};
assert_eq!(t.eval(&seg), vec![b"u:1".to_vec()]);
assert!(t.contains(b"u:1", &seg) && !t.contains(b"u:2", &seg));
assert_eq!((t.leaves(), t.depth()), (2, 2));
let mut names = Vec::new();
t.each_leaf(&mut |l: &Leaf| names.push(l.index.clone()));
assert_eq!(names, [b"city".to_vec(), b"age".to_vec()]);Variants§
Leaf(Leaf)
A single index shape.
use kevy_index::{IndexValue, Leaf, Tree};
let t = Tree::Leaf(Leaf::new("age", IndexValue::I64(0), IndexValue::I64(9)));
assert_eq!((t.leaves(), t.depth()), (1, 1));And(Box<Tree>, Box<Tree>)
Intersection.
use kevy_index::{IndexValue, Leaf, Tree};
let leaf = |lo, hi| Box::new(Tree::Leaf(Leaf::new("n", IndexValue::I64(lo), IndexValue::I64(hi))));
let t = Tree::And(leaf(0, 10), leaf(5, 20));
assert!(t.contains_values(&|_: &[u8]| Some(IndexValue::I64(7))));
assert!(!t.contains_values(&|_: &[u8]| Some(IndexValue::I64(3))));Or(Box<Tree>, Box<Tree>)
Union.
use kevy_index::{IndexValue, Leaf, Tree};
let leaf = |lo, hi| Box::new(Tree::Leaf(Leaf::new("n", IndexValue::I64(lo), IndexValue::I64(hi))));
let t = Tree::Or(leaf(0, 1), leaf(8, 9));
assert!(t.contains_values(&|_: &[u8]| Some(IndexValue::I64(9))));
assert!(!t.contains_values(&|_: &[u8]| Some(IndexValue::I64(5))));Diff(Box<Tree>, Box<Tree>)
Left minus right (NOT commutative — order is fixed).
use kevy_index::{IndexValue, Leaf, Tree};
let leaf = |lo, hi| Box::new(Tree::Leaf(Leaf::new("n", IndexValue::I64(lo), IndexValue::I64(hi))));
let t = Tree::Diff(leaf(0, 10), leaf(5, 10));
assert!(t.contains_values(&|_: &[u8]| Some(IndexValue::I64(4))));
assert!(!t.contains_values(&|_: &[u8]| Some(IndexValue::I64(6))));Implementations§
Source§impl Tree
impl Tree
Sourcepub fn eval<'a>(
&self,
seg: &impl Fn(&[u8]) -> Option<&'a Segment>,
) -> Vec<Vec<u8>>
pub fn eval<'a>( &self, seg: &impl Fn(&[u8]) -> Option<&'a Segment>, ) -> Vec<Vec<u8>>
Evaluate tree against one shard’s segments: seg resolves an
index name to its Segment (None = unknown index → empty leaf —
the runtime validates names at CREATE, so this is defensive).
Returns the member keys (unordered set semantics).
use kevy_index::{IndexValue, Leaf, Segment, Tree};
let mut age = Segment::new();
age.set_key_dir(true);
age.apply(b"u:1", None, Some(IndexValue::I64(30)));
age.apply(b"u:2", None, Some(IndexValue::I64(70)));
let t = Tree::Leaf(Leaf::new("age", IndexValue::I64(18), IndexValue::I64(65)));
let seg = |name: &[u8]| (name == b"age").then_some(&age);
assert_eq!(t.eval(&seg), vec![b"u:1".to_vec()]);
assert!(t.contains(b"u:1", &seg) && !t.contains(b"u:2", &seg));Sourcepub fn contains<'a>(
&self,
key: &[u8],
seg: &impl Fn(&[u8]) -> Option<&'a Segment>,
) -> bool
pub fn contains<'a>( &self, key: &[u8], seg: &impl Fn(&[u8]) -> Option<&'a Segment>, ) -> bool
Re-evaluate ONE key’s membership (the materialized write hook):
every leaf is a point probe in the segment’s key directory
(Segment::set_key_dir); a segment without one holds no key.
Sourcepub fn contains_values(
&self,
vals: &impl Fn(&[u8]) -> Option<IndexValue>,
) -> bool
pub fn contains_values( &self, vals: &impl Fn(&[u8]) -> Option<IndexValue>, ) -> bool
Tree::contains variant over PRE-FETCHED per-index values — the
write hook probes each referenced index ONCE per key and evaluates
every view against the same small table (bounds compares only; no
per-view re-hashing).
use kevy_index::{IndexValue, Leaf, Tree};
let t = Tree::Leaf(Leaf::new("age", IndexValue::I64(18), IndexValue::I64(65)));
assert!(t.contains_values(&|_: &[u8]| Some(IndexValue::I64(40))));
assert!(!t.contains_values(&|_: &[u8]| None));