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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
use crate::address::Address;
use crate::data_store::Datastore;
use crate::guardian::error::{GuardianError, Result};
use redb::{Database, ReadableDatabase, ReadableTable, TableDefinition};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use tracing::{Span, debug, error, info, instrument, warn};

#[allow(clippy::module_inception)]
pub mod level_down;
pub use level_down::LevelDownCache;

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

// Type aliases to simplify complex types.
type DatastoreBox = Box<dyn Datastore + Send + Sync>;
type CleanupFn = Box<dyn FnOnce() -> Result<()> + Send + Sync>;
type NewCacheResult = Result<(DatastoreBox, CleanupFn)>;

/// Defines the options for creating a cache.
#[derive(Debug, Clone)]
pub struct Options {
    /// Span for structured logging with tracing.
    pub span: Option<Span>,
    /// Maximum cache size in bytes (default: 100MB).
    pub max_cache_size: Option<u64>,
    /// Cache mode: persistent or in-memory.
    pub cache_mode: CacheMode,
}

/// Cache operating mode.
#[derive(Debug, Clone, PartialEq)]
pub enum CacheMode {
    /// Persistent cache on disk.
    Persistent,
    /// In-memory (temporary) cache.
    InMemory,
    /// Automatic: detected based on the directory.
    Auto,
}

impl Default for Options {
    fn default() -> Self {
        Self {
            span: None,
            max_cache_size: Some(100 * 1024 * 1024), // 100MB
            cache_mode: CacheMode::Auto,
        }
    }
}

/// The `Cache` trait defines the interface for a caching mechanism
/// for GuardianDB databases.
pub trait Cache: Send + Sync {
    /// Creates a new cache instance at the specified path.
    /// Returns a Datastore and a cleanup function.
    #[allow(clippy::new_ret_no_self)]
    fn new(path: &str, opts: Option<Options>) -> NewCacheResult
    where
        Self: Sized,
    {
        RedbCache::create_cache_instance(path, opts.unwrap_or_default())
    }

    /// Loads a cache for a given database address and root directory.
    fn load(&self, directory: &str, db_address: &dyn Address) -> Result<DatastoreBox>;

    /// Closes a cache and all of its associated datastores.
    fn close(&mut self) -> Result<()>;

    /// Removes all cached data for a database.
    fn destroy(&self, directory: &str, db_address: &dyn Address) -> Result<()>;
}

/// Cache implementation using redb as the backend.
pub struct RedbCache {
    caches: Arc<Mutex<HashMap<String, Arc<RedbDatastore>>>>,
    options: Options,
}

impl RedbCache {
    /// Creates a new RedbCache instance.
    pub fn new(opts: Options) -> Self {
        Self {
            caches: Arc::new(Mutex::new(HashMap::new())),
            options: opts,
        }
    }

    /// Factory method for creating cache instances.
    #[instrument(level = "info")]
    pub fn create_cache_instance(path: &str, opts: Options) -> NewCacheResult {
        info!("Creating cache instance: path={}", path);

        let datastore = RedbDatastore::new(path, opts.clone())?;
        let path_clone = path.to_string();

        // Cleanup function that removes the cache from disk (only if not in-memory).
        let cleanup: Box<dyn FnOnce() -> Result<()> + Send + Sync> = Box::new(move || {
            if path_clone != ":memory:" && Path::new(&path_clone).exists() {
                match std::fs::remove_file(&path_clone) {
                    Ok(_) => {
                        debug!("Cache file cleaned up: path={}", &path_clone);
                        Ok(())
                    }
                    Err(e) => {
                        warn!(
                            "Failed to cleanup cache file: path={}, error={}",
                            &path_clone, e
                        );
                        Err(GuardianError::Other(format!(
                            "Failed to cleanup cache: {}",
                            e
                        )))
                    }
                }
            } else {
                Ok(())
            }
        });

        Ok((Box::new(datastore), cleanup))
    }

    /// Generates a unique cache key based on the directory and address.
    fn generate_cache_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()
            .to_string()
    }
}

