Struct bonsaidb_core::connection::View
source · [−]pub struct View<'a, Cn, V: SerializedView> {
pub key: Option<QueryKey<V::Key>>,
pub access_policy: AccessPolicy,
pub sort: Sort,
pub limit: Option<usize>,
/* 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,
document::{CollectionDocument, Emit},
schema::{
CollectionViewSchema, DefaultViewSerialization, Name, ReduceResult, View,
ViewMapResult, ViewMappedValue,
},
};
#[derive(Debug, Clone, View)]
#[view(collection = MyCollection, key = u32, value = f32, name = "scores-by-rank")]
pub struct ScoresByRank;
impl CollectionViewSchema for ScoresByRank {
type View = Self;
fn map(
&self,
document: CollectionDocument<<Self::View as View>::Collection>,
) -> ViewMapResult<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<V::Key>>Key filtering criteria.
access_policy: AccessPolicyThe view’s data access policy. The default value is AccessPolicy::UpdateBefore.
sort: SortThe sort order of the query.
limit: Option<usize>The maximum number of results to return.
Implementations
sourceimpl<'a, Cn, V> View<'a, Cn, V> where
V: SerializedView,
Cn: Connection,
impl<'a, Cn, V> View<'a, Cn, V> where
V: SerializedView,
Cn: Connection,
sourcepub fn with_key(self, key: V::Key) -> Self
pub fn with_key(self, key: V::Key) -> Self
Filters for entries in the view with key.
// score is an f32 in this example
for mapping in db.view::<ScoresByRank>().with_key(42).query().await? {
assert_eq!(mapping.key, 42);
println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}sourcepub fn with_keys<IntoIter: IntoIterator<Item = V::Key>>(
self,
keys: IntoIter
) -> Self
pub fn with_keys<IntoIter: IntoIterator<Item = V::Key>>(
self,
keys: IntoIter
) -> Self
Filters for entries in the view with keys.
// score is an f32 in this example
for mapping in db
.view::<ScoresByRank>()
.with_keys([42, 43])
.query()
.await?
{
println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}sourcepub fn with_key_range<R: Into<Range<V::Key>>>(self, range: R) -> Self
pub fn with_key_range<R: Into<Range<V::Key>>>(self, range: R) -> Self
Filters for entries in the view with the range keys.
// score is an f32 in this example
for mapping in db
.view::<ScoresByRank>()
.with_key_range(42..)
.query()
.await?
{
assert!(mapping.key >= 42);
println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}sourcepub fn with_access_policy(self, policy: AccessPolicy) -> Self
pub fn with_access_policy(self, policy: AccessPolicy) -> Self
Sets the access policy for queries.
// score is an f32 in this example
for mapping in db
.view::<ScoresByRank>()
.with_access_policy(AccessPolicy::UpdateAfter)
.query()
.await?
{
println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}sourcepub fn ascending(self) -> Self
pub fn ascending(self) -> Self
Queries the view in ascending order. This is the default sorting behavior.
// score is an f32 in this example
for mapping in db.view::<ScoresByRank>().ascending().query().await? {
println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}sourcepub fn descending(self) -> Self
pub fn descending(self) -> Self
Queries the view in descending order.
// score is an f32 in this example
for mapping in db.view::<ScoresByRank>().descending().query().await? {
println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}sourcepub fn limit(self, maximum_results: usize) -> Self
pub fn limit(self, maximum_results: usize) -> Self
Sets the maximum number of results to return.
// score is an f32 in this example
let mappings = db.view::<ScoresByRank>().limit(10).query().await?;
assert!(mappings.len() <= 10);sourcepub async fn query(self) -> Result<Vec<Map<V::Key, V::Value>>, Error>
pub async fn query(self) -> Result<Vec<Map<V::Key, V::Value>>, Error>
Executes the query and retrieves the results.
// score is an f32 in this example
for mapping in db.view::<ScoresByRank>().query().await? {
println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}sourcepub async fn query_with_docs(
self
) -> Result<MappedDocuments<OwnedDocument, V>, Error>
pub async fn query_with_docs(
self
) -> Result<MappedDocuments<OwnedDocument, V>, Error>
Executes the query and retrieves the results with the associated Documents.
for mapping in &db
.view::<ScoresByRank>()
.with_key_range(42..=44)
.query_with_docs()
.await?
{
println!(
"Mapping from #{} with rank: {} and score: {}. Document bytes: {:?}",
mapping.document.header.id, mapping.key, mapping.value, mapping.document.contents
);
}sourcepub async fn query_with_collection_docs(
self
) -> Result<MappedDocuments<CollectionDocument<V::Collection>, V>, Error> where
V::Collection: SerializedCollection,
<V::Collection as SerializedCollection>::Contents: Debug,
pub async 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 &db
.view::<ScoresByRank>()
.with_key_range(42..=44)
.query_with_collection_docs()
.await?
{
println!(
"Mapping from #{} with rank: {} and score: {}. Deserialized Contents: {:?}",
mapping.document.header.id, mapping.key, mapping.value, mapping.document.contents
);
}sourcepub async fn reduce(self) -> Result<V::Value, Error>
pub async 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 = db.view::<ScoresByRank>().reduce().await?;
println!("Average score: {:3}", score);sourcepub async fn reduce_grouped(
self
) -> Result<Vec<MappedValue<V::Key, V::Value>>, Error>
pub async fn reduce_grouped(
self
) -> Result<Vec<MappedValue<V::Key, V::Value>>, Error>
Executes a reduce over the results of the query, grouping by key.
// score is an f32 in this example
for mapping in db.view::<ScoresByRank>().reduce_grouped().await? {
println!(
"Rank {} has an average score of {:3}",
mapping.key, mapping.value
);
}sourcepub async fn delete_docs(self) -> Result<u64, Error>
pub async fn delete_docs(self) -> Result<u64, Error>
Deletes all of the associated documents that match this view query.
db.view::<ScoresByRank>().delete_docs().await?;Auto Trait Implementations
impl<'a, Cn, V> RefUnwindSafe for View<'a, Cn, V> where
Cn: RefUnwindSafe,
<V as View>::Key: RefUnwindSafe,
impl<'a, Cn, V> Send for View<'a, Cn, V> where
Cn: Sync,
impl<'a, Cn, V> Sync for View<'a, Cn, V> where
Cn: Sync,
impl<'a, Cn, V> Unpin for View<'a, Cn, V> where
<V as View>::Key: Unpin,
impl<'a, Cn, V> UnwindSafe for View<'a, Cn, V> where
Cn: RefUnwindSafe,
<V as View>::Key: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more