atmst 0.0.1

light atproto-style merkle search tree impl.
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
use async_trait::async_trait;
use cid::Cid;
use dashmap::DashMap;
use std::sync::Arc;

use super::node::MstNode;
use crate::error::Result;

#[cfg(feature = "fjall")]
pub mod fjall;

pub mod multi_tenant;

/// Trait for MST node storage backends
#[async_trait]
pub trait MstStorage: Send + Sync {
    /// Get a node by its CID
    async fn get_node(&self, cid: &Cid) -> Result<Option<MstNode>>;

    /// Insert a node with the given CID
    async fn insert_node(&self, cid: Cid, node: MstNode) -> Result<()>;

    /// Remove a node by its CID
    async fn remove_node(&self, cid: &Cid) -> Result<Option<MstNode>>;

    /// Check if a node exists
    async fn contains_node(&self, cid: &Cid) -> Result<bool>;

    /// Get the number of nodes stored
    async fn len(&self) -> Result<usize>;

    /// Check if the storage is empty
    async fn is_empty(&self) -> Result<bool> {
        Ok(self.len().await? == 0)
    }

    /// Clear all nodes from storage
    async fn clear(&self) -> Result<()>;

    /// Get all CIDs in storage
    async fn all_cids(&self) -> Result<Vec<Cid>>;

    /// Batch insert multiple nodes atomically
    async fn batch_insert(&self, nodes: Vec<(Cid, MstNode)>) -> Result<()> {
        // Default implementation: insert one by one
        // Implementations can override for better atomicity
        for (cid, node) in nodes {
            self.insert_node(cid, node).await?;
        }
        Ok(())
    }

    /// Batch remove multiple nodes atomically
    async fn batch_remove(&self, cids: Vec<Cid>) -> Result<Vec<Option<MstNode>>> {
        // Default implementation: remove one by one
        // Implementations can override for better atomicity
        let mut removed = Vec::with_capacity(cids.len());
        for cid in cids {
            removed.push(self.remove_node(&cid).await?);
        }
        Ok(removed)
    }

    /// Clone this storage instance
    fn clone_storage(&self) -> Box<dyn MstStorage>;
}

/// Storage wrapper that can hold either concrete or dynamic storage
pub enum StorageWrapper {
    Memory(MemoryMstStorage),
    #[cfg(feature = "fjall")]
    Fjall(crate::mst::storage::fjall::FjallMstStorage),
    Dynamic(Box<dyn MstStorage>),
}

impl std::fmt::Debug for StorageWrapper {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            StorageWrapper::Memory(_) => write!(f, "StorageWrapper::Memory(..)"),
            #[cfg(feature = "fjall")]
            StorageWrapper::Fjall(_) => write!(f, "StorageWrapper::Fjall(..)"),
            StorageWrapper::Dynamic(_) => write!(f, "StorageWrapper::Dynamic(..)"),
        }
    }
}

impl Clone for StorageWrapper {
    fn clone(&self) -> Self {
        match self {
            StorageWrapper::Memory(storage) => StorageWrapper::Memory(storage.clone()),
            #[cfg(feature = "fjall")]
            StorageWrapper::Fjall(storage) => StorageWrapper::Fjall(storage.clone()),
            StorageWrapper::Dynamic(storage) => StorageWrapper::Dynamic(storage.clone_storage()),
        }
    }
}

#[async_trait]
impl MstStorage for StorageWrapper {
    async fn get_node(&self, cid: &Cid) -> Result<Option<MstNode>> {
        match self {
            StorageWrapper::Memory(storage) => storage.get_node(cid).await,
            #[cfg(feature = "fjall")]
            StorageWrapper::Fjall(storage) => storage.get_node(cid).await,
            StorageWrapper::Dynamic(storage) => storage.get_node(cid).await,
        }
    }

    async fn insert_node(&self, cid: Cid, node: MstNode) -> Result<()> {
        match self {
            StorageWrapper::Memory(storage) => storage.insert_node(cid, node).await,
            #[cfg(feature = "fjall")]
            StorageWrapper::Fjall(storage) => storage.insert_node(cid, node).await,
            StorageWrapper::Dynamic(storage) => storage.insert_node(cid, node).await,
        }
    }

