mushroomdb-rules 0.5.2

Rule evaluation engine for mushroomdb: declarative predicates and trigger logic
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
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
//! Materialized property views — incremental per-node derived properties.
//!
//! ## Storage choice
//!
//! View values are stored as regular entries in the `ColumnStore` under their
//! `view_prop` name, updated in place on each triggering event.
//!
//! **Why ColumnStore in-place**:
//! - Zero query-layer changes: every read path (scan, filter, project, group)
//!   already reads from `ColumnStore` — view props appear automatically.
//! - `ColumnStore::set` / `remove` handle cleanup; `remove_all` clears a
//!   deleted node's view values as part of the normal tombstone sweep.
//! - Rebuild-on-open recomputes values from persisted graph state (topo + props)
//!   without touching the snapshot format.
//! - No virtual-column overlay, no side-table, no special query path.
//!
//! **Trade-off**: view props appear in `node_info()` alongside real props.
//! That is intentional — "they're column values" per the brief.
//!
//! ## MIN/MAX retraction cost
//!
//! Retracting an edge whose endpoint held the current MIN or MAX requires
//! rescanning all remaining neighbors (O(degree)) because there is no
//! sorted structure tracking the second-best value.  This is documented
//! as the v1 cost; no auxiliary structures are maintained.
//!
//! ## Subscriptions interplay (v1)
//!
//! View-value updates do **not** emit subscription events.  Only user-write
//! WAL records and rule fire/retract deltas generate `DbEvent`s.  View
//! maintenance writes to `ColumnStore` directly and is invisible to the
//! subscription layer.  This preserves T1's `pending_deltas` discipline:
//! the `debug_assert!(pending_delta_count == 0)` at `log_then_apply_with`
//! entry remains green through view-heavy workloads because view updates
//! bypass the engine's delta path entirely.