impl Cache for RedbCache {
    #[instrument(level = "info", skip(self, db_address))]
    fn load(
        &self,
        directory: &str,
        db_address: &dyn Address,
    ) -> Result<Box<dyn Datastore + Send + Sync>> {
        let cache_key = Self::generate_cache_key(directory, db_address);

        info!(
            "Loading cache: directory={}, cache_key={}",
            directory, &cache_key
        );

        let mut caches = self.caches.lock().unwrap();

        if let Some(existing_cache) = caches.get(&cache_key) {
            debug!("Using existing cache: cache_key={}", &cache_key);
            return Ok(Box::new(RedbDatastoreHandle {
                inner: existing_cache.clone(),
            }));
        }

        // Create a new cache if it does not exist.
        let datastore = Arc::new(RedbDatastore::new(&cache_key, self.options.clone())?);
        caches.insert(cache_key.clone(), datastore.clone());

        info!("Created new cache: cache_key={}", &cache_key);
        Ok(Box::new(RedbDatastoreHandle { inner: datastore }))
    }

    #[instrument(level = "info", skip(self))]
    fn close(&mut self) -> Result<()> {
        info!("Closing all caches");

        let caches = {
            let mut cache_map = self.caches.lock().unwrap();
            let caches: Vec<Arc<RedbDatastore>> = cache_map.values().cloned().collect();
            cache_map.clear();
            caches
        };

        for cache in caches {
            if let Err(e) = cache.flush() {
                warn!("Failed to close cache: error={}", e);
            }
        }

        info!("All caches closed");
        Ok(())
    }

    #[instrument(level = "info", skip(self, db_address))]
    fn destroy(&self, directory: &str, db_address: &dyn Address) -> Result<()> {
        let cache_key = Self::generate_cache_key(directory, db_address);

        info!(
            "Destroying cache: directory={}, cache_key={}",
            directory, &cache_key
        );

        // Remove it from the cache map.
        {
            let mut caches = self.caches.lock().unwrap();
            caches.remove(&cache_key);
        }

        // Remove the file from disk (only if not in-memory).
        if directory != ":memory:" && Path::new(&cache_key).exists() {
            std::fs::remove_file(&cache_key)
                .map_err(|e| GuardianError::Other(format!("Failed to remove cache file: {}", e)))?;

            info!("Cache file removed: path={}", &cache_key);
        }

        Ok(())
    }
}

/// Datastore implementation using redb.
pub struct RedbDatastore {
    db: Database,
    path: String,
    span: Span,
}

// RedbDatastore cannot be Clone because redb::Database is not Clone.
// We use Arc<RedbDatastore> + a handle wrapper for compatibility.

impl RedbDatastore {
    /// Creates a new RedbDatastore instance.
    #[instrument(level = "debug")]
    pub fn new(path: &str, opts: Options) -> Result<Self> {
        debug!("Creating RedbDatastore: path={}", path);

        let db = if path == ":memory:" || matches!(opts.cache_mode, CacheMode::InMemory) {
            debug!("Creating in-memory cache");
            Database::builder()
                .create_with_backend(redb::backends::InMemoryBackend::new())
                .map_err(|e| {
                    GuardianError::Store(format!("Failed to create in-memory cache: {}", e))
                })?
        } else {
            debug!("Creating persistent cache: path={}", path);

            // Create the directory if it does not exist.
            if let Some(parent) = Path::new(path).parent() {
                std::fs::create_dir_all(parent).map_err(|e| {
                    GuardianError::Store(format!("Failed to create cache directory: {}", e))
                })?;
            }

            Database::create(path).map_err(|e| {
                GuardianError::Store(format!("Failed to open cache at {}: {}", path, e))
            })?
        };

        // Ensure table exists
        {
            let write_txn = db.begin_write().map_err(|e| {
                GuardianError::Store(format!("Failed to begin write transaction: {}", e))
            })?;
            {
                let _ = write_txn
                    .open_table(REDB_TABLE)
                    .map_err(|e| GuardianError::Store(format!("Failed to create table: {}", e)))?;
            }
            write_txn.commit().map_err(|e| {
                GuardianError::Store(format!("Failed to commit table creation: {}", e))
            })?;
        }

        info!(
            "RedbDatastore created successfully: path={}, memory_mode={}",
            path,
            path == ":memory:"
        );

        Ok(Self {
            db,
            path: path.to_string(),
            span: opts.span.unwrap_or_else(tracing::Span::current),
        })
    }

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

