Skip to content

Commit cba518d

Browse files
Centrilmamcx
andauthored
Use newtypes ColId, TableId, IndexId, SequenceId everywhere (*) (#408)
* Use newtypes ColId, TableId, IndexId, SequenceId everywhere (*) * Addressing some PR comments --------- Co-authored-by: Mario Alejandro Montoya Cortés <[email protected]>
1 parent 759a9ee commit cba518d

File tree

43 files changed

+931
-893
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+931
-893
lines changed

Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ members = [
1515
"crates/sdk",
1616
"crates/client-api-messages",
1717
"crates/sqltest",
18+
"crates/primitives",
1819
"modules/rust-wasm-test",
1920
"modules/benchmarks",
2021
"modules/spacetimedb-quickstart",

crates/bench/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ spacetimedb-core = { path = "../core" }
2525
spacetimedb-standalone = { path = "../standalone" }
2626
spacetimedb-client-api = { path = "../client-api" }
2727
spacetimedb-testing = { path = "../testing" }
28+
spacetimedb-primitives = { path = "../primitives" }
2829

2930
ahash.workspace = true
3031
log.workspace = true

crates/bench/benches/generic.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fn bench_harness<
122122

123123
#[inline(never)]
124124
fn empty<DB: BenchDatabase>(g: &mut Group, db: &mut DB) -> ResultBench<()> {
125-
let id = format!("empty");
125+
let id = "empty".to_string();
126126
g.bench_function(&id, |b| {
127127
bench_harness(
128128
b,
@@ -159,11 +159,11 @@ fn insert_1<DB: BenchDatabase, T: BenchTable + RandomTable>(
159159
let mut data = data.clone();
160160
db.clear_table(table_id)?;
161161
let row = data.pop().unwrap();
162-
db.insert_bulk(&table_id, data)?;
162+
db.insert_bulk(table_id, data)?;
163163
Ok(row)
164164
},
165165
|db, row| {
166-
db.insert(&table_id, row)?;
166+
db.insert(table_id, row)?;
167167
Ok(())
168168
},
169169
)
@@ -196,12 +196,12 @@ fn insert_bulk<DB: BenchDatabase, T: BenchTable + RandomTable>(
196196
db.clear_table(table_id)?;
197197
let to_insert = data.split_off(load as usize);
198198
if !data.is_empty() {
199-
db.insert_bulk(&table_id, data)?;
199+
db.insert_bulk(table_id, data)?;
200200
}
201201
Ok(to_insert)
202202
},
203203
|db, to_insert| {
204-
db.insert_bulk(&table_id, to_insert)?;
204+
db.insert_bulk(table_id, to_insert)?;
205205
Ok(())
206206
},
207207
)
@@ -269,7 +269,7 @@ fn filter<DB: BenchDatabase, T: BenchTable + RandomTable>(
269269

270270
let data = create_sequential::<T>(0xdeadbeef, load, buckets as u64);
271271

272-
db.insert_bulk(&table_id, data.clone())?;
272+
db.insert_bulk(table_id, data.clone())?;
273273

274274
// Each iteration performs a single transaction.
275275
g.throughput(criterion::Throughput::Elements(1));
@@ -290,7 +290,7 @@ fn filter<DB: BenchDatabase, T: BenchTable + RandomTable>(
290290
Ok(value)
291291
},
292292
|db, value| {
293-
db.filter::<T>(&table_id, column_index, value)?;
293+
db.filter::<T>(table_id, column_index, value)?;
294294
Ok(())
295295
},
296296
)
@@ -319,7 +319,7 @@ fn find<DB: BenchDatabase, T: BenchTable + RandomTable>(
319319

320320
let data = create_sequential::<T>(0xdeadbeef, load, buckets as u64);
321321

322-
db.insert_bulk(&table_id, data.clone())?;
322+
db.insert_bulk(table_id, data.clone())?;
323323

324324
// Each iteration performs a single transaction.
325325
g.throughput(criterion::Throughput::Elements(1));
@@ -339,7 +339,7 @@ fn find<DB: BenchDatabase, T: BenchTable + RandomTable>(
339339
Ok(value)
340340
},
341341
|db, value| {
342-
db.filter::<T>(&table_id, column_id, value)?;
342+
db.filter::<T>(table_id, column_id, value)?;
343343
Ok(())
344344
},
345345
)

crates/bench/src/spacetime_raw.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ use crate::{
33
schemas::{table_name, BenchTable, IndexStrategy},
44
ResultBench,
55
};
6-
use spacetimedb::db::datastore::traits::{ColId, IndexDef, TableDef};
6+
use spacetimedb::db::datastore::traits::{IndexDef, TableDef};
77
use spacetimedb::db::relational_db::{open_db, RelationalDB};
88
use spacetimedb_lib::sats::AlgebraicValue;
9+
use spacetimedb_primitives::{ColId, TableId};
910
use std::hint::black_box;
1011
use tempdir::TempDir;
1112

@@ -15,11 +16,12 @@ pub struct SpacetimeRaw {
1516
db: RelationalDB,
1617
_temp_dir: TempDir,
1718
}
19+
1820
impl BenchDatabase for SpacetimeRaw {
1921
fn name() -> &'static str {
2022
"stdb_raw"
2123
}
22-
type TableId = u32;
24+
type TableId = TableId;
2325

2426
fn build(in_memory: bool, fsync: bool) -> ResultBench<Self>
2527
where
@@ -43,14 +45,14 @@ impl BenchDatabase for SpacetimeRaw {
4345
match index_strategy {
4446
IndexStrategy::Unique => {
4547
self.db
46-
.create_index(tx, IndexDef::new("id".to_string(), table_id, 0, true))?;
48+
.create_index(tx, IndexDef::new("id".to_string(), table_id, 0.into(), true))?;
4749
}
4850
IndexStrategy::NonUnique => (),
4951
IndexStrategy::MultiIndex => {
5052
for (i, column) in T::product_type().elements.iter().enumerate() {
5153
self.db.create_index(
5254
tx,
53-
IndexDef::new(column.name.clone().unwrap(), table_id, i as u32, false),
55+
IndexDef::new(column.name.clone().unwrap(), table_id, i.into(), false),
5456
)?;
5557
}
5658
}

crates/bindings-macro/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,8 @@ fn spacetimedb_tabletype_impl(item: syn::DeriveInput) -> syn::Result<TokenStream
533533
let mut columns = Vec::<Column>::new();
534534

535535
let get_table_id_func = quote! {
536-
fn table_id() -> u32 {
537-
static TABLE_ID: spacetimedb::rt::OnceCell<u32> = spacetimedb::rt::OnceCell::new();
536+
fn table_id() -> spacetimedb::TableId {
537+
static TABLE_ID: spacetimedb::rt::OnceCell<spacetimedb::TableId> = spacetimedb::rt::OnceCell::new();
538538
*TABLE_ID.get_or_init(|| {
539539
spacetimedb::get_table_id(<Self as spacetimedb::TableType>::TABLE_NAME)
540540
})

crates/bindings-sys/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ bench = false
1111

1212
[dependencies]
1313
getrandom = {workspace = true, optional = true}
14+
spacetimedb-primitives = { path = "../primitives" }

crates/bindings-sys/src/lib.rs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ use std::ptr;
1313

1414
use alloc::boxed::Box;
1515

16+
use spacetimedb_primitives::{ColId, TableId};
17+
1618
/// Provides a raw set of sys calls which abstractions can be built atop of.
1719
pub mod raw {
1820
use core::mem::ManuallyDrop;
21+
use spacetimedb_primitives::{ColId, TableId};
1922

2023
// this module identifier determines the abi version that modules built with this crate depend
2124
// on. Any non-breaking additions to the abi surface should be put in a new `extern {}` block
@@ -33,7 +36,7 @@ pub mod raw {
3336
name_len: usize,
3437
schema: *const u8,
3538
schema_len: usize,
36-
out: *mut u32,
39+
out: *mut TableId,
3740
) -> u16;
3841
*/
3942

@@ -47,7 +50,7 @@ pub mod raw {
4750
/// - the slice `(name, name_len)` is not valid UTF-8
4851
/// - `name + name_len` overflows a 64-bit address.
4952
/// - writing to `out` overflows a 32-bit integer
50-
pub fn _get_table_id(name: *const u8, name_len: usize, out: *mut u32) -> u16;
53+
pub fn _get_table_id(name: *const u8, name_len: usize, out: *mut TableId) -> u16;
5154

5255
/// Creates an index with the name `index_name` and type `index_type`,
5356
/// on a product of the given columns in `col_ids`
@@ -69,7 +72,7 @@ pub mod raw {
6972
pub fn _create_index(
7073
index_name: *const u8,
7174
index_name_len: usize,
72-
table_id: u32,
75+
table_id: TableId,
7376
index_type: u8,
7477
col_ids: *const u8,
7578
col_len: usize,
@@ -92,7 +95,13 @@ pub mod raw {
9295
/// - `(val, val_len)` cannot be decoded to an `AlgebraicValue`
9396
/// typed at the `AlgebraicType` of the column,
9497
/// - `val + val_len` overflows a 64-bit integer
95-
pub fn _iter_by_col_eq(table_id: u32, col_id: u32, val: *const u8, val_len: usize, out: *mut Buffer) -> u16;
98+
pub fn _iter_by_col_eq(
99+
table_id: TableId,
100+
col_id: ColId,
101+
val: *const u8,
102+
val_len: usize,
103+
out: *mut Buffer,
104+
) -> u16;
96105

97106
/// Inserts a row into the table identified by `table_id`,
98107
/// where the row is read from the byte slice `row` in WASM memory,
@@ -109,7 +118,7 @@ pub mod raw {
109118
/// - `row + row_len` overflows a 64-bit integer
110119
/// - `(row, row_len)` doesn't decode from BSATN to a `ProductValue`
111120
/// according to the `ProductType` that the table's schema specifies.
112-
pub fn _insert(table_id: u32, row: *mut u8, row_len: usize) -> u16;
121+
pub fn _insert(table_id: TableId, row: *mut u8, row_len: usize) -> u16;
113122

114123
/// Deletes all rows in the table identified by `table_id`
115124
/// where the column identified by `col_id` matches the byte string,
@@ -128,7 +137,13 @@ pub mod raw {
128137
/// according to the `AlgebraicType` that the table's schema specifies for `col_id`.
129138
/// - `value + value_len` overflows a 64-bit integer
130139
/// - writing to `out` would overflow a 32-bit integer
131-
pub fn _delete_by_col_eq(table_id: u32, col_id: u32, value: *const u8, value_len: usize, out: *mut u32) -> u16;
140+
pub fn _delete_by_col_eq(
141+
table_id: TableId,
142+
col_id: ColId,
143+
value: *const u8,
144+
value_len: usize,
145+
out: *mut u32,
146+
) -> u16;
132147

133148
/// Start iteration on each row, as bytes, of a table identified by `table_id`.
134149
///
@@ -137,7 +152,7 @@ pub mod raw {
137152
///
138153
/// Returns an error if
139154
/// - a table with the provided `table_id` doesn't exist
140-
pub fn _iter_start(table_id: u32, out: *mut BufferIter) -> u16;
155+
pub fn _iter_start(table_id: TableId, out: *mut BufferIter) -> u16;
141156

142157
/// Like [`_iter_start`], start iteration on each row,
143158
/// as bytes, of a table identified by `table_id`.
@@ -152,7 +167,12 @@ pub mod raw {
152167
/// - a table with the provided `table_id` doesn't exist
153168
/// - `(filter, filter_len)` doesn't decode to a filter expression
154169
/// - `filter + filter_len` overflows a 64-bit integer
155-
pub fn _iter_start_filtered(table_id: u32, filter: *const u8, filter_len: usize, out: *mut BufferIter) -> u16;
170+
pub fn _iter_start_filtered(
171+
table_id: TableId,
172+
filter: *const u8,
173+
filter_len: usize,
174+
out: *mut BufferIter,
175+
) -> u16;
156176

157177
/// Advances the registered iterator with the index given by `iter_key`.
158178
///
@@ -471,7 +491,7 @@ unsafe fn call<T>(f: impl FnOnce(*mut T) -> u16) -> Result<T, Errno> {
471491
///
472492
/// Returns an error if the table does not exist.
473493
#[inline]
474-
pub fn get_table_id(name: &str) -> Result<u32, Errno> {
494+
pub fn get_table_id(name: &str) -> Result<TableId, Errno> {
475495
unsafe { call(|out| raw::_get_table_id(name.as_ptr(), name.len(), out)) }
476496
}
477497

@@ -488,7 +508,7 @@ pub fn get_table_id(name: &str) -> Result<u32, Errno> {
488508
///
489509
/// Traps if `index_type == 1` or `col_ids.len() != 1`.
490510
#[inline]
491-
pub fn create_index(index_name: &str, table_id: u32, index_type: u8, col_ids: &[u8]) -> Result<(), Errno> {
511+
pub fn create_index(index_name: &str, table_id: TableId, index_type: u8, col_ids: &[u8]) -> Result<(), Errno> {
492512
cvt(unsafe {
493513
raw::_create_index(
494514
index_name.as_ptr(),
@@ -518,7 +538,7 @@ pub fn create_index(index_name: &str, table_id: u32, index_type: u8, col_ids: &[
518538
/// - `val` cannot be BSATN-decoded to an `AlgebraicValue`
519539
/// typed at the `AlgebraicType` of the column
520540
#[inline]
521-
pub fn iter_by_col_eq(table_id: u32, col_id: u32, val: &[u8]) -> Result<Buffer, Errno> {
541+
pub fn iter_by_col_eq(table_id: TableId, col_id: ColId, val: &[u8]) -> Result<Buffer, Errno> {
522542
unsafe { call(|out| raw::_iter_by_col_eq(table_id, col_id, val.as_ptr(), val.len(), out)) }
523543
}
524544

@@ -535,7 +555,7 @@ pub fn iter_by_col_eq(table_id: u32, col_id: u32, val: &[u8]) -> Result<Buffer,
535555
/// - `row` doesn't decode from BSATN to a `ProductValue`
536556
/// according to the `ProductType` that the table's schema specifies.
537557
#[inline]
538-
pub fn insert(table_id: u32, row: &mut [u8]) -> Result<(), Errno> {
558+
pub fn insert(table_id: TableId, row: &mut [u8]) -> Result<(), Errno> {
539559
cvt(unsafe { raw::_insert(table_id, row.as_mut_ptr(), row.len()) })
540560
}
541561

@@ -552,7 +572,7 @@ pub fn insert(table_id: u32, row: &mut [u8]) -> Result<(), Errno> {
552572
/// - no columns were deleted
553573
/// - `col_id` does not identify a column of the table
554574
#[inline]
555-
pub fn delete_by_col_eq(table_id: u32, col_id: u32, value: &[u8]) -> Result<u32, Errno> {
575+
pub fn delete_by_col_eq(table_id: TableId, col_id: ColId, value: &[u8]) -> Result<u32, Errno> {
556576
unsafe { call(|out| raw::_delete_by_col_eq(table_id, col_id, value.as_ptr(), value.len(), out)) }
557577
}
558578

@@ -568,7 +588,7 @@ pub fn delete_by_col_eq(table_id: u32, col_id: u32, value: &[u8]) -> Result<u32,
568588
/// - a table with the provided `table_id` doesn't exist
569589
/// - `Some(filter)` doesn't decode to a filter expression
570590
#[inline]
571-
pub fn iter(table_id: u32, filter: Option<&[u8]>) -> Result<BufferIter, Errno> {
591+
pub fn iter(table_id: TableId, filter: Option<&[u8]>) -> Result<BufferIter, Errno> {
572592
unsafe {
573593
call(|out| match filter {
574594
None => raw::_iter_start(table_id, out),

crates/bindings/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ getrandom = ["spacetimedb-bindings-sys/getrandom"]
1919
spacetimedb-bindings-sys = { path = "../bindings-sys", version = "0.7.1" }
2020
spacetimedb-lib = { path = "../lib", default-features = false, version = "0.7.1"}
2121
spacetimedb-bindings-macro = { path = "../bindings-macro", version = "0.7.1"}
22+
spacetimedb-primitives = { path = "../primitives", version = "0.7.1" }
2223

2324
derive_more.workspace = true
2425
log.workspace = true

0 commit comments

Comments
 (0)