use core_storage::v8::layout::ArchivedColumns;
use core_storage::v8::seam::{ColumnsView, TopologyView};
use core_storage::{ColumnStore, Direction, IdMap, Interner, Value};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum AggFn {
    Sum,
    Avg,
    Min,
    Max,
    Count,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ViewSource {
    Degree {
        edge_type: String,
        direction: Direction,
    },
    NeighborAgg {
        edge_type: String,
        direction: Direction,
        agg: AggFn,
        /// Neighbor property to aggregate.
        prop: String,
    },
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ViewDef {
    pub name: String,
    /// Node label this view applies to.
    pub label: String,
    /// The synthetic property name written into `ColumnStore`.
    pub view_prop: String,
    pub source: ViewSource,
}

impl ViewDef {
    pub fn validate(&self) -> Result<(), String> {
        if self.name.is_empty() {
            return Err("view name must not be empty".into());
        }
        if self.label.is_empty() {
            return Err("view label must not be empty".into());
        }
        if self.view_prop.is_empty() {
            return Err("view_prop must not be empty".into());
        }
        match &self.source {
            ViewSource::Degree { edge_type, .. } => {
                if edge_type.is_empty() {
                    return Err("Degree edge_type must not be empty".into());
                }
            }
            ViewSource::NeighborAgg {
                edge_type, prop, ..
            } => {
                if edge_type.is_empty() {
                    return Err("NeighborAgg edge_type must not be empty".into());
                }
                if prop.is_empty() {
                    return Err("NeighborAgg prop must not be empty".into());
                }
            }
        }
        Ok(())
    }

    fn edge_type(&self) -> &str {
        match &self.source {
            ViewSource::Degree { edge_type, .. } => edge_type,
            ViewSource::NeighborAgg { edge_type, .. } => edge_type,
        }
    }

    fn direction(&self) -> Direction {
        match &self.source {
            ViewSource::Degree { direction, .. } => *direction,
            ViewSource::NeighborAgg { direction, .. } => *direction,
        }
    }
}

// ---------------------------------------------------------------------------
// ViewStore
// ---------------------------------------------------------------------------

#[derive(Debug, Default, Clone)]
pub struct ViewStore {
    /// Ordered by name.
    views: BTreeMap<String, ViewDef>,
}

impl ViewStore {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn views(&self) -> impl Iterator<Item = &ViewDef> {
        self.views.values()
    }

    pub fn is_empty(&self) -> bool {
        self.views.is_empty()
    }

    pub fn has_view(&self, name: &str) -> bool {
        self.views.contains_key(name)
    }

    /// If `prop_name` is managed by any view, return that view's name.
    pub fn view_for_prop(&self, prop_name: &str) -> Option<&str> {
        self.views
            .values()
            .find(|v| v.view_prop == prop_name)
            .map(|v| v.name.as_str())
    }

    // -----------------------------------------------------------------------
    // DDL
    // -----------------------------------------------------------------------

    /// Register a view and backfill values for all existing nodes.
    ///
    /// # Errors
    /// - Duplicate view name.
    /// - `view_prop` already claimed by another view.
    /// - `view_prop` already present as a real node property in `ColumnStore`.
    pub fn create_view(
        &mut self,
        def: ViewDef,
        props: &mut ColumnStore,
        topo: &TopologyView<'_>,
        ids: &IdMap,
        syms: &Interner,
        labels: &[u32],
    ) -> Result<(), String> {
        def.validate()?;

        if self.views.contains_key(&def.name) {
            return Err(format!("view {:?} already exists", def.name));
        }
        if let Some(existing) = self.views.values().find(|v| v.view_prop == def.view_prop) {
            return Err(format!(
                "view_prop {:?} is already used by view {:?}",
                def.view_prop, existing.name
            ));
        }
        // Real-prop collision: any ColumnStore field not owned by a view.
        let view_props: std::collections::HashSet<&str> =
            self.views.values().map(|v| v.view_prop.as_str()).collect();
        if props
            .fields()
            .any(|f| f == def.view_prop && !view_props.contains(f))
        {
            return Err(format!(
                "view_prop {:?} conflicts with an existing node property",
                def.view_prop
            ));
        }

        // Backfill: compute initial values for all existing nodes.
        backfill_view(&def, props, topo, ids, syms, labels);

        self.views.insert(def.name.clone(), def);
        Ok(())
    }

    /// Restore a view definition from a snapshot without collision checking or
    /// backfilling.  The snapshot's `ColumnStore` already contains view values;
    /// this method simply registers the definition so the store is aware of it.
    ///
    /// Called only from `open_with` during snapshot restore.  Callers must then
    /// call `rebuild_all` to ensure values are correct after WAL replay.
    pub fn restore_view(&mut self, def: ViewDef) -> Result<(), String> {
        def.validate()?;
        if self.views.contains_key(&def.name) {
            return Ok(()); // idempotent during snapshot restore
        }
        self.views.insert(def.name.clone(), def);
        Ok(())
    }

    /// Remove a view and delete its values from every node.
    ///
    /// # Errors
    /// - View not found.
    pub fn delete_view(
        &mut self,
        name: &str,
        props: &mut ColumnStore,
        ids: &IdMap,
        labels: &[u32],
        syms: &Interner,
    ) -> Result<(), String> {
        let def = self
            .views
            .remove(name)
            .ok_or_else(|| format!("view {:?} not found", name))?;

        // Remove the view-prop value from every matching node.
        if let Some(label_sym) = syms.get(&def.label) {
            for id in 0..ids.len() as u32 {
                if labels.get(id as usize).copied() == Some(label_sym) {
                    props.remove(id, &def.view_prop);
                }
            }
        }
        Ok(())
    }

    // -----------------------------------------------------------------------
    // Incremental maintenance
    // -----------------------------------------------------------------------

    /// Called when an edge of type `etype` (symbol) between `src` and `dst` is
    /// inserted (`inserted=true`) or deleted (`inserted=false`).
    ///
    /// Covers both manual user edges and derived engine edges.  The topo must
    /// already reflect the new state (edge added before this call on insert;
    /// edge removed before this call on delete) so that full-recompute paths
    /// (Avg, Min, Max) see correct neighbor sets.
    #[allow(clippy::too_many_arguments)]
    pub fn on_edge_changed(
        &self,
        etype: u32,
        src: u32,
        dst: u32,
        inserted: bool,
        props: &mut ColumnStore,
        topo: &TopologyView<'_>,
        ids: &IdMap,
        syms: &Interner,
        labels: &[u32],
        base_cols: Option<&ArchivedColumns>,
    ) {
        for def in self.views.values() {
            let Some(et_sym) = syms.get(def.edge_type()) else {
                continue;
            };
            if et_sym != etype {
                continue;
            }
            let direction = def.direction();
            // Determine which node is the "subject" whose view value changes.
            // direction=Out: subject is src (counts/aggregates its out-neighbors)
            // direction=In:  subject is dst (counts/aggregates its in-neighbors)
            let subject = match direction {
                Direction::Out => src,
                Direction::In => dst,
            };
            // The "neighbor" whose property is read for NeighborAgg.
            let neighbor = match direction {
                Direction::Out => dst,
                Direction::In => src,
            };
            update_node_view(
                def, subject, neighbor, inserted, props, topo, ids, syms, labels, base_cols,
            );
        }
    }

    /// Called after `SetProp` / `RemoveProp` — finds NeighborAgg views that
    /// read `field` from neighbors and recomputes values for all subject nodes.
    #[allow(clippy::too_many_arguments)]
    pub fn on_prop_changed(
        &self,
        changed_node: u32,
        field: &str,
        props: &mut ColumnStore,
        topo: &TopologyView<'_>,
        ids: &IdMap,
        syms: &Interner,
        labels: &[u32],
        base_cols: Option<&ArchivedColumns>,
    ) {
        for def in self.views.values() {
            let ViewSource::NeighborAgg {
                edge_type,
                direction,
                prop,
                ..
            } = &def.source
            else {
                continue;
            };
            if prop != field {
                continue;
            }
            let Some(et_sym) = syms.get(edge_type) else {
                continue;
            };
            // Find subject nodes that have changed_node as a neighbor via et_sym/direction.
            // direction=Out: subjects X where edge X→changed_node exists → In-neighbors of changed_node
            // direction=In:  subjects X where edge changed_node→X exists → Out-neighbors of changed_node
            let reverse_dir = match direction {
                Direction::Out => Direction::In,
                Direction::In => Direction::Out,
            };
            let subjects: Vec<u32> = topo.neighbors(et_sym, reverse_dir, changed_node).to_vec();
            for subject in subjects {
                // Full recompute using the full base+overlay props view.
                // Build the ColumnsView in a block so the immutable borrow of `props`
                // ends before we write back.
                let val = {
                    let pv = build_cols_view(props, base_cols);
                    compute_view_value(def, subject, pv, topo, ids, syms, labels)
                };
                match val {
                    Some(v) => props.set(subject, &def.view_prop, v),
                    None => {
                        props.remove(subject, &def.view_prop);
                    }
                }
            }
        }
    }

    /// Initialize view values for a newly inserted node.
    ///
    /// Sets Degree, Count, and Sum views to their zero values (0 / 0.0).
    /// Avg / Min / Max are left absent — there is no sensible neutral value
    /// when no neighbors have been observed yet.
    ///
    /// Call this BEFORE the rule engine's `on_node_changed` for the new node so
    /// that subsequent `on_edge_changed` calls (from derived-edge deltas) can
    /// increment correctly from a known baseline.
    pub fn init_node_views(
        &self,
        node: u32,
        props: &mut ColumnStore,
        syms: &Interner,
        labels: &[u32],
    ) {
        for def in self.views.values() {
            let Some(label_sym) = syms.get(&def.label) else {
                continue;
            };
            if labels.get(node as usize).copied() != Some(label_sym) {
                continue;
            }
            match &def.source {
                ViewSource::Degree { .. } => {
                    if props.get(node, &def.view_prop).is_none() {
                        props.set(node, &def.view_prop, Value::Int(0));
                    }
                }
                ViewSource::NeighborAgg {
                    agg: AggFn::Count, ..
                } => {
                    if props.get(node, &def.view_prop).is_none() {
                        props.set(node, &def.view_prop, Value::Int(0));
                    }
                }
                ViewSource::NeighborAgg {
                    agg: AggFn::Sum, ..
                } => {
                    // Always set to 0.0 — init is only called for freshly-inserted
                    // nodes whose view_prop does not yet exist.
                    props.set(node, &def.view_prop, Value::Float(0.0));
                }
                _ => {} // Avg / Min / Max: absent until neighbors exist
            }
        }
    }

    // -----------------------------------------------------------------------
    // Rebuild
    // -----------------------------------------------------------------------

    /// Recompute all view values from scratch.  Called on open after WAL
    /// replay so values are consistent with persisted graph state.
    pub fn rebuild_all(
        &self,
        props: &mut ColumnStore,
        topo: &TopologyView<'_>,
        ids: &IdMap,
        syms: &Interner,
        labels: &[u32],
    ) {
        for def in self.views.values() {
            backfill_view(def, props, topo, ids, syms, labels);
        }
    }
}

// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------

/// Build a `ColumnsView` that reads from both overlay props and (when present)
/// the archived base columns.  Used in update/recompute paths so view values
/// that live in the base after a V8 snapshot open are visible alongside
/// overlay-only props added during WAL replay or live mutations.
fn build_cols_view<'a>(
    overlay: &'a ColumnStore,
    base_cols: Option<&'a ArchivedColumns>,
) -> ColumnsView<'a> {
    match base_cols {
        None => ColumnsView::owned(overlay),
        Some(b) => ColumnsView::with_base(overlay, b),
    }
}

