From 311054dfe61a856f4246221fa32968b7753effa5 Mon Sep 17 00:00:00 2001 From: Noa Date: Thu, 26 Sep 2024 15:34:10 -0500 Subject: [PATCH 1/9] Fix btree_scan (#1748) --- .../locking_tx_datastore/committed_state.rs | 19 +++-- .../datastore/locking_tx_datastore/mut_tx.rs | 71 +++++++++++++++---- .../locking_tx_datastore/tx_state.rs | 11 ++- 3 files changed, 77 insertions(+), 24 deletions(-) diff --git a/crates/core/src/db/datastore/locking_tx_datastore/committed_state.rs b/crates/core/src/db/datastore/locking_tx_datastore/committed_state.rs index 72ee480475..c909d95dd3 100644 --- a/crates/core/src/db/datastore/locking_tx_datastore/committed_state.rs +++ b/crates/core/src/db/datastore/locking_tx_datastore/committed_state.rs @@ -2,7 +2,7 @@ use super::{ datastore::Result, sequence::{Sequence, SequencesState}, state_view::{Iter, IterByColRange, ScanIterByColRange, StateView}, - tx_state::{DeleteTable, IndexIdMap, TxState}, + tx_state::{DeleteTable, IndexIdMap, RemovedIndexIdSet, TxState}, }; use crate::{ db::{ @@ -30,8 +30,8 @@ use spacetimedb_lib::{ address::Address, db::auth::{StAccess, StTableType}, }; -use spacetimedb_primitives::{ColList, ColSet, IndexId, TableId}; -use spacetimedb_sats::{AlgebraicValue, ProductValue}; +use spacetimedb_primitives::{ColList, ColSet, TableId}; +use spacetimedb_sats::{AlgebraicType, AlgebraicValue, ProductValue}; use spacetimedb_schema::schema::TableSchema; use spacetimedb_table::{ blob_store::{BlobStore, HashMapBlobStore}, @@ -469,7 +469,7 @@ impl CommittedState { self.merge_apply_inserts(&mut tx_data, tx_state.insert_tables, tx_state.blob_store); // Merge index id fast-lookup map changes. - self.merge_index_map(tx_state.index_id_map, &tx_state.index_id_map_removals); + self.merge_index_map(tx_state.index_id_map, tx_state.index_id_map_removals.as_deref()); // If the TX will be logged, record its projected tx offset, // then increment the counter. @@ -562,8 +562,8 @@ impl CommittedState { } } - fn merge_index_map(&mut self, index_id_map: IndexIdMap, index_id_map_removals: &[IndexId]) { - for index_id in index_id_map_removals { + fn merge_index_map(&mut self, index_id_map: IndexIdMap, index_id_map_removals: Option<&RemovedIndexIdSet>) { + for index_id in index_id_map_removals.into_iter().flatten() { self.index_id_map.remove(index_id); } self.index_id_map.extend(index_id_map); @@ -609,6 +609,13 @@ impl CommittedState { let blob_store = &mut self.blob_store; (table, blob_store) } + + /// Returns the table and index associated with the given `table_id` and `col_list`, if any. + pub(super) fn get_table_and_index_type(&self, table_id: TableId, col_list: &ColList) -> Option<&AlgebraicType> { + let table = self.tables.get(&table_id)?; + let index = table.indexes.get(col_list)?; + Some(&index.key_type) + } } pub struct CommittedIndexIter<'a> { diff --git a/crates/core/src/db/datastore/locking_tx_datastore/mut_tx.rs b/crates/core/src/db/datastore/locking_tx_datastore/mut_tx.rs index ef16e21cd9..d69b10619a 100644 --- a/crates/core/src/db/datastore/locking_tx_datastore/mut_tx.rs +++ b/crates/core/src/db/datastore/locking_tx_datastore/mut_tx.rs @@ -488,7 +488,10 @@ impl MutTxId { } // Remove the `index_id -> (table_id, col_list)` association. idx_map.remove(&index_id); - self.tx_state.index_id_map_removals.push(index_id); + self.tx_state + .index_id_map_removals + .get_or_insert_with(Default::default) + .insert(index_id); log::trace!("INDEX DROPPED: {}", index_id); Ok(()) @@ -530,34 +533,54 @@ impl MutTxId { .map_err(IndexError::Decode)?; // Get an index seek iterator for the tx and committed state. - let tx_iter = self.tx_state.index_seek(table_id, col_list, &bounds).unwrap(); + let tx_iter = self.tx_state.index_seek(table_id, col_list, &bounds); let commit_iter = self.committed_state_write_lock.index_seek(table_id, col_list, &bounds); // Chain together the indexed rows in the tx and committed state, // but don't yield rows deleted in the tx state. - enum Choice { + use itertools::Either::{Left, Right}; + // this is gross, but nested `Either`s don't optimize + enum Choice { A(A), B(B), C(C), + D(D), + E(E), + F(F), } - impl, B: Iterator, C: Iterator> Iterator for Choice { + impl< + T, + A: Iterator, + B: Iterator, + C: Iterator, + D: Iterator, + E: Iterator, + F: Iterator, + > Iterator for Choice + { type Item = T; fn next(&mut self) -> Option { match self { Self::A(i) => i.next(), Self::B(i) => i.next(), Self::C(i) => i.next(), + Self::D(i) => i.next(), + Self::E(i) => i.next(), + Self::F(i) => i.next(), } } } - let iter = match commit_iter { - None => Choice::A(tx_iter), - Some(commit_iter) => match self.tx_state.delete_tables.get(&table_id) { - None => Choice::B(tx_iter.chain(commit_iter)), - Some(tx_dels) => { - Choice::C(tx_iter.chain(commit_iter.filter(move |row| !tx_dels.contains(&row.pointer())))) - } - }, + let commit_iter = commit_iter.map(|commit_iter| match self.tx_state.delete_tables.get(&table_id) { + None => Left(commit_iter), + Some(tx_dels) => Right(commit_iter.filter(move |row| !tx_dels.contains(&row.pointer()))), + }); + let iter = match (tx_iter, commit_iter) { + (None, None) => Choice::A(std::iter::empty()), + (Some(tx_iter), None) => Choice::B(tx_iter), + (None, Some(Left(commit_iter))) => Choice::C(commit_iter), + (None, Some(Right(commit_iter))) => Choice::D(commit_iter), + (Some(tx_iter), Some(Left(commit_iter))) => Choice::E(tx_iter.chain(commit_iter)), + (Some(tx_iter), Some(Right(commit_iter))) => Choice::F(tx_iter.chain(commit_iter)), }; Ok((table_id, iter)) } @@ -567,16 +590,34 @@ impl MutTxId { // The order of querying the committed vs. tx state for the translation is not important. // But it is vastly more likely that it is in the committed state, // so query that first to avoid two lookups. - let (table_id, col_list) = self + let &(table_id, ref col_list) = self .committed_state_write_lock .index_id_map .get(&index_id) .or_else(|| self.tx_state.index_id_map.get(&index_id))?; + // The tx state must have the index. // If the index was e.g., dropped from the tx state but exists physically in the committed state, // the index does not exist, semantically. - let key_ty = self.tx_state.get_table_and_index_type(*table_id, col_list)?; - Some((*table_id, col_list, key_ty)) + // TODO: handle the case where the table has been dropped in this transaction. + let key_ty = if let Some(key_ty) = self + .committed_state_write_lock + .get_table_and_index_type(table_id, col_list) + { + if self + .tx_state + .index_id_map_removals + .as_ref() + .is_some_and(|s| s.contains(&index_id)) + { + return None; + } + key_ty + } else { + self.tx_state.get_table_and_index_type(table_id, col_list)? + }; + + Some((table_id, col_list, key_ty)) } /// Decode the bounds for a btree scan for an index typed at `key_type`. diff --git a/crates/core/src/db/datastore/locking_tx_datastore/tx_state.rs b/crates/core/src/db/datastore/locking_tx_datastore/tx_state.rs index b993bb3c08..5b04d76eed 100644 --- a/crates/core/src/db/datastore/locking_tx_datastore/tx_state.rs +++ b/crates/core/src/db/datastore/locking_tx_datastore/tx_state.rs @@ -1,19 +1,20 @@ use core::ops::RangeBounds; -use spacetimedb_data_structures::map::IntMap; +use spacetimedb_data_structures::map::{IntMap, IntSet}; use spacetimedb_primitives::{ColList, IndexId, TableId}; use spacetimedb_sats::{AlgebraicType, AlgebraicValue}; use spacetimedb_table::{ blob_store::{BlobStore, HashMapBlobStore}, indexes::{RowPointer, SquashedOffset}, + static_assert_size, table::{IndexScanIter, RowRef, Table}, }; use std::collections::{btree_map, BTreeMap, BTreeSet}; -use thin_vec::ThinVec; pub(super) type DeleteTable = BTreeSet; /// A mapping to find the actual index given an `IndexId`. pub(super) type IndexIdMap = IntMap; +pub(super) type RemovedIndexIdSet = IntSet; /// `TxState` tracks all of the modifications made during a particular transaction. /// Rows inserted during a transaction will be added to insert_tables, and similarly, @@ -71,9 +72,13 @@ pub(super) struct TxState { pub(super) index_id_map: IndexIdMap, /// Lists all the `IndexId` that are to be removed from `CommittedState::index_id_map`. - pub(super) index_id_map_removals: ThinVec, + // This is in an `Option>` to reduce the size of `TxState` - it's very uncommon + // that this would be created. + pub(super) index_id_map_removals: Option>, } +static_assert_size!(TxState, 120); + impl TxState { /// Returns the row count in insert tables /// and the number of rows deleted from committed state. From 1ccc8e6c03a348823e43014e5cfaca45de8c3a3b Mon Sep 17 00:00:00 2001 From: Noa Date: Thu, 29 Aug 2024 13:26:08 -0500 Subject: [PATCH 2/9] Rust module API rework --- crates/bindings-macro/src/lib.rs | 527 ++++++---------------- crates/bindings-macro/src/module.rs | 6 +- crates/bindings-sys/src/lib.rs | 2 + crates/bindings/src/impls.rs | 39 -- crates/bindings/src/lib.rs | 557 +++-------------------- crates/bindings/src/rng.rs | 156 +++---- crates/bindings/src/rt.rs | 76 +--- crates/bindings/src/table.rs | 668 ++++++++++++++++++++++++++++ crates/lib/src/filter.rs | 4 - crates/sats/src/bsatn.rs | 7 +- crates/sats/src/bsatn/ser.rs | 1 + modules/rust-wasm-test/src/lib.rs | 74 +-- 12 files changed, 1012 insertions(+), 1105 deletions(-) delete mode 100644 crates/bindings/src/impls.rs create mode 100644 crates/bindings/src/table.rs diff --git a/crates/bindings-macro/src/lib.rs b/crates/bindings-macro/src/lib.rs index 67a5111116..6b2502d1da 100644 --- a/crates/bindings-macro/src/lib.rs +++ b/crates/bindings-macro/src/lib.rs @@ -22,13 +22,10 @@ use std::collections::HashMap; use std::time::Duration; use syn::ext::IdentExt; use syn::meta::ParseNestedMeta; -use syn::parse::{Parse, ParseStream, Parser}; +use syn::parse::{Parse, ParseStream, Parser as _}; use syn::punctuated::Punctuated; use syn::spanned::Spanned; -use syn::{ - parse_quote, BinOp, Expr, ExprBinary, ExprLit, ExprUnary, FnArg, Ident, ItemFn, Member, Path, Token, Type, - TypePath, UnOp, -}; +use syn::{parse_quote, FnArg, Ident, ItemFn, Path, Token}; mod sym { @@ -361,7 +358,7 @@ fn reducer_impl(args: ReducerArgs, original_function: &ItemFn) -> syn::Result(#func_name) + spacetimedb::rt::register_reducer::<_, #func_name>(#func_name) } }; @@ -409,7 +406,7 @@ fn add_scheduled_fields(item: &mut syn::DeriveInput) { fn reducer_type_check(item: &syn::DeriveInput, reducer_name: &Path) -> TokenStream { let struct_name = &item.ident; quote! { - const _: () = spacetimedb::rt::assert_reducer_typecheck::<(#struct_name,), _>(#reducer_name); + const _: () = spacetimedb::rt::assert_reducer_typecheck::<(#struct_name,)>(#reducer_name); } } @@ -544,7 +541,12 @@ impl IndexArg { Ok(IndexArg { kind, name }) } - fn to_index_desc(&self, table_name: &str, cols: &[Column]) -> Result { + fn to_desc_and_accessor( + &self, + table_name: &str, + index_index: u32, + cols: &[Column], + ) -> Result<(TokenStream, TokenStream), syn::Error> { match &self.kind { IndexType::BTree { columns } => { let cols = columns @@ -564,11 +566,21 @@ impl IndexArg { .join("_"); let col_ids = cols.iter().map(|col| col.index); - Ok(quote!(spacetimedb::IndexDesc { + let desc = quote!(spacetimedb::table::IndexDesc { name: #name, ty: spacetimedb::spacetimedb_lib::db::raw_def::IndexType::BTree, col_ids: &[#(#col_ids),*], - })) + }); + + let index_ident = &self.name; + let col_tys = cols.iter().map(|col| col.ty); + let accessor = quote! { + fn #index_ident(&self) -> spacetimedb::BTreeIndex { + spacetimedb::BTreeIndex::__new() + } + }; + + Ok((desc, accessor)) } } } @@ -576,8 +588,8 @@ impl IndexArg { /// Generates code for treating this struct type as a table. /// -/// Among other things, this derives [`Serialize`], [`Deserialize`], -/// [`SpacetimeType`], and [`TableType`] for our type. +/// Among other things, this derives `Serialize`, `Deserialize`, +/// `SpacetimeType`, and `Table` for our type. /// /// # Example /// @@ -666,6 +678,7 @@ struct Column<'a> { index: u16, field: &'a module::SatsField<'a>, attr: ColumnAttribute, + ty: &'a syn::Type, } enum ColumnAttr { @@ -694,29 +707,18 @@ impl ColumnAttr { } } -/// Heuristically determine if the path `p` is one of Rust's primitive integer types. -/// This is an approximation, as the user could do `use String as u8`. -fn is_integer_type(p: &Path) -> bool { - p.get_ident().map_or(false, |i| { - matches!( - i.to_string().as_str(), - "u8" | "i8" | "u16" | "i16" | "u32" | "i32" | "u64" | "i64" | "u128" | "i128" - // These are not Rust int primitives but we still support them. - | "u256" | "i256" - ) - }) -} - fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn::Result { let scheduled_reducer_type_check = args.scheduled.as_ref().map(|reducer| { add_scheduled_fields(&mut item); reducer_type_check(&item, reducer) }); + let vis = &item.vis; let sats_ty = module::sats_type_from_derive(&item, quote!(spacetimedb::spacetimedb_lib))?; let original_struct_ident = sats_ty.ident; - let table_name = args.name.unraw().to_string(); + let table_ident = &args.name; + let table_name = table_ident.unraw().to_string(); let module::SatsTypeData::Product(fields) = &sats_ty.data else { return Err(syn::Error::new(Span::call_site(), "spacetimedb table must be a struct")); }; @@ -727,11 +729,13 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: fn table_id() -> spacetimedb::TableId { static TABLE_ID: std::sync::OnceLock = std::sync::OnceLock::new(); *TABLE_ID.get_or_init(|| { - spacetimedb::table_id_from_name(::TABLE_NAME) + spacetimedb::table_id_from_name(::TABLE_NAME) }) } }; + let mut has_autoinc = false; + for (i, field) in fields.iter().enumerate() { let col_num: u16 = i .try_into() @@ -762,122 +766,56 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: col_attr |= extra_col_attr; } - if col_attr.contains(ColumnAttribute::AUTO_INC) - && !matches!(field.ty, syn::Type::Path(p) if is_integer_type(&p.path)) - { - return Err(syn::Error::new_spanned(field.ident, "An `auto_inc` or `identity` column must be one of the integer types: u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, u256, i256")); - } + has_autoinc |= col_attr.contains(ColumnAttribute::AUTO_INC); let column = Column { index: col_num, field, attr: col_attr, + ty: field.ty, }; columns.push(column); } - let indexes = args + let row_type = quote!(#original_struct_ident); + + let (indexes, index_accessors) = args .indices .iter() - .map(|index| index.to_index_desc(&table_name, &columns)) - .collect::>>()?; - - let (unique_columns, nonunique_columns): (Vec<_>, Vec<_>) = - columns.iter().partition(|x| x.attr.contains(ColumnAttribute::UNIQUE)); - - let has_unique = !unique_columns.is_empty(); - - let mut unique_filter_funcs = Vec::with_capacity(unique_columns.len()); - let mut unique_update_funcs = Vec::with_capacity(unique_columns.len()); - let mut unique_delete_funcs = Vec::with_capacity(unique_columns.len()); - for unique in unique_columns { - let column_index = unique.index; - let vis = unique.field.vis; - let column_type = unique.field.ty; - let column_ident = unique.field.ident.unwrap(); - - let filter_func_ident = format_ident!("filter_by_{}", column_ident); - let update_func_ident = format_ident!("update_by_{}", column_ident); - let delete_func_ident = format_ident!("delete_by_{}", column_ident); - - unique_filter_funcs.push(quote! { - #vis fn #filter_func_ident(#column_ident: &#column_type) -> Option { - spacetimedb::query::filter_by_unique_field::(#column_ident) - } - }); - - unique_update_funcs.push(quote! { - #vis fn #update_func_ident(#column_ident: &#column_type, value: Self) -> bool { - spacetimedb::query::update_by_field::(#column_ident, value) - } - }); - - unique_delete_funcs.push(quote! { - #vis fn #delete_func_ident(#column_ident: &#column_type) -> bool { - spacetimedb::query::delete_by_unique_field::(#column_ident) - } - }); - } - - let non_primary_filter_func = nonunique_columns.into_iter().filter_map(|column| { - let vis = column.field.vis; - let column_ident = column.field.ident.unwrap(); - let column_type = column.field.ty; - let column_index = column.index; - - let filter_func_ident = format_ident!("filter_by_{}", column_ident); - let delete_func_ident = format_ident!("delete_by_{}", column_ident); - - let is_filterable = if let syn::Type::Path(TypePath { path, .. }) = column_type { - // TODO: this is janky as heck - is_integer_type(path) - || path.is_ident("String") - || path.is_ident("bool") - // For these we use the last element of the path because they can be more commonly namespaced. - || matches!( - &*path.segments.last().unwrap().ident.to_string(), - "Address" | "Identity" - ) - } else { - false - }; - - if !is_filterable { - return None; - } - - Some(quote! { - // TODO: should we expose spacetimedb::query::FilterByIter ? - #vis fn #filter_func_ident(#column_ident: &#column_type) -> impl Iterator { - spacetimedb::query::filter_by_field::(#column_ident) - } - #vis fn #delete_func_ident(#column_ident: &#column_type) -> u32 { - spacetimedb::query::delete_by_field::(#column_ident) + .enumerate() + .map(|(i, index)| index.to_desc_and_accessor(&table_name, i as u32, &columns)) + // TODO: stabilized in 1.79 + // .collect::, Vec<_>)>>()?; + .collect::>>()? + .into_iter() + .unzip::<_, _, Vec<_>, Vec<_>>(); + + let unique_field_accessors = columns + .iter() + .filter(|x| x.attr.contains(ColumnAttribute::UNIQUE)) + .map(|unique| { + let column_index = unique.index; + let vis = unique.field.vis; + let column_type = unique.field.ty; + let column_ident = unique.field.ident.unwrap(); + + let doc = format!( + "Gets the [`UniqueColumn`][spacetimedb::UniqueColumn] for the \ + [`{column_ident}`][{row_type}::{column_ident}] column." + ); + quote! { + #[doc = #doc] + #vis fn #column_ident(&self) -> spacetimedb::UniqueColumn { + spacetimedb::UniqueColumn::__new() + } } }) - }); - let non_primary_filter_func = non_primary_filter_func.collect::>(); + .collect::>(); - let insert_result = if has_unique { - quote!(std::result::Result>) - } else { - quote!(Self) - }; + let has_unique = !unique_field_accessors.is_empty(); - let db_insert = quote! { - #[allow(unused_variables)] - pub fn insert(ins: #original_struct_ident) -> #insert_result { - ::insert(ins) - } - }; - - let db_iter = quote! { - #[allow(unused_variables)] - pub fn iter() -> spacetimedb::TableIter { - ::iter() - } - }; + let tablehandle_ident = format_ident!("{}__TableHandle", table_ident); let table_access = if let Some(span) = args.public { quote_spanned!(span=> spacetimedb::spacetimedb_lib::db::auth::StAccess::Public) @@ -906,13 +844,13 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: .map(|col| col.field.ident.unwrap()) .map(|field| { quote_spanned!(field.span()=> - if spacetimedb::IsSequenceTrigger::is_sequence_trigger(&_row.#field) { + if spacetimedb::table::IsSequenceTrigger::is_sequence_trigger(&_row.#field) { _row.#field = spacetimedb::sats::bsatn::from_reader(_in).unwrap(); } ) }); let integrate_generated_columns = quote_spanned!(item.span() => - fn integrate_generated_columns(_row: &mut Self, mut _generated_cols: &[u8]) { + fn integrate_generated_columns(_row: &mut #row_type, mut _generated_cols: &[u8]) { let mut _in = &mut _generated_cols; #(#integrate_gen_col)* } @@ -923,36 +861,55 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: None => quote!(None), }; + let unique_err = if has_unique { + quote!(spacetimedb::UniqueConstraintViolation) + } else { + quote!(::core::convert::Infallible) + }; + let autoinc_err = if has_autoinc { + quote!(spacetimedb::AutoIncOverflow) + } else { + quote!(::core::convert::Infallible) + }; + + let field_names = fields.iter().map(|f| f.ident.unwrap()).collect::>(); + let field_types = fields.iter().map(|f| f.ty).collect::>(); + + let table_name = &sats_ty.name; let tabletype_impl = quote! { - impl spacetimedb::TableType for #original_struct_ident { + impl spacetimedb::Table for #tablehandle_ident<'_> { + type Row = #row_type; + + type UniqueConstraintViolation = #unique_err; + type AutoIncOverflow = #autoinc_err; + + #integrate_generated_columns + } + impl spacetimedb::table::TableInternal for #tablehandle_ident<'_> { const TABLE_NAME: &'static str = #table_name; const TABLE_ACCESS: spacetimedb::spacetimedb_lib::db::auth::StAccess = #table_access; const SCHEDULED_REDUCER_NAME: Option<&'static str> = #scheduled_constant; const COLUMN_ATTRS: &'static [spacetimedb::spacetimedb_lib::db::attr::ColumnAttribute] = &[ #(spacetimedb::spacetimedb_lib::db::attr::ColumnAttribute::#column_attrs),* ]; - const INDEXES: &'static [spacetimedb::IndexDesc<'static>] = &[#(#indexes),*]; - type InsertResult = #insert_result; - #integrate_generated_columns + const INDEXES: &'static [spacetimedb::table::IndexDesc<'static>] = &[#(#indexes),*]; + #table_id_from_name_func } }; - let register_describer_symbol = format!("__preinit__20_register_describer_{table_name}"); + let register_describer_symbol = format!("__preinit__20_register_describer_{table_ident}"); let describe_table_func = quote! { #[export_name = #register_describer_symbol] extern "C" fn __register_describer() { - spacetimedb::rt::register_table::<#original_struct_ident>() + spacetimedb::rt::register_table::<#tablehandle_ident<'static>>() } }; - let field_names = fields.iter().map(|f| f.ident.unwrap()).collect::>(); - let field_types = fields.iter().map(|f| f.ty).collect::>(); - let col_num = 0u16..; let field_access_impls = quote! { - #(impl spacetimedb::query::FieldAccess<#col_num> for #original_struct_ident { + #(impl spacetimedb::table::FieldAccess<#col_num> for #original_struct_ident { type Field = #field_types; fn get_field(&self) -> &Self::Field { &self.#field_names @@ -960,22 +917,6 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: })* }; - let filter_impl = quote! { - const _: () = { - #[derive(Debug, spacetimedb::Serialize, spacetimedb::Deserialize)] - #[sats(crate = spacetimedb::spacetimedb_lib)] - #[repr(u16)] - #[allow(non_camel_case_types)] - pub enum FieldIndex { - #(#field_names),* - } - - impl spacetimedb::spacetimedb_lib::filter::Table for #original_struct_ident { - type FieldIndex = FieldIndex; - } - }; - }; - // Attempt to improve the compile error when a table field doesn't satisfy // the supertraits of `TableType`. We make it so the field span indicates // which fields are offenders, and error reporting stops if the field doesn't @@ -998,33 +939,63 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: } }; + let row_type_to_table = quote!(<#row_type as spacetimedb::table::__MapRowTypeToTable>::Table); + // Output all macro data + let trait_def = quote_spanned! {table_ident.span()=> + #[allow(non_camel_case_types, dead_code)] + #vis trait #table_ident { + fn #table_ident(&self) -> #row_type_to_table<'_>; + } + impl #table_ident for spacetimedb::Local { + fn #table_ident(&self) -> #row_type_to_table<'_> { + #[allow(non_camel_case_types)] + type #tablehandle_ident<'a> = #row_type_to_table<'a>; + #tablehandle_ident { _local: ::core::marker::PhantomData } + } + } + }; + + let tablehandle_def = quote! { + #[allow(non_camel_case_types)] + #vis struct #tablehandle_ident<'a> { + _local: ::core::marker::PhantomData<&'a spacetimedb::Local>, + } + }; + let emission = quote! { const _: () = { - #describe_table_func + #assert_fields_are_spacetimetypes }; + #trait_def + + #[cfg(doc)] + #tablehandle_def + const _: () = { - #assert_fields_are_spacetimetypes - }; + #[cfg(not(doc))] + #tablehandle_def - impl #original_struct_ident { - #db_insert - #(#unique_filter_funcs)* - #(#unique_update_funcs)* - #(#unique_delete_funcs)* + impl spacetimedb::table::__MapRowTypeToTable for #row_type { + type Table<'a> = #tablehandle_ident<'a>; + } - #db_iter - #(#non_primary_filter_func)* - } + impl<'a> #tablehandle_ident<'a> { + #(#unique_field_accessors)* + #(#index_accessors)* + } + + #tabletype_impl + + #describe_table_func + }; #schema_impl #deserialize_impl #serialize_impl - #tabletype_impl #field_access_impls - #filter_impl #scheduled_reducer_type_check }; @@ -1113,223 +1084,3 @@ pub fn schema_type(input: proc_macro::TokenStream) -> proc_macro::TokenStream { .unwrap_or_else(syn::Error::into_compile_error) .into() } - -struct ClosureArg { - // only ident for now as we want to do scope analysis and for now this makes things easier - row_name: Ident, - table_ty: Type, -} - -impl Parse for ClosureArg { - fn parse(input: syn::parse::ParseStream) -> syn::Result { - input.parse::()?; - let row_name = input.parse()?; - input.parse::()?; - let table_ty = input.parse()?; - input.parse::()?; - Ok(Self { row_name, table_ty }) - } -} - -impl ClosureArg { - fn expr_as_table_field<'e>(&self, expr: &'e Expr) -> syn::Result<&'e Ident> { - match expr { - Expr::Field(field) - if match field.base.as_ref() { - Expr::Path(path) => path.path.is_ident(&self.row_name), - _ => false, - } => - { - match &field.member { - Member::Named(ident) => Ok(ident), - Member::Unnamed(index) => Err(syn::Error::new_spanned(index, "unnamed members are not allowed")), - } - } - _ => Err(syn::Error::new_spanned(expr, "expected table field access")), - } - } - - fn make_rhs(&self, e: &mut Expr) -> syn::Result<()> { - match e { - // support `E::A`, `foobar`, etc. - any path except the `row` argument - Expr::Path(path) if !path.path.is_ident(&self.row_name) => Ok(()), - // support any field of a valid RHS expression - this makes it work like - // Rust 2021 closures where `|| foo.bar.baz` captures only `foo.bar.baz` - Expr::Field(field) => self.make_rhs(&mut field.base), - // string literals need to be converted to their owned version for serialization - Expr::Lit(ExprLit { - lit: syn::Lit::Str(_), .. - }) => { - *e = parse_quote!(#e.to_owned()); - Ok(()) - } - // other literals can be inlined into the AST as-is - Expr::Lit(_) => Ok(()), - // unary expressions can be also hoisted out to AST builder, in particular this - // is important to support negative literals like `-123` - Expr::Unary(ExprUnary { expr: arg, .. }) => self.make_rhs(arg), - Expr::Group(group) => self.make_rhs(&mut group.expr), - Expr::Paren(paren) => self.make_rhs(&mut paren.expr), - _ => Err(syn::Error::new_spanned( - e, - "this expression is not supported in the right-hand side of the comparison", - )), - } - } - - fn handle_cmp(&self, expr: &ExprBinary) -> syn::Result { - let left = self.expr_as_table_field(&expr.left)?; - - let mut right = expr.right.clone(); - self.make_rhs(&mut right)?; - - let table_ty = &self.table_ty; - - let lhs_field = quote_spanned!(left.span()=> <#table_ty as spacetimedb::spacetimedb_lib::filter::Table>::FieldIndex::#left as u16); - - let rhs = quote_spanned!(right.span()=> spacetimedb::spacetimedb_lib::filter::Rhs::Value( - std::convert::identity::<<#table_ty as spacetimedb::query::FieldAccess::<{#lhs_field}>>::Field>(#right).into() - )); - - let op = match expr.op { - BinOp::Lt(op) => quote_spanned!(op.span()=> spacetimedb::spacetimedb_lib::operator::OpCmp::Lt), - BinOp::Le(op) => quote_spanned!(op.span()=> spacetimedb::spacetimedb_lib::operator::OpCmp::LtEq), - BinOp::Eq(op) => quote_spanned!(op.span()=> spacetimedb::spacetimedb_lib::operator::OpCmp::Eq), - BinOp::Ne(op) => quote_spanned!(op.span()=> spacetimedb::spacetimedb_lib::operator::OpCmp::NotEq), - BinOp::Ge(op) => quote_spanned!(op.span()=> spacetimedb::spacetimedb_lib::operator::OpCmp::GtEq), - BinOp::Gt(op) => quote_spanned!(op.span()=> spacetimedb::spacetimedb_lib::operator::OpCmp::Gt), - _ => unreachable!(), - }; - - Ok( - quote_spanned!(expr.span()=> spacetimedb::spacetimedb_lib::filter::Expr::Cmp(spacetimedb::spacetimedb_lib::filter::Cmp { - op: #op, - args: spacetimedb::spacetimedb_lib::filter::CmpArgs { - lhs_field: #lhs_field, - rhs: #rhs, - }, - })), - ) - } - - fn handle_logic(&self, expr: &ExprBinary) -> syn::Result { - let op = match expr.op { - BinOp::And(op) => quote_spanned!(op.span()=> spacetimedb::spacetimedb_lib::operator::OpLogic::And), - BinOp::Or(op) => quote_spanned!(op.span()=> spacetimedb::spacetimedb_lib::operator::OpLogic::Or), - _ => unreachable!(), - }; - - let left = self.handle_expr(&expr.left)?; - let right = self.handle_expr(&expr.right)?; - - Ok( - quote_spanned!(expr.span()=> spacetimedb::spacetimedb_lib::filter::Expr::Logic(spacetimedb::spacetimedb_lib::filter::Logic { - lhs: Box::new(#left), - op: #op, - rhs: Box::new(#right), - })), - ) - } - - fn handle_binop(&self, expr: &ExprBinary) -> syn::Result { - match expr.op { - BinOp::Lt(_) | BinOp::Le(_) | BinOp::Eq(_) | BinOp::Ne(_) | BinOp::Ge(_) | BinOp::Gt(_) => { - self.handle_cmp(expr) - } - BinOp::And(_) | BinOp::Or(_) => self.handle_logic(expr), - _ => Err(syn::Error::new_spanned(expr.op, "unsupported binary operator")), - } - } - - fn handle_unop(&self, expr: &ExprUnary) -> syn::Result { - let op = match expr.op { - UnOp::Not(op) => quote_spanned!(op.span()=> spacetimedb::spacetimedb_lib::operator::OpUnary::Not), - _ => return Err(syn::Error::new_spanned(expr.op, "unsupported unary operator")), - }; - - let arg = self.handle_expr(&expr.expr)?; - - Ok( - quote_spanned!(expr.span()=> spacetimedb::spacetimedb_lib::filter::Expr::Unary(spacetimedb::spacetimedb_lib::filter::Unary { - op: #op, - arg: Box::new(#arg), - })), - ) - } - - fn handle_expr(&self, expr: &Expr) -> syn::Result { - Ok(match expr { - Expr::Binary(expr) => self.handle_binop(expr)?, - Expr::Unary(expr) => self.handle_unop(expr)?, - Expr::Group(group) => self.handle_expr(&group.expr)?, - Expr::Paren(paren) => self.handle_expr(&paren.expr)?, - expr => return Err(syn::Error::new_spanned(expr, "unsupported expression")), - }) - } -} - -struct ClosureLike { - arg: ClosureArg, - body: Box, -} - -impl Parse for ClosureLike { - fn parse(input: syn::parse::ParseStream) -> syn::Result { - Ok(Self { - arg: input.parse()?, - body: input.parse()?, - }) - } -} - -impl ClosureLike { - pub fn handle(&self) -> syn::Result { - let table_ty = &self.arg.table_ty; - let expr = self.arg.handle_expr(&self.body)?; - - Ok(quote_spanned!(self.body.span()=> { - <#table_ty as spacetimedb::TableType>::iter_filtered(#expr) - })) - } -} - -/// Implements query!(|row| ...) macro for filtering rows. -/// -/// # Example -/// -/// ```ignore // unfortunately, doctest doesn't work well inside proc-macro -/// use spacetimedb::query; -/// -/// #[spacetimedb::table(name = people)] -/// pub struct Person { -/// name: String, -/// age: u32, -/// } -/// -/// for person in query!(|person: Person| person.age >= 18) { -/// println!("{person:?}"); -/// } -/// ``` -/// -/// # Syntax -/// -/// Supports Rust-like closure syntax, with the following limitations: -/// -/// - Only one argument is supported. -/// - Argument must be an identifier (destructuring is not yet implemented). -/// - Argument must have an explicit table type annotation. -/// - Left hand side of any comparison must be a table field access. -/// - Right hand side of any comparison must be a literal or a captured variable `foo` or a property `foo.bar.baz` (which will be inlined as its value). -/// In the future field-to-field comparisons will be supported too. -/// - Comparisons can be combined with `&&` and `||` operators. -/// - Parentheses are supported. -/// - Unary `!` operator is supported at the syntax level but not yet implemented by the VM so it will panic at translation phase. -#[proc_macro] -pub fn query(input: proc_macro::TokenStream) -> proc_macro::TokenStream { - let closure_like = syn::parse_macro_input!(input as ClosureLike); - - closure_like - .handle() - .unwrap_or_else(syn::Error::into_compile_error) - .into() -} diff --git a/crates/bindings-macro/src/module.rs b/crates/bindings-macro/src/module.rs index 83a6b4b823..e7d01d3f96 100644 --- a/crates/bindings-macro/src/module.rs +++ b/crates/bindings-macro/src/module.rs @@ -12,7 +12,7 @@ use crate::{check_duplicate, sym}; pub(crate) struct SatsType<'a> { pub ident: &'a syn::Ident, pub generics: &'a syn::Generics, - pub name: String, + pub name: LitStr, pub krate: TokenStream, // may want to use in the future #[allow(unused)] @@ -104,14 +104,14 @@ pub(crate) fn extract_sats_type<'a>( check_duplicate(&name, &meta)?; let value = meta.value()?; let v = value.parse::()?; - name = Some(v.value()); + name = Some(v); } }); Ok(()) })?; } let krate = krate.unwrap_or(crate_fallback); - let name = name.unwrap_or_else(|| ident.to_string()); + let name = name.unwrap_or_else(|| LitStr::new(&ident.to_string(), ident.span())); Ok(SatsType { ident, diff --git a/crates/bindings-sys/src/lib.rs b/crates/bindings-sys/src/lib.rs index 07f7dadaa2..2a41e9e7ab 100644 --- a/crates/bindings-sys/src/lib.rs +++ b/crates/bindings-sys/src/lib.rs @@ -654,6 +654,8 @@ pub struct Errno(NonZeroU16); // once Error gets exposed from core this crate can be no_std again impl std::error::Error for Errno {} +pub type Result = core::result::Result; + macro_rules! def_errno { ($($err_name:ident($errno:literal, $errmsg:literal),)*) => { impl Errno { diff --git a/crates/bindings/src/impls.rs b/crates/bindings/src/impls.rs deleted file mode 100644 index 2e081e02ff..0000000000 --- a/crates/bindings/src/impls.rs +++ /dev/null @@ -1,39 +0,0 @@ -use crate::{FilterableValue, IsSequenceTrigger}; -use spacetimedb_lib::{ - sats::{i256, u256}, - Address, Identity, -}; - -macro_rules! impl_filterable_value { - ($($t:ty),*) => { - $( - impl FilterableValue for $t {} - )* - }; -} - -impl_filterable_value![u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, u256, i256, bool, String, Identity, Address]; - -macro_rules! impl_is_seq_trigger { - ($($t:ty),*) => { - $( - impl IsSequenceTrigger for $t { - fn is_sequence_trigger(&self) -> bool { *self == 0 } - } - )* - }; -} - -impl_is_seq_trigger![u8, i8, u16, i16, u32, i32, u64, i64, u128, i128]; - -impl IsSequenceTrigger for i256 { - fn is_sequence_trigger(&self) -> bool { - *self == Self::ZERO - } -} - -impl IsSequenceTrigger for u256 { - fn is_sequence_trigger(&self) -> bool { - *self == Self::ZERO - } -} diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index 6102b4924b..50739f22c1 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -3,38 +3,30 @@ #[macro_use] mod io; -mod impls; pub mod log_stopwatch; mod logger; #[cfg(feature = "rand")] mod rng; #[doc(hidden)] pub mod rt; +#[doc(hidden)] +pub mod table; mod timestamp; -use spacetimedb_lib::buffer::{BufReader, BufWriter, Cursor, DecodeError}; -use spacetimedb_lib::db::attr::ColumnAttribute; -use spacetimedb_lib::db::auth::StAccess; -use spacetimedb_lib::db::raw_def::IndexType; -use spacetimedb_lib::{bsatn, ProductType, ProductValue}; -use spacetimedb_primitives::ColId; +use spacetimedb_lib::bsatn; use std::cell::RefCell; use std::collections::VecDeque; -use std::marker::PhantomData; -use std::slice::from_ref; -use std::{fmt, panic}; -use sys::RowIter; pub use log; #[cfg(feature = "rand")] pub use rand; #[cfg(feature = "rand")] -pub use rng::{random, rng, StdbRng}; +pub use rng::StdbRng; pub use sats::SpacetimeType; #[doc(hidden)] pub use spacetimedb_bindings_macro::__TableHelper; -pub use spacetimedb_bindings_macro::{duration, query, reducer, table}; +pub use spacetimedb_bindings_macro::{duration, reducer, table}; pub use spacetimedb_bindings_sys as sys; pub use spacetimedb_lib; pub use spacetimedb_lib::de::{Deserialize, DeserializeOwned}; @@ -45,15 +37,13 @@ pub use spacetimedb_lib::AlgebraicValue; pub use spacetimedb_lib::Identity; pub use spacetimedb_primitives::TableId; pub use sys::Errno; +pub use table::{AutoIncOverflow, BTreeIndex, Table, TryInsertError, UniqueColumn, UniqueConstraintViolation}; pub use timestamp::Timestamp; -pub type Result = core::result::Result; - pub type ReducerResult = core::result::Result<(), Box>; /// A context that any reducer is provided with. #[non_exhaustive] -#[derive(Copy, Clone)] pub struct ReducerContext { /// The `Identity` of the client that invoked the reducer. pub sender: Identity, @@ -67,527 +57,126 @@ pub struct ReducerContext { /// For automatic reducers, i.e. `init`, `update` and scheduled reducers, /// this will be the module's `Address`. pub address: Option
, + pub db: Local, + + #[cfg(feature = "rand")] + rng: std::cell::OnceCell, } impl ReducerContext { #[doc(hidden)] pub fn __dummy() -> Self { Self { + db: Local {}, sender: Identity::__dummy(), timestamp: Timestamp::UNIX_EPOCH, address: None, + rng: std::cell::OnceCell::new(), } } } -// #[cfg(target_arch = "wasm32")] -// #[global_allocator] -// static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; - -/// Run a function `f` provided with an empty mutable row buffer -/// and return the result of the function. -fn with_row_buf(f: impl FnOnce(&mut Vec) -> R) -> R { - thread_local! { - /// A global buffer used for row data. - // This gets optimized away to a normal global since wasm32 doesn't have threads by default. - static ROW_BUF: RefCell> = RefCell::new(Vec::with_capacity(DEFAULT_BUFFER_CAPACITY)); - } +/// A handle on a database with a particular table schema. +pub trait DbContext { + /// A view into the tables of a database. + /// + /// This type is specialized on the database's particular schema. + /// + /// Methods on the `DbView` type will allow querying tables defined by the module. + type DbView; - ROW_BUF.with_borrow_mut(|buf| { - buf.clear(); - f(buf) - }) + /// Get a view into the tables. + /// + /// This method is provided for times when a programmer wants to be generic over the `DbContext` type. + /// Concrete-typed code is expected to read the `.db` field off the particular `DbContext` implementor. + /// Currently, being this generic is only meaningful in clients, + /// as modules have only a single implementor of `DbContext`. + fn db(&self) -> &Self::DbView; } -pub fn encode_row(row: ProductValue, bytes: &mut impl BufWriter) { - row.encode(bytes); -} +impl DbContext for ReducerContext { + type DbView = Local; -pub fn decode_row<'a>(schema: &ProductType, bytes: &mut impl BufReader<'a>) -> Result { - ProductValue::decode(schema, bytes) + fn db(&self) -> &Self::DbView { + &self.db + } } -pub fn encode_schema(schema: ProductType, bytes: &mut impl BufWriter) { - schema.encode(bytes); -} +#[non_exhaustive] +pub struct Local {} -pub fn decode_schema<'a>(bytes: &mut impl BufReader<'a>) -> Result { - ProductType::decode(bytes) -} +// #[cfg(target_arch = "wasm32")] +// #[global_allocator] +// static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + +// This should guarantee in most cases that we don't have to reallocate an iterator +// buffer, unless there's a single row that serializes to >1 MiB. +const DEFAULT_BUFFER_CAPACITY: usize = spacetimedb_primitives::ROW_ITER_CHUNK_SIZE * 2; /// Queries and returns the `table_id` associated with the given (table) `name`. /// /// Panics if the table does not exist. +#[doc(hidden)] pub fn table_id_from_name(table_name: &str) -> TableId { sys::table_id_from_name(table_name).unwrap_or_else(|_| { panic!("Failed to get table with name: {}", table_name); }) } -/// Insert a row of type `T` into the table identified by `table_id`. -/// This will call `handle_gen_cols` to write back the generated column values passed in the `&[u8]`. -pub fn insert_raw( - table_id: TableId, - row: T, - handle_gen_cols: impl FnOnce(&mut T, &[u8]), -) -> Result { - with_row_buf(|bytes| { - // Encode the row as bsatn into the buffer `bytes`. - bsatn::to_writer(bytes, &row).unwrap(); - - // Insert row into table. - sys::insert(table_id, bytes).map(|gen_cols| { - let mut row = row; - // Let the caller handle any generated columns written back by `sys::insert` to `bytes`. - handle_gen_cols(&mut row, gen_cols); - row - }) - }) -} - -/// Insert a row of type `T` into the table identified by `table_id`. -pub fn insert(table_id: TableId, row: T) -> T::InsertResult { - let res = insert_raw(table_id, row, T::integrate_generated_columns); - sealed::InsertResult::from_res(res) -} - -/// Finds all rows in the table identified by `table_id`, -/// where the row has a column, identified by `col_id`, -/// with data matching `val` that can be serialized. -/// -/// Matching is defined by decoding of `value` to an `AlgebraicValue` -/// according to the column's schema and then `Ord for AlgebraicValue`. -/// -/// The rows found are BSATN encoded and then concatenated. -/// The resulting byte string from the concatenation is written -/// to a fresh buffer with a handle to it returned as a `Buffer`. -/// -/// Panics if -/// - BSATN serialization fails -/// - there were unique constraint violations -/// - `row` doesn't decode from BSATN to a `ProductValue` -/// according to the `ProductType` that the table's schema specifies -pub fn iter_by_col_eq(table_id: TableId, col_id: ColId, val: &impl Serialize) -> Result { - with_row_buf(|bytes| { - // Encode `val` as BSATN into `bytes` and then use that. - bsatn::to_writer(bytes, val).unwrap(); - sys::iter_by_col_eq(table_id, col_id, bytes) - }) -} - -/// Deletes all rows in the table identified by `table_id` -/// where the column identified by `col_id` matches a `value` that can be serialized. -/// -/// Matching is defined by decoding of `value` to an `AlgebraicValue` -/// according to the column's schema and then `Ord for AlgebraicValue`. -/// -/// Returns the number of rows deleted. -/// -/// Returns an error if -/// - a table with the provided `table_id` doesn't exist -/// - no columns were deleted -/// - `col_id` does not identify a column of the table, -/// - `value` doesn't decode from BSATN to an `AlgebraicValue` -/// according to the `AlgebraicType` that the table's schema specifies for `col_id`. -/// -/// Panics when serialization fails. -pub fn delete_by_col_eq(table_id: TableId, col_id: ColId, value: &impl Serialize) -> Result { - with_row_buf(|bytes| { - // Encode `value` as BSATN into `bytes` and then use that. - bsatn::to_writer(bytes, value).unwrap(); - sys::delete_by_col_eq(table_id, col_id, bytes) - }) -} - -/// Deletes those rows, in the table identified by `table_id`, -/// that match any row in `relation`. -/// -/// The `relation` will be BSATN encoded to `[ProductValue]` -/// i.e., a list of product values, so each element in `relation` -/// must serialize to what a `ProductValue` would in BSATN. -/// -/// Matching is then defined by first BSATN-decoding -/// the resulting bsatn to a `Vec` -/// according to the row schema of the table -/// and then using `Ord for AlgebraicValue`. -/// -/// Returns the number of rows deleted. -/// -/// Returns an error if -/// - a table with the provided `table_id` doesn't exist -/// - `(relation, relation_len)` doesn't decode from BSATN to a `Vec` -/// - this is called outside a transaction -/// -/// Panics when serialization fails. -pub fn delete_by_rel(table_id: TableId, relation: &[impl Serialize]) -> Result { - with_row_buf(|bytes| { - // Encode `value` as BSATN into `bytes` and then use that. - bsatn::to_writer(bytes, relation).unwrap(); - sys::datastore_delete_all_by_eq_bsatn(table_id, bytes) - }) -} - -// Get the iterator for this table with an optional filter, -fn table_iter(table_id: TableId) -> Result> { - sys::datastore_table_scan_bsatn(table_id).map(TableIter::new) -} - -fn table_iter_filtered( - table_id: TableId, - filter: &spacetimedb_lib::filter::Expr, -) -> Result> { - with_row_buf(|buf| { - bsatn::to_writer(buf, filter).expect("Couldn't encode the filter query"); - sys::iter_filtered(table_id, buf).map(TableIter::new) - }) -} - -/// A table iterator which yields values of the `TableType` corresponding to the table. -pub struct TableIter { - /// The underlying source of our `Buffer`s. - inner: RowIter, - - /// The current position in the buffer, from which `deserializer` can read. - reader: Cursor>, - - _marker: PhantomData, -} - -// This should guarantee in most cases that we don't have to reallocate an iterator -// buffer, unless there's a single row that serializes to >1 MiB. -const DEFAULT_BUFFER_CAPACITY: usize = spacetimedb_primitives::ROW_ITER_CHUNK_SIZE * 2; - thread_local! { /// A global pool of buffers used for iteration. // This gets optimized away to a normal global since wasm32 doesn't have threads by default. static ITER_BUFS: RefCell>> = const { RefCell::new(VecDeque::new()) }; } -/// Take a buffer from the pool of buffers for row iterators, if one exists. Otherwise, allocate a new one. -fn take_iter_buf() -> Vec { - ITER_BUFS - .with_borrow_mut(|v| v.pop_front()) - .unwrap_or_else(|| Vec::with_capacity(DEFAULT_BUFFER_CAPACITY)) -} - -/// Return the buffer to the pool of buffers for row iterators. -fn return_iter_buf(mut buf: Vec) { - buf.clear(); - ITER_BUFS.with_borrow_mut(|v| v.push_back(buf)) -} - -impl TableIter { - fn new(iter: RowIter) -> Self { - TableIter { - inner: iter, - reader: Cursor::new(take_iter_buf()), - _marker: PhantomData, - } - } -} - -impl Drop for TableIter { - fn drop(&mut self) { - return_iter_buf(std::mem::take(&mut self.reader.buf)) - } -} - -impl TableIter { - fn decode(&mut self) -> T { - bsatn::from_reader(&mut &self.reader).expect("Failed to decode row!") - } +struct IterBuf { + buf: Vec, } -impl Iterator for TableIter { - type Item = T; - - fn next(&mut self) -> Option { - // If we currently have some bytes in the buffer to still decode, do that. - if (&self.reader).remaining() > 0 { - return Some(self.decode()); - } - - // Don't fetch the next chunk if there is none. - if self.inner.is_exhausted() { - return None; - } - - // Otherwise, try to fetch the next chunk while reusing the buffer. - self.reader.buf.clear(); - self.reader.pos.set(0); - if self.inner.read(&mut self.reader.buf) == 0 { - return None; - } - Some(self.decode()) - } -} - -/// Describe a named index with an index type over a set of columns identified by their IDs. -#[derive(Clone, Copy)] -pub struct IndexDesc<'a> { - /// The name of the index. - pub name: &'a str, - /// The type of index used, i.e. the strategy used for indexing. - pub ty: IndexType, - /// The set of columns indexed over given by the identifiers of the columns. - pub col_ids: &'a [u16], -} - -/// A trait for the set of types serializable, deserializable, and convertible to `AlgebraicType`. -/// -/// Additionally, the type knows its own table name, its column attributes, and indices. -pub trait TableType: SpacetimeType + DeserializeOwned + Serialize { - const TABLE_NAME: &'static str; - const TABLE_ACCESS: StAccess; - const COLUMN_ATTRS: &'static [ColumnAttribute]; - const INDEXES: &'static [IndexDesc<'static>]; - const SCHEDULED_REDUCER_NAME: Option<&'static str> = None; - - type InsertResult: sealed::InsertResult; - - /// Returns the ID of this table. - fn table_id() -> TableId; - - // Re-integrates the BSATN of the `generated_cols` into `row`. - fn integrate_generated_columns(row: &mut Self, generated_cols: &[u8]); - - /// Insert `ins` as a row in this table. - fn insert(ins: Self) -> Self::InsertResult { - insert(Self::table_id(), ins) - } - - /// Returns an iterator over the rows in this table. - fn iter() -> TableIter { - table_iter(Self::table_id()).unwrap() - } - - /// Returns an iterator filtered by `filter` over the rows in this table. - /// - /// **NOTE:** Do not use directly. This is exposed as `query!(...)`. - #[doc(hidden)] - fn iter_filtered(filter: spacetimedb_lib::filter::Expr) -> TableIter { - table_iter_filtered(Self::table_id(), &filter).unwrap() +impl IterBuf { + /// Take a buffer from the pool of buffers for row iterators, if one exists. Otherwise, allocate a new one. + fn take() -> Self { + let buf = ITER_BUFS + .with_borrow_mut(|v| v.pop_front()) + .unwrap_or_else(|| Vec::with_capacity(DEFAULT_BUFFER_CAPACITY)); + Self { buf } } - /// Deletes this row `self` from the table. - /// - /// Returns `true` if the row was deleted. - fn delete(&self) -> bool { - let count = delete_by_rel(Self::table_id(), from_ref(self)).unwrap(); - debug_assert!(count < 2); - count == 1 + fn serialize(val: &T) -> Result { + let mut buf = IterBuf::take(); + buf.serialize_into(val)?; + Ok(buf) } -} - -mod sealed { - use super::*; - /// A trait of result types which know how to convert a `Result` into itself. - pub trait InsertResult { - type T: TableType; - fn from_res(res: Result) -> Self; + #[inline] + fn serialize_into(&mut self, val: &T) -> Result<(), bsatn::EncodeError> { + bsatn::to_writer(&mut **self, val) } } -/// A UNIQUE constraint violation on table type `T` was attempted. -pub struct UniqueConstraintViolation(PhantomData); -impl fmt::Debug for UniqueConstraintViolation { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "UniqueConstraintViolation({})", T::TABLE_NAME) - } -} -impl fmt::Display for UniqueConstraintViolation { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "not able to insert into table {}; duplicate unique column", - T::TABLE_NAME - ) - } -} -impl From> for String { - fn from(err: UniqueConstraintViolation) -> Self { - err.to_string() +impl Drop for IterBuf { + fn drop(&mut self) { + self.buf.clear(); + let buf = std::mem::take(&mut self.buf); + ITER_BUFS.with_borrow_mut(|v| v.push_back(buf)); } } -impl std::error::Error for UniqueConstraintViolation {} -impl sealed::InsertResult for Result> { - type T = T; - fn from_res(res: Result) -> Self { - res.map_err(|e| match e { - Errno::UNIQUE_ALREADY_EXISTS => UniqueConstraintViolation(PhantomData), - _ => panic!("unexpected error from insert(): {e}"), - }) +impl AsRef<[u8]> for IterBuf { + fn as_ref(&self) -> &[u8] { + &self.buf } } -impl sealed::InsertResult for T { - type T = T; - fn from_res(res: Result) -> Self { - res.unwrap_or_else(|e| panic!("unexpected error from insert(): {e}")) +impl std::ops::Deref for IterBuf { + type Target = Vec; + fn deref(&self) -> &Self::Target { + &self.buf } } - -/// A trait for types that know if their value will trigger a sequence. -/// This is used for auto-inc columns to determine if an insertion of a row -/// will require the column to be updated in the row. -/// -/// For now, this is equivalent to a "is zero" test. -pub trait IsSequenceTrigger { - /// Is this value one that will trigger a sequence, if any, - /// when used as a column value. - fn is_sequence_trigger(&self) -> bool; -} - -/// A trait for types that can be serialized and tested for equality. -/// -/// A type `T` implementing this trait should uphold the invariant: -/// ```text -/// ∀ a, b ∈ T. a == b <=> serialize(a) == serialize(b) -/// ``` -/// That is, if two values `a: T` and `b: T` are equal, -/// then so are the values in their serialized representation. -pub trait FilterableValue: Serialize + Eq {} - -#[doc(hidden)] -pub mod query { - use super::*; - - /// A trait for types exposing an operation to access their `N`th field. - /// - /// In other words, a type implementing `FieldAccess` allows - /// shared projection from `self` to its `N`th field. - pub trait FieldAccess { - /// The type of the field at the `N`th position. - type Field; - - /// Project to the value of the field at position `N`. - fn get_field(&self) -> &Self::Field; - } - - /// Finds the row of `Table` where the column at `COL_IDX` matches `val`, - /// as defined by decoding to an `AlgebraicValue` - /// according to the column's schema and then `Ord for AlgebraicValue`. - /// - /// **NOTE:** Do not use directly. - /// This is exposed as `filter_by_{$field_name}` on types with `#[spacetimedb::table]`. - #[doc(hidden)] - pub fn filter_by_unique_field< - Table: TableType + FieldAccess, - T: FilterableValue, - const COL_IDX: u16, - >( - val: &T, - ) -> Option { - // Find the row with a match. - let mut iter = iter_by_col_eq(Table::table_id(), COL_IDX.into(), val).unwrap(); - with_row_buf(|buf| { - // We will always find either 0 or 1 rows here due to the unique constraint. - iter.read(buf); - debug_assert!(iter.is_exhausted()); - - if buf.is_empty() { - return None; - } - let mut reader = buf.as_slice(); - let row = bsatn::from_reader(&mut reader).unwrap(); - assert!( - reader.is_empty(), - "iter_by_col_eq on unique field cannot return >1 rows" - ); - Some(row) - }) - } - - /// Finds all rows of `Table` where the column at `COL_IDX` matches `val`, - /// as defined by decoding to an `AlgebraicValue` - /// according to the column's schema and then `Ord for AlgebraicValue`. - /// - /// **NOTE:** Do not use directly. - /// This is exposed as `filter_by_{$field_name}` on types with `#[spacetimedb::table]`. - #[doc(hidden)] - pub fn filter_by_field(val: &T) -> TableIter
{ - let iter = iter_by_col_eq(Table::table_id(), COL_IDX.into(), val).expect("iter_by_col_eq failed"); - TableIter::new(iter) - } - - /// Deletes all rows of `Table` where the column at `COL_IDX` matches `val`, - /// as defined by decoding to an `AlgebraicValue` - /// according to the column's schema and then `Ord for AlgebraicValue`. - /// - /// Returns the number of deleted rows. - /// - /// **NOTE:** Do not use directly. - /// This is exposed as `delete_by_{$field_name}` on types with `#[spacetimedb::table]` - /// where the field does not have a unique constraint. - #[doc(hidden)] - pub fn delete_by_field(val: &T) -> u32 { - delete_by_col_eq(Table::table_id(), COL_IDX.into(), val) - // TODO: Returning `Err` here was supposed to signify an error, - // but it can also return `Err(_)` when there is nothing to delete. - .unwrap_or(0) - } - - /// Deletes the row of `Table` where the column at `COL_IDX` matches `val`, - /// as defined by decoding to an `AlgebraicValue` - /// according to the column's schema and then `Ord for AlgebraicValue`. - /// - /// Returns whether any rows were deleted. - /// - /// **NOTE:** Do not use directly. - /// This is exposed as `delete_by_{$field_name}` on types with `#[spacetimedb::table]` - /// where the field has a unique constraint. - pub fn delete_by_unique_field(val: &T) -> bool { - let count = delete_by_field::(val); - debug_assert!(count <= 1); - count > 0 - } - - /// Updates the row of `Table`, where the column at `COL_IDX` matches `old`, to be `new` instead. - /// - /// Matching is defined by decoding to an `AlgebraicValue` - /// according to the column's schema and then `Ord for AlgebraicValue`. - /// - /// **NOTE:** Do not use directly. - /// This is exposed as `update_by_{$field_name}` on types with `#[spacetimedb::table]`. - #[doc(hidden)] - pub fn update_by_field(old: &T, new: Table) -> bool { - // Delete the existing row, if any. - delete_by_field::(old); - - // Insert the new row. - Table::insert(new); - - // TODO: For now this is always successful. - // In the future, this could return what `delete_by_field` returns? - true - } - - /// An iterator returned by `filter_by_field`, - /// which yields all of the rows of a table where a particular column's value - /// matches a given target value. - /// - /// Matching is defined by decoding to an `AlgebraicValue` - /// according to the column's schema and then `Ord for AlgebraicValue`. - #[doc(hidden)] - pub struct FilterByIter { - /// The buffer of rows returned by `iter_by_col_eq`. - cursor: Cursor>, - - _phantom: PhantomData
, - } - - impl
Iterator for FilterByIter
- where - Table: TableType, - { - type Item = Table; - - fn next(&mut self) -> Option { - let mut cursor = &self.cursor; - (cursor.remaining() != 0).then(|| bsatn::from_reader(&mut cursor).unwrap()) - } +impl std::ops::DerefMut for IterBuf { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.buf } } diff --git a/crates/bindings/src/rng.rs b/crates/bindings/src/rng.rs index 066c2ee24e..0797a350b6 100644 --- a/crates/bindings/src/rng.rs +++ b/crates/bindings/src/rng.rs @@ -1,63 +1,58 @@ -use std::cell::{OnceCell, UnsafeCell}; -use std::fmt; +use std::cell::UnsafeCell; use std::marker::PhantomData; use rand::distributions::{Distribution, Standard}; use rand::rngs::StdRng; use rand::{RngCore, SeedableRng}; -use crate::Timestamp; - -scoped_tls::scoped_thread_local! { - static RNG: OnceCell -} - -pub(crate) fn with_rng_set(f: impl FnOnce() -> R) -> R { - RNG.set(&OnceCell::new(), f) -} - -fn seed_from_timestamp() -> StdRngCell { - StdRngCell::new(StdRng::seed_from_u64(Timestamp::now().micros_since_epoch)) -} - -/// Generates a random value. -/// -/// Similar to [`rand::random()`], but using [`StdbRng`] instead. -/// -/// See also [`spacetimedb::rng()`][rng()] -pub fn random() -> T -where - Standard: Distribution, -{ - if !RNG.is_set() { - panic!("cannot use `spacetimedb::random()` outside of a reducer"); +use crate::ReducerContext; + +impl ReducerContext { + /// Generates a random value. + /// + /// Similar to [`rand::random()`], but using [`StdbRng`] instead. + /// + /// See also [`ReducerContext::rng()`]. + pub fn random(&self) -> T + where + Standard: Distribution, + { + Standard.sample(&mut self.rng()) } - Standard.sample(&mut rng()) -} -/// Retrieve the random number generator for this reducer transaction, -/// seeded by the timestamp of the reducer call. -/// -/// If you only need a single random value, use [`spacetimedb::random()`][random]. -/// -/// Can be used in method chaining style, e.g. with [`rand::Rng`] -/// imported: `spacetimedb::rng().gen_range(0..=10)`. Or, cache it locally -/// for reuse: `let mut rng = spacetimedb::rng();`. -/// -/// For more information see [`StdbRng`]. -pub fn rng() -> StdbRng { - if !RNG.is_set() { - panic!("cannot use `spacetimedb::rng()` outside of a reducer"); + /// Retrieve the random number generator for this reducer transaction, + /// seeded by the timestamp of the reducer call. + /// + /// If you only need a single random value, you can use [`ReducerContext::random()`]. + /// + /// # Examples + /// + /// ``` + /// # #[spacetimedb::reducer] + /// # fn rng_demo(ctx: &spacetimedb::ReducerContext) { + /// use rand::Rng; + /// + /// // Can be used in method chaining style: + /// let digit = ctx.rng().gen_range(0..=9); + /// + /// // Or, cache locally for reuse: + /// let mut rng = ctx.rng(); + /// let floats: Vec = rng.sample_iter(rand::distribution::Standard).collect(); + /// # } + /// ``` + /// + /// For more information, see [`StdbRng`] and [`rand::Rng`]. + pub fn rng(&self) -> &StdbRng { + self.rng.get_or_init(|| StdbRng { + rng: StdRng::seed_from_u64(self.timestamp.micros_since_epoch).into(), + _marker: PhantomData, + }) } - RNG.with(|r| { - r.get_or_init(seed_from_timestamp); - }); - StdbRng { _marker: PhantomData } } /// A reference to the random number generator for this reducer call. /// -/// An instance can be obtained via [`spacetimedb::rng()`][rng()]. Import +/// An instance can be obtained via [`ReducerContext::rng()`]. Import /// [`rand::Rng`] in order to access many useful random algorithms. /// /// `StdbRng` uses the same PRNG as `rand`'s [`StdRng`]. Note, however, that @@ -71,75 +66,38 @@ pub fn rng() -> StdbRng { /// Just note that you must not store any state, including an rng, in a global /// variable or any other in-WASM side channel. Any and all state persisted /// across reducer calls _must_ be stored in the database. -#[derive(Clone)] pub struct StdbRng { + // Comments in the rand crate claim RefCell can have an overhead of up to 15%, + // and so they use an UnsafeCell instead: + // + // This is safe as long as no method on `StdRngCell` is re-entrant. + rng: UnsafeCell, + // !Send + !Sync _marker: PhantomData<*mut ()>, } -impl StdbRng { - fn try_with(&self, f: impl FnOnce(&StdRngCell) -> R) -> Result { - if !RNG.is_set() { - return Err(RngError); - } - Ok(RNG.with(|r| f(r.get_or_init(seed_from_timestamp)))) - } - - fn with(&self, f: impl FnOnce(&StdRngCell) -> R) -> R { - self.try_with(f).unwrap_or_else( - #[cold] - |e| panic!("{e}"), - ) - } -} - impl RngCore for StdbRng { fn next_u32(&mut self) -> u32 { - self.with(|rng| rng.next_u32()) + (&*self).next_u32() } fn next_u64(&mut self) -> u64 { - self.with(|rng| rng.next_u64()) + (&*self).next_u64() } fn fill_bytes(&mut self, dest: &mut [u8]) { - self.with(|rng| rng.fill_bytes(dest)) + (&*self).fill_bytes(dest) } fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> { - self.try_with(|rng| rng.try_fill_bytes(dest)) - .unwrap_or_else(|e| Err(rand::Error::new(e))) + (&*self).try_fill_bytes(dest) } } -#[derive(Debug)] -struct RngError; - -impl fmt::Display for RngError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("an StdbRng was stored and used outside the context of a reducer") - } -} - -impl std::error::Error for RngError {} - -// Comments in the rand crate claim RefCell can have an overhead of up to 15%, -// and so they use an UnsafeCell instead: -// -// This is safe as long as no method on `StdRngCell` is re-entrant. -struct StdRngCell { - rng: UnsafeCell, -} - -impl StdRngCell { - fn new(rng: StdRng) -> Self { - Self { rng: rng.into() } - } -} - -impl StdRngCell { +impl RngCore for &StdbRng { #[inline(always)] - fn next_u32(&self) -> u32 { + fn next_u32(&mut self) -> u32 { // SAFETY: We must make sure to stop using `rng` before anyone else // creates another mutable reference let rng = unsafe { &mut *self.rng.get() }; @@ -147,21 +105,21 @@ impl StdRngCell { } #[inline(always)] - fn next_u64(&self) -> u64 { + fn next_u64(&mut self) -> u64 { // SAFETY: We must make sure to stop using `rng` before anyone else // creates another mutable reference let rng = unsafe { &mut *self.rng.get() }; rng.next_u64() } - fn fill_bytes(&self, dest: &mut [u8]) { + fn fill_bytes(&mut self, dest: &mut [u8]) { // SAFETY: We must make sure to stop using `rng` before anyone else // creates another mutable reference let rng = unsafe { &mut *self.rng.get() }; rng.fill_bytes(dest) } - fn try_fill_bytes(&self, dest: &mut [u8]) -> Result<(), rand::Error> { + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> { // SAFETY: We must make sure to stop using `rng` before anyone else // creates another mutable reference let rng = unsafe { &mut *self.rng.get() }; diff --git a/crates/bindings/src/rt.rs b/crates/bindings/src/rt.rs index 8113601cbb..d1ed31fcc2 100644 --- a/crates/bindings/src/rt.rs +++ b/crates/bindings/src/rt.rs @@ -1,7 +1,7 @@ #![deny(unsafe_op_in_unsafe_fn)] use crate::timestamp::with_timestamp_set; -use crate::{return_iter_buf, sys, take_iter_buf, ReducerContext, ReducerResult, SpacetimeType, TableType, Timestamp}; +use crate::{sys, IterBuf, ReducerContext, ReducerResult, SpacetimeType, Table, Timestamp}; use spacetimedb_lib::db::auth::StTableType; use spacetimedb_lib::db::raw_def::{ RawColumnDefV8, RawConstraintDefV8, RawIndexDefV8, RawSequenceDefV8, RawTableDefV8, @@ -22,8 +22,8 @@ use sys::raw::{BytesSink, BytesSource}; /// /// Returns an invalid buffer on success /// and otherwise the error is written into the fresh one returned. -pub fn invoke_reducer<'a, A: Args<'a>, T>( - reducer: impl Reducer<'a, A, T>, +pub fn invoke_reducer<'a, A: Args<'a>>( + reducer: impl Reducer<'a, A>, ctx: ReducerContext, args: &'a [u8], ) -> Result<(), Box> { @@ -31,16 +31,11 @@ pub fn invoke_reducer<'a, A: Args<'a>, T>( let SerDeArgs(args) = bsatn::from_slice(args).expect("unable to decode args"); // Run the reducer with the environment all set up. - let invoke = || reducer.invoke(ctx, args); - #[cfg(feature = "rand")] - let invoke = || crate::rng::with_rng_set(invoke); - with_timestamp_set(ctx.timestamp, invoke) + with_timestamp_set(ctx.timestamp, || reducer.invoke(&ctx, args)) } /// A trait for types representing the *execution logic* of a reducer. -/// -/// The type parameter `T` is used for determining whether there is a context argument. -pub trait Reducer<'de, A: Args<'de>, T> { - fn invoke(&self, ctx: ReducerContext, args: A) -> ReducerResult; +pub trait Reducer<'de, A: Args<'de>> { + fn invoke(&self, ctx: &ReducerContext, args: A) -> ReducerResult; type ArgsWithContext; fn extract_args(args: Self::ArgsWithContext) -> A; @@ -91,31 +86,28 @@ impl IntoReducerResult for () { Ok(self) } } -impl IntoReducerResult for Result<(), E> { +impl IntoReducerResult for Result<(), E> { #[inline] fn into_result(self) -> Result<(), Box> { - self.map_err(|e| format!("{e:?}").into()) + self.map_err(|e| e.to_string().into()) } } /// A trait of types that can be an argument of a reducer. pub trait ReducerArg<'de> {} impl<'de, T: Deserialize<'de>> ReducerArg<'de> for T {} -impl ReducerArg<'_> for ReducerContext {} +impl ReducerArg<'_> for &ReducerContext {} /// Assert that `T: ReducerArg`. pub fn assert_reducer_arg<'de, T: ReducerArg<'de>>() {} /// Assert that `T: IntoReducerResult`. pub fn assert_reducer_ret() {} -pub const fn assert_reducer_typecheck<'de, A: Args<'de>, T>(_: impl Reducer<'de, A, T> + Copy) {} +/// Assert that a reducer type-checks with a given type. +pub const fn assert_reducer_typecheck<'de, A: Args<'de>>(_: impl Reducer<'de, A> + Copy) {} /// Used in the last type parameter of `Reducer` to indicate that the /// context argument *should* be passed to the reducer logic. pub struct ContextArg; -/// Used in the last type parameter of `Reducer` to indicate that the -/// context argument *should not* be passed to the reducer logic. -pub struct NoContextArg; - /// A visitor providing a deserializer for a type `A: Args`. struct ArgsVisitor { _marker: PhantomData, @@ -190,13 +182,13 @@ macro_rules! impl_reducer { } // Implement `Reducer<..., ContextArg>` for the tuple type `($($T,)*)`. - impl<'de, Func, Ret, $($T: SpacetimeType + Deserialize<'de> + Serialize),*> Reducer<'de, ($($T,)*), ContextArg> for Func + impl<'de, Func, Ret, $($T: SpacetimeType + Deserialize<'de> + Serialize),*> Reducer<'de, ($($T,)*)> for Func where - Func: Fn(ReducerContext, $($T),*) -> Ret, + Func: Fn(&ReducerContext, $($T),*) -> Ret, Ret: IntoReducerResult { #[allow(non_snake_case)] - fn invoke(&self, ctx: ReducerContext, args: ($($T,)*)) -> Result<(), Box> { + fn invoke(&self, ctx: &ReducerContext, args: ($($T,)*)) -> Result<(), Box> { let ($($T,)*) = args; self(ctx, $($T),*).into_result() } @@ -209,23 +201,6 @@ macro_rules! impl_reducer { } } - // Implement `Reducer<..., NoContextArg>` for the tuple type `($($T,)*)`. - impl<'de, Func, Ret, $($T: SpacetimeType + Deserialize<'de> + Serialize),*> Reducer<'de, ($($T,)*), NoContextArg> for Func - where - Func: Fn($($T),*) -> Ret, - Ret: IntoReducerResult - { - #[allow(non_snake_case)] - fn invoke(&self, _ctx: ReducerContext, args: ($($T,)*)) -> Result<(), Box> { - let ($($T,)*) = args; - self($($T),*).into_result() - } - - type ArgsWithContext = ($($T,)*); - fn extract_args(args: Self::ArgsWithContext) -> ($($T,)*) { - args - } - } }; // Counts the number of elements in the tuple. (@count $($T:ident)*) => { @@ -277,9 +252,9 @@ pub fn register_reftype() { } /// Registers a describer for the `TableType` `T`. -pub fn register_table() { +pub fn register_table() { register_describer(|module| { - let data = *T::make_type(&mut module.inner).as_ref().unwrap(); + let data = *T::Row::make_type(&mut module.inner).as_ref().unwrap(); let columns: Vec = RawColumnDefV8::from_product_type( module .inner @@ -338,8 +313,8 @@ pub fn register_table() { }) } -impl From> for RawIndexDefV8 { - fn from(index: crate::IndexDesc<'_>) -> RawIndexDefV8 { +impl From> for RawIndexDefV8 { + fn from(index: crate::table::IndexDesc<'_>) -> RawIndexDefV8 { let columns = index.col_ids.iter().copied().collect::(); if columns.is_empty() { panic!("Need at least one column in IndexDesc for index `{}`", index.name); @@ -355,7 +330,7 @@ impl From> for RawIndexDefV8 { } /// Registers a describer for the reducer `I` with arguments `A`. -pub fn register_reducer<'a, A: Args<'a>, T, I: ReducerInfo>(_: impl Reducer<'a, A, T>) { +pub fn register_reducer<'a, A: Args<'a>, I: ReducerInfo>(_: impl Reducer<'a, A>) { register_describer(|module| { let schema = A::schema::(&mut module.inner); module.inner.add_reducer(schema); @@ -471,9 +446,11 @@ extern "C" fn __call_reducer__( // Assemble the `ReducerContext`. let timestamp = Timestamp::UNIX_EPOCH + Duration::from_micros(timestamp); let ctx = ReducerContext { + db: crate::Local {}, sender, timestamp, address, + rng: std::cell::OnceCell::new(), }; // Fetch reducer function. @@ -501,18 +478,11 @@ fn with_read_args(args: BytesSource, logic: impl FnOnce(&[u8]) -> R) -> R { // but it's likely we have one sitting around being unused at this point, // so use it to avoid allocating a temporary buffer if possible. // And if we do allocate a temporary buffer now, it will likely be reused later. - let mut buf = take_iter_buf(); + let mut buf = IterBuf::take(); // Read `args` and run `logic`. read_bytes_source_into(args, &mut buf); - let ret = logic(&buf); - - // Return the `buf` back to the pool. - // Should a panic occur before reaching here, - // the WASM module cannot recover and will trap, - // so we don't need to care that this is not returned to the pool. - return_iter_buf(buf); - ret + logic(&buf) } const NO_SPACE: u16 = errno::NO_SPACE.get(); diff --git a/crates/bindings/src/table.rs b/crates/bindings/src/table.rs new file mode 100644 index 0000000000..ea338c782e --- /dev/null +++ b/crates/bindings/src/table.rs @@ -0,0 +1,668 @@ +use std::borrow::Borrow; +use std::convert::Infallible; +use std::marker::PhantomData; +use std::{fmt, ops}; + +use spacetimedb_lib::buffer::{BufReader, Cursor}; +use spacetimedb_lib::db::attr::ColumnAttribute; +use spacetimedb_lib::db::auth::StAccess; +use spacetimedb_lib::db::raw_def::IndexType; +use spacetimedb_primitives::ColId; + +use crate::{bsatn, sys, DeserializeOwned, IterBuf, Serialize, SpacetimeType, TableId}; + +/// Implemented for every `TableHandle` struct generated in the client `module_bindings` +/// and the module macroexpansion. +pub trait Table: TableInternal { + /// The type of rows stored in this table. + type Row: SpacetimeType + Serialize + DeserializeOwned + Sized + 'static; + + /// Returns the number of rows of this table in the TX state, + /// i.e. num(committed_state) + num(insert_table) - num(delete_table). + /// + /// This API is new to modules (though it previously existed in the Rust SDK) + /// and will require a new host function in the ABI. + fn count(&self) -> u64 { + sys::datastore_table_row_count(Self::table_id()).expect("datastore_table_row_count() call failed") + } + + /// Iterate over all rows in the TX state, + /// i.e. committed_state ∪ insert_table ∖ delete_table. + #[inline] + fn iter(&self) -> impl Iterator { + let table_id = Self::table_id(); + let iter = sys::datastore_table_scan_bsatn(table_id).expect("datastore_table_scan_bsatn() call failed"); + TableIter::new(iter) + } + + /// Inserts `row` into the TX state, + /// i.e. removes it from the delete table or adds it to the insert table as appropriate. + /// + /// The return value is the inserted row, with any auto-incrementing columns replaced with computed values. + /// The `insert` method always returns the inserted row, + /// even when the table contains no auto-incrementing columns. + /// + /// May panic if inserting the row violates any constraints. + /// Callers which intend to handle constraint violation errors should instead use [`Self::try_insert`]. + /// + /// Note that, in languages where error handling is based on exceptions, + /// no distinction is provided between `Table::insert` and `Table::try_insert`. + /// A single method `insert` is defined which throws an exception on failure, + /// and callers may either install handlers around it or allow the exception to bubble up. + /// + /// Note on MVCC: because callers have no way to determine if the row was previously present, + /// two concurrent transactions which delete the same row + /// may be ordered arbitrarily with respect to one another + /// while maintaining sequential consistency, assuming no other conflicts. + #[track_caller] + fn insert(&self, row: Self::Row) -> Self::Row { + self.try_insert(row).unwrap_or_else(|e| panic!("{e}")) + } + + /// The error type for this table for unique constraint violations. Will either be + /// [`UniqueConstraintViolation`] if the table has any unique constraints, or [`Infallible`] + /// otherwise. + type UniqueConstraintViolation: MaybeError; + /// The error type for this table for auto-increment overflows. Will either be + /// [`AutoIncOverflow`] if the table has any auto-incrementing columns, or [`Infallible`] + /// otherwise. + type AutoIncOverflow: MaybeError; + + /// Counterpart to [`Self::insert`] which allows handling failed insertions. + /// + /// For tables without any constraints, [`Self::TryInsertError`] will be [`std::convert::Infallible`], + /// and this will be a more-verbose [`Self::insert`]. + /// For tables with constraints, this method returns an `Err` when the insertion fails rather than panicking. + /// + /// Note that, in languages where error handling is based on exceptions, + /// no distinction is provided between `Table::insert` and `Table::try_insert`. + /// A single method `insert` is defined which throws an exception on failure, + /// and callers may either install handlers around it or allow the exception to bubble up. + #[track_caller] + fn try_insert(&self, row: Self::Row) -> Result> { + insert::(row) + } + + /// Deletes a row equal to `row` from the TX state, + /// i.e. deletes it from the insert table or adds it to the delete table as appropriate. + /// + /// Returns `true` if the row was present and has been deleted, + /// or `false` if the row was not present and therefore the tables have not changed. + /// + /// Unlike [`Self::insert`], there is no need to return the deleted row, + /// as it must necessarily have been exactly equal to the `row` argument. + /// No analogue to auto-increment placeholders exists for deletions. + /// + /// May panic if deleting the row violates any constraints. + /// Note that as of writing deletion is infallible, but future work may define new constraints, + /// e.g. foreign keys, which cause deletion to fail in some cases. + /// If and when these new constraints are added, + /// we should define `Self::try_delete` and `Self::TryDeleteError`, + /// analogous to [`Self::try_insert`] and [`Self::TryInsertError`]. + /// + /// Note on MVCC: the return value means that logically a `delete` performs a query + /// to see if the row is present. + /// As such, two concurrent transactions which delete the same row + /// cannot be placed in a sequentially-consistent ordering, + /// and one of them must be retried. + fn delete(&self, row: Self::Row) -> bool { + let relation = std::slice::from_ref(&row); + let buf = IterBuf::serialize(relation).unwrap(); + let count = sys::datastore_delete_all_by_eq_bsatn(Self::table_id(), &buf).unwrap(); + count == 1 + } + + // Re-integrates the BSATN of the `generated_cols` into `row`. + #[doc(hidden)] + fn integrate_generated_columns(row: &mut Self::Row, generated_cols: &[u8]); +} + +#[doc(hidden)] +pub trait TableInternal: Sized { + const TABLE_NAME: &'static str; + const TABLE_ACCESS: StAccess; + const COLUMN_ATTRS: &'static [ColumnAttribute]; + const INDEXES: &'static [IndexDesc<'static>]; + const SCHEDULED_REDUCER_NAME: Option<&'static str> = None; + + /// Whether any of [`Self::COLUMN_ATTRS`] have an autoinc constraint. + const HAS_AUTOINC: bool = 'autoinc: { + // NOTE: Written this way because iterators aren't const-stable. + // Same as `T::COLUMN_ATTRS.iter().any(|attr| attr.has_autoinc())`. + let mut i = 0; + while i < Self::COLUMN_ATTRS.len() { + if Self::COLUMN_ATTRS[i].has_autoinc() { + break 'autoinc true; + } + i += 1; + } + false + }; + + /// Returns the ID of this table. + fn table_id() -> TableId; +} + +/// Describe a named index with an index type over a set of columns identified by their IDs. +#[derive(Clone, Copy)] +pub struct IndexDesc<'a> { + /// The name of the index. + pub name: &'a str, + /// The type of index used, i.e. the strategy used for indexing. + pub ty: IndexType, + /// The set of columns indexed over given by the identifiers of the columns. + pub col_ids: &'a [u16], +} + +#[doc(hidden)] +pub trait __MapRowTypeToTable { + type Table<'a>: Table; +} + +/// A UNIQUE constraint violation on a table was attempted. +// TODO: add column name for better error message +#[derive(Debug)] +#[non_exhaustive] +pub struct UniqueConstraintViolation; + +impl fmt::Display for UniqueConstraintViolation { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "duplicate unique column") + } +} + +impl std::error::Error for UniqueConstraintViolation {} + +/// An auto-inc column overflowed its data type. +#[derive(Debug)] +#[non_exhaustive] +// TODO: add column name for better error message +pub struct AutoIncOverflow; + +impl fmt::Display for AutoIncOverflow { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "auto-inc sequence overflowed its column type") + } +} + +impl std::error::Error for AutoIncOverflow {} + +/// The error type returned from [`Table::try_insert()`], signalling a constraint violation. +pub enum TryInsertError { + /// A [`UniqueConstraintViolation`]. + /// + /// Returned from [`Table::try_insert`] if an attempted insertion + /// has the same value in a unique column as an already-present row. + /// + /// This variant is only possible if the table has at least one unique column, + /// and is otherwise [`std::convert::Infallible`]. + UniqueConstraintViolation(Tbl::UniqueConstraintViolation), + + /// An [`AutoIncOverflow`]. + /// + /// Returned from [`TableHandle::try_insert`] if an attempted insertion + /// advances an auto-inc sequence past the bounds of the column type. + /// + /// This variant is only possible if the table has at least one auto-inc column, + /// and is otherwise [`std::convert::Infallible`]. + AutoIncOverflow(Tbl::AutoIncOverflow), +} + +impl fmt::Debug for TryInsertError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "TryInsertError::<{}>::", Tbl::TABLE_NAME)?; + match self { + Self::UniqueConstraintViolation(e) => fmt::Debug::fmt(e, f), + Self::AutoIncOverflow(e) => fmt::Debug::fmt(e, f), + } + } +} + +impl fmt::Display for TryInsertError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "insertion error on table `{}`:", Tbl::TABLE_NAME)?; + match self { + Self::UniqueConstraintViolation(e) => fmt::Display::fmt(e, f), + Self::AutoIncOverflow(e) => fmt::Display::fmt(e, f), + } + } +} + +impl std::error::Error for TryInsertError {} + +impl From> for String { + fn from(err: TryInsertError) -> Self { + err.to_string() + } +} + +#[doc(hidden)] +pub trait MaybeError: std::error::Error + Sized { + fn get() -> Option; +} + +impl MaybeError for Infallible { + fn get() -> Option { + None + } +} + +impl MaybeError for UniqueConstraintViolation { + fn get() -> Option { + Some(UniqueConstraintViolation) + } +} + +impl MaybeError for AutoIncOverflow { + fn get() -> Option { + Some(AutoIncOverflow) + } +} + +/// A trait for types exposing an operation to access their `N`th field. +/// +/// In other words, a type implementing `FieldAccess` allows +/// shared projection from `self` to its `N`th field. +#[doc(hidden)] +pub trait FieldAccess { + /// The type of the field at the `N`th position. + type Field; + + /// Project to the value of the field at position `N`. + fn get_field(&self) -> &Self::Field; +} + +pub struct UniqueColumn +where + ColType: SpacetimeType + Serialize + DeserializeOwned, + Tbl::Row: FieldAccess, +{ + _marker: PhantomData, +} + +impl UniqueColumn +where + ColType: SpacetimeType + Serialize + DeserializeOwned, + Tbl::Row: FieldAccess, +{ + #[doc(hidden)] + pub fn __new() -> Self { + Self { _marker: PhantomData } + } + + /// Finds and returns the row where the value in the unique column matches the supplied `col_val`, + /// or `None` if no such row is present in the database state. + // + // TODO: consider whether we should accept the sought value by ref or by value. + // Should be consistent with the implementors of `BTreeIndexBounds` (see below). + // By-value makes passing `Copy` fields more convenient, + // whereas by-ref makes passing `!Copy` fields more performant. + // Can we do something smart with `std::borrow::Borrow`? + pub fn find(&self, col_val: impl Borrow) -> Option { + // Find the row with a match. + let buf = IterBuf::serialize(col_val.borrow()).unwrap(); + let iter = sys::iter_by_col_eq(Tbl::table_id(), COL_IDX.into(), &buf).unwrap(); + let mut iter = TableIter::new_with_buf(iter, buf); + + // We will always find either 0 or 1 rows here due to the unique constraint. + let row = iter.next(); + assert!( + iter.is_exhausted(), + "iter_by_col_eq on unique field cannot return >1 rows" + ); + row + } + + /// Deletes the row where the value in the unique column matches the supplied `col_val`, + /// if any such row is present in the database state. + /// + /// Returns `true` if a row with the specified `col_val` was previously present and has been deleted, + /// or `false` if no such row was present. + /// + /// May panic if deleting the row would violate a constraint, + /// though as of proposing no such constraints exist. + pub fn delete(&self, col_val: impl Borrow) -> bool { + let buf = IterBuf::serialize(col_val.borrow()).unwrap(); + sys::delete_by_col_eq(Tbl::table_id(), COL_IDX.into(), &buf) + // TODO: Returning `Err` here was supposed to signify an error, + // but it can also return `Err(_)` when there is nothing to delete. + .unwrap_or(0) + > 0 + } + + /// Deletes the row where the value in the unique column matches that in the corresponding field of `new_row`, + /// then inserts the `new_row`. + /// + /// Returns the new row as actually inserted, with any auto-inc placeholders substituted for computed values. + /// + /// Panics if no row was previously present with the matching value in the unique column, + /// or if either the delete or the insertion would violate a constraint. + /// + /// Implementors are encouraged to include the table name, unique column name, and unique column value + /// in the panic message when no such row previously existed. + #[track_caller] + pub fn update(&self, new_row: Tbl::Row) -> Tbl::Row { + assert!( + self.delete(new_row.get_field()), + "Row passed to UniqueColumn::update() did not already exist in table." + ); + insert::(new_row).unwrap_or_else(|e| panic!("{e}")) + } +} + +pub struct BTreeIndex { + _marker: PhantomData<(Tbl, IndexType)>, +} + +impl BTreeIndex { + #[doc(hidden)] + pub fn __new() -> Self { + Self { _marker: PhantomData } + } + + /// Returns an iterator over all rows in the database state where the indexed column(s) match the bounds `b`. + /// + /// `b` may be: + /// - A value for the first indexed column. + /// - A range of values for the first indexed column. + /// - A tuple of values for any prefix of the indexed columns, optionally terminated by a range for the next. + pub fn filter, K>(&self, b: B) -> impl Iterator { + let args = b.get_args(); + let (prefix, prefix_elems, rstart, rend) = args.args_for_syscall(); + #[allow(unreachable_code)] + TableIter::new(todo!( + "once implemented: datastore_btree_scan_bsatn({prefix:?}, {prefix_elems:?}, {rstart:?}, {rend:?})" + )) + } + + /// Deletes all rows in the database state where the indexed column(s) match the bounds `b`. + /// + /// `b` may be: + /// - A value for the first indexed column. + /// - A range of values for the first indexed column. + /// - A tuple of values for any prefix of the indexed columns, optionally terminated by a range for the next. + /// + /// May panic if deleting any one of the rows would violate a constraint, + /// though as of proposing no such constraints exist. + pub fn delete, K>(&self, b: B) -> u64 { + let args = b.get_args(); + let (prefix, prefix_elems, rstart, rend) = args.args_for_syscall(); + todo!("once implemented: datastore_delete_by_btree_scan_bsatn({prefix:?}, {prefix_elems:?}, {rstart:?}, {rend:?})") + } +} + +pub trait BTreeIndexBounds { + #[doc(hidden)] + fn get_args(&self) -> BTreeScanArgs; +} + +#[doc(hidden)] +pub struct BTreeScanArgs { + data: IterBuf, + prefix_elems: usize, + rstart_idx: usize, + // None if rstart and rend are the same + rend_idx: Option, +} + +impl BTreeScanArgs { + pub(crate) fn args_for_syscall(&self) -> (&[u8], ColId, &[u8], &[u8]) { + let len = self.data.len(); + ( + &self.data[..self.rstart_idx], + ColId::from(self.prefix_elems), + &self.data[self.rstart_idx..self.rend_idx.unwrap_or(len)], + &self.data[self.rend_idx.unwrap_or(self.rstart_idx)..], + ) + } +} + +macro_rules! impl_btree_index_bounds { + ($T:ident $(, $U:ident)*) => { + impl_btree_index_bounds!(@impl (), ($T $(, $U)*)); + + impl_btree_index_bounds!($($U),*); + }; + () => {}; + (@impl ($($V:ident),*), ($T:ident $(, $U:ident)+)) => { + impl<$($V,)* $T: Serialize, $($U: Serialize,)+ Term: BTreeIndexBoundsTerminator<$T>> BTreeIndexBounds<($($U,)+ $T, $($V,)*)> for ($($U,)+ Term,) { + fn get_args(&self) -> BTreeScanArgs { + let mut data = IterBuf::take(); + let prefix_elems = impl_btree_index_bounds!(@count $($U)+); + #[allow(non_snake_case)] + let ($($U,)+ term,) = self; + Ok(()) + $(.and_then(|()| data.serialize_into($U)))+ + .unwrap(); + let rstart_idx = data.len(); + let rend_idx = term.bounds().serialize_into(&mut data); + BTreeScanArgs { data, prefix_elems, rstart_idx, rend_idx } + } + } + impl_btree_index_bounds!(@impl ($($V,)* $T), ($($U),*)); + }; + (@impl ($($V:ident),*), ($T:ident)) => { + impl<$($V,)* $T: Serialize, Term: BTreeIndexBoundsTerminator<$T>> BTreeIndexBounds<($T, $($V,)*)> for (Term,) { + fn get_args(&self) -> BTreeScanArgs { + BTreeIndexBounds::<($T, $($V,)*), SingleBound>::get_args(&self.0) + } + } + impl<$($V,)* $T: Serialize, Term: BTreeIndexBoundsTerminator<$T>> BTreeIndexBounds<($T, $($V,)*), SingleBound> for Term { + fn get_args(&self) -> BTreeScanArgs { + let mut data = IterBuf::take(); + let rend_idx = self.bounds().serialize_into(&mut data); + BTreeScanArgs { data, prefix_elems: 0, rstart_idx: 0, rend_idx } + } + } + }; + // Counts the number of elements in the tuple. + (@count $($T:ident)*) => { + 0 $(+ impl_btree_index_bounds!(@drop $T 1))* + }; + (@drop $a:tt $b:tt) => { $b }; +} + +pub struct SingleBound; + +impl_btree_index_bounds!(A, B, C, D, E, F); + +pub enum TermBound { + Single(ops::Bound), + Range(ops::Bound, ops::Bound), +} +impl TermBound<&T> { + #[inline] + fn serialize_into(&self, buf: &mut Vec) -> Option { + let (start, end) = match self { + TermBound::Single(elem) => (elem, None), + TermBound::Range(start, end) => (start, Some(end)), + }; + let serialize_bound = |_buf: &mut Vec, _bound: &ops::Bound<&T>| { + // bsatn::to_writer(buf, bound).unwrap(); + todo!(); + }; + serialize_bound(buf, start); + end.map(|end| { + let rend_idx = buf.len(); + serialize_bound(buf, end); + rend_idx + }) + } +} +pub trait BTreeIndexBoundsTerminator { + fn bounds(&self) -> TermBound<&T>; +} + +impl BTreeIndexBoundsTerminator for T { + fn bounds(&self) -> TermBound<&T> { + TermBound::Single(ops::Bound::Included(self)) + } +} +impl BTreeIndexBoundsTerminator for &T { + fn bounds(&self) -> TermBound<&T> { + TermBound::Single(ops::Bound::Included(self)) + } +} + +macro_rules! impl_terminator { + ($($range:ty,)*) => { + $(impl BTreeIndexBoundsTerminator for $range { + fn bounds(&self) -> TermBound<&T> { + TermBound::Range( + ops::RangeBounds::start_bound(self), + ops::RangeBounds::end_bound(self), + ) + } + })* + }; +} + +impl_terminator!( + ops::Range, + ops::Range<&T>, + ops::RangeFrom, + ops::RangeFrom<&T>, + ops::RangeInclusive, + ops::RangeInclusive<&T>, + ops::RangeTo, + ops::RangeTo<&T>, + ops::RangeToInclusive, + ops::RangeToInclusive<&T>, + (ops::Bound, ops::Bound), + (ops::Bound<&T>, ops::Bound<&T>), +); + +// Single-column indices +// impl BTreeIndexBounds<(T,)> for Range {} +// impl BTreeIndexBounds<(T,)> for T {} + +// // Two-column indices +// impl BTreeIndexBounds<(T, U)> for Range {} +// impl BTreeIndexBounds<(T, U)> for T {} +// impl BTreeIndexBounds<(T, U)> for (T, Range) {} +// impl BTreeIndexBounds<(T, U)> for (T, U) {} + +// // Three-column indices +// impl BTreeIndexBounds<(T, U, V)> for Range {} +// impl BTreeIndexBounds<(T, U, V)> for T {} +// impl BTreeIndexBounds<(T, U, V)> for (T, Range) {} +// impl BTreeIndexBounds<(T, U, V)> for (T, U) {} +// impl BTreeIndexBounds<(T, U, V)> for (T, U, Range) {} +// impl BTreeIndexBounds<(T, U, V)> for (T, U, V) {} + +/// A trait for types that know if their value will trigger a sequence. +/// This is used for auto-inc columns to determine if an insertion of a row +/// will require the column to be updated in the row. +/// +/// For now, this is equivalent to a "is zero" test. +pub trait IsSequenceTrigger { + /// Is this value one that will trigger a sequence, if any, + /// when used as a column value. + fn is_sequence_trigger(&self) -> bool; +} + +macro_rules! impl_is_seq_trigger { + ($($t:ty),*) => { + $( + impl IsSequenceTrigger for $t { + fn is_sequence_trigger(&self) -> bool { *self == 0 } + } + )* + }; +} + +impl_is_seq_trigger![u8, i8, u16, i16, u32, i32, u64, i64, u128, i128]; + +impl IsSequenceTrigger for crate::sats::i256 { + fn is_sequence_trigger(&self) -> bool { + *self == Self::ZERO + } +} + +impl IsSequenceTrigger for crate::sats::u256 { + fn is_sequence_trigger(&self) -> bool { + *self == Self::ZERO + } +} + +/// Insert a row of type `T` into the table identified by `table_id`. +#[track_caller] +fn insert(mut row: T::Row) -> Result> { + let table_id = T::table_id(); + // Encode the row as bsatn into the buffer `buf`. + let mut buf = IterBuf::serialize(&row).unwrap(); + + // Insert row into table. + // When table has an auto-incrementing column, we must re-decode the changed `buf`. + let res = sys::insert(table_id, &mut buf).map(|gen_cols| { + // Let the caller handle any generated columns written back by `sys::insert` to `buf`. + T::integrate_generated_columns(&mut row, gen_cols); + row + }); + res.map_err(|e| { + let err = match e { + sys::Errno::UNIQUE_ALREADY_EXISTS => { + T::UniqueConstraintViolation::get().map(TryInsertError::UniqueConstraintViolation) + } + // sys::Errno::AUTO_INC_OVERFLOW => Tbl::AutoIncOverflow::get().map(TryInsertError::AutoIncOverflow), + _ => None, + }; + err.unwrap_or_else(|| panic!("unexpected insertion error: {e}")) + }) +} + +/// A table iterator which yields values of the `TableType` corresponding to the table. +struct TableIter { + /// The underlying source of our `Buffer`s. + inner: sys::RowIter, + + /// The current position in the buffer, from which `deserializer` can read. + reader: Cursor, + + _marker: PhantomData, +} + +impl TableIter { + #[inline] + fn new(iter: sys::RowIter) -> Self { + TableIter::new_with_buf(iter, IterBuf::take()) + } + + #[inline] + fn new_with_buf(iter: sys::RowIter, mut buf: IterBuf) -> Self { + buf.clear(); + TableIter { + inner: iter, + reader: Cursor::new(buf), + _marker: PhantomData, + } + } + + fn is_exhausted(&self) -> bool { + (&self.reader).remaining() == 0 && self.inner.is_exhausted() + } +} + +impl Iterator for TableIter { + type Item = T; + + fn next(&mut self) -> Option { + loop { + // If we currently have some bytes in the buffer to still decode, do that. + if (&self.reader).remaining() > 0 { + let row = bsatn::from_reader(&mut &self.reader).expect("Failed to decode row!"); + return Some(row); + } + + // Don't fetch the next chunk if there is none. + if self.inner.is_exhausted() { + return None; + } + + // Otherwise, try to fetch the next chunk while reusing the buffer. + self.reader.buf.clear(); + self.reader.pos.set(0); + self.inner.read(&mut self.reader.buf); + } + } +} diff --git a/crates/lib/src/filter.rs b/crates/lib/src/filter.rs index 1578ede494..403fc1ebf4 100644 --- a/crates/lib/src/filter.rs +++ b/crates/lib/src/filter.rs @@ -11,10 +11,6 @@ use spacetimedb_sats::{ProductTypeElement, Typespace}; use std::fmt::Debug; use std::marker::PhantomData; -pub trait Table { - type FieldIndex; -} - macro_rules! impl_sum { ($seed_ty:ident, $ty:ident { $($variant:ident ( $variant_ty:ty ),)* }) => { const _: () = { diff --git a/crates/sats/src/bsatn.rs b/crates/sats/src/bsatn.rs index 3de46d2395..6ec0bf3c22 100644 --- a/crates/sats/src/bsatn.rs +++ b/crates/sats/src/bsatn.rs @@ -12,21 +12,22 @@ pub use de::Deserializer; pub use ser::Serializer; pub use crate::buffer::DecodeError; +pub use ser::BsatnError as EncodeError; /// Serialize `value` into the buffered writer `w` in the BSATN format. -pub fn to_writer(w: &mut W, value: &T) -> Result<(), ser::BsatnError> { +pub fn to_writer(w: &mut W, value: &T) -> Result<(), EncodeError> { value.serialize(Serializer::new(w)) } /// Serialize `value` into a `Vec` in the BSATN format. -pub fn to_vec(value: &T) -> Result, ser::BsatnError> { +pub fn to_vec(value: &T) -> Result, EncodeError> { let mut v = Vec::new(); to_writer(&mut v, value)?; Ok(v) } /// Computes the size of `val` when BSATN encoding without actually encoding. -pub fn to_len(value: &T) -> Result { +pub fn to_len(value: &T) -> Result { let mut writer = CountWriter::default(); to_writer(&mut writer, value)?; Ok(writer.finish()) diff --git a/crates/sats/src/bsatn/ser.rs b/crates/sats/src/bsatn/ser.rs index 429fe36e13..df7b47bb85 100644 --- a/crates/sats/src/bsatn/ser.rs +++ b/crates/sats/src/bsatn/ser.rs @@ -24,6 +24,7 @@ impl<'a, W> Serializer<'a, W> { /// An error during BSATN serialization. #[derive(Debug)] +// TODO: rename to EncodeError pub struct BsatnError { /// The error message for the BSATN error. custom: String, diff --git a/modules/rust-wasm-test/src/lib.rs b/modules/rust-wasm-test/src/lib.rs index a5c36eba27..2c09dccdd5 100644 --- a/modules/rust-wasm-test/src/lib.rs +++ b/modules/rust-wasm-test/src/lib.rs @@ -1,11 +1,9 @@ #![allow(clippy::disallowed_names)] use spacetimedb::spacetimedb_lib::db::auth::StAccess; use spacetimedb::spacetimedb_lib::{self, bsatn}; -use spacetimedb::{ - duration, query, table, Address, Deserialize, Identity, ReducerContext, SpacetimeType, TableType, Timestamp, -}; +use spacetimedb::{duration, table, Address, Deserialize, Identity, ReducerContext, SpacetimeType, Table, Timestamp}; -#[spacetimedb::table(name = test_a)] +#[spacetimedb::table(name = test_a, index(name = foo, btree(columns = [x])))] pub struct TestA { #[index(btree)] pub x: u32, @@ -30,8 +28,14 @@ pub struct TestD { test_c: Option, } +// uses internal apis that should not be used by user code +#[allow(dead_code)] // false positive +const fn get_table_access() -> StAccess { + as spacetimedb::table::TableInternal>::TABLE_ACCESS +} + // This table was specified as public. -const _: () = assert!(matches!(TestD::TABLE_ACCESS, StAccess::Public)); +const _: () = assert!(matches!(get_table_access::(), StAccess::Public)); #[spacetimedb::table(name = test_e)] #[derive(Debug)] @@ -39,6 +43,7 @@ pub struct TestE { #[primary_key] #[auto_inc] id: u64, + #[index(btree)] name: String, } @@ -50,8 +55,8 @@ pub enum TestF { Baz(String), } -// All tables are private by default. -const _: () = assert!(matches!(TestE::TABLE_ACCESS, StAccess::Private)); +// // All tables are private by default. +const _: () = assert!(matches!(get_table_access::(), StAccess::Private)); #[spacetimedb::table(name = private)] pub struct Private { @@ -65,7 +70,7 @@ pub struct Point { } // It is redundant, but we can explicitly specify a table as private. -const _: () = assert!(matches!(Point::TABLE_ACCESS, StAccess::Private)); +const _: () = assert!(matches!(get_table_access::(), StAccess::Private)); // Test we can compile multiple constraints #[spacetimedb::table(name = pk_multi_identity)] @@ -93,8 +98,8 @@ pub struct HasSpecialStuff { } #[spacetimedb::reducer(init)] -pub fn init() { - let _ = RepeatingTestArg::insert(RepeatingTestArg { +pub fn init(ctx: &ReducerContext) { + ctx.db.repeating_test_arg().insert(RepeatingTestArg { prev_time: Timestamp::now(), scheduled_id: 0, scheduled_at: duration!("1000ms").into(), @@ -102,13 +107,13 @@ pub fn init() { } #[spacetimedb::reducer] -pub fn repeating_test(ctx: ReducerContext, arg: RepeatingTestArg) { +pub fn repeating_test(ctx: &ReducerContext, arg: RepeatingTestArg) { let delta_time = arg.prev_time.elapsed(); log::trace!("Timestamp: {:?}, Delta time: {:?}", ctx.timestamp, delta_time); } #[spacetimedb::reducer] -pub fn test(ctx: ReducerContext, arg: TestAlias, arg2: TestB, arg3: TestC, arg4: TestF) -> anyhow::Result<()> { +pub fn test(ctx: &ReducerContext, arg: TestAlias, arg2: TestB, arg3: TestC, arg4: TestF) -> anyhow::Result<()> { log::info!("BEGIN"); log::info!("sender: {:?}", ctx.sender); log::info!("timestamp: {:?}", ctx.timestamp); @@ -124,25 +129,25 @@ pub fn test(ctx: ReducerContext, arg: TestAlias, arg2: TestB, arg3: TestC, arg4: TestF::Baz(string) => log::info!("{}", string), } for i in 0..1000 { - TestA::insert(TestA { + ctx.db.test_a().insert(TestA { x: i + arg.x, y: i + arg.y, z: "Yo".to_owned(), }); } - let row_count_before_delete = TestA::iter().count(); + let row_count_before_delete = ctx.db.test_a().count(); log::info!("Row count before delete: {:?}", row_count_before_delete); let mut num_deleted = 0; for row in 5..10 { - num_deleted += TestA::delete_by_x(&row); + num_deleted += ctx.db.test_a().foo().delete(row); } - let row_count_after_delete = TestA::iter().count(); + let row_count_after_delete = ctx.db.test_a().count(); - if row_count_before_delete != row_count_after_delete + num_deleted as usize { + if row_count_before_delete != row_count_after_delete + num_deleted { log::error!( "Started with {} rows, deleted {}, and wound up with {} rows... huh?", row_count_before_delete, @@ -151,7 +156,7 @@ pub fn test(ctx: ReducerContext, arg: TestAlias, arg2: TestB, arg3: TestC, arg4: ); } - match TestE::insert(TestE { + match ctx.db.test_e().try_insert(TestE { id: 0, name: "Tyler".to_owned(), }) { @@ -161,20 +166,25 @@ pub fn test(ctx: ReducerContext, arg: TestAlias, arg2: TestB, arg3: TestC, arg4: log::info!("Row count after delete: {:?}", row_count_after_delete); - let other_row_count = query!(|row: TestA| row.x >= 0 && row.x <= u32::MAX).count(); + let other_row_count = ctx + .db + .test_a() + // .iter() + // .filter(|row| row.x >= 0 && row.x <= u32::MAX) + .count(); log::info!("Row count filtered by condition: {:?}", other_row_count); log::info!("MultiColumn"); for i in 0i64..1000 { - Point::insert(Point { + ctx.db.points().insert(Point { x: i + arg.x as i64, y: i + arg.y as i64, }); } - let multi_row_count = query!(|row: Point| row.x >= 0 && row.y <= 200).count(); + let multi_row_count = ctx.db.points().iter().filter(|row| row.x >= 0 && row.y <= 200).count(); log::info!("Row count filtered by multi-column condition: {:?}", multi_row_count); @@ -183,14 +193,14 @@ pub fn test(ctx: ReducerContext, arg: TestAlias, arg2: TestB, arg3: TestC, arg4: } #[spacetimedb::reducer] -pub fn add_player(name: String) -> Result<(), String> { - TestE::insert(TestE { id: 0, name })?; +pub fn add_player(ctx: &ReducerContext, name: String) -> Result<(), String> { + ctx.db.test_e().try_insert(TestE { id: 0, name })?; Ok(()) } #[spacetimedb::reducer] -pub fn delete_player(id: u64) -> Result<(), String> { - if TestE::delete_by_id(&id) { +pub fn delete_player(ctx: &ReducerContext, id: u64) -> Result<(), String> { + if ctx.db.test_e().id().delete(id) { Ok(()) } else { Err(format!("No TestE row with id {}", id)) @@ -198,8 +208,8 @@ pub fn delete_player(id: u64) -> Result<(), String> { } #[spacetimedb::reducer] -pub fn delete_players_by_name(name: String) -> Result<(), String> { - match TestE::delete_by_name(&name) { +pub fn delete_players_by_name(ctx: &ReducerContext, name: String) -> Result<(), String> { + match ctx.db.test_e().name().delete(&name) { 0 => Err(format!("No TestE row with name {:?}", name)), num_deleted => { log::info!("Deleted {} player(s) with name {:?}", num_deleted, name); @@ -209,7 +219,7 @@ pub fn delete_players_by_name(name: String) -> Result<(), String> { } #[spacetimedb::reducer(client_connected)] -fn on_connect(_ctx: ReducerContext) {} +fn on_connect(_ctx: &ReducerContext) {} // We can derive `Deserialize` for lifetime generic types: @@ -225,13 +235,13 @@ impl Foo<'_> { } #[spacetimedb::reducer] -pub fn add_private(name: String) { - Private::insert(Private { name }); +pub fn add_private(ctx: &ReducerContext, name: String) { + ctx.db.private().insert(Private { name }); } #[spacetimedb::reducer] -pub fn query_private() { - for person in Private::iter() { +pub fn query_private(ctx: &ReducerContext) { + for person in ctx.db.private().iter() { log::info!("Private, {}!", person.name); } log::info!("Private, World!"); From aa2e85fe5872f03bc8b07e502e11fdbbce61d34f Mon Sep 17 00:00:00 2001 From: Noa Date: Mon, 9 Sep 2024 21:24:42 -0500 Subject: [PATCH 3/9] Port modules to new api --- Cargo.lock | 1 + crates/sdk/examples/cursive-chat/main.rs | 2 +- .../connect_disconnect_client/src/main.rs | 4 +- .../src/module_bindings/connected.rs | 2 +- .../src/module_bindings/disconnected.rs | 2 +- .../src/module_bindings/mod.rs | 8 +- crates/sdk/tests/test-client/src/main.rs | 180 ++++----- .../src/module_bindings/large_table.rs | 2 +- .../test-client/src/module_bindings/mod.rs | 372 +++++++++--------- .../src/module_bindings/one_address.rs | 2 +- .../src/module_bindings/one_bool.rs | 2 +- .../src/module_bindings/one_byte_struct.rs | 2 +- .../module_bindings/one_enum_with_payload.rs | 2 +- .../one_every_primitive_struct.rs | 2 +- .../module_bindings/one_every_vec_struct.rs | 2 +- .../src/module_bindings/one_f_32.rs | 2 +- .../src/module_bindings/one_f_64.rs | 2 +- .../src/module_bindings/one_i_128.rs | 2 +- .../src/module_bindings/one_i_16.rs | 2 +- .../src/module_bindings/one_i_256.rs | 2 +- .../src/module_bindings/one_i_32.rs | 2 +- .../src/module_bindings/one_i_64.rs | 2 +- .../src/module_bindings/one_i_8.rs | 2 +- .../src/module_bindings/one_identity.rs | 2 +- .../src/module_bindings/one_simple_enum.rs | 2 +- .../src/module_bindings/one_string.rs | 2 +- .../src/module_bindings/one_u_128.rs | 2 +- .../src/module_bindings/one_u_16.rs | 2 +- .../src/module_bindings/one_u_256.rs | 2 +- .../src/module_bindings/one_u_32.rs | 2 +- .../src/module_bindings/one_u_64.rs | 2 +- .../src/module_bindings/one_u_8.rs | 2 +- .../src/module_bindings/one_unit_struct.rs | 2 +- .../option_every_primitive_struct.rs | 2 +- .../src/module_bindings/option_i_32.rs | 2 +- .../src/module_bindings/option_identity.rs | 2 +- .../src/module_bindings/option_simple_enum.rs | 2 +- .../src/module_bindings/option_string.rs | 2 +- .../module_bindings/option_vec_option_i_32.rs | 2 +- .../src/module_bindings/pk_address.rs | 2 +- .../src/module_bindings/pk_bool.rs | 2 +- .../src/module_bindings/pk_i_128.rs | 2 +- .../src/module_bindings/pk_i_16.rs | 2 +- .../src/module_bindings/pk_i_256.rs | 2 +- .../src/module_bindings/pk_i_32.rs | 2 +- .../src/module_bindings/pk_i_64.rs | 2 +- .../test-client/src/module_bindings/pk_i_8.rs | 2 +- .../src/module_bindings/pk_identity.rs | 2 +- .../src/module_bindings/pk_string.rs | 2 +- .../src/module_bindings/pk_u_128.rs | 2 +- .../src/module_bindings/pk_u_16.rs | 2 +- .../src/module_bindings/pk_u_256.rs | 2 +- .../src/module_bindings/pk_u_32.rs | 2 +- .../src/module_bindings/pk_u_64.rs | 2 +- .../test-client/src/module_bindings/pk_u_8.rs | 2 +- .../src/module_bindings/table_holds_table.rs | 2 +- .../src/module_bindings/unique_address.rs | 2 +- .../src/module_bindings/unique_bool.rs | 2 +- .../src/module_bindings/unique_i_128.rs | 2 +- .../src/module_bindings/unique_i_16.rs | 2 +- .../src/module_bindings/unique_i_256.rs | 2 +- .../src/module_bindings/unique_i_32.rs | 2 +- .../src/module_bindings/unique_i_64.rs | 2 +- .../src/module_bindings/unique_i_8.rs | 2 +- .../src/module_bindings/unique_identity.rs | 2 +- .../src/module_bindings/unique_string.rs | 2 +- .../src/module_bindings/unique_u_128.rs | 2 +- .../src/module_bindings/unique_u_16.rs | 2 +- .../src/module_bindings/unique_u_256.rs | 2 +- .../src/module_bindings/unique_u_32.rs | 2 +- .../src/module_bindings/unique_u_64.rs | 2 +- .../src/module_bindings/unique_u_8.rs | 2 +- .../src/module_bindings/vec_address.rs | 2 +- .../src/module_bindings/vec_bool.rs | 2 +- .../src/module_bindings/vec_byte_struct.rs | 2 +- .../module_bindings/vec_enum_with_payload.rs | 2 +- .../vec_every_primitive_struct.rs | 2 +- .../module_bindings/vec_every_vec_struct.rs | 2 +- .../src/module_bindings/vec_f_32.rs | 2 +- .../src/module_bindings/vec_f_64.rs | 2 +- .../src/module_bindings/vec_i_128.rs | 2 +- .../src/module_bindings/vec_i_16.rs | 2 +- .../src/module_bindings/vec_i_256.rs | 2 +- .../src/module_bindings/vec_i_32.rs | 2 +- .../src/module_bindings/vec_i_64.rs | 2 +- .../src/module_bindings/vec_i_8.rs | 2 +- .../src/module_bindings/vec_identity.rs | 2 +- .../src/module_bindings/vec_simple_enum.rs | 2 +- .../src/module_bindings/vec_string.rs | 2 +- .../src/module_bindings/vec_u_128.rs | 2 +- .../src/module_bindings/vec_u_16.rs | 2 +- .../src/module_bindings/vec_u_256.rs | 2 +- .../src/module_bindings/vec_u_32.rs | 2 +- .../src/module_bindings/vec_u_64.rs | 2 +- .../src/module_bindings/vec_u_8.rs | 2 +- .../src/module_bindings/vec_unit_struct.rs | 2 +- modules/benchmarks/src/circles.rs | 54 +-- modules/benchmarks/src/ia_loop.rs | 184 +++++---- modules/benchmarks/src/synthetic.rs | 233 +++++------ modules/perf-test/src/lib.rs | 27 +- modules/quickstart-chat/src/lib.rs | 42 +- .../sdk-test-connect-disconnect/src/lib.rs | 14 +- modules/sdk-test/Cargo.toml | 1 + modules/sdk-test/src/lib.rs | 85 ++-- modules/spacetimedb-quickstart/src/lib.rs | 14 +- 105 files changed, 744 insertions(+), 657 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ff060f43d6..e034a00995 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3865,6 +3865,7 @@ version = "0.1.0" dependencies = [ "anyhow", "log", + "paste", "spacetimedb", ] diff --git a/crates/sdk/examples/cursive-chat/main.rs b/crates/sdk/examples/cursive-chat/main.rs index 3f59e8aec9..cc3765df67 100644 --- a/crates/sdk/examples/cursive-chat/main.rs +++ b/crates/sdk/examples/cursive-chat/main.rs @@ -312,7 +312,7 @@ fn connect_to_db() { /// Register subscriptions for all rows of both tables. fn subscribe_to_tables() { - subscribe(&["SELECT * FROM User;", "SELECT * FROM Message;"]).unwrap(); + subscribe(&["SELECT * FROM user;", "SELECT * FROM message;"]).unwrap(); } // # Construct the user interface diff --git a/crates/sdk/tests/connect_disconnect_client/src/main.rs b/crates/sdk/tests/connect_disconnect_client/src/main.rs index 1119f0720b..6b9bd19235 100644 --- a/crates/sdk/tests/connect_disconnect_client/src/main.rs +++ b/crates/sdk/tests/connect_disconnect_client/src/main.rs @@ -35,7 +35,7 @@ fn main() { sub_applied_one_row_result(check()); }); once_on_connect(move |_, _| { - subscribe_result(subscribe(&["SELECT * FROM Connected;"])); + subscribe_result(subscribe(&["SELECT * FROM connected;"])); }); connect_result(connect(LOCALHOST, &db_name_or_panic(), None)); @@ -68,7 +68,7 @@ fn main() { sub_applied_one_row_result(check()); }); once_on_connect(move |_, _| { - subscribe_result(subscribe(&["SELECT * FROM Disconnected;"])); + subscribe_result(subscribe(&["SELECT * FROM disconnected;"])); }); connect_result(connect(LOCALHOST, &db_name_or_panic(), Some(credentials().unwrap()))); diff --git a/crates/sdk/tests/connect_disconnect_client/src/module_bindings/connected.rs b/crates/sdk/tests/connect_disconnect_client/src/module_bindings/connected.rs index 3331e43ad8..d474b36b2b 100644 --- a/crates/sdk/tests/connect_disconnect_client/src/module_bindings/connected.rs +++ b/crates/sdk/tests/connect_disconnect_client/src/module_bindings/connected.rs @@ -18,7 +18,7 @@ pub struct Connected { } impl TableType for Connected { - const TABLE_NAME: &'static str = "Connected"; + const TABLE_NAME: &'static str = "connected"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/connect_disconnect_client/src/module_bindings/disconnected.rs b/crates/sdk/tests/connect_disconnect_client/src/module_bindings/disconnected.rs index ea6e5dfb36..322d98f48a 100644 --- a/crates/sdk/tests/connect_disconnect_client/src/module_bindings/disconnected.rs +++ b/crates/sdk/tests/connect_disconnect_client/src/module_bindings/disconnected.rs @@ -18,7 +18,7 @@ pub struct Disconnected { } impl TableType for Disconnected { - const TABLE_NAME: &'static str = "Disconnected"; + const TABLE_NAME: &'static str = "disconnected"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/connect_disconnect_client/src/module_bindings/mod.rs b/crates/sdk/tests/connect_disconnect_client/src/module_bindings/mod.rs index 4721e2a8f9..89118e4816 100644 --- a/crates/sdk/tests/connect_disconnect_client/src/module_bindings/mod.rs +++ b/crates/sdk/tests/connect_disconnect_client/src/module_bindings/mod.rs @@ -41,10 +41,10 @@ impl SpacetimeModule for Module { ) { let table_name = &table_update.table_name[..]; match table_name { - "Connected" => { + "connected" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "Disconnected" => { + "disconnected" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } _ => spacetimedb_sdk::log::error!("TableRowOperation on unknown table {:?}", table_name), @@ -83,8 +83,8 @@ impl SpacetimeModule for Module { ) { let table_name = &new_subs.table_name[..]; match table_name { - "Connected" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "Disconnected" => { + "connected" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "disconnected" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } _ => spacetimedb_sdk::log::error!("TableRowOperation on unknown table {:?}", table_name), diff --git a/crates/sdk/tests/test-client/src/main.rs b/crates/sdk/tests/test-client/src/main.rs index a1fb276655..33696b2af9 100644 --- a/crates/sdk/tests/test-client/src/main.rs +++ b/crates/sdk/tests/test-client/src/main.rs @@ -239,94 +239,94 @@ fn assert_all_tables_empty() -> anyhow::Result<()> { /// A great big honking query that subscribes to all rows from all tables. const SUBSCRIBE_ALL: &[&str] = &[ - "SELECT * FROM OneU8;", - "SELECT * FROM OneU16;", - "SELECT * FROM OneU32;", - "SELECT * FROM OneU64;", - "SELECT * FROM OneU128;", - "SELECT * FROM OneU256;", - "SELECT * FROM OneI8;", - "SELECT * FROM OneI16;", - "SELECT * FROM OneI32;", - "SELECT * FROM OneI64;", - "SELECT * FROM OneI128;", - "SELECT * FROM OneI256;", - "SELECT * FROM OneBool;", - "SELECT * FROM OneF32;", - "SELECT * FROM OneF64;", - "SELECT * FROM OneString;", - "SELECT * FROM OneIdentity;", - "SELECT * FROM OneAddress;", - "SELECT * FROM OneSimpleEnum;", - "SELECT * FROM OneEnumWithPayload;", - "SELECT * FROM OneUnitStruct;", - "SELECT * FROM OneByteStruct;", - "SELECT * FROM OneEveryPrimitiveStruct;", - "SELECT * FROM OneEveryVecStruct;", - "SELECT * FROM VecU8;", - "SELECT * FROM VecU16;", - "SELECT * FROM VecU32;", - "SELECT * FROM VecU64;", - "SELECT * FROM VecU128;", - "SELECT * FROM VecU256;", - "SELECT * FROM VecI8;", - "SELECT * FROM VecI16;", - "SELECT * FROM VecI32;", - "SELECT * FROM VecI64;", - "SELECT * FROM VecI128;", - "SELECT * FROM VecI256;", - "SELECT * FROM VecBool;", - "SELECT * FROM VecF32;", - "SELECT * FROM VecF64;", - "SELECT * FROM VecString;", - "SELECT * FROM VecIdentity;", - "SELECT * FROM VecAddress;", - "SELECT * FROM VecSimpleEnum;", - "SELECT * FROM VecEnumWithPayload;", - "SELECT * FROM VecUnitStruct;", - "SELECT * FROM VecByteStruct;", - "SELECT * FROM VecEveryPrimitiveStruct;", - "SELECT * FROM VecEveryVecStruct;", - "SELECT * FROM OptionI32;", - "SELECT * FROM OptionString;", - "SELECT * FROM OptionIdentity;", - "SELECT * FROM OptionSimpleEnum;", - "SELECT * FROM OptionEveryPrimitiveStruct;", - "SELECT * FROM OptionVecOptionI32;", - "SELECT * FROM UniqueU8;", - "SELECT * FROM UniqueU16;", - "SELECT * FROM UniqueU32;", - "SELECT * FROM UniqueU64;", - "SELECT * FROM UniqueU128;", - "SELECT * FROM UniqueU256;", - "SELECT * FROM UniqueI8;", - "SELECT * FROM UniqueI16;", - "SELECT * FROM UniqueI32;", - "SELECT * FROM UniqueI64;", - "SELECT * FROM UniqueI128;", - "SELECT * FROM UniqueI256;", - "SELECT * FROM UniqueBool;", - "SELECT * FROM UniqueString;", - "SELECT * FROM UniqueIdentity;", - "SELECT * FROM UniqueAddress;", - "SELECT * FROM PkU8;", - "SELECT * FROM PkU16;", - "SELECT * FROM PkU32;", - "SELECT * FROM PkU64;", - "SELECT * FROM PkU128;", - "SELECT * FROM PkU256;", - "SELECT * FROM PkI8;", - "SELECT * FROM PkI16;", - "SELECT * FROM PkI32;", - "SELECT * FROM PkI64;", - "SELECT * FROM PkI128;", - "SELECT * FROM PkI256;", - "SELECT * FROM PkBool;", - "SELECT * FROM PkString;", - "SELECT * FROM PkIdentity;", - "SELECT * FROM PkAddress;", - "SELECT * FROM LargeTable;", - "SELECT * FROM TableHoldsTable;", + "SELECT * FROM one_u8;", + "SELECT * FROM one_u16;", + "SELECT * FROM one_u32;", + "SELECT * FROM one_u64;", + "SELECT * FROM one_u128;", + "SELECT * FROM one_u256;", + "SELECT * FROM one_i8;", + "SELECT * FROM one_i16;", + "SELECT * FROM one_i32;", + "SELECT * FROM one_i64;", + "SELECT * FROM one_i128;", + "SELECT * FROM one_i256;", + "SELECT * FROM one_bool;", + "SELECT * FROM one_f32;", + "SELECT * FROM one_f64;", + "SELECT * FROM one_string;", + "SELECT * FROM one_identity;", + "SELECT * FROM one_address;", + "SELECT * FROM one_simple_enum;", + "SELECT * FROM one_enum_with_payload;", + "SELECT * FROM one_unit_struct;", + "SELECT * FROM one_byte_struct;", + "SELECT * FROM one_every_primitive_struct;", + "SELECT * FROM one_every_vec_struct;", + "SELECT * FROM vec_u8;", + "SELECT * FROM vec_u16;", + "SELECT * FROM vec_u32;", + "SELECT * FROM vec_u64;", + "SELECT * FROM vec_u128;", + "SELECT * FROM vec_u256;", + "SELECT * FROM vec_i8;", + "SELECT * FROM vec_i16;", + "SELECT * FROM vec_i32;", + "SELECT * FROM vec_i64;", + "SELECT * FROM vec_i128;", + "SELECT * FROM vec_i256;", + "SELECT * FROM vec_bool;", + "SELECT * FROM vec_f32;", + "SELECT * FROM vec_f64;", + "SELECT * FROM vec_string;", + "SELECT * FROM vec_identity;", + "SELECT * FROM vec_address;", + "SELECT * FROM vec_simple_enum;", + "SELECT * FROM vec_enum_with_payload;", + "SELECT * FROM vec_unit_struct;", + "SELECT * FROM vec_byte_struct;", + "SELECT * FROM vec_every_primitive_struct;", + "SELECT * FROM vec_every_vec_struct;", + "SELECT * FROM option_i32;", + "SELECT * FROM option_string;", + "SELECT * FROM option_identity;", + "SELECT * FROM option_simple_enum;", + "SELECT * FROM option_every_primitive_struct;", + "SELECT * FROM option_vec_option_i32;", + "SELECT * FROM unique_u8;", + "SELECT * FROM unique_u16;", + "SELECT * FROM unique_u32;", + "SELECT * FROM unique_u64;", + "SELECT * FROM unique_u128;", + "SELECT * FROM unique_u256;", + "SELECT * FROM unique_i8;", + "SELECT * FROM unique_i16;", + "SELECT * FROM unique_i32;", + "SELECT * FROM unique_i64;", + "SELECT * FROM unique_i128;", + "SELECT * FROM unique_i256;", + "SELECT * FROM unique_bool;", + "SELECT * FROM unique_string;", + "SELECT * FROM unique_identity;", + "SELECT * FROM unique_address;", + "SELECT * FROM pk_u8;", + "SELECT * FROM pk_u16;", + "SELECT * FROM pk_u32;", + "SELECT * FROM pk_u64;", + "SELECT * FROM pk_u128;", + "SELECT * FROM pk_u256;", + "SELECT * FROM pk_i8;", + "SELECT * FROM pk_i16;", + "SELECT * FROM pk_i32;", + "SELECT * FROM pk_i64;", + "SELECT * FROM pk_i128;", + "SELECT * FROM pk_i256;", + "SELECT * FROM pk_bool;", + "SELECT * FROM pk_string;", + "SELECT * FROM pk_identity;", + "SELECT * FROM pk_address;", + "SELECT * FROM large_table;", + "SELECT * FROM table_holds_table;", ]; /// This tests that we can: @@ -1387,7 +1387,7 @@ fn exec_resubscribe() { subscribe_less_result(run_checks()); }); let subscribe_result = test_counter.add_test("resubscribe"); - subscribe_result(subscribe(&["SELECT * FROM OneU8 WHERE n > 127"])); + subscribe_result(subscribe(&["SELECT * FROM one_u8 WHERE n > 127"])); // Wait before continuing, and remove callbacks. test_counter.wait_for_all(); OneU8::remove_on_delete(on_delete_verify); @@ -1417,7 +1417,7 @@ fn exec_resubscribe() { subscribe_more_result(run_checks()); }); let subscribe_result = test_counter.add_test("resubscribe-again"); - subscribe_result(subscribe(&["SELECT * FROM OneU8"])); + subscribe_result(subscribe(&["SELECT * FROM one_u8"])); test_counter.wait_for_all(); } diff --git a/crates/sdk/tests/test-client/src/module_bindings/large_table.rs b/crates/sdk/tests/test-client/src/module_bindings/large_table.rs index 4997d33e72..f96b7293d5 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/large_table.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/large_table.rs @@ -45,7 +45,7 @@ pub struct LargeTable { } impl TableType for LargeTable { - const TABLE_NAME: &'static str = "LargeTable"; + const TABLE_NAME: &'static str = "large_table"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/mod.rs b/crates/sdk/tests/test-client/src/module_bindings/mod.rs index 4c284558bd..5dc21060f3 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/mod.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/mod.rs @@ -714,182 +714,198 @@ impl SpacetimeModule for Module { ) { let table_name = &table_update.table_name[..]; match table_name { - "LargeTable" => { + "large_table" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "OneAddress" => { + "one_address" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "OneBool" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "OneByteStruct" => client_cache + "one_bool" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "one_byte_struct" => client_cache .handle_table_update_no_primary_key::(callbacks, table_update), - "OneEnumWithPayload" => client_cache + "one_enum_with_payload" => client_cache .handle_table_update_no_primary_key::( callbacks, table_update, ), - "OneEveryPrimitiveStruct" => client_cache + "one_every_primitive_struct" => client_cache .handle_table_update_no_primary_key::( callbacks, table_update, ), - "OneEveryVecStruct" => client_cache + "one_every_vec_struct" => client_cache .handle_table_update_no_primary_key::(callbacks, table_update), - "OneF32" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "OneF64" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "OneI128" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "OneI16" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "OneI256" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "OneI32" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "OneI64" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "OneI8" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "OneIdentity" => { + "one_f32" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "one_f64" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "one_i128" => { + client_cache.handle_table_update_no_primary_key::(callbacks, table_update) + } + "one_i16" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "one_i256" => { + client_cache.handle_table_update_no_primary_key::(callbacks, table_update) + } + "one_i32" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "one_i64" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "one_i8" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "one_identity" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "OneSimpleEnum" => client_cache + "one_simple_enum" => client_cache .handle_table_update_no_primary_key::(callbacks, table_update), - "OneString" => { + "one_string" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "OneU128" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "OneU16" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "OneU256" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "OneU32" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "OneU64" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "OneU8" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "OneUnitStruct" => client_cache + "one_u128" => { + client_cache.handle_table_update_no_primary_key::(callbacks, table_update) + } + "one_u16" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "one_u256" => { + client_cache.handle_table_update_no_primary_key::(callbacks, table_update) + } + "one_u32" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "one_u64" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "one_u8" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "one_unit_struct" => client_cache .handle_table_update_no_primary_key::(callbacks, table_update), - "OptionEveryPrimitiveStruct" => client_cache + "option_every_primitive_struct" => client_cache .handle_table_update_no_primary_key::( callbacks, table_update, ), - "OptionI32" => { + "option_i32" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "OptionIdentity" => client_cache + "option_identity" => client_cache .handle_table_update_no_primary_key::(callbacks, table_update), - "OptionSimpleEnum" => client_cache + "option_simple_enum" => client_cache .handle_table_update_no_primary_key::(callbacks, table_update), - "OptionString" => { + "option_string" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "OptionVecOptionI32" => client_cache + "option_vec_option_i32" => client_cache .handle_table_update_no_primary_key::( callbacks, table_update, ), - "PkAddress" => { + "pk_address" => { client_cache.handle_table_update_with_primary_key::(callbacks, table_update) } - "PkBool" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), - "PkI128" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), - "PkI16" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), - "PkI256" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), - "PkI32" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), - "PkI64" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), - "PkI8" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), - "PkIdentity" => { + "pk_bool" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), + "pk_i128" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), + "pk_i16" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), + "pk_i256" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), + "pk_i32" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), + "pk_i64" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), + "pk_i8" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), + "pk_identity" => { client_cache.handle_table_update_with_primary_key::(callbacks, table_update) } - "PkString" => { + "pk_string" => { client_cache.handle_table_update_with_primary_key::(callbacks, table_update) } - "PkU128" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), - "PkU16" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), - "PkU256" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), - "PkU32" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), - "PkU64" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), - "PkU8" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), - "TableHoldsTable" => client_cache + "pk_u128" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), + "pk_u16" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), + "pk_u256" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), + "pk_u32" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), + "pk_u64" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), + "pk_u8" => client_cache.handle_table_update_with_primary_key::(callbacks, table_update), + "table_holds_table" => client_cache .handle_table_update_no_primary_key::(callbacks, table_update), - "UniqueAddress" => client_cache + "unique_address" => client_cache .handle_table_update_no_primary_key::(callbacks, table_update), - "UniqueBool" => { + "unique_bool" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "UniqueI128" => { + "unique_i128" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "UniqueI16" => { + "unique_i16" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "UniqueI256" => { + "unique_i256" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "UniqueI32" => { + "unique_i32" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "UniqueI64" => { + "unique_i64" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "UniqueI8" => { + "unique_i8" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "UniqueIdentity" => client_cache + "unique_identity" => client_cache .handle_table_update_no_primary_key::(callbacks, table_update), - "UniqueString" => { + "unique_string" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "UniqueU128" => { + "unique_u128" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "UniqueU16" => { + "unique_u16" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "UniqueU256" => { + "unique_u256" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "UniqueU32" => { + "unique_u32" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "UniqueU64" => { + "unique_u64" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "UniqueU8" => { + "unique_u8" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "VecAddress" => { + "vec_address" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "VecBool" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "VecByteStruct" => client_cache + "vec_bool" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "vec_byte_struct" => client_cache .handle_table_update_no_primary_key::(callbacks, table_update), - "VecEnumWithPayload" => client_cache + "vec_enum_with_payload" => client_cache .handle_table_update_no_primary_key::( callbacks, table_update, ), - "VecEveryPrimitiveStruct" => client_cache + "vec_every_primitive_struct" => client_cache .handle_table_update_no_primary_key::( callbacks, table_update, ), - "VecEveryVecStruct" => client_cache + "vec_every_vec_struct" => client_cache .handle_table_update_no_primary_key::(callbacks, table_update), - "VecF32" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "VecF64" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "VecI128" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "VecI16" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "VecI256" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "VecI32" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "VecI64" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "VecI8" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "VecIdentity" => { + "vec_f32" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "vec_f64" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "vec_i128" => { + client_cache.handle_table_update_no_primary_key::(callbacks, table_update) + } + "vec_i16" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "vec_i256" => { + client_cache.handle_table_update_no_primary_key::(callbacks, table_update) + } + "vec_i32" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "vec_i64" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "vec_i8" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "vec_identity" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "VecSimpleEnum" => client_cache + "vec_simple_enum" => client_cache .handle_table_update_no_primary_key::(callbacks, table_update), - "VecString" => { + "vec_string" => { client_cache.handle_table_update_no_primary_key::(callbacks, table_update) } - "VecU128" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "VecU16" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "VecU256" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "VecU32" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "VecU64" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "VecU8" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), - "VecUnitStruct" => client_cache + "vec_u128" => { + client_cache.handle_table_update_no_primary_key::(callbacks, table_update) + } + "vec_u16" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "vec_u256" => { + client_cache.handle_table_update_no_primary_key::(callbacks, table_update) + } + "vec_u32" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "vec_u64" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "vec_u8" => client_cache.handle_table_update_no_primary_key::(callbacks, table_update), + "vec_unit_struct" => client_cache .handle_table_update_no_primary_key::(callbacks, table_update), _ => spacetimedb_sdk::log::error!("TableRowOperation on unknown table {:?}", table_name), } @@ -1184,134 +1200,138 @@ match &reducer_call.reducer_name[..] { ) { let table_name = &new_subs.table_name[..]; match table_name { - "LargeTable" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneAddress" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneBool" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneByteStruct" => { + "large_table" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_address" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_bool" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_byte_struct" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } - "OneEnumWithPayload" => client_cache + "one_enum_with_payload" => client_cache .handle_resubscribe_for_type::(callbacks, new_subs), - "OneEveryPrimitiveStruct" => client_cache + "one_every_primitive_struct" => client_cache .handle_resubscribe_for_type::( callbacks, new_subs, ), - "OneEveryVecStruct" => { + "one_every_vec_struct" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } - "OneF32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneF64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneI128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneI16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneI256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneI32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneI64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneI8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneIdentity" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneSimpleEnum" => { + "one_f32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_f64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_i128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_i16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_i256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_i32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_i64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_i8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_identity" => { + client_cache.handle_resubscribe_for_type::(callbacks, new_subs) + } + "one_simple_enum" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } - "OneString" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneU128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneU16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneU256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneU32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneU64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneU8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OneUnitStruct" => { + "one_string" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_u128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_u16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_u256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_u32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_u64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_u8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "one_unit_struct" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } - "OptionEveryPrimitiveStruct" => client_cache + "option_every_primitive_struct" => client_cache .handle_resubscribe_for_type::( callbacks, new_subs, ), - "OptionI32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "OptionIdentity" => { + "option_i32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "option_identity" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } - "OptionSimpleEnum" => { + "option_simple_enum" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } - "OptionString" => { + "option_string" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } - "OptionVecOptionI32" => client_cache + "option_vec_option_i32" => client_cache .handle_resubscribe_for_type::(callbacks, new_subs), - "PkAddress" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "PkBool" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "PkI128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "PkI16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "PkI256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "PkI32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "PkI64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "PkI8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "PkIdentity" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "PkString" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "PkU128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "PkU16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "PkU256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "PkU32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "PkU64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "PkU8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "TableHoldsTable" => { + "pk_address" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "pk_bool" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "pk_i128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "pk_i16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "pk_i256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "pk_i32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "pk_i64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "pk_i8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "pk_identity" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "pk_string" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "pk_u128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "pk_u16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "pk_u256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "pk_u32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "pk_u64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "pk_u8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "table_holds_table" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } - "UniqueAddress" => { + "unique_address" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } - "UniqueBool" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "UniqueI128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "UniqueI16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "UniqueI256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "UniqueI32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "UniqueI64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "UniqueI8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "UniqueIdentity" => { + "unique_bool" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "unique_i128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "unique_i16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "unique_i256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "unique_i32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "unique_i64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "unique_i8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "unique_identity" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } - "UniqueString" => { + "unique_string" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } - "UniqueU128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "UniqueU16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "UniqueU256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "UniqueU32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "UniqueU64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "UniqueU8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecAddress" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecBool" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecByteStruct" => { + "unique_u128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "unique_u16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "unique_u256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "unique_u32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "unique_u64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "unique_u8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_address" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_bool" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_byte_struct" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } - "VecEnumWithPayload" => client_cache + "vec_enum_with_payload" => client_cache .handle_resubscribe_for_type::(callbacks, new_subs), - "VecEveryPrimitiveStruct" => client_cache + "vec_every_primitive_struct" => client_cache .handle_resubscribe_for_type::( callbacks, new_subs, ), - "VecEveryVecStruct" => { + "vec_every_vec_struct" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } - "VecF32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecF64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecI128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecI16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecI256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecI32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecI64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecI8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecIdentity" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecSimpleEnum" => { + "vec_f32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_f64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_i128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_i16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_i256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_i32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_i64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_i8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_identity" => { + client_cache.handle_resubscribe_for_type::(callbacks, new_subs) + } + "vec_simple_enum" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } - "VecString" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecU128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecU16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecU256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecU32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecU64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecU8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), - "VecUnitStruct" => { + "vec_string" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_u128" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_u16" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_u256" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_u32" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_u64" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_u8" => client_cache.handle_resubscribe_for_type::(callbacks, new_subs), + "vec_unit_struct" => { client_cache.handle_resubscribe_for_type::(callbacks, new_subs) } _ => spacetimedb_sdk::log::error!("TableRowOperation on unknown table {:?}", table_name), diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_address.rs b/crates/sdk/tests/test-client/src/module_bindings/one_address.rs index 90c409ecd9..58296d3933 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_address.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_address.rs @@ -18,7 +18,7 @@ pub struct OneAddress { } impl TableType for OneAddress { - const TABLE_NAME: &'static str = "OneAddress"; + const TABLE_NAME: &'static str = "one_address"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_bool.rs b/crates/sdk/tests/test-client/src/module_bindings/one_bool.rs index 9856d7a359..7cd2b4f4d9 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_bool.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_bool.rs @@ -18,7 +18,7 @@ pub struct OneBool { } impl TableType for OneBool { - const TABLE_NAME: &'static str = "OneBool"; + const TABLE_NAME: &'static str = "one_bool"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_byte_struct.rs b/crates/sdk/tests/test-client/src/module_bindings/one_byte_struct.rs index 50354fadba..d6e3efe0e6 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_byte_struct.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_byte_struct.rs @@ -19,7 +19,7 @@ pub struct OneByteStruct { } impl TableType for OneByteStruct { - const TABLE_NAME: &'static str = "OneByteStruct"; + const TABLE_NAME: &'static str = "one_byte_struct"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_enum_with_payload.rs b/crates/sdk/tests/test-client/src/module_bindings/one_enum_with_payload.rs index d130bf0c9b..5735263551 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_enum_with_payload.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_enum_with_payload.rs @@ -19,7 +19,7 @@ pub struct OneEnumWithPayload { } impl TableType for OneEnumWithPayload { - const TABLE_NAME: &'static str = "OneEnumWithPayload"; + const TABLE_NAME: &'static str = "one_enum_with_payload"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_every_primitive_struct.rs b/crates/sdk/tests/test-client/src/module_bindings/one_every_primitive_struct.rs index db0c8b52ce..256a7b32b4 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_every_primitive_struct.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_every_primitive_struct.rs @@ -19,7 +19,7 @@ pub struct OneEveryPrimitiveStruct { } impl TableType for OneEveryPrimitiveStruct { - const TABLE_NAME: &'static str = "OneEveryPrimitiveStruct"; + const TABLE_NAME: &'static str = "one_every_primitive_struct"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_every_vec_struct.rs b/crates/sdk/tests/test-client/src/module_bindings/one_every_vec_struct.rs index 4c3ae5d070..5edfa117e6 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_every_vec_struct.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_every_vec_struct.rs @@ -19,7 +19,7 @@ pub struct OneEveryVecStruct { } impl TableType for OneEveryVecStruct { - const TABLE_NAME: &'static str = "OneEveryVecStruct"; + const TABLE_NAME: &'static str = "one_every_vec_struct"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_f_32.rs b/crates/sdk/tests/test-client/src/module_bindings/one_f_32.rs index f402461a46..905ba00c9c 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_f_32.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_f_32.rs @@ -18,7 +18,7 @@ pub struct OneF32 { } impl TableType for OneF32 { - const TABLE_NAME: &'static str = "OneF32"; + const TABLE_NAME: &'static str = "one_f32"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_f_64.rs b/crates/sdk/tests/test-client/src/module_bindings/one_f_64.rs index 526957ed5f..40c4485842 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_f_64.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_f_64.rs @@ -18,7 +18,7 @@ pub struct OneF64 { } impl TableType for OneF64 { - const TABLE_NAME: &'static str = "OneF64"; + const TABLE_NAME: &'static str = "one_f64"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_i_128.rs b/crates/sdk/tests/test-client/src/module_bindings/one_i_128.rs index ad58903aed..2662e38ab5 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_i_128.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_i_128.rs @@ -18,7 +18,7 @@ pub struct OneI128 { } impl TableType for OneI128 { - const TABLE_NAME: &'static str = "OneI128"; + const TABLE_NAME: &'static str = "one_i128"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_i_16.rs b/crates/sdk/tests/test-client/src/module_bindings/one_i_16.rs index 5c605b57cc..cbfc2d4958 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_i_16.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_i_16.rs @@ -18,7 +18,7 @@ pub struct OneI16 { } impl TableType for OneI16 { - const TABLE_NAME: &'static str = "OneI16"; + const TABLE_NAME: &'static str = "one_i16"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_i_256.rs b/crates/sdk/tests/test-client/src/module_bindings/one_i_256.rs index 9eaa969217..b4b7426b14 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_i_256.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_i_256.rs @@ -18,7 +18,7 @@ pub struct OneI256 { } impl TableType for OneI256 { - const TABLE_NAME: &'static str = "OneI256"; + const TABLE_NAME: &'static str = "one_i256"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_i_32.rs b/crates/sdk/tests/test-client/src/module_bindings/one_i_32.rs index 00cde31aee..4cacdb5776 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_i_32.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_i_32.rs @@ -18,7 +18,7 @@ pub struct OneI32 { } impl TableType for OneI32 { - const TABLE_NAME: &'static str = "OneI32"; + const TABLE_NAME: &'static str = "one_i32"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_i_64.rs b/crates/sdk/tests/test-client/src/module_bindings/one_i_64.rs index ccbabdf64b..3f84f7a1cd 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_i_64.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_i_64.rs @@ -18,7 +18,7 @@ pub struct OneI64 { } impl TableType for OneI64 { - const TABLE_NAME: &'static str = "OneI64"; + const TABLE_NAME: &'static str = "one_i64"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_i_8.rs b/crates/sdk/tests/test-client/src/module_bindings/one_i_8.rs index 41f6b5263d..4d71db8135 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_i_8.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_i_8.rs @@ -18,7 +18,7 @@ pub struct OneI8 { } impl TableType for OneI8 { - const TABLE_NAME: &'static str = "OneI8"; + const TABLE_NAME: &'static str = "one_i8"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_identity.rs b/crates/sdk/tests/test-client/src/module_bindings/one_identity.rs index 60a0a21c9b..0a1e60dc8f 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_identity.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_identity.rs @@ -18,7 +18,7 @@ pub struct OneIdentity { } impl TableType for OneIdentity { - const TABLE_NAME: &'static str = "OneIdentity"; + const TABLE_NAME: &'static str = "one_identity"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_simple_enum.rs b/crates/sdk/tests/test-client/src/module_bindings/one_simple_enum.rs index fae69a9dd3..8c8987368e 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_simple_enum.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_simple_enum.rs @@ -19,7 +19,7 @@ pub struct OneSimpleEnum { } impl TableType for OneSimpleEnum { - const TABLE_NAME: &'static str = "OneSimpleEnum"; + const TABLE_NAME: &'static str = "one_simple_enum"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_string.rs b/crates/sdk/tests/test-client/src/module_bindings/one_string.rs index e4a1040ab3..4755835d53 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_string.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_string.rs @@ -18,7 +18,7 @@ pub struct OneString { } impl TableType for OneString { - const TABLE_NAME: &'static str = "OneString"; + const TABLE_NAME: &'static str = "one_string"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_u_128.rs b/crates/sdk/tests/test-client/src/module_bindings/one_u_128.rs index 85c49c9a37..c9129f3ca0 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_u_128.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_u_128.rs @@ -18,7 +18,7 @@ pub struct OneU128 { } impl TableType for OneU128 { - const TABLE_NAME: &'static str = "OneU128"; + const TABLE_NAME: &'static str = "one_u128"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_u_16.rs b/crates/sdk/tests/test-client/src/module_bindings/one_u_16.rs index 79b502f0a0..e00076054e 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_u_16.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_u_16.rs @@ -18,7 +18,7 @@ pub struct OneU16 { } impl TableType for OneU16 { - const TABLE_NAME: &'static str = "OneU16"; + const TABLE_NAME: &'static str = "one_u16"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_u_256.rs b/crates/sdk/tests/test-client/src/module_bindings/one_u_256.rs index 3326a99d16..291fd708cc 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_u_256.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_u_256.rs @@ -18,7 +18,7 @@ pub struct OneU256 { } impl TableType for OneU256 { - const TABLE_NAME: &'static str = "OneU256"; + const TABLE_NAME: &'static str = "one_u256"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_u_32.rs b/crates/sdk/tests/test-client/src/module_bindings/one_u_32.rs index ee8c3c1022..fbb94a14a4 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_u_32.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_u_32.rs @@ -18,7 +18,7 @@ pub struct OneU32 { } impl TableType for OneU32 { - const TABLE_NAME: &'static str = "OneU32"; + const TABLE_NAME: &'static str = "one_u32"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_u_64.rs b/crates/sdk/tests/test-client/src/module_bindings/one_u_64.rs index 6953551467..46b964c1f9 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_u_64.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_u_64.rs @@ -18,7 +18,7 @@ pub struct OneU64 { } impl TableType for OneU64 { - const TABLE_NAME: &'static str = "OneU64"; + const TABLE_NAME: &'static str = "one_u64"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_u_8.rs b/crates/sdk/tests/test-client/src/module_bindings/one_u_8.rs index 3e37940f13..bff3d33e9a 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_u_8.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_u_8.rs @@ -18,7 +18,7 @@ pub struct OneU8 { } impl TableType for OneU8 { - const TABLE_NAME: &'static str = "OneU8"; + const TABLE_NAME: &'static str = "one_u8"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/one_unit_struct.rs b/crates/sdk/tests/test-client/src/module_bindings/one_unit_struct.rs index 31483dac8d..4bcfc82bc6 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/one_unit_struct.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/one_unit_struct.rs @@ -19,7 +19,7 @@ pub struct OneUnitStruct { } impl TableType for OneUnitStruct { - const TABLE_NAME: &'static str = "OneUnitStruct"; + const TABLE_NAME: &'static str = "one_unit_struct"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/option_every_primitive_struct.rs b/crates/sdk/tests/test-client/src/module_bindings/option_every_primitive_struct.rs index b55aabd9a0..b5dbc9803d 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/option_every_primitive_struct.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/option_every_primitive_struct.rs @@ -19,7 +19,7 @@ pub struct OptionEveryPrimitiveStruct { } impl TableType for OptionEveryPrimitiveStruct { - const TABLE_NAME: &'static str = "OptionEveryPrimitiveStruct"; + const TABLE_NAME: &'static str = "option_every_primitive_struct"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/option_i_32.rs b/crates/sdk/tests/test-client/src/module_bindings/option_i_32.rs index a92c42b1e1..96f14b9565 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/option_i_32.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/option_i_32.rs @@ -18,7 +18,7 @@ pub struct OptionI32 { } impl TableType for OptionI32 { - const TABLE_NAME: &'static str = "OptionI32"; + const TABLE_NAME: &'static str = "option_i32"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/option_identity.rs b/crates/sdk/tests/test-client/src/module_bindings/option_identity.rs index 668ba4d805..406ed8c8dc 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/option_identity.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/option_identity.rs @@ -18,7 +18,7 @@ pub struct OptionIdentity { } impl TableType for OptionIdentity { - const TABLE_NAME: &'static str = "OptionIdentity"; + const TABLE_NAME: &'static str = "option_identity"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/option_simple_enum.rs b/crates/sdk/tests/test-client/src/module_bindings/option_simple_enum.rs index 5b12c00dee..db942efd5d 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/option_simple_enum.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/option_simple_enum.rs @@ -19,7 +19,7 @@ pub struct OptionSimpleEnum { } impl TableType for OptionSimpleEnum { - const TABLE_NAME: &'static str = "OptionSimpleEnum"; + const TABLE_NAME: &'static str = "option_simple_enum"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/option_string.rs b/crates/sdk/tests/test-client/src/module_bindings/option_string.rs index 34facf037d..09fe10f935 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/option_string.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/option_string.rs @@ -18,7 +18,7 @@ pub struct OptionString { } impl TableType for OptionString { - const TABLE_NAME: &'static str = "OptionString"; + const TABLE_NAME: &'static str = "option_string"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/option_vec_option_i_32.rs b/crates/sdk/tests/test-client/src/module_bindings/option_vec_option_i_32.rs index ac486da01d..fd5c4adcb7 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/option_vec_option_i_32.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/option_vec_option_i_32.rs @@ -18,7 +18,7 @@ pub struct OptionVecOptionI32 { } impl TableType for OptionVecOptionI32 { - const TABLE_NAME: &'static str = "OptionVecOptionI32"; + const TABLE_NAME: &'static str = "option_vec_option_i32"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_address.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_address.rs index 387841f707..5ee05f0658 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_address.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_address.rs @@ -19,7 +19,7 @@ pub struct PkAddress { } impl TableType for PkAddress { - const TABLE_NAME: &'static str = "PkAddress"; + const TABLE_NAME: &'static str = "pk_address"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_bool.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_bool.rs index aa32bc36d4..dc14a2c1f8 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_bool.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_bool.rs @@ -19,7 +19,7 @@ pub struct PkBool { } impl TableType for PkBool { - const TABLE_NAME: &'static str = "PkBool"; + const TABLE_NAME: &'static str = "pk_bool"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_i_128.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_i_128.rs index ff01d1f16d..ef1f95e2b5 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_i_128.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_i_128.rs @@ -19,7 +19,7 @@ pub struct PkI128 { } impl TableType for PkI128 { - const TABLE_NAME: &'static str = "PkI128"; + const TABLE_NAME: &'static str = "pk_i128"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_i_16.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_i_16.rs index 33ff9ea53e..644ddfc963 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_i_16.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_i_16.rs @@ -19,7 +19,7 @@ pub struct PkI16 { } impl TableType for PkI16 { - const TABLE_NAME: &'static str = "PkI16"; + const TABLE_NAME: &'static str = "pk_i16"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_i_256.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_i_256.rs index 6415e03aca..b5c97481aa 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_i_256.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_i_256.rs @@ -19,7 +19,7 @@ pub struct PkI256 { } impl TableType for PkI256 { - const TABLE_NAME: &'static str = "PkI256"; + const TABLE_NAME: &'static str = "pk_i256"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_i_32.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_i_32.rs index 00be74c248..728c6d2384 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_i_32.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_i_32.rs @@ -19,7 +19,7 @@ pub struct PkI32 { } impl TableType for PkI32 { - const TABLE_NAME: &'static str = "PkI32"; + const TABLE_NAME: &'static str = "pk_i32"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_i_64.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_i_64.rs index 8edf993eca..c10b354284 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_i_64.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_i_64.rs @@ -19,7 +19,7 @@ pub struct PkI64 { } impl TableType for PkI64 { - const TABLE_NAME: &'static str = "PkI64"; + const TABLE_NAME: &'static str = "pk_i64"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_i_8.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_i_8.rs index e113d76d84..7e568e10cf 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_i_8.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_i_8.rs @@ -19,7 +19,7 @@ pub struct PkI8 { } impl TableType for PkI8 { - const TABLE_NAME: &'static str = "PkI8"; + const TABLE_NAME: &'static str = "pk_i8"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_identity.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_identity.rs index f4fc994e18..1ac7ebb904 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_identity.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_identity.rs @@ -19,7 +19,7 @@ pub struct PkIdentity { } impl TableType for PkIdentity { - const TABLE_NAME: &'static str = "PkIdentity"; + const TABLE_NAME: &'static str = "pk_identity"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_string.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_string.rs index 62c7d201ac..9c0b164249 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_string.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_string.rs @@ -19,7 +19,7 @@ pub struct PkString { } impl TableType for PkString { - const TABLE_NAME: &'static str = "PkString"; + const TABLE_NAME: &'static str = "pk_string"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_u_128.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_u_128.rs index 53c1663a15..43c28a82bb 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_u_128.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_u_128.rs @@ -19,7 +19,7 @@ pub struct PkU128 { } impl TableType for PkU128 { - const TABLE_NAME: &'static str = "PkU128"; + const TABLE_NAME: &'static str = "pk_u128"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_u_16.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_u_16.rs index c3f8f2e7f0..01cb955ac2 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_u_16.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_u_16.rs @@ -19,7 +19,7 @@ pub struct PkU16 { } impl TableType for PkU16 { - const TABLE_NAME: &'static str = "PkU16"; + const TABLE_NAME: &'static str = "pk_u16"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_u_256.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_u_256.rs index 466b65c392..afc2d434e5 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_u_256.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_u_256.rs @@ -19,7 +19,7 @@ pub struct PkU256 { } impl TableType for PkU256 { - const TABLE_NAME: &'static str = "PkU256"; + const TABLE_NAME: &'static str = "pk_u256"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_u_32.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_u_32.rs index 03a0b54295..50d44d0a02 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_u_32.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_u_32.rs @@ -19,7 +19,7 @@ pub struct PkU32 { } impl TableType for PkU32 { - const TABLE_NAME: &'static str = "PkU32"; + const TABLE_NAME: &'static str = "pk_u32"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_u_64.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_u_64.rs index db45ad034a..caa853a765 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_u_64.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_u_64.rs @@ -19,7 +19,7 @@ pub struct PkU64 { } impl TableType for PkU64 { - const TABLE_NAME: &'static str = "PkU64"; + const TABLE_NAME: &'static str = "pk_u64"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/pk_u_8.rs b/crates/sdk/tests/test-client/src/module_bindings/pk_u_8.rs index 07f97d10c6..2d57dabd1f 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/pk_u_8.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/pk_u_8.rs @@ -19,7 +19,7 @@ pub struct PkU8 { } impl TableType for PkU8 { - const TABLE_NAME: &'static str = "PkU8"; + const TABLE_NAME: &'static str = "pk_u8"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/table_holds_table.rs b/crates/sdk/tests/test-client/src/module_bindings/table_holds_table.rs index 2ebf264770..7273dec796 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/table_holds_table.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/table_holds_table.rs @@ -21,7 +21,7 @@ pub struct TableHoldsTable { } impl TableType for TableHoldsTable { - const TABLE_NAME: &'static str = "TableHoldsTable"; + const TABLE_NAME: &'static str = "table_holds_table"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_address.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_address.rs index 6751509b08..a8d2bea3d6 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_address.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_address.rs @@ -19,7 +19,7 @@ pub struct UniqueAddress { } impl TableType for UniqueAddress { - const TABLE_NAME: &'static str = "UniqueAddress"; + const TABLE_NAME: &'static str = "unique_address"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_bool.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_bool.rs index dab6a0f3a3..ce88316791 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_bool.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_bool.rs @@ -19,7 +19,7 @@ pub struct UniqueBool { } impl TableType for UniqueBool { - const TABLE_NAME: &'static str = "UniqueBool"; + const TABLE_NAME: &'static str = "unique_bool"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_i_128.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_i_128.rs index feaf3943f8..a0a9a4dc36 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_i_128.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_i_128.rs @@ -19,7 +19,7 @@ pub struct UniqueI128 { } impl TableType for UniqueI128 { - const TABLE_NAME: &'static str = "UniqueI128"; + const TABLE_NAME: &'static str = "unique_i128"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_i_16.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_i_16.rs index 3aff9d83f2..0ac44e1f69 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_i_16.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_i_16.rs @@ -19,7 +19,7 @@ pub struct UniqueI16 { } impl TableType for UniqueI16 { - const TABLE_NAME: &'static str = "UniqueI16"; + const TABLE_NAME: &'static str = "unique_i16"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_i_256.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_i_256.rs index 1cb6d1bebb..5dda4e14c8 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_i_256.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_i_256.rs @@ -19,7 +19,7 @@ pub struct UniqueI256 { } impl TableType for UniqueI256 { - const TABLE_NAME: &'static str = "UniqueI256"; + const TABLE_NAME: &'static str = "unique_i256"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_i_32.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_i_32.rs index 4ad5c107c2..81d1ae307a 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_i_32.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_i_32.rs @@ -19,7 +19,7 @@ pub struct UniqueI32 { } impl TableType for UniqueI32 { - const TABLE_NAME: &'static str = "UniqueI32"; + const TABLE_NAME: &'static str = "unique_i32"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_i_64.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_i_64.rs index a5019d72c7..5ae6838584 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_i_64.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_i_64.rs @@ -19,7 +19,7 @@ pub struct UniqueI64 { } impl TableType for UniqueI64 { - const TABLE_NAME: &'static str = "UniqueI64"; + const TABLE_NAME: &'static str = "unique_i64"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_i_8.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_i_8.rs index 4eb5e6ca09..d3f7c0a06f 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_i_8.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_i_8.rs @@ -19,7 +19,7 @@ pub struct UniqueI8 { } impl TableType for UniqueI8 { - const TABLE_NAME: &'static str = "UniqueI8"; + const TABLE_NAME: &'static str = "unique_i8"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_identity.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_identity.rs index 9479a698a0..ea24d24644 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_identity.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_identity.rs @@ -19,7 +19,7 @@ pub struct UniqueIdentity { } impl TableType for UniqueIdentity { - const TABLE_NAME: &'static str = "UniqueIdentity"; + const TABLE_NAME: &'static str = "unique_identity"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_string.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_string.rs index 6c1b8b841a..37b927db49 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_string.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_string.rs @@ -19,7 +19,7 @@ pub struct UniqueString { } impl TableType for UniqueString { - const TABLE_NAME: &'static str = "UniqueString"; + const TABLE_NAME: &'static str = "unique_string"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_u_128.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_u_128.rs index 521a87a1d9..782e008e2f 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_u_128.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_u_128.rs @@ -19,7 +19,7 @@ pub struct UniqueU128 { } impl TableType for UniqueU128 { - const TABLE_NAME: &'static str = "UniqueU128"; + const TABLE_NAME: &'static str = "unique_u128"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_u_16.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_u_16.rs index 222b418a82..eafbcd4895 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_u_16.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_u_16.rs @@ -19,7 +19,7 @@ pub struct UniqueU16 { } impl TableType for UniqueU16 { - const TABLE_NAME: &'static str = "UniqueU16"; + const TABLE_NAME: &'static str = "unique_u16"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_u_256.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_u_256.rs index 6a7953553a..8c22bdcb63 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_u_256.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_u_256.rs @@ -19,7 +19,7 @@ pub struct UniqueU256 { } impl TableType for UniqueU256 { - const TABLE_NAME: &'static str = "UniqueU256"; + const TABLE_NAME: &'static str = "unique_u256"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_u_32.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_u_32.rs index 1238419f5d..7ee281592b 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_u_32.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_u_32.rs @@ -19,7 +19,7 @@ pub struct UniqueU32 { } impl TableType for UniqueU32 { - const TABLE_NAME: &'static str = "UniqueU32"; + const TABLE_NAME: &'static str = "unique_u32"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_u_64.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_u_64.rs index ff339e2fe5..980f00f3f6 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_u_64.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_u_64.rs @@ -19,7 +19,7 @@ pub struct UniqueU64 { } impl TableType for UniqueU64 { - const TABLE_NAME: &'static str = "UniqueU64"; + const TABLE_NAME: &'static str = "unique_u64"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/unique_u_8.rs b/crates/sdk/tests/test-client/src/module_bindings/unique_u_8.rs index 3bdc067428..2b1d775c03 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/unique_u_8.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/unique_u_8.rs @@ -19,7 +19,7 @@ pub struct UniqueU8 { } impl TableType for UniqueU8 { - const TABLE_NAME: &'static str = "UniqueU8"; + const TABLE_NAME: &'static str = "unique_u8"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_address.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_address.rs index 25d582881a..712396fb65 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_address.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_address.rs @@ -18,7 +18,7 @@ pub struct VecAddress { } impl TableType for VecAddress { - const TABLE_NAME: &'static str = "VecAddress"; + const TABLE_NAME: &'static str = "vec_address"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_bool.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_bool.rs index 8f23d0d147..2f8f240ad5 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_bool.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_bool.rs @@ -18,7 +18,7 @@ pub struct VecBool { } impl TableType for VecBool { - const TABLE_NAME: &'static str = "VecBool"; + const TABLE_NAME: &'static str = "vec_bool"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_byte_struct.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_byte_struct.rs index 197fdb178e..1764a72033 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_byte_struct.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_byte_struct.rs @@ -19,7 +19,7 @@ pub struct VecByteStruct { } impl TableType for VecByteStruct { - const TABLE_NAME: &'static str = "VecByteStruct"; + const TABLE_NAME: &'static str = "vec_byte_struct"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_enum_with_payload.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_enum_with_payload.rs index c6a42f3d1e..0ad149b76f 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_enum_with_payload.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_enum_with_payload.rs @@ -19,7 +19,7 @@ pub struct VecEnumWithPayload { } impl TableType for VecEnumWithPayload { - const TABLE_NAME: &'static str = "VecEnumWithPayload"; + const TABLE_NAME: &'static str = "vec_enum_with_payload"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_every_primitive_struct.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_every_primitive_struct.rs index ea53f96146..4dcf31a347 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_every_primitive_struct.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_every_primitive_struct.rs @@ -19,7 +19,7 @@ pub struct VecEveryPrimitiveStruct { } impl TableType for VecEveryPrimitiveStruct { - const TABLE_NAME: &'static str = "VecEveryPrimitiveStruct"; + const TABLE_NAME: &'static str = "vec_every_primitive_struct"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_every_vec_struct.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_every_vec_struct.rs index d3c235457e..64db590b8a 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_every_vec_struct.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_every_vec_struct.rs @@ -19,7 +19,7 @@ pub struct VecEveryVecStruct { } impl TableType for VecEveryVecStruct { - const TABLE_NAME: &'static str = "VecEveryVecStruct"; + const TABLE_NAME: &'static str = "vec_every_vec_struct"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_f_32.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_f_32.rs index 6ef4a146b8..7fbf3c3ab6 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_f_32.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_f_32.rs @@ -18,7 +18,7 @@ pub struct VecF32 { } impl TableType for VecF32 { - const TABLE_NAME: &'static str = "VecF32"; + const TABLE_NAME: &'static str = "vec_f32"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_f_64.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_f_64.rs index 89a63e810c..fd7130d33c 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_f_64.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_f_64.rs @@ -18,7 +18,7 @@ pub struct VecF64 { } impl TableType for VecF64 { - const TABLE_NAME: &'static str = "VecF64"; + const TABLE_NAME: &'static str = "vec_f64"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_i_128.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_i_128.rs index 501b60499a..4847fbd25a 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_i_128.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_i_128.rs @@ -18,7 +18,7 @@ pub struct VecI128 { } impl TableType for VecI128 { - const TABLE_NAME: &'static str = "VecI128"; + const TABLE_NAME: &'static str = "vec_i128"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_i_16.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_i_16.rs index a9b8de6df4..36c8e7b7b7 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_i_16.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_i_16.rs @@ -18,7 +18,7 @@ pub struct VecI16 { } impl TableType for VecI16 { - const TABLE_NAME: &'static str = "VecI16"; + const TABLE_NAME: &'static str = "vec_i16"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_i_256.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_i_256.rs index f8d453b58f..1039a862bd 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_i_256.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_i_256.rs @@ -18,7 +18,7 @@ pub struct VecI256 { } impl TableType for VecI256 { - const TABLE_NAME: &'static str = "VecI256"; + const TABLE_NAME: &'static str = "vec_i256"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_i_32.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_i_32.rs index 87f2715487..17dd8e0eb6 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_i_32.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_i_32.rs @@ -18,7 +18,7 @@ pub struct VecI32 { } impl TableType for VecI32 { - const TABLE_NAME: &'static str = "VecI32"; + const TABLE_NAME: &'static str = "vec_i32"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_i_64.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_i_64.rs index bc86cdfb0d..cf68dc7e21 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_i_64.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_i_64.rs @@ -18,7 +18,7 @@ pub struct VecI64 { } impl TableType for VecI64 { - const TABLE_NAME: &'static str = "VecI64"; + const TABLE_NAME: &'static str = "vec_i64"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_i_8.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_i_8.rs index 08f8a90c75..b8bab0359c 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_i_8.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_i_8.rs @@ -18,7 +18,7 @@ pub struct VecI8 { } impl TableType for VecI8 { - const TABLE_NAME: &'static str = "VecI8"; + const TABLE_NAME: &'static str = "vec_i8"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_identity.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_identity.rs index 4e715a9855..cbf758052a 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_identity.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_identity.rs @@ -18,7 +18,7 @@ pub struct VecIdentity { } impl TableType for VecIdentity { - const TABLE_NAME: &'static str = "VecIdentity"; + const TABLE_NAME: &'static str = "vec_identity"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_simple_enum.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_simple_enum.rs index 395c6914a1..d5ff20ec08 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_simple_enum.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_simple_enum.rs @@ -19,7 +19,7 @@ pub struct VecSimpleEnum { } impl TableType for VecSimpleEnum { - const TABLE_NAME: &'static str = "VecSimpleEnum"; + const TABLE_NAME: &'static str = "vec_simple_enum"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_string.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_string.rs index 26f238a9b7..8169c5a012 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_string.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_string.rs @@ -18,7 +18,7 @@ pub struct VecString { } impl TableType for VecString { - const TABLE_NAME: &'static str = "VecString"; + const TABLE_NAME: &'static str = "vec_string"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_u_128.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_u_128.rs index 3ea1fef347..a2f4087072 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_u_128.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_u_128.rs @@ -18,7 +18,7 @@ pub struct VecU128 { } impl TableType for VecU128 { - const TABLE_NAME: &'static str = "VecU128"; + const TABLE_NAME: &'static str = "vec_u128"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_u_16.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_u_16.rs index f7eece501c..c7d999e32c 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_u_16.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_u_16.rs @@ -18,7 +18,7 @@ pub struct VecU16 { } impl TableType for VecU16 { - const TABLE_NAME: &'static str = "VecU16"; + const TABLE_NAME: &'static str = "vec_u16"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_u_256.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_u_256.rs index e0c53fb1fe..62bb836c04 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_u_256.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_u_256.rs @@ -18,7 +18,7 @@ pub struct VecU256 { } impl TableType for VecU256 { - const TABLE_NAME: &'static str = "VecU256"; + const TABLE_NAME: &'static str = "vec_u256"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_u_32.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_u_32.rs index c23d9455fa..551ede0b66 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_u_32.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_u_32.rs @@ -18,7 +18,7 @@ pub struct VecU32 { } impl TableType for VecU32 { - const TABLE_NAME: &'static str = "VecU32"; + const TABLE_NAME: &'static str = "vec_u32"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_u_64.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_u_64.rs index e69a96b1c6..c2c1b27f0a 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_u_64.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_u_64.rs @@ -18,7 +18,7 @@ pub struct VecU64 { } impl TableType for VecU64 { - const TABLE_NAME: &'static str = "VecU64"; + const TABLE_NAME: &'static str = "vec_u64"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_u_8.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_u_8.rs index 1d838d3f98..54016a72b5 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_u_8.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_u_8.rs @@ -18,7 +18,7 @@ pub struct VecU8 { } impl TableType for VecU8 { - const TABLE_NAME: &'static str = "VecU8"; + const TABLE_NAME: &'static str = "vec_u8"; type ReducerEvent = super::ReducerEvent; } diff --git a/crates/sdk/tests/test-client/src/module_bindings/vec_unit_struct.rs b/crates/sdk/tests/test-client/src/module_bindings/vec_unit_struct.rs index 5fcdf2fe30..a277ebd6c3 100644 --- a/crates/sdk/tests/test-client/src/module_bindings/vec_unit_struct.rs +++ b/crates/sdk/tests/test-client/src/module_bindings/vec_unit_struct.rs @@ -19,7 +19,7 @@ pub struct VecUnitStruct { } impl TableType for VecUnitStruct { - const TABLE_NAME: &'static str = "VecUnitStruct"; + const TABLE_NAME: &'static str = "vec_unit_struct"; type ReducerEvent = super::ReducerEvent; } diff --git a/modules/benchmarks/src/circles.rs b/modules/benchmarks/src/circles.rs index 9e3f42ab0e..bf6cba6d88 100644 --- a/modules/benchmarks/src/circles.rs +++ b/modules/benchmarks/src/circles.rs @@ -1,6 +1,6 @@ //! STDB module used for benchmarks based on "realistic" workloads we are focusing in improving. use crate::Load; -use spacetimedb::{log, SpacetimeType, Timestamp}; +use spacetimedb::{log, ReducerContext, SpacetimeType, Table, Timestamp}; use std::hint::black_box; #[derive(SpacetimeType, Debug, Clone, Copy)] @@ -79,25 +79,29 @@ fn is_overlapping(entity1: &Entity, entity2: &Entity) -> bool { // ---------- insert bulk ---------- #[spacetimedb::reducer] -pub fn insert_bulk_entity(count: u32) { +pub fn insert_bulk_entity(ctx: &ReducerContext, count: u32) { for id in 0..count { - Entity::insert(Entity::new(0, id as f32, (id + 5) as f32, id * 5)).unwrap(); + ctx.db + .entity() + .insert(Entity::new(0, id as f32, (id + 5) as f32, id * 5)); } log::info!("INSERT ENTITY: {count}"); } #[spacetimedb::reducer] -pub fn insert_bulk_circle(count: u32) { +pub fn insert_bulk_circle(ctx: &ReducerContext, count: u32) { for id in 0..count { - Circle::insert(Circle::new(id, id, id as f32, (id + 5) as f32, (id * 5) as f32)).unwrap(); + ctx.db + .circle() + .insert(Circle::new(id, id, id as f32, (id + 5) as f32, (id * 5) as f32)); } log::info!("INSERT CIRCLE: {count}"); } #[spacetimedb::reducer] -pub fn insert_bulk_food(count: u32) { +pub fn insert_bulk_food(ctx: &ReducerContext, count: u32) { for id in 1..=count { - Food::insert(Food::new(id)).unwrap(); + ctx.db.food().insert(Food::new(id)); } log::info!("INSERT FOOD: {count}"); } @@ -107,11 +111,11 @@ pub fn insert_bulk_food(count: u32) { // SELECT * FROM Circle, Entity, Food // ``` #[spacetimedb::reducer] -pub fn cross_join_all(expected: u32) { +pub fn cross_join_all(ctx: &ReducerContext, expected: u32) { let mut count = 0; - for _circle in Circle::iter() { - for _entity in Entity::iter() { - for _food in Food::iter() { + for _circle in ctx.db.circle().iter() { + for _entity in ctx.db.entity().iter() { + for _food in ctx.db.food().iter() { count += 1; } } @@ -125,16 +129,20 @@ pub fn cross_join_all(expected: u32) { // SELECT * FROM Circle JOIN ENTITY USING(entity_id), Food JOIN ENTITY USING(entity_id) // ``` #[spacetimedb::reducer] -pub fn cross_join_circle_food(expected: u32) { +pub fn cross_join_circle_food(ctx: &ReducerContext, expected: u32) { let mut count = 0; - for circle in Circle::iter() { - let Some(circle_entity) = Entity::filter_by_id(&circle.entity_id) else { + for circle in ctx.db.circle().iter() { + let Some(circle_entity) = ctx.db.entity().id().find(circle.entity_id) else { continue; }; - for food in Food::iter() { + for food in ctx.db.food().iter() { count += 1; - let food_entity = Entity::filter_by_id(&food.entity_id) + let food_entity = ctx + .db + .entity() + .id() + .find(food.entity_id) .unwrap_or_else(|| panic!("Entity not found: {})", food.entity_id)); black_box(is_overlapping(&circle_entity, &food_entity)); } @@ -144,18 +152,18 @@ pub fn cross_join_circle_food(expected: u32) { } #[spacetimedb::reducer] -pub fn init_game_circles(initial_load: u32) { +pub fn init_game_circles(ctx: &ReducerContext, initial_load: u32) { let load = Load::new(initial_load); - insert_bulk_food(load.initial_load); - insert_bulk_entity(load.initial_load); - insert_bulk_circle(load.small_table); + insert_bulk_food(ctx, load.initial_load); + insert_bulk_entity(ctx, load.initial_load); + insert_bulk_circle(ctx, load.small_table); } #[spacetimedb::reducer] -pub fn run_game_circles(initial_load: u32) { +pub fn run_game_circles(ctx: &ReducerContext, initial_load: u32) { let load = Load::new(initial_load); - cross_join_circle_food(initial_load * load.small_table); - cross_join_all(initial_load * initial_load * load.small_table); + cross_join_circle_food(ctx, initial_load * load.small_table); + cross_join_all(ctx, initial_load * initial_load * load.small_table); } diff --git a/modules/benchmarks/src/ia_loop.rs b/modules/benchmarks/src/ia_loop.rs index 69d206805a..20869fb616 100644 --- a/modules/benchmarks/src/ia_loop.rs +++ b/modules/benchmarks/src/ia_loop.rs @@ -3,7 +3,7 @@ #![allow(clippy::too_many_arguments, unused_variables)] use crate::Load; -use spacetimedb::{log, SpacetimeType, Timestamp}; +use spacetimedb::{log, ReducerContext, SpacetimeType, Table, Timestamp}; use std::hash::{Hash, Hasher}; #[spacetimedb::table(name = velocity)] @@ -138,17 +138,21 @@ fn calculate_hash(t: &T) -> u64 { // ---------- insert bulk ---------- #[spacetimedb::reducer] -pub fn insert_bulk_position(count: u32) { +pub fn insert_bulk_position(ctx: &ReducerContext, count: u32) { for id in 0..count { - Position::insert(Position::new(id, id as f32, (id + 5) as f32, (id * 5) as f32)).unwrap(); + ctx.db + .position() + .insert(Position::new(id, id as f32, (id + 5) as f32, (id * 5) as f32)); } log::info!("INSERT POSITION: {count}"); } #[spacetimedb::reducer] -pub fn insert_bulk_velocity(count: u32) { +pub fn insert_bulk_velocity(ctx: &ReducerContext, count: u32) { for id in 0..count { - Velocity::insert(Velocity::new(id, id as f32, (id + 5) as f32, (id * 5) as f32)).unwrap(); + ctx.db + .velocity() + .insert(Velocity::new(id, id as f32, (id + 5) as f32, (id * 5) as f32)); } log::info!("INSERT VELOCITY: {count}"); } @@ -161,15 +165,15 @@ pub fn insert_bulk_velocity(count: u32) { // z = z + vz; // ``` #[spacetimedb::reducer] -pub fn update_position_all(expected: u32) { +pub fn update_position_all(ctx: &ReducerContext, expected: u32) { let mut count = 0; - for mut position in Position::iter() { + for mut position in ctx.db.position().iter() { position.x += position.vx; position.y += position.vy; position.z += position.vz; let id = position.entity_id; - Position::update_by_entity_id(&id, position); + ctx.db.position().entity_id().update(position); count += 1; } log::info!("UPDATE POSITION ALL: {expected}, processed: {count}"); @@ -186,10 +190,10 @@ pub fn update_position_all(expected: u32) { // WHERE Position.entity_id = Velocity.entity_id; // ``` #[spacetimedb::reducer] -pub fn update_position_with_velocity(expected: u32) { +pub fn update_position_with_velocity(ctx: &ReducerContext, expected: u32) { let mut count = 0; - for velocity in Velocity::iter() { - let Some(mut position) = Position::filter_by_entity_id(&velocity.entity_id) else { + for velocity in ctx.db.velocity().iter() { + let Some(mut position) = ctx.db.position().entity_id().find(velocity.entity_id) else { continue; }; @@ -198,7 +202,7 @@ pub fn update_position_with_velocity(expected: u32) { position.z += velocity.z; let id = position.entity_id; - Position::update_by_entity_id(&id, position); + ctx.db.position().entity_id().update(position); count += 1; } log::info!("UPDATE POSITION BY VELOCITY: {expected}, processed: {count}"); @@ -207,7 +211,7 @@ pub fn update_position_with_velocity(expected: u32) { // Simulations for a game loop #[spacetimedb::reducer] -pub fn insert_world(players: u64) { +pub fn insert_world(ctx: &ReducerContext, players: u64) { for (i, id) in (0..players).enumerate() { let next_action_timestamp = if i & 2 == 2 { moment_milliseconds() + 2000 // Check every 2secs @@ -215,41 +219,36 @@ pub fn insert_world(players: u64) { moment_milliseconds() }; - GameEnemyAiAgentState::insert(GameEnemyAiAgentState { + ctx.db.game_enemy_ai_agent_state().insert(GameEnemyAiAgentState { entity_id: id, next_action_timestamp, last_move_timestamps: vec![id, 0, id * 2], action: AgentAction::Idle, - }) - .unwrap(); + }); - GameLiveTargetableState::insert(GameLiveTargetableState { + ctx.db.game_live_targetable_state().insert(GameLiveTargetableState { entity_id: id, quad: id as i64, - }) - .unwrap(); + }); - GameTargetableState::insert(GameTargetableState { + ctx.db.game_targetable_state().insert(GameTargetableState { entity_id: id, quad: id as i64, - }) - .unwrap(); + }); - GameMobileEntityState::insert(GameMobileEntityState { + ctx.db.game_mobile_entity_state().insert(GameMobileEntityState { entity_id: id, location_x: id as i32, location_y: id as i32, timestamp: next_action_timestamp, - }) - .unwrap(); + }); - GameEnemyState::insert(GameEnemyState { + ctx.db.game_enemy_state().insert(GameEnemyState { entity_id: id, herd_id: id as i32, - }) - .unwrap(); + }); - GameHerdCache::insert(GameHerdCache { + ctx.db.game_herd_cache().insert(GameHerdCache { id: id as i32, dimension_id: id as u32, current_population: id as i32 * 2, @@ -261,18 +260,23 @@ pub fn insert_world(players: u64) { z: id as i32, dimension: id as u32 * 2, }, - }) - .unwrap(); + }); } log::info!("INSERT WORLD PLAYERS: {players}"); } -fn get_targetables_near_quad(entity_id: u64, num_players: u64) -> Vec { +fn get_targetables_near_quad(ctx: &ReducerContext, entity_id: u64, num_players: u64) -> Vec { let mut result = Vec::with_capacity(4); for id in entity_id..num_players { - for t in GameLiveTargetableState::filter_by_quad(&(id as i64)) { - result.push(GameTargetableState::filter_by_entity_id(&t.entity_id).expect("Identity not found")) + for t in ctx.db.game_live_targetable_state().quad().filter(&(id as i64)) { + result.push( + ctx.db + .game_targetable_state() + .entity_id() + .find(t.entity_id) + .expect("Identity not found"), + ) } } @@ -280,13 +284,22 @@ fn get_targetables_near_quad(entity_id: u64, num_players: u64) -> Vec current_time_ms { continue; } - let agent_targetable = GameTargetableState::filter_by_entity_id(&agent.entity_id) + let agent_targetable = ctx + .db + .game_targetable_state() + .entity_id() + .find(agent.entity_id) .expect("No TargetableState for AgentState entity"); - let surrounding_agents = get_targetables_near_quad(agent_targetable.entity_id, players); + let surrounding_agents = get_targetables_near_quad(ctx, agent_targetable.entity_id, players); agent.action = AgentAction::Fighting; - agent_loop(agent, agent_targetable, &surrounding_agents, current_time_ms); + agent_loop(ctx, agent, agent_targetable, &surrounding_agents, current_time_ms); count += 1; } @@ -372,20 +418,20 @@ pub fn game_loop_enemy_ia(players: u64) { } #[spacetimedb::reducer] -pub fn init_game_ia_loop(initial_load: u32) { +pub fn init_game_ia_loop(ctx: &ReducerContext, initial_load: u32) { let load = Load::new(initial_load); - insert_bulk_position(load.biggest_table); - insert_bulk_velocity(load.big_table); - update_position_all(load.biggest_table); - update_position_with_velocity(load.big_table); + insert_bulk_position(ctx, load.biggest_table); + insert_bulk_velocity(ctx, load.big_table); + update_position_all(ctx, load.biggest_table); + update_position_with_velocity(ctx, load.big_table); - insert_world(load.num_players as u64); + insert_world(ctx, load.num_players as u64); } #[spacetimedb::reducer] -pub fn run_game_ia_loop(initial_load: u32) { +pub fn run_game_ia_loop(ctx: &ReducerContext, initial_load: u32) { let load = Load::new(initial_load); - game_loop_enemy_ia(load.num_players as u64); + game_loop_enemy_ia(ctx, load.num_players as u64); } diff --git a/modules/benchmarks/src/synthetic.rs b/modules/benchmarks/src/synthetic.rs index 05f930d220..4d0f1ff04c 100644 --- a/modules/benchmarks/src/synthetic.rs +++ b/modules/benchmarks/src/synthetic.rs @@ -23,13 +23,13 @@ //! Obviously more could be added... #![allow(non_camel_case_types)] #![allow(clippy::too_many_arguments)] -use spacetimedb::println; +use spacetimedb::{println, ReducerContext, Table}; use std::hint::black_box; // ---------- schemas ---------- #[spacetimedb::table(name = unique_0_u32_u64_str)] -pub struct unique_0_u32_u64_str { +pub struct unique_0_u32_u64_str_t { #[unique] id: u32, age: u64, @@ -37,14 +37,14 @@ pub struct unique_0_u32_u64_str { } #[spacetimedb::table(name = no_index_u32_u64_str)] -pub struct no_index_u32_u64_str { +pub struct no_index_u32_u64_str_t { id: u32, age: u64, name: String, } #[spacetimedb::table(name = btree_each_column_u32_u64_str)] -pub struct btree_each_column_u32_u64_str { +pub struct btree_each_column_u32_u64_str_t { #[index(btree)] id: u32, #[index(btree)] @@ -54,7 +54,7 @@ pub struct btree_each_column_u32_u64_str { } #[spacetimedb::table(name = unique_0_u32_u64_u64)] -pub struct unique_0_u32_u64_u64 { +pub struct unique_0_u32_u64_u64_t { #[unique] id: u32, x: u64, @@ -62,14 +62,14 @@ pub struct unique_0_u32_u64_u64 { } #[spacetimedb::table(name = no_index_u32_u64_u64)] -pub struct no_index_u32_u64_u64 { +pub struct no_index_u32_u64_u64_t { id: u32, x: u64, y: u64, } #[spacetimedb::table(name = btree_each_column_u32_u64_u64)] -pub struct btree_each_column_u32_u64_u64 { +pub struct btree_each_column_u32_u64_u64_t { #[index(btree)] id: u32, #[index(btree)] @@ -81,121 +81,121 @@ pub struct btree_each_column_u32_u64_u64 { // ---------- empty ---------- #[spacetimedb::reducer] -pub fn empty() {} +pub fn empty(_ctx: &ReducerContext) {} // ---------- insert ---------- #[spacetimedb::reducer] -pub fn insert_unique_0_u32_u64_str(id: u32, age: u64, name: String) { - unique_0_u32_u64_str::insert(unique_0_u32_u64_str { id, name, age }).unwrap(); +pub fn insert_unique_0_u32_u64_str(ctx: &ReducerContext, id: u32, age: u64, name: String) { + ctx.db + .unique_0_u32_u64_str() + .insert(unique_0_u32_u64_str_t { id, name, age }); } #[spacetimedb::reducer] -pub fn insert_no_index_u32_u64_str(id: u32, age: u64, name: String) { - no_index_u32_u64_str::insert(no_index_u32_u64_str { id, name, age }); +pub fn insert_no_index_u32_u64_str(ctx: &ReducerContext, id: u32, age: u64, name: String) { + ctx.db + .no_index_u32_u64_str() + .insert(no_index_u32_u64_str_t { id, name, age }); } #[spacetimedb::reducer] -pub fn insert_btree_each_column_u32_u64_str(id: u32, age: u64, name: String) { - btree_each_column_u32_u64_str::insert(btree_each_column_u32_u64_str { id, name, age }); +pub fn insert_btree_each_column_u32_u64_str(ctx: &ReducerContext, id: u32, age: u64, name: String) { + ctx.db + .btree_each_column_u32_u64_str() + .insert(btree_each_column_u32_u64_str_t { id, name, age }); } #[spacetimedb::reducer] -pub fn insert_unique_0_u32_u64_u64(id: u32, x: u64, y: u64) { - unique_0_u32_u64_u64::insert(unique_0_u32_u64_u64 { id, x, y }).unwrap(); +pub fn insert_unique_0_u32_u64_u64(ctx: &ReducerContext, id: u32, x: u64, y: u64) { + ctx.db + .unique_0_u32_u64_u64() + .insert(unique_0_u32_u64_u64_t { id, x, y }); } #[spacetimedb::reducer] -pub fn insert_no_index_u32_u64_u64(id: u32, x: u64, y: u64) { - no_index_u32_u64_u64::insert(no_index_u32_u64_u64 { id, x, y }); +pub fn insert_no_index_u32_u64_u64(ctx: &ReducerContext, id: u32, x: u64, y: u64) { + ctx.db + .no_index_u32_u64_u64() + .insert(no_index_u32_u64_u64_t { id, x, y }); } #[spacetimedb::reducer] -pub fn insert_btree_each_column_u32_u64_u64(id: u32, x: u64, y: u64) { - btree_each_column_u32_u64_u64::insert(btree_each_column_u32_u64_u64 { id, x, y }); +pub fn insert_btree_each_column_u32_u64_u64(ctx: &ReducerContext, id: u32, x: u64, y: u64) { + ctx.db + .btree_each_column_u32_u64_u64() + .insert(btree_each_column_u32_u64_u64_t { id, x, y }); } // ---------- insert bulk ---------- #[spacetimedb::reducer] -pub fn insert_bulk_unique_0_u32_u64_u64(locs: Vec) { +pub fn insert_bulk_unique_0_u32_u64_u64(ctx: &ReducerContext, locs: Vec) { for loc in locs { - unique_0_u32_u64_u64::insert(loc).unwrap(); + ctx.db.unique_0_u32_u64_u64().insert(loc); } } #[spacetimedb::reducer] -pub fn insert_bulk_no_index_u32_u64_u64(locs: Vec) { +pub fn insert_bulk_no_index_u32_u64_u64(ctx: &ReducerContext, locs: Vec) { for loc in locs { - no_index_u32_u64_u64::insert(loc); + ctx.db.no_index_u32_u64_u64().insert(loc); } } #[spacetimedb::reducer] -pub fn insert_bulk_btree_each_column_u32_u64_u64(locs: Vec) { +pub fn insert_bulk_btree_each_column_u32_u64_u64(ctx: &ReducerContext, locs: Vec) { for loc in locs { - btree_each_column_u32_u64_u64::insert(loc); + ctx.db.btree_each_column_u32_u64_u64().insert(loc); } } #[spacetimedb::reducer] -pub fn insert_bulk_unique_0_u32_u64_str(people: Vec) { +pub fn insert_bulk_unique_0_u32_u64_str(ctx: &ReducerContext, people: Vec) { for u32_u64_str in people { - unique_0_u32_u64_str::insert(u32_u64_str).unwrap(); + ctx.db.unique_0_u32_u64_str().insert(u32_u64_str); } } #[spacetimedb::reducer] -pub fn insert_bulk_no_index_u32_u64_str(people: Vec) { +pub fn insert_bulk_no_index_u32_u64_str(ctx: &ReducerContext, people: Vec) { for u32_u64_str in people { - no_index_u32_u64_str::insert(u32_u64_str); + ctx.db.no_index_u32_u64_str().insert(u32_u64_str); } } #[spacetimedb::reducer] -pub fn insert_bulk_btree_each_column_u32_u64_str(people: Vec) { +pub fn insert_bulk_btree_each_column_u32_u64_str(ctx: &ReducerContext, people: Vec) { for u32_u64_str in people { - btree_each_column_u32_u64_str::insert(u32_u64_str); + ctx.db.btree_each_column_u32_u64_str().insert(u32_u64_str); } } // ---------- update ---------- #[spacetimedb::reducer] -pub fn update_bulk_unique_0_u32_u64_u64(row_count: u32) { +pub fn update_bulk_unique_0_u32_u64_u64(ctx: &ReducerContext, row_count: u32) { let mut hit: u32 = 0; - for loc in unique_0_u32_u64_u64::iter().take(row_count as usize) { + for loc in ctx.db.unique_0_u32_u64_u64().iter().take(row_count as usize) { hit += 1; - assert!( - unique_0_u32_u64_u64::update_by_id( - &loc.id, - unique_0_u32_u64_u64 { - id: loc.id, - x: loc.x.wrapping_add(1), - y: loc.y, - }, - ), - "failed to update u32_u64_u64" - ); + ctx.db.unique_0_u32_u64_u64().id().update(unique_0_u32_u64_u64_t { + id: loc.id, + x: loc.x.wrapping_add(1), + y: loc.y, + }); } assert_eq!(hit, row_count, "not enough rows to perform requested amount of updates"); } #[spacetimedb::reducer] -pub fn update_bulk_unique_0_u32_u64_str(row_count: u32) { +pub fn update_bulk_unique_0_u32_u64_str(ctx: &ReducerContext, row_count: u32) { let mut hit: u32 = 0; - for u32_u64_str in unique_0_u32_u64_str::iter().take(row_count as usize) { + for u32_u64_str in ctx.db.unique_0_u32_u64_str().iter().take(row_count as usize) { hit += 1; - assert!( - unique_0_u32_u64_str::update_by_id( - &u32_u64_str.id, - unique_0_u32_u64_str { - id: u32_u64_str.id, - name: u32_u64_str.name, - age: u32_u64_str.age.wrapping_add(1), - }, - ), - "failed to update u32_u64_str" - ); + ctx.db.unique_0_u32_u64_str().id().update(unique_0_u32_u64_str_t { + id: u32_u64_str.id, + name: u32_u64_str.name, + age: u32_u64_str.age.wrapping_add(1), + }); } assert_eq!(hit, row_count, "not enough rows to perform requested amount of updates"); } @@ -203,14 +203,14 @@ pub fn update_bulk_unique_0_u32_u64_str(row_count: u32) { // ---------- iterate ---------- #[spacetimedb::reducer] -pub fn iterate_unique_0_u32_u64_str() { - for u32_u64_str in unique_0_u32_u64_str::iter() { +pub fn iterate_unique_0_u32_u64_str(ctx: &ReducerContext) { + for u32_u64_str in ctx.db.unique_0_u32_u64_str().iter() { black_box(u32_u64_str); } } #[spacetimedb::reducer] -pub fn iterate_unique_0_u32_u64_u64() { - for u32_u64_u64 in unique_0_u32_u64_u64::iter() { +pub fn iterate_unique_0_u32_u64_u64(ctx: &ReducerContext) { + for u32_u64_u64 in ctx.db.unique_0_u32_u64_u64().iter() { black_box(u32_u64_u64); } } @@ -218,106 +218,106 @@ pub fn iterate_unique_0_u32_u64_u64() { // ---------- filtering ---------- #[spacetimedb::reducer] -pub fn filter_unique_0_u32_u64_str_by_id(id: u32) { - if let Some(p) = unique_0_u32_u64_str::filter_by_id(&id) { +pub fn filter_unique_0_u32_u64_str_by_id(ctx: &ReducerContext, id: u32) { + if let Some(p) = ctx.db.unique_0_u32_u64_str().id().find(id) { black_box(p); } } #[spacetimedb::reducer] -pub fn filter_no_index_u32_u64_str_by_id(id: u32) { - for p in no_index_u32_u64_str::filter_by_id(&id) { +pub fn filter_no_index_u32_u64_str_by_id(ctx: &ReducerContext, id: u32) { + for p in ctx.db.no_index_u32_u64_str().iter().filter(|p| p.id == id) { black_box(p); } } #[spacetimedb::reducer] -pub fn filter_btree_each_column_u32_u64_str_by_id(id: u32) { - for p in btree_each_column_u32_u64_str::filter_by_id(&id) { +pub fn filter_btree_each_column_u32_u64_str_by_id(ctx: &ReducerContext, id: u32) { + for p in ctx.db.btree_each_column_u32_u64_str().id().filter(&id) { black_box(p); } } #[spacetimedb::reducer] -pub fn filter_unique_0_u32_u64_str_by_name(name: String) { - for p in unique_0_u32_u64_str::filter_by_name(&name) { +pub fn filter_unique_0_u32_u64_str_by_name(ctx: &ReducerContext, name: String) { + for p in ctx.db.unique_0_u32_u64_str().iter().filter(|p| p.name == name) { black_box(p); } } #[spacetimedb::reducer] -pub fn filter_no_index_u32_u64_str_by_name(name: String) { - for p in no_index_u32_u64_str::filter_by_name(&name) { +pub fn filter_no_index_u32_u64_str_by_name(ctx: &ReducerContext, name: String) { + for p in ctx.db.no_index_u32_u64_str().iter().filter(|p| p.name == name) { black_box(p); } } #[spacetimedb::reducer] -pub fn filter_btree_each_column_u32_u64_str_by_name(name: String) { - for p in btree_each_column_u32_u64_str::filter_by_name(&name) { +pub fn filter_btree_each_column_u32_u64_str_by_name(ctx: &ReducerContext, name: String) { + for p in ctx.db.btree_each_column_u32_u64_str().name().filter(&name) { black_box(p); } } #[spacetimedb::reducer] -pub fn filter_unique_0_u32_u64_u64_by_id(id: u32) { - if let Some(loc) = unique_0_u32_u64_u64::filter_by_id(&id) { +pub fn filter_unique_0_u32_u64_u64_by_id(ctx: &ReducerContext, id: u32) { + if let Some(loc) = ctx.db.unique_0_u32_u64_u64().id().find(id) { black_box(loc); } } #[spacetimedb::reducer] -pub fn filter_no_index_u32_u64_u64_by_id(id: u32) { - for loc in no_index_u32_u64_u64::filter_by_id(&id) { +pub fn filter_no_index_u32_u64_u64_by_id(ctx: &ReducerContext, id: u32) { + for loc in ctx.db.no_index_u32_u64_u64().iter().filter(|p| p.id == id) { black_box(loc); } } #[spacetimedb::reducer] -pub fn filter_btree_each_column_u32_u64_u64_by_id(id: u32) { - for loc in btree_each_column_u32_u64_u64::filter_by_id(&id) { +pub fn filter_btree_each_column_u32_u64_u64_by_id(ctx: &ReducerContext, id: u32) { + for loc in ctx.db.btree_each_column_u32_u64_u64().id().filter(&id) { black_box(loc); } } #[spacetimedb::reducer] -pub fn filter_unique_0_u32_u64_u64_by_x(x: u64) { - for loc in unique_0_u32_u64_u64::filter_by_x(&x) { +pub fn filter_unique_0_u32_u64_u64_by_x(ctx: &ReducerContext, x: u64) { + for loc in ctx.db.unique_0_u32_u64_u64().iter().filter(|p| p.x == x) { black_box(loc); } } #[spacetimedb::reducer] -pub fn filter_no_index_u32_u64_u64_by_x(x: u64) { - for loc in no_index_u32_u64_u64::filter_by_x(&x) { +pub fn filter_no_index_u32_u64_u64_by_x(ctx: &ReducerContext, x: u64) { + for loc in ctx.db.no_index_u32_u64_u64().iter().filter(|p| p.x == x) { black_box(loc); } } #[spacetimedb::reducer] -pub fn filter_btree_each_column_u32_u64_u64_by_x(x: u64) { - for loc in btree_each_column_u32_u64_u64::filter_by_x(&x) { +pub fn filter_btree_each_column_u32_u64_u64_by_x(ctx: &ReducerContext, x: u64) { + for loc in ctx.db.btree_each_column_u32_u64_u64().x().filter(&x) { black_box(loc); } } #[spacetimedb::reducer] -pub fn filter_unique_0_u32_u64_u64_by_y(x: u64) { - for loc in unique_0_u32_u64_u64::filter_by_y(&x) { +pub fn filter_unique_0_u32_u64_u64_by_y(ctx: &ReducerContext, x: u64) { + for loc in ctx.db.unique_0_u32_u64_u64().iter().filter(|p| p.y == x) { black_box(loc); } } #[spacetimedb::reducer] -pub fn filter_no_index_u32_u64_u64_by_y(x: u64) { - for loc in no_index_u32_u64_u64::filter_by_y(&x) { +pub fn filter_no_index_u32_u64_u64_by_y(ctx: &ReducerContext, x: u64) { + for loc in ctx.db.no_index_u32_u64_u64().iter().filter(|p| p.y == x) { black_box(loc); } } #[spacetimedb::reducer] -pub fn filter_btree_each_column_u32_u64_u64_by_y(x: u64) { - for loc in btree_each_column_u32_u64_u64::filter_by_y(&x) { +pub fn filter_btree_each_column_u32_u64_u64_by_y(ctx: &ReducerContext, x: u64) { + for loc in ctx.db.btree_each_column_u32_u64_u64().y().filter(&x) { black_box(loc); } } @@ -327,43 +327,43 @@ pub fn filter_btree_each_column_u32_u64_u64_by_y(x: u64) { // FIXME: current nonunique delete interface is UNUSABLE!!!! #[spacetimedb::reducer] -pub fn delete_unique_0_u32_u64_str_by_id(id: u32) { - unique_0_u32_u64_str::delete_by_id(&id); +pub fn delete_unique_0_u32_u64_str_by_id(ctx: &ReducerContext, id: u32) { + ctx.db.unique_0_u32_u64_str().id().delete(id); } #[spacetimedb::reducer] -pub fn delete_unique_0_u32_u64_u64_by_id(id: u32) { - unique_0_u32_u64_u64::delete_by_id(&id); +pub fn delete_unique_0_u32_u64_u64_by_id(ctx: &ReducerContext, id: u32) { + ctx.db.unique_0_u32_u64_u64().id().delete(id); } // ---------- clear table ---------- #[spacetimedb::reducer] -pub fn clear_table_unique_0_u32_u64_str() { +pub fn clear_table_unique_0_u32_u64_str(_ctx: &ReducerContext) { unimplemented!("Modules currently have no interface to clear a table"); } #[spacetimedb::reducer] -pub fn clear_table_no_index_u32_u64_str() { +pub fn clear_table_no_index_u32_u64_str(_ctx: &ReducerContext) { unimplemented!("Modules currently have no interface to clear a table"); } #[spacetimedb::reducer] -pub fn clear_table_btree_each_column_u32_u64_str() { +pub fn clear_table_btree_each_column_u32_u64_str(_ctx: &ReducerContext) { unimplemented!("Modules currently have no interface to clear a table"); } #[spacetimedb::reducer] -pub fn clear_table_unique_0_u32_u64_u64() { +pub fn clear_table_unique_0_u32_u64_u64(_ctx: &ReducerContext) { unimplemented!("Modules currently have no interface to clear a table"); } #[spacetimedb::reducer] -pub fn clear_table_no_index_u32_u64_u64() { +pub fn clear_table_no_index_u32_u64_u64(_ctx: &ReducerContext) { unimplemented!("Modules currently have no interface to clear a table"); } #[spacetimedb::reducer] -pub fn clear_table_btree_each_column_u32_u64_u64() { +pub fn clear_table_btree_each_column_u32_u64_u64(_ctx: &ReducerContext) { unimplemented!("Modules currently have no interface to clear a table"); } // ---------- count ---------- @@ -371,41 +371,42 @@ pub fn clear_table_btree_each_column_u32_u64_u64() { // You need to inspect the module outputs to actually read the result from these. #[spacetimedb::reducer] -pub fn count_unique_0_u32_u64_str() { - println!("COUNT: {}", unique_0_u32_u64_str::iter().count()); +pub fn count_unique_0_u32_u64_str(ctx: &ReducerContext) { + println!("COUNT: {}", ctx.db.unique_0_u32_u64_str().count()); } #[spacetimedb::reducer] -pub fn count_no_index_u32_u64_str() { - println!("COUNT: {}", no_index_u32_u64_str::iter().count()); +pub fn count_no_index_u32_u64_str(ctx: &ReducerContext) { + println!("COUNT: {}", ctx.db.no_index_u32_u64_str().count()); } #[spacetimedb::reducer] -pub fn count_btree_each_column_u32_u64_str() { - println!("COUNT: {}", btree_each_column_u32_u64_str::iter().count()); +pub fn count_btree_each_column_u32_u64_str(ctx: &ReducerContext) { + println!("COUNT: {}", ctx.db.btree_each_column_u32_u64_str().count()); } #[spacetimedb::reducer] -pub fn count_unique_0_u32_u64_u64() { - println!("COUNT: {}", unique_0_u32_u64_u64::iter().count()); +pub fn count_unique_0_u32_u64_u64(ctx: &ReducerContext) { + println!("COUNT: {}", ctx.db.unique_0_u32_u64_u64().count()); } #[spacetimedb::reducer] -pub fn count_no_index_u32_u64_u64() { - println!("COUNT: {}", no_index_u32_u64_u64::iter().count()); +pub fn count_no_index_u32_u64_u64(ctx: &ReducerContext) { + println!("COUNT: {}", ctx.db.no_index_u32_u64_u64().count()); } #[spacetimedb::reducer] -pub fn count_btree_each_column_u32_u64_u64() { - println!("COUNT: {}", btree_each_column_u32_u64_u64::iter().count()); +pub fn count_btree_each_column_u32_u64_u64(ctx: &ReducerContext) { + println!("COUNT: {}", ctx.db.btree_each_column_u32_u64_u64().count()); } // ---------- module-specific stuff ---------- #[spacetimedb::reducer] -pub fn fn_with_1_args(_arg: String) {} +pub fn fn_with_1_args(_ctx: &ReducerContext, _arg: String) {} #[spacetimedb::reducer] pub fn fn_with_32_args( + _ctx: &ReducerContext, _arg1: String, _arg2: String, _arg3: String, @@ -442,7 +443,7 @@ pub fn fn_with_32_args( } #[spacetimedb::reducer] -pub fn print_many_things(n: u32) { +pub fn print_many_things(_ctx: &ReducerContext, n: u32) { for _ in 0..n { println!("hello again!"); } diff --git a/modules/perf-test/src/lib.rs b/modules/perf-test/src/lib.rs index 82ed03fc7d..b2caf8e08d 100644 --- a/modules/perf-test/src/lib.rs +++ b/modules/perf-test/src/lib.rs @@ -1,4 +1,4 @@ -use spacetimedb::{log_stopwatch::LogStopwatch, query}; +use spacetimedb::{log_stopwatch::LogStopwatch, ReducerContext, Table}; #[spacetimedb::table(name = location, index(name = coordinates, btree(columns = [x, z, dimension])))] #[derive(Debug, PartialEq, Eq)] @@ -18,14 +18,14 @@ const NUM_CHUNKS: u64 = 1000; const ROWS_PER_CHUNK: u64 = 1200; #[spacetimedb::reducer] -pub fn load_location_table() { +pub fn load_location_table(ctx: &ReducerContext) { for chunk in 0u64..NUM_CHUNKS { for i in 0u64..ROWS_PER_CHUNK { let id = chunk * 1200 + i; let x = 0i32; let z = chunk as i32; let dimension = id as u32; - let _ = Location::insert(Location { + ctx.db.location().insert(Location { id, chunk, x, @@ -41,39 +41,44 @@ const CHUNK: u64 = ID / ROWS_PER_CHUNK; #[spacetimedb::reducer] /// Probing a single column index for a single row should be fast! -pub fn test_index_scan_on_id() { +pub fn test_index_scan_on_id(ctx: &ReducerContext) { let span = LogStopwatch::new("Index scan on {id}"); - let location = Location::filter_by_id(&ID).unwrap(); + let location = ctx.db.location().id().find(ID).unwrap(); span.end(); assert_eq!(ID, location.id); } #[spacetimedb::reducer] /// Scanning a single column index for `ROWS_PER_CHUNK` rows should also be fast! -pub fn test_index_scan_on_chunk() { +pub fn test_index_scan_on_chunk(ctx: &ReducerContext) { let span = LogStopwatch::new("Index scan on {chunk}"); - let n = Location::filter_by_chunk(&CHUNK).count(); + let n = ctx.db.location().chunk().filter(&CHUNK).count(); span.end(); assert_eq!(n as u64, ROWS_PER_CHUNK); } #[spacetimedb::reducer] /// Probing a multi-column index for a single row should be fast! -pub fn test_index_scan_on_x_z_dimension() { +pub fn test_index_scan_on_x_z_dimension(ctx: &ReducerContext) { let z = CHUNK as i32; let dimension = ID as u32; let span = LogStopwatch::new("Index scan on {x, z, dimension}"); - let n = query!(|r: Location| r.x == 0 && r.z == z && r.dimension == dimension).count(); + let n = ctx + .db + .location() + .iter() + .filter(|r| r.x == 0 && r.z == z && r.dimension == dimension) + .count(); span.end(); assert_eq!(n, 1); } #[spacetimedb::reducer] /// Probing a multi-column index for `ROWS_PER_CHUNK` rows should also be fast! -pub fn test_index_scan_on_x_z() { +pub fn test_index_scan_on_x_z(ctx: &ReducerContext) { let z = CHUNK as i32; let span = LogStopwatch::new("Index scan on {x, z}"); - let n = query!(|r: Location| r.x == 0 && r.z == z).count(); + let n = ctx.db.location().iter().filter(|r| r.x == 0 && r.z == z).count(); span.end(); assert_eq!(n as u64, ROWS_PER_CHUNK); } diff --git a/modules/quickstart-chat/src/lib.rs b/modules/quickstart-chat/src/lib.rs index 5ae82d0d85..cf98647056 100644 --- a/modules/quickstart-chat/src/lib.rs +++ b/modules/quickstart-chat/src/lib.rs @@ -1,4 +1,4 @@ -use spacetimedb::{Identity, ReducerContext, Timestamp}; +use spacetimedb::{Identity, ReducerContext, Table, Timestamp}; #[spacetimedb::table(name = user, public)] pub struct User { @@ -24,16 +24,13 @@ fn validate_name(name: String) -> Result { } #[spacetimedb::reducer] -pub fn set_name(ctx: ReducerContext, name: String) -> Result<(), String> { +pub fn set_name(ctx: &ReducerContext, name: String) -> Result<(), String> { let name = validate_name(name)?; - if let Some(user) = User::filter_by_identity(&ctx.sender) { - User::update_by_identity( - &ctx.sender, - User { - name: Some(name), - ..user - }, - ); + if let Some(user) = ctx.db.user().identity().find(ctx.sender) { + ctx.db.user().identity().update(User { + name: Some(name), + ..user + }); Ok(()) } else { Err("Cannot set name for unknown user".to_string()) @@ -49,12 +46,12 @@ fn validate_message(text: String) -> Result { } #[spacetimedb::reducer] -pub fn send_message(ctx: ReducerContext, text: String) -> Result<(), String> { +pub fn send_message(ctx: &ReducerContext, text: String) -> Result<(), String> { // Things to consider: // - Rate-limit messages per-user. - // - Reject messages from unnamed users. + // - Reject messages from unnamed user. let text = validate_message(text)?; - Message::insert(Message { + ctx.db.message().insert(Message { sender: ctx.sender, text, sent: ctx.timestamp, @@ -64,30 +61,29 @@ pub fn send_message(ctx: ReducerContext, text: String) -> Result<(), String> { #[spacetimedb::reducer(init)] // Called when the module is initially published -pub fn init() {} +pub fn init(_ctx: &ReducerContext) {} #[spacetimedb::reducer(client_connected)] -pub fn identity_connected(ctx: ReducerContext) { - if let Some(user) = User::filter_by_identity(&ctx.sender) { +pub fn identity_connected(ctx: &ReducerContext) { + if let Some(user) = ctx.db.user().identity().find(ctx.sender) { // If this is a returning user, i.e. we already have a `User` with this `Identity`, // set `online: true`, but leave `name` and `identity` unchanged. - User::update_by_identity(&ctx.sender, User { online: true, ..user }); + ctx.db.user().identity().update(User { online: true, ..user }); } else { // If this is a new user, create a `User` row for the `Identity`, // which is online, but hasn't set a name. - User::insert(User { + ctx.db.user().insert(User { name: None, identity: ctx.sender, online: true, - }) - .unwrap(); + }); } } #[spacetimedb::reducer(client_disconnected)] -pub fn identity_disconnected(ctx: ReducerContext) { - if let Some(user) = User::filter_by_identity(&ctx.sender) { - User::update_by_identity(&ctx.sender, User { online: false, ..user }); +pub fn identity_disconnected(ctx: &ReducerContext) { + if let Some(user) = ctx.db.user().identity().find(ctx.sender) { + ctx.db.user().identity().update(User { online: false, ..user }); } else { // This branch should be unreachable, // as it doesn't make sense for a client to disconnect without connecting first. diff --git a/modules/sdk-test-connect-disconnect/src/lib.rs b/modules/sdk-test-connect-disconnect/src/lib.rs index 5cb6a6fdd0..c89b7af1f3 100644 --- a/modules/sdk-test-connect-disconnect/src/lib.rs +++ b/modules/sdk-test-connect-disconnect/src/lib.rs @@ -8,24 +8,24 @@ //! - Disconnect, then reconnect again. //! - Subscribe to `Disconnected`. //! - Observe the presence of one row with the client's `Identity`. -use spacetimedb::{Identity, ReducerContext}; +use spacetimedb::{Identity, ReducerContext, Table}; -#[spacetimedb::table(name = Connected, public)] +#[spacetimedb::table(name = connected, public)] pub struct Connected { identity: Identity, } -#[spacetimedb::table(name = Disconnected, public)] +#[spacetimedb::table(name = disconnected, public)] pub struct Disconnected { identity: Identity, } #[spacetimedb::reducer(client_connected)] -pub fn identity_connected(ctx: ReducerContext) { - Connected::insert(Connected { identity: ctx.sender }); +pub fn identity_connected(ctx: &ReducerContext) { + ctx.db.connected().insert(Connected { identity: ctx.sender }); } #[spacetimedb::reducer(client_disconnected)] -pub fn identity_disconnected(ctx: ReducerContext) { - Disconnected::insert(Disconnected { identity: ctx.sender }); +pub fn identity_disconnected(ctx: &ReducerContext) { + ctx.db.disconnected().insert(Disconnected { identity: ctx.sender }); } diff --git a/modules/sdk-test/Cargo.toml b/modules/sdk-test/Cargo.toml index fbd8a926f3..1bc7e8695c 100644 --- a/modules/sdk-test/Cargo.toml +++ b/modules/sdk-test/Cargo.toml @@ -12,3 +12,4 @@ crate-type = ["cdylib"] spacetimedb.workspace = true log.workspace = true anyhow.workspace = true +paste.workspace = true diff --git a/modules/sdk-test/src/lib.rs b/modules/sdk-test/src/lib.rs index c540d70c12..252ae7592d 100644 --- a/modules/sdk-test/src/lib.rs +++ b/modules/sdk-test/src/lib.rs @@ -9,7 +9,7 @@ use anyhow::{Context, Result}; use spacetimedb::{ sats::{i256, u256}, - Address, Identity, ReducerContext, SpacetimeType, + Address, Identity, ReducerContext, SpacetimeType, Table, }; #[derive(SpacetimeType)] @@ -152,9 +152,11 @@ macro_rules! define_tables { { insert $insert:ident $(, $($ops:tt)* )? } $($field_name:ident $ty:ty),* $(,)*) => { - #[spacetimedb::reducer] - pub fn $insert ($($field_name : $ty,)*) { - $name::insert($name { $($field_name,)* }); + paste::paste! { + #[spacetimedb::reducer] + pub fn $insert (ctx: &ReducerContext, $($field_name : $ty,)*) { + ctx.db.[<$name:snake>]().insert($name { $($field_name,)* }); + } } define_tables!(@impl_ops $name { $($($ops)*)? } $($field_name $ty,)*); @@ -166,9 +168,11 @@ macro_rules! define_tables { { insert_or_panic $insert:ident $(, $($ops:tt)* )? } $($field_name:ident $ty:ty),* $(,)*) => { - #[spacetimedb::reducer] - pub fn $insert ($($field_name : $ty,)*) { - $name::insert($name { $($field_name,)* }).expect(concat!("Failed to insert row for table: ", stringify!($name))); + paste::paste! { + #[spacetimedb::reducer] + pub fn $insert (ctx: &ReducerContext, $($field_name : $ty,)*) { + ctx.db.[<$name:snake>]().insert($name { $($field_name,)* }); + } } define_tables!(@impl_ops $name { $($($ops)*)? } $($field_name $ty,)*); @@ -180,10 +184,11 @@ macro_rules! define_tables { { update_by $update:ident = $update_method:ident($unique_field:ident) $(, $($ops:tt)* )? } $($field_name:ident $ty:ty),* $(,)*) => { - #[spacetimedb::reducer] - pub fn $update ($($field_name : $ty,)*) { - let key = $unique_field.clone(); - $name::$update_method(&key, $name { $($field_name,)* }); + paste::paste! { + #[spacetimedb::reducer] + pub fn $update (ctx: &ReducerContext, $($field_name : $ty,)*) { + ctx.db.[<$name:snake>]().$unique_field().update($name { $($field_name,)* }); + } } define_tables!(@impl_ops $name { $($($ops)*)? } $($field_name $ty,)*); @@ -195,9 +200,11 @@ macro_rules! define_tables { { delete_by $delete:ident = $delete_method:ident($unique_field:ident : $unique_ty:ty) $(, $($ops:tt)*)? } $($other_fields:tt)* ) => { - #[spacetimedb::reducer] - pub fn $delete ($unique_field : $unique_ty) { - $name::$delete_method(&$unique_field); + paste::paste! { + #[spacetimedb::reducer] + pub fn $delete (ctx: &ReducerContext, $unique_field : $unique_ty) { + ctx.db.[<$name:snake>]().$unique_field().delete(&$unique_field); + } } define_tables!(@impl_ops $name { $($($ops)*)? } $($other_fields)*); @@ -205,9 +212,11 @@ macro_rules! define_tables { // Define a table. (@one $name:ident { $($ops:tt)* } $($(#[$attr:meta])* $field_name:ident $ty:ty),* $(,)*) => { - #[spacetimedb::table(name = $name, public)] - pub struct $name { - $($(#[$attr])* pub $field_name : $ty,)* + paste::paste! { + #[spacetimedb::table(name = [<$name:snake>], public)] + pub struct $name { + $($(#[$attr])* pub $field_name : $ty,)* + } } // Recursively implement reducers based on the `ops`. @@ -505,66 +514,66 @@ define_tables! { } #[spacetimedb::reducer] -fn insert_caller_one_identity(ctx: ReducerContext) -> anyhow::Result<()> { - OneIdentity::insert(OneIdentity { i: ctx.sender }); +fn insert_caller_one_identity(ctx: &ReducerContext) -> anyhow::Result<()> { + ctx.db.one_identity().insert(OneIdentity { i: ctx.sender }); Ok(()) } #[spacetimedb::reducer] -fn insert_caller_vec_identity(ctx: ReducerContext) -> anyhow::Result<()> { - VecIdentity::insert(VecIdentity { i: vec![ctx.sender] }); +fn insert_caller_vec_identity(ctx: &ReducerContext) -> anyhow::Result<()> { + ctx.db.vec_identity().insert(VecIdentity { i: vec![ctx.sender] }); Ok(()) } #[spacetimedb::reducer] -fn insert_caller_unique_identity(ctx: ReducerContext, data: i32) -> anyhow::Result<()> { - UniqueIdentity::insert(UniqueIdentity { i: ctx.sender, data })?; +fn insert_caller_unique_identity(ctx: &ReducerContext, data: i32) -> anyhow::Result<()> { + ctx.db.unique_identity().insert(UniqueIdentity { i: ctx.sender, data }); Ok(()) } #[spacetimedb::reducer] -fn insert_caller_pk_identity(ctx: ReducerContext, data: i32) -> anyhow::Result<()> { - PkIdentity::insert(PkIdentity { i: ctx.sender, data })?; +fn insert_caller_pk_identity(ctx: &ReducerContext, data: i32) -> anyhow::Result<()> { + ctx.db.pk_identity().insert(PkIdentity { i: ctx.sender, data }); Ok(()) } #[spacetimedb::reducer] -fn insert_caller_one_address(ctx: ReducerContext) -> anyhow::Result<()> { - OneAddress::insert(OneAddress { +fn insert_caller_one_address(ctx: &ReducerContext) -> anyhow::Result<()> { + ctx.db.one_address().insert(OneAddress { a: ctx.address.context("No address in reducer context")?, }); Ok(()) } #[spacetimedb::reducer] -fn insert_caller_vec_address(ctx: ReducerContext) -> anyhow::Result<()> { - VecAddress::insert(VecAddress { +fn insert_caller_vec_address(ctx: &ReducerContext) -> anyhow::Result<()> { + ctx.db.vec_address().insert(VecAddress { a: vec![ctx.address.context("No address in reducer context")?], }); Ok(()) } #[spacetimedb::reducer] -fn insert_caller_unique_address(ctx: ReducerContext, data: i32) -> anyhow::Result<()> { - UniqueAddress::insert(UniqueAddress { +fn insert_caller_unique_address(ctx: &ReducerContext, data: i32) -> anyhow::Result<()> { + ctx.db.unique_address().insert(UniqueAddress { a: ctx.address.context("No address in reducer context")?, data, - })?; + }); Ok(()) } #[spacetimedb::reducer] -fn insert_caller_pk_address(ctx: ReducerContext, data: i32) -> anyhow::Result<()> { - PkAddress::insert(PkAddress { +fn insert_caller_pk_address(ctx: &ReducerContext, data: i32) -> anyhow::Result<()> { + ctx.db.pk_address().insert(PkAddress { a: ctx.address.context("No address in reducer context")?, data, - })?; + }); Ok(()) } #[spacetimedb::reducer] -fn insert_primitives_as_strings(s: EveryPrimitiveStruct) { - VecString::insert(VecString { +fn insert_primitives_as_strings(ctx: &ReducerContext, s: EveryPrimitiveStruct) { + ctx.db.vec_string().insert(VecString { s: vec![ s.a.to_string(), s.b.to_string(), @@ -629,4 +638,4 @@ define_tables! { } #[spacetimedb::reducer] -fn no_op_succeeds() {} +fn no_op_succeeds(_ctx: &ReducerContext) {} diff --git a/modules/spacetimedb-quickstart/src/lib.rs b/modules/spacetimedb-quickstart/src/lib.rs index 87fb28418c..b40d0c92a6 100644 --- a/modules/spacetimedb-quickstart/src/lib.rs +++ b/modules/spacetimedb-quickstart/src/lib.rs @@ -1,4 +1,4 @@ -use spacetimedb::{println, query}; +use spacetimedb::{println, ReducerContext, Table}; #[spacetimedb::table(name = person, public)] pub struct Person { @@ -10,21 +10,21 @@ pub struct Person { } #[spacetimedb::reducer] -pub fn add(name: String, age: u8) { - Person::insert(Person { id: 0, name, age }).unwrap(); +pub fn add(ctx: &ReducerContext, name: String, age: u8) { + ctx.db.person().insert(Person { id: 0, name, age }); } #[spacetimedb::reducer] -pub fn say_hello() { - for person in Person::iter() { +pub fn say_hello(ctx: &ReducerContext) { + for person in ctx.db.person().iter() { println!("Hello, {}!", person.name); } println!("Hello, World!"); } #[spacetimedb::reducer] -pub fn list_over_age(age: u8) { - for person in query!(|person: Person| person.age >= age) { +pub fn list_over_age(ctx: &ReducerContext, age: u8) { + for person in ctx.db.person().iter().filter(|person| person.age >= age) { println!("{} has age {} >= {}", person.name, person.age, age); } } From 3fa1b7644d658ee2142821dc179d3333921dd752 Mon Sep 17 00:00:00 2001 From: Noa Date: Tue, 17 Sep 2024 14:39:15 -0500 Subject: [PATCH 4/9] Port smoketests to new api --- crates/bindings-macro/src/lib.rs | 1 - crates/bindings/src/lib.rs | 7 +- crates/bindings/src/rt.rs | 15 +-- .../cli/src/subcommands/project/rust/lib._rs | 18 ++-- modules/rust-wasm-test/src/lib.rs | 1 - smoketests/tests/add_remove_index.py | 22 +++-- smoketests/tests/add_table_pseudomigration.py | 38 +++---- smoketests/tests/auto_inc.py | 26 ++--- .../tests/connect_disconnect_from_cli.py | 6 +- smoketests/tests/describe.py | 10 +- smoketests/tests/detect_wasm_bindgen.py | 8 +- smoketests/tests/filtering.py | 64 ++++++------ smoketests/tests/module_nested_op.py | 20 ++-- smoketests/tests/modules.py | 50 +++++----- smoketests/tests/new_user_flow.py | 10 +- smoketests/tests/panic.py | 6 +- smoketests/tests/permissions.py | 12 ++- smoketests/tests/schedule_reducer.py | 41 ++++---- smoketests/tests/sql.py | 15 +-- smoketests/tests/zz_docker.py | 36 +++---- test/tests/update-module.sh | 98 ------------------- 21 files changed, 200 insertions(+), 304 deletions(-) delete mode 100644 test/tests/update-module.sh diff --git a/crates/bindings-macro/src/lib.rs b/crates/bindings-macro/src/lib.rs index 6b2502d1da..566b210f32 100644 --- a/crates/bindings-macro/src/lib.rs +++ b/crates/bindings-macro/src/lib.rs @@ -875,7 +875,6 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: let field_names = fields.iter().map(|f| f.ident.unwrap()).collect::>(); let field_types = fields.iter().map(|f| f.ty).collect::>(); - let table_name = &sats_ty.name; let tabletype_impl = quote! { impl spacetimedb::Table for #tablehandle_ident<'_> { type Row = #row_type; diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index 50739f22c1..ed872df74e 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -198,17 +198,14 @@ macro_rules! __volatile_nonatomic_schedule_immediate_impl { ([$($cur:tt)*] [$next:tt $($rest:tt)*]) => { $crate::__volatile_nonatomic_schedule_immediate_impl!([$($cur)* $next] [$($rest)*]) }; - (@process_args $repeater:path, (_$(, $args:expr)* $(,)?)) => { - $crate::__volatile_nonatomic_schedule_immediate_impl!(@call $repeater, ($crate::ReducerContext::__dummy(), $($args),*)) - }; (@process_args $repeater:path, ($($args:expr),* $(,)?)) => { $crate::__volatile_nonatomic_schedule_immediate_impl!(@call $repeater, ($($args),*)) }; (@call $repeater:path, ($($args:expr),*)) => { if false { - let _ = $repeater($($args,)*); + let _ = $repeater(&$crate::ReducerContext::__dummy(), $($args,)*); } else { - $crate::rt::volatile_nonatomic_schedule_immediate::<_, _, $repeater, _>($repeater, ($($args,)*)) + $crate::rt::volatile_nonatomic_schedule_immediate::<_, _, $repeater>($repeater, ($($args,)*)) } }; } diff --git a/crates/bindings/src/rt.rs b/crates/bindings/src/rt.rs index d1ed31fcc2..50355acb78 100644 --- a/crates/bindings/src/rt.rs +++ b/crates/bindings/src/rt.rs @@ -36,9 +36,6 @@ pub fn invoke_reducer<'a, A: Args<'a>>( /// A trait for types representing the *execution logic* of a reducer. pub trait Reducer<'de, A: Args<'de>> { fn invoke(&self, ctx: &ReducerContext, args: A) -> ReducerResult; - - type ArgsWithContext; - fn extract_args(args: Self::ArgsWithContext) -> A; } /// A trait for types that can *describe* a reducer. @@ -192,13 +189,6 @@ macro_rules! impl_reducer { let ($($T,)*) = args; self(ctx, $($T),*).into_result() } - - type ArgsWithContext = (ReducerContext, $($T,)*); - #[allow(non_snake_case, clippy::unused_unit)] - fn extract_args(args: Self::ArgsWithContext) -> ($($T,)*) { - let (_ctx, $($T,)*) = args; - ($($T,)*) - } } }; @@ -554,11 +544,10 @@ macro_rules! __make_register_reftype { #[cfg(feature = "unstable_abi")] #[doc(hidden)] -pub fn volatile_nonatomic_schedule_immediate<'de, A: Args<'de>, R: Reducer<'de, A, T>, R2: ReducerInfo, T>( +pub fn volatile_nonatomic_schedule_immediate<'de, A: Args<'de>, R: Reducer<'de, A>, R2: ReducerInfo>( _reducer: R, - args: R::ArgsWithContext, + args: A, ) { - let args = R::extract_args(args); let arg_bytes = bsatn::to_vec(&SerDeArgs(args)).unwrap(); // Schedule the reducer. diff --git a/crates/cli/src/subcommands/project/rust/lib._rs b/crates/cli/src/subcommands/project/rust/lib._rs index c7b087daf0..b5477b73c9 100644 --- a/crates/cli/src/subcommands/project/rust/lib._rs +++ b/crates/cli/src/subcommands/project/rust/lib._rs @@ -1,33 +1,33 @@ -use spacetimedb::ReducerContext; +use spacetimedb::{ReducerContext, Table}; -#[spacetimedb::table(name = people)] +#[spacetimedb::table(name = person)] pub struct Person { name: String } #[spacetimedb::reducer(init)] -pub fn init() { +pub fn init(_ctx: &ReducerContext) { // Called when the module is initially published } #[spacetimedb::reducer(client_connected)] -pub fn identity_connected(_ctx: ReducerContext) { +pub fn identity_connected(_ctx: &ReducerContext) { // Called everytime a new client connects } #[spacetimedb::reducer(client_disconnected)] -pub fn identity_disconnected(_ctx: ReducerContext) { +pub fn identity_disconnected(_ctx: &ReducerContext) { // Called everytime a client disconnects } #[spacetimedb::reducer] -pub fn add(name: String) { - Person::insert(Person { name }); +pub fn add(ctx: &ReducerContext, name: String) { + ctx.db.person().insert(Person { name }); } #[spacetimedb::reducer] -pub fn say_hello() { - for person in Person::iter() { +pub fn say_hello(ctx: &ReducerContext) { + for person in ctx.db.person().iter() { log::info!("Hello, {}!", person.name); } log::info!("Hello, World!"); diff --git a/modules/rust-wasm-test/src/lib.rs b/modules/rust-wasm-test/src/lib.rs index 2c09dccdd5..3d0077307f 100644 --- a/modules/rust-wasm-test/src/lib.rs +++ b/modules/rust-wasm-test/src/lib.rs @@ -5,7 +5,6 @@ use spacetimedb::{duration, table, Address, Deserialize, Identity, ReducerContex #[spacetimedb::table(name = test_a, index(name = foo, btree(columns = [x])))] pub struct TestA { - #[index(btree)] pub x: u32, pub y: u32, pub z: String, diff --git a/smoketests/tests/add_remove_index.py b/smoketests/tests/add_remove_index.py index a8ab0cb061..a909204e82 100644 --- a/smoketests/tests/add_remove_index.py +++ b/smoketests/tests/add_remove_index.py @@ -4,6 +4,8 @@ class AddRemoveIndex(Smoketest): AUTOPUBLISH = False MODULE_CODE = """ +use spacetimedb::{ReducerContext, Table}; + #[spacetimedb::table(name = t1)] pub struct T1 { id: u64 } @@ -11,14 +13,16 @@ class AddRemoveIndex(Smoketest): pub struct T2 { id: u64 } #[spacetimedb::reducer(init)] -pub fn init() { +pub fn init(ctx: &ReducerContext) { for id in 0..1_000 { - T1::insert(T1 { id }); - T2::insert(T2 { id }); + ctx.db.t1().insert(T1 { id }); + ctx.db.t2().insert(T2 { id }); } } """ MODULE_CODE_INDEXED = """ +use spacetimedb::{ReducerContext, Table}; + #[spacetimedb::table(name = t1)] pub struct T1 { #[index(btree)] id: u64 } @@ -26,18 +30,18 @@ class AddRemoveIndex(Smoketest): pub struct T2 { #[index(btree)] id: u64 } #[spacetimedb::reducer(init)] -pub fn init() { +pub fn init(ctx: &ReducerContext) { for id in 0..1_000 { - T1::insert(T1 { id }); - T2::insert(T2 { id }); + ctx.db.t1().insert(T1 { id }); + ctx.db.t2().insert(T2 { id }); } } #[spacetimedb::reducer] -pub fn add() { +pub fn add(ctx: &ReducerContext) { let id = 1_001; - T1::insert(T1 { id }); - T2::insert(T2 { id }); + ctx.db.t1().insert(T1 { id }); + ctx.db.t2().insert(T2 { id }); } """ diff --git a/smoketests/tests/add_table_pseudomigration.py b/smoketests/tests/add_table_pseudomigration.py index 83250828c9..10dba18df9 100644 --- a/smoketests/tests/add_table_pseudomigration.py +++ b/smoketests/tests/add_table_pseudomigration.py @@ -5,7 +5,7 @@ class AddTablePseudomigration(Smoketest): MODULE_CODE = """ -use spacetimedb::println; +use spacetimedb::{println, ReducerContext, Table}; #[spacetimedb::table(name = person)] pub struct Person { @@ -13,13 +13,13 @@ class AddTablePseudomigration(Smoketest): } #[spacetimedb::reducer] -pub fn add_person(name: String) { - Person::insert(Person { name }); +pub fn add_person(ctx: &ReducerContext, name: String) { + ctx.db.person().insert(Person { name }); } #[spacetimedb::reducer] -pub fn print_persons(prefix: String) { - for person in Person::iter() { +pub fn print_persons(ctx: &ReducerContext, prefix: String) { + for person in ctx.db.person().iter() { println!("{}: {}", prefix, person.name); } } @@ -34,13 +34,13 @@ class AddTablePseudomigration(Smoketest): } #[spacetimedb::reducer] -pub fn add_book(isbn: String) { - Book::insert(Book { isbn }); +pub fn add_book(ctx: &ReducerContext, isbn: String) { + ctx.db.book().insert(Book { isbn }); } #[spacetimedb::reducer] -pub fn print_books(prefix: String) { - for book in Book::iter() { +pub fn print_books(ctx: &ReducerContext, prefix: String) { + for book in ctx.db.book().iter() { println!("{}: {}", prefix, book.isbn); } } @@ -87,7 +87,7 @@ def test_add_table_pseudomigration(self): class RejectTableChanges(Smoketest): MODULE_CODE = """ -use spacetimedb::println; +use spacetimedb::{println, ReducerContext, Table}; #[spacetimedb::table(name = person)] pub struct Person { @@ -95,20 +95,20 @@ class RejectTableChanges(Smoketest): } #[spacetimedb::reducer] -pub fn add_person(name: String) { - Person::insert(Person { name }); +pub fn add_person(ctx: &ReducerContext, name: String) { + ctx.db.person().insert(Person { name }); } #[spacetimedb::reducer] -pub fn print_persons(prefix: String) { - for person in Person::iter() { +pub fn print_persons(ctx: &ReducerContext, prefix: String) { + for person in ctx.db.person().iter() { println!("{}: {}", prefix, person.name); } } """ MODULE_CODE_UPDATED = """ -use spacetimedb::println; +use spacetimedb::{println, ReducerContext, Table}; #[spacetimedb::table(name = person)] pub struct Person { @@ -117,13 +117,13 @@ class RejectTableChanges(Smoketest): } #[spacetimedb::reducer] -pub fn add_person(name: String) { - Person::insert(Person { name, age: 70 }); +pub fn add_person(ctx: &ReducerContext, name: String) { + ctx.db.person().insert(Person { name, age: 70 }); } #[spacetimedb::reducer] -pub fn print_persons(prefix: String) { - for person in Person::iter() { +pub fn print_persons(ctx: &ReducerContext, prefix: String) { + for person in ctx.db.person().iter() { println!("{}: {}", prefix, person.name); } } diff --git a/smoketests/tests/auto_inc.py b/smoketests/tests/auto_inc.py index 8a5bb6205d..f91bf93955 100644 --- a/smoketests/tests/auto_inc.py +++ b/smoketests/tests/auto_inc.py @@ -22,14 +22,14 @@ class IntTests: } #[spacetimedb::reducer] -pub fn add_$KEY_TY(name: String, expected_value: $KEY_TY) { - let value = Person_$KEY_TY::insert(Person_$KEY_TY { key_col: 0, name }); +pub fn add_$KEY_TY(ctx: &ReducerContext, name: String, expected_value: $KEY_TY) { + let value = ctx.db.person_$KEY_TY().insert(Person_$KEY_TY { key_col: 0, name }); assert_eq!(value.key_col, expected_value); } #[spacetimedb::reducer] -pub fn say_hello_$KEY_TY() { - for person in Person_$KEY_TY::iter() { +pub fn say_hello_$KEY_TY(ctx: &ReducerContext) { + for person in ctx.db.person_$KEY_TY().iter() { println!("Hello, {}:{}!", person.key_col, person.name); } println!("Hello, World!"); @@ -43,7 +43,7 @@ class AutoincBasic(IntTests, Smoketest): MODULE_CODE = f""" #![allow(non_camel_case_types)] -use spacetimedb::println; +use spacetimedb::{{println, ReducerContext, Table}}; {"".join(autoinc1_template.substitute(KEY_TY=int_ty) for int_ty in ints)} """ @@ -71,21 +71,21 @@ def do_test_autoinc(self, int_ty): } #[spacetimedb::reducer] -pub fn add_new_$KEY_TY(name: String) -> Result<(), Box> { - let value = Person_$KEY_TY::insert(Person_$KEY_TY { key_col: 0, name })?; +pub fn add_new_$KEY_TY(ctx: &ReducerContext, name: String) -> Result<(), Box> { + let value = ctx.db.person_$KEY_TY().insert(Person_$KEY_TY { key_col: 0, name }); println!("Assigned Value: {} -> {}", value.key_col, value.name); Ok(()) } #[spacetimedb::reducer] -pub fn update_$KEY_TY(name: String, new_id: $KEY_TY) { - Person_$KEY_TY::delete_by_name(&name); - let _value = Person_$KEY_TY::insert(Person_$KEY_TY { key_col: new_id, name }); +pub fn update_$KEY_TY(ctx: &ReducerContext, name: String, new_id: $KEY_TY) { + ctx.db.person_$KEY_TY().name().delete(&name); + let _value = ctx.db.person_$KEY_TY().insert(Person_$KEY_TY { key_col: new_id, name }); } #[spacetimedb::reducer] -pub fn say_hello_$KEY_TY() { - for person in Person_$KEY_TY::iter() { +pub fn say_hello_$KEY_TY(ctx: &ReducerContext) { + for person in ctx.db.person_$KEY_TY().iter() { println!("Hello, {}:{}!", person.key_col, person.name); } println!("Hello, World!"); @@ -99,7 +99,7 @@ class AutoincUnique(IntTests, Smoketest): MODULE_CODE = f""" #![allow(non_camel_case_types)] use std::error::Error; -use spacetimedb::println; +use spacetimedb::{{println, ReducerContext, Table}}; {"".join(autoinc2_template.substitute(KEY_TY=int_ty) for int_ty in ints)} """ diff --git a/smoketests/tests/connect_disconnect_from_cli.py b/smoketests/tests/connect_disconnect_from_cli.py index 10ddae581f..c56d5fc430 100644 --- a/smoketests/tests/connect_disconnect_from_cli.py +++ b/smoketests/tests/connect_disconnect_from_cli.py @@ -6,19 +6,19 @@ class ConnDisconnFromCli(Smoketest): use spacetimedb::{println, ReducerContext}; #[spacetimedb::reducer(client_connected)] -pub fn connected(_ctx: ReducerContext) { +pub fn connected(_ctx: &ReducerContext) { println!("_connect called"); panic!("Panic on connect"); } #[spacetimedb::reducer(client_disconnected)] -pub fn disconnected(_ctx: ReducerContext) { +pub fn disconnected(_ctx: &ReducerContext) { println!("disconnect called"); panic!("Panic on disconnect"); } #[spacetimedb::reducer] -pub fn say_hello() { +pub fn say_hello(_ctx: &ReducerContext) { println!("Hello, World!"); } """ diff --git a/smoketests/tests/describe.py b/smoketests/tests/describe.py index 73c1c9a6b0..69be3cb960 100644 --- a/smoketests/tests/describe.py +++ b/smoketests/tests/describe.py @@ -2,7 +2,7 @@ class ModuleDescription(Smoketest): MODULE_CODE = """ -use spacetimedb::println; +use spacetimedb::{println, ReducerContext, Table}; #[spacetimedb::table(name = person)] pub struct Person { @@ -10,13 +10,13 @@ class ModuleDescription(Smoketest): } #[spacetimedb::reducer] -pub fn add(name: String) { - Person::insert(Person { name }); +pub fn add(ctx: &ReducerContext, name: String) { + ctx.db.person().insert(Person { name }); } #[spacetimedb::reducer] -pub fn say_hello() { - for person in Person::iter() { +pub fn say_hello(ctx: &ReducerContext) { + for person in ctx.db.person().iter() { println!("Hello, {}!", person.name); } println!("Hello, World!"); diff --git a/smoketests/tests/detect_wasm_bindgen.py b/smoketests/tests/detect_wasm_bindgen.py index 5d58c3a149..d2221fa54e 100644 --- a/smoketests/tests/detect_wasm_bindgen.py +++ b/smoketests/tests/detect_wasm_bindgen.py @@ -3,10 +3,10 @@ class WasmBindgen(Smoketest): AUTOPUBLISH = False MODULE_CODE = """ -use spacetimedb::log; +use spacetimedb::{log, ReducerContext}; #[spacetimedb::reducer] -pub fn test() { +pub fn test(_ctx: &ReducerContext) { log::info!("Hello! {}", now()); } @@ -27,10 +27,10 @@ def test_detect_wasm_bindgen(self): class Getrandom(Smoketest): AUTOPUBLISH = False MODULE_CODE = """ -use spacetimedb::log; +use spacetimedb::{log, ReducerContext}; #[spacetimedb::reducer] -pub fn test() { +pub fn test(_ctx: &ReducerContext) { log::info!("Hello! {}", rand::random::()); } """ diff --git a/smoketests/tests/filtering.py b/smoketests/tests/filtering.py index e976c2f41c..ab5fbef5d1 100644 --- a/smoketests/tests/filtering.py +++ b/smoketests/tests/filtering.py @@ -2,7 +2,7 @@ class Filtering(Smoketest): MODULE_CODE = """ -use spacetimedb::{println, Identity}; +use spacetimedb::{println, Identity, ReducerContext, Table}; #[spacetimedb::table(name = person)] pub struct Person { @@ -15,14 +15,14 @@ class Filtering(Smoketest): } #[spacetimedb::reducer] -pub fn insert_person(id: i32, name: String, nick: String) { - Person::insert(Person { id, name, nick} ); +pub fn insert_person(ctx: &ReducerContext, id: i32, name: String, nick: String) { + ctx.db.person().insert(Person { id, name, nick} ); } #[spacetimedb::reducer] -pub fn insert_person_twice(id: i32, name: String, nick: String) { - Person::insert(Person { id, name: name.clone(), nick: nick.clone()} ); - match Person::insert(Person { id, name: name.clone(), nick: nick.clone()}) { +pub fn insert_person_twice(ctx: &ReducerContext, id: i32, name: String, nick: String) { + ctx.db.person().insert(Person { id, name: name.clone(), nick: nick.clone()} ); + match ctx.db.person().try_insert(Person { id, name: name.clone(), nick: nick.clone()}) { Ok(_) => {}, Err(_) => { println!("UNIQUE CONSTRAINT VIOLATION ERROR: id {}: {}", id, name) @@ -31,28 +31,28 @@ class Filtering(Smoketest): } #[spacetimedb::reducer] -pub fn delete_person(id: i32) { - Person::delete_by_id(&id); +pub fn delete_person(ctx: &ReducerContext, id: i32) { + ctx.db.person().id().delete(&id); } #[spacetimedb::reducer] -pub fn find_person(id: i32) { - match Person::filter_by_id(&id) { +pub fn find_person(ctx: &ReducerContext, id: i32) { + match ctx.db.person().id().find(&id) { Some(person) => println!("UNIQUE FOUND: id {}: {}", id, person.name), None => println!("UNIQUE NOT FOUND: id {}", id), } } #[spacetimedb::reducer] -pub fn find_person_by_name(name: String) { - for person in Person::filter_by_name(&name) { +pub fn find_person_by_name(ctx: &ReducerContext, name: String) { + for person in ctx.db.person().iter().filter(|p| p.name == name) { println!("UNIQUE FOUND: id {}: {} aka {}", person.id, person.name, person.nick); } } #[spacetimedb::reducer] -pub fn find_person_by_nick(nick: String) { - match Person::filter_by_nick(&nick) { +pub fn find_person_by_nick(ctx: &ReducerContext, nick: String) { + match ctx.db.person().nick().find(&nick) { Some(person) => println!("UNIQUE FOUND: id {}: {}", person.id, person.nick), None => println!("UNIQUE NOT FOUND: nick {}", nick), } @@ -67,27 +67,27 @@ class Filtering(Smoketest): } #[spacetimedb::reducer] -pub fn insert_nonunique_person(id: i32, name: String, is_human: bool) { - NonuniquePerson::insert(NonuniquePerson { id, name, is_human } ); +pub fn insert_nonunique_person(ctx: &ReducerContext, id: i32, name: String, is_human: bool) { + ctx.db.nonunique_person().insert(NonuniquePerson { id, name, is_human } ); } #[spacetimedb::reducer] -pub fn find_nonunique_person(id: i32) { - for person in NonuniquePerson::filter_by_id(&id) { +pub fn find_nonunique_person(ctx: &ReducerContext, id: i32) { + for person in ctx.db.nonunique_person().id().filter(&id) { println!("NONUNIQUE FOUND: id {}: {}", id, person.name) } } #[spacetimedb::reducer] -pub fn find_nonunique_humans() { - for person in NonuniquePerson::filter_by_is_human(&true) { +pub fn find_nonunique_humans(ctx: &ReducerContext) { + for person in ctx.db.nonunique_person().iter().filter(|p| p.is_human) { println!("HUMAN FOUND: id {}: {}", person.id, person.name); } } #[spacetimedb::reducer] -pub fn find_nonunique_non_humans() { - for person in NonuniquePerson::filter_by_is_human(&false) { +pub fn find_nonunique_non_humans(ctx: &ReducerContext) { + for person in ctx.db.nonunique_person().iter().filter(|p| !p.is_human) { println!("NON-HUMAN FOUND: id {}: {}", person.id, person.name); } } @@ -107,15 +107,15 @@ class Filtering(Smoketest): } #[spacetimedb::reducer] -fn insert_identified_person(id_number: u64, name: String) { +fn insert_identified_person(ctx: &ReducerContext, id_number: u64, name: String) { let identity = identify(id_number); - IdentifiedPerson::insert(IdentifiedPerson { identity, name }); + ctx.db.identified_person().insert(IdentifiedPerson { identity, name }); } #[spacetimedb::reducer] -fn find_identified_person(id_number: u64) { +fn find_identified_person(ctx: &ReducerContext, id_number: u64) { let identity = identify(id_number); - match IdentifiedPerson::filter_by_identity(&identity) { + match ctx.db.identified_person().identity().find(&identity) { Some(person) => println!("IDENTIFIED FOUND: {}", person.name), None => println!("IDENTIFIED NOT FOUND"), } @@ -132,18 +132,18 @@ class Filtering(Smoketest): } #[spacetimedb::reducer] -fn insert_indexed_person(id: i32, given_name: String, surname: String) { - IndexedPerson::insert(IndexedPerson { id, given_name, surname }); +fn insert_indexed_person(ctx: &ReducerContext, id: i32, given_name: String, surname: String) { + ctx.db.indexed_person().insert(IndexedPerson { id, given_name, surname }); } #[spacetimedb::reducer] -fn delete_indexed_person(id: i32) { - IndexedPerson::delete_by_id(&id); +fn delete_indexed_person(ctx: &ReducerContext, id: i32) { + ctx.db.indexed_person().id().delete(&id); } #[spacetimedb::reducer] -fn find_indexed_people(surname: String) { - for person in IndexedPerson::filter_by_surname(&surname) { +fn find_indexed_people(ctx: &ReducerContext, surname: String) { + for person in ctx.db.indexed_person().surname().filter(&surname) { println!("INDEXED FOUND: id {}: {}, {}", person.id, person.surname, person.given_name); } } diff --git a/smoketests/tests/module_nested_op.py b/smoketests/tests/module_nested_op.py index 7e18bbd922..bf2fa1ef88 100644 --- a/smoketests/tests/module_nested_op.py +++ b/smoketests/tests/module_nested_op.py @@ -2,7 +2,7 @@ class ModuleNestedOp(Smoketest): MODULE_CODE = """ -use spacetimedb::println; +use spacetimedb::{println, ReducerContext, Table}; #[spacetimedb::table(name = account)] pub struct Account { @@ -18,26 +18,26 @@ class ModuleNestedOp(Smoketest): } #[spacetimedb::reducer] -pub fn create_account(account_id: i32, name: String) { - Account::insert(Account { id: account_id, name } ); +pub fn create_account(ctx: &ReducerContext, account_id: i32, name: String) { + ctx.db.account().insert(Account { id: account_id, name } ); } #[spacetimedb::reducer] -pub fn add_friend(my_id: i32, their_id: i32) { +pub fn add_friend(ctx: &ReducerContext, my_id: i32, their_id: i32) { // Make sure our friend exists - for account in Account::iter() { + for account in ctx.db.account().iter() { if account.id == their_id { - Friends::insert(Friends { friend_1: my_id, friend_2: their_id }); + ctx.db.friends().insert(Friends { friend_1: my_id, friend_2: their_id }); return; } } } #[spacetimedb::reducer] -pub fn say_friends() { - for friendship in Friends::iter() { - let friend1 = Account::filter_by_id(&friendship.friend_1).unwrap(); - let friend2 = Account::filter_by_id(&friendship.friend_2).unwrap(); +pub fn say_friends(ctx: &ReducerContext) { + for friendship in ctx.db.friends().iter() { + let friend1 = ctx.db.account().id().find(&friendship.friend_1).unwrap(); + let friend2 = ctx.db.account().id().find(&friendship.friend_2).unwrap(); println!("{} is friends with {}", friend1.name, friend2.name); } } diff --git a/smoketests/tests/modules.py b/smoketests/tests/modules.py index 2a917377d7..e9a7e5f302 100644 --- a/smoketests/tests/modules.py +++ b/smoketests/tests/modules.py @@ -7,7 +7,7 @@ class UpdateModule(Smoketest): AUTOPUBLISH = False MODULE_CODE = """ -use spacetimedb::println; +use spacetimedb::{println, ReducerContext, Table}; #[spacetimedb::table(name = person)] pub struct Person { @@ -18,13 +18,13 @@ class UpdateModule(Smoketest): } #[spacetimedb::reducer] -pub fn add(name: String) { - Person::insert(Person { id: 0, name }).unwrap(); +pub fn add(ctx: &ReducerContext, name: String) { + ctx.db.person().insert(Person { id: 0, name }); } #[spacetimedb::reducer] -pub fn say_hello() { - for person in Person::iter() { +pub fn say_hello(ctx: &ReducerContext) { + for person in ctx.db.person().iter() { println!("Hello, {}!", person.name); } println!("Hello, World!"); @@ -42,7 +42,7 @@ class UpdateModule(Smoketest): """ MODULE_CODE_C = """ -use spacetimedb::println; +use spacetimedb::{println, ReducerContext, Table}; #[spacetimedb::table(name = person)] pub struct Person { @@ -58,7 +58,7 @@ class UpdateModule(Smoketest): } #[spacetimedb::reducer] -pub fn are_we_updated_yet() { +pub fn are_we_updated_yet(ctx: &ReducerContext) { println!("MODULE UPDATED"); } """ @@ -102,7 +102,7 @@ def test_module_update(self): class UploadModule1(Smoketest): MODULE_CODE = """ -use spacetimedb::println; +use spacetimedb::{println, ReducerContext, Table}; #[spacetimedb::table(name = person)] pub struct Person { @@ -110,13 +110,13 @@ class UploadModule1(Smoketest): } #[spacetimedb::reducer] -pub fn add(name: String) { - Person::insert(Person { name }); +pub fn add(ctx: &ReducerContext, name: String) { + ctx.db.person().insert(Person { name }); } #[spacetimedb::reducer] -pub fn say_hello() { - for person in Person::iter() { +pub fn say_hello(ctx: &ReducerContext) { + for person in ctx.db.person().iter() { println!("Hello, {}!", person.name); } println!("Hello, World!"); @@ -139,21 +139,21 @@ def test_upload_module_1(self): class UploadModule2(Smoketest): MODULE_CODE = """ -use spacetimedb::{println, duration, Timestamp, ReducerContext}; +use spacetimedb::{println, duration, ReducerContext, Table, Timestamp}; -#[spacetimedb::table(name = scheduled_messages, public, scheduled(my_repeating_reducer))] +#[spacetimedb::table(name = scheduled_message, public, scheduled(my_repeating_reducer))] pub struct ScheduledMessage { prev: Timestamp, } #[spacetimedb::reducer(init)] -fn init() { - let _ = ScheduledMessage::insert(ScheduledMessage { prev: Timestamp::now(), scheduled_id: 0, scheduled_at: duration!(100ms).into(), }); +fn init(ctx: &ReducerContext) { + ctx.db.scheduled_message().insert(ScheduledMessage { prev: Timestamp::now(), scheduled_id: 0, scheduled_at: duration!(100ms).into(), }); } #[spacetimedb::reducer] -pub fn my_repeating_reducer(_ctx: ReducerContext, arg: ScheduledMessage) { +pub fn my_repeating_reducer(_ctx: &ReducerContext, arg: ScheduledMessage) { println!("Invoked: ts={:?}, delta={:?}", Timestamp::now(), arg.prev.elapsed()); } """ @@ -171,6 +171,8 @@ class HotswapModule(Smoketest): AUTOPUBLISH = False MODULE_CODE = """ +use spacetimedb::{ReducerContext, Table}; + #[spacetimedb::table(name = person)] pub struct Person { #[primary_key] @@ -180,13 +182,13 @@ class HotswapModule(Smoketest): } #[spacetimedb::reducer] -pub fn add_person(name: String) { - Person::insert(Person { id: 0, name }).ok(); +pub fn add_person(ctx: &ReducerContext, name: String) { + ctx.db.person().insert(Person { id: 0, name }); } """ MODULE_CODE_B = """ -use spacetimedb; +use spacetimedb::{ReducerContext, Table}; #[spacetimedb::table(name = person)] pub struct Person { @@ -197,8 +199,8 @@ class HotswapModule(Smoketest): } #[spacetimedb::reducer] -pub fn add_person(name: String) { - Person::insert(Person { id: 0, name }).ok(); +pub fn add_person(ctx: &ReducerContext, name: String) { + ctx.db.person().insert(Person { id: 0, name }); } #[spacetimedb::table(name = pet)] @@ -208,8 +210,8 @@ class HotswapModule(Smoketest): } #[spacetimedb::reducer] -pub fn add_pet(species: String) { - Pet::insert(Pet { species }).ok(); +pub fn add_pet(ctx: &ReducerContext, species: String) { + ctx.db.pet().insert(Pet { species }); } """ diff --git a/smoketests/tests/new_user_flow.py b/smoketests/tests/new_user_flow.py index e79349abf8..1002b9f4c9 100644 --- a/smoketests/tests/new_user_flow.py +++ b/smoketests/tests/new_user_flow.py @@ -4,7 +4,7 @@ class NewUserFlow(Smoketest): AUTOPUBLISH = False MODULE_CODE = """ -use spacetimedb::println; +use spacetimedb::{println, ReducerContext, Table}; #[spacetimedb::table(name = person)] pub struct Person { @@ -12,13 +12,13 @@ class NewUserFlow(Smoketest): } #[spacetimedb::reducer] -pub fn add(name: String) { - Person::insert(Person { name }); +pub fn add(ctx: &ReducerContext, name: String) { + ctx.db.person().insert(Person { name }); } #[spacetimedb::reducer] -pub fn say_hello() { - for person in Person::iter() { +pub fn say_hello(ctx: &ReducerContext) { + for person in ctx.db.person().iter() { println!("Hello, {}!", person.name); } println!("Hello, World!"); diff --git a/smoketests/tests/panic.py b/smoketests/tests/panic.py index f99d2917f9..a8f860ec2f 100644 --- a/smoketests/tests/panic.py +++ b/smoketests/tests/panic.py @@ -2,21 +2,21 @@ class Panic(Smoketest): MODULE_CODE = """ -use spacetimedb::println; +use spacetimedb::{println, ReducerContext}; use std::cell::RefCell; thread_local! { static X: RefCell = RefCell::new(0); } #[spacetimedb::reducer] -fn first() { +fn first(_ctx: &ReducerContext) { X.with(|x| { let _x = x.borrow_mut(); panic!() }) } #[spacetimedb::reducer] -fn second() { +fn second(_ctx: &ReducerContext) { X.with(|x| *x.borrow_mut()); println!("Test Passed"); } diff --git a/smoketests/tests/permissions.py b/smoketests/tests/permissions.py index eaaaa8347e..82504cef98 100644 --- a/smoketests/tests/permissions.py +++ b/smoketests/tests/permissions.py @@ -77,6 +77,8 @@ def test_publish(self): class PrivateTablePermissions(Smoketest): MODULE_CODE = """ +use spacetimedb::{ReducerContext, Table}; + #[spacetimedb::table(name = secret)] pub struct Secret { answer: u8, @@ -88,14 +90,14 @@ class PrivateTablePermissions(Smoketest): } #[spacetimedb::reducer(init)] -pub fn init() { - Secret::insert(Secret { answer: 42 }); +pub fn init(ctx: &ReducerContext) { + ctx.db.secret().insert(Secret { answer: 42 }); } #[spacetimedb::reducer] -pub fn do_thing() { - Secret::insert(Secret { answer: 20 }); - CommonKnowledge::insert(CommonKnowledge { thing: "howdy".to_owned() }); +pub fn do_thing(ctx: &ReducerContext) { + ctx.db.secret().insert(Secret { answer: 20 }); + ctx.db.common_knowledge().insert(CommonKnowledge { thing: "howdy".to_owned() }); } """ diff --git a/smoketests/tests/schedule_reducer.py b/smoketests/tests/schedule_reducer.py index 64bd044e80..d1ab4d132c 100644 --- a/smoketests/tests/schedule_reducer.py +++ b/smoketests/tests/schedule_reducer.py @@ -4,37 +4,37 @@ class CancelReducer(Smoketest): MODULE_CODE = """ - use spacetimedb::{duration, println, ReducerContext}; +use spacetimedb::{duration, println, ReducerContext, Table}; #[spacetimedb::reducer(init)] -fn init() { - let schedule = ScheuledReducerArgs::insert(ScheuledReducerArgs { +fn init(ctx: &ReducerContext) { + let schedule = ctx.db.scheduled_reducer_args().insert(ScheduledReducerArgs { num: 1, scheduled_id: 0, scheduled_at: duration!(100ms).into(), }); - ScheuledReducerArgs::delete_by_scheduled_id(&schedule.unwrap().scheduled_id); + ctx.db.scheduled_reducer_args().scheduled_id().delete(&schedule.scheduled_id); - let schedule = ScheuledReducerArgs::insert(ScheuledReducerArgs { + let schedule = ctx.db.scheduled_reducer_args().insert(ScheduledReducerArgs { num: 2, scheduled_id: 0, scheduled_at: duration!(1000ms).into(), }); - do_cancel(schedule.unwrap().scheduled_id); + do_cancel(ctx, schedule.scheduled_id); } #[spacetimedb::table(name = scheduled_reducer_args, public, scheduled(reducer))] -pub struct ScheuledReducerArgs { +pub struct ScheduledReducerArgs { num: i32, } #[spacetimedb::reducer] -fn do_cancel(schedule_id: u64) { - ScheuledReducerArgs::delete_by_scheduled_id(&schedule_id); +fn do_cancel(ctx: &ReducerContext, schedule_id: u64) { + ctx.db.scheduled_reducer_args().scheduled_id().delete(&schedule_id); } #[spacetimedb::reducer] -fn reducer(_ctx: ReducerContext, args: ScheuledReducerArgs) { +fn reducer(_ctx: &ReducerContext, args: ScheduledReducerArgs) { println!("the reducer ran: {}", args.num); } """ @@ -49,8 +49,7 @@ def test_cancel_reducer(self): class SubscribeScheduledTable(Smoketest): MODULE_CODE = """ -use spacetimedb::{println, duration, Timestamp, ReducerContext}; - +use spacetimedb::{println, duration, ReducerContext, Table, Timestamp}; #[spacetimedb::table(name = scheduled_table, public, scheduled(my_reducer))] pub struct ScheduledTable { @@ -58,17 +57,17 @@ class SubscribeScheduledTable(Smoketest): } #[spacetimedb::reducer] -fn schedule_reducer() { - let _ = ScheduledTable::insert(ScheduledTable { prev: Timestamp::from_micros_since_epoch(0), scheduled_id: 2, scheduled_at: Timestamp::from_micros_since_epoch(0).into(), }); +fn schedule_reducer(ctx: &ReducerContext) { + ctx.db.scheduled_table().insert(ScheduledTable { prev: Timestamp::from_micros_since_epoch(0), scheduled_id: 2, scheduled_at: Timestamp::from_micros_since_epoch(0).into(), }); } #[spacetimedb::reducer] -fn schedule_repeated_reducer() { - let _ = ScheduledTable::insert(ScheduledTable { prev: Timestamp::from_micros_since_epoch(0), scheduled_id: 1, scheduled_at: duration!(100ms).into(), }); +fn schedule_repeated_reducer(ctx: &ReducerContext) { + ctx.db.scheduled_table().insert(ScheduledTable { prev: Timestamp::from_micros_since_epoch(0), scheduled_id: 1, scheduled_at: duration!(100ms).into(), }); } #[spacetimedb::reducer] -pub fn my_reducer(_ctx: ReducerContext, arg: ScheduledTable) { +pub fn my_reducer(_ctx: &ReducerContext, arg: ScheduledTable) { println!("Invoked: ts={:?}, delta={:?}", Timestamp::now(), arg.prev.elapsed()); } """ @@ -115,19 +114,21 @@ def test_scheduled_table_subscription_repeated_reducer(self): class VolatileNonatomicScheduleImmediate(Smoketest): BINDINGS_FEATURES = ["unstable_abi"] MODULE_CODE = """ +use spacetimedb::{ReducerContext, Table}; + #[spacetimedb::table(name = my_table, public)] pub struct MyTable { x: String, } #[spacetimedb::reducer] -fn do_schedule() { +fn do_schedule(_ctx: &ReducerContext) { spacetimedb::volatile_nonatomic_schedule_immediate!(do_insert("hello".to_owned())); } #[spacetimedb::reducer] -fn do_insert(x: String) { - MyTable::insert(MyTable { x }); +fn do_insert(ctx: &ReducerContext, x: String) { + ctx.db.my_table().insert(MyTable { x }); } """ def test_volatile_nonatomic_schedule_immediate(self): diff --git a/smoketests/tests/sql.py b/smoketests/tests/sql.py index 67287c5901..6ad8bfa116 100644 --- a/smoketests/tests/sql.py +++ b/smoketests/tests/sql.py @@ -3,6 +3,7 @@ class SqlFormat(Smoketest): MODULE_CODE = """ use spacetimedb::sats::{i256, u256}; +use spacetimedb::{ReducerContext, Table}; #[derive(Copy, Clone)] #[spacetimedb::table(name = t_ints)] @@ -52,7 +53,7 @@ class SqlFormat(Smoketest): } #[spacetimedb::reducer] -pub fn test() { +pub fn test(ctx: &ReducerContext) { let tuple = TInts { i8: -25, i16: -3224, @@ -61,8 +62,8 @@ class SqlFormat(Smoketest): i128: -234434897853, i256: (-234434897853i128).into(), }; - TInts::insert(tuple); - TIntsTuple::insert(TIntsTuple { tuple }); + ctx.db.t_ints().insert(tuple); + ctx.db.t_ints_tuple().insert(TIntsTuple { tuple }); let tuple = TUints { u8: 105, @@ -72,8 +73,8 @@ class SqlFormat(Smoketest): u128: 4378528978889, u256: 4378528978889u128.into(), }; - TUints::insert(tuple); - TUintsTuple::insert(TUintsTuple { tuple }); + ctx.db.t_uints().insert(tuple); + ctx.db.t_uints_tuple().insert(TUintsTuple { tuple }); let tuple = TOthers { bool: true, @@ -82,8 +83,8 @@ class SqlFormat(Smoketest): str: "This is spacetimedb".to_string(), bytes: vec!(1, 2, 3, 4, 5, 6, 7), }; - TOthers::insert(tuple.clone()); - TOthersTuple::insert(TOthersTuple { tuple }); + ctx.db.t_others().insert(tuple.clone()); + ctx.db.t_others_tuple().insert(TOthersTuple { tuple }); } """ diff --git a/smoketests/tests/zz_docker.py b/smoketests/tests/zz_docker.py index 65bb9cca0c..0bb9a45211 100644 --- a/smoketests/tests/zz_docker.py +++ b/smoketests/tests/zz_docker.py @@ -45,7 +45,7 @@ class DockerRestartModule(Smoketest): # Note: creating indexes on `Person` # exercises more possible failure cases when replaying after restart MODULE_CODE = """ -use spacetimedb::println; +use spacetimedb::{println, ReducerContext, Table}; #[spacetimedb::table(name = person, index(name = name_idx, btree(columns = [name])))] pub struct Person { @@ -56,13 +56,13 @@ class DockerRestartModule(Smoketest): } #[spacetimedb::reducer] -pub fn add(name: String) { -Person::insert(Person { id: 0, name }).unwrap(); +pub fn add(ctx: &ReducerContext, name: String) { + ctx.db.person().insert(Person { id: 0, name }); } #[spacetimedb::reducer] -pub fn say_hello() { - for person in Person::iter() { +pub fn say_hello(ctx: &ReducerContext) { + for person in ctx.db.person().iter() { println!("Hello, {}!", person.name); } println!("Hello, World!"); @@ -91,7 +91,7 @@ class DockerRestartSql(Smoketest): # Note: creating indexes on `Person` # exercises more possible failure cases when replaying after restart MODULE_CODE = """ -use spacetimedb::println; +use spacetimedb::{println, ReducerContext, Table}; #[spacetimedb::table(name = person, index(name = name_idx, btree(columns = [name])))] pub struct Person { @@ -102,13 +102,13 @@ class DockerRestartSql(Smoketest): } #[spacetimedb::reducer] -pub fn add(name: String) { -Person::insert(Person { id: 0, name }).unwrap(); +pub fn add(ctx: &ReducerContext, name: String) { + ctx.db.person().insert(Person { id: 0, name }); } #[spacetimedb::reducer] -pub fn say_hello() { - for person in Person::iter() { +pub fn say_hello(ctx: &ReducerContext) { + for person in ctx.db.person().iter() { println!("Hello, {}!", person.name); } println!("Hello, World!"); @@ -137,7 +137,7 @@ def test_restart_module(self): class DockerRestartAutoDisconnect(Smoketest): MODULE_CODE = """ use log::info; -use spacetimedb::{Address, Identity, ReducerContext, TableType}; +use spacetimedb::{Address, Identity, ReducerContext, Table}; #[spacetimedb::table(name = connected_client)] pub struct ConnectedClient { @@ -146,28 +146,28 @@ class DockerRestartAutoDisconnect(Smoketest): } #[spacetimedb::reducer(client_connected)] -fn on_connect(ctx: ReducerContext) { - ConnectedClient::insert(ConnectedClient { +fn on_connect(ctx: &ReducerContext) { + ctx.db.connected_client().insert(ConnectedClient { identity: ctx.sender, address: ctx.address.expect("sender address unset"), }); } #[spacetimedb::reducer(client_disconnected)] -fn on_disconnect(ctx: ReducerContext) { +fn on_disconnect(ctx: &ReducerContext) { let sender_identity = &ctx.sender; let sender_address = ctx.address.as_ref().expect("sender address unset"); let match_client = |row: &ConnectedClient| { &row.identity == sender_identity && &row.address == sender_address }; - if let Some(client) = ConnectedClient::iter().find(match_client) { - ConnectedClient::delete(&client); + if let Some(client) = ctx.db.connected_client().iter().find(match_client) { + ctx.db.connected_client().delete(client); } } #[spacetimedb::reducer] -fn print_num_connected() { - let n = ConnectedClient::iter().count(); +fn print_num_connected(ctx: &ReducerContext) { + let n = ctx.db.connected_client().count(); info!("CONNECTED CLIENTS: {n}") } """ diff --git a/test/tests/update-module.sh b/test/tests/update-module.sh deleted file mode 100644 index 52d416b1fe..0000000000 --- a/test/tests/update-module.sh +++ /dev/null @@ -1,98 +0,0 @@ -#!/bin/bash - -if [ "$DESCRIBE_TEST" = 1 ] ; then - echo "This tests publishing a module without the --clear-database option" - exit -fi - -set -euox pipefail - -source "./test/lib.include" - -cat > "${PROJECT_PATH}/src/lib.rs" << EOF -use spacetimedb::println; - -#[spacetimedb::table(name = person)] -pub struct Person { - #[primary_key] - #[auto_inc] - id: u64, - name: String, -} - -#[spacetimedb::reducer] -pub fn add(name: String) { - Person::insert(Person { id: 0, name }).unwrap(); -} - -#[spacetimedb::reducer] -pub fn say_hello() { - for person in Person::iter() { - println!("Hello, {}!", person.name); - } - println!("Hello, World!"); -} -EOF - -IDENT=$(basename "$PROJECT_PATH") -run_test cargo run publish --skip_clippy --project-path "$PROJECT_PATH" "$IDENT" -[ "1" == "$(grep -c "reated new database" "$TEST_OUT")" ] - -run_test cargo run call "$IDENT" add Robert -run_test cargo run call "$IDENT" add Julie -run_test cargo run call "$IDENT" add Samantha -run_test cargo run call "$IDENT" say_hello -run_test cargo run logs "$IDENT" 100 -[ ' Hello, Samantha!' == "$(grep 'Samantha' "$TEST_OUT" | tail -n 4 | cut -d: -f6-)" ] -[ ' Hello, Julie!' == "$(grep 'Julie' "$TEST_OUT" | tail -n 4 | cut -d: -f6-)" ] -[ ' Hello, Robert!' == "$(grep 'Robert' "$TEST_OUT" | tail -n 4 | cut -d: -f6-)" ] -[ ' Hello, World!' == "$(grep 'World' "$TEST_OUT" | tail -n 4 | cut -d: -f6-)" ] - -# Unchanged module is ok -run_test cargo run publish --skip_clippy --project-path "$PROJECT_PATH" "$IDENT" -[ "1" == "$(grep -c "Updated database" "$TEST_OUT")" ] - -# Changing an existing table isn't -cat > "${PROJECT_PATH}/src/lib.rs" < "${PROJECT_PATH}/src/lib.rs" < Date: Thu, 19 Sep 2024 13:02:16 -0500 Subject: [PATCH 5/9] Output moduledef v9 --- crates/bindings-macro/src/lib.rs | 297 +++++++++++++++------------- crates/bindings-macro/src/module.rs | 11 +- crates/bindings/src/rt.rs | 135 +++++-------- crates/bindings/src/table.rs | 37 ++-- crates/lib/src/db/raw_def/v8.rs | 4 +- modules/rust-wasm-test/src/lib.rs | 10 +- 6 files changed, 226 insertions(+), 268 deletions(-) diff --git a/crates/bindings-macro/src/lib.rs b/crates/bindings-macro/src/lib.rs index 566b210f32..8cfc187ee0 100644 --- a/crates/bindings-macro/src/lib.rs +++ b/crates/bindings-macro/src/lib.rs @@ -11,13 +11,11 @@ mod module; extern crate core; extern crate proc_macro; -use bitflags::Flags; use heck::ToSnakeCase; use module::{derive_deserialize, derive_satstype, derive_serialize}; use proc_macro::TokenStream as StdTokenStream; use proc_macro2::{Span, TokenStream}; use quote::{format_ident, quote, quote_spanned}; -use spacetimedb_primitives::ColumnAttribute; use std::collections::HashMap; use std::time::Duration; use syn::ext::IdentExt; @@ -121,6 +119,10 @@ fn cvt_attr( TokenStream::from_iter([extra_attr, item, generated]).into() } +fn ident_to_litstr(ident: &Ident) -> syn::LitStr { + syn::LitStr::new(&ident.to_string(), ident.span()) +} + struct MutItem<'a, T> { val: &'a mut T, modified: &'a mut bool, @@ -179,29 +181,41 @@ struct ReducerArgs { } enum LifecycleReducer { - Init, - ClientConnected, - ClientDisconnected, - Update, + Init(Span), + ClientConnected(Span), + ClientDisconnected(Span), + Update(Span), } impl LifecycleReducer { fn reducer_name(&self) -> &'static str { match self { - Self::Init => "__init__", - Self::ClientConnected => "__identity_connected__", - Self::ClientDisconnected => "__identity_disconnected__", - Self::Update => "__update__", + Self::Init(_) => "__init__", + Self::ClientConnected(_) => "__identity_connected__", + Self::ClientDisconnected(_) => "__identity_disconnected__", + Self::Update(_) => "__update__", } } + fn to_lifecycle_value(&self) -> Option { + let (Self::Init(span) | Self::ClientConnected(span) | Self::ClientDisconnected(span) | Self::Update(span)) = + *self; + let name = match self { + Self::Init(_) => "Init", + Self::ClientConnected(_) => "OnConnect", + Self::ClientDisconnected(_) => "OnDisconnect", + Self::Update(_) => return None, + }; + let ident = Ident::new(name, span); + Some(quote_spanned!(span => spacetimedb::rt::LifecycleReducer::#ident)) + } } impl ReducerArgs { fn parse(input: TokenStream) -> syn::Result { let mut args = Self::default(); syn::meta::parser(|meta| { - let mut set_lifecycle = |kind| -> syn::Result<()> { + let mut set_lifecycle = |kind: fn(Span) -> _| -> syn::Result<()> { check_duplicate_msg(&args.lifecycle, &meta, "already specified a lifecycle reducer kind")?; - args.lifecycle = Some(kind); + args.lifecycle = Some(kind(meta.path.span())); Ok(()) }; match_meta!(match meta { @@ -301,7 +315,7 @@ fn reducer_impl(args: ReducerArgs, original_function: &ItemFn) -> syn::Result lifecycle.reducer_name(), None => { reducer_name = func_name.to_string(); @@ -315,6 +329,8 @@ fn reducer_impl(args: ReducerArgs, original_function: &ItemFn) -> syn::Result syn::Result = Some(#lifecycle);)* const ARG_NAMES: &'static [Option<&'static str>] = &[#(#opt_arg_names),*]; const INVOKE: spacetimedb::rt::ReducerFn = { #generated_function @@ -380,12 +397,29 @@ fn reducer_impl(args: ReducerArgs, original_function: &ItemFn) -> syn::Result, + access: Option, scheduled: Option, name: Ident, indices: Vec, } +enum TableAccess { + Public(Span), + Private(Span), +} + +impl TableAccess { + fn to_value(&self) -> TokenStream { + let (TableAccess::Public(span) | TableAccess::Private(span)) = *self; + let name = match self { + TableAccess::Public(_) => "Public", + TableAccess::Private(_) => "Private", + }; + let ident = Ident::new(name, span); + quote_spanned!(span => spacetimedb::table::TableAccess::#ident) + } +} + // add scheduled_id and scheduled_at fields to the struct fn add_scheduled_fields(item: &mut syn::DeriveInput) { if let syn::Data::Struct(struct_data) = &mut item.data { @@ -411,7 +445,6 @@ fn reducer_type_check(item: &syn::DeriveInput, reducer_name: &Path) -> TokenStre } struct IndexArg { - #[allow(unused)] name: Ident, kind: IndexType, } @@ -422,26 +455,19 @@ enum IndexType { impl TableArgs { fn parse(input: TokenStream, struct_ident: &Ident) -> syn::Result { - let mut specified_access = false; - let mut public = None; + let mut access = None; let mut scheduled = None; let mut name = None; let mut indices = Vec::new(); syn::meta::parser(|meta| { - let mut specified_access = || { - if specified_access { - return Err(meta.error("already specified access level")); - } - specified_access = true; - Ok(()) - }; match_meta!(match meta { sym::public => { - specified_access()?; - public = Some(meta.path.span()); + check_duplicate_msg(&access, &meta, "already specified access level")?; + access = Some(TableAccess::Public(meta.path.span())); } sym::private => { - specified_access()?; + check_duplicate_msg(&access, &meta, "already specified access level")?; + access = Some(TableAccess::Private(meta.path.span())); } sym::name => { check_duplicate(&name, &meta)?; @@ -467,7 +493,7 @@ impl TableArgs { ) })?; Ok(TableArgs { - public, + access, scheduled, name, indices, @@ -543,11 +569,10 @@ impl IndexArg { fn to_desc_and_accessor( &self, - table_name: &str, index_index: u32, cols: &[Column], ) -> Result<(TokenStream, TokenStream), syn::Error> { - match &self.kind { + let (algo, accessor) = match &self.kind { IndexType::BTree { columns } => { let cols = columns .iter() @@ -560,16 +585,9 @@ impl IndexArg { }) .collect::>>()?; - let name = (["btree", table_name].into_iter()) - .chain(cols.iter().map(|col| col.field.name.as_deref().unwrap())) - .collect::>() - .join("_"); - let col_ids = cols.iter().map(|col| col.index); - let desc = quote!(spacetimedb::table::IndexDesc { - name: #name, - ty: spacetimedb::spacetimedb_lib::db::raw_def::IndexType::BTree, - col_ids: &[#(#col_ids),*], + let algo = quote!(spacetimedb::table::IndexAlgo::BTree { + columns: &[#(#col_ids),*] }); let index_ident = &self.name; @@ -580,9 +598,15 @@ impl IndexArg { } }; - Ok((desc, accessor)) + (algo, accessor) } - } + }; + let accessor_name = ident_to_litstr(&self.name); + let desc = quote!(spacetimedb::table::IndexDesc { + accessor_name: #accessor_name, + algo: #algo, + }); + Ok((desc, accessor)) } } @@ -673,34 +697,37 @@ pub fn table_helper(_input: StdTokenStream) -> StdTokenStream { Default::default() } -// TODO: We actually need to add a constraint that requires this column to be unique! +#[derive(Copy, Clone)] struct Column<'a> { index: u16, field: &'a module::SatsField<'a>, - attr: ColumnAttribute, ty: &'a syn::Type, } enum ColumnAttr { Unique(Span), - Autoinc(Span), - Primarykey(Span), + AutoInc(Span), + PrimaryKey(Span), + Index(IndexArg), } impl ColumnAttr { - fn parse(attr: &syn::Attribute) -> syn::Result> { + fn parse(attr: &syn::Attribute, field_ident: &Ident) -> syn::Result> { let Some(ident) = attr.path().get_ident() else { return Ok(None); }; - Ok(if ident == sym::unique { + Ok(if ident == sym::index { + let index = IndexArg::parse_index_attr(field_ident, attr)?; + Some(ColumnAttr::Index(index)) + } else if ident == sym::unique { attr.meta.require_path_only()?; Some(ColumnAttr::Unique(ident.span())) } else if ident == sym::auto_inc { attr.meta.require_path_only()?; - Some(ColumnAttr::Autoinc(ident.span())) + Some(ColumnAttr::AutoInc(ident.span())) } else if ident == sym::primary_key { attr.meta.require_path_only()?; - Some(ColumnAttr::Primarykey(ident.span())) + Some(ColumnAttr::PrimaryKey(ident.span())) } else { None }) @@ -723,8 +750,6 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: return Err(syn::Error::new(Span::call_site(), "spacetimedb table must be a struct")); }; - let mut columns = Vec::::new(); - let table_id_from_name_func = quote! { fn table_id() -> spacetimedb::TableId { static TABLE_ID: std::sync::OnceLock = std::sync::OnceLock::new(); @@ -734,47 +759,63 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: } }; - let mut has_autoinc = false; + if fields.len() > u16::MAX.into() { + return Err(syn::Error::new_spanned( + &*item, + "too many columns; the most a table can have is 2^16", + )); + } + + let mut columns = vec![]; + let mut unique_columns = vec![]; + let mut sequenced_columns = vec![]; + let mut primary_key_column = None; for (i, field) in fields.iter().enumerate() { - let col_num: u16 = i - .try_into() - .map_err(|_| syn::Error::new_spanned(field.ident, "too many columns; the most a table can have is 2^16"))?; + let col_num = i as u16; + let field_ident = field.ident.unwrap(); - let mut col_attr = ColumnAttribute::UNSET; + let mut unique = None; + let mut auto_inc = None; + let mut primary_key = None; for attr in field.original_attrs { - if attr.path() == sym::index { - let index = IndexArg::parse_index_attr(field.ident.unwrap(), attr)?; - args.indices.push(index); + let Some(attr) = ColumnAttr::parse(attr, field_ident)? else { continue; - } - let Some(attr) = ColumnAttr::parse(attr)? else { continue }; - let duplicate = |span| syn::Error::new(span, "duplicate attribute"); - let (extra_col_attr, span) = match attr { - ColumnAttr::Unique(span) => (ColumnAttribute::UNIQUE, span), - ColumnAttr::Autoinc(span) => (ColumnAttribute::AUTO_INC, span), - ColumnAttr::Primarykey(span) => (ColumnAttribute::PRIMARY_KEY, span), }; - // do those attributes intersect (not counting the INDEXED bit which is present in all attributes)? - // this will check that no two attributes both have UNIQUE, AUTOINC or PRIMARY_KEY bits set - if !(col_attr & extra_col_attr) - .difference(ColumnAttribute::INDEXED) - .is_empty() - { - return Err(duplicate(span)); + match attr { + ColumnAttr::Unique(span) => { + check_duplicate(&unique, span)?; + unique = Some(span); + } + ColumnAttr::AutoInc(span) => { + check_duplicate(&auto_inc, span)?; + auto_inc = Some(span); + } + ColumnAttr::PrimaryKey(span) => { + check_duplicate(&primary_key, span)?; + primary_key = Some(span); + } + ColumnAttr::Index(index_arg) => args.indices.push(index_arg), } - col_attr |= extra_col_attr; } - has_autoinc |= col_attr.contains(ColumnAttribute::AUTO_INC); - let column = Column { index: col_num, field, - attr: col_attr, ty: field.ty, }; + if unique.is_some() || primary_key.is_some() { + unique_columns.push(column); + } + if auto_inc.is_some() { + sequenced_columns.push(column); + } + if let Some(span) = primary_key { + check_duplicate_msg(&primary_key_column, span, "can only have one primary key per table")?; + primary_key_column = Some(column); + } + columns.push(column); } @@ -784,71 +825,47 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: .indices .iter() .enumerate() - .map(|(i, index)| index.to_desc_and_accessor(&table_name, i as u32, &columns)) + .map(|(i, index)| index.to_desc_and_accessor(i as u32, &columns)) // TODO: stabilized in 1.79 // .collect::, Vec<_>)>>()?; .collect::>>()? .into_iter() .unzip::<_, _, Vec<_>, Vec<_>>(); - let unique_field_accessors = columns - .iter() - .filter(|x| x.attr.contains(ColumnAttribute::UNIQUE)) - .map(|unique| { - let column_index = unique.index; - let vis = unique.field.vis; - let column_type = unique.field.ty; - let column_ident = unique.field.ident.unwrap(); - - let doc = format!( - "Gets the [`UniqueColumn`][spacetimedb::UniqueColumn] for the \ - [`{column_ident}`][{row_type}::{column_ident}] column." - ); - quote! { - #[doc = #doc] - #vis fn #column_ident(&self) -> spacetimedb::UniqueColumn { - spacetimedb::UniqueColumn::__new() - } + let unique_field_accessors = unique_columns.iter().map(|unique| { + let column_index = unique.index; + let vis = unique.field.vis; + let column_type = unique.field.ty; + let column_ident = unique.field.ident.unwrap(); + + let doc = format!( + "Gets the [`UniqueColumn`][spacetimedb::UniqueColumn] for the \ + [`{column_ident}`][{row_type}::{column_ident}] column." + ); + quote! { + #[doc = #doc] + #vis fn #column_ident(&self) -> spacetimedb::UniqueColumn { + spacetimedb::UniqueColumn::__new() } - }) - .collect::>(); - - let has_unique = !unique_field_accessors.is_empty(); + } + }); let tablehandle_ident = format_ident!("{}__TableHandle", table_ident); - let table_access = if let Some(span) = args.public { - quote_spanned!(span=> spacetimedb::spacetimedb_lib::db::auth::StAccess::Public) - } else { - quote!(spacetimedb::spacetimedb_lib::db::auth::StAccess::Private) - }; - let deserialize_impl = derive_deserialize(&sats_ty); let serialize_impl = derive_serialize(&sats_ty); - let schema_impl = derive_satstype(&sats_ty, true); - let column_attrs = columns.iter().map(|col| { - Ident::new( - ColumnAttribute::FLAGS - .iter() - .find_map(|f| (col.attr == *f.value()).then_some(f.name())) - .expect("Invalid column attribute"), - Span::call_site(), - ) - }); + let schema_impl = derive_satstype(&sats_ty); // Generate `integrate_generated_columns` // which will integrate all generated auto-inc col values into `_row`. - let integrate_gen_col = columns - .iter() - .filter(|col| col.attr.has_autoinc()) - .map(|col| col.field.ident.unwrap()) - .map(|field| { - quote_spanned!(field.span()=> - if spacetimedb::table::IsSequenceTrigger::is_sequence_trigger(&_row.#field) { - _row.#field = spacetimedb::sats::bsatn::from_reader(_in).unwrap(); - } - ) - }); + let integrate_gen_col = sequenced_columns.iter().map(|col| { + let field = col.field.ident.unwrap(); + quote_spanned!(field.span()=> + if spacetimedb::table::IsSequenceTrigger::is_sequence_trigger(&_row.#field) { + _row.#field = spacetimedb::sats::bsatn::from_reader(_in).unwrap(); + } + ) + }); let integrate_generated_columns = quote_spanned!(item.span() => fn integrate_generated_columns(_row: &mut #row_type, mut _generated_cols: &[u8]) { let mut _in = &mut _generated_cols; @@ -856,17 +873,18 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: } ); - let scheduled_constant = match &args.scheduled { - Some(reducer_name) => quote!(Some(<#reducer_name as spacetimedb::rt::ReducerInfo>::NAME)), - None => quote!(None), - }; + let table_access = args.access.iter().map(|acc| acc.to_value()); + let unique_col_ids = unique_columns.iter().map(|col| col.index); + let primary_col_id = primary_key_column.iter().map(|col| col.index); + let sequence_col_ids = sequenced_columns.iter().map(|col| col.index); + let scheduled_reducer_ident = args.scheduled.iter(); - let unique_err = if has_unique { + let unique_err = if !unique_columns.is_empty() { quote!(spacetimedb::UniqueConstraintViolation) } else { quote!(::core::convert::Infallible) }; - let autoinc_err = if has_autoinc { + let autoinc_err = if !sequenced_columns.is_empty() { quote!(spacetimedb::AutoIncOverflow) } else { quote!(::core::convert::Infallible) @@ -886,12 +904,13 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: } impl spacetimedb::table::TableInternal for #tablehandle_ident<'_> { const TABLE_NAME: &'static str = #table_name; - const TABLE_ACCESS: spacetimedb::spacetimedb_lib::db::auth::StAccess = #table_access; - const SCHEDULED_REDUCER_NAME: Option<&'static str> = #scheduled_constant; - const COLUMN_ATTRS: &'static [spacetimedb::spacetimedb_lib::db::attr::ColumnAttribute] = &[ - #(spacetimedb::spacetimedb_lib::db::attr::ColumnAttribute::#column_attrs),* - ]; + // the default value if not specified is Private + #(const TABLE_ACCESS: spacetimedb::table::TableAccess = #table_access;)* + const UNIQUE_COLUMNS: &'static [u16] = &[#(#unique_col_ids),*]; const INDEXES: &'static [spacetimedb::table::IndexDesc<'static>] = &[#(#indexes),*]; + #(const PRIMARY_KEY: Option = Some(#primary_col_id);)* + const SEQUENCES: &'static [u16] = &[#(#sequence_col_ids),*]; + #(const SCHEDULED_REDUCER_NAME: Option<&'static str> = Some(<#scheduled_reducer_ident as spacetimedb::rt::ReducerInfo>::NAME);)* #table_id_from_name_func } @@ -1058,7 +1077,7 @@ pub fn schema_type(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let name = &ty.name; let krate = &ty.krate; - let schema_impl = derive_satstype(&ty, true); + let schema_impl = derive_satstype(&ty); let deserialize_impl = derive_deserialize(&ty); let serialize_impl = derive_serialize(&ty); diff --git a/crates/bindings-macro/src/module.rs b/crates/bindings-macro/src/module.rs index e7d01d3f96..4faf0793b6 100644 --- a/crates/bindings-macro/src/module.rs +++ b/crates/bindings-macro/src/module.rs @@ -111,7 +111,7 @@ pub(crate) fn extract_sats_type<'a>( })?; } let krate = krate.unwrap_or(crate_fallback); - let name = name.unwrap_or_else(|| LitStr::new(&ident.to_string(), ident.span())); + let name = name.unwrap_or_else(|| crate::ident_to_litstr(ident)); Ok(SatsType { ident, @@ -123,7 +123,7 @@ pub(crate) fn extract_sats_type<'a>( }) } -pub(crate) fn derive_satstype(ty: &SatsType<'_>, gen_type_alias: bool) -> TokenStream { +pub(crate) fn derive_satstype(ty: &SatsType<'_>) -> TokenStream { let ty_name = &ty.name; let name = &ty.ident; let krate = &ty.krate; @@ -190,11 +190,6 @@ pub(crate) fn derive_satstype(ty: &SatsType<'_>, gen_type_alias: bool) -> TokenS } let (_, typeid_ty_generics, _) = typeid_generics.split_for_impl(); - let ty_name = if gen_type_alias { - quote!(Some(#ty_name)) - } else { - quote!(None) - }; quote! { #[automatically_derived] impl #impl_generics #krate::SpacetimeType for #name #ty_generics #where_clause { @@ -202,7 +197,7 @@ pub(crate) fn derive_satstype(ty: &SatsType<'_>, gen_type_alias: bool) -> TokenS #krate::sats::typespace::TypespaceBuilder::add( __typespace, core::any::TypeId::of::<#name #typeid_ty_generics>(), - #ty_name, + Some(#ty_name), |__typespace| #typ, ) } diff --git a/crates/bindings/src/rt.rs b/crates/bindings/src/rt.rs index 50355acb78..f3e59821cc 100644 --- a/crates/bindings/src/rt.rs +++ b/crates/bindings/src/rt.rs @@ -2,15 +2,13 @@ use crate::timestamp::with_timestamp_set; use crate::{sys, IterBuf, ReducerContext, ReducerResult, SpacetimeType, Table, Timestamp}; -use spacetimedb_lib::db::auth::StTableType; -use spacetimedb_lib::db::raw_def::{ - RawColumnDefV8, RawConstraintDefV8, RawIndexDefV8, RawSequenceDefV8, RawTableDefV8, -}; +pub use spacetimedb_lib::db::raw_def::v9::Lifecycle as LifecycleReducer; +use spacetimedb_lib::db::raw_def::v9::{RawIndexAlgorithm, RawModuleDefV9Builder, TableType}; use spacetimedb_lib::de::{self, Deserialize, SeqProductAccess}; use spacetimedb_lib::sats::typespace::TypespaceBuilder; use spacetimedb_lib::sats::{impl_deserialize, impl_serialize, ProductTypeElement}; use spacetimedb_lib::ser::{Serialize, SerializeSeqProduct}; -use spacetimedb_lib::{bsatn, Address, Identity, ModuleDefBuilder, RawModuleDef, ReducerDef, TableDesc}; +use spacetimedb_lib::{bsatn, Address, Identity, ProductType, RawModuleDef}; use spacetimedb_primitives::*; use std::fmt; use std::marker::PhantomData; @@ -43,6 +41,9 @@ pub trait ReducerInfo { /// The name of the reducer. const NAME: &'static str; + /// The lifecycle of the reducer, if there is one. + const LIFECYCLE: Option = None; + /// A description of the parameter names of the reducer. const ARG_NAMES: &'static [Option<&'static str>]; @@ -50,12 +51,6 @@ pub trait ReducerInfo { const INVOKE: ReducerFn; } -/// A trait for reducer types knowing their repeat interval. -pub trait RepeaterInfo: ReducerInfo { - /// At what duration intervals should this reducer repeat? - const REPEAT_INTERVAL: Duration; -} - /// A trait of types representing the arguments of a reducer. pub trait Args<'de>: Sized { /// How many arguments does the reducer accept? @@ -68,7 +63,7 @@ pub trait Args<'de>: Sized { fn serialize_seq_product(&self, prod: &mut S) -> Result<(), S::Error>; /// Returns the schema for this reducer provided a `typespace`. - fn schema(typespace: &mut impl TypespaceBuilder) -> ReducerDef; + fn schema(typespace: &mut impl TypespaceBuilder) -> ProductType; } /// A trait of types representing the result of executing a reducer. @@ -163,18 +158,15 @@ macro_rules! impl_reducer { #[inline] #[allow(non_snake_case, irrefutable_let_patterns)] - fn schema(_typespace: &mut impl TypespaceBuilder) -> ReducerDef { + fn schema(_typespace: &mut impl TypespaceBuilder) -> ProductType { // Extract the names of the arguments. let [.., $($T),*] = Info::ARG_NAMES else { panic!() }; - ReducerDef { - name: Info::NAME.into(), - args: vec![ + ProductType::new(vec![ $(ProductTypeElement { name: $T.map(Into::into), algebraic_type: <$T>::make_type(_typespace), }),* - ], - } + ].into()) } } @@ -244,77 +236,40 @@ pub fn register_reftype() { /// Registers a describer for the `TableType` `T`. pub fn register_table() { register_describer(|module| { - let data = *T::Row::make_type(&mut module.inner).as_ref().unwrap(); - let columns: Vec = RawColumnDefV8::from_product_type( - module - .inner - .typespace() - .with_type(&data) - .resolve_refs() - .expect("Failed to retrieve the column types from the module") - .into_product() - .expect("Table is not a product type"), - ); - - let indexes: Vec<_> = T::INDEXES.iter().copied().map(Into::into).collect(); - //WARNING: The definition of table assumes the # of constraints == # of columns elsewhere `T::COLUMN_ATTRS` is queried - let constraints: Vec<_> = T::COLUMN_ATTRS - .iter() - .enumerate() - .map(|(col_pos, x)| { - let col = &columns[col_pos]; - let kind = match (*x).try_into() { - Ok(x) => x, - Err(_) => Constraints::unset(), - }; - - RawConstraintDefV8::for_column(T::TABLE_NAME, &col.col_name, kind, ColList::new(col_pos.into())) - }) - .collect(); - - let sequences: Vec<_> = T::COLUMN_ATTRS - .iter() - .enumerate() - .filter_map(|(col_pos, x)| { - let col = &columns[col_pos]; - - if x.kind() == AttributeKind::AUTO_INC { - Some(RawSequenceDefV8::for_column( - T::TABLE_NAME, - &col.col_name, - col_pos.into(), - )) - } else { - None - } - }) - .collect(); - - let schema = RawTableDefV8::new(T::TABLE_NAME.into(), columns) - .with_type(StTableType::User) - .with_access(T::TABLE_ACCESS) - .with_constraints(constraints) - .with_sequences(sequences) - .with_indexes(indexes) - .with_scheduled(T::SCHEDULED_REDUCER_NAME.map(Into::into)); - let schema = TableDesc { schema, data }; - - module.inner.add_table(schema) + let product_type_ref = *T::Row::make_type(&mut module.inner).as_ref().unwrap(); + + let mut table = module + .inner + .build_table(T::TABLE_NAME, product_type_ref) + .with_type(TableType::User) + .with_access(T::TABLE_ACCESS); + + for &col in T::UNIQUE_COLUMNS { + table = table.with_unique_constraint(col, None); + } + for &index in T::INDEXES { + table = table.with_index(index.algo.into(), index.accessor_name, None); + } + if let Some(primary_key) = T::PRIMARY_KEY { + table = table.with_primary_key(primary_key); + } + for &col in T::SEQUENCES { + table = table.with_column_sequence(col, None); + } + if let Some(scheduled_reducer) = T::SCHEDULED_REDUCER_NAME { + table = table.with_schedule(scheduled_reducer, None); + } + + table.finish(); }) } -impl From> for RawIndexDefV8 { - fn from(index: crate::table::IndexDesc<'_>) -> RawIndexDefV8 { - let columns = index.col_ids.iter().copied().collect::(); - if columns.is_empty() { - panic!("Need at least one column in IndexDesc for index `{}`", index.name); - }; - - RawIndexDefV8 { - index_name: index.name.into(), - is_unique: false, - index_type: index.ty, - columns, +impl From> for RawIndexAlgorithm { + fn from(algo: crate::table::IndexAlgo<'_>) -> RawIndexAlgorithm { + match algo { + crate::table::IndexAlgo::BTree { columns } => RawIndexAlgorithm::BTree { + columns: columns.iter().copied().collect(), + }, } } } @@ -322,8 +277,8 @@ impl From> for RawIndexDefV8 { /// Registers a describer for the reducer `I` with arguments `A`. pub fn register_reducer<'a, A: Args<'a>, I: ReducerInfo>(_: impl Reducer<'a, A>) { register_describer(|module| { - let schema = A::schema::(&mut module.inner); - module.inner.add_reducer(schema); + let params = A::schema::(&mut module.inner); + module.inner.add_reducer(I::NAME, params, I::LIFECYCLE); module.reducers.push(I::INVOKE); }) } @@ -332,7 +287,7 @@ pub fn register_reducer<'a, A: Args<'a>, I: ReducerInfo>(_: impl Reducer<'a, A>) #[derive(Default)] struct ModuleBuilder { /// The module definition. - inner: ModuleDefBuilder, + inner: RawModuleDefV9Builder, /// The reducers of the module. reducers: Vec, } @@ -370,7 +325,7 @@ extern "C" fn __describe_module__(description: BytesSink) { // Serialize the module to bsatn. let module_def = module.inner.finish(); - let module_def = RawModuleDef::V8BackCompat(module_def); + let module_def = RawModuleDef::V9(module_def); let bytes = bsatn::to_vec(&module_def).expect("unable to serialize typespace"); // Write the set of reducers. diff --git a/crates/bindings/src/table.rs b/crates/bindings/src/table.rs index ea338c782e..f88e969f35 100644 --- a/crates/bindings/src/table.rs +++ b/crates/bindings/src/table.rs @@ -4,9 +4,7 @@ use std::marker::PhantomData; use std::{fmt, ops}; use spacetimedb_lib::buffer::{BufReader, Cursor}; -use spacetimedb_lib::db::attr::ColumnAttribute; -use spacetimedb_lib::db::auth::StAccess; -use spacetimedb_lib::db::raw_def::IndexType; +pub use spacetimedb_lib::db::raw_def::v9::TableAccess; use spacetimedb_primitives::ColId; use crate::{bsatn, sys, DeserializeOwned, IterBuf, Serialize, SpacetimeType, TableId}; @@ -120,25 +118,13 @@ pub trait Table: TableInternal { #[doc(hidden)] pub trait TableInternal: Sized { const TABLE_NAME: &'static str; - const TABLE_ACCESS: StAccess; - const COLUMN_ATTRS: &'static [ColumnAttribute]; + const TABLE_ACCESS: TableAccess = TableAccess::Private; + const UNIQUE_COLUMNS: &'static [u16]; const INDEXES: &'static [IndexDesc<'static>]; + const PRIMARY_KEY: Option = None; + const SEQUENCES: &'static [u16]; const SCHEDULED_REDUCER_NAME: Option<&'static str> = None; - /// Whether any of [`Self::COLUMN_ATTRS`] have an autoinc constraint. - const HAS_AUTOINC: bool = 'autoinc: { - // NOTE: Written this way because iterators aren't const-stable. - // Same as `T::COLUMN_ATTRS.iter().any(|attr| attr.has_autoinc())`. - let mut i = 0; - while i < Self::COLUMN_ATTRS.len() { - if Self::COLUMN_ATTRS[i].has_autoinc() { - break 'autoinc true; - } - i += 1; - } - false - }; - /// Returns the ID of this table. fn table_id() -> TableId; } @@ -146,12 +132,13 @@ pub trait TableInternal: Sized { /// Describe a named index with an index type over a set of columns identified by their IDs. #[derive(Clone, Copy)] pub struct IndexDesc<'a> { - /// The name of the index. - pub name: &'a str, - /// The type of index used, i.e. the strategy used for indexing. - pub ty: IndexType, - /// The set of columns indexed over given by the identifiers of the columns. - pub col_ids: &'a [u16], + pub accessor_name: &'a str, + pub algo: IndexAlgo<'a>, +} + +#[derive(Clone, Copy)] +pub enum IndexAlgo<'a> { + BTree { columns: &'a [u16] }, } #[doc(hidden)] diff --git a/crates/lib/src/db/raw_def/v8.rs b/crates/lib/src/db/raw_def/v8.rs index 3555994f2c..a5bab53c0b 100644 --- a/crates/lib/src/db/raw_def/v8.rs +++ b/crates/lib/src/db/raw_def/v8.rs @@ -478,10 +478,12 @@ impl RawTableDefV8 { /// /// It looks into [Self::constraints] for possible duplicates and remove them from the result pub fn generated_sequences(&self) -> impl Iterator + '_ { + let cols: HashSet<_> = self.sequences.iter().map(|seq| ColList::new(seq.col_pos)).collect(); + self.constraints .iter() // We are only interested in constraints implying a sequence. - .filter(|x| x.constraints.has_autoinc()) + .filter(move |x| !cols.contains(&x.columns) && x.constraints.has_autoinc()) // Create the `SequenceDef`. .map(|x| RawSequenceDefV8::for_column(&self.table_name, &x.constraint_name, x.columns.head().unwrap())) // Only keep those we don't yet have in the list of sequences (checked by name). diff --git a/modules/rust-wasm-test/src/lib.rs b/modules/rust-wasm-test/src/lib.rs index 3d0077307f..634b835891 100644 --- a/modules/rust-wasm-test/src/lib.rs +++ b/modules/rust-wasm-test/src/lib.rs @@ -1,5 +1,5 @@ #![allow(clippy::disallowed_names)] -use spacetimedb::spacetimedb_lib::db::auth::StAccess; +use spacetimedb::spacetimedb_lib::db::raw_def::v9::TableAccess; use spacetimedb::spacetimedb_lib::{self, bsatn}; use spacetimedb::{duration, table, Address, Deserialize, Identity, ReducerContext, SpacetimeType, Table, Timestamp}; @@ -29,12 +29,12 @@ pub struct TestD { // uses internal apis that should not be used by user code #[allow(dead_code)] // false positive -const fn get_table_access() -> StAccess { +const fn get_table_access() -> TableAccess { as spacetimedb::table::TableInternal>::TABLE_ACCESS } // This table was specified as public. -const _: () = assert!(matches!(get_table_access::(), StAccess::Public)); +const _: () = assert!(matches!(get_table_access::(), TableAccess::Public)); #[spacetimedb::table(name = test_e)] #[derive(Debug)] @@ -55,7 +55,7 @@ pub enum TestF { } // // All tables are private by default. -const _: () = assert!(matches!(get_table_access::(), StAccess::Private)); +const _: () = assert!(matches!(get_table_access::(), TableAccess::Private)); #[spacetimedb::table(name = private)] pub struct Private { @@ -69,7 +69,7 @@ pub struct Point { } // It is redundant, but we can explicitly specify a table as private. -const _: () = assert!(matches!(get_table_access::(), StAccess::Private)); +const _: () = assert!(matches!(get_table_access::(), TableAccess::Private)); // Test we can compile multiple constraints #[spacetimedb::table(name = pk_multi_identity)] From cc354ac2870922a1a12f2a060720e622e75586d2 Mon Sep 17 00:00:00 2001 From: Noa Date: Thu, 19 Sep 2024 13:41:11 -0500 Subject: [PATCH 6/9] Implement btree api on new abi --- crates/bindings-macro/src/lib.rs | 129 ++++++++++++++++++++++++------- crates/bindings/src/rt.rs | 2 +- crates/bindings/src/table.rs | 60 ++++++++------ 3 files changed, 136 insertions(+), 55 deletions(-) diff --git a/crates/bindings-macro/src/lib.rs b/crates/bindings-macro/src/lib.rs index 8cfc187ee0..c9344397fb 100644 --- a/crates/bindings-macro/src/lib.rs +++ b/crates/bindings-macro/src/lib.rs @@ -15,7 +15,7 @@ use heck::ToSnakeCase; use module::{derive_deserialize, derive_satstype, derive_serialize}; use proc_macro::TokenStream as StdTokenStream; use proc_macro2::{Span, TokenStream}; -use quote::{format_ident, quote, quote_spanned}; +use quote::{format_ident, quote, quote_spanned, ToTokens}; use std::collections::HashMap; use std::time::Duration; use syn::ext::IdentExt; @@ -567,12 +567,8 @@ impl IndexArg { Ok(IndexArg { kind, name }) } - fn to_desc_and_accessor( - &self, - index_index: u32, - cols: &[Column], - ) -> Result<(TokenStream, TokenStream), syn::Error> { - let (algo, accessor) = match &self.kind { + fn validate<'a>(&'a self, table_name: &str, cols: &'a [Column<'a>]) -> syn::Result> { + let kind = match &self.kind { IndexType::BTree { columns } => { let cols = columns .iter() @@ -585,28 +581,97 @@ impl IndexArg { }) .collect::>>()?; + ValidatedIndexType::BTree { cols } + } + }; + let index_name = match &kind { + ValidatedIndexType::BTree { cols } => ([table_name, "btree"].into_iter()) + .chain(cols.iter().map(|col| col.field.name.as_deref().unwrap())) + .collect::>() + .join("_"), + }; + Ok(ValidatedIndex { + index_name, + accessor_name: &self.name, + kind, + }) + } +} + +struct ValidatedIndex<'a> { + index_name: String, + accessor_name: &'a Ident, + kind: ValidatedIndexType<'a>, +} + +enum ValidatedIndexType<'a> { + BTree { cols: Vec<&'a Column<'a>> }, +} + +impl ValidatedIndex<'_> { + fn desc(&self) -> TokenStream { + let algo = match &self.kind { + ValidatedIndexType::BTree { cols } => { let col_ids = cols.iter().map(|col| col.index); - let algo = quote!(spacetimedb::table::IndexAlgo::BTree { + quote!(spacetimedb::table::IndexAlgo::BTree { columns: &[#(#col_ids),*] - }); + }) + } + }; + let index_name = &self.index_name; + let accessor_name = ident_to_litstr(self.accessor_name); + quote!(spacetimedb::table::IndexDesc { + name: #index_name, + accessor_name: #accessor_name, + algo: #algo, + }) + } - let index_ident = &self.name; + fn accessor(&self, vis: &syn::Visibility, row_type_ident: &Ident) -> TokenStream { + match &self.kind { + ValidatedIndexType::BTree { cols } => { + let index_ident = self.accessor_name; let col_tys = cols.iter().map(|col| col.ty); - let accessor = quote! { - fn #index_ident(&self) -> spacetimedb::BTreeIndex { + let mut doc = format!( + "Gets the `{index_ident}` [`BTreeIndex`][spacetimedb::BTreeIndex] as defined \ + on this table. \n\ + \n\ + This B-tree index is defined on the following columns, in order:\n" + ); + for col in cols { + use std::fmt::Write; + writeln!( + doc, + "- [`{ident}`][{row_type_ident}#structfield.{ident}]: [`{ty}`]", + ident = col.field.ident.unwrap(), + ty = col.ty.to_token_stream() + ) + .unwrap(); + } + quote! { + #[doc = #doc] + #vis fn #index_ident(&self) -> spacetimedb::BTreeIndex { spacetimedb::BTreeIndex::__new() } - }; + } + } + } + } - (algo, accessor) + fn marker_type(&self) -> TokenStream { + let index_ident = self.accessor_name; + let index_name = &self.index_name; + quote! { + pub struct #index_ident; + impl spacetimedb::table::Index for #index_ident { + fn index_id() -> spacetimedb::table::IndexId { + static INDEX_ID: std::sync::OnceLock = std::sync::OnceLock::new(); + *INDEX_ID.get_or_init(|| { + spacetimedb::sys::index_id_from_name(#index_name).unwrap() + }) + } } - }; - let accessor_name = ident_to_litstr(&self.name); - let desc = quote!(spacetimedb::table::IndexDesc { - accessor_name: #accessor_name, - algo: #algo, - }); - Ok((desc, accessor)) + } } } @@ -821,16 +886,15 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: let row_type = quote!(#original_struct_ident); - let (indexes, index_accessors) = args + let indices = args .indices .iter() - .enumerate() - .map(|(i, index)| index.to_desc_and_accessor(i as u32, &columns)) - // TODO: stabilized in 1.79 - // .collect::, Vec<_>)>>()?; - .collect::>>()? - .into_iter() - .unzip::<_, _, Vec<_>, Vec<_>>(); + .map(|index| index.validate(&table_name, &columns)) + .collect::>>()?; + + let index_descs = indices.iter().map(|index| index.desc()); + let index_accessors = indices.iter().map(|index| index.accessor(vis, original_struct_ident)); + let index_marker_types = indices.iter().map(|index| index.marker_type()); let unique_field_accessors = unique_columns.iter().map(|unique| { let column_index = unique.index; @@ -907,7 +971,7 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: // the default value if not specified is Private #(const TABLE_ACCESS: spacetimedb::table::TableAccess = #table_access;)* const UNIQUE_COLUMNS: &'static [u16] = &[#(#unique_col_ids),*]; - const INDEXES: &'static [spacetimedb::table::IndexDesc<'static>] = &[#(#indexes),*]; + const INDEXES: &'static [spacetimedb::table::IndexDesc<'static>] = &[#(#index_descs),*]; #(const PRIMARY_KEY: Option = Some(#primary_col_id);)* const SEQUENCES: &'static [u16] = &[#(#sequence_col_ids),*]; #(const SCHEDULED_REDUCER_NAME: Option<&'static str> = Some(<#scheduled_reducer_ident as spacetimedb::rt::ReducerInfo>::NAME);)* @@ -1006,6 +1070,11 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: #tabletype_impl + #[allow(non_camel_case_types)] + mod __indices { + #(#index_marker_types)* + } + #describe_table_func }; diff --git a/crates/bindings/src/rt.rs b/crates/bindings/src/rt.rs index f3e59821cc..51f02ae7df 100644 --- a/crates/bindings/src/rt.rs +++ b/crates/bindings/src/rt.rs @@ -248,7 +248,7 @@ pub fn register_table() { table = table.with_unique_constraint(col, None); } for &index in T::INDEXES { - table = table.with_index(index.algo.into(), index.accessor_name, None); + table = table.with_index(index.algo.into(), index.accessor_name, Some(index.name.into())); } if let Some(primary_key) = T::PRIMARY_KEY { table = table.with_primary_key(primary_key); diff --git a/crates/bindings/src/table.rs b/crates/bindings/src/table.rs index f88e969f35..88d2d8a274 100644 --- a/crates/bindings/src/table.rs +++ b/crates/bindings/src/table.rs @@ -4,8 +4,9 @@ use std::marker::PhantomData; use std::{fmt, ops}; use spacetimedb_lib::buffer::{BufReader, Cursor}; + pub use spacetimedb_lib::db::raw_def::v9::TableAccess; -use spacetimedb_primitives::ColId; +pub use spacetimedb_primitives::{ColId, IndexId}; use crate::{bsatn, sys, DeserializeOwned, IterBuf, Serialize, SpacetimeType, TableId}; @@ -132,6 +133,7 @@ pub trait TableInternal: Sized { /// Describe a named index with an index type over a set of columns identified by their IDs. #[derive(Clone, Copy)] pub struct IndexDesc<'a> { + pub name: &'a str, pub accessor_name: &'a str, pub algo: IndexAlgo<'a>, } @@ -337,11 +339,15 @@ where } } -pub struct BTreeIndex { - _marker: PhantomData<(Tbl, IndexType)>, +pub trait Index { + fn index_id() -> IndexId; +} + +pub struct BTreeIndex { + _marker: PhantomData<(Tbl, IndexType, Idx)>, } -impl BTreeIndex { +impl BTreeIndex { #[doc(hidden)] pub fn __new() -> Self { Self { _marker: PhantomData } @@ -353,13 +359,16 @@ impl BTreeIndex, K>(&self, b: B) -> impl Iterator { + pub fn filter(&self, b: B) -> impl Iterator + where + B: BTreeIndexBounds, + { + let index_id = Idx::index_id(); let args = b.get_args(); let (prefix, prefix_elems, rstart, rend) = args.args_for_syscall(); - #[allow(unreachable_code)] - TableIter::new(todo!( - "once implemented: datastore_btree_scan_bsatn({prefix:?}, {prefix_elems:?}, {rstart:?}, {rend:?})" - )) + let iter = sys::datastore_btree_scan_bsatn(index_id, prefix, prefix_elems, rstart, rend) + .unwrap_or_else(|e| panic!("unexpected error from datastore_btree_scan_bsatn: {e}")); + TableIter::new(iter) } /// Deletes all rows in the database state where the indexed column(s) match the bounds `b`. @@ -371,10 +380,16 @@ impl BTreeIndex, K>(&self, b: B) -> u64 { + pub fn delete(&self, b: B) -> u64 + where + B: BTreeIndexBounds, + { + let index_id = Idx::index_id(); let args = b.get_args(); let (prefix, prefix_elems, rstart, rend) = args.args_for_syscall(); - todo!("once implemented: datastore_delete_by_btree_scan_bsatn({prefix:?}, {prefix_elems:?}, {rstart:?}, {rend:?})") + sys::datastore_delete_by_btree_scan_bsatn(index_id, prefix, prefix_elems, rstart, rend) + .unwrap_or_else(|e| panic!("unexpected error from datastore_delete_by_btree_scan_bsatn: {e}")) + .into() } } @@ -394,13 +409,14 @@ pub struct BTreeScanArgs { impl BTreeScanArgs { pub(crate) fn args_for_syscall(&self) -> (&[u8], ColId, &[u8], &[u8]) { - let len = self.data.len(); - ( - &self.data[..self.rstart_idx], - ColId::from(self.prefix_elems), - &self.data[self.rstart_idx..self.rend_idx.unwrap_or(len)], - &self.data[self.rend_idx.unwrap_or(self.rstart_idx)..], - ) + let prefix = &self.data[..self.rstart_idx]; + let (rstart, rend) = if let Some(rend_idx) = self.rend_idx { + (&self.data[self.rstart_idx..rend_idx], &self.data[rend_idx..]) + } else { + let elem = &self.data[self.rstart_idx..]; + (elem, elem) + }; + (prefix, ColId::from(self.prefix_elems), rstart, rend) } } @@ -464,14 +480,10 @@ impl TermBound<&T> { TermBound::Single(elem) => (elem, None), TermBound::Range(start, end) => (start, Some(end)), }; - let serialize_bound = |_buf: &mut Vec, _bound: &ops::Bound<&T>| { - // bsatn::to_writer(buf, bound).unwrap(); - todo!(); - }; - serialize_bound(buf, start); + bsatn::to_writer(buf, start).unwrap(); end.map(|end| { let rend_idx = buf.len(); - serialize_bound(buf, end); + bsatn::to_writer(buf, end).unwrap(); rend_idx }) } From 1e2fcc20d2fb47386522f62c70c7ba4234d70efb Mon Sep 17 00:00:00 2001 From: Noa Date: Mon, 23 Sep 2024 15:47:06 -0500 Subject: [PATCH 7/9] Make TryInsertError static --- crates/bindings-macro/src/lib.rs | 23 +++++++++++------------ crates/bindings/src/table.rs | 2 +- modules/rust-wasm-test/src/lib.rs | 10 +++++----- smoketests/tests/auto_inc.py | 2 +- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/crates/bindings-macro/src/lib.rs b/crates/bindings-macro/src/lib.rs index c9344397fb..7e5495f525 100644 --- a/crates/bindings-macro/src/lib.rs +++ b/crates/bindings-macro/src/lib.rs @@ -958,7 +958,7 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: let field_types = fields.iter().map(|f| f.ty).collect::>(); let tabletype_impl = quote! { - impl spacetimedb::Table for #tablehandle_ident<'_> { + impl spacetimedb::Table for #tablehandle_ident { type Row = #row_type; type UniqueConstraintViolation = #unique_err; @@ -966,7 +966,7 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: #integrate_generated_columns } - impl spacetimedb::table::TableInternal for #tablehandle_ident<'_> { + impl spacetimedb::table::TableInternal for #tablehandle_ident { const TABLE_NAME: &'static str = #table_name; // the default value if not specified is Private #(const TABLE_ACCESS: spacetimedb::table::TableAccess = #table_access;)* @@ -985,7 +985,7 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: let describe_table_func = quote! { #[export_name = #register_describer_symbol] extern "C" fn __register_describer() { - spacetimedb::rt::register_table::<#tablehandle_ident<'static>>() + spacetimedb::rt::register_table::<#tablehandle_ident>() } }; @@ -1027,22 +1027,21 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: let trait_def = quote_spanned! {table_ident.span()=> #[allow(non_camel_case_types, dead_code)] #vis trait #table_ident { - fn #table_ident(&self) -> #row_type_to_table<'_>; + fn #table_ident(&self) -> &#row_type_to_table; } impl #table_ident for spacetimedb::Local { - fn #table_ident(&self) -> #row_type_to_table<'_> { + fn #table_ident(&self) -> &#row_type_to_table { #[allow(non_camel_case_types)] - type #tablehandle_ident<'a> = #row_type_to_table<'a>; - #tablehandle_ident { _local: ::core::marker::PhantomData } + type #tablehandle_ident = #row_type_to_table; + &#tablehandle_ident {} } } }; let tablehandle_def = quote! { #[allow(non_camel_case_types)] - #vis struct #tablehandle_ident<'a> { - _local: ::core::marker::PhantomData<&'a spacetimedb::Local>, - } + #[non_exhaustive] + #vis struct #tablehandle_ident {} }; let emission = quote! { @@ -1060,10 +1059,10 @@ fn table_impl(mut args: TableArgs, mut item: MutItem) -> syn:: #tablehandle_def impl spacetimedb::table::__MapRowTypeToTable for #row_type { - type Table<'a> = #tablehandle_ident<'a>; + type Table = #tablehandle_ident; } - impl<'a> #tablehandle_ident<'a> { + impl #tablehandle_ident { #(#unique_field_accessors)* #(#index_accessors)* } diff --git a/crates/bindings/src/table.rs b/crates/bindings/src/table.rs index 88d2d8a274..0114f534a7 100644 --- a/crates/bindings/src/table.rs +++ b/crates/bindings/src/table.rs @@ -145,7 +145,7 @@ pub enum IndexAlgo<'a> { #[doc(hidden)] pub trait __MapRowTypeToTable { - type Table<'a>: Table; + type Table: Table; } /// A UNIQUE constraint violation on a table was attempted. diff --git a/modules/rust-wasm-test/src/lib.rs b/modules/rust-wasm-test/src/lib.rs index 634b835891..1d122641d6 100644 --- a/modules/rust-wasm-test/src/lib.rs +++ b/modules/rust-wasm-test/src/lib.rs @@ -29,12 +29,12 @@ pub struct TestD { // uses internal apis that should not be used by user code #[allow(dead_code)] // false positive -const fn get_table_access() -> TableAccess { - as spacetimedb::table::TableInternal>::TABLE_ACCESS +const fn get_table_access(_: impl Fn(&spacetimedb::Local) -> &Tbl + Copy) -> TableAccess { + ::TABLE_ACCESS } // This table was specified as public. -const _: () = assert!(matches!(get_table_access::(), TableAccess::Public)); +const _: () = assert!(matches!(get_table_access(test_d::test_d), TableAccess::Public)); #[spacetimedb::table(name = test_e)] #[derive(Debug)] @@ -55,7 +55,7 @@ pub enum TestF { } // // All tables are private by default. -const _: () = assert!(matches!(get_table_access::(), TableAccess::Private)); +const _: () = assert!(matches!(get_table_access(test_e::test_e), TableAccess::Private)); #[spacetimedb::table(name = private)] pub struct Private { @@ -69,7 +69,7 @@ pub struct Point { } // It is redundant, but we can explicitly specify a table as private. -const _: () = assert!(matches!(get_table_access::(), TableAccess::Private)); +const _: () = assert!(matches!(get_table_access(points::points), TableAccess::Private)); // Test we can compile multiple constraints #[spacetimedb::table(name = pk_multi_identity)] diff --git a/smoketests/tests/auto_inc.py b/smoketests/tests/auto_inc.py index f91bf93955..22af3379c9 100644 --- a/smoketests/tests/auto_inc.py +++ b/smoketests/tests/auto_inc.py @@ -72,7 +72,7 @@ def do_test_autoinc(self, int_ty): #[spacetimedb::reducer] pub fn add_new_$KEY_TY(ctx: &ReducerContext, name: String) -> Result<(), Box> { - let value = ctx.db.person_$KEY_TY().insert(Person_$KEY_TY { key_col: 0, name }); + let value = ctx.db.person_$KEY_TY().try_insert(Person_$KEY_TY { key_col: 0, name })?; println!("Assigned Value: {} -> {}", value.key_col, value.name); Ok(()) } From 62e8e504d3a0bc263fbe6dc9c3a87c332863c4e5 Mon Sep 17 00:00:00 2001 From: Noa Date: Wed, 25 Sep 2024 13:20:27 -0500 Subject: [PATCH 8/9] Link btree abi functions to module --- crates/core/src/host/wasm_common.rs | 3 +++ crates/core/src/host/wasmtime/wasm_instance_env.rs | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/core/src/host/wasm_common.rs b/crates/core/src/host/wasm_common.rs index 9efdb64f5e..9c689ef0bc 100644 --- a/crates/core/src/host/wasm_common.rs +++ b/crates/core/src/host/wasm_common.rs @@ -366,6 +366,9 @@ macro_rules! abi_funcs { "spacetime_10.0"::iter_by_col_eq, "spacetime_10.0"::iter_start_filtered, "spacetime_10.0"::volatile_nonatomic_schedule_immediate, + "spacetime_10.0"::index_id_from_name, + "spacetime_10.0"::datastore_btree_scan_bsatn, + "spacetime_10.0"::datastore_delete_by_btree_scan_bsatn, } }; } diff --git a/crates/core/src/host/wasmtime/wasm_instance_env.rs b/crates/core/src/host/wasmtime/wasm_instance_env.rs index 98f77c6905..52772d40d0 100644 --- a/crates/core/src/host/wasmtime/wasm_instance_env.rs +++ b/crates/core/src/host/wasmtime/wasm_instance_env.rs @@ -518,7 +518,7 @@ impl WasmInstanceEnv { /// Or when `rstart` or `rend` cannot be decoded to an `Bound` /// where the inner `AlgebraicValue`s are /// typed at the `prefix_elems + 1` `AlgebraicType` of the index's key type. - fn datastore_btree_scan_bsatn( + pub fn datastore_btree_scan_bsatn( caller: Caller<'_, Self>, index_id: u32, prefix_ptr: WasmPtr, From fd576130fb44a68457e78dd415d2c1b7507db470 Mon Sep 17 00:00:00 2001 From: Noa Date: Thu, 26 Sep 2024 14:11:36 -0500 Subject: [PATCH 9/9] Remove validate_integration tests --- crates/schema/tests/validate_integration.rs | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/crates/schema/tests/validate_integration.rs b/crates/schema/tests/validate_integration.rs index 47bc970cdf..c670b3651e 100644 --- a/crates/schema/tests/validate_integration.rs +++ b/crates/schema/tests/validate_integration.rs @@ -63,16 +63,6 @@ fn assert_identical(module_def_1: ModuleDef, module_def_2: ModuleDef) { assert_eq!(s1, s2, "ModuleDefs are not identical"); } -#[test] -fn validate_rust_wasm_test() { - validate_module("rust-wasm-test"); -} - -#[test] -fn validate_sdk_test() { - validate_module("sdk-test"); -} - #[test] fn validate_cs_modules() { // These need to be called in sequence because running them in parallel locks the codegen DLLs @@ -81,13 +71,3 @@ fn validate_cs_modules() { validate_module("sdk-test-connect-disconnect-cs"); validate_module("spacetimedb-quickstart-cs"); } - -#[test] -fn validate_sdk_test_connect_disconnect() { - validate_module("sdk-test-connect-disconnect"); -} - -#[test] -fn validate_spacetimedb_quickstart() { - validate_module("spacetimedb-quickstart"); -}