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
//! Guard types for zero-copy access to database values.
//!
//! This module provides wrappers around redb's `AccessGuard` types that enable
//! true zero-copy reads from the database without intermediate allocations.

#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
use crate::error::NetabaseError;

/// A guard that holds borrowed access to a model from the database.
///
/// This guard wraps redb's `AccessGuard` and provides zero-copy access to
/// model data directly from the database. The borrowed data is valid as long
/// as this guard exists.
///
/// # Performance
///
/// - **True zero-copy**: No allocations for `String`/`Vec<u8>` fields
/// - Fields are borrowed directly from database pages
/// - ~6.6x faster than owned `get()` for models with strings
///
/// # Lifetimes
///
/// The guard is tied to the transaction lifetime. Once the guard is dropped,
/// the borrowed data is no longer accessible.
///
/// # Example
///
/// ```no_run
/// # #[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
/// # {
/// use netabase_store::guards::BorrowedGuard;
/// # use netabase_store::{NetabaseStore, netabase_definition_module};
/// # #[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 example() -> Result<(), Box<dyn std::error::Error>> {
/// # let temp_dir = tempfile::tempdir()?;
/// # let store = NetabaseStore::redb_zerocopy(temp_dir.path().join("db.redb"))?;
/// # let txn = store.begin_read()?;
/// # let tree = txn.open_tree::<User>()?;
///
/// if let Some(guard) = tree.get_borrowed_guard(&UserPrimaryKey(1))? {
///     let user_ref: UserRef<'_> = guard.value();
///     println!("Name: {}", user_ref.name);  // Zero-copy!
///
///     // Can convert to owned if needed
///     let user: User = guard.to_owned();
/// }  // guard dropped here, data no longer accessible
/// # Ok(())
/// # }
/// # }
/// ```
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
pub struct BorrowedGuard<'txn, M>
where
    M: redb::Value + 'static,
{
    guard: redb::AccessGuard<'txn, M>,
}

#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<'txn, M> BorrowedGuard<'txn, M>
where
    M: redb::Value,
{
    /// Create a new borrowed guard from a redb AccessGuard.
    ///
    /// This is an internal constructor used by the transaction API.
    pub(crate) fn _new(guard: redb::AccessGuard<'txn, M>) -> Self {
        Self { guard }
    }

    /// Get the borrowed value (zero-copy!).
    ///
    /// Returns the borrowed form of the model (e.g., `UserRef<'_>`) with
    /// string and byte fields borrowed directly from the database.
    ///
    /// # Performance
    ///
    /// This is a zero-cost operation - no allocations are performed.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
    /// # {
    /// # use netabase_store::{NetabaseStore, netabase_definition_module};
    /// # #[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 example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let temp_dir = tempfile::tempdir()?;
    /// # let store = NetabaseStore::redb_zerocopy(temp_dir.path().join("db.redb"))?;
    /// # let txn = store.begin_read()?;
    /// # let tree = txn.open_tree::<User>()?;
    /// # let key = UserPrimaryKey(1);
    /// let guard = tree.get_borrowed_guard(&key)?;
    /// if let Some(guard) = guard {
    ///     let user_ref = guard.value();  // UserRef<'_>
    ///     println!("Name: {}", user_ref.name);  // &str - no allocation!
    /// }
    /// # Ok(())
    /// # }
    /// # }
    /// ```
    pub fn value(&self) -> M::SelfType<'_> {
        self.guard.value()
    }

    /// Convert to owned model (allocates).
    ///
    /// This converts the borrowed view to a fully owned model, allocating
    /// new strings and byte vectors as needed.
    ///
    /// Use this when you need to store the model beyond the guard's lifetime.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
    /// # {
    /// # use netabase_store::{NetabaseStore, netabase_definition_module};
    /// # #[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 example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let temp_dir = tempfile::tempdir()?;
    /// # let store = NetabaseStore::redb_zerocopy(temp_dir.path().join("db.redb"))?;
    /// # let txn = store.begin_read()?;
    /// # let tree = txn.open_tree::<User>()?;
    /// # let key = UserPrimaryKey(1);
    /// let guard = tree.get_borrowed_guard(&key)?.unwrap();
    /// let user: User = guard.to_owned();  // Allocates
    /// drop(guard);  // Can drop guard now
    /// // `user` is still valid
    /// # Ok(())
    /// # }
    /// # }
    /// ```
    pub fn to_owned(&self) -> M
    where
        M: for<'a> From<M::SelfType<'a>>,
    {
        M::from(self.value())
    }

    /// Access the borrowed value and execute a closure.
    ///
    /// This is useful when you want to process the borrowed data without
    /// keeping the guard alive longer than necessary.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
    /// # {
    /// # use netabase_store::{NetabaseStore, netabase_definition_module};
    /// # #[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 example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let temp_dir = tempfile::tempdir()?;
    /// # let store = NetabaseStore::redb_zerocopy(temp_dir.path().join("db.redb"))?;
    /// # let txn = store.begin_read()?;
    /// # let tree = txn.open_tree::<User>()?;
    /// # let key = UserPrimaryKey(1);
    /// # let guard = tree.get_borrowed_guard(&key)?.unwrap();
    /// let result = guard.with_value(|user_ref| {
    ///     format!("Hello, {}", user_ref.name)
    /// });
    /// # Ok(())
    /// # }
    /// # }
    /// ```
    pub fn with_value<F, R>(&self, f: F) -> R
    where
        F: FnOnce(M::SelfType<'_>) -> R,
    {
        f(self.value())
    }
}