/// Compute and store the view value for every node matching `def.label`.
fn backfill_view(
    def: &ViewDef,
    props: &mut ColumnStore,
    topo: &TopologyView<'_>,
    ids: &IdMap,
    syms: &Interner,
    labels: &[u32],
) {
    let Some(label_sym) = syms.get(&def.label) else {
        return;
    };
    let Some(et_sym) = syms.get(def.edge_type()) else {
        // Edge type not yet interned → no such edges exist.
        // Set zero for Degree / Count / Sum; leave absent for Avg / Min / Max.
        for id in 0..ids.len() as u32 {
            if labels.get(id as usize).copied() == Some(label_sym) {
                match &def.source {
                    ViewSource::Degree { .. } => {
                        props.set(id, &def.view_prop, Value::Int(0));
                    }
                    ViewSource::NeighborAgg {
                        agg: AggFn::Count, ..
                    } => {
                        props.set(id, &def.view_prop, Value::Int(0));
                    }
                    ViewSource::NeighborAgg {
                        agg: AggFn::Sum, ..
                    } => {
                        props.set(id, &def.view_prop, Value::Float(0.0));
                    }
                    _ => {}
                }
            }
        }
        return;
    };
    for id in 0..ids.len() as u32 {
        if labels.get(id as usize).copied() != Some(label_sym) {
            continue;
        }
        match compute_view_value(
            def,
            id,
            ColumnsView::owned(&*props),
            topo,
            ids,
            syms,
            labels,
        ) {
            Some(val) => props.set(id, &def.view_prop, val),
            None => {
                props.remove(id, &def.view_prop);
                // Degree and Count always yield Some; only Avg/Min/Max
                // return None when there are no qualifying neighbors.
            }
        }
        // Ensure Degree and Count always have a value (even 0).
        // compute_view_value already returns Some(Int(0)) for empty neighbor sets
        // in Degree and Count, so no extra step needed here.
        let _ = et_sym;
    }
}

