Trait bonsaidb_core::schema::view::ViewSchema
source · pub trait ViewSchema: Send + Sync + Debug + 'static {
type View: SerializedView;
fn map(&self, document: &BorrowedDocument<'_>) -> ViewMapResult<Self::View>;
fn unique(&self) -> bool { ... }
fn lazy(&self) -> bool { ... }
fn version(&self) -> u64 { ... }
fn reduce(
&self,
mappings: &[ViewMappedValue<Self::View>],
rereduce: bool
) -> Result<<Self::View as View>::Value, Error> { ... }
}
Expand description
The implementation of Map/Reduce for a View
.
Views allow querying documents contained within Collection
s in
an efficient manner.
The ViewSchema::map()
function is responsible for “mapping” data from the
stored document into the Key
type. In the example below, the field rank
is
being used as the View’s Key
type, and the field score
is being
used as the View’s Value
The ViewSchema::reduce()
function is responsible for “reducing” a list of
Value
s into a single value. The example below implements
reduce()
to return an average value.
This enables querying the view in many powerful ways:
db.view::<MyView>().query().await
: Return all entries in the view (without the associated documents).db.view::<MyView>().query_with_docs().await
: Return all entries in the view, including the associatedOwnedDocument
s.db.view::<MyView>().query_with_collection_docs().await
: Return all entries in the view, including the associatedCollectionDocument<T>
s.db.view::<MyView>().reduce().await
: Returns the reduced value of the view query. For the example below, the result type of this call isf32
.db.view::<MyView>().reduce_grouped().await
: Returns the reduced value of the view query, grouped by key. For the example below, thevalue
returned will be anf32
that is the averagescore
of all documents with a matchingrank
.
All of the queries above can be filtered and customized by the methods available
on connection::View
.
For a more detailed walkthrough, see our user guide’s section on Views.
Required Associated Types§
sourcetype View: SerializedView
type View: SerializedView
The view this schema is defined for.
Required Methods§
sourcefn map(&self, document: &BorrowedDocument<'_>) -> ViewMapResult<Self::View>
fn map(&self, document: &BorrowedDocument<'_>) -> ViewMapResult<Self::View>
The map function for this view. This function is responsible for emitting entries for any documents that should be contained in this View. If None is returned, the View will not include the document. See the user guide’s chapter on views for more information on how map works.
Provided Methods§
sourcefn unique(&self) -> bool
fn unique(&self) -> bool
If true, no two documents may emit the same key. Unique views are
updated when the document is saved, allowing for this check to be done
atomically. When a document is updated, all unique views will be
updated, and if any of them fail, the document will not be allowed to
update and an
Error::UniqueKeyViolation
will be
returned.
sourcefn lazy(&self) -> bool
fn lazy(&self) -> bool
Returns whether this view should be lazily updated. If true, views will be updated only when accessed. If false, views will be updated during the transaction that is updating the affected documents.
sourcefn version(&self) -> u64
fn version(&self) -> u64
The version of the view. Changing this value will cause indexes to be rebuilt.
sourcefn reduce(
&self,
mappings: &[ViewMappedValue<Self::View>],
rereduce: bool
) -> Result<<Self::View as View>::Value, Error>
fn reduce(
&self,
mappings: &[ViewMappedValue<Self::View>],
rereduce: bool
) -> Result<<Self::View as View>::Value, Error>
Returns a value that is produced by reducing a list of mappings
into a
single value. If rereduce
is true, the values contained in the
mappings have already been reduced at least one time. If an error of
ReduceUnimplemented
is returned,
queries that ask for a reduce operation will return an error. See the
user guide’s chapter on views for more information on how reduce
works.