    /// Flushes (compacts) the datastore.
    #[instrument(level = "debug", skip(self))]
    pub fn flush(&self) -> Result<()> {
        let _entered = self.span.enter();
        debug!("Flushing RedbDatastore: path={}", &self.path);

        // Data is already persisted via write transactions in redb.
        // compact() requires &mut self, which is incompatible with Arc-based access.

        info!("RedbDatastore flushed: path={}", &self.path);
        Ok(())
    }
}

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

#[async_trait::async_trait]
impl Datastore for RedbDatastore {
    #[instrument(level = "debug", skip(self, key))]
    async fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
        let _entered = self.span.enter();
        let read_txn = self
            .db
            .begin_read()
            .map_err(|e| GuardianError::Store(format!("Cache get read txn error: {}", e)))?;
        let table = read_txn
            .open_table(REDB_TABLE)
            .map_err(|e| GuardianError::Store(format!("Cache get open table error: {}", e)))?;
        match table.get(key) {
            Ok(Some(value)) => {
                debug!("Cache hit: key_len={}", key.len());
                Ok(Some(value.value().to_vec()))
            }
            Ok(None) => {
                debug!("Cache miss: key_len={}", key.len());
                Ok(None)
            }
            Err(e) => {
                error!("Cache get error: key_len={}, error={}", key.len(), e);
                Err(GuardianError::Store(format!("Cache get error: {}", e)))
            }
        }
    }

    #[instrument(level = "debug", skip(self, key, value))]
    async fn put(&self, key: &[u8], value: &[u8]) -> Result<()> {
        let _entered = self.span.enter();
        let write_txn = self
            .db
            .begin_write()
            .map_err(|e| GuardianError::Store(format!("Cache put write txn error: {}", e)))?;
        {
            let mut table = write_txn
                .open_table(REDB_TABLE)
                .map_err(|e| GuardianError::Store(format!("Cache put open table error: {}", e)))?;
            table
                .insert(key, value)
                .map_err(|e| GuardianError::Store(format!("Cache put error: {}", e)))?;
        }
        write_txn
            .commit()
            .map_err(|e| GuardianError::Store(format!("Cache put commit error: {}", e)))?;
        debug!(
            "Cache put success: key_len={}, value_len={}",
            key.len(),
            value.len()
        );
        Ok(())
    }

    #[instrument(level = "debug", skip(self, key))]
    async fn has(&self, key: &[u8]) -> Result<bool> {
        let _entered = self.span.enter();
        let read_txn = self
            .db
            .begin_read()
            .map_err(|e| GuardianError::Store(format!("Cache has read txn error: {}", e)))?;
        let table = read_txn
            .open_table(REDB_TABLE)
            .map_err(|e| GuardianError::Store(format!("Cache has open table error: {}", e)))?;
        match table.get(key) {
            Ok(Some(_)) => {
                debug!("Cache has check: key_len={}, exists=true", key.len());
                Ok(true)
            }
            Ok(None) => {
                debug!("Cache has check: key_len={}, exists=false", key.len());
                Ok(false)
            }
            Err(e) => {
                error!("Cache has error: key_len={}, error={}", key.len(), e);
                Err(GuardianError::Store(format!("Cache has error: {}", e)))
            }
        }
    }

    #[instrument(level = "debug", skip(self, key))]
    async fn delete(&self, key: &[u8]) -> Result<()> {
        let _entered = self.span.enter();
        let write_txn = self
            .db
            .begin_write()
            .map_err(|e| GuardianError::Store(format!("Cache delete write txn error: {}", e)))?;
        {
            let mut table = write_txn.open_table(REDB_TABLE).map_err(|e| {
                GuardianError::Store(format!("Cache delete open table error: {}", e))
            })?;
            table
                .remove(key)
                .map_err(|e| GuardianError::Store(format!("Cache delete error: {}", e)))?;
        }
        write_txn
            .commit()
            .map_err(|e| GuardianError::Store(format!("Cache delete commit error: {}", e)))?;
        debug!("Cache delete success: key_len={}", key.len());
        Ok(())
    }

    #[instrument(level = "debug", skip(self, query))]
    async fn query(&self, query: &crate::data_store::Query) -> Result<crate::data_store::Results> {
        let _entered = self.span.enter();
        use crate::data_store::{Key, ResultItem};

        debug!(
            "Cache query: has_prefix={}, limit={:?}, order={:?}",
            query.prefix.is_some(),
            query.limit,
            query.order
        );

        let read_txn = self
            .db
            .begin_read()
            .map_err(|e| GuardianError::Store(format!("Cache query read txn error: {}", e)))?;
        let table = read_txn
            .open_table(REDB_TABLE)
            .map_err(|e| GuardianError::Store(format!("Cache query open table error: {}", e)))?;

        let mut results = Vec::new();
        let mut count = 0;
        let skip_count = query.offset.unwrap_or(0);
        let mut skipped = 0;

        if let Some(prefix_key) = &query.prefix {
            let prefix_bytes = prefix_key.as_bytes();
            // Use range scan starting from prefix
            let iter = table
                .range(prefix_bytes.as_slice()..)
                .map_err(|e| GuardianError::Store(format!("Cache query range error: {}", e)))?;

            for entry_result in iter {
                match entry_result {
                    Ok(entry) => {
                        let key_bytes = entry.0.value();
                        // Check if key still has the prefix
                        if !key_bytes.starts_with(&prefix_bytes) {
                            break;
                        }

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

                        let key_str = String::from_utf8_lossy(key_bytes);
                        let key = Key::new(key_str.to_string());
                        let value = entry.1.value().to_vec();
                        results.push(ResultItem::new(key, value));
                        count += 1;

                        if let Some(limit) = query.limit
                            && count >= limit
                        {
                            break;
                        }
                    }
                    Err(e) => {
                        error!("Cache query iteration error: error={}", e);
                        return Err(GuardianError::Store(format!("Cache query error: {}", e)));
                    }
                }
            }
        } else {
            let iter = table
                .iter()
                .map_err(|e| GuardianError::Store(format!("Cache query iter error: {}", e)))?;

            for entry_result in iter {
                match entry_result {
                    Ok(entry) => {
                        if skipped < skip_count {
                            skipped += 1;
                            continue;
                        }

                        let key_bytes = entry.0.value();
                        let key_str = String::from_utf8_lossy(key_bytes);
                        let key = Key::new(key_str.to_string());
                        let value = entry.1.value().to_vec();
                        results.push(ResultItem::new(key, value));
                        count += 1;

                        if let Some(limit) = query.limit
                            && count >= limit
                        {
                            break;
                        }
                    }
                    Err(e) => {
                        error!("Cache query iteration error: error={}", e);
                        return Err(GuardianError::Store(format!("Cache query error: {}", e)));
                    }
                }
            }
        }

        // redb returns in ascending order by default
        if matches!(query.order, crate::data_store::Order::Desc) {
            results.reverse();
        }

        debug!(
            "Cache query completed: results_count={}, skipped={}",
            results.len(),
            skipped
        );

        Ok(results)
    }

    #[instrument(level = "debug", skip(self, prefix))]
    async fn list_keys(&self, prefix: &[u8]) -> Result<Vec<crate::data_store::Key>> {
        let _entered = self.span.enter();
        use crate::data_store::Key;

        debug!("Cache list_keys: prefix_len={}", prefix.len());

        let read_txn = self
            .db
            .begin_read()
            .map_err(|e| GuardianError::Store(format!("Cache list_keys read txn error: {}", e)))?;
        let table = read_txn.open_table(REDB_TABLE).map_err(|e| {
            GuardianError::Store(format!("Cache list_keys open table error: {}", e))
        })?;

        let mut keys = Vec::new();
        let iter = table
            .range(prefix..)
            .map_err(|e| GuardianError::Store(format!("Cache list_keys range error: {}", e)))?;

        for entry_result in iter {
            match entry_result {
                Ok(entry) => {
                    let key_bytes = entry.0.value();
                    if !key_bytes.starts_with(prefix) {
                        break;
                    }
                    let key_str = String::from_utf8_lossy(key_bytes);
                    let key = Key::new(key_str.to_string());
                    keys.push(key);
                }
                Err(e) => {
                    error!("Cache list_keys iteration error: error={}", e);
                    return Err(GuardianError::Store(format!(
                        "Cache list_keys error: {}",
                        e
                    )));
                }
            }
        }

        debug!("Cache list_keys completed: keys_count={}", keys.len());
        Ok(keys)
    }

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

