hyphae 1.0.6

Reactive cells and runtime primitives for rship
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
//! Reactive grouped view over a [`CellMap`], indexed by foreign key.
//!
//! `NestedMap<PK, K, V>` takes an underlying `CellMap<K, V>` and partitions its
//! entries by a foreign-key extractor `Fn(&V) -> PK`.  It maintains a live
//! reverse index so:
//!
//! - Subscriptions can be scoped to a single parent key.
//! - Child diffs bubbling up through a tree can be re-tagged with the correct
//!   parent key via [`lookup_parent`](NestedMap::lookup_parent).
//! - It implements [`ReactiveKeys`] and [`ReactiveMap`], so joins compose with
//!   it exactly like they do with `CellMap`.
//!
//! `NestedMap` is **read-only** — writes go to the underlying `CellMap`, and
//! the grouped view updates reactively.

use std::{
    collections::{HashMap, HashSet},
    hash::Hash,
    marker::PhantomData,
    sync::Arc,
};

use arc_swap::ArcSwap;
use dashmap::DashMap;

use crate::{
    cell_map::{CellMap, CellMapInner, MapDiff},
    subscription::SubscriptionGuard,
    traits::{
        CellValue,
        reactive_keys::{KeyChange, ReactiveKeys},
        reactive_map::ReactiveMap,
    },
};

/// Internal index state: FK grouping + reverse lookup.
#[derive(Clone)]
struct NestState<PK, K>
where
    PK: Hash + Eq + CellValue,
    K: Hash + Eq + CellValue,
{
    /// parent_key → { child_keys }
    forward: HashMap<PK, HashSet<K>>,
    /// child_key → parent_key
    reverse: HashMap<K, PK>,
}

impl<PK, K> Default for NestState<PK, K>
where
    PK: Hash + Eq + CellValue,
    K: Hash + Eq + CellValue,
{
    fn default() -> Self {
        Self {
            forward: HashMap::new(),
            reverse: HashMap::new(),
        }
    }
}

/// A reactive grouped view over a `CellMap`, indexed by a foreign key.
///
/// Created via [`CellMap::nest`] (or the planned `TreeNode::join`).
///
/// ```text
/// CellMap<OrderId, Order>
///   └─ NestedMap<CustomerId, OrderId, Order>
///        ├── "cust_1" → { "ord_1", "ord_3" }
///        └── "cust_2" → { "ord_2" }
/// ```
pub struct NestedMap<PK, K, V>
where
    PK: Hash + Eq + CellValue,
    K: Hash + Eq + CellValue,
    V: CellValue,
{
    /// The underlying source data (M-erased: CellMapInner doesn't vary by M).
    source_inner: Arc<CellMapInner<K, V>>,
    /// Live FK index, updated reactively.
    state: Arc<ArcSwap<NestState<PK, K>>>,
    /// For concurrent parent-key lookups.
    reverse_cache: Arc<DashMap<K, PK>>,
    /// Subscription guard keeping the index alive.
    _index_guard: Arc<SubscriptionGuard>,
    _marker: PhantomData<V>,
}

impl<PK, K, V> Clone for NestedMap<PK, K, V>
where
    PK: Hash + Eq + CellValue,
    K: Hash + Eq + CellValue,
    V: CellValue,
{
    fn clone(&self) -> Self {
        Self {
            source_inner: self.source_inner.clone(),
            state: self.state.clone(),
            reverse_cache: self.reverse_cache.clone(),
            _index_guard: self._index_guard.clone(),
            _marker: PhantomData,
        }
    }
}

