guardian-db 0.19.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
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
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 redb::{Database, ReadableDatabase, ReadableTable, TableDefinition};
use std::{
    collections::HashMap,
    path::{Path, PathBuf},
    sync::{Arc, Mutex, Weak},
};
use tracing::{Span, debug, instrument};

pub const IN_MEMORY_DIRECTORY: &str = ":memory:";

const LEVEL_DOWN_TABLE: TableDefinition<&[u8], &[u8]> = TableDefinition::new("level_down");

/// A single redb-backed cache instance, keyed by its datastore path and
/// tracked by the owning `LevelDownCache` manager.
pub struct WrappedCache {
    id: String,
    db: Database,
    manager_map: Weak<Mutex<HashMap<String, Arc<WrappedCache>>>>,
    #[allow(dead_code)]
    span: Span,
    closed: Mutex<bool>,
}

impl WrappedCache {
    /// Returns the value stored under `key`, or an error if it is missing.
    #[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>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(LEVEL_DOWN_TABLE)?;
        match table.get(key.as_bytes().as_slice())? {
            Some(v) => Ok(v.value().to_vec()),
            None => Err(format!("key not found: {}", key).into()),
        }
    }

    /// Returns whether a value exists under `key`.
    pub fn has(
        &self,
        _ctx: &mut dyn core::any::Any,
        key: &Key,
    ) -> std::result::Result<bool, Box<dyn std::error::Error + Send + Sync>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(LEVEL_DOWN_TABLE)?;
        Ok(table.get(key.as_bytes().as_slice())?.is_some())
    }

    /// Returns the byte length of the value stored under `key`.
    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 read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(LEVEL_DOWN_TABLE)?;
        let v = table
            .get(key.as_bytes().as_slice())?
            .ok_or_else(|| format!("key not found: {}", key))?;
        Ok(v.value().len())
    }

    /// Runs a query against the cache, supporting optional prefix filtering,
    /// offset, limit and ordering.
    pub fn query(
        &self,
        _ctx: &mut dyn core::any::Any,
        q: &Query,
    ) -> std::result::Result<Results, Box<dyn std::error::Error + Send + Sync>> {
        let read_txn = self.db.begin_read()?;
        let table = read_txn.open_table(LEVEL_DOWN_TABLE)?;

        let mut items: Results = Vec::new();
        let mut count = 0;
        let skip_count = q.offset.unwrap_or(0);
        let mut skipped = 0;

        if let Some(prefix_key) = &q.prefix {
            let prefix_bytes = prefix_key.as_bytes();
            let iter = table.range(prefix_bytes.as_slice()..)?;

            for entry_result in iter {
                let entry = entry_result?;
                let key_bytes = entry.0.value();
                if !key_bytes.starts_with(&prefix_bytes) {
                    break;
                }

                if skipped < skip_count {
                    skipped += 1;
                    continue;
                }

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

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

            for entry_result in iter {
                let entry = entry_result?;

                if skipped < skip_count {
                    skipped += 1;
                    continue;
                }

                let key_bytes = entry.0.value();
                let key_str = String::from_utf8(key_bytes.to_vec()).unwrap_or_default();
                items.push(ResultItem::new(Key::new(key_str), entry.1.value().to_vec()));
                count += 1;

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

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

        Ok(items)
    }

    /// Stores `value` under `key`, committing the write transaction.
    #[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>> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(LEVEL_DOWN_TABLE)?;
            table.insert(key.as_bytes().as_slice(), value)?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Removes the value stored under `key`, committing the write transaction.
    pub fn delete(
        &self,
        _ctx: &mut dyn core::any::Any,
        key: &Key,
    ) -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let write_txn = self.db.begin_write()?;
        {
            let mut table = write_txn.open_table(LEVEL_DOWN_TABLE)?;
            table.remove(key.as_bytes().as_slice())?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// No-op flush: data is already persisted via redb write transactions.
    pub fn sync(
        &self,
        _ctx: &mut dyn core::any::Any,
        _key: &Key,
    ) -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync>> {
        // Data is already persisted via write transactions in redb.
        Ok(())
    }

    /// Closes the cache and unregisters it from the manager map (idempotent).
    #[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);
        }

        // Data is already persisted via write transactions in redb.
        *closed = true;
        Ok(())
    }
}

// Send + Sync is safe because redb::Database is thread-safe
unsafe impl Send for WrappedCache {}
unsafe impl Sync for WrappedCache {}

/// Wrapper that adapts a `WrappedCache` to the `Datastore` trait.
pub struct DatastoreWrapper {
    cache: Arc<WrappedCache>,
}

impl DatastoreWrapper {
    /// Wraps a shared `WrappedCache` in the adapter.
    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>> {
        // Convert the byte prefix into a prefix Query.
        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)))?;

        // Extract only the keys from the results.
        Ok(results.into_iter().map(|item| item.key).collect())
    }

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

/// Cache manager that owns a set of `WrappedCache` instances keyed by their
/// datastore path, and implements the `Cache` trait.
pub struct LevelDownCache {
    span: Span,
    caches: Arc<Mutex<HashMap<String, Arc<WrappedCache>>>>,
}

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

    /// Returns a reference to the tracing span used for instrumentation.
    pub fn span(&self) -> &Span {
        &self.span
    }

    /// Loads (or creates) the `WrappedCache` for a directory and address,
    /// reusing an existing instance on a cache hit.
    #[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 {
            Database::builder().create_with_backend(redb::backends::InMemoryBackend::new())?
        } else {
            if let Some(parent) = Path::new(&key_path).parent() {
                std::fs::create_dir_all(parent)?;
            }
            Database::create(&key_path)?
        };

        // Ensure table exists
        {
            let write_txn = db.begin_write()?;
            {
                let _ = write_txn.open_table(LEVEL_DOWN_TABLE)?;
            }
            write_txn.commit()?;
        }

        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)
    }

    /// Closes every cache instance owned by this manager.
    #[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(())
    }

    /// Closes and removes the cache for a directory/address, deleting its file
    /// from disk when it is not an in-memory cache.
    #[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);

        // Close it and remove it from the map.
        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_file(&key_path)?;
        }

        Ok(())
    }
}

// Cache trait implementation for 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(())
    }
}

/// Builds the on-disk/in-memory key path for a datastore from its directory and
/// database address (root hash joined with the address path).
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.
            // For the test, use a Hash derived from 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: inspect what is being generated.
        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();
    }
}