netabase_store 0.0.8

A type-safe, multi-backend key-value storage library for Rust with support for native (Sled, Redb) and WASM (IndexedDB) environments.
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
//! Enhanced subscription tree implementation using merkle trees for efficient synchronization
//!
//! This module provides the core implementation for subscription trees that track
//! data changes using merkle trees. It allows for efficient comparison and
//! synchronization between different nodes.

use std::collections::{BTreeMap, HashMap};
use std::marker::PhantomData;

use netabase_deps::blake3;
use rs_merkle::{MerkleTree, algorithms::Sha256};

use crate::{
    error::NetabaseError,
    traits::subscription::{
        SubscriptionManager, SubscriptionStats, SubscriptionTree, Subscriptions,
        subscription_tree::ModelHash,
    },
};

/// Represents the difference between two subscription trees
#[derive(Debug, Clone)]
pub struct SubscriptionDiff<S: Subscriptions> {
    /// Items that are missing in the first tree but present in the second
    pub missing_in_self: Vec<(Vec<u8>, ModelHash)>,
    /// Items that are missing in the second tree but present in the first
    pub missing_in_other: Vec<(Vec<u8>, ModelHash)>,
    /// Items that exist in both trees but with different values
    pub different_values: Vec<(Vec<u8>, ModelHash, ModelHash)>, // key, their_hash, our_hash
    /// Topic this diff relates to
    pub topic: S::Subscriptions,
}

impl<S: Subscriptions> SubscriptionDiff<S> {
    pub fn new(topic: S::Subscriptions) -> Self {
        Self {
            missing_in_self: Vec::new(),
            missing_in_other: Vec::new(),
            different_values: Vec::new(),
            topic,
        }
    }

    /// Check if there are any differences
    pub fn has_differences(&self) -> bool {
        !self.missing_in_self.is_empty()
            || !self.missing_in_other.is_empty()
            || !self.different_values.is_empty()
    }

    /// Get the total number of differences
    pub fn total_differences(&self) -> usize {
        self.missing_in_self.len() + self.missing_in_other.len() + self.different_values.len()
    }

    /// Get all keys that need to be synchronized from the other tree to self
    pub fn keys_needed_by_self(&self) -> Vec<&Vec<u8>> {
        self.missing_in_self.iter().map(|(key, _)| key).collect()
    }

    /// Get all keys that need to be synchronized from self to the other tree
    pub fn keys_needed_by_other(&self) -> Vec<&Vec<u8>> {
        self.missing_in_other.iter().map(|(key, _)| key).collect()
    }

    /// Get all keys that have conflicting values
    pub fn conflicting_keys(&self) -> Vec<&Vec<u8>> {
        self.different_values
            .iter()
            .map(|(key, _, _)| key)
            .collect()
    }

    /// Get a summary of the differences
    pub fn summary(&self) -> String {
        format!(
            "Topic: {:?}, Missing in self: {}, Missing in other: {}, Different values: {}",
            self.topic,
            self.missing_in_self.len(),
            self.missing_in_other.len(),
            self.different_values.len()
        )
    }

    /// Merge this diff with another diff for the same topic
    pub fn merge(&mut self, other: SubscriptionDiff<S>) {
        assert_eq!(
            self.topic, other.topic,
            "Cannot merge diffs for different topics"
        );

        self.missing_in_self.extend(other.missing_in_self);
        self.missing_in_other.extend(other.missing_in_other);
        self.different_values.extend(other.different_values);
    }
}

/// A merkle tree-based subscription tree for a specific topic
#[derive(Clone)]
pub struct MerkleSubscriptionTree<S: Subscriptions> {
    topic: S::Subscriptions,
    /// Map from model key to model hash
    items: BTreeMap<Vec<u8>, ModelHash>,
    /// Cached merkle tree root - rebuilt when items change
    merkle_root: Option<[u8; 32]>,
    /// Merkle tree for efficient comparison
    merkle_tree: Option<MerkleTree<Sha256>>,
    /// Flag to track if merkle tree needs rebuilding
    needs_rebuild: bool,
    _phantom: PhantomData<S>,
}

impl<S: Subscriptions> MerkleSubscriptionTree<S> {
    /// Create a new subscription tree for the given topic
    pub fn new(topic: S::Subscriptions) -> Self {
        Self {
            topic,
            items: BTreeMap::new(),
            merkle_root: None,
            merkle_tree: None,
            needs_rebuild: false,
            _phantom: PhantomData,
        }
    }

    /// Get the topic this tree tracks
    pub fn topic(&self) -> S::Subscriptions {
        self.topic
    }

    /// Mark the merkle tree as needing to be rebuilt
    fn mark_dirty(&mut self) {
        self.needs_rebuild = true;
        self.merkle_root = None;
    }

