Skip to main content

Query

Struct Query 

Source
pub struct Query<'db, T: Document> { /* private fields */ }
Expand description

The M8 query builder.

Obtain via Db::query. Compose with Query::filter, Query::limit, and Query::index_range; terminate with Query::fetch.

Query borrows &'db Db so multiple builders can coexist; the borrow ends when .fetch() returns. The actual scan runs inside a fresh read_transaction opened by .fetch() — the builder itself holds no locks.

Implementations§

Source§

impl<'db, T> Query<'db, T>
where T: Send + 'static + Document,

Source

pub fn filter<F>(self, predicate: F) -> Self
where F: Fn(&T) -> bool + 'static,

Append a filter predicate. Filters compose with AND — every predicate must return true for a doc to be emitted.

'static is required so the closure can outlive the temporary builder; capture by value if you need to borrow a stack-local value.

Source

pub fn limit(self, n: usize) -> Self

Cap the result set at n documents. Order is the source order (primary Id for full-scan; index-key bytes for an index range).

Source

pub fn index_range<R>(self, name: &str, range: R) -> Result<Self>
where R: RangeBounds<Dynamic>,

Switch the query source from full-scan to the named index’s range. Bounds are Dynamic values; the builder encodes them through obj_core::index::encode_field at call time so the actual range arithmetic sees byte-ordered keys.

§Errors
  • obj_core::Error::Codec if a Dynamic::String bound carries an embedded NUL byte (the order-preserving encoder rejects those — see obj_core::index::encode_field).
Source

pub fn sort_by<F>(self, key: F) -> Self
where F: Fn(&T) -> Dynamic + 'static,

Sort the result by key’s output in ascending key order.

key returns an obj_core::codec::Dynamic for each document; the builder runs each value through obj_core::index::encode_field (M7 #55) so the comparator is a byte comparison whose ordering matches the value’s natural Ord. This reuses the same order-preserving encoder the index layer uses, so a .sort_by(|o| o.placed_at.into()) produces the same ordering an index_range("placed_at", ...) scan would visit.

Last-call-wins: a second .sort_by (or .sort_by_bytes) overwrites the first; the two extractors share the same internal slot because they are mutually exclusive.

The sort runs BEFORE Query::limit truncation, so .sort_by(...).limit(N) returns the N smallest by the extractor’s key — the “top-N sorted” workload design.md shows for the Orders example.

§Errors at fetch time

sort_by itself is infallible — it stores the closure. The encoder runs during Query::fetch; a Dynamic whose encode_field representation cannot be computed (e.g. a Dynamic::String carrying an embedded 0x00 byte that the order-preserving encoder rejects) surfaces as Error::SortKeyEncode — power-of-ten Rule 7 (no silent errors). Callers who want to bypass encode_field entirely should use Query::sort_by_bytes.

§Sort-buffer bound

The pre-sort buffer is capped at Query::sort_buffer_limit (default MAX_SORT_BUFFER = 100 000). A scan that produces more candidates surfaces Error::SortBufferExceeded; the user should narrow the source via .filter / .index_range / .limit, or raise the cap with .sort_buffer_limit(N).

Source

pub fn sort_by_bytes<F>(self, key: F) -> Self
where F: Fn(&T) -> Vec<u8> + 'static,

Sort the result by key’s raw byte output in ascending order.

Companion to Query::sort_by that lets callers supply the already-encoded sort bytes. The byte-order = sort-order invariant is the caller’s responsibility: two documents whose key bytes compare Less MUST also be in the desired sort order, otherwise the produced ordering is unspecified.

Bypassing obj_core::index::encode_field means Error::SortKeyEncode cannot fire — sort_by_bytes is the right shape for callers who already have an order-preserving byte form (e.g. a precomputed i64::to_be_bytes of a signed counter that they have already biased to the unsigned range).

Last-call-wins: sort_by_bytes overwrites a prior Query::sort_by (and vice versa).

§Sort-buffer bound

Same bound as Query::sort_by — see that method’s docs.

Source

pub fn sort_buffer_limit(self, n: usize) -> Self

Override the per-query sort-buffer ceiling. Only consulted when Query::sort_by is set. Defaults to MAX_SORT_BUFFER (100 000).

Source

pub fn fetch(self) -> Result<Vec<T>>

Execute the query and materialise the matching documents.

Opens a fresh read_transaction, drives the configured source iterator, applies every filter in declaration order, and truncates to limit (if set). Returns the documents in source order.

§Errors
Source

pub fn count(&self) -> Result<u64>

Count the documents this query would return, ignoring Query::sort_by (sort does not change the count) but honouring Query::limit (returns min(total, limit)).

Takes &self rather than consuming the builder so callers can chain a follow-up .fetch() on the same predicate set.

§Fast path (no filter set)

When no filter is set, count walks the source B-tree without decoding any document. The exact shape of the walk depends on the source’s index kind so the answer matches what fetch would return:

  • Full scan — walks the primary B-tree counting entries (Collection::count_all). One entry per doc; the count is exact.
  • Standard / Unique / Composite index_range — walks the named index’s B-tree counting entries (Collection::count_index_range). One entry per doc; the count is exact.
  • Each index_range — walks the named index’s B-tree tracking distinct trailing-id suffixes via Collection::count_distinct_ids_in_range (M8 follow-up #72). A single doc may emit multiple entries under different element keys; the entry count would overshoot. The distinct set is bounded by crate::MAX_DISTINCT_IDS (100 000); exceeding it surfaces obj_core::Error::DistinctCountExceeded.
§Slow path (filter set)

When at least one filter is set, the predicate has to see a decoded T, so the slow path pays the per-doc decode cost (same as fetch). Sort, if set, is ignored — the total count is the same.

§Errors

Auto Trait Implementations§

§

impl<'db, T> Freeze for Query<'db, T>

§

impl<'db, T> !RefUnwindSafe for Query<'db, T>

§

impl<'db, T> !Send for Query<'db, T>

§

impl<'db, T> !Sync for Query<'db, T>

§

impl<'db, T> Unpin for Query<'db, T>

§

impl<'db, T> UnsafeUnpin for Query<'db, T>

§

impl<'db, T> !UnwindSafe for Query<'db, T>

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>,

Source§

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>,

Source§

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.
Source§

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

Source§

fn vzip(self) -> V