netabase_store 0.0.8

A type-safe, multi-backend key-value storage library for Rust with support for native (Sled, Redb) and WASM (IndexedDB) environments.
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
//! Unified store interface providing a single entry point for all storage backends.
//!
//! The `NetabaseStore<Backend>` wrapper provides a consistent API across different
//! storage backends (Sled, Redb, IndexedDB, Memory) while allowing backend-specific
//! functionality through specialized implementations.
//!
//! # Examples
//!
//! ```no_run
//! use netabase_store::{NetabaseStore, netabase_definition_module, NetabaseModel};
//! use netabase_store::traits::tree::NetabaseTreeSync;
//! use netabase_store::traits::model::NetabaseModelTrait;
//!
//! #[netabase_definition_module(MyDef, MyKeys)]
//! mod models {
//!     use netabase_store::{NetabaseModel, netabase};
//!     #[derive(NetabaseModel, Clone, Debug, PartialEq,
//!              bincode::Encode, bincode::Decode,
//!              serde::Serialize, serde::Deserialize)]
//!     #[netabase(MyDef)]
//!     pub struct User {
//!         #[primary_key]
//!         pub id: u64,
//!         pub name: String,
//!     }
//! }
//! use models::*;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a Sled-backed store
//! let temp_dir = tempfile::tempdir()?;
//! let store = NetabaseStore::sled(temp_dir.path().join("data"))?;
//! let tree = store.open_tree::<User>();
//! let user = User { id: 1, name: "Alice".to_string() };
//! tree.put(user)?;
//!
//! // Create a Redb-backed store
//! let store = NetabaseStore::redb(temp_dir.path().join("data.redb"))?;
//! let tree = store.open_tree::<User>();
//! # Ok(())
//! # }
//! ```

use crate::error::NetabaseError;
use crate::traits::definition::NetabaseDefinitionTrait;
use crate::traits::model::NetabaseModelTrait;
use crate::traits::store_ops::OpenTree;
use std::marker::PhantomData;
use std::path::Path;

/// Marker trait for backends that store a specific Definition type.
///
/// This trait is automatically implemented for all backend stores and binds
/// the Definition type to the backend at compile time.
pub trait BackendFor<D: NetabaseDefinitionTrait> {}

/// Trait for backends that can be constructed from a path.
///
/// This enables the generic `NetabaseStore::new()` constructor.
pub trait BackendConstructor<D: NetabaseDefinitionTrait>: BackendFor<D> + Sized {
    /// Create a new backend instance from a path.
    fn new_backend<P: AsRef<Path>>(path: P) -> Result<Self, NetabaseError>;
}

// Blanket implementation for all backend types that match the pattern
#[cfg(feature = "sled")]
impl<D> BackendFor<D> for crate::databases::sled_store::SledStore<D> where D: NetabaseDefinitionTrait
{}

#[cfg(feature = "sled")]
impl<D> BackendConstructor<D> for crate::databases::sled_store::SledStore<D>
where
    D: NetabaseDefinitionTrait + crate::traits::convert::ToIVec,
    <D as strum::IntoDiscriminant>::Discriminant: crate::traits::definition::NetabaseDiscriminant,
{
    fn new_backend<P: AsRef<Path>>(path: P) -> Result<Self, NetabaseError> {
        crate::databases::sled_store::SledStore::new(path)
    }
}

#[cfg(feature = "redb")]
impl<D> BackendFor<D> for crate::databases::redb_store::RedbStore<D> where D: NetabaseDefinitionTrait
{}

#[cfg(feature = "redb")]
impl<D> BackendConstructor<D> for crate::databases::redb_store::RedbStore<D>
where
    D: NetabaseDefinitionTrait + crate::traits::convert::ToIVec,
    <D as strum::IntoDiscriminant>::Discriminant: crate::traits::definition::NetabaseDiscriminant,
{
    fn new_backend<P: AsRef<Path>>(path: P) -> Result<Self, NetabaseError> {
        crate::databases::redb_store::RedbStore::new(path)
    }
}

/// Unified store wrapper providing a consistent API across all storage backends.
///
/// This generic wrapper allows you to write backend-agnostic code while still
/// having access to backend-specific features when needed.
///
/// # Type Parameters
///
/// * `D` - The NetabaseDefinition type that defines all models for this store
/// * `Backend` - The underlying storage backend (SledStore, RedbStore, etc.)
pub struct NetabaseStore<D, Backend>
where
    D: NetabaseDefinitionTrait,
    Backend: BackendFor<D>,
{
    backend: Backend,
    _phantom: PhantomData<D>,
}

