Skip to main content

PointIndex

Struct PointIndex 

Source
pub struct PointIndex<Tbl: Table, IndexType, Idx: Index> { /* private fields */ }
Expand description

A handle to a Hash index on a table.

To get one of these from a ReducerContext, use:

ctx.db.{table}().{index}()

for a table table and an index index.

Example:

use spacetimedb::{table, DbContext, PointIndex, ReducerContext};

#[table(accessor = user,
    index(accessor = dogs_and_name, hash(columns = [dogs, name])))]
struct User {
    id: u32,
    name: String,
    /// Number of dogs owned by the user.
    dogs: u64
}

fn demo(ctx: &ReducerContext) {
    let by_dogs_and_name: PointIndex<_, (u64, String), _> = ctx.db.user().dogs_and_name();
}

For single-column indexes, use the name of the column:

use spacetimedb::{table, DbContext, RangedIndex, ReducerContext};

#[table(accessor = user)]
struct User {
    id: u32,
    username: String,
    #[index(btree)]
    dogs: u64
}

fn demo(ctx: &ReducerContext) {
    let by_dogs: RangedIndex<_, (u64,), _> = ctx.db().user().dogs();
}

Implementations§

Source§

impl<Tbl: Table, IndexType, Idx: IndexIsPointed> PointIndex<Tbl, IndexType, Idx>

Source

pub fn filter<P, K>( &self, point: P, ) -> impl Iterator<Item = Tbl::Row> + use<P, K, Tbl, IndexType, Idx>
where P: WithPointArg<K>,

Returns an iterator over all rows in the database state where the indexed column(s) equal point.

Unlike for ranged indices, this method only accepts a point and not any prefix or range.

For example:

use spacetimedb::{table, ReducerContext, PointIndex};

#[table(accessor = user,
    index(accessor = dogs_and_name, hash(columns = [dogs, name])))]
struct User {
    id: u32,
    name: String,
    dogs: u64
}

fn demo(ctx: &ReducerContext) {
    let by_dogs_and_name: PointIndex<_, (u64, String), _> = ctx.db.user().dogs_and_name();

    // Find user with exactly 25 dogs and exactly the name "Joseph".
    for user in by_dogs_and_name.filter((25u64, "Joseph")) {
        /* ... */
    }

    // You can also pass arguments by reference if desired.
    for user in by_dogs_and_name.filter((&25u64, &"Joseph".to_string())) {
        /* ... */
    }
}
Source

pub fn delete<P, K>(&self, point: P) -> u64
where P: WithPointArg<K>,

Deletes all rows in the database state where the indexed column(s) equal point.

Unlike for ranged indices, this method only accepts a point and not any prefix or range.

For example:

use spacetimedb::{table, ReducerContext, PointIndex};

#[table(accessor = user,
    index(accessor = dogs_and_name, hash(columns = [dogs, name])))]
struct User {
    id: u32,
    name: String,
    dogs: u64
}

fn demo(ctx: &ReducerContext) {
    let by_dogs_and_name: PointIndex<_, (u64, String), _> = ctx.db.user().dogs_and_name();

    // Delete users with exactly 25 dogs, and exactly the name "Joseph".
    by_dogs_and_name.delete((25u64, "Joseph"));

    // You can also pass arguments by reference if desired.
    by_dogs_and_name.delete((&25u64, &"Joseph".to_string()));
}

May panic if deleting any one of the rows would violate a constraint, though at present no such constraints exist.

Auto Trait Implementations§

§

impl<Tbl, IndexType, Idx> Freeze for PointIndex<Tbl, IndexType, Idx>

§

impl<Tbl, IndexType, Idx> RefUnwindSafe for PointIndex<Tbl, IndexType, Idx>
where Tbl: RefUnwindSafe, IndexType: RefUnwindSafe, Idx: RefUnwindSafe,

§

impl<Tbl, IndexType, Idx> Send for PointIndex<Tbl, IndexType, Idx>
where Tbl: Send, IndexType: Send, Idx: Send,

§

impl<Tbl, IndexType, Idx> Sync for PointIndex<Tbl, IndexType, Idx>
where Tbl: Sync, IndexType: Sync, Idx: Sync,

§

impl<Tbl, IndexType, Idx> Unpin for PointIndex<Tbl, IndexType, Idx>
where Tbl: Unpin, IndexType: Unpin, Idx: Unpin,

§

impl<Tbl, IndexType, Idx> UnsafeUnpin for PointIndex<Tbl, IndexType, Idx>

§

impl<Tbl, IndexType, Idx> UnwindSafe for PointIndex<Tbl, IndexType, Idx>
where Tbl: UnwindSafe, IndexType: UnwindSafe, Idx: UnwindSafe,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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