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
//! Database backend implementations for Netabase Store.
//!
//! This module contains implementations of various storage backends, each with different
//! performance characteristics and use cases.
//!
//! ## Available Backends
//!
//! ### Native Backends (for desktop/server applications)
//!
//! #### Redb (`redb_store`)
//! - **Best for**: Write-heavy workloads, ACID guarantees
//! - **Features**: MVCC transactions, excellent write performance, efficient storage
//! - **API Options**:
//! - Standard wrapper: Simple API with auto-commit per operation
//! - Bulk methods: `put_many()`, `get_many()`, `get_many_by_secondary_keys()`
//! - ZeroCopy: Explicit transaction management for maximum performance
//! - **Overhead**: 118-133% for bulk operations vs raw redb
//! - **Enable with**: `features = ["redb"]`
//!
//! Example:
//! ```no_run
//! use netabase_store::{netabase_definition_module, NetabaseModel, netabase};
//! use netabase_store::databases::redb_store::RedbStore;
//! use netabase_store::traits::batch::Batchable;
//!
//! #[netabase_definition_module(MyDef, MyKeys)]
//! mod models {
//! use netabase_store::{NetabaseModel, netabase};
//! #[derive(NetabaseModel, Clone, Debug,
//! bincode::Encode, bincode::Decode,
//! serde::Serialize, serde::Deserialize)]
//! #[netabase(MyDef)]
//! pub struct User { #[primary_key] pub id: u64, pub name: String }
//! }
//! use models::*;
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let store = RedbStore::<MyDef>::new("./database.redb")?;
//! let tree = store.open_tree::<User>();
//!
//! // Bulk insert - 8-9x faster than loop
//! let models = vec![User { id: 1, name: "Alice".into() }];
//! tree.put_many(models)?;
//! # Ok(())
//! # }
//! ```
//!
//! #### Redb ZeroCopy (`redb_zerocopy`)
//! - **Best for**: High-performance scenarios requiring explicit control
//! - **Features**: Direct transaction management, zero-copy reads where possible
//! - **Performance**: Up to 54x faster for secondary key queries
//! - **Complexity**: Requires manual transaction management
//! - **Enable with**: `features = ["redb", "redb-zerocopy"]`
//!
//! Example:
//! ```no_run
//! use netabase_store::{netabase_definition_module, NetabaseModel, netabase};
//! use netabase_store::databases::redb_zerocopy::{RedbStoreZeroCopy, with_write_transaction};
//!
//! #[netabase_definition_module(MyDef, MyKeys)]
//! mod models {
//! use netabase_store::{NetabaseModel, netabase};
//! #[derive(NetabaseModel, Clone, Debug,
//! bincode::Encode, bincode::Decode,
//! serde::Serialize, serde::Deserialize)]
//! #[netabase(MyDef)]
//! pub struct User { #[primary_key] pub id: u64, pub name: String }
//! }
//! use models::*;
//!
//! # fn example() -> Result<(), netabase_store::error::NetabaseError> {
//! let store = RedbStoreZeroCopy::<MyDef>::new("./database.redb")?;
//!
//! with_write_transaction(&store, |txn| {
//! let mut tree = txn.open_tree::<User>()?;
//! tree.put_many(vec![User { id: 1, name: "Alice".into() }])?;
//! Ok(())
//! })?;
//! # Ok(())
//! # }
//! ```
//!
//! #### Sled (`sled_store`)
//! - **Best for**: Read-heavy workloads
//! - **Features**: Battle-tested, very low read overhead (~20%)
//! - **Performance**: Excellent for read-heavy applications
//! - **Enable with**: `features = ["sled"]`
//!
//! Example:
//! ```no_run
//! use netabase_store::{netabase_definition_module, NetabaseModel, netabase};
//! use netabase_store::databases::sled_store::SledStore;
//!
//! #[netabase_definition_module(MyDef, MyKeys)]
//! mod models {
//! use netabase_store::{NetabaseModel, netabase};
//! #[derive(NetabaseModel, Clone, Debug,
//! bincode::Encode, bincode::Decode,
//! serde::Serialize, serde::Deserialize)]
//! #[netabase(MyDef)]
//! pub struct User { #[primary_key] pub id: u64, pub name: String }
//! }
//! use models::*;
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let store = SledStore::<MyDef>::new("./database")?;
//! let tree = store.open_tree::<User>();
//! # Ok(())
//! # }
//! ```
//!
//! ### WASM Backend (for browser applications)
//!
//! #### IndexedDB (`indexeddb_store`)
//! - **Best for**: Browser-based applications
//! - **Features**: Async API, browser-native storage
//! - **Note**: All operations are async
//! - **Enable with**: `features = ["wasm"]` on wasm32 targets
//!
//! Example (requires wasm32 target):
//! ```text
//! let store = IndexedDBStore::<MyDef>::new("my_db").await?;
//! let tree = store.open_tree::<User>();
//! tree.put(user).await?;
//! ```
//!
//! ## Performance Comparison
//!
//! | Backend | Insert (1000 items) | Get (1000 items) | Best For |
//! |---------|-------------------|------------------|----------|
//! | Redb (bulk) | 3.10 ms | 382 µs | Write-heavy |
//! | Sled | ~4 ms | ~305 µs | Read-heavy |
//! | ZeroCopy | 3.51 ms | 692 µs | High-performance |
//!
//! ## Choosing a Backend
//!
//! 1. **For production applications**:
//! - Write-heavy → `redb_store` with bulk methods
//! - Read-heavy → `sled_store`
//! - Need maximum performance → `redb_zerocopy`
//!
//! 2. **For browser applications**:
//! - Use `indexeddb_store` (only option for WASM)
//!
//! 3. **For testing**:
//! - Use temporary stores with `temp()` methods for fast, isolated tests
//!
//! ## Bulk Methods vs Transactions
//!
//! All native backends support bulk operations for better performance:
//!
//! ```text
//! // ❌ Slow: Creates 1000 transactions
//! for model in models {
//! tree.put(model)?;
//! }
//!
//! // ✅ Fast: Single transaction
//! tree.put_many(models)?; // 8-9x faster!
//! ```
//!
//! Available bulk methods:
//! - `put_many(Vec<M>)` - Bulk insert
//! - `get_many(Vec<M::Keys>)` - Bulk read
//! - `get_many_by_secondary_keys(Vec<SecondaryKey>)` - Bulk secondary queries
// libp2p RecordStore implementation module (native-only, requires mio/networking)