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
//! [`Catalog`] — the index registry: declarations, states, and the
//! compiled prefix matcher the write-path hook consults.
use crate::error::{CatalogError, Declared};
use crate::spec::IndexSpec;
/// Declared scalar type of an index (`TYPE i64|f64|str`).
///
/// ```
/// use kevy_index::ValType;
/// assert_eq!(ValType::parse(b"F64"), Some(ValType::F64));
/// assert_eq!(ValType::parse(ValType::Str.tag().as_bytes()), Some(ValType::Str));
/// assert_eq!(ValType::parse(b"u8"), None);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ValType {
/// f32 LE vector blob (ANN kinds parse the field
/// themselves — never coerced through IndexValue).
///
/// ```
/// use kevy_index::{AnnSpec, IndexKind, IndexSpec, IndexValue, ValType};
/// let knn = IndexSpec::builder("emb", "doc:", IndexKind::Ann, ValType::Vector)
/// .with_field("emb")
/// .with_ann(AnnSpec::new(3))
/// .build()?;
/// assert_eq!(knn.ty(), ValType::Vector);
/// assert_eq!(IndexValue::coerce(ValType::Vector, b"\0\0\0\0"), None, "never a scalar");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
Vector,
/// Signed 64-bit integer.
///
/// ```
/// use kevy_index::{IndexValue, ValType};
/// assert_eq!(IndexValue::coerce(ValType::I64, b"-7"), Some(IndexValue::I64(-7)));
/// assert_eq!(IndexValue::coerce(ValType::I64, b"7.5"), None);
/// ```
I64,
/// Finite 64-bit float (NaN coerce-fails).
///
/// ```
/// use kevy_index::{IndexValue, ValType};
/// assert_eq!(IndexValue::coerce(ValType::F64, b"7.5"), Some(IndexValue::F64(7.5)));
/// assert_eq!(IndexValue::coerce(ValType::F64, b"nan"), None);
/// ```
F64,
/// Raw bytes, memcmp order.
///
/// ```
/// use kevy_index::{IndexValue, ValType};
/// let (a, b) = (IndexValue::coerce(ValType::Str, b"B"), IndexValue::coerce(ValType::Str, b"a"));
/// assert!(a < b, "bytewise: 'B' (0x42) sorts before 'a' (0x61)");
/// ```
Str,
}
impl ValType {
/// Wire tag (catalog sidecar + IDX.LIST).
pub fn tag(self) -> &'static str {
match self {
ValType::I64 => "i64",
ValType::F64 => "f64",
ValType::Str => "str",
ValType::Vector => "vector",
}
}
/// Parse a wire tag.
pub fn parse(raw: &[u8]) -> Option<ValType> {
if raw.eq_ignore_ascii_case(b"i64") {
Some(ValType::I64)
} else if raw.eq_ignore_ascii_case(b"f64") {
Some(ValType::F64)
} else if raw.eq_ignore_ascii_case(b"str") {
Some(ValType::Str)
} else if raw.eq_ignore_ascii_case(b"vector") {
Some(ValType::Vector)
} else {
None
}
}
}
/// Index kind (`KIND range|unique`).
///
/// ```
/// use kevy_index::IndexKind;
/// assert_eq!(IndexKind::parse(b"UNIQUE"), Some(IndexKind::Unique));
/// assert_eq!(IndexKind::parse(IndexKind::Agg.tag().as_bytes()), Some(IndexKind::Agg));
/// assert_eq!(IndexKind::parse(b"hash"), None);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum IndexKind {
/// Ordered scan over `(value, key)` pairs.
///
/// ```
/// use kevy_index::{IndexValue, Segment};
/// let mut seg = Segment::new();
/// seg.apply(b"u:1", None, Some(IndexValue::I64(30)));
/// seg.apply(b"u:2", None, Some(IndexValue::I64(20)));
/// let (hits, _) = seg.range(&IndexValue::I64(0), &IndexValue::I64(99), None, 10);
/// assert_eq!(hits[0].0, b"u:2", "value order, not key order");
/// ```
Range,
/// Point lookup by value; duplicates recorded (declarative fence:
/// uniqueness is verified, not write-enforced).
///
/// ```
/// use kevy_index::{IndexValue, Segment};
/// let mut seg = Segment::new();
/// seg.apply(b"u:1", None, Some(IndexValue::Str(b"a@x".to_vec())));
/// seg.apply(b"u:2", None, Some(IndexValue::Str(b"a@x".to_vec())));
/// // the second write is not refused; the lookup shows the duplicate
/// assert_eq!(seg.eq(&IndexValue::Str(b"a@x".to_vec()), 10).len(), 2);
/// ```
Unique,
/// Full-text: the field tokenizes into an inverted segment
/// (kevy-text); queried with `MATCH`, BM25-ranked.
///
/// ```
/// use kevy_index::{FieldSpec, IndexKind, IndexSpec, ValType};
/// let fields = vec![FieldSpec::new("title").with_weight(2.0), FieldSpec::new("body")];
/// let text = IndexSpec::builder("posts", "post:", IndexKind::Text, ValType::Str)
/// .with_fields(fields)
/// .build()?;
/// assert_eq!(text.fields().len(), 2, "only text serves several fields");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
Text,
/// ANN: the field holds an f32 LE vector indexed in an HNSW
/// graph (kevy-vector); queried with `KNN`, distance-ranked.
///
/// ```
/// use kevy_index::{AnnSpec, IndexKind, IndexSpec, ValType};
/// let b = IndexSpec::builder("emb", "doc:", IndexKind::Ann, ValType::Vector).with_field("v");
/// assert!(b.clone().build().is_err(), "an ANN index needs its parameters");
/// assert_eq!(b.with_ann(AnnSpec::new(128)).build()?.ann(), Some(AnnSpec::new(128)));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
Ann,
/// Aggregate: per-group count/sum/min/max of the field,
/// grouped by `IndexSpec::group_by`; queried with `GROUP`/`GROUPS`.
///
/// ```
/// use kevy_index::{IndexKind, IndexSpec, ValType};
/// let spend = IndexSpec::builder("spend", "o:", IndexKind::Agg, ValType::F64)
/// .with_field("total")
/// .with_group_by("region")
/// .build()?;
/// assert_eq!(spend.group_by(), Some(&b"region"[..]));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
Agg,
}
impl IndexKind {
/// Wire tag.
pub fn tag(self) -> &'static str {
match self {
IndexKind::Range => "range",
IndexKind::Unique => "unique",
IndexKind::Text => "text",
IndexKind::Ann => "ann",
IndexKind::Agg => "agg",
}
}
/// Parse a wire tag.
pub fn parse(raw: &[u8]) -> Option<IndexKind> {
if raw.eq_ignore_ascii_case(b"range") {
Some(IndexKind::Range)
} else if raw.eq_ignore_ascii_case(b"unique") {
Some(IndexKind::Unique)
} else if raw.eq_ignore_ascii_case(b"text") {
Some(IndexKind::Text)
} else if raw.eq_ignore_ascii_case(b"ann") {
Some(IndexKind::Ann)
} else if raw.eq_ignore_ascii_case(b"agg") {
Some(IndexKind::Agg)
} else {
None
}
}
}
/// Lifecycle state.
///
/// ```
/// use kevy_index::{Catalog, IndexKind, IndexSpec, IndexState, ValType};
/// let mut c = Catalog::new();
/// c.create(IndexSpec::builder("age", "u:", IndexKind::Range, ValType::I64).with_field("age").build()?)?;
/// assert_eq!(c.get(b"age").map(|(_, st)| st), Some(IndexState::Building));
/// assert!(c.set_state(b"age", IndexState::Ready));
/// assert_eq!(c.get(b"age").map(|(_, st)| st), Some(IndexState::Ready));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum IndexState {
/// Backfill in progress; queries answer `-INDEXBUILDING`.
///
/// ```
/// use kevy_index::{Catalog, IndexKind, IndexSpec, IndexState, ValType};
/// let mut c = Catalog::new();
/// c.create(IndexSpec::builder("age", "u:", IndexKind::Range, ValType::I64).with_field("age").build()?)?;
/// assert_eq!(c.iter().next().map(|(_, st)| st), Some(IndexState::Building), "every index starts here");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
Building,
/// Serving.
///
/// ```
/// use kevy_index::{Catalog, IndexKind, IndexSpec, IndexState, ValType};
/// let mut c = Catalog::new();
/// c.create(IndexSpec::builder("age", "u:", IndexKind::Range, ValType::I64).with_field("age").build()?)?;
/// c.set_state(b"age", IndexState::Ready); // backfill finished
/// let serving = c.matching(b"u:1").filter(|(_, st)| *st == IndexState::Ready).count();
/// assert_eq!(serving, 1);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
Ready,
/// Build aborted over budget; queries answer an error.
///
/// ```
/// use kevy_index::{Catalog, IndexKind, IndexSpec, IndexState, ValType};
/// let mut c = Catalog::new();
/// let spec = IndexSpec::builder("age", "u:", IndexKind::Range, ValType::I64).with_field("age");
/// c.create(spec.with_max_bytes(4096).build()?)?;
/// // the runtime's backfill passed max_bytes and gave up
/// c.set_state(b"age", IndexState::FailedOverBudget);
/// assert_eq!(c.get(b"age").map(|(_, st)| st), Some(IndexState::FailedOverBudget));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
FailedOverBudget,
}
/// Hard cap on declared indexes.
pub const MAX_INDEXES: usize = 64;
/// The registry. The runtime holds one per process behind an RCU-style
/// swap; shards read their clone lock-free.
///
/// ```
/// use kevy_index::{Catalog, CatalogError, IndexKind, IndexSpec, ValType};
/// let spec = |name: &str, prefix: &str| {
/// IndexSpec::builder(name, prefix, IndexKind::Range, ValType::I64).with_field("n").build()
/// };
/// let mut c = Catalog::new();
/// assert!(c.is_empty());
/// c.create(spec("age", "user:")?)?;
/// c.create(spec("ttl", "sess:")?)?;
/// assert!(matches!(c.create(spec("age", "x:")?), Err(CatalogError::Exists(_))));
/// assert_eq!(c.len(), 2);
/// let hit: Vec<_> = c.matching(b"user:7").map(|(s, _)| s.name().to_vec()).collect();
/// assert_eq!(hit, [b"age".to_vec()]);
/// assert_eq!(c.get(b"ttl").map(|(s, _)| s.prefix()), Some(&b"sess:"[..]));
/// assert!(c.drop_index(b"ttl") && !c.drop_index(b"ttl"));
/// assert_eq!(c.iter().count(), 1);
///
/// // the sidecar text restores the declarations, every one as Building
/// let back = Catalog::from_sidecar(&c.to_sidecar()).ok_or("sidecar did not parse")?;
/// assert_eq!(back.get(b"age").map(|(s, _)| s.name()), Some(&b"age"[..]));
/// assert!(Catalog::from_sidecar("not a sidecar").is_none());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, Default)]
pub struct Catalog {
pub(crate) specs: Vec<(IndexSpec, IndexState)>,
/// The global indexes' partitionings, by name; any other is local.
pub(crate) parts: Vec<(Vec<u8>, crate::Partitioning)>,
}
impl Catalog {
/// Empty catalog.
pub fn new() -> Self {
Self::default()
}
/// Register a new index. Errors on duplicate name / cap; the spec
/// itself is consistent by construction.
pub fn create(&mut self, spec: IndexSpec) -> Result<(), CatalogError> {
if self.specs.len() >= MAX_INDEXES {
return Err(CatalogError::Full(Declared::Index));
}
if self.specs.iter().any(|(s, _)| s.name == spec.name) {
return Err(CatalogError::Exists(Declared::Index));
}
self.specs.push((spec, IndexState::Building));
Ok(())
}
/// Drop by name; `false` if absent.
pub fn drop_index(&mut self, name: &[u8]) -> bool {
let before = self.specs.len();
self.specs.retain(|(s, _)| s.name != name);
self.parts.retain(|(n, _)| n != name);
self.specs.len() != before
}
/// Set an index's lifecycle state; `false` if absent.
pub fn set_state(&mut self, name: &[u8], state: IndexState) -> bool {
for (s, st) in &mut self.specs {
if s.name == name {
*st = state;
return true;
}
}
false
}
/// Look up by name.
pub fn get(&self, name: &[u8]) -> Option<(&IndexSpec, IndexState)> {
self.specs.iter().find(|(s, _)| s.name == name).map(|(s, st)| (s, *st))
}
/// All specs with states, declaration order.
pub fn iter(&self) -> impl Iterator<Item = (&IndexSpec, IndexState)> {
self.specs.iter().map(|(s, st)| (s, *st))
}
/// Number of declared indexes.
pub fn len(&self) -> usize {
self.specs.len()
}
/// Whether no indexes are declared (the write hook's fast path).
pub fn is_empty(&self) -> bool {
self.specs.is_empty()
}
/// The write-path matcher: indexes whose prefix domain contains
/// `key`. Linear over ≤64 specs with a memcmp each — the compiled
/// trie of the RFC becomes worthwhile only past this cap, so the
/// simple form IS the fast form at our scale.
pub fn matching<'a>(
&'a self,
key: &'a [u8],
) -> impl Iterator<Item = (&'a IndexSpec, IndexState)> {
self.specs.iter().filter(move |(s, _)| key.starts_with(&s.prefix)).map(|(s, st)| (s, *st))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::FieldSpec;
fn spec(name: &str, prefix: &str) -> IndexSpec {
builder(name, prefix).build().unwrap()
}
fn builder(name: &str, prefix: &str) -> crate::IndexSpecBuilder {
IndexSpec::builder(name, prefix, IndexKind::Range, ValType::I64).with_field("age")
}
#[test]
fn create_drop_match_lifecycle() {
let mut c = Catalog::new();
c.create(spec("a", "user:")).unwrap();
c.create(spec("b", "sess:")).unwrap();
assert!(c.create(spec("a", "x:")).is_err(), "dup name");
assert_eq!(c.matching(b"user:42").count(), 1);
assert_eq!(c.matching(b"other:1").count(), 0);
assert_eq!(c.get(b"a").unwrap().1, IndexState::Building);
assert!(c.set_state(b"a", IndexState::Ready));
assert_eq!(c.get(b"a").unwrap().1, IndexState::Ready);
assert!(c.drop_index(b"b"));
assert!(!c.drop_index(b"b"));
assert_eq!(c.len(), 1);
}
#[test]
fn sidecar_roundtrip_with_escapes() {
let mut c = Catalog::new();
let s = IndexSpec::builder("weird", "pre\tfix:", IndexKind::Range, ValType::I64)
.with_field(b"f%\n".to_vec())
.with_max_bytes(1024);
c.create(s.build().unwrap()).unwrap();
let text = c.to_sidecar();
let c2 = Catalog::from_sidecar(&text).unwrap();
let (got, st) = c2.get(b"weird").unwrap();
assert_eq!(got.prefix, b"pre\tfix:".to_vec());
assert_eq!(got.field(), b"f%\n");
assert_eq!(got.max_bytes, 1024);
assert_eq!(st, IndexState::Building, "boot loads as Building");
assert!(Catalog::from_sidecar("bogus").is_none());
}
/// Text serves several fields; every other kind reads one scalar,
/// so a second field there would be declared and never consulted.
/// Accept-and-ignore is the shape this refuses.
#[test]
fn only_text_indexes_accept_several_fields() {
let two = || vec![FieldSpec::new(b"title".to_vec()), FieldSpec::new(b"body".to_vec())];
let range = builder("multi-range", "p:").with_fields(two());
assert!(range.build().is_err(), "range must refuse two fields");
let text = IndexSpec::builder("multi-text", "p:", IndexKind::Text, ValType::Str);
let text = text.with_fields(two()).build().expect("text must accept them");
assert!(Catalog::new().create(text).is_ok());
}
#[test]
fn an_index_needs_at_least_one_field() {
let s = builder("nofields", "p:").with_fields(Vec::new());
assert!(s.build().is_err());
}
#[test]
fn cap_enforced() {
let mut c = Catalog::new();
for i in 0..MAX_INDEXES {
c.create(spec(&format!("i{i}"), "p:")).unwrap();
}
assert!(c.create(spec("over", "p:")).is_err());
}
}