Skip to content

Commit

Permalink
feat(260): Add index scans for equality predicates to query engine (#295
Browse files Browse the repository at this point in the history
)

Fixes #260.
  • Loading branch information
joshua-spacetime authored Sep 16, 2023
1 parent 1bfe4c5 commit 4490bea
Show file tree
Hide file tree
Showing 14 changed files with 482 additions and 132 deletions.
3 changes: 3 additions & 0 deletions crates/core/src/database_instance_context_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ impl DatabaseInstanceContextController {
Self::default()
}

#[tracing::instrument(skip_all)]
pub fn get(&self, database_instance_id: u64) -> Option<(Arc<DatabaseInstanceContext>, Scheduler)> {
let contexts = self.contexts.lock().unwrap();
contexts.get(&database_instance_id).cloned()
}

#[tracing::instrument(skip_all)]
pub fn insert(&self, database_instance_context: Arc<DatabaseInstanceContext>, scheduler: Scheduler) {
let database_instance_id = database_instance_context.database_instance_id;
let mut contexts = self.contexts.lock().unwrap();
contexts.insert(database_instance_id, (database_instance_context, scheduler));
}

#[tracing::instrument(skip_all)]
pub fn remove(&self, database_instance_id: u64) -> Option<(Arc<DatabaseInstanceContext>, Scheduler)> {
let mut contexts = self.contexts.lock().unwrap();
contexts.remove(&database_instance_id)
Expand Down
15 changes: 14 additions & 1 deletion crates/core/src/db/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::error::DBError;
use spacetimedb_lib::relation::{DbTable, RowCount};
use spacetimedb_sats::ProductValue;

use super::datastore::locking_tx_datastore::Iter;
use super::datastore::locking_tx_datastore::{Iter, IterByColEq};

#[derive(Debug, Clone, Copy)]
pub enum CatalogKind {
Expand All @@ -24,6 +24,19 @@ impl<'a> TableCursor<'a> {
}
}

/// A relational iterator wrapping a storage level index iterator.
/// A relational iterator returns [RelValue]s whereas storage iterators return [DataRef]s.
pub struct IndexCursor<'a> {
pub table: DbTable,
pub iter: IterByColEq<'a>,
}

impl<'a> IndexCursor<'a> {
pub fn new(table: DbTable, iter: IterByColEq<'a>) -> Result<Self, DBError> {
Ok(Self { table, iter })
}
}

/// Common wrapper for relational iterators of [Catalog].
pub struct CatalogCursor<I> {
pub(crate) table: DbTable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl BTreeIndex {
///
/// For a unique index this will always yield at most one `RowId`.
#[tracing::instrument(skip_all)]
pub(crate) fn seek<'a>(&'a self, value: &'a AlgebraicValue) -> BTreeIndexRangeIter<'a> {
pub(crate) fn seek<'a>(&'a self, value: &AlgebraicValue) -> BTreeIndexRangeIter<'a> {
let k_start = IndexKey::from_row(value, DataKey::min_datakey());
let k_end = IndexKey::from_row(value, DataKey::max_datakey());
BTreeIndexRangeIter {
Expand Down
Loading

0 comments on commit 4490bea

Please sign in to comment.