impl<D, Backend> NetabaseStore<D, Backend>
where
    D: NetabaseDefinitionTrait,
    Backend: BackendFor<D>,
{
    /// Create a new store from an existing backend instance.
    ///
    /// This is useful when you need to configure the backend with specific options
    /// before wrapping it in a NetabaseStore.
    pub fn from_backend(backend: Backend) -> Self {
        Self {
            backend,
            _phantom: PhantomData,
        }
    }
}

// Generic new() constructor for backends that support it
impl<D, Backend> NetabaseStore<D, Backend>
where
    D: NetabaseDefinitionTrait,
    Backend: BackendConstructor<D>,
{
    /// Create a new store with the specified backend at the given path.
    ///
    /// This generic constructor works with any backend that implements `BackendConstructor`.
    /// It's more flexible than backend-specific constructors like `sled()` or `redb()`.
    ///
    /// # Type Parameters
    ///
    /// You must specify both the Definition and Backend types using turbofish syntax:
    /// ```no_run
    /// # use netabase_store::{NetabaseStore, netabase_definition_module};
    /// # use netabase_store::databases::sled_store::SledStore;
    /// # use netabase_store::databases::redb_store::RedbStore;
    /// # #[netabase_definition_module(MyDef, MyKeys)]
    /// # mod models {
    /// #     use netabase_store::{NetabaseModel, netabase};
    /// #     #[derive(NetabaseModel, Clone, Debug, PartialEq,
    /// #              bincode::Encode, bincode::Decode,
    /// #              serde::Serialize, serde::Deserialize)]
    /// #     #[netabase(MyDef)]
    /// #     pub struct User {
    /// #         #[primary_key]
    /// #         pub id: u64,
    /// #         pub name: String,
    /// #     }
    /// # }
    /// # use models::*;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let store = NetabaseStore::<MyDef, SledStore<MyDef>>::new("./db")?;
    /// let store = NetabaseStore::<MyDef, RedbStore<MyDef>>::new("./db.redb")?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the database file or directory
    ///
    /// # Examples
    ///
    /// ## Basic Usage with Sled Backend
    ///
    /// ```rust
    /// use netabase_store::{NetabaseStore, netabase_definition_module, NetabaseModel};
    /// use netabase_store::databases::sled_store::SledStore;
    /// use netabase_store::traits::tree::NetabaseTreeSync;
    /// use netabase_store::traits::model::NetabaseModelTrait;
    ///
    /// #[netabase_definition_module(MyDef, MyKeys)]
    /// mod my_models {
    ///     use netabase_store::{NetabaseModel, netabase};
    ///     #[derive(NetabaseModel, Clone, Debug, PartialEq,
    ///              bincode::Encode, bincode::Decode,
    ///              serde::Serialize, serde::Deserialize)]
    ///     #[netabase(MyDef)]
    ///     pub struct User {
    ///         #[primary_key]
    ///         pub id: u64,
    ///         pub name: String,
    ///     }
    /// }
    /// use my_models::*;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// // Create store using generic constructor
    /// let temp_dir = tempfile::tempdir()?;
    /// let store = NetabaseStore::<MyDef, SledStore<MyDef>>::new(
    ///     temp_dir.path().join("test.db")
    /// )?;
    ///
    /// // Use the store normally
    /// let tree = store.open_tree::<User>();
    /// let user = User { id: 1, name: "Alice".to_string() };
    /// tree.put(user.clone())?;
    ///
    /// let retrieved = tree.get(user.primary_key())?.unwrap();
    /// assert_eq!(retrieved, user);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// ## Using with Redb Backend
    ///
    /// ```rust
    /// use netabase_store::{NetabaseStore, netabase_definition_module, NetabaseModel};
    /// use netabase_store::databases::redb_store::RedbStore;
    /// use netabase_store::traits::tree::NetabaseTreeSync;
    /// use netabase_store::traits::model::NetabaseModelTrait;
    ///
    /// #[netabase_definition_module(MyDef, MyKeys)]
    /// mod my_models {
    ///     use netabase_store::{NetabaseModel, netabase};
    ///     #[derive(NetabaseModel, Clone, Debug, PartialEq,
    ///              bincode::Encode, bincode::Decode,
    ///              serde::Serialize, serde::Deserialize)]
    ///     #[netabase(MyDef)]
    ///     pub struct Post {
    ///         #[primary_key]
    ///         pub id: String,
    ///         pub title: String,
    ///     }
    /// }
    /// use my_models::*;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// // Create Redb store using generic constructor
    /// let temp_dir = tempfile::tempdir()?;
    /// let store = NetabaseStore::<MyDef, RedbStore<MyDef>>::new(
    ///     temp_dir.path().join("test.redb")
    /// )?;
    ///
    /// let tree = store.open_tree::<Post>();
    /// let post = Post { id: "post-1".to_string(), title: "Hello".to_string() };
    /// tree.put(post.clone())?;
    ///
    /// let retrieved = tree.get(PostKey::Primary(post.primary_key()))?.unwrap();
    /// assert_eq!(retrieved, post);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// ## Generic Function Example
    ///
    /// The generic constructor is useful for writing backend-agnostic code:
    ///
    /// ```rust
    /// use netabase_store::{NetabaseStore, netabase_definition_module, NetabaseModel};
    /// use netabase_store::store::BackendConstructor;
    /// use netabase_store::traits::definition::NetabaseDefinitionTrait;
    /// use netabase_store::traits::tree::NetabaseTreeSync;
    /// use netabase_store::traits::model::NetabaseModelTrait;
    ///
    /// #[netabase_definition_module(MyDef, MyKeys)]
    /// mod my_models {
    ///     use netabase_store::{NetabaseModel, netabase};
    ///     #[derive(NetabaseModel, Clone, Debug, PartialEq,
    ///              bincode::Encode, bincode::Decode,
    ///              serde::Serialize, serde::Deserialize)]
    ///     #[netabase(MyDef)]
    ///     pub struct Item {
    ///         #[primary_key]
    ///         pub id: u64,
    ///         pub data: String,
    ///     }
    /// }
    /// use my_models::*;
    ///
    /// // Example using generic new() method with concrete backend
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use netabase_store::databases::sled_store::SledStore;
    /// let temp_dir = tempfile::tempdir()?;
    /// let store = NetabaseStore::<MyDef, SledStore<MyDef>>::new(
    ///     temp_dir.path().join("test.db")
    /// )?;
    ///
    /// let tree = store.open_tree::<Item>();
    /// let item = Item { id: 1, data: "test".to_string() };
    /// tree.put(item.clone())?;
    ///
    /// let retrieved = tree.get(item.primary_key())?.unwrap();
    /// assert_eq!(retrieved, item);
    /// # Ok(())
    /// # }
    /// ```
    pub fn new<P: AsRef<Path>>(path: P) -> Result<Self, NetabaseError> {
        Ok(Self::from_backend(Backend::new_backend(path)?))
    }
}

