guardian-db 0.15.0

High-performance, local-first decentralized database built on Rust and Iroh
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
use crate::address::Address;
use crate::cache::{Cache, Options};
use crate::data_store::{Datastore, Key, Order, Query, ResultItem, Results};
use crate::guardian::error::{GuardianError, Result};
use sled::{Config, Db, IVec};
use std::{
    collections::HashMap,
    path::{Path, PathBuf},
    sync::{Arc, Mutex, Weak},
};
use tracing::{Span, debug, instrument};

pub const IN_MEMORY_DIRECTORY: &str = ":memory:";
pub struct WrappedCache {
    id: String,
    db: Db,
    manager_map: Weak<Mutex<HashMap<String, Arc<WrappedCache>>>>,
    #[allow(dead_code)]
    span: Span,
    closed: Mutex<bool>,
}

impl WrappedCache {
    #[instrument(level = "debug", skip(self, _ctx))]
    pub fn get(
        &self,
        _ctx: &mut dyn core::any::Any,
        key: &Key,
    ) -> std::result::Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
        match self
            .db
            .get(key.as_bytes())
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?
        {
            Some(v) => Ok(v.to_vec()),
            None => Err(format!("key not found: {}", key).into()),
        }
    }

    pub fn has(
        &self,
        _ctx: &mut dyn core::any::Any,
        key: &Key,
    ) -> std::result::Result<bool, Box<dyn std::error::Error + Send + Sync>> {
        self.db
            .contains_key(key.as_bytes())
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)
    }

    pub fn get_size(
        &self,
        _ctx: &mut dyn core::any::Any,
        key: &Key,
    ) -> std::result::Result<usize, Box<dyn std::error::Error + Send + Sync>> {
        let v = self
            .db
            .get(key.as_bytes())
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?
            .ok_or_else(|| format!("key not found: {}", key))?;
        Ok(v.len())
    }

    pub fn query(
        &self,
        _ctx: &mut dyn core::any::Any,
        q: &Query,
    ) -> std::result::Result<Results, Box<dyn std::error::Error + Send + Sync>> {
        let iter: Box<dyn Iterator<Item = sled::Result<(IVec, IVec)>>> =
            if let Some(prefix_key) = &q.prefix {
                // Converte Key para bytes para usar como prefixo
                let prefix_bytes = prefix_key.as_bytes();
                Box::new(self.db.scan_prefix(prefix_bytes))
            } else {
                Box::new(self.db.iter())
            };

        // coleta (ordenado asc pelo sled por padrão)
        let mut items: Results = Vec::new();
        let mut count = 0;

        // Aplica offset se especificado
        let skip_count = q.offset.unwrap_or(0);
        let mut skipped = 0;

        for kv in iter {
            let (k, v) = kv.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;

            // Aplica offset
            if skipped < skip_count {
                skipped += 1;
                continue;
            }

            let key_str = String::from_utf8(k.to_vec()).unwrap_or_default();
            items.push(ResultItem::new(Key::new(key_str), v.to_vec()));
            count += 1;

            if let Some(n) = q.limit
                && count >= n
            {
                break;
            }
        }

        if matches!(q.order, Order::Desc) {
            items.reverse();
        }

        Ok(items)
    }

    #[instrument(level = "debug", skip(self, _ctx, value))]
    pub fn put(
        &self,
        _ctx: &mut dyn core::any::Any,
        key: &Key,
        value: &[u8],
    ) -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync>> {
        self.db
            .insert(key.as_bytes(), value)
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
        Ok(())
    }

    pub fn delete(
        &self,
        _ctx: &mut dyn core::any::Any,
        key: &Key,
    ) -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync>> {
        self.db
            .remove(key.as_bytes())
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
        Ok(())
    }

    pub fn sync(
        &self,
        _ctx: &mut dyn core::any::Any,
        _key: &Key,
    ) -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync>> {
        self.db
            .flush()
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
        Ok(())
    }

    #[instrument(level = "debug", skip(self))]
    pub fn close(&self) -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut closed = self.closed.lock().unwrap();
        if *closed {
            return Ok(());
        }

        if let Some(map) = self.manager_map.upgrade() {
            let mut m = map.lock().unwrap();
            m.remove(&self.id);
        }

        self.db
            .flush()
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
        *closed = true;
        Ok(())
    }
}

