ic-db 0.26.7

Experimental embedded Internet Computer database
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
//! Tighten the selected traversal by mandatory leading-key predicates and continuation.
//! Uses the admitted fixed prefix; this does not select new paths.

use super::{Comparison, Plan, Request, Term, TermOperand};
use crate::{EntitySchema, Error, IndexSchema, store_key::StoreKey};
use std::{cmp::Ordering, ops::Bound};

pub(super) type Bounds = (Bound<StoreKey>, Bound<StoreKey>);

// Only an inclusive endpoint can be observed as the final physical map key.
pub(super) fn terminal_key(bounds: &Bounds, descending: bool) -> Option<&StoreKey> {
    match if descending { &bounds.0 } else { &bounds.1 } {
        Bound::Included(key) => Some(key),
        _ => None,
    }
}

// Skip an entire encoded prefix, including every trailing component/owner.
// Namespace framing guarantees a successor, even for all-FF component bytes.
fn successor(mut bytes: Vec<u8>) -> Result<Vec<u8>, Error> {
    while bytes.last() == Some(&255) {
        bytes.pop();
    }
    *bytes.last_mut().ok_or(Error::InvalidQuery)? += 1;

    Ok(bytes)
}

struct Endpoint {
    key: StoreKey,
    inclusive: bool,
}
impl Endpoint {
    // Equal endpoints tighten only toward exclusion, at either end of a range.
    fn tighten(&mut self, key: StoreKey, inclusive: bool, direction: Ordering) {
        let order = key.cmp(&self.key);
        if order == direction || (order == Ordering::Equal && !inclusive) {
            *self = Self { key, inclusive };
        }
    }

    fn into_bound(self) -> Bound<StoreKey> {
        if self.inclusive {
            Bound::Included(self.key)
        } else {
            Bound::Excluded(self.key)
        }
    }
}

// A physical component names the complete scalar. Siblings share the parent slot.
fn matches_field(entity: &EntitySchema, term: &Term, path: &str) -> bool {
    let parent = entity.fields[term.position].name;
    match &term.operand {
        TermOperand::Nested { member, .. }
        | TermOperand::Length {
            member: Some(member),
            ..
        } => path.split_once('.') == Some((parent, member.field().name)),
        _ => path == parent,
    }
}

fn literal(term: &Term) -> Option<(&[u8], bool)> {
    match &term.operand {
        TermOperand::Literal { bytes, absent }
        | TermOperand::Nested {
            literal: bytes,
            absent,
            ..
        } => Some((bytes, *absent)),
        _ => None,
    }
}

// Report the actual encoded operand used to tighten this path. A residual
// operand may exceed the physical key bound even when its query is valid.
fn bound_operand<'a>(
    entity: &EntitySchema,
    term: &'a Term,
    secondary: bool,
    prefix_len: usize,
) -> Result<Option<(Comparison, &'a [u8])>, Error> {
    // All present values share the 01 tag on an optional secondary component.
    if secondary && term.tests_presence && entity.fields[term.position].optional {
        return Ok((prefix_len < crate::MAX_INDEX_KEY_BYTES).then_some((Comparison::Ge, &[1])));
    }
    let Some((bytes, _)) = literal(term) else {
        return Ok(None);
    };
    let operand = match term.comparison {
        Comparison::Eq | Comparison::Lt | Comparison::Le | Comparison::Gt | Comparison::Ge => bytes,
        // Drop the terminator but retain escaping and the optional-present tag.
        Comparison::StartsWith if secondary => {
            bytes.strip_suffix(&[0, 0]).ok_or(Error::InvalidQuery)?
        }
        _ => return Ok(None),
    };

    Ok((prefix_len + operand.len() <= crate::MAX_INDEX_KEY_BYTES)
        .then_some((term.comparison, operand)))
}

// A non-unique index stores the owner key immediately after all declared
// components. Once they are fixed, owner comparisons bound complete keys.
fn next_field<'a>(
    entity: &'a EntitySchema,
    index: Option<&'a IndexSchema>,
    fixed: usize,
) -> Option<(&'a str, bool)> {
    if let Some(index) = index {
        if let Some(field) = index.fields.get(fixed) {
            return Some((field, true));
        }
        if index.unique {
            return None;
        }
    }

    Some((entity.primary_key, false))
}

// Both path ranking and key-only counting use the same encodable range proof.
pub(super) fn bounds_key_term(
    entity: &EntitySchema,
    index: Option<&IndexSchema>,
    fixed: usize,
    term: &Term,
    prefix_len: usize,
) -> bool {
    next_field(entity, index, fixed).is_some_and(|(name, secondary)| {
        matches_field(entity, term, name)
            && matches!(
                bound_operand(entity, term, secondary, prefix_len),
                Ok(Some(_))
            )
    })
}

fn bounds_index_clause(
    entity: &EntitySchema,
    path: &super::order::IndexPath<'_>,
    clause: &[Term],
) -> bool {
    if let [term] = clause {
        return bounds_key_term(
            entity,
            Some(path.index),
            path.fixed,
            term,
            path.prefix.len(),
        );
    }

    next_field(entity, Some(path.index), path.fixed).is_some_and(|(field, _)| {
        membership_interval(entity, field, clause, path.prefix.len()).is_some()
    })
}

