native_db_32bit/transaction/query/scan/
mod.rs

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
mod primary_scan;
mod secondary_scan;

use crate::db_type::{
    DatabaseInnerKeyValue, DatabaseSecondaryKeyOptions, Input, KeyDefinition, Result,
};
pub use primary_scan::*;
pub use secondary_scan::*;

use crate::transaction::internal::private_readable_transaction::PrivateReadableTransaction;
use crate::transaction::internal::r_transaction::InternalRTransaction;
use crate::transaction::internal::rw_transaction::InternalRwTransaction;

/// Get values from the database.
pub struct RScan<'db, 'txn> {
    pub(crate) internal: &'txn InternalRTransaction<'db>,
}

impl<'txn> RScan<'_, 'txn> {
    /// Get a values from the database by primary key.
    pub fn primary<T: Input>(
        &self,
    ) -> Result<PrimaryScan<redb::ReadOnlyTable<'txn, DatabaseInnerKeyValue, &'static [u8]>, T>>
    {
        let model = T::native_db_model();
        let table = self.internal.get_primary_table(&model)?;
        let out = PrimaryScan::new(table);
        Ok(out)
    }

    /// Get a values from the database by secondary key.
    pub fn secondary<T: Input>(
        &self,
        key_def: impl KeyDefinition<DatabaseSecondaryKeyOptions>,
    ) -> Result<
        SecondaryScan<
            redb::ReadOnlyTable<'txn, DatabaseInnerKeyValue, &'static [u8]>,
            redb::ReadOnlyTable<'txn, DatabaseInnerKeyValue, DatabaseInnerKeyValue>,
            T,
        >,
    > {
        let model = T::native_db_model();
        let primary_table = self.internal.get_primary_table(&model)?;
        let secondary_key = key_def.database_key();
        let secondary_table = self.internal.get_secondary_table(&model, &secondary_key)?;
        let out = SecondaryScan::new(primary_table, secondary_table);
        Ok(out)
    }
}

pub struct RwScan<'db, 'txn> {
    pub(crate) internal: &'txn InternalRwTransaction<'db>,
}

impl<'db, 'txn> RwScan<'db, 'txn> {
    pub fn primary<T: Input>(
        &self,
    ) -> Result<PrimaryScan<redb::Table<'db, 'txn, DatabaseInnerKeyValue, &'static [u8]>, T>> {
        let model = T::native_db_model();
        let table = self.internal.get_primary_table(&model)?;
        let out = PrimaryScan::new(table);
        Ok(out)
    }

    pub fn secondary<T: Input>(
        &self,
        key_def: impl KeyDefinition<DatabaseSecondaryKeyOptions>,
    ) -> Result<
        SecondaryScan<
            redb::Table<'db, 'txn, DatabaseInnerKeyValue, &'static [u8]>,
            redb::Table<'db, 'txn, DatabaseInnerKeyValue, DatabaseInnerKeyValue>,
            T,
        >,
    > {
        let model = T::native_db_model();
        let primary_table = self.internal.get_primary_table(&model)?;
        let secondary_key = key_def.database_key();
        let secondary_table = self.internal.get_secondary_table(&model, &secondary_key)?;
        let out = SecondaryScan::new(primary_table, secondary_table);
        Ok(out)
    }
}