impl<PK, K, V> NestedMap<PK, K, V>
where
    PK: Hash + Eq + CellValue,
    K: Hash + Eq + CellValue,
    V: CellValue,
{
    /// Create a new `NestedMap` by grouping `source` entries via `fk`.
    pub fn new<M: Send + Sync + 'static>(
        source: &CellMap<K, V, M>,
        fk: impl Fn(&V) -> PK + Send + Sync + 'static,
    ) -> Self {
        let state = Arc::new(ArcSwap::from_pointee(NestState::default()));
        let reverse_cache = Arc::new(DashMap::<K, PK>::new());

        let state_for_sub = state.clone();
        let reverse_cache_for_sub = reverse_cache.clone();
        let fk = Arc::new(fk);

        let guard = source.subscribe_diffs(move |diff| {
            fn apply<PK, K, V>(
                st: &mut NestState<PK, K>,
                rc: &DashMap<K, PK>,
                diff: &MapDiff<K, V>,
                fk: &dyn Fn(&V) -> PK,
            ) where
                PK: Hash + Eq + CellValue,
                K: Hash + Eq + CellValue,
                V: CellValue,
            {
                match diff {
                    MapDiff::Initial { entries } => {
                        st.forward.clear();
                        st.reverse.clear();
                        rc.clear();
                        for (k, v) in entries {
                            let pk = fk(v);
                            st.forward.entry(pk.clone()).or_default().insert(k.clone());
                            st.reverse.insert(k.clone(), pk.clone());
                            rc.insert(k.clone(), pk);
                        }
                    }
                    MapDiff::Insert { key, value } => {
                        let pk = fk(value);
                        st.forward
                            .entry(pk.clone())
                            .or_default()
                            .insert(key.clone());
                        st.reverse.insert(key.clone(), pk.clone());
                        rc.insert(key.clone(), pk);
                    }
                    MapDiff::Remove { key, .. } => {
                        if let Some(old_pk) = st.reverse.remove(key)
                            && let Some(set) = st.forward.get_mut(&old_pk)
                        {
                            set.remove(key);
                            if set.is_empty() {
                                st.forward.remove(&old_pk);
                            }
                        }
                        rc.remove(key);
                    }
                    MapDiff::Update { key, new_value, .. } => {
                        let new_pk = fk(new_value);
                        // Remove from old group
                        if let Some(old_pk) = st.reverse.insert(key.clone(), new_pk.clone())
                            && old_pk != new_pk
                            && let Some(set) = st.forward.get_mut(&old_pk)
                        {
                            set.remove(key);
                            if set.is_empty() {
                                st.forward.remove(&old_pk);
                            }
                        }
                        st.forward
                            .entry(new_pk.clone())
                            .or_default()
                            .insert(key.clone());
                        rc.insert(key.clone(), new_pk);
                    }
                    MapDiff::Batch { changes } => {
                        for change in changes {
                            apply(st, rc, change, fk);
                        }
                    }
                }
            }

            state_for_sub.rcu(|current| {
                let mut next = current.as_ref().clone();
                apply(&mut next, &reverse_cache_for_sub, diff, fk.as_ref());
                next
            });
        });

        Self {
            source_inner: source.inner.clone(),
            state,
            reverse_cache,
            _index_guard: Arc::new(guard),
            _marker: PhantomData,
        }
    }

    /// Look up the parent key for a given child key.
    ///
    /// This is the critical operation for tree diff routing: when a child diff
    /// bubbles up, the parent node calls `lookup_parent` to re-tag it with the
    /// correct parent key.
    pub fn lookup_parent(&self, child_key: &K) -> Option<PK> {
        self.reverse_cache.get(child_key).map(|r| r.value().clone())
    }

    /// Get all child keys currently grouped under `parent_key`.
    pub fn children_of(&self, parent_key: &PK) -> Vec<K> {
        let snapshot = self.state.load();
        snapshot
            .forward
            .get(parent_key)
            .map(|set| set.iter().cloned().collect())
            .unwrap_or_default()
    }

    /// Subscribe to the underlying source's diffs (initial + subsequent).
    fn subscribe_source_diffs(
        &self,
        cb: impl Fn(&MapDiff<K, V>) + Send + Sync + 'static,
    ) -> SubscriptionGuard {
        use crate::traits::Watchable;

        // Emit initial snapshot
        let entries: Vec<(K, V)> = self
            .source_inner
            .data
            .iter()
            .map(|r| (r.key().clone(), r.value().clone()))
            .collect();
        cb(&MapDiff::Initial { entries });

        // Subscribe to subsequent diffs
        let keepalive = self.source_inner.clone();
        let diffs = self.source_inner.diffs_cell.clone().lock();
        let first = Arc::new(std::sync::atomic::AtomicBool::new(true));
        diffs.subscribe(move |signal| {
            let _ = &keepalive;
            if first.swap(false, std::sync::atomic::Ordering::SeqCst) {
                return;
            }
            if let crate::Signal::Value(diff) = signal {
                cb(diff.as_ref());
            }
        })
    }

    /// Subscribe to diffs scoped to a single parent key.
    ///
    /// Only diffs affecting children of `parent_key` are forwarded.
    pub fn subscribe_parent(
        &self,
        parent_key: PK,
        cb: impl Fn(&MapDiff<K, V>) + Send + Sync + 'static,
    ) -> SubscriptionGuard {
        let reverse_cache = self.reverse_cache.clone();
        self.subscribe_source_diffs(move |diff| {
            fn filter_for_parent<PK, K, V>(
                diff: &MapDiff<K, V>,
                _parent_key: &PK,
                _rc: &DashMap<K, PK>,
                fk_match: &dyn Fn(&K) -> bool,
            ) -> Option<MapDiff<K, V>>
            where
                PK: Hash + Eq + CellValue,
                K: Hash + Eq + CellValue,
                V: CellValue,
            {
                match diff {
                    MapDiff::Initial { entries } => {
                        let filtered: Vec<_> = entries
                            .iter()
                            .filter(|(k, _)| fk_match(k))
                            .cloned()
                            .collect();
                        if filtered.is_empty() {
                            None
                        } else {
                            Some(MapDiff::Initial { entries: filtered })
                        }
                    }
                    MapDiff::Insert { key, .. }
                    | MapDiff::Remove { key, .. }
                    | MapDiff::Update { key, .. } => {
                        if fk_match(key) {
                            Some(diff.clone())
                        } else {
                            None
                        }
                    }
                    MapDiff::Batch { changes } => {
                        let filtered: Vec<_> = changes
                            .iter()
                            .filter_map(|c| filter_for_parent(c, _parent_key, _rc, fk_match))
                            .collect();
                        if filtered.is_empty() {
                            None
                        } else {
                            Some(MapDiff::Batch { changes: filtered })
                        }
                    }
                }
            }

            let pk = parent_key.clone();
            let fk_match =
                |k: &K| -> bool { reverse_cache.get(k).is_some_and(|r| *r.value() == pk) };

            if let Some(filtered) = filter_for_parent(diff, &parent_key, &reverse_cache, &fk_match)
            {
                cb(&filtered);
            }
        })
    }

    /// Subscribe to all diffs, tagged with the parent key.
    ///
    /// The callback receives `(parent_key, child_diff)` for every change.
    /// This is used by the tree subscription machinery.
    pub fn subscribe_grouped(
        &self,
        cb: impl Fn(&PK, &MapDiff<K, V>) + Send + Sync + 'static,
    ) -> SubscriptionGuard {
        let reverse_cache = self.reverse_cache.clone();
        let fk_arc = self.state.clone();
        self.subscribe_source_diffs(move |diff| {
            fn route_diff<PK, K, V>(
                diff: &MapDiff<K, V>,
                rc: &DashMap<K, PK>,
                cb: &dyn Fn(&PK, &MapDiff<K, V>),
            ) where
                PK: Hash + Eq + CellValue,
                K: Hash + Eq + CellValue,
                V: CellValue,
            {
                match diff {
                    MapDiff::Initial { entries } => {
                        // Group entries by parent key, emit one Initial per group.
                        let mut groups: HashMap<PK, Vec<(K, V)>> = HashMap::new();
                        for (k, v) in entries {
                            if let Some(pk) = rc.get(k) {
                                groups
                                    .entry(pk.value().clone())
                                    .or_default()
                                    .push((k.clone(), v.clone()));
                            }
                        }
                        for (pk, group_entries) in groups {
                            cb(
                                &pk,
                                &MapDiff::Initial {
                                    entries: group_entries,
                                },
                            );
                        }
                    }
                    MapDiff::Insert { key, .. }
                    | MapDiff::Remove { key, .. }
                    | MapDiff::Update { key, .. } => {
                        if let Some(pk) = rc.get(key) {
                            cb(pk.value(), diff);
                        }
                    }
                    MapDiff::Batch { changes } => {
                        // Group batch members by parent key, emit one Batch per group.
                        let mut groups: HashMap<PK, Vec<MapDiff<K, V>>> = HashMap::new();
                        fn collect_by_parent<PK, K, V>(
                            diff: &MapDiff<K, V>,
                            rc: &DashMap<K, PK>,
                            groups: &mut HashMap<PK, Vec<MapDiff<K, V>>>,
                        ) where
                            PK: Hash + Eq + CellValue,
                            K: Hash + Eq + CellValue,
                            V: CellValue,
                        {
                            match diff {
                                MapDiff::Insert { key, .. }
                                | MapDiff::Remove { key, .. }
                                | MapDiff::Update { key, .. } => {
                                    if let Some(pk) = rc.get(key) {
                                        groups
                                            .entry(pk.value().clone())
                                            .or_default()
                                            .push(diff.clone());
                                    }
                                }
                                MapDiff::Batch { changes } => {
                                    for change in changes {
                                        collect_by_parent(change, rc, groups);
                                    }
                                }
                                MapDiff::Initial { .. } => {
                                    // Initial inside a Batch is unusual but handle it
                                    // by treating each entry as an insert.
                                }
                            }
                        }

                        for change in changes {
                            collect_by_parent(change, rc, &mut groups);
                        }
                        for (pk, diffs) in groups {
                            if diffs.len() == 1 {
                                cb(&pk, &diffs[0]);
                            } else {
                                cb(&pk, &MapDiff::Batch { changes: diffs });
                            }
                        }
                    }
                }
            }

            let _ = &fk_arc; // keep state alive
            route_diff(diff, &reverse_cache, &cb);
        })
    }
}