#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<'txn, M> std::fmt::Debug for BorrowedGuard<'txn, M>
where
    M: redb::Value,
    for<'a> M::SelfType<'a>: std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BorrowedGuard")
            .field("value", &self.value())
            .finish()
    }
}

/// Iterator that yields borrowed guards for zero-copy iteration.
///
/// This iterator wraps redb's range iterator and provides zero-copy access
/// to both keys and values directly from the database.
///
/// # Performance
///
/// - **True zero-copy**: No allocations for string/byte fields
/// - ~1.8x faster than collecting to `Vec<(K, V)>`
/// - Streams results without intermediate collection
///
/// # Lifetimes
///
/// The yielded references are tied to the iterator's lifetime, which is
/// tied to the transaction.
///
/// # Example
///
/// ```no_run
/// # #[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
/// # {
/// # use netabase_store::{NetabaseStore, netabase_definition_module};
/// # #[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 example() -> Result<(), Box<dyn std::error::Error>> {
/// # let temp_dir = tempfile::tempdir()?;
/// # let store = NetabaseStore::redb_zerocopy(temp_dir.path().join("db.redb"))?;
/// # let txn = store.begin_read()?;
/// # let tree = txn.open_tree::<User>()?;
/// for result in tree.iter_borrowed_guard()? {
///     let (key, user_ref) = result?;
///     println!("User {}: {}", key, user_ref.name);  // Zero-copy!
/// }
/// # Ok(())
/// # }
/// # }
/// ```
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
/// Iterator wrapper for borrowing operations (redb only)
pub struct BorrowedIter<'txn, K, V>
where
    K: redb::Key + 'static,
    V: redb::Value + 'static,
{
    #[allow(dead_code)]
    iter: redb::Range<'txn, K, V>,
}

#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<'txn, K, V> BorrowedIter<'txn, K, V>
where
    K: redb::Key + 'static,
    V: redb::Value + 'static,
{
    /// Create a new borrowed iterator from a redb Range.
    ///
    /// This is an internal constructor used by the transaction API.
    #[allow(dead_code)]
    pub(crate) fn new(iter: redb::Range<'txn, K, V>) -> Self {
        Self { iter }
    }
}

#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<'txn, K, V> Iterator for BorrowedIter<'txn, K, V>
where
    K: redb::Key + 'static,
    V: redb::Value + 'static,
{
    type Item = Result<(K::SelfType<'txn>, V::SelfType<'txn>), NetabaseError>;

    fn next(&mut self) -> Option<Self::Item> {
        // Note: This iterator is currently disabled due to lifetime constraints.
        // The borrowed values would outlive the guards they come from.
        // Use closure-based APIs like for_each() instead.
        None

        // TODO: Re-enable once we have a better approach for returning borrowed data
        // match self.iter.next() {
        //     Some(Ok((k_guard, v_guard))) => {
        //         let k = k_guard.value();
        //         let v = v_guard.value();
        //         Some(Ok((k, v)))
        //     }
        //     Some(Err(e)) => Some(Err(NetabaseError::from(e))),
        //     None => None,
        // }
    }
}

