Skip to content

Commit 49f366f

Browse files
authored
WASM ABI: iter_start -> datastore_table_scan_bsatn (#1637)
1 parent 8ee1de6 commit 49f366f

File tree

9 files changed

+86
-47
lines changed

9 files changed

+86
-47
lines changed

crates/bindings-csharp/Runtime/Internal/FFI.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ public static partial CheckedStatus _datastore_table_row_count(
126126
out ulong out_
127127
);
128128

129+
[LibraryImport(StdbNamespace)]
130+
public static partial CheckedStatus _datastore_table_scan_bsatn(
131+
TableId table_id,
132+
out RowIter out_
133+
);
134+
129135
[LibraryImport(StdbNamespace)]
130136
public static partial CheckedStatus _iter_by_col_eq(
131137
TableId table_id,
@@ -155,9 +161,6 @@ public static partial CheckedStatus _delete_by_rel(
155161
out uint out_
156162
);
157163

158-
[LibraryImport(StdbNamespace)]
159-
public static partial CheckedStatus _iter_start(TableId table_id, out RowIter out_);
160-
161164
[LibraryImport(StdbNamespace)]
162165
public static partial CheckedStatus _iter_start_filtered(
163166
TableId table_id,

crates/bindings-csharp/Runtime/Internal/ITable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public IEnumerable<T> Parse()
114114
private class RawTableIter(FFI.TableId tableId) : RawTableIterBase
115115
{
116116
protected override void IterStart(out FFI.RowIter handle) =>
117-
FFI._iter_start(tableId, out handle);
117+
FFI._datastore_table_scan_bsatn(tableId, out handle);
118118
}
119119

120120
private class RawTableIterFiltered(FFI.TableId tableId, byte[] filterBytes) : RawTableIterBase

crates/bindings-csharp/Runtime/bindings.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ IMPORT(Status, _table_id_from_name,
4848
IMPORT(Status, _datastore_table_row_count,
4949
(TableId table_id, uint64_t* count),
5050
(table_id, count));
51+
IMPORT(Status, _datastore_table_scan_bsatn, (TableId table_id, RowIter* iter),
52+
(table_id, iter));
5153
IMPORT(Status, _iter_by_col_eq,
5254
(TableId table_id, ColId col_id, const uint8_t* value,
5355
uint32_t value_len, RowIter* iter),
@@ -62,8 +64,6 @@ IMPORT(Status, _delete_by_rel,
6264
(TableId table_id, const uint8_t* relation, uint32_t relation_len,
6365
uint32_t* num_deleted),
6466
(table_id, relation, relation_len, num_deleted));
65-
IMPORT(Status, _iter_start, (TableId table_id, RowIter* iter),
66-
(table_id, iter));
6767
IMPORT(Status, _iter_start_filtered,
6868
(TableId table_id, const uint8_t* filter, uint32_t filter_len,
6969
RowIter* iter),

crates/bindings-csharp/Runtime/build/SpacetimeDB.Runtime.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
<WasmImport Include="$(SpacetimeNamespace)!_insert" />
99
<WasmImport Include="$(SpacetimeNamespace)!_delete_by_col_eq" />
1010
<WasmImport Include="$(SpacetimeNamespace)!_delete_by_rel" />
11-
<WasmImport Include="$(SpacetimeNamespace)!_iter_start" />
1211
<WasmImport Include="$(SpacetimeNamespace)!_iter_start_filtered" />
1312
<WasmImport Include="$(SpacetimeNamespace)!_table_id_from_name" />
13+
<WasmImport Include="$(SpacetimeNamespace)!_datastore_table_row_count" />
14+
<WasmImport Include="$(SpacetimeNamespace)!_datastore_table_scan_bsatn" />
1415
<WasmImport Include="$(SpacetimeNamespace)!_row_iter_bsatn_advance" />
1516
<WasmImport Include="$(SpacetimeNamespace)!_row_iter_bsatn_close" />
1617
<WasmImport Include="$(SpacetimeNamespace)!_bytes_source_read" />

crates/bindings-sys/src/lib.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ pub mod raw {
5555
/// - `NO_SUCH_TABLE`, when `table_id` is not a known ID of a table.
5656
pub fn _datastore_table_row_count(table_id: TableId, out: *mut u64) -> u16;
5757

58+
/// Starts iteration on each row, as BSATN-encoded, of a table identified by `table_id`.
59+
///
60+
/// On success, the iterator handle is written to the `out` pointer.
61+
/// This handle can be advanced by [`row_iter_bsatn_advance`].
62+
///
63+
/// # Traps
64+
///
65+
/// This function does not trap.
66+
///
67+
/// # Errors
68+
///
69+
/// Returns an error:
70+
///
71+
/// - `NOT_IN_TRANSACTION`, when called outside of a transaction.
72+
/// - `NO_SUCH_TABLE`, when `table_id` is not a known ID of a table.
73+
pub fn _datastore_table_scan_bsatn(table_id: TableId, out: *mut RowIter) -> u16;
74+
5875
/// Finds all rows in the table identified by `table_id`,
5976
/// where the row has a column, identified by `col_id`,
6077
/// with data matching the byte string, in WASM memory, pointed to at by `val`.
@@ -138,16 +155,7 @@ pub mod raw {
138155
/// - writing to `out` would overflow a 32-bit integer
139156
pub fn _delete_by_rel(table_id: TableId, relation: *const u8, relation_len: usize, out: *mut u32) -> u16;
140157

141-
/// Start iteration on each row, as bytes, of a table identified by `table_id`.
142-
///
143-
/// On success, the iterator handle is written to the `out` pointer.
144-
///
145-
/// # Errors
146-
///
147-
/// - `NO_SUCH_TABLE`, if a table with the provided `table_id` doesn't exist
148-
pub fn _iter_start(table_id: TableId, out: *mut RowIter) -> u16;
149-
150-
/// Like [`_iter_start`], start iteration on each row,
158+
/// Like [`_datastore_table_scan_bsatn`], start iteration on each row,
151159
/// as bytes, of a table identified by `table_id`.
152160
///
153161
/// The rows are filtered through `filter`, which is read from WASM memory
@@ -620,13 +628,22 @@ pub fn delete_by_rel(table_id: TableId, relation: &[u8]) -> Result<u32, Errno> {
620628
unsafe { call(|out| raw::_delete_by_rel(table_id, relation.as_ptr(), relation.len(), out)) }
621629
}
622630

623-
/// Returns an iterator of a table identified by `table_id`.
631+
/// Starts iteration on each row, as BSATN-encoded, of a table identified by `table_id`.
632+
/// Returns iterator handle is written to the `out` pointer.
633+
/// This handle can be advanced by [`row_iter_bsatn_advance`].
634+
///
635+
/// # Traps
636+
///
637+
/// This function does not trap.
624638
///
625639
/// # Errors
626640
///
627-
/// - `NO_SUCH_TABLE`, if `table_id` doesn't exist.
628-
pub fn iter(table_id: TableId) -> Result<RowIter, Errno> {
629-
let raw = unsafe { call(|out| raw::_iter_start(table_id, out))? };
641+
/// Returns an error:
642+
///
643+
/// - `NOT_IN_TRANSACTION`, when called outside of a transaction.
644+
/// - `NO_SUCH_TABLE`, when `table_id` is not a known ID of a table.
645+
pub fn datastore_table_scan_bsatn(table_id: TableId) -> Result<RowIter, Errno> {
646+
let raw = unsafe { call(|out| raw::_datastore_table_scan_bsatn(table_id, out))? };
630647
Ok(RowIter { raw })
631648
}
632649

crates/bindings/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ pub fn delete_by_rel(table_id: TableId, relation: &[impl Serialize]) -> Result<u
237237

238238
// Get the iterator for this table with an optional filter,
239239
fn table_iter<T: TableType>(table_id: TableId) -> Result<TableIter<T>> {
240-
sys::iter(table_id).map(TableIter::new)
240+
sys::datastore_table_scan_bsatn(table_id).map(TableIter::new)
241241
}
242242

243243
fn table_iter_filtered<T: TableType>(

crates/core/src/host/instance_env.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,11 @@ impl InstanceEnv {
238238
}
239239

240240
#[tracing::instrument(skip_all)]
241-
pub fn iter_chunks(&self, ctx: &ExecutionContext, table_id: TableId) -> Result<Vec<Box<[u8]>>, NodesError> {
241+
pub fn datastore_table_scan_bsatn_chunks(
242+
&self,
243+
ctx: &ExecutionContext,
244+
table_id: TableId,
245+
) -> Result<Vec<Box<[u8]>>, NodesError> {
242246
let stdb = &*self.dbic.relational_db;
243247
let tx = &mut *self.tx.get()?;
244248

crates/core/src/host/wasm_common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ macro_rules! abi_funcs {
348348
$mac! {
349349
"spacetime_10.0"::table_id_from_name,
350350
"spacetime_10.0"::datastore_table_row_count,
351+
"spacetime_10.0"::datastore_table_scan_bsatn,
351352
"spacetime_10.0"::row_iter_bsatn_advance,
352353
"spacetime_10.0"::row_iter_bsatn_close,
353354
"spacetime_10.0"::bytes_source_read,
@@ -358,7 +359,6 @@ macro_rules! abi_funcs {
358359
"spacetime_10.0"::delete_by_rel,
359360
"spacetime_10.0"::insert,
360361
"spacetime_10.0"::iter_by_col_eq,
361-
"spacetime_10.0"::iter_start,
362362
"spacetime_10.0"::iter_start_filtered,
363363
"spacetime_10.0"::span_end,
364364
"spacetime_10.0"::span_start,

crates/core/src/host/wasmtime/wasm_instance_env.rs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,42 @@ impl WasmInstanceEnv {
504504
})
505505
}
506506

507+
/// Starts iteration on each row, as BSATN-encoded, of a table identified by `table_id`.
508+
///
509+
/// On success, the iterator handle is written to the `out` pointer.
510+
/// This handle can be advanced by [`row_iter_bsatn_advance`].
511+
///
512+
/// # Traps
513+
///
514+
/// This function does not trap.
515+
///
516+
/// # Errors
517+
///
518+
/// Returns an error:
519+
///
520+
/// - `NOT_IN_TRANSACTION`, when called outside of a transaction.
521+
/// - `NO_SUCH_TABLE`, when `table_id` is not a known ID of a table.
522+
// #[tracing::instrument(skip_all)]
523+
pub fn datastore_table_scan_bsatn(
524+
caller: Caller<'_, Self>,
525+
table_id: u32,
526+
out: WasmPtr<RowIterIdx>,
527+
) -> RtResult<u32> {
528+
Self::cvt_ret(caller, AbiCall::IterStart, out, |caller| {
529+
let env = caller.data_mut();
530+
// Retrieve the execution context for the current reducer.
531+
let ctx = env.reducer_context()?;
532+
// Collect the iterator chunks.
533+
let chunks = env
534+
.instance_env
535+
.datastore_table_scan_bsatn_chunks(&ctx, table_id.into())?;
536+
drop(ctx);
537+
// Register the iterator and get back the index to write to `out`.
538+
// Calls to the iterator are done through dynamic dispatch.
539+
Ok(env.iters.insert(chunks.into_iter()))
540+
})
541+
}
542+
507543
/// Finds all rows in the table identified by `table_id`,
508544
/// where the row has a column, identified by `cols`,
509545
/// with data matching the byte string, in WASM memory, pointed to at by `val`.
@@ -552,29 +588,7 @@ impl WasmInstanceEnv {
552588
})
553589
}
554590

555-
/// Start iteration on each row, as bytes, of a table identified by `table_id`.
556-
///
557-
/// The iterator is registered in the host environment
558-
/// under an assigned index which is written to the `out` pointer provided.
559-
///
560-
/// Returns an error if
561-
/// - a table with the provided `table_id` doesn't exist
562-
// #[tracing::instrument(skip_all)]
563-
pub fn iter_start(caller: Caller<'_, Self>, table_id: u32, out: WasmPtr<RowIterIdx>) -> RtResult<u32> {
564-
Self::cvt_ret(caller, AbiCall::IterStart, out, |caller| {
565-
let env = caller.data_mut();
566-
// Retrieve the execution context for the current reducer.
567-
let ctx = env.reducer_context()?;
568-
// Collect the iterator chunks.
569-
let chunks = env.instance_env.iter_chunks(&ctx, table_id.into())?;
570-
drop(ctx);
571-
// Register the iterator and get back the index to write to `out`.
572-
// Calls to the iterator are done through dynamic dispatch.
573-
Ok(env.iters.insert(chunks.into_iter()))
574-
})
575-
}
576-
577-
/// Like [`WasmInstanceEnv::iter_start`], start iteration on each row,
591+
/// Like [`WasmInstanceEnv::datastore_table_scan_bsatn`], start iteration on each row,
578592
/// as bytes, of a table identified by `table_id`.
579593
///
580594
/// The rows are filtered through `filter`, which is read from WASM memory

0 commit comments

Comments
 (0)