infinite-db 0.4.0

A spatial-graph database using n-dimensional curves and hyperedges for engineering logic.
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//! Concurrent query execution over sealed blocks + live tail.

use std::collections::{BTreeMap, HashMap};
use std::sync::atomic::{AtomicU32, Ordering};

use crate::infinitedb_core::{
    address::{DimensionVector, RevisionId, SpaceId},
    block::Record,
    hlc::SessionId,
    hilbert_key::{CachedHilbertKey, HilbertKey},
    record_identity::{AddressKey, RecordIdentityKey},
    snapshot::BlockIndexEntry,
    space::SpaceRegistry,
};
use crate::infinitedb_index::composite::KeyConfig;
use crate::infinitedb_index::key::{hilbert_key_for, hilbert_key_standard};
use crate::infinitedb_index::range_decompose::{
    block_overlaps_intervals, decompose_bbox, key_in_intervals, KeyInterval,
};

use super::hilbert_shard::{hilbert_shard_id, ShardRef};
use crate::infinitedb_storage::nvme::BlockStore;

use crate::infinitedb_core::branch::BranchId;

use super::branch_overlay::BranchOverlayStore;
use super::session::{SessionWatermarks, VersionVector};
use super::hilbert_live_tails::HilbertLiveTails;
use super::live_tail::LiveTailView;
use super::snapshot_store::SnapshotStore;

/// Return the cached Hilbert key on `record`, or compute it when unset (legacy blocks).
pub fn record_hilbert_key(spaces: &SpaceRegistry, record: &Record) -> HilbertKey {
    if let Some(k) = record.hilbert_key.get() {
        k
    } else {
        HilbertKey(space_key(spaces, record.address.space, &record.address.point))
    }
}

pub fn space_key(spaces: &SpaceRegistry, space: SpaceId, point: &DimensionVector) -> u128 {
    match spaces.get(space) {
        Some(config) => hilbert_key_for(point, KeyConfig {
            bits_per_dim: config.bits_per_dim,
        }),
        None => hilbert_key_standard(point),
    }
}

/// Address-only identity for latest-wins grouping.
pub fn address_key(spaces: &SpaceRegistry, record: &Record) -> AddressKey {
    AddressKey::from_hilbert(record, record_hilbert_key(spaces, record))
}

/// Full record identity for seal deduplication.
pub fn record_identity_key(spaces: &SpaceRegistry, record: &Record) -> RecordIdentityKey {
    RecordIdentityKey::from_hilbert(record, record_hilbert_key(spaces, record))
}

/// Ensure `record` carries a cached Hilbert key, computing from coordinates when unset.
pub fn ensure_record_hilbert_key(spaces: &SpaceRegistry, record: &mut Record) -> HilbertKey {
    if let Some(k) = record.hilbert_key.get() {
        return k;
    }
    let k = HilbertKey(space_key(spaces, record.address.space, &record.address.point));
    record.hilbert_key = CachedHilbertKey::set(k);
    k
}

/// Resolve Hilbert keys and sort records for block seal.
pub fn prepare_records_for_seal(spaces: &SpaceRegistry, records: &mut [Record]) {
    for record in records.iter_mut() {
        ensure_record_hilbert_key(spaces, record);
    }
    records.sort_by_key(|r| (r.hilbert_key.get().unwrap(), r.revision));
}

fn live_tail_for_space(
    spaces: &SpaceRegistry,
    space: SpaceId,
    live_tail: Option<&LiveTailView>,
    hilbert_live_tails: Option<&HilbertLiveTails>,
    shard_filter: Option<ShardRef>,
) -> Vec<Record> {
    if let Some(hilbert) = hilbert_live_tails {
        let views = hilbert.views_for_space(space);
        if !views.is_empty() {
            let mut records = Vec::new();
            for view in views {
                if let Some(shard) = shard_filter {
                    let has_records = view.tail_iter().any(|r| {
                        shard.contains_key(record_hilbert_key(spaces, r))
                    });
                    let has_blocks = view.blocks.iter().any(|(min_key, _)| {
                        shard.contains_key(*min_key)
                    });
                    if !has_records && !has_blocks {
                        continue;
                    }
                }
                records.extend(view.tail_iter().cloned());
            }
            return records
                .into_iter()
                .filter(|r| r.address.space == space)
                .collect();
        }
    }
    live_tail
        .map(|t| t.snapshot())
        .unwrap_or_default()
        .into_iter()
        .filter(|r| r.address.space == space)
        .collect()
}

