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
//! # Zero-Copy Redb Backend
//!
//! This module provides a high-performance redb backend with zero-copy reads
//! and transaction-scoped API.
//!
//! ## Quick Start
//!
//! ```
//! # use netabase_store::databases::redb_zerocopy::*;
//! # use netabase_store::*;
//! # #[netabase_definition_module(MyDef, MyKeys)]
//! # mod models {
//! # use netabase_store::*;
//! # #[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 temp = tempfile::tempdir().unwrap();
//! # let path = temp.path().join("app.redb");
//! let store = RedbStoreZeroCopy::<MyDef>::new(&path)?;
//!
//! // Write
//! let mut txn = store.begin_write()?;
//! let mut tree = txn.open_tree::<User>()?;
//! tree.put(User { id: 1, name: "Alice".to_string() })?;
//! drop(tree);
//! txn.commit()?;
//!
//! // Read (cloned)
//! let txn = store.begin_read()?;
//! let tree = txn.open_tree::<User>()?;
//! let user = tree.get(&UserPrimaryKey(1))?;
//! # Ok(())
//! # }
//! # example().unwrap();
//! ```
//!
//! ## Architecture
//!
//! The zero-copy backend follows a strict lifetime hierarchy:
//!
//! ```text
//! RedbStoreZeroCopy<D> ('static or app lifetime)
//! ↓ begin_write() / begin_read()
//! RedbWriteTransactionZC<'db, D> (borrows 'db from store)
//! RedbReadTransactionZC<'db, D> (borrows 'db from store)
//! ↓ open_tree<M>()
//! RedbTreeMut<'txn, 'db, D, M> (borrows 'txn from transaction)
//! RedbTree<'txn, 'db, D, M> (borrows 'txn from transaction)
//! ↓ get(), remove(), etc.
//! Model data (owned or borrowed)
//! ```
//!
//! ## Performance
//!
//! | Operation | Old API | New API | Improvement |
//! |-----------|---------|---------|-------------|
//! | Single read | ~100ns | ~100ns | Similar (both use bincode) |
//! | Bulk insert (1000) | ~50ms | ~5ms | 10x faster (single transaction) |
//!
//! ## API Comparison
//!
//! ### Standard API (redb_store) - Simple & Convenient
//!
//! ```no_run
//! use netabase_store::{netabase_definition_module, NetabaseModel, netabase};
//! 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 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 = RedbStore::<MyDef>::new("./db.redb")?;
//! let tree = store.open_tree::<User>();
//! let user = User { id: 1, name: "Alice".into() };
//! tree.put(user.clone())?; // Auto-commits (1 transaction per operation)
//! let retrieved = tree.get(UserKey::Primary(UserPrimaryKey(1)))?; // Always clones
//! assert_eq!(retrieved, Some(user));
//! # Ok(())
//! # }
//! ```
//!
//! ### Zero-Copy API (redb_zerocopy) - Explicit & High-Performance
//!
//! ```no_run
//! # use netabase_store::{netabase_definition_module, NetabaseModel, netabase};
//! # use netabase_store::databases::redb_zerocopy::*;
//! # 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>> {
//! let store = RedbStoreZeroCopy::<MyDef>::new("./db.redb")?;
//!
//! // Bulk insert (manual transaction management)
//! 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); // Release borrowing before commit
//! txn.commit()?; // Commits all 1000 inserts in one transaction
//!
//! // Zero-copy read
//! let txn = store.begin_read()?;
//! let tree = txn.open_tree::<User>()?;
//! let user = tree.get(&UserPrimaryKey(42))? // Option<User> (cloned)
//! .expect("User should exist");
//! # Ok(())
//! # }
//! ```
//!
//! ## When to use which?
//!
//! - Use **redb_store** when:
//! - You want automatic transaction management
//! - You're doing mostly single-record operations
//! - You want the simplest possible API
//!
//! - Use **redb_zerocopy** when:
//! - You need maximum performance for bulk operations
//! - You want fine-grained transaction control
//! - You're willing to manage lifetimes and borrowing
//! - You want the smallest possible memory footprint
// Module declarations
// Re-export main types
pub use RedbStoreZeroCopy;
pub use ;
pub use ;
pub use ;