// Rank only bounds the traversal can actually encode on the next index field.
pub(super) fn has_secondary_bound(
    entity: &EntitySchema,
    plan: &Plan<'_>,
    path: &super::order::IndexPath<'_>,
) -> bool {
    plan.clauses().any(|clause| {
        if let [term] = clause
            && let Some((bytes, _)) = literal(term)
        {
            let field = match &term.operand {
                TermOperand::Nested { member, .. } => member.field(),
                _ => &entity.fields[term.position],
            };
            // The lowest/highest possible fixed-width key excludes no candidates
            // at an inclusive bound. An optional field has a separate absence tag.
            if !field.optional
                && crate::key::fixed_width(crate::key::operand_type(field.value))
                    == Some(bytes.len())
                && match term.comparison {
                    Comparison::Ge => bytes.iter().all(|&b| b == 0),
                    Comparison::Le => bytes.iter().all(|&b| b == u8::MAX),
                    _ => false,
                }
            {
                return false;
            }
        }
        bounds_index_clause(entity, path, clause)
    })
}

// A disjunction of literal equalities on one key component fits one inclusive scan between
// its extrema. The original predicate still rejects every gap in that scan.
fn membership_interval<'a>(
    entity: &EntitySchema,
    field: &str,
    clause: &'a [Term],
    prefix_len: usize,
) -> Option<(&'a [u8], &'a [u8])> {
    let mut operands = clause.iter().map(|term| {
        if matches_field(entity, term, field) && term.comparison == Comparison::Eq {
            bound_operand(entity, term, false, prefix_len)
                .ok()
                .flatten()
                .map(|(_, bytes)| bytes)
        } else {
            None
        }
    });
    let mut low = operands.next()??;
    let mut high = low;
    for operand in operands {
        let key = operand?;
        low = low.min(key);
        high = high.max(key);
    }

    Some((low, high))
}

// Membership on the last component of a physical key supplies all possible keys. Other
// key-only clauses can narrow that set before we spend any storage probes.
pub(super) fn point_members<'a>(
    entity: &EntitySchema,
    plan: &'a Plan<'_>,
    request: &Request<'_>,
    allow_row_checks: bool,
) -> Option<impl ExactSizeIterator<Item = (&'a [u8], bool)>> {
    if plan.terms.len() < 2 {
        return None;
    }
    let position = match plan.index {
        None => plan.primary,
        Some(index) if index.is_unique_lookup(plan.fixed_components + 1) => {
            entity.field_position(index.fields[plan.fixed_components])?
        }
        // Non-unique indexes append the owner ID after all declared components.
        Some(index) if !index.unique && plan.fixed_components == index.fields.len() => plan.primary,
        Some(_) => return None,
    };
    let field = entity.fields[position].name;
    let (seed_index, (seed, literals)) =
        plan.clauses()
            .zip(request.clauses())
            .enumerate()
            .find(|(_, (clause, _))| {
                clause.len() > 1
                    && clause.iter().all(|term| {
                        term.position == position
                            && term.comparison == Comparison::Eq
                            && matches!(term.operand, TermOperand::Literal { .. })
                    })
            })?;
    let mut members = Vec::new();
    for (term, predicate) in seed.iter().zip(literals) {
        let (key, absent) = literal(term)?;
        if absent {
            // An admitted present-only index has no physical key for absence.
            plan.index.filter(|index| index.present_only)?;
            continue;
        }
        // This is an exact, complete key. One too large to store cannot match.
        if plan.prefix.len() + key.len() > crate::MAX_INDEX_KEY_BYTES {
            continue;
        }
        let super::QueryOperand::Literal(value) = &predicate.operand else {
            return None;
        };
        // Keep physical key order so existence stops at the same first candidate as a scan.
        if let Err(at) = members.binary_search_by_key(&key, |&(existing, _, _)| existing) {
            members.insert(at, (key, value, false));
        }
    }
    for (i, (clause, terms)) in request.clauses().zip(plan.clauses()).enumerate() {
        if i == seed_index {
            continue;
        }
        members.retain_mut(|(key, value, unresolved)| {
            let lookup = |name: &str| {
                if name == field {
                    Some(**value)
                } else {
                    super::order::fixed_prefix_value(request, plan, name)
                }
            };
            let mut needs_row = false;
            for (predicate, term) in clause.iter().zip(terms) {
                // Same-field literal bounds already have the candidate's key framing.
                let result = if term.position == position
                    && term.comparison.is_relational()
                    && let TermOperand::Literal { bytes, absent } = &term.operand
                {
                    Some(term.comparison.accepts(
                        Some(key),
                        (!absent).then_some(bytes.as_slice()),
                        entity.fields[position].optional,
                    ))
                } else {
                    super::order::fixed_comparison(entity, predicate, &lookup)
                };
                match result {
                    Some(true) => return true,
                    Some(false) => {}
                    None => needs_row = true,
                }
            }
            // A later mandatory clause may still exclude a candidate that needs its row.
            *unresolved |= needs_row;
            needs_row
        });
    }
    if members.len() > request.work
        || (!allow_row_checks && members.iter().any(|(_, _, unresolved)| *unresolved))
    {
        return None;
    }
    Some(
        members
            .into_iter()
            .map(|(key, _, unresolved)| (key, unresolved)),
    )
}