    /// Rebuild the merkle tree from current items if needed
    fn rebuild_if_needed(&mut self) -> Result<(), NetabaseError> {
        if !self.needs_rebuild {
            return Ok(());
        }

        if self.items.is_empty() {
            self.merkle_tree = None;
            self.merkle_root = None;
            self.needs_rebuild = false;
            return Ok(());
        }

        // Create leaves from all item hashes, sorted by key for deterministic ordering
        let mut leaves: Vec<[u8; 32]> = self
            .items
            .iter()
            .map(|(key, hash)| {
                // Combine key and hash for the leaf
                let mut hasher = blake3::Hasher::new();
                hasher.update(key);
                hasher.update(hash.as_bytes());
                *hasher.finalize().as_bytes()
            })
            .collect();

        // Sort leaves for deterministic tree structure
        leaves.sort();

        // Build merkle tree
        let tree = MerkleTree::<Sha256>::from_leaves(&leaves);
        self.merkle_root = tree.root().map(|root| {
            let mut root_bytes = [0u8; 32];
            root_bytes.copy_from_slice(&root);
            root_bytes
        });
        self.merkle_tree = Some(tree);
        self.needs_rebuild = false;

        Ok(())
    }

    /// Get all items as an iterator
    pub fn iter(&self) -> impl Iterator<Item = (&Vec<u8>, &ModelHash)> {
        self.items.iter()
    }

    /// Get all hashes
    pub fn all_hashes(&self) -> Vec<ModelHash> {
        self.items.values().cloned().collect()
    }

    /// Get statistics about this tree
    pub fn stats(&self) -> TreeStats {
        TreeStats {
            item_count: self.items.len(),
            has_merkle_root: self.merkle_root.is_some(),
            needs_rebuild: self.needs_rebuild,
        }
    }

    /// Compare this tree with another tree and return their differences
    pub fn compare_with(
        &mut self,
        other: &mut MerkleSubscriptionTree<S>,
    ) -> Result<SubscriptionDiff<S>, NetabaseError> {
        let our_root = self.merkle_root()?;
        let their_root = other.merkle_root()?;

        let mut diff = SubscriptionDiff::new(self.topic);

        // If roots are the same, no differences
        if our_root == their_root {
            return Ok(diff);
        }

        let our_items = self.get_all_items();
        let their_items = other.get_all_items();

        let our_map: HashMap<Vec<u8>, ModelHash> = our_items.into_iter().collect();
        let their_map: HashMap<Vec<u8>, ModelHash> = their_items.into_iter().collect();

        // Find missing in self
        for (key, hash) in &their_map {
            if !our_map.contains_key(key) {
                diff.missing_in_self.push((key.clone(), hash.clone()));
            }
        }

        // Find missing in other
        for (key, hash) in &our_map {
            if !their_map.contains_key(key) {
                diff.missing_in_other.push((key.clone(), hash.clone()));
            }
        }

        // Find different values
        for (key, our_hash) in &our_map {
            if let Some(their_hash) = their_map.get(key)
                && our_hash != their_hash {
                    diff.different_values
                        .push((key.clone(), their_hash.clone(), our_hash.clone()));
                }
        }

        Ok(diff)
    }
}

impl<S: Subscriptions> SubscriptionTree<S> for MerkleSubscriptionTree<S> {
    type Topic = S::Subscriptions;

    fn topic(&self) -> Self::Topic {
        self.topic
    }

    fn put_item(&mut self, key: Vec<u8>, hash: ModelHash) -> Result<(), NetabaseError> {
        self.items.insert(key, hash);
        self.mark_dirty();
        Ok(())
    }

    fn remove_item(&mut self, key: &[u8]) -> Result<Option<ModelHash>, NetabaseError> {
        let result = self.items.remove(key);
        if result.is_some() {
            self.mark_dirty();
        }
        Ok(result)
    }

    fn get_all_hashes(&self) -> Result<Vec<ModelHash>, NetabaseError> {
        Ok(self.all_hashes())
    }

    fn merkle_root(&mut self) -> Result<Option<[u8; 32]>, NetabaseError> {
        self.rebuild_if_needed()?;
        Ok(self.merkle_root)
    }

    fn len(&self) -> usize {
        self.items.len()
    }

    fn clear(&mut self) -> Result<(), NetabaseError> {
        self.items.clear();
        self.mark_dirty();
        Ok(())
    }

    fn contains_key(&self, key: &[u8]) -> bool {
        self.items.contains_key(key)
    }

    fn get_hash(&self, key: &[u8]) -> Option<&ModelHash> {
        self.items.get(key)
    }

    fn get_all_keys(&self) -> Vec<Vec<u8>> {
        self.items.keys().cloned().collect()
    }