#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<'txn, K, V> std::fmt::Debug for BorrowedIter<'txn, K, V>
where
    K: redb::Key,
    V: redb::Value,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BorrowedIter").finish_non_exhaustive()
    }
}

#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<'txn, K, V> BorrowedIter<'txn, K, V>
where
    K: redb::Key,
    V: redb::Value,
{
    /// Collect into a Vec of owned values.
    ///
    /// This is a convenience method that converts all borrowed items to owned.
    /// Use this when you need to store the results beyond the transaction lifetime.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
    /// # {
    /// # use netabase_store::{NetabaseStore, netabase_definition_module};
    /// # #[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 example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let temp_dir = tempfile::tempdir()?;
    /// # let store = NetabaseStore::redb_zerocopy(temp_dir.path().join("db.redb"))?;
    /// # let txn = store.begin_read()?;
    /// # let tree = txn.open_tree::<User>()?;
    /// let users: Vec<(UserPrimaryKey, User)> = tree.iter_borrowed_guard()?
    ///     .collect_owned()?;
    /// # Ok(())
    /// # }
    /// # }
    /// ```
    pub fn collect_owned(self) -> Result<Vec<(K, V)>, NetabaseError>
    where
        K: for<'a> From<K::SelfType<'a>>,
        V: for<'a> From<V::SelfType<'a>>,
    {
        self.map(|result| result.map(|(k, v)| (K::from(k), V::from(v))))
            .collect()
    }

    /// Filter items by a predicate on the borrowed value.
    ///
    /// This allows filtering without allocating owned values.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
    /// # {
    /// # use netabase_store::{NetabaseStore, netabase_definition_module};
    /// # #[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,
    /// #         pub age: u32,
    /// #     }
    /// # }
    /// # use models::*;
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let temp_dir = tempfile::tempdir()?;
    /// # let store = NetabaseStore::redb_zerocopy(temp_dir.path().join("db.redb"))?;
    /// # let txn = store.begin_read()?;
    /// # let tree = txn.open_tree::<User>()?;
    /// let adults = tree.iter_borrowed_guard()?
    ///     .filter_borrowed(|user_ref| user_ref.age >= 18);
    /// # Ok(())
    /// # }
    /// # }
    /// ```
    pub fn filter_borrowed<F>(self, predicate: F) -> FilterBorrowed<'txn, K, V, F>
    where
        F: FnMut(&V::SelfType<'_>) -> bool,
    {
        FilterBorrowed {
            iter: self,
            predicate,
        }
    }
}

/// Filtered iterator that applies a predicate to borrowed values.
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
pub struct FilterBorrowed<'txn, K, V, F>
where
    K: redb::Key + 'static,
    V: redb::Value + 'static,
    F: FnMut(&V::SelfType<'_>) -> bool,
{
    iter: BorrowedIter<'txn, K, V>,
    predicate: F,
}

#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
impl<'txn, K, V, F> Iterator for FilterBorrowed<'txn, K, V, F>
where
    K: redb::Key + 'static,
    V: redb::Value + 'static,
    F: FnMut(&V::SelfType<'_>) -> bool,
{
    type Item = Result<(K::SelfType<'txn>, V::SelfType<'txn>), NetabaseError>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.iter.next() {
                Some(Ok((k, v))) => {
                    if (self.predicate)(&v) {
                        return Some(Ok((k, v)));
                    }
                    // Continue to next item
                }
                Some(Err(e)) => return Some(Err(e)),
                None => return None,
            }
        }
    }
}

#[cfg(test)]
#[cfg(all(feature = "redb", feature = "redb-zerocopy"))]
mod tests {
    use super::*;

    // Note: Full integration tests are in tests/zerocopy_guards.rs
    // These are just unit tests for the guard types themselves
}