kevy-index 7.0.0

Declarative secondary indexes over prefix domains: range/unique kinds, derived-by-construction, cursor pagination.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! Views: named composition trees over declared indexes.
//!
//! Pure logic: [`ViewSpec`] (the declaration), [`eval_tree`] (the
//! virtual-mode evaluator over segment closures), and
//! [`MaterializedSet`] (the incremental ordered result set with the
//! bounded top-K discipline). The runtime supplies segment access and
//! wires maintenance to its write hook — nothing here does I/O.
//!
//! Locked structural rules: components are NAMED indexes (leaves carry
//! a shape; the view layer holds no predicates of its own); a view
//! stores MEMBERSHIP + ORDER only (never field values); AND/OR
//! subtrees may be re-ordered by the engine (DIFF is fixed
//! left-right).

use crate::segment::Segment;
use crate::value::IndexValue;

use kevy_text::SortOrder;

pub use crate::view_materialized::{MaterializedSet, Membership};
pub use crate::view_mode::ViewMode;
pub use crate::view_sidecar::{MAX_VIEWS, ViewCatalog};

/// One leaf: a declared index + the shape it contributes.
///
/// ```
/// use kevy_index::{IndexValue, Leaf};
/// let adults = Leaf::new("age", IndexValue::I64(18), IndexValue::I64(i64::MAX));
/// assert_eq!(adults.index, b"age");
/// ```
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Leaf {
    /// Index name (resolved by the runtime).
    ///
    /// ```
    /// use kevy_index::{IndexValue, Leaf};
    /// let leaf = Leaf::new("city", IndexValue::Str(b"a".to_vec()), IndexValue::Str(b"m".to_vec()));
    /// assert_eq!(leaf.index, b"city");
    /// ```
    pub index: Vec<u8>,
    /// Inclusive bounds (EQ = same min/max), already coerced to the
    /// index's type by the runtime at CREATE time.
    ///
    /// ```
    /// use kevy_index::{IndexValue, Leaf, Tree};
    /// let leaf = Leaf::new("age", IndexValue::I64(18), IndexValue::I64(65));
    /// assert_eq!(leaf.min, IndexValue::I64(18));
    /// let t = Tree::Leaf(leaf);
    /// assert!(t.contains_values(&|_: &[u8]| Some(IndexValue::I64(18))), "min is inclusive");
    /// assert!(!t.contains_values(&|_: &[u8]| Some(IndexValue::I64(17))));
    /// ```
    pub min: IndexValue,
    /// Upper bound.
    ///
    /// ```
    /// use kevy_index::{IndexValue, Leaf, Tree};
    /// let leaf = Leaf::new("age", IndexValue::I64(18), IndexValue::I64(65));
    /// assert_eq!(leaf.max, IndexValue::I64(65));
    /// let t = Tree::Leaf(leaf);
    /// assert!(t.contains_values(&|_: &[u8]| Some(IndexValue::I64(65))), "max is inclusive");
    /// assert!(!t.contains_values(&|_: &[u8]| Some(IndexValue::I64(66))));
    /// ```
    pub max: IndexValue,
}

impl Leaf {
    /// Rows of index `index` whose value lies in `min..=max` (EQ is
    /// `min == max`), bounds already coerced to the index's type.
    ///
    /// ```
    /// use kevy_index::{IndexValue, Leaf};
    /// let eq = Leaf::new("city", IndexValue::Str(b"kyoto".to_vec()), IndexValue::Str(b"kyoto".to_vec()));
    /// assert_eq!(eq.min, eq.max);
    /// ```
    pub fn new(index: impl Into<Vec<u8>>, min: IndexValue, max: IndexValue) -> Leaf {
        Leaf { index: index.into(), min, max }
    }
}

/// 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()]);
/// ```
#[derive(Debug, Clone, PartialEq)]
pub enum Tree {
    /// 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));
    /// ```
    Leaf(Leaf),
    /// 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))));
    /// ```
    And(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))));
    /// ```
    Or(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))));
    /// ```
    Diff(Box<Tree>, Box<Tree>),
}

