pub struct View<'a, Cn, V: SerializedView, Key>where
    V::Key: Borrow<Key> + PartialEq<Key>,
    Key: PartialEq + ?Sized,{
    pub key: Option<QueryKey<'a, V::Key, Key>>,
    pub access_policy: AccessPolicy,
    pub sort: Sort,
    pub limit: Option<u32>,
    /* private fields */
}
Expand description

Parameters to query a schema::View.

The examples for this type use this view definition:

use bonsaidb_core::define_basic_unique_mapped_view;
use bonsaidb_core::document::{CollectionDocument, Emit};
use bonsaidb_core::schema::{
    CollectionMapReduce, DefaultViewSerialization, Name, ReduceResult, View, ViewMapResult,
    ViewMappedValue, ViewSchema,
};

#[derive(Debug, Clone, View, ViewSchema)]
#[view(collection = MyCollection, key = u32, value = f32, name = "scores-by-rank")]
pub struct ScoresByRank;

impl CollectionMapReduce for ScoresByRank {
    fn map<'doc>(
        &self,
        document: CollectionDocument<<Self::View as View>::Collection>,
    ) -> ViewMapResult<'doc, Self::View> {
        document
            .header
            .emit_key_and_value(document.contents.rank, document.contents.score)
    }

    fn reduce(
        &self,
        mappings: &[ViewMappedValue<'_, Self::View>],
        rereduce: bool,
    ) -> ReduceResult<Self::View> {
        if mappings.is_empty() {
            Ok(0.)
        } else {
            Ok(mappings.iter().map(|map| map.value).sum::<f32>() / mappings.len() as f32)
        }
    }
}

Fields§

§key: Option<QueryKey<'a, V::Key, Key>>

Key filtering criteria.

§access_policy: AccessPolicy

The view’s data access policy. The default value is AccessPolicy::UpdateBefore.

§sort: Sort

The sort order of the query.

§limit: Option<u32>

The maximum number of results to return.

Implementations§

source§

impl<'a, Cn, V, Key> View<'a, Cn, V, Key>where V::Key: Borrow<Key> + PartialEq<Key>, V: SerializedView, Cn: Connection, Key: KeyEncoding<V::Key> + PartialEq + ?Sized,

source

