rpstate 0.3.0

Type-safe reactive persistence for Rust GUI apps...
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
use crate::error::Error;
use crate::reactive::change::MapChange;
use crate::reactive::intercept::{InterceptDisposer, InterceptGuard};
use crate::store::Store;
use crate::{AccessMode, DefaultStore, ReadOnlyMode, Result, StoreSubscription, WritableMode};
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::collections::HashMap;
use std::fmt::Display;
use std::hash::Hash;
use std::marker::PhantomData;
use std::str::FromStr;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

pub struct MapSubscription {
    pub(crate) id: u64,
    pub(crate) cleanup: Arc<dyn Fn(u64) + Send + Sync + 'static>,
}

impl Drop for MapSubscription {
    fn drop(&mut self) {
        (self.cleanup)(self.id);
    }
}

pub type InterceptorAny<K, V> =
    Arc<dyn Fn(MapChange<K, V>) -> Option<MapChange<K, V>> + Send + Sync + 'static>;
pub type InterceptorKey<K, V> =
    Arc<dyn Fn(MapChange<K, V>) -> Option<MapChange<K, V>> + Send + Sync + 'static>;
pub type SubscriberAny<K, V> = Arc<dyn Fn(&MapChange<K, V>) + Send + Sync + 'static>;
pub type SubscriberKey<K, V> = Arc<dyn Fn(&MapChange<K, V>) + Send + Sync + 'static>;

pub struct ReactiveMap<K, V, S: Store = DefaultStore, M: AccessMode = ReadOnlyMode> {
    pub path: Arc<str>,
    pub store: Arc<S>,
    pub(crate) _mode: PhantomData<M>,
    pub(crate) _key: PhantomData<K>,
    pub(crate) _value: PhantomData<V>,

    pub(crate) interceptors_any: Arc<Mutex<Vec<(u64, InterceptorAny<K, V>)>>>,
    pub(crate) interceptors_key: Arc<Mutex<HashMap<K, Vec<(u64, InterceptorKey<K, V>)>>>>,

    pub(crate) subscribers_any: Arc<Mutex<Vec<(u64, SubscriberAny<K, V>)>>>,
    pub(crate) subscribers_key: Arc<Mutex<HashMap<K, Vec<(u64, SubscriberKey<K, V>)>>>>,

    pub(crate) next_id: Arc<AtomicU64>,
    pub(crate) intercept_depth: Arc<AtomicUsize>,
    pub(crate) store_sub: Arc<StoreSubscription<S>>,

    pub(crate) known_keys: Arc<Mutex<std::collections::HashSet<K>>>,
}

impl<K, V, S: Store, M: AccessMode> Clone for ReactiveMap<K, V, S, M> {
    fn clone(&self) -> Self {
        Self {
            path: self.path.clone(),
            store: self.store.clone(),
            _mode: PhantomData,
            _key: PhantomData,
            _value: PhantomData,
            interceptors_any: self.interceptors_any.clone(),
            interceptors_key: self.interceptors_key.clone(),
            subscribers_any: self.subscribers_any.clone(),
            subscribers_key: self.subscribers_key.clone(),
            next_id: self.next_id.clone(),
            intercept_depth: self.intercept_depth.clone(),
            store_sub: self.store_sub.clone(),
            known_keys: self.known_keys.clone(),
        }
    }
}