    fn get_all_items(&self) -> Vec<(Vec<u8>, ModelHash)> {
        self.items
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect()
    }

    fn rebuild_merkle_tree(&mut self) -> Result<(), NetabaseError> {
        self.needs_rebuild = true;
        self.rebuild_if_needed()
    }
}

/// Statistics about a subscription tree
#[derive(Debug, Clone)]
pub struct TreeStats {
    pub item_count: usize,
    pub has_merkle_root: bool,
    pub needs_rebuild: bool,
}

/// Default subscription manager implementation
pub struct DefaultSubscriptionManager<S: Subscriptions> {
    trees: HashMap<S::Subscriptions, MerkleSubscriptionTree<S>>,
    _phantom: PhantomData<S>,
}

impl<S: Subscriptions> DefaultSubscriptionManager<S> {
    /// Create a new subscription manager with all topics initialized
    pub fn new() -> Self {
        let mut trees = HashMap::new();
        for topic in S::subscriptions() {
            trees.insert(topic, MerkleSubscriptionTree::new(topic));
        }
        Self {
            trees,
            _phantom: PhantomData,
        }
    }

    /// Compare this manager with another manager and return differences for all topics
    pub fn compare_with(
        &mut self,
        other: &mut DefaultSubscriptionManager<S>,
    ) -> Result<std::collections::HashMap<S::Subscriptions, SubscriptionDiff<S>>, NetabaseError>
    {
        let mut diffs = std::collections::HashMap::new();

        for topic in S::subscriptions() {
            if let (Some(our_tree), Some(their_tree)) =
                (self.trees.get_mut(&topic), other.trees.get_mut(&topic))
            {
                let diff = our_tree.compare_with(their_tree)?;
                if diff.has_differences() {
                    diffs.insert(topic, diff);
                }
            }
        }

        Ok(diffs)
    }

    /// Initialize only specific topics
    pub fn with_topics(topics: &[S::Subscriptions]) -> Self {
        let mut trees = HashMap::new();
        for &topic in topics {
            trees.insert(topic, MerkleSubscriptionTree::new(topic));
        }
        Self {
            trees,
            _phantom: PhantomData,
        }
    }
}

impl<S: Subscriptions> Default for DefaultSubscriptionManager<S> {
    fn default() -> Self {
        Self::new()
    }
}

impl<S: Subscriptions> SubscriptionManager<S> for DefaultSubscriptionManager<S> {
    type TopicType = S::Subscriptions;

    fn subscribe_item<T>(
        &mut self,
        topic: S::Subscriptions,
        key: Vec<u8>,
        data: &T,
    ) -> Result<(), NetabaseError>
    where
        T: AsRef<[u8]>,
    {
        let hash = ModelHash::from_key_and_data(&key, data);
        if let Some(tree) = self.trees.get_mut(&topic) {
            tree.put_item(key, hash)?;
        }
        Ok(())
    }

    fn unsubscribe_item(
        &mut self,
        topic: S::Subscriptions,
        key: &[u8],
    ) -> Result<Option<ModelHash>, NetabaseError> {
        if let Some(tree) = self.trees.get_mut(&topic) {
            tree.remove_item(key)
        } else {
            Ok(None)
        }
    }

    fn topic_merkle_root(
        &mut self,
        topic: S::Subscriptions,
    ) -> Result<Option<[u8; 32]>, NetabaseError> {
        if let Some(tree) = self.trees.get_mut(&topic) {
            tree.merkle_root()
        } else {
            Ok(None)
        }
    }