    async fn remove_node(&self, cid: &Cid) -> Result<Option<MstNode>> {
        match self {
            StorageWrapper::Memory(storage) => storage.remove_node(cid).await,
            #[cfg(feature = "fjall")]
            StorageWrapper::Fjall(storage) => storage.remove_node(cid).await,
            StorageWrapper::Dynamic(storage) => storage.remove_node(cid).await,
        }
    }

    async fn contains_node(&self, cid: &Cid) -> Result<bool> {
        match self {
            StorageWrapper::Memory(storage) => storage.contains_node(cid).await,
            #[cfg(feature = "fjall")]
            StorageWrapper::Fjall(storage) => storage.contains_node(cid).await,
            StorageWrapper::Dynamic(storage) => storage.contains_node(cid).await,
        }
    }

    async fn len(&self) -> Result<usize> {
        match self {
            StorageWrapper::Memory(storage) => storage.len().await,
            #[cfg(feature = "fjall")]
            StorageWrapper::Fjall(storage) => storage.len().await,
            StorageWrapper::Dynamic(storage) => storage.len().await,
        }
    }

    async fn clear(&self) -> Result<()> {
        match self {
            StorageWrapper::Memory(storage) => storage.clear().await,
            #[cfg(feature = "fjall")]
            StorageWrapper::Fjall(storage) => storage.clear().await,
            StorageWrapper::Dynamic(storage) => storage.clear().await,
        }
    }

    async fn all_cids(&self) -> Result<Vec<Cid>> {
        match self {
            StorageWrapper::Memory(storage) => storage.all_cids().await,
            #[cfg(feature = "fjall")]
            StorageWrapper::Fjall(storage) => storage.all_cids().await,
            StorageWrapper::Dynamic(storage) => storage.all_cids().await,
        }
    }

    async fn batch_insert(&self, nodes: Vec<(Cid, MstNode)>) -> Result<()> {
        match self {
            StorageWrapper::Memory(storage) => storage.batch_insert(nodes).await,
            #[cfg(feature = "fjall")]
            StorageWrapper::Fjall(storage) => storage.batch_insert(nodes).await,
            StorageWrapper::Dynamic(storage) => storage.batch_insert(nodes).await,
        }
    }

    async fn batch_remove(&self, cids: Vec<Cid>) -> Result<Vec<Option<MstNode>>> {
        match self {
            StorageWrapper::Memory(storage) => storage.batch_remove(cids).await,
            #[cfg(feature = "fjall")]
            StorageWrapper::Fjall(storage) => storage.batch_remove(cids).await,
            StorageWrapper::Dynamic(storage) => storage.batch_remove(cids).await,
        }
    }

    fn clone_storage(&self) -> Box<dyn MstStorage> {
        Box::new(self.clone())
    }
}

/// In-memory MST storage using DashMap
#[derive(Debug, Clone)]
pub struct MemoryMstStorage {
    nodes: Arc<DashMap<Cid, MstNode>>,
}

impl MemoryMstStorage {
    /// Create a new empty memory storage
    pub fn new() -> Self {
        Self {
            nodes: Arc::new(DashMap::new()),
        }
    }

    /// Create a new memory storage with initial capacity
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            nodes: Arc::new(DashMap::with_capacity(capacity)),
        }
    }

    /// Get the underlying DashMap for direct access
    pub fn inner(&self) -> &Arc<DashMap<Cid, MstNode>> {
        &self.nodes
    }
}

impl Default for MemoryMstStorage {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl MstStorage for MemoryMstStorage {
    async fn get_node(&self, cid: &Cid) -> Result<Option<MstNode>> {
        Ok(self.nodes.get(cid).map(|entry| entry.clone()))
    }

    async fn insert_node(&self, cid: Cid, node: MstNode) -> Result<()> {
        self.nodes.insert(cid, node);
        Ok(())
    }

    async fn remove_node(&self, cid: &Cid) -> Result<Option<MstNode>> {
        Ok(self.nodes.remove(cid).map(|(_, node)| node))
    }