impl<D, Backend> NetabaseStore<D, Backend>
where
    D: NetabaseDefinitionTrait,
    Backend: BackendFor<D>,
{
    /// Get a reference to the underlying backend.
    ///
    /// This allows access to backend-specific methods and configuration.
    pub fn backend(&self) -> &Backend {
        &self.backend
    }

    /// Get a mutable reference to the underlying backend.
    ///
    /// This allows access to backend-specific mutable methods.
    pub fn backend_mut(&mut self) -> &mut Backend {
        &mut self.backend
    }

    /// Consume the store and return the underlying backend.
    pub fn into_backend(self) -> Backend {
        self.backend
    }
}

// Implement OpenTree for any backend that implements it
impl<D, M, Backend> OpenTree<D, M> for NetabaseStore<D, Backend>
where
    D: NetabaseDefinitionTrait,
    M: NetabaseModelTrait<D>,
    <D as strum::IntoDiscriminant>::Discriminant: crate::traits::definition::NetabaseDiscriminant,
    Backend: BackendFor<D> + OpenTree<D, M>,
{
    type Tree<'a>
        = Backend::Tree<'a>
    where
        Self: 'a;

    fn open_tree(&self) -> Self::Tree<'_> {
        self.backend.open_tree()
    }
}

// Generic open_tree method for all backends
impl<D, Backend> NetabaseStore<D, Backend>
where
    D: NetabaseDefinitionTrait,
    Backend: BackendFor<D>,
{
    /// Open a tree for a specific model type.
    ///
    /// This is a generic method that works with any backend.
    ///
    /// # Type Parameters
    ///
    /// * `M` - The model type to open a tree for
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use netabase_store::{NetabaseStore, netabase_definition_module};
    /// # #[netabase_definition_module(MyDefinition, MyKeys)]
    /// # mod models {
    /// #     use netabase_store::{NetabaseModel, netabase};
    /// #     #[derive(NetabaseModel, Clone, Debug, PartialEq,
    /// #              bincode::Encode, bincode::Decode,
    /// #              serde::Serialize, serde::Deserialize)]
    /// #     #[netabase(MyDefinition)]
    /// #     pub struct User {
    /// #         #[primary_key]
    /// #         pub id: u64,
    /// #         pub name: String,
    /// #     }
    /// # }
    /// # use models::*;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let store = NetabaseStore::sled("./db")?;
    /// let user_tree = store.open_tree::<User>();
    /// # Ok(())
    /// # }
    /// ```
    #[inline]
    pub fn open_tree<M>(&self) -> Backend::Tree<'_>
    where
        M: crate::traits::model::NetabaseModelTrait<D>,
        Backend: crate::traits::store_ops::OpenTree<D, M>,
    {
        use crate::traits::store_ops::OpenTree;
        OpenTree::open_tree(self)
    }
}