/// Compute the view value for a single node.  Returns `None` when there are
/// no qualifying neighbors for Avg/Min/Max (no sensible neutral value).
pub fn compute_view_value(
    def: &ViewDef,
    node: u32,
    props: ColumnsView<'_>,
    topo: &TopologyView<'_>,
    _ids: &IdMap,
    syms: &Interner,
    labels: &[u32],
) -> Option<Value> {
    // Label check.
    let label_sym = syms.get(&def.label)?;
    if labels.get(node as usize).copied() != Some(label_sym) {
        return None;
    }

    // If the edge_type has never been used it is not in the symbol table.
    // There are zero such edges, so Degree/Count return 0, Sum returns 0.0,
    // and Avg/Min/Max are absent.
    let et_sym = match syms.get(def.edge_type()) {
        Some(s) => s,
        None => {
            return match &def.source {
                ViewSource::Degree { .. } => Some(Value::Int(0)),
                ViewSource::NeighborAgg {
                    agg: AggFn::Count, ..
                } => Some(Value::Int(0)),
                ViewSource::NeighborAgg {
                    agg: AggFn::Sum, ..
                } => Some(Value::Float(0.0)),
                ViewSource::NeighborAgg { .. } => None,
            };
        }
    };
    let direction = def.direction();
    let neighbors = topo.neighbors(et_sym, direction, node);

    match &def.source {
        ViewSource::Degree { .. } => Some(Value::Int(neighbors.len() as i64)),
        ViewSource::NeighborAgg { agg, prop, .. } => match agg {
            AggFn::Count => Some(Value::Int(
                neighbors
                    .iter()
                    .filter(|&&n| props.get(n, prop).is_some())
                    .count() as i64,
            )),
            AggFn::Sum => {
                let mut sum = 0.0f64;
                for &nbr in neighbors.as_ref() {
                    if let Some(vr) = props.get(nbr, prop) {
                        if let Some(n) = as_float(vr.as_value()) {
                            sum += n;
                        }
                    }
                }
                Some(Value::Float(sum))
            }
            AggFn::Avg => {
                let mut sum = 0.0f64;
                let mut count = 0usize;
                for &nbr in neighbors.as_ref() {
                    if let Some(vr) = props.get(nbr, prop) {
                        if let Some(n) = as_float(vr.as_value()) {
                            sum += n;
                            count += 1;
                        }
                    }
                }
                if count == 0 {
                    None
                } else {
                    Some(Value::Float(sum / count as f64))
                }
            }
            AggFn::Min => {
                let mut best: Option<f64> = None;
                for &nbr in neighbors.as_ref() {
                    if let Some(vr) = props.get(nbr, prop) {
                        if let Some(n) = as_float(vr.as_value()) {
                            best = Some(best.map_or(n, |m: f64| m.min(n)));
                        }
                    }
                }
                best.map(Value::Float)
            }
            AggFn::Max => {
                let mut best: Option<f64> = None;
                for &nbr in neighbors.as_ref() {
                    if let Some(vr) = props.get(nbr, prop) {
                        if let Some(n) = as_float(vr.as_value()) {
                            best = Some(best.map_or(n, |m: f64| m.max(n)));
                        }
                    }
                }
                best.map(Value::Float)
            }
        },
    }
}