// A secondary candidate must retain every bound the primary scan could seek.
pub(super) fn preserves_primary_bounds(
    entity: &EntitySchema,
    plan: &Plan<'_>,
    path: &super::order::IndexPath<'_>,
) -> bool {
    plan.clauses().all(|clause| {
        let primary_bound = match clause {
            [term] => {
                term.position == plan.primary
                    && matches!(
                        bound_operand(entity, term, false, plan.prefix.len()),
                        Ok(Some(_))
                    )
            }
            _ => {
                membership_interval(entity, entity.primary_key, clause, plan.prefix.len()).is_some()
            }
        };

        !primary_bound || bounds_index_clause(entity, path, clause)
    })
}

pub(super) fn bounds(
    entity: &EntitySchema,
    request: &Request<'_>,
    plan: &Plan<'_>,
) -> Result<Option<Bounds>, Error> {
    if super::order::has_conflicting_predicates(entity, request, plan.index) {
        return Ok(None);
    }
    let mut lower = Endpoint {
        key: StoreKey::new(plan.prefix.clone())?,
        inclusive: true,
    };
    let full_unique = plan
        .index
        .is_some_and(|index| index.is_unique_lookup(plan.fixed_components));
    let mut upper = if full_unique {
        Endpoint {
            key: lower.key.clone(),
            inclusive: true,
        }
    } else {
        Endpoint {
            key: StoreKey::new(successor(plan.prefix.clone())?)?,
            inclusive: false,
        }
    };
    if let Some((field, secondary)) = next_field(entity, plan.index, plan.fixed_components) {
        for clause in plan.clauses() {
            let [term] = clause else {
                if let Some((low, high)) =
                    membership_interval(entity, field, clause, plan.prefix.len())
                {
                    lower.tighten(
                        StoreKey::new([plan.prefix.as_slice(), low].concat())?,
                        true,
                        Ordering::Greater,
                    );
                    // Secondary components can have trailing fields and owner IDs.
                    // Stop after all keys sharing the greatest member's prefix.
                    let end = [plan.prefix.as_slice(), high].concat();
                    let end = if secondary { successor(end)? } else { end };
                    upper.tighten(StoreKey::new(end)?, !secondary, Ordering::Less);
                }
                continue;
            };
            if !matches_field(entity, term, field) {
                continue;
            }
            let Some((comparison, operand)) =
                bound_operand(entity, term, secondary, plan.prefix.len())?
            else {
                continue;
            };
            let mut bytes = plan.prefix.clone();
            bytes.extend_from_slice(operand);
            let end = if secondary
                && matches!(
                    comparison,
                    Comparison::Eq | Comparison::Gt | Comparison::Le | Comparison::StartsWith
                ) {
                successor(bytes.clone())?
            } else {
                bytes.clone()
            };
            let key = StoreKey::new(bytes)?;
            let end = StoreKey::new(end)?;
            // Less-than predicates require presence too. Their upper bound alone
            // would include the optional absent component (00) before present (01).
            if secondary
                && entity.fields[term.position].optional
                && matches!(comparison, Comparison::Lt | Comparison::Le)
            {
                let mut present = plan.prefix.clone();
                present.push(1);
                lower.tighten(StoreKey::new(present)?, true, Ordering::Greater);
            }

            match comparison {
                Comparison::Eq | Comparison::StartsWith => {
                    lower.tighten(key, true, Ordering::Greater);
                    upper.tighten(end, !secondary, Ordering::Less);
                }
                Comparison::Gt => lower.tighten(end, secondary, Ordering::Greater),
                Comparison::Ge => lower.tighten(key, true, Ordering::Greater),
                Comparison::Lt => upper.tighten(key, false, Ordering::Less),
                Comparison::Le => upper.tighten(end, !secondary, Ordering::Less),
                _ => {}
            }
        }
    }
    if let Some(cursor) = request.cursor {
        let key = StoreKey::new(cursor.position.clone())?;

        // Old or caller-selected positions must never widen the predicate range.
        if plan.descending {
            upper.tighten(key, cursor.resume == super::Resume::Retry, Ordering::Less);
        } else {
            lower.tighten(
                key,
                cursor.resume == super::Resume::Retry,
                Ordering::Greater,
            );
        }
    }
    // Do not hand contradictory or equal-exclusive bounds to the supplier.
    if lower.key > upper.key || (lower.key == upper.key && !(lower.inclusive && upper.inclusive)) {
        return Ok(None);
    }

    Ok(Some((lower.into_bound(), upper.into_bound())))
}