Skip to content

Commit f2a75f7

Browse files
authored
take AV by ref in iter_by_col_eq (#925)
1 parent 9afbfa7 commit f2a75f7

File tree

9 files changed

+91
-80
lines changed

9 files changed

+91
-80
lines changed

crates/bench/src/spacetime_raw.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl BenchDatabase for SpacetimeRaw {
108108
// (update_by_{field} -> spacetimedb::query::update_by_field -> (delete_by_col_eq; insert))
109109
let id = self
110110
.db
111-
.iter_by_col_eq_mut(&ctx, tx, *table_id, ColId(0), row.elements[0].clone())?
111+
.iter_by_col_eq_mut(&ctx, tx, *table_id, ColId(0), &row.elements[0])?
112112
.next()
113113
.expect("failed to find row during update!")
114114
.pointer();
@@ -151,7 +151,7 @@ impl BenchDatabase for SpacetimeRaw {
151151
let col: ColId = column_index.into();
152152
let ctx = ExecutionContext::default();
153153
self.db.with_auto_commit(&ctx, |tx| {
154-
for row in self.db.iter_by_col_eq_mut(&ctx, tx, *table_id, col, value)? {
154+
for row in self.db.iter_by_col_eq_mut(&ctx, tx, *table_id, col, &value)? {
155155
black_box(row);
156156
}
157157
Ok(())

crates/core/src/db/datastore/locking_tx_datastore/datastore.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl Tx for Locking {
249249

250250
impl TxDatastore for Locking {
251251
type Iter<'a> = Iter<'a> where Self: 'a;
252-
type IterByColEq<'a> = IterByColRange<'a, AlgebraicValue> where Self: 'a;
252+
type IterByColEq<'a, 'r> = IterByColRange<'a, &'r AlgebraicValue> where Self: 'a;
253253
type IterByColRange<'a, R: RangeBounds<AlgebraicValue>> = IterByColRange<'a, R> where Self: 'a;
254254

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

270-
fn iter_by_col_eq_tx<'a>(
270+
fn iter_by_col_eq_tx<'a, 'r>(
271271
&'a self,
272272
ctx: &'a ExecutionContext,
273273
tx: &'a Self::Tx,
274274
table_id: TableId,
275275
cols: impl Into<ColList>,
276-
value: AlgebraicValue,
277-
) -> Result<Self::IterByColEq<'a>> {
276+
value: &'r AlgebraicValue,
277+
) -> Result<Self::IterByColEq<'a, 'r>> {
278278
tx.iter_by_col_eq(ctx, &table_id, cols.into(), value)
279279
}
280280

@@ -413,14 +413,14 @@ impl MutTxDatastore for Locking {
413413
tx.iter_by_col_range(ctx, &table_id, cols.into(), range)
414414
}
415415

416-
fn iter_by_col_eq_mut_tx<'a>(
416+
fn iter_by_col_eq_mut_tx<'a, 'r>(
417417
&'a self,
418418
ctx: &'a ExecutionContext,
419419
tx: &'a Self::MutTx,
420420
table_id: TableId,
421421
cols: impl Into<ColList>,
422-
value: AlgebraicValue,
423-
) -> Result<Self::IterByColEq<'a>> {
422+
value: &'r AlgebraicValue,
423+
) -> Result<Self::IterByColEq<'a, 'r>> {
424424
tx.iter_by_col_eq(ctx, &table_id, cols.into(), value)
425425
}
426426

@@ -669,7 +669,7 @@ mod tests {
669669
pub fn scan_st_tables_by_col(
670670
&self,
671671
cols: impl Into<ColList>,
672-
value: AlgebraicValue,
672+
value: &AlgebraicValue,
673673
) -> Result<Vec<StTableRow<String>>> {
674674
Ok(self
675675
.db
@@ -691,7 +691,7 @@ mod tests {
691691
pub fn scan_st_columns_by_col(
692692
&self,
693693
cols: impl Into<ColList>,
694-
value: AlgebraicValue,
694+
value: &AlgebraicValue,
695695
) -> Result<Vec<StColumnRow<String>>> {
696696
Ok(self
697697
.db
@@ -1136,12 +1136,12 @@ mod tests {
11361136
let ctx = ExecutionContext::default();
11371137
let query = query_st_tables(&ctx, &tx);
11381138

1139-
let table_rows = query.scan_st_tables_by_col(ColId(0), table_id.into())?;
1139+
let table_rows = query.scan_st_tables_by_col(ColId(0), &table_id.into())?;
11401140
#[rustfmt::skip]
11411141
assert_eq!(table_rows, map_array([
11421142
TableRow { id: 6, name: "Foo", ty: StTableType::User, access: StAccess::Public }
11431143
]));
1144-
let column_rows = query.scan_st_columns_by_col(ColId(0), table_id.into())?;
1144+
let column_rows = query.scan_st_columns_by_col(ColId(0), &table_id.into())?;
11451145
#[rustfmt::skip]
11461146
assert_eq!(column_rows, map_array(basic_table_schema_cols()));
11471147
Ok(())
@@ -1155,12 +1155,12 @@ mod tests {
11551155
let ctx = ExecutionContext::default();
11561156
let query = query_st_tables(&ctx, &tx);
11571157

1158-
let table_rows = query.scan_st_tables_by_col(ColId(0), table_id.into())?;
1158+
let table_rows = query.scan_st_tables_by_col(ColId(0), &table_id.into())?;
11591159
#[rustfmt::skip]
11601160
assert_eq!(table_rows, map_array([
11611161
TableRow { id: 6, name: "Foo", ty: StTableType::User, access: StAccess::Public }
11621162
]));
1163-
let column_rows = query.scan_st_columns_by_col(ColId(0), table_id.into())?;
1163+
let column_rows = query.scan_st_columns_by_col(ColId(0), &table_id.into())?;
11641164
#[rustfmt::skip]
11651165
assert_eq!(column_rows, map_array(basic_table_schema_cols()));
11661166

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

1182-
let table_rows = query.scan_st_tables_by_col(ColId(0), table_id.into())?;
1182+
let table_rows = query.scan_st_tables_by_col(ColId(0), &table_id.into())?;
11831183
assert_eq!(table_rows, []);
1184-
let column_rows = query.scan_st_columns_by_col(ColId(0), table_id.into())?;
1184+
let column_rows = query.scan_st_columns_by_col(ColId(0), &table_id.into())?;
11851185
assert_eq!(column_rows, []);
11861186
Ok(())
11871187
}
@@ -1565,7 +1565,7 @@ mod tests {
15651565
tx,
15661566
table_id,
15671567
ColId(0),
1568-
AlgebraicValue::U32(1),
1568+
&AlgebraicValue::U32(1),
15691569
)
15701570
.unwrap()
15711571
.map(|row_ref| row_ref.to_product_value())

crates/core/src/db/datastore/locking_tx_datastore/mut_tx.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl MutTxId {
5959
&mut self,
6060
table_id: TableId,
6161
col_pos: ColId,
62-
value: AlgebraicValue,
62+
value: &AlgebraicValue,
6363
database_address: Address,
6464
) -> Result<()> {
6565
let ctx = ExecutionContext::internal(database_address);
@@ -217,13 +217,13 @@ impl MutTxId {
217217
self.drop_col_eq(
218218
ST_TABLES_ID,
219219
StTableFields::TableId.col_id(),
220-
table_id.into(),
220+
&table_id.into(),
221221
database_address,
222222
)?;
223223
self.drop_col_eq(
224224
ST_COLUMNS_ID,
225225
StColumnFields::TableId.col_id(),
226-
table_id.into(),
226+
&table_id.into(),
227227
database_address,
228228
)?;
229229

@@ -244,7 +244,7 @@ impl MutTxId {
244244
&ctx,
245245
&ST_TABLES_ID,
246246
StTableFields::TableId.col_id().into(),
247-
table_id.into(),
247+
&table_id.into(),
248248
)?
249249
.next()
250250
.ok_or_else(|| TableError::IdNotFound(SystemTable::st_table, table_id.into()))?;
@@ -260,15 +260,15 @@ impl MutTxId {
260260

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

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

@@ -370,7 +370,7 @@ impl MutTxId {
370370
&ctx,
371371
&ST_INDEXES_ID,
372372
StIndexFields::IndexId.col_id().into(),
373-
index_id.into(),
373+
&index_id.into(),
374374
)?
375375
.next()
376376
.ok_or_else(|| TableError::IdNotFound(SystemTable::st_indexes, index_id.into()))?;
@@ -407,7 +407,7 @@ impl MutTxId {
407407

408408
pub fn index_id_from_name(&self, index_name: &str, database_address: Address) -> Result<Option<IndexId>> {
409409
let ctx = ExecutionContext::internal(database_address);
410-
let name = index_name.to_owned().into();
410+
let name = &index_name.to_owned().into();
411411
self.iter_by_col_eq(&ctx, &ST_INDEXES_ID, StIndexFields::IndexName.into(), name)
412412
.map(|mut iter| iter.next().map(|row| row.read_col(StIndexFields::IndexId).unwrap()))
413413
}
@@ -432,7 +432,7 @@ impl MutTxId {
432432
&ctx,
433433
&ST_SEQUENCES_ID,
434434
StSequenceFields::SequenceId.into(),
435-
seq_id.into(),
435+
&seq_id.into(),
436436
)?
437437
.last()
438438
.unwrap();
@@ -509,7 +509,7 @@ impl MutTxId {
509509
&ctx,
510510
&ST_SEQUENCES_ID,
511511
StSequenceFields::SequenceId.col_id().into(),
512-
sequence_id.into(),
512+
&sequence_id.into(),
513513
)?
514514
.next()
515515
.ok_or_else(|| TableError::IdNotFound(SystemTable::st_sequence, sequence_id.into()))?;
@@ -530,7 +530,7 @@ impl MutTxId {
530530

531531
pub fn sequence_id_from_name(&self, seq_name: &str, database_address: Address) -> Result<Option<SequenceId>> {
532532
let ctx = ExecutionContext::internal(database_address);
533-
let name = seq_name.to_owned().into();
533+
let name = &seq_name.to_owned().into();
534534
self.iter_by_col_eq(&ctx, &ST_SEQUENCES_ID, StSequenceFields::SequenceName.into(), name)
535535
.map(|mut iter| {
536536
iter.next()
@@ -604,7 +604,7 @@ impl MutTxId {
604604
&ctx,
605605
&ST_CONSTRAINTS_ID,
606606
StConstraintFields::ConstraintId.col_id().into(),
607-
constraint_id.into(),
607+
&constraint_id.into(),
608608
)?
609609
.next()
610610
.ok_or_else(|| TableError::IdNotFound(SystemTable::st_constraints, constraint_id.into()))?;
@@ -629,7 +629,7 @@ impl MutTxId {
629629
&ExecutionContext::internal(database_address),
630630
&ST_CONSTRAINTS_ID,
631631
StConstraintFields::ConstraintName.into(),
632-
constraint_name.to_owned().into(),
632+
&constraint_name.to_owned().into(),
633633
)
634634
.map(|mut iter| {
635635
iter.next()
@@ -738,7 +738,7 @@ impl MutTxId {
738738
&ctx,
739739
&ST_SEQUENCES_ID,
740740
StSequenceFields::TableId.into(),
741-
table_id.into(),
741+
&table_id.into(),
742742
)? {
743743
let seq_col_pos: ColId = seq_row.read_col(StSequenceFields::ColPos)?;
744744
if seq_col_pos == seq.col_pos {

crates/core/src/db/datastore/locking_tx_datastore/state_view.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(crate) trait StateView {
2929

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

55-
fn iter_by_col_eq<'a>(
55+
fn iter_by_col_eq<'a, 'r>(
5656
&'a self,
5757
ctx: &'a ExecutionContext,
5858
table_id: &TableId,
5959
cols: ColList,
60-
value: AlgebraicValue,
61-
) -> Result<IterByColEq<'_>> {
60+
value: &'r AlgebraicValue,
61+
) -> Result<IterByColEq<'a, 'r>> {
6262
self.iter_by_col_range(ctx, table_id, cols, value)
6363
}
6464

6565
/// Reads the schema information for the specified `table_id` directly from the database.
6666
fn schema_for_table_raw(&self, ctx: &ExecutionContext, table_id: TableId) -> Result<TableSchema> {
6767
// Look up the table_name for the table in question.
6868
let st_table_table_id_col = StTableFields::TableId.col_id().into();
69-
let value: AlgebraicValue = table_id.into();
69+
let value_eq = &table_id.into();
7070
let row = self
71-
.iter_by_col_eq(ctx, &ST_TABLES_ID, st_table_table_id_col, table_id.into())?
71+
.iter_by_col_eq(ctx, &ST_TABLES_ID, st_table_table_id_col, value_eq)?
7272
.next()
7373
.ok_or_else(|| TableError::IdNotFound(SystemTable::st_table, table_id.into()))?;
7474
let row = StTableRow::try_from(row)?;
@@ -80,7 +80,7 @@ pub(crate) trait StateView {
8080
// Look up the columns for the table in question.
8181
let st_columns_table_id_col = StColumnFields::TableId.col_id().into();
8282
let mut columns = self
83-
.iter_by_col_eq(ctx, &ST_COLUMNS_ID, st_columns_table_id_col, value)?
83+
.iter_by_col_eq(ctx, &ST_COLUMNS_ID, st_columns_table_id_col, value_eq)?
8484
.map(|row| {
8585
let row = StColumnRow::try_from(row)?;
8686
Ok(ColumnSchema {
@@ -96,7 +96,7 @@ pub(crate) trait StateView {
9696
// Look up the constraints for the table in question.
9797
let st_constraints_table_id = StConstraintFields::TableId.col_id().into();
9898
let constraints = self
99-
.iter_by_col_eq(ctx, &ST_CONSTRAINTS_ID, st_constraints_table_id, table_id.into())?
99+
.iter_by_col_eq(ctx, &ST_CONSTRAINTS_ID, st_constraints_table_id, value_eq)?
100100
.map(|row| {
101101
let row = StConstraintRow::try_from(row)?;
102102
Ok(ConstraintSchema {
@@ -112,7 +112,7 @@ pub(crate) trait StateView {
112112
// Look up the sequences for the table in question.
113113
let st_seq_table_id = StSequenceFields::TableId.col_id().into();
114114
let sequences = self
115-
.iter_by_col_eq(ctx, &ST_SEQUENCES_ID, st_seq_table_id, table_id.into())?
115+
.iter_by_col_eq(ctx, &ST_SEQUENCES_ID, st_seq_table_id, value_eq)?
116116
.map(|row| {
117117
let row = StSequenceRow::try_from(row)?;
118118
Ok(SequenceSchema {
@@ -132,7 +132,7 @@ pub(crate) trait StateView {
132132
// Look up the indexes for the table in question.
133133
let st_idx_table_id = StIndexFields::TableId.col_id().into();
134134
let indexes = self
135-
.iter_by_col_eq(ctx, &ST_INDEXES_ID, st_idx_table_id, table_id.into())?
135+
.iter_by_col_eq(ctx, &ST_INDEXES_ID, st_idx_table_id, value_eq)?
136136
.map(|row| {
137137
let row = StIndexRow::try_from(row)?;
138138
Ok(IndexSchema {
@@ -410,7 +410,7 @@ impl<'a> Iterator for IndexSeekIterMutTxId<'a> {
410410
}
411411

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

415415
/// An iterator for a range of values in a column.
416416
pub enum IterByColRange<'a, R: RangeBounds<AlgebraicValue>> {

crates/core/src/db/datastore/traits.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ pub trait TxDatastore: DataRow + Tx {
230230
where
231231
Self: 'a;
232232

233-
type IterByColEq<'a>: Iterator<Item = Self::RowRef<'a>>
233+
type IterByColEq<'a, 'r>: Iterator<Item = Self::RowRef<'a>>
234234
where
235235
Self: 'a;
236236

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

248-
fn iter_by_col_eq_tx<'a>(
248+
fn iter_by_col_eq_tx<'a, 'r>(
249249
&'a self,
250250
ctx: &'a ExecutionContext,
251251
tx: &'a Self::Tx,
252252
table_id: TableId,
253253
cols: impl Into<ColList>,
254-
value: AlgebraicValue,
255-
) -> Result<Self::IterByColEq<'a>>;
254+
value: &'r AlgebraicValue,
255+
) -> Result<Self::IterByColEq<'a, 'r>>;
256256

257257
fn table_id_exists_tx(&self, tx: &Self::Tx, table_id: &TableId) -> bool;
258258
fn table_id_from_name_tx(&self, tx: &Self::Tx, table_name: &str) -> Result<Option<TableId>>;
@@ -332,14 +332,14 @@ pub trait MutTxDatastore: TxDatastore + MutTx {
332332
cols: impl Into<ColList>,
333333
range: R,
334334
) -> Result<Self::IterByColRange<'a, R>>;
335-
fn iter_by_col_eq_mut_tx<'a>(
335+
fn iter_by_col_eq_mut_tx<'a, 'r>(
336336
&'a self,
337337
ctx: &'a ExecutionContext,
338338
tx: &'a Self::MutTx,
339339
table_id: TableId,
340340
cols: impl Into<ColList>,
341-
value: AlgebraicValue,
342-
) -> Result<Self::IterByColEq<'a>>;
341+
value: &'r AlgebraicValue,
342+
) -> Result<Self::IterByColEq<'a, 'r>>;
343343
fn get_mut_tx<'a>(
344344
&self,
345345
tx: &'a Self::MutTx,

0 commit comments

Comments
 (0)