leptos-sync-core 0.9.0

Core synchronization library for Leptos applications
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
//! IndexedDB storage implementation with real browser storage

use super::{LocalStorage, StorageError};
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Serialize};
use std::sync::Arc;

pub mod connection;
pub mod crdt_store;
pub mod errors;
pub mod operations;

use connection::IndexedDbConnection;
use crdt_store::CrdtStore;
use errors::IndexedDbError;
use operations::IndexedDbOperations;

/// IndexedDB storage implementation with real browser storage
pub struct IndexedDbStorage {
    db_name: String,
    store_name: String,
    fallback: super::memory::MemoryStorage,
    connection: Option<Arc<IndexedDbConnection>>,
    operations: Option<IndexedDbOperations>,
    crdt_store: Option<CrdtStore>,
    initialized: bool,
}

impl IndexedDbStorage {
    /// Create a new IndexedDB storage instance
    pub fn new(db_name: String, store_name: String) -> Self {
        Self {
            db_name,
            store_name,
            fallback: super::memory::MemoryStorage::new(),
            connection: None,
            operations: None,
            crdt_store: None,
            initialized: false,
        }
    }

    /// Initialize the IndexedDB database
    pub async fn initialize(&mut self) -> Result<(), StorageError> {
        if self.initialized {
            return Ok(());
        }

        match IndexedDbConnection::open(&self.db_name, 1).await {
            Ok(connection) => {
                let connection = Arc::new(connection);
                let operations = IndexedDbOperations::new(connection.clone());
                let crdt_store = CrdtStore::new(connection.clone());

                self.connection = Some(connection);
                self.operations = Some(operations);
                self.crdt_store = Some(crdt_store);
                self.initialized = true;
                Ok(())
            }
            Err(e) => {
                tracing::warn!(
                    "Failed to initialize IndexedDB: {:?}, falling back to memory storage",
                    e
                );
                self.initialized = true; // Mark as initialized to avoid retry
                Ok(()) // Don't fail, just use fallback
            }
        }
    }

    /// Ensure the storage is initialized
    async fn ensure_initialized(&mut self) -> Result<(), StorageError> {
        if !self.initialized {
            self.initialize().await?;
        }
        Ok(())
    }

    /// Get the operations instance (if available)
    fn get_operations(&self) -> Option<&IndexedDbOperations> {
        self.operations.as_ref()
    }

    /// Get the CRDT store instance (if available)
    fn get_crdt_store(&self) -> Option<&CrdtStore> {
        self.crdt_store.as_ref()
    }

    /// Check if IndexedDB is available and working
    pub fn is_indexeddb_available(&self) -> bool {
        self.connection.is_some()
    }

    /// Get storage statistics
    pub async fn get_stats(&self) -> Result<crdt_store::StorageStats, StorageError> {
        if let Some(crdt_store) = &self.crdt_store {
            crdt_store.get_stats().await.map_err(Into::into)
        } else {
            // Return empty stats if IndexedDB is not available
            Ok(crdt_store::StorageStats {
                collections_count: 0,
                deltas_count: 0,
                peers_count: 0,
            })
        }
    }

    /// Store a CRDT delta
    pub async fn store_delta(
        &mut self,
        collection_id: &str,
        delta: &[u8],
        replica_id: &str,
        operation_type: &str,
    ) -> Result<(), StorageError> {
        self.ensure_initialized().await?;

        if let Some(crdt_store) = &self.crdt_store {
            crdt_store
                .store_delta(collection_id, delta, replica_id, operation_type)
                .await
                .map_err(Into::into)
        } else {
            // Fall back to memory storage for deltas
            let key = format!("delta:{}:{}:{}", collection_id, replica_id, operation_type);
            self.fallback.set(&key, &delta.to_vec()).await
        }
    }

    /// Get CRDT deltas for a collection
    pub async fn get_deltas(
        &mut self,
        collection_id: &str,
        from_timestamp: Option<u64>,
        to_timestamp: Option<u64>,
    ) -> Result<Vec<crdt_store::DeltaRecord>, StorageError> {
        self.ensure_initialized().await?;

        if let Some(crdt_store) = &self.crdt_store {
            crdt_store
                .get_deltas(collection_id, from_timestamp, to_timestamp)
                .await
                .map_err(Into::into)
        } else {
            // Return empty deltas if IndexedDB is not available
            Ok(Vec::new())
        }
    }