/// Update `subject`'s view value after one edge change.
/// `neighbor` is the endpoint whose property is read for NeighborAgg.
/// `inserted`: true = edge was added, false = edge was removed.
///
/// The topo must already reflect the new state before this is called.
/// Uses full recompute from `topo` (base+overlay) and `base_cols` (for reading
/// neighbor props from base when the overlay is empty after a V8 snapshot open).
#[allow(clippy::too_many_arguments)]
fn update_node_view(
    def: &ViewDef,
    subject: u32,
    neighbor: u32,
    inserted: bool,
    props: &mut ColumnStore,
    topo: &TopologyView<'_>,
    ids: &IdMap,
    syms: &Interner,
    labels: &[u32],
    base_cols: Option<&ArchivedColumns>,
) {
    // Label check: only subjects with the matching label get this view.
    let Some(label_sym) = syms.get(&def.label) else {
        return;
    };
    if labels.get(subject as usize).copied() != Some(label_sym) {
        return;
    }

    match &def.source {
        ViewSource::Degree { direction, .. } => {
            // Degree: full recompute from topo so the count is always correct
            // even when the current value lives in the base (V8 snapshot open).
            let et_sym = match syms.get(def.edge_type()) {
                Some(s) => s,
                None => {
                    props.set(subject, &def.view_prop, Value::Int(0));
                    return;
                }
            };
            let count = topo.neighbors(et_sym, *direction, subject).len() as i64;
            props.set(subject, &def.view_prop, Value::Int(count));
        }
        ViewSource::NeighborAgg { .. } => {
            // Full recompute using the merged base+overlay props view.
            // Build the ColumnsView in a block so the immutable borrow of `props`
            // ends before the write, satisfying the borrow checker under NLL.
            let val = {
                let pv = build_cols_view(props, base_cols);
                compute_view_value(def, subject, pv, topo, ids, syms, labels)
            };
            match val {
                Some(v) => props.set(subject, &def.view_prop, v),
                None => {
                    props.remove(subject, &def.view_prop);
                }
            }
            // Ensure Degree/Count/Sum always have a value even when the
            // agg result is absent (e.g. Sum=0.0 when no neighbors yet).
            // compute_view_value already returns Some for those cases.
        }
    }
    let _ = (neighbor, inserted); // consumed by full-recompute path above
}