impl Tree {
    /// 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));
    /// ```
    pub fn eval<'a>(&self, seg: &impl Fn(&[u8]) -> Option<&'a Segment>) -> Vec<Vec<u8>> {
        eval_tree(self, seg)
    }

    /// 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.
    pub fn contains<'a>(&self, key: &[u8], seg: &impl Fn(&[u8]) -> Option<&'a Segment>) -> bool {
        key_in_tree(self, key, seg)
    }

    /// [`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));
    /// ```
    pub fn contains_values(&self, vals: &impl Fn(&[u8]) -> Option<IndexValue>) -> bool {
        key_in_tree_vals(self, vals)
    }

    /// Number of leaves.
    pub fn leaves(&self) -> usize {
        match self {
            Tree::Leaf(_) => 1,
            Tree::And(a, b) | Tree::Or(a, b) | Tree::Diff(a, b) => a.leaves() + b.leaves(),
        }
    }

    /// Depth (a leaf is 1).
    pub fn depth(&self) -> usize {
        match self {
            Tree::Leaf(_) => 1,
            Tree::And(a, b) | Tree::Or(a, b) | Tree::Diff(a, b) => 1 + a.depth().max(b.depth()),
        }
    }

    /// Visit every leaf.
    pub fn each_leaf<F: FnMut(&Leaf)>(&self, f: &mut F) {
        match self {
            Tree::Leaf(l) => f(l),
            Tree::And(a, b) | Tree::Or(a, b) | Tree::Diff(a, b) => {
                a.each_leaf(f);
                b.each_leaf(f);
            }
        }
    }
}

/// A declared view.
///
/// ```
/// use kevy_index::{IndexValue, Leaf, SortOrder, Tree, ViewMode, ViewSpec};
/// let leaf = Leaf::new("age", IndexValue::I64(18), IndexValue::I64(65));
/// let v = ViewSpec::new("adults", Tree::Leaf(leaf), "age")
///     .with_order(SortOrder::Desc)
///     .with_mode(ViewMode::Materialized { top_k: 100 });
/// assert!(v.validate().is_ok());
/// ```
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ViewSpec {
    /// Catalog name.
    ///
    /// ```
    /// use kevy_index::{IndexValue, Leaf, Tree, ViewCatalog, ViewSpec};
    /// let t = Tree::Leaf(Leaf::new("age", IndexValue::I64(0), IndexValue::I64(99)));
    /// let mut views = ViewCatalog::new();
    /// views.create(ViewSpec::new("adults", t, "age"))?;
    /// assert_eq!(views.get(b"adults").map(|v| v.name.clone()), Some(b"adults".to_vec()));
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub name: Vec<u8>,
    /// The composition.
    ///
    /// ```
    /// use kevy_index::{IndexValue, Leaf, Tree, ViewSpec};
    /// let t = Tree::Leaf(Leaf::new("age", IndexValue::I64(0), IndexValue::I64(99)));
    /// let v = ViewSpec::new("v", t.clone(), "age");
    /// assert_eq!(v.tree, t);
    /// assert_eq!(v.tree.leaves(), 1);
    /// ```
    pub tree: Tree,
    /// Index whose coerced value orders the view (a row absent from
    /// this index is excluded — declaratively, counted).
    ///
    /// ```
    /// use kevy_index::{IndexValue, Leaf, Tree, ViewSpec};
    /// let city = |c: &[u8]| IndexValue::Str(c.to_vec());
    /// let t = Tree::Leaf(Leaf::new("city", city(b"a"), city(b"z")));
    /// let v = ViewSpec::new("by_age", t, "age");
    /// assert_eq!(v.order_by, b"age", "the order index need not be in the tree");
    /// ```
    pub order_by: Vec<u8>,
    /// Direction of that order.
    ///
    /// ```
    /// use kevy_index::{IndexValue, Leaf, SortOrder, Tree, ViewSpec};
    /// let t = Tree::Leaf(Leaf::new("age", IndexValue::I64(0), IndexValue::I64(99)));
    /// let v = ViewSpec::new("v", t, "age");
    /// assert_eq!(v.order, SortOrder::Asc);
    /// assert_eq!(v.with_order(SortOrder::Desc).order, SortOrder::Desc);
    /// ```
    pub order: SortOrder,
    /// Virtual or materialized.
    ///
    /// ```
    /// use kevy_index::{IndexValue, Leaf, Tree, ViewMode, ViewSpec};
    /// let t = Tree::Leaf(Leaf::new("age", IndexValue::I64(0), IndexValue::I64(99)));
    /// let v = ViewSpec::new("v", t, "age").with_mode(ViewMode::Materialized { top_k: 50 });
    /// assert_eq!(v.mode.name(), "materialized");
    /// ```
    pub mode: ViewMode,
    /// Optional `VIA` hydration byte-template (`{key}` / `{key.N}`
    /// placeholders; pure dereference, one template hop).
    ///
    /// ```
    /// use kevy_index::{IndexValue, Leaf, Tree, ViewSpec};
    /// let t = Tree::Leaf(Leaf::new("age", IndexValue::I64(0), IndexValue::I64(99)));
    /// let v = ViewSpec::new("v", t, "age");
    /// assert_eq!(v.via, None, "members are returned as keys unless VIA is declared");
    /// ```
    pub via: Option<Vec<u8>>,
}