/// Handle wrapper around Arc<RedbDatastore> to implement Datastore trait
/// since RedbDatastore itself is not Clone (redb::Database is not Clone).
#[derive(Clone)]
pub struct RedbDatastoreHandle {
    inner: Arc<RedbDatastore>,
}

impl RedbDatastoreHandle {
    /// Wraps a shared RedbDatastore in a cloneable handle.
    pub fn new(datastore: Arc<RedbDatastore>) -> Self {
        Self { inner: datastore }
    }

    /// Flushes (compacts) the underlying datastore.
    pub fn flush(&self) -> Result<()> {
        self.inner.flush()
    }
}

#[async_trait::async_trait]
impl Datastore for RedbDatastoreHandle {
    async fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
        self.inner.get(key).await
    }
    async fn put(&self, key: &[u8], value: &[u8]) -> Result<()> {
        self.inner.put(key, value).await
    }
    async fn has(&self, key: &[u8]) -> Result<bool> {
        self.inner.has(key).await
    }
    async fn delete(&self, key: &[u8]) -> Result<()> {
        self.inner.delete(key).await
    }
    async fn query(&self, query: &crate::data_store::Query) -> Result<crate::data_store::Results> {
        self.inner.query(query).await
    }
    async fn list_keys(&self, prefix: &[u8]) -> Result<Vec<crate::data_store::Key>> {
        self.inner.list_keys(prefix).await
    }
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

