Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/bench/src/spacetime_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl BenchDatabase for SpacetimeRaw {
// (update_by_{field} -> spacetimedb::query::update_by_field -> (delete_by_col_eq; insert))
let id = self
.db
.iter_by_col_eq_mut(&ctx, tx, *table_id, ColId(0), row.elements[0].clone())?
.iter_by_col_eq_mut(&ctx, tx, *table_id, ColId(0), &row.elements[0])?
.next()
.expect("failed to find row during update!")
.pointer();
Expand Down Expand Up @@ -151,7 +151,7 @@ impl BenchDatabase for SpacetimeRaw {
let col: ColId = column_index.into();
let ctx = ExecutionContext::default();
self.db.with_auto_commit(&ctx, |tx| {
for row in self.db.iter_by_col_eq_mut(&ctx, tx, *table_id, col, value)? {
for row in self.db.iter_by_col_eq_mut(&ctx, tx, *table_id, col, &value)? {
black_box(row);
}
Ok(())
Expand Down
32 changes: 16 additions & 16 deletions crates/core/src/db/datastore/locking_tx_datastore/datastore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl Tx for Locking {

impl TxDatastore for Locking {
type Iter<'a> = Iter<'a> where Self: 'a;
type IterByColEq<'a> = IterByColRange<'a, AlgebraicValue> where Self: 'a;
type IterByColEq<'a, 'r> = IterByColRange<'a, &'r AlgebraicValue> where Self: 'a;
type IterByColRange<'a, R: RangeBounds<AlgebraicValue>> = IterByColRange<'a, R> where Self: 'a;

fn iter_tx<'a>(&'a self, ctx: &'a ExecutionContext, tx: &'a Self::Tx, table_id: TableId) -> Result<Self::Iter<'a>> {
Expand All @@ -267,14 +267,14 @@ impl TxDatastore for Locking {
tx.iter_by_col_range(ctx, &table_id, cols.into(), range)
}

fn iter_by_col_eq_tx<'a>(
fn iter_by_col_eq_tx<'a, 'r>(
&'a self,
ctx: &'a ExecutionContext,
tx: &'a Self::Tx,
table_id: TableId,
cols: impl Into<ColList>,
value: AlgebraicValue,
) -> Result<Self::IterByColEq<'a>> {
value: &'r AlgebraicValue,
) -> Result<Self::IterByColEq<'a, 'r>> {
tx.iter_by_col_eq(ctx, &table_id, cols.into(), value)
}

Expand Down Expand Up @@ -413,14 +413,14 @@ impl MutTxDatastore for Locking {
tx.iter_by_col_range(ctx, &table_id, cols.into(), range)
}

fn iter_by_col_eq_mut_tx<'a>(
fn iter_by_col_eq_mut_tx<'a, 'r>(
&'a self,
ctx: &'a ExecutionContext,
tx: &'a Self::MutTx,
table_id: TableId,
cols: impl Into<ColList>,
value: AlgebraicValue,
) -> Result<Self::IterByColEq<'a>> {
value: &'r AlgebraicValue,
) -> Result<Self::IterByColEq<'a, 'r>> {
tx.iter_by_col_eq(ctx, &table_id, cols.into(), value)
}

Expand Down Expand Up @@ -669,7 +669,7 @@ mod tests {
pub fn scan_st_tables_by_col(
&self,
cols: impl Into<ColList>,
value: AlgebraicValue,
value: &AlgebraicValue,
) -> Result<Vec<StTableRow<String>>> {
Ok(self
.db
Expand All @@ -691,7 +691,7 @@ mod tests {
pub fn scan_st_columns_by_col(
&self,
cols: impl Into<ColList>,
value: AlgebraicValue,
value: &AlgebraicValue,
) -> Result<Vec<StColumnRow<String>>> {
Ok(self
.db
Expand Down Expand Up @@ -1136,12 +1136,12 @@ mod tests {
let ctx = ExecutionContext::default();
let query = query_st_tables(&ctx, &tx);

let table_rows = query.scan_st_tables_by_col(ColId(0), table_id.into())?;
let table_rows = query.scan_st_tables_by_col(ColId(0), &table_id.into())?;
#[rustfmt::skip]
assert_eq!(table_rows, map_array([
TableRow { id: 6, name: "Foo", ty: StTableType::User, access: StAccess::Public }
]));
let column_rows = query.scan_st_columns_by_col(ColId(0), table_id.into())?;
let column_rows = query.scan_st_columns_by_col(ColId(0), &table_id.into())?;
#[rustfmt::skip]
assert_eq!(column_rows, map_array(basic_table_schema_cols()));
Ok(())
Expand All @@ -1155,12 +1155,12 @@ mod tests {
let ctx = ExecutionContext::default();
let query = query_st_tables(&ctx, &tx);

let table_rows = query.scan_st_tables_by_col(ColId(0), table_id.into())?;
let table_rows = query.scan_st_tables_by_col(ColId(0), &table_id.into())?;
#[rustfmt::skip]
assert_eq!(table_rows, map_array([
TableRow { id: 6, name: "Foo", ty: StTableType::User, access: StAccess::Public }
]));
let column_rows = query.scan_st_columns_by_col(ColId(0), table_id.into())?;
let column_rows = query.scan_st_columns_by_col(ColId(0), &table_id.into())?;
#[rustfmt::skip]
assert_eq!(column_rows, map_array(basic_table_schema_cols()));

Expand All @@ -1179,9 +1179,9 @@ mod tests {
let ctx = ExecutionContext::default();
let query = query_st_tables(&ctx, &tx);

let table_rows = query.scan_st_tables_by_col(ColId(0), table_id.into())?;
let table_rows = query.scan_st_tables_by_col(ColId(0), &table_id.into())?;
assert_eq!(table_rows, []);
let column_rows = query.scan_st_columns_by_col(ColId(0), table_id.into())?;
let column_rows = query.scan_st_columns_by_col(ColId(0), &table_id.into())?;
assert_eq!(column_rows, []);
Ok(())
}
Expand Down Expand Up @@ -1565,7 +1565,7 @@ mod tests {
tx,
table_id,
ColId(0),
AlgebraicValue::U32(1),
&AlgebraicValue::U32(1),
)
.unwrap()
.map(|row_ref| row_ref.to_product_value())
Expand Down
28 changes: 14 additions & 14 deletions crates/core/src/db/datastore/locking_tx_datastore/mut_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl MutTxId {
&mut self,
table_id: TableId,
col_pos: ColId,
value: AlgebraicValue,
value: &AlgebraicValue,
database_address: Address,
) -> Result<()> {
let ctx = ExecutionContext::internal(database_address);
Expand Down Expand Up @@ -217,13 +217,13 @@ impl MutTxId {
self.drop_col_eq(
ST_TABLES_ID,
StTableFields::TableId.col_id(),
table_id.into(),
&table_id.into(),
database_address,
)?;
self.drop_col_eq(
ST_COLUMNS_ID,
StColumnFields::TableId.col_id(),
table_id.into(),
&table_id.into(),
database_address,
)?;

Expand All @@ -244,7 +244,7 @@ impl MutTxId {
&ctx,
&ST_TABLES_ID,
StTableFields::TableId.col_id().into(),
table_id.into(),
&table_id.into(),
)?
.next()
.ok_or_else(|| TableError::IdNotFound(SystemTable::st_table, table_id.into()))?;
Expand All @@ -260,15 +260,15 @@ impl MutTxId {

pub fn table_id_from_name(&self, table_name: &str, database_address: Address) -> Result<Option<TableId>> {
let ctx = ExecutionContext::internal(database_address);
let table_name = table_name.to_owned().into();
let table_name = &table_name.to_owned().into();
let row = self
.iter_by_col_eq(&ctx, &ST_TABLES_ID, StTableFields::TableName.into(), table_name)?
.next();
Ok(row.map(|row| row.read_col(StTableFields::TableId).unwrap()))
}

pub fn table_name_from_id<'a>(&'a self, ctx: &'a ExecutionContext, table_id: TableId) -> Result<Option<String>> {
self.iter_by_col_eq(ctx, &ST_TABLES_ID, StTableFields::TableId.into(), table_id.into())
self.iter_by_col_eq(ctx, &ST_TABLES_ID, StTableFields::TableId.into(), &table_id.into())
.map(|mut iter| iter.next().map(|row| row.read_col(StTableFields::TableName).unwrap()))
}

Expand Down Expand Up @@ -370,7 +370,7 @@ impl MutTxId {
&ctx,
&ST_INDEXES_ID,
StIndexFields::IndexId.col_id().into(),
index_id.into(),
&index_id.into(),
)?
.next()
.ok_or_else(|| TableError::IdNotFound(SystemTable::st_indexes, index_id.into()))?;
Expand Down Expand Up @@ -407,7 +407,7 @@ impl MutTxId {

pub fn index_id_from_name(&self, index_name: &str, database_address: Address) -> Result<Option<IndexId>> {
let ctx = ExecutionContext::internal(database_address);
let name = index_name.to_owned().into();
let name = &index_name.to_owned().into();
self.iter_by_col_eq(&ctx, &ST_INDEXES_ID, StIndexFields::IndexName.into(), name)
.map(|mut iter| iter.next().map(|row| row.read_col(StIndexFields::IndexId).unwrap()))
}
Expand All @@ -432,7 +432,7 @@ impl MutTxId {
&ctx,
&ST_SEQUENCES_ID,
StSequenceFields::SequenceId.into(),
seq_id.into(),
&seq_id.into(),
)?
.last()
.unwrap();
Expand Down Expand Up @@ -509,7 +509,7 @@ impl MutTxId {
&ctx,
&ST_SEQUENCES_ID,
StSequenceFields::SequenceId.col_id().into(),
sequence_id.into(),
&sequence_id.into(),
)?
.next()
.ok_or_else(|| TableError::IdNotFound(SystemTable::st_sequence, sequence_id.into()))?;
Expand All @@ -530,7 +530,7 @@ impl MutTxId {

pub fn sequence_id_from_name(&self, seq_name: &str, database_address: Address) -> Result<Option<SequenceId>> {
let ctx = ExecutionContext::internal(database_address);
let name = seq_name.to_owned().into();
let name = &seq_name.to_owned().into();
self.iter_by_col_eq(&ctx, &ST_SEQUENCES_ID, StSequenceFields::SequenceName.into(), name)
.map(|mut iter| {
iter.next()
Expand Down Expand Up @@ -604,7 +604,7 @@ impl MutTxId {
&ctx,
&ST_CONSTRAINTS_ID,
StConstraintFields::ConstraintId.col_id().into(),
constraint_id.into(),
&constraint_id.into(),
)?
.next()
.ok_or_else(|| TableError::IdNotFound(SystemTable::st_constraints, constraint_id.into()))?;
Expand All @@ -629,7 +629,7 @@ impl MutTxId {
&ExecutionContext::internal(database_address),
&ST_CONSTRAINTS_ID,
StConstraintFields::ConstraintName.into(),
constraint_name.to_owned().into(),
&constraint_name.to_owned().into(),
)
.map(|mut iter| {
iter.next()
Expand Down Expand Up @@ -738,7 +738,7 @@ impl MutTxId {
&ctx,
&ST_SEQUENCES_ID,
StSequenceFields::TableId.into(),
table_id.into(),
&table_id.into(),
)? {
let seq_col_pos: ColId = seq_row.read_col(StSequenceFields::ColPos)?;
if seq_col_pos == seq.col_pos {
Expand Down
22 changes: 11 additions & 11 deletions crates/core/src/db/datastore/locking_tx_datastore/state_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) trait StateView {

fn table_id_from_name(&self, table_name: &str, database_address: Address) -> Result<Option<TableId>> {
let ctx = ExecutionContext::internal(database_address);
let name = table_name.to_owned().into();
let name = &table_name.to_owned().into();
let row = self
.iter_by_col_eq(&ctx, &ST_TABLES_ID, StTableFields::TableName.col_id().into(), name)?
.next();
Expand All @@ -52,23 +52,23 @@ pub(crate) trait StateView {
range: R,
) -> Result<IterByColRange<'a, R>>;

fn iter_by_col_eq<'a>(
fn iter_by_col_eq<'a, 'r>(
&'a self,
ctx: &'a ExecutionContext,
table_id: &TableId,
cols: ColList,
value: AlgebraicValue,
) -> Result<IterByColEq<'_>> {
value: &'r AlgebraicValue,
) -> Result<IterByColEq<'a, 'r>> {
self.iter_by_col_range(ctx, table_id, cols, value)
}

/// Reads the schema information for the specified `table_id` directly from the database.
fn schema_for_table_raw(&self, ctx: &ExecutionContext, table_id: TableId) -> Result<TableSchema> {
// Look up the table_name for the table in question.
let st_table_table_id_col = StTableFields::TableId.col_id().into();
let value: AlgebraicValue = table_id.into();
let value_eq = &table_id.into();
let row = self
.iter_by_col_eq(ctx, &ST_TABLES_ID, st_table_table_id_col, table_id.into())?
.iter_by_col_eq(ctx, &ST_TABLES_ID, st_table_table_id_col, value_eq)?
.next()
.ok_or_else(|| TableError::IdNotFound(SystemTable::st_table, table_id.into()))?;
let row = StTableRow::try_from(row)?;
Expand All @@ -80,7 +80,7 @@ pub(crate) trait StateView {
// Look up the columns for the table in question.
let st_columns_table_id_col = StColumnFields::TableId.col_id().into();
let mut columns = self
.iter_by_col_eq(ctx, &ST_COLUMNS_ID, st_columns_table_id_col, value)?
.iter_by_col_eq(ctx, &ST_COLUMNS_ID, st_columns_table_id_col, value_eq)?
.map(|row| {
let row = StColumnRow::try_from(row)?;
Ok(ColumnSchema {
Expand All @@ -96,7 +96,7 @@ pub(crate) trait StateView {
// Look up the constraints for the table in question.
let st_constraints_table_id = StConstraintFields::TableId.col_id().into();
let constraints = self
.iter_by_col_eq(ctx, &ST_CONSTRAINTS_ID, st_constraints_table_id, table_id.into())?
.iter_by_col_eq(ctx, &ST_CONSTRAINTS_ID, st_constraints_table_id, value_eq)?
.map(|row| {
let row = StConstraintRow::try_from(row)?;
Ok(ConstraintSchema {
Expand All @@ -112,7 +112,7 @@ pub(crate) trait StateView {
// Look up the sequences for the table in question.
let st_seq_table_id = StSequenceFields::TableId.col_id().into();
let sequences = self
.iter_by_col_eq(ctx, &ST_SEQUENCES_ID, st_seq_table_id, table_id.into())?
.iter_by_col_eq(ctx, &ST_SEQUENCES_ID, st_seq_table_id, value_eq)?
.map(|row| {
let row = StSequenceRow::try_from(row)?;
Ok(SequenceSchema {
Expand All @@ -132,7 +132,7 @@ pub(crate) trait StateView {
// Look up the indexes for the table in question.
let st_idx_table_id = StIndexFields::TableId.col_id().into();
let indexes = self
.iter_by_col_eq(ctx, &ST_INDEXES_ID, st_idx_table_id, table_id.into())?
.iter_by_col_eq(ctx, &ST_INDEXES_ID, st_idx_table_id, value_eq)?
.map(|row| {
let row = StIndexRow::try_from(row)?;
Ok(IndexSchema {
Expand Down Expand Up @@ -410,7 +410,7 @@ impl<'a> Iterator for IndexSeekIterMutTxId<'a> {
}

/// An [IterByColRange] for an individual column value.
pub type IterByColEq<'a> = IterByColRange<'a, AlgebraicValue>;
pub type IterByColEq<'a, 'r> = IterByColRange<'a, &'r AlgebraicValue>;

/// An iterator for a range of values in a column.
pub enum IterByColRange<'a, R: RangeBounds<AlgebraicValue>> {
Expand Down
14 changes: 7 additions & 7 deletions crates/core/src/db/datastore/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ pub trait TxDatastore: DataRow + Tx {
where
Self: 'a;

type IterByColEq<'a>: Iterator<Item = Self::RowRef<'a>>
type IterByColEq<'a, 'r>: Iterator<Item = Self::RowRef<'a>>
where
Self: 'a;

Expand All @@ -245,14 +245,14 @@ pub trait TxDatastore: DataRow + Tx {
range: R,
) -> Result<Self::IterByColRange<'a, R>>;

fn iter_by_col_eq_tx<'a>(
fn iter_by_col_eq_tx<'a, 'r>(
&'a self,
ctx: &'a ExecutionContext,
tx: &'a Self::Tx,
table_id: TableId,
cols: impl Into<ColList>,
value: AlgebraicValue,
) -> Result<Self::IterByColEq<'a>>;
value: &'r AlgebraicValue,
) -> Result<Self::IterByColEq<'a, 'r>>;

fn table_id_exists_tx(&self, tx: &Self::Tx, table_id: &TableId) -> bool;
fn table_id_from_name_tx(&self, tx: &Self::Tx, table_name: &str) -> Result<Option<TableId>>;
Expand Down Expand Up @@ -332,14 +332,14 @@ pub trait MutTxDatastore: TxDatastore + MutTx {
cols: impl Into<ColList>,
range: R,
) -> Result<Self::IterByColRange<'a, R>>;
fn iter_by_col_eq_mut_tx<'a>(
fn iter_by_col_eq_mut_tx<'a, 'r>(
&'a self,
ctx: &'a ExecutionContext,
tx: &'a Self::MutTx,
table_id: TableId,
cols: impl Into<ColList>,
value: AlgebraicValue,
) -> Result<Self::IterByColEq<'a>>;
value: &'r AlgebraicValue,
) -> Result<Self::IterByColEq<'a, 'r>>;
fn get_mut_tx<'a>(
&self,
tx: &'a Self::MutTx,
Expand Down
Loading