#[derive(Clone, Copy)]
enum KeyFilter<'a> {
    All,
    Single(HilbertKey, HilbertKey),
    Intervals(&'a [KeyInterval]),
}

fn block_entries_from_snapshot(
    snapshot: &crate::infinitedb_core::snapshot::Snapshot,
    key_filter: KeyFilter<'_>,
) -> Vec<(HilbertKey, BlockIndexEntry)> {
    let overlaps = |min_key: HilbertKey, max_key: HilbertKey| match key_filter {
        KeyFilter::All => true,
        KeyFilter::Single(lo, hi) => min_key <= hi && max_key >= lo,
        KeyFilter::Intervals(intervals) => block_overlaps_intervals(min_key, max_key, intervals),
    };
    snapshot
        .blocks
        .iter()
        .filter(|(min_key, entry)| overlaps(**min_key, entry.max_key))
        .map(|(k, e)| (*k, e.clone()))
        .collect()
}

fn block_entries_for_space(
    spaces: &SpaceRegistry,
    space: SpaceId,
    snapshots: &SnapshotStore,
    key_filter: KeyFilter<'_>,
    live_tail: Option<&LiveTailView>,
    hilbert_live_tails: Option<&HilbertLiveTails>,
    shard_filter: Option<ShardRef>,
) -> Vec<(HilbertKey, BlockIndexEntry)> {
    let overlaps = |min_key: HilbertKey, max_key: HilbertKey| match key_filter {
        KeyFilter::All => true,
        KeyFilter::Single(lo, hi) => min_key <= hi && max_key >= lo,
        KeyFilter::Intervals(intervals) => block_overlaps_intervals(min_key, max_key, intervals),
    };

    if let Some(hilbert) = hilbert_live_tails {
        let views = hilbert.views_for_space(space);
        if !views.is_empty() {
            let mut entries = Vec::new();
            for view in views {
                if let Some(shard) = shard_filter {
                    let shard_match = view.blocks.iter().any(|(min_key, _)| {
                        shard.contains_key(*min_key)
                    }) || view.tail_iter().any(|r| {
                        shard.contains_key(record_hilbert_key(spaces, r))
                    });
                    if !shard_match {
                        continue;
                    }
                }
                for (min_key, entry) in view.blocks.iter() {
                    if overlaps(*min_key, entry.max_key) {
                        entries.push((*min_key, entry.clone()));
                    }
                }
            }
            return entries;
        }
    }
    let _ = live_tail;
    snapshots
        .get(space)
        .map(|snapshot| {
            snapshot
                .blocks
                .iter()
                .filter(|(min_key, entry)| overlaps(**min_key, entry.max_key))
                .map(|(k, e)| (*k, e.clone()))
                .collect()
        })
        .unwrap_or_default()
}

fn record_matches_filter(
    spaces: &SpaceRegistry,
    record: &Record,
    key_filter: KeyFilter<'_>,
) -> bool {
    match key_filter {
        KeyFilter::All => true,
        KeyFilter::Single(lo, hi) => {
            let k = record_hilbert_key(spaces, record);
            if lo == hi {
                k == lo
            } else {
                k >= lo && k <= hi
            }
        }
        KeyFilter::Intervals(intervals) => {
            key_in_intervals(record_hilbert_key(spaces, record), intervals)
        }
    }
}

/// Query plan instrumentation for frame performance-contract tests (M6).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct QueryPlanStats {
    pub interval_scans: u32,
}

static QUERY_INTERVAL_SCANS: AtomicU32 = AtomicU32::new(0);

/// Reset the global interval-scan counter (tests).
pub fn reset_query_plan_stats() {
    QUERY_INTERVAL_SCANS.store(0, Ordering::Relaxed);
}

/// Read accumulated interval-scan count since last reset.
pub fn query_plan_stats() -> QueryPlanStats {
    QueryPlanStats {
        interval_scans: QUERY_INTERVAL_SCANS.load(Ordering::Relaxed),
    }
}

fn record_interval_scan(count: u32) {
    QUERY_INTERVAL_SCANS.fetch_add(count, Ordering::Relaxed);
}

