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
//! Tree types for redb zero-copy backend
//!
//! This module provides tree abstractions for CRUD operations within
//! transactions, maintaining strict lifetime relationships and providing
//! both mutable and immutable tree access.

use crate::error::NetabaseError;
use crate::traits::definition::NetabaseDefinitionTrait;
use crate::traits::model::{NetabaseModelTrait, NetabaseModelTraitKey};
use redb::{
    MultimapTableDefinition, MultimapValue, ReadTransaction, ReadableTable, ReadableTableMetadata,
    TableDefinition, WriteTransaction,
};
use std::marker::PhantomData;

/// Mutable tree for write operations
///
/// This provides methods to insert, remove, and query models within a write transaction.
/// The tree borrows mutably from the transaction, ensuring exclusive access.
///
/// # Lifetime Management
///
/// - `'txn`: Borrows from the transaction
/// - `'db`: Borrows from the database (through transaction)
/// - `D`: Database definition type
/// - `M`: Model type for this tree
///
/// # Examples
///
/// ```no_run
/// use netabase_store::databases::redb_zerocopy::RedbStoreZeroCopy;
/// use netabase_store::error::NetabaseError;
/// use netabase_store::{NetabaseModel, netabase, netabase_definition_module};
///
/// #[netabase_definition_module(MyDefinition, MyKeys)]
/// mod my_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 my_models::*;
///
/// # fn main() -> Result<(), NetabaseError> {
/// let store = RedbStoreZeroCopy::<MyDefinition>::new("./test.db")?;
/// let mut txn = store.begin_write()?;
/// let mut tree = txn.open_tree::<User>()?;
///
/// // Insert a user
/// let user = User { id: 1, name: "Alice".to_string() };
/// tree.put(user)?;
///
/// // Remove a user by key
/// tree.remove(UserPrimaryKey(1))?;
///
/// drop(tree); // Must drop before commit
/// txn.commit()?;
/// # Ok(())
/// # }
/// ```
pub struct RedbTreeMut<'txn, 'db, D, M>
where
    D: NetabaseDefinitionTrait,
    M: NetabaseModelTrait<D>,
{
    pub(crate) txn: &'txn mut WriteTransaction,
    #[allow(dead_code)]
    pub(crate) discriminant: D::Discriminant,
    pub(crate) table_name: &'static str,
    pub(crate) secondary_table_name: &'static str,
    pub(crate) _phantom: PhantomData<(&'db D, M)>,
}

impl<'txn, 'db, D, M> RedbTreeMut<'txn, 'db, D, M>
where
    D: NetabaseDefinitionTrait,
    M: NetabaseModelTrait<D>,
    M::Keys: NetabaseModelTraitKey<D>,
{
    /// Get the table definition for the primary table
    fn table_def(
        &self,
    ) -> TableDefinition<'static, M::Keys, super::super::redb_store::BincodeWrapper<M>> {
        TableDefinition::new(self.table_name)
    }

    /// Get the table definition for secondary keys
    fn secondary_table_def(
        &self,
    ) -> MultimapTableDefinition<
        'static,
        <M::Keys as NetabaseModelTraitKey<D>>::SecondaryKey,
        <M::Keys as NetabaseModelTraitKey<D>>::PrimaryKey,
    > {
        MultimapTableDefinition::new(self.secondary_table_name)
    }

    /// Insert a model into the tree
    ///
    /// This will insert the model into the primary table and update secondary indexes.
    /// If a model with the same primary key already exists, it will be replaced.
    ///
    /// # Arguments
    ///
    /// * `model` - The model instance to insert
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use netabase_store::databases::redb_zerocopy::RedbStoreZeroCopy;
    /// use netabase_store::error::NetabaseError;
    /// use netabase_store::{NetabaseModel, netabase, netabase_definition_module};
    ///
    /// #[netabase_definition_module(MyDefinition, MyKeys)]
    /// mod my_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 my_models::*;
    ///
    /// # fn main() -> Result<(), NetabaseError> {
    /// let store = RedbStoreZeroCopy::<MyDefinition>::new("./test.db")?;
    /// let mut txn = store.begin_write()?;
    /// let mut tree = txn.open_tree::<User>()?;
    /// let user = User { id: 1, name: "Alice".to_string() };
    /// tree.put(user)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn put(&mut self, model: M) -> Result<(), NetabaseError> {
        let table_def = self.table_def();
        let sec_table_def = self.secondary_table_def();

        let primary_key = model.primary_key();
        let secondary_keys = model.secondary_keys();
        let wrapped_key = M::Keys::from(primary_key.clone());

        // Insert into primary table
        let mut table = self.txn.open_table(table_def)?;
        table.insert(wrapped_key, super::super::redb_store::BincodeWrapper(model))?;

        // Insert into secondary indexes
        if !secondary_keys.is_empty() {
            let mut sec_table = self.txn.open_multimap_table(sec_table_def)?;
            for sec_key in secondary_keys.values() {
                sec_table.insert(sec_key.clone(), primary_key.clone())?;
            }
        }

        Ok(())
    }

    /// Insert multiple models in bulk
    ///
    /// This is more efficient than calling put() in a loop as it reuses
    /// the table handles and reduces transaction overhead.
    ///
    /// # Arguments
    ///
    /// * `models` - Vector of model instances to insert
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use netabase_store::databases::redb_zerocopy::RedbStoreZeroCopy;
    /// use netabase_store::error::NetabaseError;
    /// use netabase_store::{NetabaseModel, netabase, netabase_definition_module};
    ///
    /// #[netabase_definition_module(MyDefinition, MyKeys)]
    /// mod my_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 my_models::*;
    ///
    /// # fn main() -> Result<(), NetabaseError> {
    /// let store = RedbStoreZeroCopy::<MyDefinition>::new("./test.db")?;
    /// let mut txn = store.begin_write()?;
    /// let mut tree = txn.open_tree::<User>()?;
    /// let users = vec![
    ///     User { id: 1, name: "Alice".to_string() },
    ///     User { id: 2, name: "Bob".to_string() },
    /// ];
    /// tree.put_many(users)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn put_many(&mut self, models: Vec<M>) -> Result<(), NetabaseError> {
        let table_def = self.table_def();
        let sec_table_def = self.secondary_table_def();

        let mut table = self.txn.open_table(table_def)?;
        let mut sec_table = self.txn.open_multimap_table(sec_table_def)?;

        for model in models {
            let primary_key = model.primary_key();
            let secondary_keys = model.secondary_keys();
            let wrapped_key = M::Keys::from(primary_key.clone());

            table.insert(wrapped_key, super::super::redb_store::BincodeWrapper(model))?;

            if !secondary_keys.is_empty() {
                for sec_key in secondary_keys.values() {
                    sec_table.insert(sec_key.clone(), primary_key.clone())?;
                }
            }
        }

        Ok(())
    }

    /// Get a model by primary key (returns owned/cloned data)
    ///
    /// # Arguments
    ///
    /// * `key` - The primary key to look up
    ///
    /// # Returns
    ///
    /// Some(model) if found, None if not found
    pub fn get(
        &self,
        key: &<M::Keys as NetabaseModelTraitKey<D>>::PrimaryKey,
    ) -> Result<Option<M>, NetabaseError> {
        let table_def = self.table_def();
        let table = self.txn.open_table(table_def)?;
        let wrapped_key = M::Keys::from(key.clone());

        match table.get(wrapped_key)? {
            Some(guard) => {
                let model: M = guard.value(); // BincodeWrapper::SelfType = T, so this returns M directly
                Ok(Some(model))
            }
            None => Ok(None),
        }
    }

    /// Remove a model by primary key
    ///
    /// Returns the removed model if it existed. Also removes all secondary
    /// key entries for the model.
    ///
    /// # Arguments
    ///
    /// * `key` - The primary key of the model to remove
    ///
    /// # Returns
    ///
    /// Some(model) if the model existed and was removed, None if it didn't exist
    pub fn remove(
        &mut self,
        key: <M::Keys as NetabaseModelTraitKey<D>>::PrimaryKey,
    ) -> Result<Option<M>, NetabaseError> {
        let table_def = self.table_def();
        let sec_table_def = self.secondary_table_def();
        let wrapped_key = M::Keys::from(key.clone());

        let mut table = self.txn.open_table(table_def)?;

        // Get the model to extract secondary keys
        let model = match table.get(wrapped_key.clone())? {
            Some(guard) => {
                let model: M = guard.value(); // BincodeWrapper::SelfType = T, so this returns M directly
                Some(model)
            }
            None => None,
        };

        // Remove from primary table
        table.remove(wrapped_key)?;

        // Remove from secondary indexes
        if let Some(ref m) = model {
            let secondary_keys = m.secondary_keys();
            if !secondary_keys.is_empty() {
                let mut sec_table = self.txn.open_multimap_table(sec_table_def)?;
                for sec_key in secondary_keys.values() {
                    sec_table.remove(sec_key.clone(), key.clone())?;
                }
            }
        }

        Ok(model)
    }

    /// Remove multiple models in bulk
    ///
    /// Returns the removed models in the same order as the input keys.
    /// Some(model) if the model existed and was removed, None if it didn't exist.
    ///
    /// # Arguments
    ///
    /// * `keys` - Vector of primary keys to remove
    ///
    /// # Returns
    ///
    /// Vector of `Option<model>` in the same order as input keys
    pub fn remove_many(
        &mut self,
        keys: Vec<<M::Keys as NetabaseModelTraitKey<D>>::PrimaryKey>,
    ) -> Result<Vec<Option<M>>, NetabaseError> {
        let mut results = Vec::with_capacity(keys.len());
        for key in keys {
            results.push(self.remove(key)?);
        }
        Ok(results)
    }

    /// Get the number of models in the tree
    ///
    /// # Returns
    ///
    /// The count of models stored in this tree
    pub fn len(&self) -> Result<usize, NetabaseError> {
        let table_def = self.table_def();
        match self.txn.open_table(table_def) {
            Ok(table) => Ok(table.len()? as usize),
            Err(redb::TableError::TableDoesNotExist(_)) => Ok(0),
            Err(e) => Err(NetabaseError::RedbTableError(e)),
        }
    }

    /// Check if the tree is empty
    ///
    /// # Returns
    ///
    /// true if the tree contains no models, false otherwise
    pub fn is_empty(&self) -> Result<bool, NetabaseError> {
        Ok(self.len()? == 0)
    }

    /// Clear all models from the tree
    ///
    /// This removes all entries from both primary and secondary tables.
    /// Use with caution as this operation cannot be undone within the transaction.
    pub fn clear(&mut self) -> Result<(), NetabaseError>
    where
        <M as NetabaseModelTrait<D>>::Keys: std::borrow::Borrow<
            <<<M as NetabaseModelTrait<D>>::Keys as NetabaseModelTraitKey<D>>::SecondaryKey as redb::Value>::SelfType<'db>
        >
    {
        let table_def = self.table_def();

        // Clear primary table by removing first entry repeatedly
        let mut table = self.txn.open_table(table_def)?;
        while let Some((key, _)) = table.pop_first()? {
            // Key is already removed by pop_first
            drop(key);
        }

        // TODO: Clear secondary table - need to figure out the right approach for MultimapTable
        // For now, secondary indices will be cleared when models are individually removed
        // or when the database is closed

        Ok(())
    }
}