    /// Store collection metadata
    pub async fn store_metadata(
        &mut self,
        metadata: &crdt_store::CollectionMetadata,
    ) -> Result<(), StorageError> {
        self.ensure_initialized().await?;

        if let Some(crdt_store) = &self.crdt_store {
            crdt_store
                .store_metadata(metadata)
                .await
                .map_err(Into::into)
        } else {
            // Fall back to memory storage for metadata
            let key = format!("metadata:{}", metadata.id);
            self.fallback.set(&key, metadata).await
        }
    }

    /// Get collection metadata
    pub async fn get_metadata(
        &mut self,
        collection_id: &str,
    ) -> Result<Option<crdt_store::CollectionMetadata>, StorageError> {
        self.ensure_initialized().await?;

        if let Some(crdt_store) = &self.crdt_store {
            crdt_store
                .get_metadata(collection_id)
                .await
                .map_err(Into::into)
        } else {
            // Fall back to memory storage for metadata
            let key = format!("metadata:{}", collection_id);
            self.fallback.get(&key).await
        }
    }

    /// List all collections
    pub async fn list_collections(
        &mut self,
    ) -> Result<Vec<crdt_store::CollectionMetadata>, StorageError> {
        self.ensure_initialized().await?;

        if let Some(crdt_store) = &self.crdt_store {
            crdt_store.list_collections().await.map_err(Into::into)
        } else {
            // Return empty list if IndexedDB is not available
            Ok(Vec::new())
        }
    }

    /// Store peer information
    pub async fn store_peer(&mut self, peer: &crdt_store::PeerInfo) -> Result<(), StorageError> {
        self.ensure_initialized().await?;

        if let Some(crdt_store) = &self.crdt_store {
            crdt_store.store_peer(peer).await.map_err(Into::into)
        } else {
            // Fall back to memory storage for peers
            let key = format!("peer:{}", peer.id);
            self.fallback.set(&key, peer).await
        }
    }

    /// Get peer information
    pub async fn get_peer(
        &mut self,
        peer_id: &str,
    ) -> Result<Option<crdt_store::PeerInfo>, StorageError> {
        self.ensure_initialized().await?;

        if let Some(crdt_store) = &self.crdt_store {
            crdt_store.get_peer(peer_id).await.map_err(Into::into)
        } else {
            // Fall back to memory storage for peers
            let key = format!("peer:{}", peer_id);
            self.fallback.get(&key).await
        }
    }

    /// List all peers
    pub async fn list_peers(&mut self) -> Result<Vec<crdt_store::PeerInfo>, StorageError> {
        self.ensure_initialized().await?;

        if let Some(crdt_store) = &self.crdt_store {
            crdt_store.list_peers().await.map_err(Into::into)
        } else {
            // Return empty list if IndexedDB is not available
            Ok(Vec::new())
        }
    }

    /// Clean up old deltas
    pub async fn cleanup_old_deltas(
        &mut self,
        collection_id: &str,
        keep_count: usize,
    ) -> Result<(), StorageError> {
        self.ensure_initialized().await?;

        if let Some(crdt_store) = &self.crdt_store {
            crdt_store
                .cleanup_old_deltas(collection_id, keep_count)
                .await
                .map_err(Into::into)
        } else {
            // No cleanup needed for memory storage
            Ok(())
        }
    }

    /// Clear all data
    pub async fn clear_all(&mut self) -> Result<(), StorageError> {
        self.ensure_initialized().await?;

        if let Some(crdt_store) = &self.crdt_store {
            crdt_store
                .clear_all()
                .await
                .map_err(|e| StorageError::OperationFailed(e.to_string()))?;
        }

        // Also clear fallback storage
        self.fallback.clear().await?;
        Ok(())
    }
}

impl Clone for IndexedDbStorage {
    fn clone(&self) -> Self {
        Self {
            db_name: self.db_name.clone(),
            store_name: self.store_name.clone(),
            fallback: self.fallback.clone(),
            connection: None,   // Cannot clone connections
            operations: None,   // Cannot clone operations
            crdt_store: None,   // Cannot clone CRDT store
            initialized: false, // Will need to reinitialize
        }
    }
}

