pub struct SecondaryScan<PrimaryTable, SecondaryTable, T: Input>where
PrimaryTable: ReadableTable<DatabaseInnerKeyValue, &'static [u8]>,
SecondaryTable: ReadableTable<DatabaseInnerKeyValue, DatabaseInnerKeyValue>,{ /* private fields */ }
Expand description
Scan values from the database by secondary key.
Implementations§
Source§impl<PrimaryTable, SecondaryTable, T: Input> SecondaryScan<PrimaryTable, SecondaryTable, T>where
PrimaryTable: ReadableTable<DatabaseInnerKeyValue, &'static [u8]>,
SecondaryTable: ReadableTable<DatabaseInnerKeyValue, DatabaseInnerKeyValue>,
impl<PrimaryTable, SecondaryTable, T: Input> SecondaryScan<PrimaryTable, SecondaryTable, T>where
PrimaryTable: ReadableTable<DatabaseInnerKeyValue, &'static [u8]>,
SecondaryTable: ReadableTable<DatabaseInnerKeyValue, DatabaseInnerKeyValue>,
Sourcepub fn all(&self) -> SecondaryScanIterator<'_, PrimaryTable, T> ⓘ
pub fn all(&self) -> SecondaryScanIterator<'_, PrimaryTable, T> ⓘ
Iterate over all values by secondary key.
If the secondary key is optional
you will
get all values that have the secondary key set.
Anatomy of a secondary key it is a enum
with the following structure: <table_name>Key::<name>
.
§Example
use native_db::*;
use native_model::{native_model, Model};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
#[native_model(id=1, version=1)]
#[native_db]
struct Data {
#[primary_key]
id: u64,
#[secondary_key(optional)]
name: Option<String>,
}
fn main() -> Result<(), db_type::Error> {
let mut builder = DatabaseBuilder::new();
builder.define::<Data>()?;
let db = builder.create_in_memory()?;
// Open a read transaction
let r = db.r_transaction()?;
// Get only values that have the secondary key set (name is not None)
let _values: Vec<Data> = r.scan().secondary(DataKey::name)?.all().collect();
Ok(())
}
Sourcepub fn range<TR: InnerKeyValue, R: RangeBounds<TR>>(
&self,
range: R,
) -> SecondaryScanIterator<'_, PrimaryTable, T> ⓘ
pub fn range<TR: InnerKeyValue, R: RangeBounds<TR>>( &self, range: R, ) -> SecondaryScanIterator<'_, PrimaryTable, T> ⓘ
Iterate over all values by secondary key.
Anatomy of a secondary key it is a enum
with the following structure: <table_name>Key::<name>
.
§Example
use native_db::*;
use native_model::{native_model, Model};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
#[native_model(id=1, version=1)]
#[native_db]
struct Data {
#[primary_key]
id: u64,
#[secondary_key]
name: String,
}
fn main() -> Result<(), db_type::Error> {
let mut builder = DatabaseBuilder::new();
builder.define::<Data>()?;
let db = builder.create_in_memory()?;
// Open a read transaction
let r = db.r_transaction()?;
// Get only values that have the secondary key name from C to the end
let _values: Vec<Data> = r.scan().secondary(DataKey::name)?.range("C"..).collect();
Ok(())
}
Sourcepub fn start_with<'a>(
&'a self,
start_with: impl InnerKeyValue + 'a,
) -> SecondaryScanIteratorStartWith<'a, PrimaryTable, T> ⓘ
pub fn start_with<'a>( &'a self, start_with: impl InnerKeyValue + 'a, ) -> SecondaryScanIteratorStartWith<'a, PrimaryTable, T> ⓘ
Iterate over all values by secondary key.
Anatomy of a secondary key it is a enum
with the following structure: <table_name>Key::<name>
.
§Example
use native_db::*;
use native_model::{native_model, Model};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
#[native_model(id=1, version=1)]
#[native_db]
struct Data {
#[primary_key]
id: u64,
#[secondary_key]
name: String,
}
fn main() -> Result<(), db_type::Error> {
let mut builder = DatabaseBuilder::new();
builder.define::<Data>()?;
let db = builder.create_in_memory()?;
// Open a read transaction
let r = db.r_transaction()?;
// Get only values that have the secondary key name starting with "hello"
let _values: Vec<Data> = r.scan().secondary(DataKey::name)?.start_with("hello").collect();
Ok(())
}
Auto Trait Implementations§
impl<PrimaryTable, SecondaryTable, T> Freeze for SecondaryScan<PrimaryTable, SecondaryTable, T>
impl<PrimaryTable, SecondaryTable, T> RefUnwindSafe for SecondaryScan<PrimaryTable, SecondaryTable, T>
impl<PrimaryTable, SecondaryTable, T> Send for SecondaryScan<PrimaryTable, SecondaryTable, T>
impl<PrimaryTable, SecondaryTable, T> Sync for SecondaryScan<PrimaryTable, SecondaryTable, T>
impl<PrimaryTable, SecondaryTable, T> Unpin for SecondaryScan<PrimaryTable, SecondaryTable, T>
impl<PrimaryTable, SecondaryTable, T> UnwindSafe for SecondaryScan<PrimaryTable, SecondaryTable, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more