// Convenience constructors for each backend type
#[cfg(feature = "sled")]
impl<D> NetabaseStore<D, crate::databases::sled_store::SledStore<D>>
where
    D: NetabaseDefinitionTrait + crate::traits::convert::ToIVec,
    <D as strum::IntoDiscriminant>::Discriminant: crate::traits::definition::NetabaseDiscriminant,
{
    /// Create a new Sled-backed store at the given path.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use netabase_store::{NetabaseStore, netabase_definition_module};
    /// # #[netabase_definition_module(MyDefinition, MyKeys)]
    /// # mod models {
    /// #     use netabase_store::{NetabaseModel, netabase};
    /// #     #[derive(NetabaseModel, Clone, Debug, PartialEq,
    /// #              bincode::Encode, bincode::Decode,
    /// #              serde::Serialize, serde::Deserialize)]
    /// #     #[netabase(MyDefinition)]
    /// #     pub struct User {
    /// #         #[primary_key]
    /// #         pub id: u64,
    /// #     }
    /// # }
    /// # use models::*;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let store = NetabaseStore::<MyDefinition, _>::sled("./my_database")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn sled<P: AsRef<Path>>(path: P) -> Result<Self, NetabaseError> {
        Ok(Self::from_backend(
            crate::databases::sled_store::SledStore::new(path)?,
        ))
    }

    /// Create a temporary Sled-backed store (Sled-specific).
    ///
    /// The database will be deleted when the store is dropped.
    /// This is useful for testing and temporary storage.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use netabase_store::{NetabaseStore, netabase_definition_module};
    /// # #[netabase_definition_module(MyDefinition, MyKeys)]
    /// # mod models {
    /// #     use netabase_store::{NetabaseModel, netabase};
    /// #     #[derive(NetabaseModel, Clone, Debug, PartialEq,
    /// #              bincode::Encode, bincode::Decode,
    /// #              serde::Serialize, serde::Deserialize)]
    /// #     #[netabase(MyDefinition)]
    /// #     pub struct User {
    /// #         #[primary_key]
    /// #         pub id: u64,
    /// #     }
    /// # }
    /// # use models::*;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let store = NetabaseStore::<MyDefinition, _>::temp()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn temp() -> Result<Self, NetabaseError> {
        Ok(Self::from_backend(
            crate::databases::sled_store::SledStore::temp()?,
        ))
    }
}