impl<K, V, S, M> ReactiveMap<K, V, S, M>
where
    K: FromStr + Display + Clone + Hash + Eq + Send + Sync + 'static,
    V: Serialize + DeserializeOwned + Clone + Send + Sync + 'static + Default,
    S: Store,
    M: AccessMode,
{
    pub fn get(&self, key: &K) -> Result<Option<V>> {
        let full_path = format!("{}.{}", self.path, key);
        self.store.get(&full_path)
    }

    pub fn contains_key(&self, key: &K) -> Result<bool> {
        self.get(key).map(|v| v.is_some())
    }

    pub fn entries(&self) -> Result<Vec<(K, V)>> {
        let prefix = format!("{}.", self.path);
        let kvs = self.store.scan_prefix(&prefix)?;
        let mut results = Vec::new();
        for (full_path, bytes) in kvs {
            if let Some(key_str) = full_path.strip_prefix(&prefix)
                && let Ok(k) = K::from_str(key_str)
                && let Ok(v) = self.store.decode::<V>(&bytes)
            {
                results.push((k, v));
            }
        }
        Ok(results)
    }

    pub fn len(&self) -> Result<usize> {
        let prefix = format!("{}.", self.path);
        let kvs = self.store.scan_prefix(&prefix)?;
        Ok(kvs.len())
    }

    pub fn is_empty(&self) -> Result<bool> {
        self.len().map(|l| l == 0)
    }

    pub fn subscribe_any<F>(&self, callback: F) -> MapSubscription
    where
        F: Fn(&MapChange<K, V>) + Send + Sync + 'static,
    {
        let id = self.next_id.fetch_add(1, Ordering::Relaxed);
        self.subscribers_any
            .lock()
            .unwrap()
            .push((id, Arc::new(callback)));
        let subs = self.subscribers_any.clone();
        MapSubscription {
            id,
            cleanup: Arc::new(move |id| {
                if let Ok(mut lock) = subs.lock() {
                    lock.retain(|(i, _)| *i != id);
                }
            }),
        }
    }

    pub fn subscribe_key<F>(&self, key: K, callback: F) -> MapSubscription
    where
        F: Fn(&MapChange<K, V>) + Send + Sync + 'static,
    {
        let id = self.next_id.fetch_add(1, Ordering::Relaxed);
        self.subscribers_key
            .lock()
            .unwrap()
            .entry(key.clone())
            .or_default()
            .push((id, Arc::new(callback)));
        let subs = self.subscribers_key.clone();
        MapSubscription {
            id,
            cleanup: Arc::new(move |id| {
                if let Ok(mut lock) = subs.lock()
                    && let Some(list) = lock.get_mut(&key)
                {
                    list.retain(|(i, _)| *i != id);
                }
            }),
        }
    }
}