    async fn contains_node(&self, cid: &Cid) -> Result<bool> {
        Ok(self.nodes.contains_key(cid))
    }

    async fn len(&self) -> Result<usize> {
        Ok(self.nodes.len())
    }

    async fn clear(&self) -> Result<()> {
        self.nodes.clear();
        Ok(())
    }

    async fn all_cids(&self) -> Result<Vec<Cid>> {
        Ok(self.nodes.iter().map(|entry| *entry.key()).collect())
    }

    async fn batch_insert(&self, nodes: Vec<(Cid, MstNode)>) -> Result<()> {
        // DashMap allows concurrent inserts, so we can do them all at once
        for (cid, node) in nodes {
            self.nodes.insert(cid, node);
        }
        Ok(())
    }

    async fn batch_remove(&self, cids: Vec<Cid>) -> Result<Vec<Option<MstNode>>> {
        let mut removed = Vec::with_capacity(cids.len());
        for cid in cids {
            removed.push(self.nodes.remove(&cid).map(|(_, node)| node));
        }
        Ok(removed)
    }

    fn clone_storage(&self) -> Box<dyn MstStorage> {
        Box::new(self.clone())
    }
}

/// Configuration for memory storage
#[derive(Debug, Clone)]
pub struct MemoryStorageConfig {
    /// Initial capacity for the underlying DashMap
    pub initial_capacity: Option<usize>,
}

impl Default for MemoryStorageConfig {
    fn default() -> Self {
        Self {
            initial_capacity: None,
        }
    }
}

impl MemoryStorageConfig {
    /// Create a new config
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the initial capacity
    pub fn with_capacity(mut self, capacity: usize) -> Self {
        self.initial_capacity = Some(capacity);
        self
    }

    /// Build the storage with this configuration
    pub fn build(self) -> MemoryMstStorage {
        match self.initial_capacity {
            Some(capacity) => MemoryMstStorage::with_capacity(capacity),
            None => MemoryMstStorage::new(),
        }
    }
}

// Re-export Fjall storage when feature is enabled
#[cfg(feature = "fjall")]
pub use fjall::{
    CompressionAlgorithm, CompressionConfig, FjallMstStorage, FjallStorageConfig,
    SerializationFormat,
};

// Re-export multi-tenant storage
pub use multi_tenant::{
    GlobalStats, MultiTenantConfig, MultiTenantConfigBuilder, MultiTenantMstStorage, PdsStorage,
    RepoId, RepoLifecycleConfig, RepoStats, StorageBackend,
};

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Bytes;
    use crate::mst::node::{MstNode, MstNodeLeaf};
    use multihash::Multihash;
    use sha2::Digest;

    fn create_test_cid(data: &str) -> Cid {
        let hash = Multihash::wrap(0x12, &sha2::Sha256::digest(data.as_bytes())).unwrap();
        Cid::new_v1(0x71, hash)
    }

    fn create_test_node(entries: Vec<(usize, String, Cid, Option<Cid>)>) -> MstNode {
        let e = entries
            .into_iter()
            .map(|(p, k, v, t)| MstNodeLeaf {
                p,
                k: Bytes::from(k.into_bytes()),
                v,
                t,
            })
            .collect();

        MstNode::new(None, e)
    }

    #[tokio::test]
    async fn test_memory_storage_basic_operations() {
        let storage = MemoryMstStorage::new();

        let cid = create_test_cid("test_node");
        let node = create_test_node(vec![(
            0,
            "key1".to_string(),
            create_test_cid("value1"),
            None,
        )]);

        // Test insert and get
        storage.insert_node(cid, node.clone()).await.unwrap();
        let retrieved = storage.get_node(&cid).await.unwrap();
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().entries().len(), 1);

        // Test contains
        assert!(storage.contains_node(&cid).await.unwrap());

        // Test len
        assert_eq!(storage.len().await.unwrap(), 1);
        assert!(!storage.is_empty().await.unwrap());