#[async_trait]
impl LocalStorage for IndexedDbStorage {
    async fn set<T: Serialize + Send + Sync>(
        &self,
        key: &str,
        value: &T,
    ) -> Result<(), StorageError> {
        if let Some(operations) = &self.operations {
            operations
                .set(&self.store_name, key, value)
                .await
                .map_err(Into::into)
        } else {
            // Fall back to memory storage
            self.fallback.set(key, value).await
        }
    }

    async fn get<T: DeserializeOwned + Send + Sync>(
        &self,
        key: &str,
    ) -> Result<Option<T>, StorageError> {
        if let Some(operations) = &self.operations {
            operations
                .get(&self.store_name, key)
                .await
                .map_err(Into::into)
        } else {
            // Fall back to memory storage
            self.fallback.get(key).await
        }
    }

    async fn remove(&self, key: &str) -> Result<(), StorageError> {
        if let Some(operations) = &self.operations {
            operations
                .delete(&self.store_name, key)
                .await
                .map_err(Into::into)
        } else {
            // Fall back to memory storage
            self.fallback.remove(key).await
        }
    }

    async fn keys(&self) -> Result<Vec<String>, StorageError> {
        if let Some(operations) = &self.operations {
            operations.keys(&self.store_name).await.map_err(Into::into)
        } else {
            // Fall back to memory storage
            self.fallback.keys().await
        }
    }

    async fn contains_key(&self, key: &str) -> Result<bool, StorageError> {
        if let Some(operations) = &self.operations {
            operations
                .contains_key(&self.store_name, key)
                .await
                .map_err(Into::into)
        } else {
            // Fall back to memory storage
            self.fallback.contains_key(key).await
        }
    }

    async fn len(&self) -> Result<usize, StorageError> {
        if let Some(operations) = &self.operations {
            operations.count(&self.store_name).await.map_err(Into::into)
        } else {
            // Fall back to memory storage
            self.fallback.len().await
        }
    }

    async fn is_empty(&self) -> Result<bool, StorageError> {
        let len = self.len().await?;
        Ok(len == 0)
    }

    async fn clear(&self) -> Result<(), StorageError> {
        if let Some(operations) = &self.operations {
            operations.clear(&self.store_name).await.map_err(Into::into)
        } else {
            // Fall back to memory storage
            self.fallback.clear().await
        }
    }
}

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

    #[tokio::test]
    async fn test_indexeddb_storage_creation() {
        let storage = IndexedDbStorage::new("test_db".to_string(), "test_store".to_string());

        assert_eq!(storage.db_name, "test_db");
        assert_eq!(storage.store_name, "test_store");
        assert!(!storage.initialized);
        assert!(!storage.is_indexeddb_available());
    }

    #[tokio::test]
    async fn test_indexeddb_storage_fallback() {
        let mut storage = IndexedDbStorage::new("test_db".to_string(), "test_store".to_string());

        // Test that operations work (falling back to memory storage)
        assert!(storage.set("key1", &"value1".to_string()).await.is_ok());

        let value = storage.get::<String>("key1").await.unwrap();
        // Note: This might be None if IndexedDB is not available in test environment
        // The important thing is that it doesn't panic

        // Test remove
        assert!(storage.remove("key1").await.is_ok());
    }

    #[tokio::test]
    async fn test_indexeddb_storage_clone() {
        let storage = IndexedDbStorage::new("test_db".to_string(), "test_store".to_string());
        let cloned_storage = storage.clone();

        // Test that cloned storage works
        assert!(cloned_storage
            .set("key1", &"value1".to_string())
            .await
            .is_ok());
    }

    #[tokio::test]
    async fn test_indexeddb_storage_stats() {
        let mut storage = IndexedDbStorage::new("test_db".to_string(), "test_store".to_string());

        let stats = storage.get_stats().await.unwrap();
        assert_eq!(stats.collections_count, 0);
        assert_eq!(stats.deltas_count, 0);
        assert_eq!(stats.peers_count, 0);
    }
}