impl<K, V, S> ReactiveMap<K, V, S, WritableMode>
where
    K: FromStr + Display + Clone + Hash + Eq + Send + Sync + 'static,
    V: Serialize + DeserializeOwned + Default + Clone + Send + Sync + 'static,
    S: Store,
{
    pub fn set(&self, key: K, value: &V) -> Result<()> {
        let old_value = self.get(&key)?;
        if let Some(old) = old_value {
            let change = MapChange::Update {
                key: key.clone(),
                old_value: old,
                new_value: value.clone(),
            };
            self.apply_change(change)
        } else {
            Err(Error::KeyNotFound(key.to_string()))
        }
    }

    pub fn set_or_create(&self, key: K, value: &V) -> Result<()> {
        let old_value = self.get(&key)?;
        let change = if let Some(old) = old_value {
            MapChange::Update {
                key: key.clone(),
                old_value: old,
                new_value: value.clone(),
            }
        } else {
            MapChange::Insert {
                key: key.clone(),
                value: value.clone(),
            }
        };
        self.apply_change(change)
    }

    pub fn remove(&self, key: K) -> Result<Option<V>> {
        let exists = {
            let keys = self.known_keys.lock().unwrap();
            keys.contains(&key)
        };

        if !exists {
            return Ok(None);
        }

        let old_value = self.get(&key)?;
        if let Some(old) = old_value {
            let change = MapChange::Remove {
                key: key.clone(),
                old_value: old.clone(),
            };
            self.apply_change(change)?;
            Ok(Some(old))
        } else {
            self.known_keys.lock().unwrap().remove(&key);
            Ok(None)
        }
    }

    pub fn clear(&self) -> Result<()> {
        self.apply_change(MapChange::Clear)
    }

    fn apply_change(&self, mut change: MapChange<K, V>) -> Result<()> {
        let context_path: Arc<str> = match change.key() {
            Some(key) => format!("{}.{}", self.path, key).into(),
            None => self.path.clone(),
        };

        if let Some(_guard) = InterceptGuard::enter(&self.intercept_depth, context_path) {
            let mut keys_to_intercept = Vec::new();
            if let Some(k) = change.key() {
                keys_to_intercept.push(k.clone());
            } else {
                let lock = self.interceptors_key.lock().unwrap();
                keys_to_intercept = lock.keys().cloned().collect();
            }

            for key in keys_to_intercept {
                let interceptors = {
                    let lock = self.interceptors_key.lock().unwrap();
                    lock.get(&key).cloned().unwrap_or_default()
                };
                for (_, interceptor) in interceptors {
                    if let Some(new_change) = interceptor(change.clone()) {
                        change = new_change;
                    } else {
                        return Err(Error::Intercepted);
                    }
                }
            }

            let interceptors_any = {
                let lock = self.interceptors_any.lock().unwrap();
                lock.clone()
            };
            for (_, interceptor) in interceptors_any {
                if let Some(new_change) = interceptor(change.clone()) {
                    change = new_change;
                } else {
                    return Err(Error::Intercepted);
                }
            }
        } else {
            tracing::warn!(
                path = %self.path,
                "max intercept depth reached, skipping map interceptors"
            );
        }

        match &change {
            MapChange::Insert { key, value }
            | MapChange::Update {
                key,
                new_value: value,
                ..
            } => {
                let full_path = format!("{}.{}", self.path, key);
                self.store.set(&full_path, value)?;
            }
            MapChange::Remove { key, .. } => {
                let full_path = format!("{}.{}", self.path, key);
                self.store.delete(&full_path)?;
            }
            MapChange::Clear => {
                let prefix = format!("{}.", self.path);
                let kvs = self.store.scan_prefix(&prefix)?;
                for (full_path, _) in kvs {
                    self.store.delete(&full_path)?;
                }
            }
        }

        Ok(())
    }

    pub fn intercept<F>(&self, callback: F) -> InterceptDisposer
    where
        F: Fn(MapChange<K, V>) -> Option<MapChange<K, V>> + Send + Sync + 'static,
    {
        let id = self.next_id.fetch_add(1, Ordering::Relaxed);
        self.interceptors_any
            .lock()
            .unwrap()
            .push((id, Arc::new(callback)));
        let subs = self.interceptors_any.clone();
        InterceptDisposer {
            id,
            path: self.path.clone(),
            cleanup: Arc::new(move |id| {
                if let Ok(mut lock) = subs.lock() {
                    lock.retain(|(i, _)| *i != id);
                }
            }),
        }
    }

    pub fn intercept_key<F>(&self, key: K, callback: F) -> InterceptDisposer
    where
        F: Fn(MapChange<K, V>) -> Option<MapChange<K, V>> + Send + Sync + 'static,
    {
        let id = self.next_id.fetch_add(1, Ordering::Relaxed);
        self.interceptors_key
            .lock()
            .unwrap()
            .entry(key.clone())
            .or_default()
            .push((id, Arc::new(callback)));
        let subs = self.interceptors_key.clone();
        InterceptDisposer {
            id,
            path: self.path.clone(),
            cleanup: Arc::new(move |id| {
                if let Ok(mut lock) = subs.lock()
                    && let Some(list) = lock.get_mut(&key)
                {
                    list.retain(|(i, _)| *i != id);
                }
            }),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::DefaultStore;
    use crate::reactive::access::WritableMode;
    use crate::store::builder::StoreBuilder;
    use std::sync::Mutex;
    use std::time::Duration;
    use tracing_test::traced_test;

    fn setup_store(name: &str) -> Arc<DefaultStore> {
        let suffix = rand::random::<u32>();
        let path = std::env::temp_dir().join(format!("rpstate-map-unit-{}-{}.db", name, suffix));
        if path.exists() {
            let _ = std::fs::remove_file(&path);
        }
        StoreBuilder::new(path)
            .debounce(50)
            .build()
            .expect("Failed to build DefaultStore")
    }

    #[test]
    fn test_map_crud_logic() {
        let store = setup_store("crud");
        let path: Arc<str> = Arc::from("test_map.data");

        let map: ReactiveMap<String, i32, DefaultStore, WritableMode> =
            crate::store::reactive_map_with_path(&store, path, HashMap::new()).unwrap();

        map.set_or_create("a".into(), &10).unwrap();
        assert_eq!(map.get(&"a".into()).unwrap(), Some(10));
        assert_eq!(map.len().unwrap(), 1);

        map.set("a".into(), &20).unwrap();
        assert_eq!(map.get(&"a".into()).unwrap(), Some(20));

        let res = map.set("missing".into(), &30);
        assert!(matches!(res, Err(Error::KeyNotFound(_))));

        map.set_or_create("b".into(), &100).unwrap();
        let entries = map.entries().unwrap();
        assert_eq!(entries.len(), 2);

        let removed = map.remove("a".into()).unwrap();
        assert_eq!(removed, Some(20));
        assert_eq!(map.len().unwrap(), 1);

        store.save_now().unwrap();
        assert_eq!(map.get(&"a".into()).unwrap(), None);
    }

    #[test]
    fn test_map_intercept_and_reject() {
        let store = setup_store("reject");
        let map: ReactiveMap<String, i32, DefaultStore, WritableMode> =
            crate::store::reactive_map_with_path(
                &store,
                Arc::from("test.intercept"),
                HashMap::new(),
            )
            .unwrap();

        map.intercept(|change| match change {
            MapChange::Insert { value, .. }
            | MapChange::Update {
                new_value: value, ..
            } if value < 0 => None,
            _ => Some(change),
        });

        let res = map.set_or_create("val".into(), &-1);
        assert!(matches!(res, Err(Error::Intercepted)));

        store.save_now().unwrap();
        assert_eq!(map.get(&"val".into()).unwrap(), None);

        map.set_or_create("val".into(), &10).unwrap();
        assert_eq!(map.get(&"val".into()).unwrap(), Some(10));
    }

    #[test]
    fn test_map_intercept_transform() {
        let store = setup_store("transform");
        let map: ReactiveMap<String, i32, DefaultStore, WritableMode> =
            crate::store::reactive_map_with_path(
                &store,
                Arc::from("test.transform"),
                HashMap::new(),
            )
            .unwrap();

        map.intercept(|change| match change {
            MapChange::Insert { key, value } => Some(MapChange::Insert {
                key,
                value: value * 2,
            }),
            MapChange::Update {
                key,
                old_value,
                new_value,
            } => Some(MapChange::Update {
                key,
                old_value,
                new_value: new_value * 2,
            }),
            _ => Some(change),
        });

        map.set_or_create("x".into(), &5).unwrap();
        assert_eq!(map.get(&"x".into()).unwrap(), Some(10));
    }

    #[test]
    fn test_map_subscriptions() {
        let store = setup_store("subs");
        let map: ReactiveMap<String, i32, DefaultStore, WritableMode> =
            crate::store::reactive_map_with_path(&store, Arc::from("test.subs"), HashMap::new())
                .unwrap();

        let events = Arc::new(Mutex::new(Vec::new()));
        let e_clone = events.clone();

        let _sub = map.subscribe_any(move |change| {
            e_clone.lock().unwrap().push(change.clone());
        });

        map.set_or_create("key1".into(), &1).unwrap();
        map.set("key1".into(), &2).unwrap();
        map.remove("key1".into()).unwrap();

        std::thread::sleep(Duration::from_millis(100));

        let res = events.lock().unwrap();

        assert!(res.len() >= 3);
        assert!(matches!(res[0], MapChange::Insert { .. }));
        assert!(matches!(res[1], MapChange::Update { .. }));
        assert!(matches!(res[2], MapChange::Remove { .. }));
    }

    #[test]
    fn test_reentrancy_guard() {
        let store = setup_store("reentrancy");
        let map: ReactiveMap<String, i32, DefaultStore, WritableMode> =
            crate::store::reactive_map_with_path(
                &store,
                Arc::from("test.reentrancy"),
                HashMap::new(),
            )
            .unwrap();

        let map_clone = map.clone();
        map.intercept(move |change| {
            if let MapChange::Update { key, .. } = &change
                && key == "a"
            {
                let _ = map_clone.set("a".into(), &999);
            }
            Some(change)
        });

        map.set_or_create("a".into(), &1).unwrap();
        map.set("a".into(), &2).unwrap();

        assert_eq!(map.get(&"a".into()).unwrap(), Some(2));
    }

    #[test]
    fn test_map_clear() {
        let store = setup_store("clear");
        let map: ReactiveMap<String, i32, DefaultStore, WritableMode> =
            crate::store::reactive_map_with_path(&store, Arc::from("test.clear"), HashMap::new())
                .unwrap();

        map.set_or_create("k1".into(), &1).unwrap();
        map.set_or_create("k2".into(), &2).unwrap();

        assert_eq!(map.len().unwrap(), 2);

        map.clear().unwrap();
        store.save_now().unwrap();

        assert_eq!(map.len().unwrap(), 0);
        assert!(map.is_empty().unwrap());
    }
    #[test]
    fn test_contains_key_and_cleanup() {
        let store = setup_store("contains");
        let map: ReactiveMap<String, i32, DefaultStore, WritableMode> =
            crate::store::reactive_map_with_path(
                &store,
                Arc::from("test.contains"),
                HashMap::new(),
            )
            .unwrap();

        map.set_or_create("key1".into(), &1).unwrap();

        assert!(map.contains_key(&"key1".into()).unwrap());
        assert!(!map.contains_key(&"key2".into()).unwrap());

        let call_count = Arc::new(AtomicUsize::new(0));
        let c_clone = call_count.clone();
        {
            let _sub = map.subscribe_any(move |_| {
                c_clone.fetch_add(1, Ordering::SeqCst);
            });
            map.set("key1".into(), &2).unwrap();
            assert_eq!(call_count.load(Ordering::SeqCst), 1);
        }

        map.set("key1".into(), &3).unwrap();
        assert_eq!(call_count.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn test_key_specific_logic() {
        let store = setup_store("key_spec");
        let map: ReactiveMap<String, i32, DefaultStore, WritableMode> =
            crate::store::reactive_map_with_path(&store, Arc::from("test.keyspec"), HashMap::new())
                .unwrap();

        map.set_or_create("target".into(), &10).unwrap();
        map.set_or_create("other".into(), &20).unwrap();

        let target_calls = Arc::new(AtomicUsize::new(0));
        let t_clone = target_calls.clone();
        let _sub = map.subscribe_key("target".into(), move |_| {
            t_clone.fetch_add(1, Ordering::SeqCst);
        });

        map.set("target".into(), &11).unwrap();
        map.set("other".into(), &21).unwrap();
        assert_eq!(target_calls.load(Ordering::SeqCst), 1);

        map.intercept_key("target".into(), |change| {
            if let MapChange::Update { new_value, .. } = change
                && new_value > 100
            {
                return None;
            }
            Some(change)
        });

        map.set("target".into(), &50).unwrap();
        let res = map.set("target".into(), &150);
        assert!(matches!(res, Err(Error::Intercepted)));

        map.set("other".into(), &150).unwrap();
    }

    #[test]
    fn test_entries_parsing_failures() {
        let store = setup_store("parsing");
        let path: Arc<str> = Arc::from("test.parse");

        {
            let map_str: ReactiveMap<String, String, DefaultStore, WritableMode> =
                crate::store::reactive_map_with_path(&store, path.clone(), HashMap::new()).unwrap();

            map_str
                .set_or_create("not_int_key".into(), &"1".into())
                .unwrap();
            map_str
                .set_or_create("123".into(), &"invalid_value".into())
                .unwrap();
        }

        let map_int: ReactiveMap<i32, i32, DefaultStore, WritableMode> =
            crate::store::reactive_map_with_path(&store, path, HashMap::new()).unwrap();

        let entries = map_int.entries().unwrap();

        // i32::from_str("123") succeed, but decoder falls back to Default (0) for invalid bytes
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0], (123, 0));
    }

    #[test]
    fn test_remove_edge_cases() {
        let store = setup_store("remove_edge");
        let map: ReactiveMap<String, i32, DefaultStore, WritableMode> =
            crate::store::reactive_map_with_path(&store, Arc::from("test.remove"), HashMap::new())
                .unwrap();

        let res = map.remove("none".into()).unwrap();
        assert!(res.is_none());

        map.set_or_create("ghost".into(), &1).unwrap();
        store.delete("test.remove.ghost").unwrap();

        let res = map.remove("ghost".into()).unwrap();
        assert!(res.is_none());
        assert!(!map.contains_key(&"ghost".into()).unwrap());
    }

    #[test]
    #[traced_test]
    fn test_map_recursion_warning() {
        let store = setup_store("map_trace");
        let map: ReactiveMap<String, i32, DefaultStore, WritableMode> =
            crate::store::reactive_map_with_path(
                &store,
                Arc::from("test.recursive_map"),
                HashMap::new(),
            )
            .unwrap();

        let map_clone = map.clone();

        map.intercept(move |change| {
            if let Some(key) = change.key() {
                let _ = map_clone.set_or_create(key.clone(), &999);
            }
            Some(change)
        });

        let _ = map.set_or_create("key_a".into(), &1);

        assert!(logs_contain("maximum intercept depth reached"));
        assert!(logs_contain("test.recursive_map.key_a"));
    }
}