// Wrapper para adaptar WrappedCache ao trait Datastore
pub struct DatastoreWrapper {
    cache: Arc<WrappedCache>,
}

impl DatastoreWrapper {
    pub fn new(cache: Arc<WrappedCache>) -> Self {
        Self { cache }
    }
}

#[async_trait::async_trait]
impl Datastore for DatastoreWrapper {
    #[instrument(level = "debug", skip(self, key))]
    async fn has(&self, key: &[u8]) -> Result<bool> {
        let key_obj = Key::new(String::from_utf8_lossy(key));
        let mut any_ctx = ();
        self.cache
            .has(&mut any_ctx, &key_obj)
            .map_err(|e| GuardianError::Other(format!("Cache has error: {}", e)))
    }

    #[instrument(level = "debug", skip(self, key, value))]
    async fn put(&self, key: &[u8], value: &[u8]) -> Result<()> {
        let key_obj = Key::new(String::from_utf8_lossy(key));
        let mut any_ctx = ();
        self.cache
            .put(&mut any_ctx, &key_obj, value)
            .map_err(|e| GuardianError::Other(format!("Cache put error: {}", e)))
    }

    #[instrument(level = "debug", skip(self, key))]
    async fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
        let key_obj = Key::new(String::from_utf8_lossy(key));
        let mut any_ctx = ();
        match self.cache.get(&mut any_ctx, &key_obj) {
            Ok(value) => Ok(Some(value)),
            Err(_) => Ok(None), // key not found
        }
    }

    #[instrument(level = "debug", skip(self, key))]
    async fn delete(&self, key: &[u8]) -> Result<()> {
        let key_obj = Key::new(String::from_utf8_lossy(key));
        let mut any_ctx = ();
        self.cache
            .delete(&mut any_ctx, &key_obj)
            .map_err(|e| GuardianError::Other(format!("Cache delete error: {}", e)))
    }

    #[instrument(level = "debug", skip(self, query))]
    async fn query(&self, query: &Query) -> Result<Results> {
        let mut any_ctx = ();
        self.cache
            .query(&mut any_ctx, query)
            .map_err(|e| GuardianError::Other(format!("Cache query error: {}", e)))
    }

    #[instrument(level = "debug", skip(self, prefix))]
    async fn list_keys(&self, prefix: &[u8]) -> Result<Vec<Key>> {
        // Converte o prefixo em bytes para uma Query com prefixo
        let prefix_str = String::from_utf8_lossy(prefix);
        let prefix_key = Key::new(prefix_str.to_string());

        let query = Query {
            prefix: Some(prefix_key),
            limit: None,
            order: Order::Asc,
            offset: None,
        };

        let mut any_ctx = ();
        let results = self
            .cache
            .query(&mut any_ctx, &query)
            .map_err(|e| GuardianError::Other(format!("Cache list_keys error: {}", e)))?;

        // Extrai apenas as chaves dos resultados
        Ok(results.into_iter().map(|item| item.key).collect())
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

pub struct LevelDownCache {
    span: Span,
    caches: Arc<Mutex<HashMap<String, Arc<WrappedCache>>>>,
}

impl LevelDownCache {
    #[instrument(level = "debug")]
    pub fn new(_opts: Option<&Options>) -> Self {
        Self {
            span: tracing::Span::current(),
            caches: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Retorna uma referência ao span de tracing para instrumentação
    pub fn span(&self) -> &Span {
        &self.span
    }

    #[instrument(level = "debug", skip(self, db_address))]
    pub fn load_internal(
        &self,
        directory: &str,
        db_address: &dyn Address,
    ) -> std::result::Result<Arc<WrappedCache>, Box<dyn std::error::Error + Send + Sync>> {
        let _entered = self.span.enter();
        let key_path = datastore_key(directory, db_address);

        // cache hit
        if let Some(ds) = self.caches.lock().unwrap().get(&key_path).cloned() {
            return Ok(ds);
        }

        debug!("opening cache db: path={}", key_path.as_str());

        let db = if directory == IN_MEMORY_DIRECTORY {
            Config::new()
                .temporary(true)
                .open()
                .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?
        } else {
            if let Some(parent) = Path::new(&key_path).parent() {
                std::fs::create_dir_all(parent)?;
            }
            Config::new()
                .path(&key_path)
                .open()
                .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?
        };

        let wrapped = Arc::new(WrappedCache {
            id: key_path.clone(),
            db,
            manager_map: Arc::downgrade(&self.caches),
            span: tracing::Span::current(),
            closed: Mutex::new(false),
        });

        self.caches
            .lock()
            .unwrap()
            .insert(key_path, wrapped.clone());
        Ok(wrapped)
    }

    #[instrument(level = "debug", skip(self))]
    pub fn close_internal(
        &self,
    ) -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let _entered = self.span.enter();
        let caches = {
            let m = self.caches.lock().unwrap();
            m.values().cloned().collect::<Vec<_>>()
        };
        for c in caches {
            let _ = c.close();
        }
        Ok(())
    }

    #[instrument(level = "debug", skip(self, db_address))]
    pub fn destroy_internal(
        &self,
        directory: &str,
        db_address: &dyn Address,
    ) -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let _entered = self.span.enter();
        let key_path = datastore_key(directory, db_address);

        // fecha e remove do mapa
        if let Some(c) = self.caches.lock().unwrap().remove(&key_path) {
            let _ = c.close();
        }

        if directory != IN_MEMORY_DIRECTORY && Path::new(&key_path).exists() {
            std::fs::remove_dir_all(&key_path)?;
        }

        Ok(())
    }
}

// Implementação do trait Cache para LevelDownCache
impl Cache for LevelDownCache {
    #[instrument(level = "info", skip(self, db_address))]
    fn load(
        &self,
        directory: &str,
        db_address: &dyn Address,
    ) -> Result<Box<dyn Datastore + Send + Sync>> {
        let _entered = self.span.enter();
        let wrapped_cache = self
            .load_internal(directory, db_address)
            .map_err(|e| GuardianError::Other(format!("Failed to load cache: {}", e)))?;
        Ok(Box::new(DatastoreWrapper {
            cache: wrapped_cache,
        }))
    }

    #[instrument(level = "info", skip(self))]
    fn close(&mut self) -> Result<()> {
        let _entered = self.span.enter();
        let caches = {
            let m = self.caches.lock().unwrap();
            m.values().cloned().collect::<Vec<_>>()
        };
        for c in caches {
            let _ = c.close();
        }
        Ok(())
    }

    #[instrument(level = "info", skip(self, db_address))]
    fn destroy(&self, directory: &str, db_address: &dyn Address) -> Result<()> {
        let _entered = self.span.enter();
        self.destroy_internal(directory, db_address)
            .map_err(|e| GuardianError::Other(format!("Failed to destroy cache: {}", e)))?;
        Ok(())
    }
}

fn datastore_key(directory: &str, db_address: &dyn Address) -> String {
    let db_path = PathBuf::from(db_address.get_root().to_string()).join(db_address.get_path());
    PathBuf::from(directory)
        .join(db_path)
        .to_string_lossy()
        .into_owned()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::address::Address;
    use std::fmt;

    // Mock Address implementation for testing
    #[derive(Debug)]
    struct MockAddress {
        root: iroh_blobs::Hash,
        path: String,
    }

    impl MockAddress {
        fn new(root_str: &str, path: &str) -> Self {
            // Create a Hash from the root string for more meaningful testing
            // Para teste, vamos usar um Hash baseado no root_str
            use blake3;
            let hash_bytes: [u8; 32] = blake3::hash(root_str.as_bytes()).into();
            let hash = iroh_blobs::Hash::from(hash_bytes);
            Self {
                root: hash,
                path: path.to_string(),
            }
        }
    }

    impl Address for MockAddress {
        fn get_root(&self) -> iroh_blobs::Hash {
            self.root
        }

        fn get_path(&self) -> &str {
            &self.path
        }

        fn equals(&self, other: &dyn Address) -> bool {
            self.root == other.get_root() && self.path == other.get_path()
        }
    }

    impl fmt::Display for MockAddress {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{}/{}", hex::encode(self.root.as_bytes()), self.path)
        }
    }

    #[tokio::test]
    async fn test_datastore_wrapper_basic_operations() {
        let cache = LevelDownCache::new(None);
        let mock_address = MockAddress::new("test_root", "test_path");

        let datastore = cache.load(IN_MEMORY_DIRECTORY, &mock_address).unwrap();

        // Test put and get
        let key = b"test_key";
        let value = b"test_value";

        datastore.put(key, value).await.unwrap();
        let retrieved = datastore.get(key).await.unwrap();
        assert_eq!(retrieved, Some(value.to_vec()));

        // Test has
        assert!(datastore.has(key).await.unwrap());
        assert!(!datastore.has(b"non_existent").await.unwrap());

        // Test delete
        datastore.delete(key).await.unwrap();
        assert!(!datastore.has(key).await.unwrap());
    }

    #[tokio::test]
    async fn test_datastore_wrapper_query() {
        let cache = LevelDownCache::new(None);
        let mock_address = MockAddress::new("test_root", "test_path");

        let datastore = cache.load(IN_MEMORY_DIRECTORY, &mock_address).unwrap();

        // Insert test data
        datastore.put(b"/users/alice", b"alice_data").await.unwrap();
        datastore.put(b"/users/bob", b"bob_data").await.unwrap();
        datastore
            .put(b"/config/database", b"db_config")
            .await
            .unwrap();

        // Test query with prefix
        let query = Query {
            prefix: Some(Key::new("/users")),
            limit: Some(10),
            order: Order::Asc,
            offset: None,
        };

        let results = datastore.query(&query).await.unwrap();
        assert_eq!(results.len(), 2);

        // Test list_keys
        let keys = datastore.list_keys(b"/users").await.unwrap();
        assert_eq!(keys.len(), 2);
    }

    #[test]
    fn test_datastore_key_generation() {
        let mock_address = MockAddress::new("root", "path/to/db");
        let key = datastore_key("/cache", &mock_address);

        // Debug: vamos ver o que está sendo gerado
        println!("Generated key: {}", key);
        println!("Root Hash: {}", mock_address.get_root());
        println!("Path: {}", mock_address.get_path());

        // The exact format depends on the platform path separator
        assert!(key.contains("cache"));
        assert!(key.contains("path"));
        assert!(key.contains(&mock_address.get_root().to_string()));
    }

    #[tokio::test]
    #[ignore] // Test takes too long in CI environment
    async fn test_cache_lifecycle() {
        let mut cache = LevelDownCache::new(None);
        let mock_address = MockAddress::new("test_root", "lifecycle_test");

        // Load cache
        let datastore = cache.load(IN_MEMORY_DIRECTORY, &mock_address).unwrap();
        datastore.put(b"test", b"data").await.unwrap();

        // Destroy cache
        cache.destroy(IN_MEMORY_DIRECTORY, &mock_address).unwrap();

        // Close cache
        cache.close().unwrap();
    }
}