/// Scalar or per-session revision ceiling for frame queries (Phase 5).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FrameTimePin {
    Scalar(RevisionId),
    Vector(VersionVector),
}

impl FrameTimePin {
    /// Scalar ceiling for diagnosis and legacy callers.
    pub fn scalar_ceiling(&self) -> RevisionId {
        match self {
            FrameTimePin::Scalar(r) => *r,
            FrameTimePin::Vector(v) => v.scalar_meet(),
        }
    }
}

fn record_visible_at<F>(record: &Record, pin: &FrameTimePin, stable_for_session: F) -> bool
where
    F: Fn(SessionId) -> RevisionId,
{
    match pin {
        FrameTimePin::Scalar(ceiling) => record.revision <= *ceiling,
        FrameTimePin::Vector(vector) => {
            let session = SessionId(record.revision.session());
            let ceiling = vector
                .get(session)
                .unwrap_or_else(|| stable_for_session(session));
            record.revision <= ceiling
        }
    }
}

/// Per-session visibility under a strict vector pin (D-P6: absent session → invisible).
pub(crate) fn record_visible_at_strict_pinned(
    record: &Record,
    vector: &VersionVector,
) -> bool {
    let session = SessionId(record.revision.session());
    vector
        .get(session)
        .is_some_and(|ceiling| record.revision <= ceiling)
}

/// Latest-wins visibility with a strict version-vector pin (ReadTxn / D-P6).
pub(crate) fn resolve_visibility_strict_pin(
    spaces: &SpaceRegistry,
    candidates: Vec<Record>,
    vector: &VersionVector,
    include_tombstones: bool,
) -> Vec<Record> {
    if include_tombstones {
        return candidates;
    }

    let mut latest: HashMap<AddressKey, Record> = HashMap::new();
    for record in candidates {
        if !record_visible_at_strict_pinned(&record, vector) {
            continue;
        }
        let key = address_key(spaces, &record);
        let replace = match latest.get(&key) {
            None => true,
            Some(existing) => record.revision > existing.revision,
        };
        if replace {
            latest.insert(key, record);
        }
    }

    latest
        .into_values()
        .filter(|r| !r.tombstone)
        .collect()
}

/// Apply latest-wins visibility with a scalar or version-vector ceiling (Phase 5).
pub(crate) fn resolve_visibility_with_pin<F>(
    spaces: &SpaceRegistry,
    candidates: Vec<Record>,
    pin: &FrameTimePin,
    include_tombstones: bool,
    stable_for_session: F,
) -> Vec<Record>
where
    F: Fn(SessionId) -> RevisionId,
{
    if include_tombstones {
        return candidates;
    }

    let mut latest: HashMap<AddressKey, Record> = HashMap::new();
    for record in candidates {
        if !record_visible_at(&record, pin, &stable_for_session) {
            continue;
        }
        let key = address_key(spaces, &record);
        let replace = match latest.get(&key) {
            None => true,
            Some(existing) => record.revision > existing.revision,
        };
        if replace {
            latest.insert(key, record);
        }
    }

    latest
        .into_values()
        .filter(|r| !r.tombstone)
        .collect()
}

/// Apply latest-wins visibility per address identity.
///
/// An address is visible iff its highest revision at or below `rev_ceiling` is a
/// live record; that record is the one returned. When `include_tombstones` is true,
/// all candidate records are returned unchanged (full revision history).
pub(crate) fn resolve_visibility(
    spaces: &SpaceRegistry,
    candidates: Vec<Record>,
    rev_ceiling: RevisionId,
    include_tombstones: bool,
) -> Vec<Record> {
    if include_tombstones {
        return candidates;
    }

    let mut latest: HashMap<AddressKey, Record> = HashMap::new();
    for record in candidates {
        if record.revision > rev_ceiling {
            continue;
        }
        let key = address_key(spaces, &record);
        let replace = match latest.get(&key) {
            None => true,
            Some(existing) => record.revision > existing.revision,
        };
        if replace {
            latest.insert(key, record);
        }
    }

    latest
        .into_values()
        .filter(|r| !r.tombstone)
        .collect()
}