// ── ReactiveKeys ────────────────────────────────────────────────────────

impl<PK, K, V> ReactiveKeys for NestedMap<PK, K, V>
where
    PK: Hash + Eq + CellValue,
    K: Hash + Eq + CellValue,
    V: CellValue,
{
    type Key = K;

    fn key_set(&self) -> Vec<K> {
        self.source_inner
            .data
            .iter()
            .map(|r| r.key().clone())
            .collect()
    }

    fn contains_key(&self, key: &K) -> bool {
        self.source_inner.data.contains_key(key)
    }

    fn subscribe_keys(
        &self,
        cb: impl Fn(&KeyChange<K>) + Send + Sync + 'static,
    ) -> SubscriptionGuard {
        use crate::cell_map::map_diff_to_key_change;
        self.subscribe_source_diffs(move |diff| {
            if let Some(kc) = map_diff_to_key_change(diff) {
                cb(&kc);
            }
        })
    }
}

// ── ReactiveMap ─────────────────────────────────────────────────────────

impl<PK, K, V> ReactiveMap for NestedMap<PK, K, V>
where
    PK: Hash + Eq + CellValue,
    K: Hash + Eq + CellValue,
    V: CellValue,
{
    type Value = V;

    fn get_value(&self, key: &K) -> Option<V> {
        self.source_inner.data.get(key).map(|r| r.value().clone())
    }

    fn snapshot(&self) -> Vec<(K, V)> {
        self.source_inner
            .data
            .iter()
            .map(|r| (r.key().clone(), r.value().clone()))
            .collect()
    }

    fn subscribe_diffs_reactive(
        &self,
        cb: impl Fn(&MapDiff<K, V>) + Send + Sync + 'static,
    ) -> SubscriptionGuard {
        self.subscribe_source_diffs(cb)
    }
}

// ── Constructor on CellMap ──────────────────────────────────────────────

impl<K, V, M> CellMap<K, V, M>
where
    K: Hash + Eq + CellValue,
    V: CellValue,
    M: Send + Sync + 'static,
{
    /// Create a grouped view of this map, partitioned by a foreign-key
    /// extractor.
    ///
    /// The returned `NestedMap` observes this `CellMap` reactively and
    /// maintains a live FK index. It implements [`ReactiveKeys`] and
    /// [`ReactiveMap`], so it composes with joins exactly like a `CellMap`.
    ///
    /// ```text
    /// let orders: CellMap<OrderId, Order> = CellMap::new();
    /// let nested = orders.nest(|order| order.customer_id.clone());
    /// // nested: NestedMap<CustomerId, OrderId, Order>
    /// ```
    pub fn nest<PK>(&self, fk: impl Fn(&V) -> PK + Send + Sync + 'static) -> NestedMap<PK, K, V>
    where
        PK: Hash + Eq + CellValue,
    {
        NestedMap::new(self, fk)
    }
}