#[cfg(feature = "redb")]
impl<D> NetabaseStore<D, crate::databases::redb_store::RedbStore<D>>
where
    D: NetabaseDefinitionTrait + crate::traits::convert::ToIVec,
    <D as strum::IntoDiscriminant>::Discriminant: crate::traits::definition::NetabaseDiscriminant,
{
    /// Create a new Redb-backed store at the given path.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use netabase_store::{NetabaseStore, netabase_definition_module};
    /// # #[netabase_definition_module(MyDefinition, MyKeys)]
    /// # mod models {
    /// #     use netabase_store::{NetabaseModel, netabase};
    /// #     #[derive(NetabaseModel, Clone, Debug, PartialEq,
    /// #              bincode::Encode, bincode::Decode,
    /// #              serde::Serialize, serde::Deserialize)]
    /// #     #[netabase(MyDefinition)]
    /// #     pub struct User {
    /// #         #[primary_key]
    /// #         pub id: u64,
    /// #     }
    /// # }
    /// # use models::*;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let store = NetabaseStore::<MyDefinition, _>::redb("./my_database.redb")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn redb<P: AsRef<Path>>(path: P) -> Result<Self, NetabaseError> {
        Ok(Self::from_backend(
            crate::databases::redb_store::RedbStore::new(path)?,
        ))
    }

    /// Open an existing Redb-backed store at the given path.
    pub fn open_redb<P: AsRef<Path>>(path: P) -> Result<Self, NetabaseError> {
        Ok(Self::from_backend(
            crate::databases::redb_store::RedbStore::open(path)?,
        ))
    }
}

// Backend-specific implementations

#[cfg(feature = "sled")]
impl<D> NetabaseStore<D, crate::databases::sled_store::SledStore<D>>
where
    D: NetabaseDefinitionTrait + crate::traits::convert::ToIVec,
    <D as strum::IntoDiscriminant>::Discriminant: crate::traits::definition::NetabaseDiscriminant,
{
    /// Get direct access to the underlying sled database (Sled-specific).
    pub fn db(&self) -> &sled::Db {
        self.backend.db()
    }

    /// Flush the database to disk (Sled-specific).
    ///
    /// This ensures all pending writes are persisted to disk.
    pub fn flush(&self) -> Result<usize, NetabaseError> {
        Ok(self.backend.db().flush()?)
    }

    /// Get the size of the database on disk in bytes (Sled-specific).
    pub fn size_on_disk(&self) -> Result<u64, NetabaseError> {
        Ok(self.backend.db().size_on_disk()?)
    }

    /// Check if the database was recovered from a previous run (Sled-specific).
    pub fn was_recovered(&self) -> bool {
        self.backend.db().was_recovered()
    }

    /// Generate a monotonic ID (Sled-specific).
    ///
    /// This is useful for generating unique IDs without coordination.
    pub fn generate_id(&self) -> Result<u64, NetabaseError> {
        Ok(self.backend.db().generate_id()?)
    }

    /// Begin a read-only transaction.
    ///
    /// Read-only transactions allow multiple concurrent reads without blocking.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use netabase_store::{NetabaseStore, netabase_definition_module};
    /// # #[netabase_definition_module(MyDefinition, MyKeys)]
    /// # mod models {
    /// #     use netabase_store::{NetabaseModel, netabase};
    /// #     #[derive(NetabaseModel, Clone, Debug, PartialEq,
    /// #              bincode::Encode, bincode::Decode,
    /// #              serde::Serialize, serde::Deserialize)]
    /// #     #[netabase(MyDefinition)]
    /// #     pub struct User {
    /// #         #[primary_key]
    /// #         pub id: u64,
    /// #         pub name: String,
    /// #     }
    /// # }
    /// # use models::*;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let store = NetabaseStore::<MyDefinition, _>::sled("./db")?;
    /// let mut txn = store.read();
    /// let tree = txn.open_tree::<User>();
    /// let user = tree.get(UserPrimaryKey(1))?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn read(&self) -> crate::transaction::TxnGuard<'_, D, crate::transaction::ReadOnly> {
        crate::transaction::TxnGuard::read_sled(self.backend.db())
    }

    /// Begin a read-write transaction.
    ///
    /// Read-write transactions allow multiple operations to be batched together
    /// and committed atomically.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use netabase_store::{NetabaseStore, netabase_definition_module};
    /// # #[netabase_definition_module(MyDefinition, MyKeys)]
    /// # mod models {
    /// #     use netabase_store::{NetabaseModel, netabase};
    /// #     #[derive(NetabaseModel, Clone, Debug, PartialEq,
    /// #              bincode::Encode, bincode::Decode,
    /// #              serde::Serialize, serde::Deserialize)]
    /// #     #[netabase(MyDefinition)]
    /// #     pub struct User {
    /// #         #[primary_key]
    /// #         pub id: u64,
    /// #         pub name: String,
    /// #     }
    /// # }
    /// # use models::*;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let store = NetabaseStore::sled("./db")?;
    /// # let user = User { id: 1, name: "Alice".to_string() };
    /// let mut txn = store.write();
    /// let mut tree = txn.open_tree::<User>();
    /// tree.put(user)?;
    /// txn.commit()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn write(&self) -> crate::transaction::TxnGuard<'_, D, crate::transaction::ReadWrite> {
        crate::transaction::TxnGuard::write_sled(self.backend.db())
    }

    /// Execute a transaction on a single model tree (Sled-specific).
    ///
    /// This is Sled's native transaction API that provides ACID guarantees for operations
    /// on a single model type. The transaction closure may be called multiple times if
    /// there are conflicts.
    ///
    /// # Type Parameters
    ///
    /// * `M` - The model type to operate on
    /// * `F` - The transaction closure
    /// * `R` - The return type
    ///
    /// # Arguments
    ///
    /// * `f` - Transaction closure that performs operations on the transactional tree
    ///
    /// # Returns
    ///
    /// * `Ok(R)` - Transaction succeeded, returns result from closure
    /// * `Err(NetabaseError)` - Transaction failed (conflict, I/O error, etc.)
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use netabase_store::{NetabaseStore, netabase_definition_module};
    /// # #[netabase_definition_module(MyDefinition, MyKeys)]
    /// # mod models {
    /// #     use netabase_store::{NetabaseModel, netabase};
    /// #     #[derive(NetabaseModel, Clone, Debug, PartialEq,
    /// #              bincode::Encode, bincode::Decode,
    /// #              serde::Serialize, serde::Deserialize)]
    /// #     #[netabase(MyDefinition)]
    /// #     pub struct Account {
    /// #         #[primary_key]
    /// #         pub id: u64,
    /// #         pub balance: i64,
    /// #     }
    /// # }
    /// # use models::*;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let store = NetabaseStore::sled("./db")?;
    /// // Atomic transfer between accounts
    /// store.transaction::<Account, _, _>(|txn_tree| {
    ///     let mut from = txn_tree.get(AccountPrimaryKey(1))?.unwrap();
    ///     let mut to = txn_tree.get(AccountPrimaryKey(2))?.unwrap();
    ///
    ///     from.balance -= 100;
    ///     to.balance += 100;
    ///
    ///     txn_tree.put(from)?;
    ///     txn_tree.put(to)?;
    ///
    ///     Ok(())
    /// })?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn transaction<M, F, R>(&self, f: F) -> Result<R, NetabaseError>
    where
        M: NetabaseModelTrait<D> + TryFrom<D> + Into<D>,
        D: TryFrom<M>,
        F: Fn(
            &crate::databases::sled_store::SledTransactionalTree<D, M>,
        ) -> Result<R, Box<dyn std::error::Error>>,
    {
        self.backend.transaction(f)
    }
}