fn as_float(v: &Value) -> Option<f64> {
    match v {
        Value::Int(i) => Some(*i as f64),
        Value::Float(f) if f.is_finite() => Some(*f),
        _ => None,
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use core_storage::{IdMap, Interner, Topology};

    fn make_setup() -> (ViewStore, ColumnStore, Topology, IdMap, Interner, Vec<u32>) {
        let mut ids = IdMap::new();
        let mut syms = Interner::new();
        let mut labels = Vec::new();
        let mut topo = Topology::new();
        let mut props = ColumnStore::new();

        // Insert nodes: p0, p1, p2 (label Person), c0 (label City)
        let person_sym = syms.intern("Person");
        let city_sym = syms.intern("City");
        let edge_sym = syms.intern("LIVES_IN");

        for key in &["p0", "p1", "p2"] {
            let id = ids.get_or_insert(key);
            if labels.len() <= id as usize {
                labels.resize(id as usize + 1, u32::MAX);
            }
            labels[id as usize] = person_sym;
        }
        let c0 = ids.get_or_insert("c0");
        if labels.len() <= c0 as usize {
            labels.resize(c0 as usize + 1, u32::MAX);
        }
        labels[c0 as usize] = city_sym;

        // p0 and p1 LIVES_IN c0
        let p0 = ids.get("p0").unwrap();
        let p1 = ids.get("p1").unwrap();
        topo.add_edge(edge_sym, p0, c0);
        topo.add_edge(edge_sym, p1, c0);

        // Set a numeric prop on p0 and p1
        props.set(p0, "score", Value::Float(3.0));
        props.set(p1, "score", Value::Float(7.0));

        (ViewStore::new(), props, topo, ids, syms, labels)
    }

    #[test]
    fn degree_view_basic() {
        let (mut vs, mut props, topo, ids, syms, labels) = make_setup();
        let def = ViewDef {
            name: "city_pop".into(),
            label: "City".into(),
            view_prop: "in_deg".into(),
            source: ViewSource::Degree {
                edge_type: "LIVES_IN".into(),
                direction: Direction::In,
            },
        };
        vs.create_view(
            def,
            &mut props,
            &TopologyView::owned(&topo),
            &ids,
            &syms,
            &labels,
        )
        .unwrap();
        let c0 = ids.get("c0").unwrap();
        assert_eq!(props.get(c0, "in_deg"), Some(&Value::Int(2)));
    }

    #[test]
    fn neighbor_agg_sum() {
        let (mut vs, mut props, topo, ids, syms, labels) = make_setup();
        let def = ViewDef {
            name: "city_score_sum".into(),
            label: "City".into(),
            view_prop: "score_sum".into(),
            source: ViewSource::NeighborAgg {
                edge_type: "LIVES_IN".into(),
                direction: Direction::In,
                agg: AggFn::Sum,
                prop: "score".into(),
            },
        };
        vs.create_view(
            def,
            &mut props,
            &TopologyView::owned(&topo),
            &ids,
            &syms,
            &labels,
        )
        .unwrap();
        let c0 = ids.get("c0").unwrap();
        // p0=3.0 + p1=7.0
        assert_eq!(props.get(c0, "score_sum"), Some(&Value::Float(10.0)));
    }

    #[test]
    fn neighbor_agg_count_skips_missing_prop() {
        let (mut vs, mut props, topo, ids, syms, labels) = make_setup();
        let p1 = ids.get("p1").unwrap();
        props.remove(p1, "score");
        let def = ViewDef {
            name: "city_score_n".into(),
            label: "City".into(),
            view_prop: "score_n".into(),
            source: ViewSource::NeighborAgg {
                edge_type: "LIVES_IN".into(),
                direction: Direction::In,
                agg: AggFn::Count,
                prop: "score".into(),
            },
        };
        vs.create_view(
            def,
            &mut props,
            &TopologyView::owned(&topo),
            &ids,
            &syms,
            &labels,
        )
        .unwrap();
        let c0 = ids.get("c0").unwrap();
        assert_eq!(props.get(c0, "score_n"), Some(&Value::Int(1)));
    }

    #[test]
    fn view_prop_collision_rejected() {
        let (mut vs, mut props, topo, ids, syms, labels) = make_setup();
        // Insert a real prop with the same name
        let p0 = ids.get("p0").unwrap();
        props.set(p0, "collision_prop", Value::Int(1));
        let def = ViewDef {
            name: "test_view".into(),
            label: "Person".into(),
            view_prop: "collision_prop".into(),
            source: ViewSource::Degree {
                edge_type: "LIVES_IN".into(),
                direction: Direction::Out,
            },
        };
        let err = vs
            .create_view(
                def,
                &mut props,
                &TopologyView::owned(&topo),
                &ids,
                &syms,
                &labels,
            )
            .unwrap_err();
        assert!(
            err.contains("conflicts with an existing node property"),
            "{err}"
        );
    }

    #[test]
    fn delete_view_removes_values() {
        let (mut vs, mut props, topo, ids, syms, labels) = make_setup();
        let def = ViewDef {
            name: "city_pop".into(),
            label: "City".into(),
            view_prop: "in_deg".into(),
            source: ViewSource::Degree {
                edge_type: "LIVES_IN".into(),
                direction: Direction::In,
            },
        };
        vs.create_view(
            def,
            &mut props,
            &TopologyView::owned(&topo),
            &ids,
            &syms,
            &labels,
        )
        .unwrap();
        let c0 = ids.get("c0").unwrap();
        assert!(props.get(c0, "in_deg").is_some());
        vs.delete_view("city_pop", &mut props, &ids, &labels, &syms)
            .unwrap();
        assert!(props.get(c0, "in_deg").is_none());
    }
}