/// Query sealed blocks and live tail(s) for `space`.
///
/// Visibility rule: an address is visible iff its highest revision ≤ the ceiling
/// is a live record; exactly one record per address is returned unless
/// `include_tombstones` is set (which preserves full revision history).
///
/// When `pinned_vector` is set, visibility is per-session (D-P6): a record is
/// visible iff `record.revision ≤ vector[record.session]` and the session is
/// present in the pin.
pub fn query_inner(
    store: &BlockStore,
    snapshots: &SnapshotStore,
    live_tail: Option<&LiveTailView>,
    spaces: &SpaceRegistry,
    watermark: &SessionWatermarks,
    space: SpaceId,
    key_range: Option<(u128, u128)>,
    as_of: Option<RevisionId>,
    pinned_vector: Option<&VersionVector>,
    include_tombstones: bool,
    hilbert_live_tails: Option<&HilbertLiveTails>,
    branch_overlays: Option<&BranchOverlayStore>,
    branch_id: Option<BranchId>,
) -> std::io::Result<Vec<Record>> {
    let rev_ceiling = pinned_vector
        .map(|v| v.fetch_ceiling())
        .unwrap_or_else(|| as_of.unwrap_or_else(|| watermark.allocated()));

    let key_filter = match key_range {
        None => KeyFilter::All,
        Some((lo, hi)) => KeyFilter::Single(HilbertKey(lo), HilbertKey(hi)),
    };

    let on_branch = branch_id.is_some_and(|b| b != BranchId::MAIN);

    let tail = if on_branch {
        match (branch_overlays, branch_id) {
            (Some(overlays), Some(branch)) => overlays.live_records(branch, space),
            _ => Vec::new(),
        }
    } else {
        live_tail_for_space(
            spaces,
            space,
            live_tail,
            hilbert_live_tails,
            None,
        )
    };

    let mut candidates: Vec<Record> = Vec::new();

    let block_entries = if let (Some(overlays), Some(branch)) = (branch_overlays, branch_id) {
        if branch != BranchId::MAIN {
            let mut entries = overlays
                .base_snapshot(branch, space)
                .map(|base| block_entries_from_snapshot(base.as_ref(), key_filter))
                .unwrap_or_default();
            for (min_key, entry) in overlays.sealed_blocks(branch, space) {
                let overlaps = match key_filter {
                    KeyFilter::All => true,
                    KeyFilter::Single(lo, hi) => min_key <= hi && entry.max_key >= lo,
                    KeyFilter::Intervals(intervals) => {
                        block_overlaps_intervals(min_key, entry.max_key, intervals)
                    }
                };
                if overlaps {
                    entries.push((min_key, entry));
                }
            }
            entries
        } else {
            block_entries_for_space(
                spaces,
                space,
                snapshots,
                key_filter,
                live_tail,
                hilbert_live_tails,
                None,
            )
        }
    } else {
        block_entries_for_space(
            spaces,
            space,
            snapshots,
            key_filter,
            live_tail,
            hilbert_live_tails,
            None,
        )
    };
    for (_, entry) in block_entries {
        let block = store.read_block_shared(entry.block_id)?;
        for record in block.records.iter() {
            if record.address.space != space || record.revision > rev_ceiling {
                continue;
            }
            if !record_matches_filter(spaces, record, key_filter) {
                continue;
            }
            candidates.push(record.clone());
        }
    }

    for record in tail {
        if record.address.space != space || record.revision > rev_ceiling {
            continue;
        }
        if !record_matches_filter(spaces, &record, key_filter) {
            continue;
        }
        candidates.push(record);
    }

    if let Some(vector) = pinned_vector {
        Ok(resolve_visibility_strict_pin(
            spaces,
            candidates,
            vector,
            include_tombstones,
        ))
    } else {
        Ok(resolve_visibility(
            spaces,
            candidates,
            rev_ceiling,
            include_tombstones,
        ))
    }
}