#[cfg(feature = "redb")]
impl<D> NetabaseStore<D, crate::databases::redb_store::RedbStore<D>>
where
    D: NetabaseDefinitionTrait + crate::traits::convert::ToIVec,
    <D as strum::IntoDiscriminant>::Discriminant: crate::traits::definition::NetabaseDiscriminant,
{
    /// Check database integrity (Redb-specific).
    ///
    /// Returns `true` if the database is consistent.
    pub fn check_integrity(&mut self) -> Result<bool, NetabaseError> {
        self.backend.check_integrity()
    }

    /// Compact the database to reclaim space (Redb-specific).
    ///
    /// Returns `true` if compaction was successful.
    pub fn compact(&mut self) -> Result<bool, NetabaseError> {
        self.backend.compact()
    }

    /// Get direct access to the underlying redb database (Redb-specific).
    pub fn db(&self) -> &redb::Database {
        self.backend.db()
    }

    /// Get all tree names (discriminants) in the database (Redb-specific).
    pub fn tree_names(&self) -> Vec<D::Discriminant> {
        self.backend.tree_names()
    }

    // TODO: Transaction API for Redb is still being optimized
    // The Sled backend has a working transaction API - see sled implementation above
}

// Zero-copy Redb backend support
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<D> BackendFor<D> for crate::databases::redb_zerocopy::RedbStoreZeroCopy<D> where
    D: NetabaseDefinitionTrait
{
}

#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<D> BackendConstructor<D> for crate::databases::redb_zerocopy::RedbStoreZeroCopy<D>
where
    D: NetabaseDefinitionTrait,
{
    fn new_backend<P: AsRef<Path>>(path: P) -> Result<Self, NetabaseError> {
        crate::databases::redb_zerocopy::RedbStoreZeroCopy::new(path)
    }
}