/// Declarative caps (RFC §1).
///
/// ```
/// use kevy_index::{IndexValue, Leaf, MAX_TREE_DEPTH, Tree, ViewError, ViewSpec};
/// let leaf = || Box::new(Tree::Leaf(Leaf::new("n", IndexValue::I64(0), IndexValue::I64(1))));
/// // And(And(Or(leaf, leaf), leaf), leaf) is four levels deep
/// let deep = Tree::And(Box::new(Tree::And(Box::new(Tree::Or(leaf(), leaf())), leaf())), leaf());
/// assert_eq!(deep.depth(), MAX_TREE_DEPTH + 1);
/// assert_eq!(ViewSpec::new("v", deep, "n").validate(), Err(ViewError::TooDeep));
/// ```
pub const MAX_TREE_DEPTH: usize = 3;
/// Max leaves per tree.
///
/// ```
/// use kevy_index::{IndexValue, Leaf, MAX_TREE_LEAVES, Tree, ViewSpec};
/// let leaf = || Box::new(Tree::Leaf(Leaf::new("n", IndexValue::I64(0), IndexValue::I64(1))));
/// let pair = || Box::new(Tree::Or(leaf(), leaf()));
/// let full = Tree::And(pair(), pair());
/// assert_eq!(full.leaves(), MAX_TREE_LEAVES);
/// assert!(ViewSpec::new("v", full, "n").validate().is_ok());
///
/// let wide = Tree::Or(Box::new(Tree::Or(pair(), pair())), leaf());
/// assert_eq!(wide.leaves(), MAX_TREE_LEAVES + 1);
/// assert!(ViewSpec::new("v", wide, "n").validate().is_err());
/// ```
pub const MAX_TREE_LEAVES: usize = 4;

impl ViewSpec {
    /// A virtual view `name` over `tree`, ascending by index `order_by`.
    ///
    /// ```
    /// use kevy_index::{IndexValue, Leaf, SortOrder, Tree, ViewMode, ViewSpec};
    /// let v = ViewSpec::new("v", Tree::Leaf(Leaf::new("a", IndexValue::I64(0), IndexValue::I64(9))), "a");
    /// assert_eq!((v.order, v.mode, v.via), (SortOrder::Asc, ViewMode::Virtual, None));
    /// ```
    pub fn new(name: impl Into<Vec<u8>>, tree: Tree, order_by: impl Into<Vec<u8>>) -> ViewSpec {
        ViewSpec {
            name: name.into(),
            tree,
            order_by: order_by.into(),
            order: SortOrder::Asc,
            mode: ViewMode::Virtual,
            via: None,
        }
    }

    /// This view ordered in `order`.
    ///
    /// ```
    /// # use kevy_index::{IndexValue, Leaf, SortOrder, Tree, ViewSpec};
    /// # let t = Tree::Leaf(Leaf::new("a", IndexValue::I64(0), IndexValue::I64(9)));
    /// assert_eq!(ViewSpec::new("v", t, "a").with_order(SortOrder::Desc).order, SortOrder::Desc);
    /// ```
    #[must_use]
    pub fn with_order(mut self, order: SortOrder) -> ViewSpec {
        self.order = order;
        self
    }