pub fn query_bbox(
    store: &BlockStore,
    snapshots: &SnapshotStore,
    live_tail: Option<&LiveTailView>,
    spaces: &SpaceRegistry,
    watermark: &SessionWatermarks,
    space: SpaceId,
    min: DimensionVector,
    max: DimensionVector,
    as_of: Option<RevisionId>,
    pinned_vector: Option<&VersionVector>,
    hilbert_live_tails: Option<&HilbertLiveTails>,
    branch_overlays: Option<&BranchOverlayStore>,
    branch_id: Option<BranchId>,
) -> std::io::Result<Vec<Record>> {
    assert_eq!(min.dims(), max.dims(), "min and max must have equal dimensions");
    let bits = spaces
        .get(space)
        .map(|c| c.bits_per_dim)
        .unwrap_or(8);
    let shard_bits = Some(ShardRef::shard_bits_for_space(spaces, space));
    let intervals = decompose_bbox(&min, &max, bits);
    let _ = intervals;
    record_interval_scan(1);
    let rev_ceiling = pinned_vector
        .map(|v| v.fetch_ceiling())
        .unwrap_or_else(|| as_of.unwrap_or_else(|| watermark.allocated()));

    let shard_filter = shard_bits.map(|sb| {
        let mut shard_ids = std::collections::BTreeSet::new();
        for interval in &intervals {
            shard_ids.insert(hilbert_shard_id(interval.lo.raw(), sb));
            shard_ids.insert(hilbert_shard_id(interval.hi.raw(), sb));
        }
        shard_ids
    });

    let on_branch = branch_id.is_some_and(|b| b != BranchId::MAIN);
    let key_filter = KeyFilter::Intervals(&intervals);

    let tail = if on_branch {
        match (branch_overlays, branch_id) {
            (Some(overlays), Some(branch)) => overlays.live_records(branch, space),
            _ => Vec::new(),
        }
    } else {
        live_tail_for_space(
            spaces,
            space,
            live_tail,
            hilbert_live_tails,
            None,
        )
    };

    let mut candidates = Vec::new();
    let block_entries = if let (Some(overlays), Some(branch)) = (branch_overlays, branch_id) {
        if branch != BranchId::MAIN {
            let mut entries = overlays
                .base_snapshot(branch, space)
                .map(|base| block_entries_from_snapshot(base.as_ref(), key_filter))
                .unwrap_or_default();
            for (min_key, entry) in overlays.sealed_blocks(branch, space) {
                if block_overlaps_intervals(min_key, entry.max_key, &intervals) {
                    entries.push((min_key, entry));
                }
            }
            entries
        } else {
            block_entries_for_space(
                spaces,
                space,
                snapshots,
                key_filter,
                live_tail,
                hilbert_live_tails,
                shard_bits.map(|sb| {
                    let first = intervals
                        .first()
                        .map(|i| hilbert_shard_id(i.lo.raw(), sb))
                        .unwrap_or(0);
                    ShardRef::new(first, sb)
                }),
            )
        }
    } else {
        block_entries_for_space(
            spaces,
            space,
            snapshots,
            key_filter,
            live_tail,
            hilbert_live_tails,
            shard_bits.map(|sb| {
                let first = intervals
                    .first()
                    .map(|i| hilbert_shard_id(i.lo.raw(), sb))
                    .unwrap_or(0);
                ShardRef::new(first, sb)
            }),
        )
    };
    for (_, entry) in block_entries {
        let block = store.read_block_shared(entry.block_id)?;
        for record in block.records.iter() {
            if record.address.space != space || record.revision > rev_ceiling {
                continue;
            }
            if !record_matches_filter(spaces, record, KeyFilter::Intervals(&intervals)) {
                continue;
            }
            candidates.push(record.clone());
        }
    }
    for record in tail {
        if record.address.space != space || record.revision > rev_ceiling {
            continue;
        }
        if let Some(ref shards) = shard_filter {
            let sb = ShardRef::shard_bits_for_space(spaces, space);
            let sid = hilbert_shard_id(record_hilbert_key(spaces, &record).raw(), sb);
            if !shards.contains(&sid) {
                continue;
            }
        }
        if !record_matches_filter(spaces, &record, KeyFilter::Intervals(&intervals)) {
            continue;
        }
        candidates.push(record);
    }

    let mut results = if let Some(vector) = pinned_vector {
        resolve_visibility_strict_pin(spaces, candidates, vector, false)
    } else {
        resolve_visibility(spaces, candidates, rev_ceiling, false)
    };
    results.retain(|r| r.address.point.within(&min, &max));
    Ok(results)
}

pub fn snapshots_map_for_persist(snapshots: &SnapshotStore) -> BTreeMap<u64, crate::infinitedb_core::snapshot::Snapshot> {
    snapshots
        .all()
        .into_iter()
        .map(|(k, v)| (k.0, (*v).clone()))
        .collect()
}