    fn stats(&self) -> SubscriptionStats {
        let mut stats = SubscriptionStats::new();
        for tree in self.trees.values() {
            stats.add_topic_count(tree.len());
        }
        stats
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use strum::IntoEnumIterator;

    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, strum::EnumIter)]
    pub enum TestTopics {
        Users,
        Posts,
    }

    impl std::fmt::Display for TestTopics {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                TestTopics::Users => write!(f, "Users"),
                TestTopics::Posts => write!(f, "Posts"),
            }
        }
    }

    struct TestDef;

    impl Subscriptions for TestDef {
        type Subscriptions = TestTopics;

        fn subscriptions() -> <Self::Subscriptions as IntoEnumIterator>::Iterator {
            TestTopics::iter()
        }
    }

    #[test]
    fn test_model_hash_creation() {
        let data = b"test data";
        let hash1 = ModelHash::from_data(data);
        let hash2 = ModelHash::from_data(data);
        assert_eq!(hash1, hash2);

        let different_data = b"different data";
        let hash3 = ModelHash::from_data(different_data);
        assert_ne!(hash1, hash3);
    }

    #[test]
    fn test_subscription_diff() {
        let mut tree1 = MerkleSubscriptionTree::<TestDef>::new(TestTopics::Users);
        let mut tree2 = MerkleSubscriptionTree::<TestDef>::new(TestTopics::Users);

        // Tree1 has user1, tree2 has user2
        tree1
            .put_item(b"user1".to_vec(), ModelHash::from_data(b"data1"))
            .unwrap();
        tree2
            .put_item(b"user2".to_vec(), ModelHash::from_data(b"data2"))
            .unwrap();

        let diff = tree1.compare_with(&mut tree2).unwrap();

        assert!(diff.has_differences());
        assert_eq!(diff.total_differences(), 2);
        assert_eq!(diff.missing_in_self.len(), 1);
        assert_eq!(diff.missing_in_other.len(), 1);
        assert_eq!(diff.different_values.len(), 0);

        // user2 is missing in tree1
        assert_eq!(diff.missing_in_self[0].0, b"user2");
        // user1 is missing in tree2
        assert_eq!(diff.missing_in_other[0].0, b"user1");
    }

    #[test]
    fn test_subscription_diff_conflicting_values() {
        let mut tree1 = MerkleSubscriptionTree::<TestDef>::new(TestTopics::Users);
        let mut tree2 = MerkleSubscriptionTree::<TestDef>::new(TestTopics::Users);

        // Both trees have user1 but with different data
        tree1
            .put_item(b"user1".to_vec(), ModelHash::from_data(b"data1"))
            .unwrap();
        tree2
            .put_item(b"user1".to_vec(), ModelHash::from_data(b"data2"))
            .unwrap();

        let diff = tree1.compare_with(&mut tree2).unwrap();

        assert!(diff.has_differences());
        assert_eq!(diff.total_differences(), 1);
        assert_eq!(diff.missing_in_self.len(), 0);
        assert_eq!(diff.missing_in_other.len(), 0);
        assert_eq!(diff.different_values.len(), 1);

        let (key, their_hash, our_hash) = &diff.different_values[0];
        assert_eq!(key, b"user1");
        assert_eq!(*their_hash, ModelHash::from_data(b"data2"));
        assert_eq!(*our_hash, ModelHash::from_data(b"data1"));
    }

    #[test]
    fn test_subscription_tree_basic_operations() {
        let mut tree = MerkleSubscriptionTree::<TestDef>::new(TestTopics::Users);

        assert!(tree.is_empty());
        assert_eq!(tree.len(), 0);

        let key1 = b"user1".to_vec();
        let hash1 = ModelHash::from_data(b"user1_data");

        tree.put_item(key1.clone(), hash1.clone()).unwrap();
        assert_eq!(tree.len(), 1);
        assert!(!tree.is_empty());
        assert!(tree.contains_key(&key1));
        assert_eq!(tree.get_hash(&key1), Some(&hash1));

        let removed = tree.remove_item(&key1).unwrap();
        assert_eq!(removed, Some(hash1));
        assert!(tree.is_empty());
        assert!(!tree.contains_key(&key1));
    }

    #[test]
    fn test_subscription_manager() {
        let mut manager = DefaultSubscriptionManager::<TestDef>::new();

        // Test initial state
        let stats = manager.stats();
        assert_eq!(stats.total_items, 0);
        assert_eq!(stats.active_topics, 0);

        // Add item to Users topic
        manager
            .subscribe_item(TestTopics::Users, b"user1".to_vec(), b"user_data")
            .unwrap();

        let stats = manager.stats();
        assert_eq!(stats.total_items, 1);
        assert_eq!(stats.active_topics, 1);

        // Remove item
        let removed = manager
            .unsubscribe_item(TestTopics::Users, b"user1")
            .unwrap();
        assert!(removed.is_some());

        let stats = manager.stats();
        assert_eq!(stats.total_items, 0);
        assert_eq!(stats.active_topics, 0);
    }

    #[test]
    fn test_manager_comparison() {
        let mut manager1 = DefaultSubscriptionManager::<TestDef>::new();
        let mut manager2 = DefaultSubscriptionManager::<TestDef>::new();

        // Add different data to each manager
        manager1
            .subscribe_item(TestTopics::Users, b"user1".to_vec(), b"data1")
            .unwrap();

        manager2
            .subscribe_item(TestTopics::Users, b"user2".to_vec(), b"data2")
            .unwrap();

        let diffs = manager1.compare_with(&mut manager2).unwrap();
        assert_eq!(diffs.len(), 1);

        let user_diff = &diffs[&TestTopics::Users];
        assert!(user_diff.has_differences());
        assert_eq!(user_diff.total_differences(), 2);
        assert_eq!(user_diff.missing_in_self.len(), 1);
        assert_eq!(user_diff.missing_in_other.len(), 1);
    }
}