Struct native_db::transaction::query::SecondaryScan
source · pub struct SecondaryScan<PrimaryTable, SecondaryTable, T: ToInput>{ /* private fields */ }
Expand description
Scan values from the database by secondary key.
Implementations§
source§impl<PrimaryTable, SecondaryTable, T: ToInput> SecondaryScan<PrimaryTable, SecondaryTable, T>
impl<PrimaryTable, SecondaryTable, T: ToInput> SecondaryScan<PrimaryTable, SecondaryTable, T>
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};
use itertools::Itertools;
#[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 models = Models::new();
models.define::<Data>()?;
let db = Builder::new().create_in_memory(&models)?;
// 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().try_collect()?;
Ok(())
}
sourcepub fn range<TR: ToKey, R: RangeBounds<TR>>(
&self,
range: R,
) -> SecondaryScanIterator<'_, PrimaryTable, T> ⓘ
pub fn range<TR: ToKey, 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};
use itertools::Itertools;
#[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 models = Models::new();
models.define::<Data>()?;
let db = Builder::new().create_in_memory(&models)?;
// 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"..).try_collect()?;
Ok(())
}
sourcepub fn start_with<'a>(
&'a self,
start_with: impl ToKey + 'a,
) -> SecondaryScanIteratorStartWith<'a, PrimaryTable, T> ⓘ
pub fn start_with<'a>( &'a self, start_with: impl ToKey + '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};
use itertools::Itertools;
#[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 models = Models::new();
models.define::<Data>()?;
let db = Builder::new().create_in_memory(&models)?;
// 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").try_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