/// Immutable tree for read operations
///
/// This provides methods to query models within a read transaction.
/// Multiple immutable trees can be opened simultaneously from the same
/// read transaction.
///
/// # Lifetime Management
///
/// - `'txn`: Borrows from the transaction
/// - `'db`: Borrows from the database (through transaction)
/// - `D`: Database definition type
/// - `M`: Model type for this tree
///
/// # Examples
///
/// ```no_run
/// use netabase_store::databases::redb_zerocopy::RedbStoreZeroCopy;
/// use netabase_store::error::NetabaseError;
/// use netabase_store::{NetabaseModel, netabase, netabase_definition_module};
///
/// #[netabase_definition_module(MyDefinition, MyKeys)]
/// mod my_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 my_models::*;
///
/// # fn main() -> Result<(), NetabaseError> {
/// let store = RedbStoreZeroCopy::<MyDefinition>::new("./test.db")?;
/// let txn = store.begin_read()?;
/// let tree = txn.open_tree::<User>()?;
///
/// // Query by primary key
/// if let Some(user) = tree.get(&UserPrimaryKey(1))? {
///     println!("Found user: {}", user.name);
/// }
///
/// // Check tree statistics
/// println!("Tree has {} users", tree.len()?);
/// # Ok(())
/// # }
/// ```
pub struct RedbTree<'txn, 'db, D, M>
where
    D: NetabaseDefinitionTrait,
    M: NetabaseModelTrait<D>,
{
    pub(crate) txn: &'txn ReadTransaction,
    #[allow(dead_code)]
    pub(crate) discriminant: D::Discriminant,
    pub(crate) table_name: &'static str,
    pub(crate) secondary_table_name: &'static str,
    pub(crate) _phantom: PhantomData<(&'db D, M)>,
}

impl<'txn, 'db, D, M> RedbTree<'txn, 'db, D, M>
where
    D: NetabaseDefinitionTrait,
    M: NetabaseModelTrait<D>,
    M::Keys: NetabaseModelTraitKey<D>,
{
    /// Get the table definition for the primary table
    fn table_def(
        &self,
    ) -> TableDefinition<'static, M::Keys, super::super::redb_store::BincodeWrapper<M>> {
        TableDefinition::new(self.table_name)
    }

    /// Get the table definition for secondary keys
    fn secondary_table_def(
        &self,
    ) -> MultimapTableDefinition<
        'static,
        <M::Keys as NetabaseModelTraitKey<D>>::SecondaryKey,
        <M::Keys as NetabaseModelTraitKey<D>>::PrimaryKey,
    > {
        MultimapTableDefinition::new(self.secondary_table_name)
    }

    /// Get a model by primary key (returns owned/cloned data)
    ///
    /// # Arguments
    ///
    /// * `key` - The primary key to look up
    ///
    /// # Returns
    ///
    /// Some(model) if found, None if not found
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use netabase_store::databases::redb_zerocopy::RedbStoreZeroCopy;
    /// use netabase_store::error::NetabaseError;
    /// use netabase_store::{NetabaseModel, netabase, netabase_definition_module};
    ///
    /// #[netabase_definition_module(MyDefinition, MyKeys)]
    /// mod my_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 my_models::*;
    ///
    /// # fn main() -> Result<(), NetabaseError> {
    /// let store = RedbStoreZeroCopy::<MyDefinition>::new("./test.db")?;
    /// let txn = store.begin_read()?;
    /// let tree = txn.open_tree::<User>()?;
    /// if let Some(user) = tree.get(&UserPrimaryKey(1))? {
    ///     println!("Found user: {}", user.name);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn get(
        &self,
        key: &<M::Keys as NetabaseModelTraitKey<D>>::PrimaryKey,
    ) -> Result<Option<M>, NetabaseError> {
        let table_def = self.table_def();
        let table = self.txn.open_table(table_def)?;
        let wrapped_key = M::Keys::from(key.clone());

        match table.get(wrapped_key)? {
            Some(guard) => {
                let model: M = guard.value(); // BincodeWrapper::SelfType = T, so this returns M directly
                Ok(Some(model))
            }
            None => Ok(None),
        }
    }

    /// Get models by secondary key
    ///
    /// Returns all models that have the given secondary key.
    /// This returns an iterator-like object that can be used to access
    /// the primary keys, which can then be used to fetch the full models.
    ///
    /// # Arguments
    ///
    /// * `sec_key` - The secondary key to look up
    ///
    /// # Returns
    ///
    /// A MultimapValue iterator over primary keys that match the secondary key
    pub fn get_by_secondary_key(
        &'txn self,
        sec_key: &<M::Keys as NetabaseModelTraitKey<D>>::SecondaryKey,
    ) -> Result<MultimapValue<'txn, <M::Keys as NetabaseModelTraitKey<D>>::PrimaryKey>, NetabaseError>
    where
        for<'a> &'a <<M as NetabaseModelTrait<D>>::Keys as NetabaseModelTraitKey<D>>::SecondaryKey:
            std::borrow::Borrow<
                <<<M as NetabaseModelTrait<D>>::Keys as NetabaseModelTraitKey<D>>::SecondaryKey as redb::Value>::SelfType<'a>
            >
    {
        let sec_table_def = self.secondary_table_def();
        let sec_table = self.txn.open_multimap_table(sec_table_def)?;
        sec_table
            .get(sec_key)
            .map_err(NetabaseError::RedbStorageError)
    }

    /// Get the number of models in the tree
    ///
    /// # Returns
    ///
    /// The count of models stored in this tree
    pub fn len(&self) -> Result<usize, NetabaseError> {
        let table_def = self.table_def();
        match self.txn.open_table(table_def) {
            Ok(table) => Ok(table.len()? as usize),
            Err(redb::TableError::TableDoesNotExist(_)) => Ok(0),
            Err(e) => Err(NetabaseError::RedbTableError(e)),
        }
    }

    /// Check if the tree is empty
    ///
    /// # Returns
    ///
    /// true if the tree contains no models, false otherwise
    pub fn is_empty(&self) -> Result<bool, NetabaseError> {
        Ok(self.len()? == 0)
    }
}

// Tests temporarily disabled due to macro resolution issues within the crate itself
// #[cfg(test)]
// mod tests {
//     use super::*;
//     use crate::databases::redb_zerocopy::RedbStoreZeroCopy;
//     use tempfile::tempdir;
//
//     // Tests would go here but require proper macro setup
// }