        // Test remove
        let removed = storage.remove_node(&cid).await.unwrap();
        assert!(removed.is_some());
        assert!(!storage.contains_node(&cid).await.unwrap());
        assert_eq!(storage.len().await.unwrap(), 0);
        assert!(storage.is_empty().await.unwrap());
    }

    #[tokio::test]
    async fn test_memory_storage_batch_operations() {
        let storage = MemoryMstStorage::new();

        let nodes = vec![
            (
                create_test_cid("node1"),
                create_test_node(vec![(
                    0,
                    "key1".to_string(),
                    create_test_cid("value1"),
                    None,
                )]),
            ),
            (
                create_test_cid("node2"),
                create_test_node(vec![(
                    0,
                    "key2".to_string(),
                    create_test_cid("value2"),
                    None,
                )]),
            ),
            (
                create_test_cid("node3"),
                create_test_node(vec![(
                    0,
                    "key3".to_string(),
                    create_test_cid("value3"),
                    None,
                )]),
            ),
        ];

        // Test batch insert
        storage.batch_insert(nodes.clone()).await.unwrap();
        assert_eq!(storage.len().await.unwrap(), 3);

        // Test all_cids
        let all_cids = storage.all_cids().await.unwrap();
        assert_eq!(all_cids.len(), 3);

        // Test batch remove
        let cids_to_remove: Vec<Cid> = nodes.iter().take(2).map(|(cid, _)| *cid).collect();
        let removed = storage.batch_remove(cids_to_remove).await.unwrap();
        assert_eq!(removed.len(), 2);
        assert!(removed[0].is_some());
        assert!(removed[1].is_some());
        assert_eq!(storage.len().await.unwrap(), 1);
    }

    #[tokio::test]
    async fn test_memory_storage_clear() {
        let storage = MemoryMstStorage::new();

        // Add some nodes
        for i in 0..5 {
            let cid = create_test_cid(&format!("node{}", i));
            let node = create_test_node(vec![(
                0,
                format!("key{}", i),
                create_test_cid(&format!("value{}", i)),
                None,
            )]);
            storage.insert_node(cid, node).await.unwrap();
        }

        assert_eq!(storage.len().await.unwrap(), 5);

        // Clear all
        storage.clear().await.unwrap();
        assert_eq!(storage.len().await.unwrap(), 0);
        assert!(storage.is_empty().await.unwrap());
    }

    #[tokio::test]
    async fn test_memory_storage_config() {
        let config = MemoryStorageConfig::new().with_capacity(100);

        let storage = config.build();
        assert_eq!(storage.len().await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_memory_storage_concurrent_access() {
        let storage = MemoryMstStorage::new();
        let storage1 = storage.clone();
        let storage2 = storage.clone();

        // Test concurrent access
        let handle1 = tokio::spawn(async move {
            for i in 0..10 {
                let cid = create_test_cid(&format!("concurrent1_{}", i));
                let node = create_test_node(vec![(
                    0,
                    format!("key{}", i),
                    create_test_cid(&format!("value{}", i)),
                    None,
                )]);
                storage1.insert_node(cid, node).await.unwrap();
            }
        });

        let handle2 = tokio::spawn(async move {
            for i in 0..10 {
                let cid = create_test_cid(&format!("concurrent2_{}", i));
                let node = create_test_node(vec![(
                    0,
                    format!("key{}", i),
                    create_test_cid(&format!("value{}", i)),
                    None,
                )]);
                storage2.insert_node(cid, node).await.unwrap();
            }
        });

        handle1.await.unwrap();
        handle2.await.unwrap();

        assert_eq!(storage.len().await.unwrap(), 20);
    }

    #[tokio::test]
    async fn test_memory_storage_nonexistent_operations() {
        let storage = MemoryMstStorage::new();
        let nonexistent_cid = create_test_cid("nonexistent");

        // Test get nonexistent
        assert!(storage.get_node(&nonexistent_cid).await.unwrap().is_none());

        // Test contains nonexistent
        assert!(!storage.contains_node(&nonexistent_cid).await.unwrap());

        // Test remove nonexistent
        assert!(
            storage
                .remove_node(&nonexistent_cid)
                .await
                .unwrap()
                .is_none()
        );
    }
}