#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<D> NetabaseStore<D, crate::databases::redb_zerocopy::RedbStoreZeroCopy<D>>
where
    D: NetabaseDefinitionTrait,
{
    /// Create a new zero-copy Redb-backed store at the given path.
    ///
    /// This constructor creates a store using the high-performance zero-copy redb backend.
    /// The zero-copy backend provides:
    /// - **Transaction batching**: Explicit write/read transactions for bulk operations
    /// - **Zero-copy reads**: Borrow data directly from database pages without allocation
    /// - **Performance**: Up to 10x faster bulk inserts, up to 54x faster secondary key queries
    ///
    /// # When to Use
    ///
    /// Use this backend when:
    /// - You need transaction batching (bulk operations)
    /// - Performance is critical
    /// - You want explicit transaction control
    ///
    /// Use the regular `redb()` constructor when:
    /// - Simplicity is more important than performance
    /// - Single-operation transactions are fine
    /// - You want the simplest possible API
    ///
    /// # Examples
    ///
    /// ```rust
    /// use netabase_store::{NetabaseStore, netabase_definition_module, NetabaseModel};
    /// use netabase_store::traits::model::NetabaseModelTrait;
    ///
    /// #[netabase_definition_module(MyDef, MyKeys)]
    /// mod models {
    ///     use netabase_store::{NetabaseModel, netabase};
    ///     #[derive(NetabaseModel, Clone, Debug, PartialEq,
    ///              bincode::Encode, bincode::Decode,
    ///              serde::Serialize, serde::Deserialize)]
    ///     #[netabase(MyDef)]
    ///     pub struct User {
    ///         #[primary_key]
    ///         pub id: u64,
    ///         pub name: String,
    ///         #[secondary_key]
    ///         pub email: String,
    ///     }
    /// }
    /// use models::*;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let temp_dir = tempfile::tempdir()?;
    /// let store = NetabaseStore::redb_zerocopy(temp_dir.path().join("app.redb"))?;
    ///
    /// // Write transaction - batched operations
    /// let mut txn = store.begin_write()?;
    /// let mut tree = txn.open_tree::<User>()?;
    /// tree.put(User { id: 1, name: "Alice".into(), email: "alice@example.com".into() })?;
    /// tree.put(User { id: 2, name: "Bob".into(), email: "bob@example.com".into() })?;
    /// drop(tree);
    /// txn.commit()?;  // Both inserts in one transaction
    ///
    /// // Read transaction - efficient reads
    /// let txn = store.begin_read()?;
    /// let tree = txn.open_tree::<User>()?;
    /// let user = tree.get(&UserPrimaryKey(1))?.unwrap();
    /// assert_eq!(user.name, "Alice");
    /// # Ok(())
    /// # }
    /// ```
    pub fn redb_zerocopy<P: AsRef<Path>>(path: P) -> Result<Self, NetabaseError> {
        Ok(Self::from_backend(
            crate::databases::redb_zerocopy::RedbStoreZeroCopy::new(path)?,
        ))
    }

    /// Open an existing zero-copy Redb-backed store at the given path.
    ///
    /// Unlike `redb_zerocopy()` which removes any existing database,
    /// this method opens an existing database or creates it if it doesn't exist.
    pub fn open_redb_zerocopy<P: AsRef<Path>>(path: P) -> Result<Self, NetabaseError> {
        Ok(Self::from_backend(
            crate::databases::redb_zerocopy::RedbStoreZeroCopy::open(path)?,
        ))
    }

    /// Begin a write transaction (zero-copy redb specific).
    ///
    /// Write transactions are exclusive - only one can be active at a time.
    /// The transaction must be explicitly committed or aborted.
    ///
    /// Multiple operations can be batched in a single transaction for better performance.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use netabase_store::{NetabaseStore, netabase_definition_module, NetabaseModel};
    /// # #[netabase_definition_module(MyDef, MyKeys)]
    /// # mod models {
    /// #     use netabase_store::{NetabaseModel, netabase};
    /// #     #[derive(NetabaseModel, Clone, Debug, PartialEq,
    /// #              bincode::Encode, bincode::Decode,
    /// #              serde::Serialize, serde::Deserialize)]
    /// #     #[netabase(MyDef)]
    /// #     pub struct User {
    /// #         #[primary_key]
    /// #         pub id: u64,
    /// #         pub name: String,
    /// #     }
    /// # }
    /// # use models::*;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let temp_dir = tempfile::tempdir()?;
    /// # let store = NetabaseStore::redb_zerocopy(temp_dir.path().join("test.redb"))?;
    /// // Batch multiple writes in one transaction
    /// let mut txn = store.begin_write()?;
    /// let mut tree = txn.open_tree::<User>()?;
    /// for i in 0..1000 {
    ///     tree.put(User { id: i, name: format!("User {}", i) })?;
    /// }
    /// drop(tree);
    /// txn.commit()?;  // All 1000 inserts in one transaction!
    /// # Ok(())
    /// # }
    /// ```
    pub fn begin_write(
        &self,
    ) -> Result<crate::databases::redb_zerocopy::RedbWriteTransactionZC<'_, D>, NetabaseError> {
        self.backend.begin_write()
    }

    /// Begin a read transaction (zero-copy redb specific).
    ///
    /// Read transactions provide a consistent snapshot of the database.
    /// Multiple read transactions can be active concurrently.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use netabase_store::{NetabaseStore, netabase_definition_module, NetabaseModel};
    /// # #[netabase_definition_module(MyDef, MyKeys)]
    /// # mod models {
    /// #     use netabase_store::{NetabaseModel, netabase};
    /// #     #[derive(NetabaseModel, Clone, Debug, PartialEq,
    /// #              bincode::Encode, bincode::Decode,
    /// #              serde::Serialize, serde::Deserialize)]
    /// #     #[netabase(MyDef)]
    /// #     pub struct User {
    /// #         #[primary_key]
    /// #         pub id: u64,
    /// #         pub name: String,
    /// #     }
    /// # }
    /// # use models::*;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let temp_dir = tempfile::tempdir()?;
    /// # let store = NetabaseStore::redb_zerocopy(temp_dir.path().join("test.redb"))?;
    /// # let mut txn = store.begin_write()?;
    /// # let mut tree = txn.open_tree::<User>()?;
    /// # tree.put(User { id: 1, name: "Alice".into() })?;
    /// # drop(tree); txn.commit()?;
    /// let txn = store.begin_read()?;
    /// let tree = txn.open_tree::<User>()?;
    /// let user = tree.get(&UserPrimaryKey(1))?;
    /// assert!(user.is_some());
    /// # Ok(())
    /// # }
    /// ```
    pub fn begin_read(
        &self,
    ) -> Result<crate::databases::redb_zerocopy::RedbReadTransactionZC<'_, D>, NetabaseError> {
        self.backend.begin_read()
    }

    /// Insert a single model with auto-commit (convenience method).
    ///
    /// This is equivalent to `begin_write() -> open_tree() -> put() -> commit()`.
    /// Use `begin_write()` directly for better performance when inserting multiple items.
    pub fn quick_put<M>(&self, model: M) -> Result<(), NetabaseError>
    where
        M: NetabaseModelTrait<D>,
        M::Keys: crate::traits::model::NetabaseModelTraitKey<D>,
    {
        self.backend.quick_put(model)
    }

    /// Get a single model (cloned) with auto-transaction (convenience method).
    ///
    /// This is equivalent to `begin_read() -> open_tree() -> get()`.
    /// Use `begin_read()` directly for better performance when reading multiple items.
    pub fn quick_get<M>(
        &self,
        key: &<M::Keys as crate::traits::model::NetabaseModelTraitKey<D>>::PrimaryKey,
    ) -> Result<Option<M>, NetabaseError>
    where
        M: NetabaseModelTrait<D>,
        M::Keys: crate::traits::model::NetabaseModelTraitKey<D>,
    {
        self.backend.quick_get(key)
    }

    /// Remove a single model with auto-commit (convenience method).
    ///
    /// This is equivalent to `begin_write() -> open_tree() -> remove() -> commit()`.
    /// Use `begin_write()` directly for better performance when removing multiple items.
    pub fn quick_remove<M>(
        &self,
        key: &<M::Keys as crate::traits::model::NetabaseModelTraitKey<D>>::PrimaryKey,
    ) -> Result<Option<M>, NetabaseError>
    where
        M: NetabaseModelTrait<D>,
        M::Keys: crate::traits::model::NetabaseModelTraitKey<D>,
    {
        self.backend.quick_remove(key)
    }
}