/// Factory function for creating cache instances.
pub fn create_cache(opts: Options) -> RedbCache {
    RedbCache::new(opts)
}

/// Creates a default cache with optimized settings.
pub fn create_default_cache() -> RedbCache {
    create_cache(Options::default())
}

/// Creates an in-memory cache for testing.
pub fn create_memory_cache() -> RedbCache {
    create_cache(Options {
        cache_mode: CacheMode::InMemory,
        ..Default::default()
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data_store::{Key, Order, Query};

    #[tokio::test]
    async fn test_redb_datastore_basic_operations() {
        let datastore = RedbDatastore::new(":memory:", Options::default()).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());
        assert_eq!(datastore.get(key).await.unwrap(), None);
    }

    #[tokio::test]
    async fn test_redb_datastore_query() {
        let datastore = RedbDatastore::new(":memory:", Options::default()).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"/users/charlie", b"charlie_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: None,
            order: Order::Asc,
            offset: None,
        };

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

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

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

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

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

    #[tokio::test]
    async fn test_redb_datastore_list_keys() {
        let datastore = RedbDatastore::new(":memory:", Options::default()).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 list_keys with prefix
        let keys = datastore.list_keys(b"/users").await.unwrap();
        assert_eq!(keys.len(), 2);

        let key_strings: Vec<String> = keys.iter().map(|k| k.as_str()).collect();
        assert!(key_strings.contains(&"/users/alice".to_string()));
        assert!(key_strings.contains(&"/users/bob".to_string()));
    }

    #[test]
    fn test_cache_mode_detection() {
        let persistent_opts = Options {
            cache_mode: CacheMode::Persistent,
            ..Default::default()
        };

        let memory_opts = Options {
            cache_mode: CacheMode::InMemory,
            ..Default::default()
        };

        assert_eq!(persistent_opts.cache_mode, CacheMode::Persistent);
        assert_eq!(memory_opts.cache_mode, CacheMode::InMemory);
    }
}