    /// This view evaluated as `mode`.
    ///
    /// ```
    /// # use kevy_index::{IndexValue, Leaf, Tree, ViewMode, ViewSpec};
    /// # let t = Tree::Leaf(Leaf::new("a", IndexValue::I64(0), IndexValue::I64(9)));
    /// let m = ViewMode::Materialized { top_k: 0 };
    /// assert_eq!(ViewSpec::new("v", t, "a").with_mode(m).mode, m);
    /// ```
    #[must_use]
    pub fn with_mode(mut self, mode: ViewMode) -> ViewSpec {
        self.mode = mode;
        self
    }

    /// This view hydrated through the `VIA` byte-template `via`.
    ///
    /// ```
    /// # use kevy_index::{IndexValue, Leaf, Tree, ViewSpec};
    /// # let t = Tree::Leaf(Leaf::new("a", IndexValue::I64(0), IndexValue::I64(9)));
    /// let v = ViewSpec::new("v", t, "a").with_via(b"doc:{key}".to_vec());
    /// assert_eq!(v.via.as_deref(), Some(&b"doc:{key}"[..]));
    /// ```
    #[must_use]
    pub fn with_via(mut self, via: Vec<u8>) -> ViewSpec {
        self.via = Some(via);
        self
    }

    /// Validate the structural caps.
    pub fn validate(&self) -> Result<(), crate::ViewError> {
        if self.tree.depth() > MAX_TREE_DEPTH {
            return Err(crate::ViewError::TooDeep);
        }
        if self.tree.leaves() > MAX_TREE_LEAVES {
            return Err(crate::ViewError::TooManyLeaves);
        }
        Ok(())
    }
}

pub(crate) fn eval_tree<'a>(
    tree: &Tree,
    seg: &impl Fn(&[u8]) -> Option<&'a Segment>,
) -> Vec<Vec<u8>> {
    match tree {
        Tree::Leaf(l) => match seg(&l.index) {
            Some(s) => {
                let (hits, _) = s.range(&l.min, &l.max, None, usize::MAX);
                hits.into_iter().map(|(k, _)| k).collect()
            }
            None => Vec::new(),
        },
        Tree::And(a, b) => {
            // Engine may re-order (locked clause): drive the smaller
            // side, probe the larger.
            let (xa, xb) = (a.eval(seg), b.eval(seg));
            let (mut drive, probe) = if xa.len() <= xb.len() { (xa, xb) } else { (xb, xa) };
            let set: std::collections::HashSet<&[u8]> = probe.iter().map(Vec::as_slice).collect();
            drive.retain(|k| set.contains(k.as_slice()));
            drive
        }
        Tree::Or(a, b) => {
            let mut xa = a.eval(seg);
            xa.extend(b.eval(seg));
            xa.sort();
            xa.dedup();
            xa
        }
        Tree::Diff(a, b) => {
            let mut xa = a.eval(seg);
            let xb = b.eval(seg);
            let set: std::collections::HashSet<&[u8]> = xb.iter().map(Vec::as_slice).collect();
            xa.retain(|k| !set.contains(k.as_slice()));
            xa
        }
    }
}

pub(crate) fn key_in_tree<'a>(
    tree: &Tree,
    key: &[u8],
    seg: &impl Fn(&[u8]) -> Option<&'a Segment>,
) -> bool {
    match tree {
        Tree::Leaf(l) => seg(&l.index)
            .and_then(|s| s.key_dir()?.get(key))
            .is_some_and(|v| v >= l.min && v <= l.max),
        Tree::And(a, b) => a.contains(key, seg) && b.contains(key, seg),
        Tree::Or(a, b) => a.contains(key, seg) || b.contains(key, seg),
        Tree::Diff(a, b) => a.contains(key, seg) && !b.contains(key, seg),
    }
}

pub(crate) fn key_in_tree_vals(tree: &Tree, vals: &impl Fn(&[u8]) -> Option<IndexValue>) -> bool {
    match tree {
        Tree::Leaf(l) => vals(&l.index).is_some_and(|v| v >= l.min && v <= l.max),
        Tree::And(a, b) => a.contains_values(vals) && b.contains_values(vals),
        Tree::Or(a, b) => a.contains_values(vals) || b.contains_values(vals),
        Tree::Diff(a, b) => a.contains_values(vals) && !b.contains_values(vals),
    }
}

#[cfg(test)]
#[path = "view_tests.rs"]
mod tests;