Struct bonsaidb::core::connection::View [−][src]
pub struct View<'a, Cn, V, Key> where
V: SerializedView, {
pub key: Option<QueryKey<Key>>,
pub access_policy: AccessPolicy,
pub sort: Sort,
pub limit: Option<u32>,
// some fields omitted
}
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<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
impl<'a, Cn, V, Key> View<'a, Cn, V, Key> where
V: SerializedView,
Cn: Connection,
Key: for<'k> KeyEncoding<'k, <V as View>::Key>,
impl<'a, Cn, V, Key> View<'a, Cn, V, Key> where
V: SerializedView,
Cn: Connection,
Key: for<'k> KeyEncoding<'k, <V as View>::Key>,
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()? {
assert_eq!(mapping.key, 42);
println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}
pub fn with_keys<K, IntoIter>(self, keys: IntoIter) -> View<'a, Cn, V, K> where
IntoIter: IntoIterator<Item = K>,
pub fn with_keys<K, IntoIter>(self, keys: IntoIter) -> View<'a, Cn, V, K> where
IntoIter: IntoIterator<Item = K>,
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()? {
println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}
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()? {
assert!(mapping.key >= 42);
println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}
pub fn with_key_prefix(
self,
prefix: <V as View>::Key
) -> View<'a, Cn, V, <V as View>::Key> where
<V as View>::Key: IntoPrefixRange,
pub fn with_key_prefix(
self,
prefix: <V as View>::Key
) -> View<'a, Cn, V, <V as View>::Key> where
<V as View>::Key: IntoPrefixRange,
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 db
.view::<ByName>()
.with_key_prefix(String::from("a"))
.query()?
{
assert!(mapping.key.starts_with("a"));
println!("{} in document {:?}", mapping.key, mapping.source);
}
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()?
{
println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}
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()? {
println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}
Queries the view in descending order.
// score is an f32 in this example
for mapping in db.view::<ScoresByRank>().descending().query()? {
println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}
Sets the maximum number of results to return.
// score is an f32 in this example
let mappings = db.view::<ScoresByRank>().limit(10).query()?;
assert!(mappings.len() <= 10);
Executes the query and retrieves the results.
// score is an f32 in this example
for mapping in db.view::<ScoresByRank>().query()? {
println!("Rank {} has a score of {:3}", mapping.key, mapping.value);
}
Executes the query and retrieves the results with the associated Document
s.
for mapping in &db
.view::<ScoresByRank>()
.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
);
}
pub fn query_with_collection_docs(
self
) -> Result<MappedDocuments<CollectionDocument<<V as View>::Collection>, V>, Error> where
<V as View>::Collection: SerializedCollection,
<<V as View>::Collection as SerializedCollection>::Contents: Debug,
pub fn query_with_collection_docs(
self
) -> Result<MappedDocuments<CollectionDocument<<V as View>::Collection>, V>, Error> where
<V as View>::Collection: SerializedCollection,
<<V as View>::Collection as SerializedCollection>::Contents: Debug,
Executes the query and retrieves the results with the associated CollectionDocument
s.
for mapping in &db
.view::<ScoresByRank>()
.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
);
}
Executes a reduce over the results of the query
// score is an f32 in this example
let score = db.view::<ScoresByRank>().reduce()?;
println!("Average score: {:3}", score);
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()? {
println!(
"Rank {} has an average score of {:3}",
mapping.key, mapping.value
);
}
Deletes all of the associated documents that match this view query.
db.view::<ScoresByRank>().delete_docs()?;
Auto Trait Implementations
impl<'a, Cn, V, Key> RefUnwindSafe for View<'a, Cn, V, Key> where
Cn: RefUnwindSafe,
Key: RefUnwindSafe,
V: RefUnwindSafe,
impl<'a, Cn, V, Key> UnwindSafe for View<'a, Cn, V, Key> where
Cn: RefUnwindSafe,
Key: UnwindSafe,
V: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more