pub fn with_key<K>(self, key: &'a K) -> View<'a, Cn, V, K>where K: KeyEncoding<V::Key> + PartialEq + ?Sized, V::Key: Borrow<K> + PartialEq<K>,

Filters for entries in the view with key.

// score is an f32 in this example
for mapping in ScoresByRank::entries(&db).with_key(&42).query()? {
    assert_eq!(mapping.key, 42);
    println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}
source

pub fn with_keys<K, IntoIter: IntoIterator<Item = &'a K>>( self, keys: IntoIter ) -> View<'a, Cn, V, K>where V::Key: Borrow<K> + PartialEq<K>, K: PartialEq + ?Sized,

Filters for entries in the view with keys.

// score is an f32 in this example
for mapping in ScoresByRank::entries(&db).with_keys(&[42, 43]).query()? {
    println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}
source

pub fn with_key_range<K, R>(self, range: R) -> View<'a, Cn, V, K>where R: Into<RangeRef<'a, V::Key, K>>, K: PartialEq, V::Key: Borrow<K> + PartialEq<K>,

Filters for entries in the view with the range keys.

// score is an f32 in this example
for mapping in ScoresByRank::entries(&db).with_key_range(42..).query()? {
    assert!(mapping.key >= 42);
    println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}
source

pub fn with_key_prefix<K>(self, prefix: &'a K) -> View<'a, Cn, V, K>where K: KeyEncoding<V::Key> + IntoPrefixRange<'a, V::Key> + PartialEq + ?Sized, V::Key: Borrow<K> + PartialEq<K>,

Filters for entries in the view with keys that begin with prefix.

#[derive(View, Debug, Clone)]
#[view(name = "by-name", key = String, collection = MyCollection)]
struct ByName;

// score is an f32 in this example
for mapping in ByName::entries(&db).with_key_prefix("a").query()? {
    assert!(mapping.key.starts_with("a"));
    println!("{} in document {:?}", mapping.key, mapping.source);
}
source

pub const fn with_access_policy(self, policy: AccessPolicy) -> Self

Sets the access policy for queries.

// score is an f32 in this example
for mapping in ScoresByRank::entries(&db)
    .with_access_policy(AccessPolicy::UpdateAfter)
    .query()?
{
    println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}
source

pub const fn ascending(self) -> Self

Returns the matching mappings in ascending key order. This is the default sorting behavior.

When more than one mapping exists for a single key, all matching mappings are returned as a unique entry. The resulting mappings are sorted only by the key, and as such, the order of mappings with the same key is undefined.

// score is an f32 in this example
for mapping in ScoresByRank::entries(&db).ascending().query()? {
    println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}
source

pub const fn descending(self) -> Self

Returns the matching mappings in descending key order.

When more than one mapping exists for a single key, all matching mappings are returned as a unique entry. The resulting mappings are sorted only by the key, and as such, the order of mappings with the same key is undefined.

// score is an f32 in this example
for mapping in ScoresByRank::entries(&db).descending().query()? {
    println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}
source

pub const fn limit(self, maximum_results: u32) -> Self

Sets the maximum number of results to return.

// score is an f32 in this example
let mappings = ScoresByRank::entries(&db).limit(10).query()?;
assert!(mappings.len() <= 10);
source

pub fn query(self) -> Result<ViewMappings<V>, Error>

Executes the query and retrieves the results.

// score is an f32 in this example
for mapping in ScoresByRank::entries(&db).query()? {
    println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}
source

pub fn query_with_docs(self) -> Result<MappedDocuments<OwnedDocument, V>, Error>

Executes the query and retrieves the results with the associated Documents.

for mapping in &ScoresByRank::entries(&db)
    .with_key_range(42..=44)
    .query_with_docs()?
{
    println!(
        "Mapping from #{} with rank: {} and score: {}. Document bytes: {:?}",
        mapping.document.header.id, mapping.key, mapping.value, mapping.document.contents
    );
}
source

pub fn query_with_collection_docs( self ) -> Result<MappedDocuments<CollectionDocument<V::Collection>, V>, Error>where V::Collection: SerializedCollection, <V::Collection as SerializedCollection>::Contents: Debug,

Executes the query and retrieves the results with the associated CollectionDocuments.

for mapping in &ScoresByRank::entries(&db)
    .with_key_range(42..=44)
    .query_with_collection_docs()?
{
    println!(
        "Mapping from #{} with rank: {} and score: {}. Deserialized Contents: {:?}",
        mapping.document.header.id, mapping.key, mapping.value, mapping.document.contents
    );
}
source

pub fn reduce(self) -> Result<V::Value, Error>

Executes a reduce over the results of the query

// score is an f32 in this example
let score = ScoresByRank::entries(&db).reduce()?;
println!("Average score: {:3}", score);
source

pub fn reduce_grouped(self) -> Result<GroupedReductions<V>, Error>

Executes a reduce over the results of the query, grouping by key.

// score is an f32 in this example
for mapping in ScoresByRank::entries(&db).reduce_grouped()? {
    println!(
        "Rank {} has an average score of {:3}",
        mapping.key, mapping.value
    );
}
source

pub fn delete_docs(self) -> Result<u64, Error>

Deletes all of the associated documents that match this view query.

ScoresByRank::entries(&db).delete_docs()?;

Auto Trait Implementations§

§

impl<'a, Cn, V, Key: ?Sized> RefUnwindSafe for View<'a, Cn, V, Key>where Cn: RefUnwindSafe, Key: RefUnwindSafe, V: RefUnwindSafe, <V as View>::Key: RefUnwindSafe,

§

impl<'a, Cn, V, Key: ?Sized> Send for View<'a, Cn, V, Key>where Cn: Sync, Key: Sync,

§

impl<'a, Cn, V, Key: ?Sized> Sync for View<'a, Cn, V, Key>where Cn: Sync, Key: Sync,

§

impl<'a, Cn, V, Key: ?Sized> Unpin for View<'a, Cn, V, Key>where V: Unpin, <V as View>::Key: Unpin,

§

impl<'a, Cn, V, Key: ?Sized> UnwindSafe for View<'a, Cn, V, Key>where Cn: RefUnwindSafe, Key: RefUnwindSafe, V: UnwindSafe, <V as View>::Key: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

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

const: unstable · 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> Same<T> for T

§

type Output = T

Should always be Self
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more