Struct native_db::transaction::query::SecondaryScan

source ·
pub struct SecondaryScan<PrimaryTable, SecondaryTable, T: Input>{ /* private fields */ }
Expand description

Scan values from the database by secondary key.

Implementations§

source§

impl<PrimaryTable, SecondaryTable, T: Input> SecondaryScan<PrimaryTable, SecondaryTable, T>

source

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(())
}
source

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(())
}
source

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>
where PrimaryTable: Freeze, SecondaryTable: Freeze,

§

impl<PrimaryTable, SecondaryTable, T> RefUnwindSafe for SecondaryScan<PrimaryTable, SecondaryTable, T>
where PrimaryTable: RefUnwindSafe, SecondaryTable: RefUnwindSafe, T: RefUnwindSafe,

§

impl<PrimaryTable, SecondaryTable, T> Send for SecondaryScan<PrimaryTable, SecondaryTable, T>
where PrimaryTable: Send, SecondaryTable: Send, T: Send,

§

impl<PrimaryTable, SecondaryTable, T> Sync for SecondaryScan<PrimaryTable, SecondaryTable, T>
where PrimaryTable: Sync, SecondaryTable: Sync, T: Sync,

§

impl<PrimaryTable, SecondaryTable, T> Unpin for SecondaryScan<PrimaryTable, SecondaryTable, T>
where PrimaryTable: Unpin, SecondaryTable: Unpin, T: Unpin,

§

impl<PrimaryTable, SecondaryTable, T> UnwindSafe for SecondaryScan<PrimaryTable, SecondaryTable, T>
where PrimaryTable: UnwindSafe, SecondaryTable: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.