Skip to content

Commit

Permalink
Refactoring IDs for system objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Oct 29, 2023
1 parent 78433c5 commit 7dba4dc
Show file tree
Hide file tree
Showing 67 changed files with 1,443 additions and 1,031 deletions.
7 changes: 6 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 13 additions & 12 deletions crates/bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,28 @@ bench = false
[dependencies]
spacetimedb-lib = { path = "../lib" }
spacetimedb-core = { path = "../core" }
spacetimedb-sats= { path = "../sats" }
spacetimedb-standalone = { path = "../standalone" }
spacetimedb-client-api = { path = "../client-api" }
spacetimedb-testing = { path = "../testing" }
spacetimedb-primitives = { path = "../primitives" }

ahash.workspace = true
log.workspace = true
clap.workspace = true
rusqlite.workspace = true
criterion.workspace = true
tempdir.workspace = true
rand.workspace = true
tokio.workspace = true
serde_json.workspace = true
serde.workspace = true
anyhow.workspace = true
anymap.workspace = true
byte-unit.workspace = true
clap.workspace = true
criterion.workspace = true
futures.workspace = true
tracing-subscriber.workspace = true
lazy_static.workspace = true
walkdir.workspace = true
regex.workspace = true
log.workspace = true
mimalloc.workspace = true
rand.workspace = true
regex.workspace = true
rusqlite.workspace = true
serde.workspace = true
serde_json.workspace = true
tempdir.workspace = true
tokio.workspace = true
tracing-subscriber = "0.3.17"
walkdir = "2.3.3"
2 changes: 1 addition & 1 deletion crates/bench/src/spacetime_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl BenchDatabase for SpacetimeModule {
}
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct TableId {
pascal_case: String,
snake_case: String,
Expand Down
8 changes: 4 additions & 4 deletions crates/bench/src/spacetime_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use crate::{
schemas::{table_name, BenchTable, IndexStrategy},
ResultBench,
};
use spacetimedb::db::datastore::traits::{IndexDef, TableDef};
use spacetimedb::db::relational_db::{open_db, RelationalDB};
use spacetimedb_lib::sats::AlgebraicValue;
use spacetimedb_primitives::{ColId, TableId};
use spacetimedb_sats::db::def::{IndexDef, TableDef};
use std::hint::black_box;
use tempdir::TempDir;

Expand Down Expand Up @@ -45,13 +45,14 @@ impl BenchDatabase for SpacetimeRaw {
match index_strategy {
IndexStrategy::Unique => {
self.db
.create_index(tx, IndexDef::new("id".to_string(), table_id, 0.into(), true))?;
.create_index(tx, table_id, IndexDef::new("id".to_string(), table_id, 0.into(), true))?;
}
IndexStrategy::NonUnique => (),
IndexStrategy::MultiIndex => {
for (i, column) in T::product_type().elements.iter().enumerate() {
self.db.create_index(
tx,
table_id,
IndexDef::new(column.name.clone().unwrap(), table_id, i.into(), false),
)?;
}
Expand Down Expand Up @@ -109,9 +110,8 @@ impl BenchDatabase for SpacetimeRaw {
column_index: u32,
value: AlgebraicValue,
) -> ResultBench<()> {
let col: ColId = column_index.into();
self.db.with_auto_commit(|tx| {
for row in self.db.iter_by_col_eq(tx, *table_id, col, value)? {
for row in self.db.iter_by_col_eq(tx, *table_id, ColId(column_index), value)? {
black_box(row);
}
Ok(())
Expand Down
52 changes: 17 additions & 35 deletions crates/bindings-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern crate proc_macro;
use std::collections::HashMap;
use std::time::Duration;

use bitflags::{bitflags, Flags};
use bitflags::Flags;
use module::{derive_deserialize, derive_satstype, derive_serialize};
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote, quote_spanned, TokenStreamExt};
Expand All @@ -24,6 +24,9 @@ use syn::{
parse_quote, BinOp, Expr, ExprBinary, ExprLit, ExprUnary, FnArg, Ident, ItemFn, ItemStruct, Member, Token, Type,
UnOp,
};
#[path = "../../sats/src/db/attr.rs"]
mod attr;
use attr::ColumnAttribute;

mod sym {
/// A symbol known at compile-time against
Expand Down Expand Up @@ -432,27 +435,7 @@ fn gen_reducer(original_function: ItemFn, reducer_name: &str, extra: ReducerExtr
struct Column<'a> {
index: u8,
field: &'a module::SatsField<'a>,
attr: ColumnIndexAttribute,
}

// TODO: any way to avoid duplication with same structure in bindings crate? Extra crate?
bitflags! {
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]
struct ColumnIndexAttribute: u8 {
const UNSET = Self::empty().bits();
/// Index no unique
const INDEXED = 0b0001;
/// Generate the next [Sequence]
const AUTO_INC = 0b0010;
/// Index unique
const UNIQUE = Self::INDEXED.bits() | 0b0100;
/// Unique + AutoInc
const IDENTITY = Self::UNIQUE.bits() | Self::AUTO_INC.bits();
/// Primary key column (implies Unique)
const PRIMARY_KEY = Self::UNIQUE.bits() | 0b1000;
/// PrimaryKey + AutoInc
const PRIMARY_KEY_AUTO = Self::PRIMARY_KEY.bits() | Self::AUTO_INC.bits();
}
attr: ColumnAttribute,
}

fn spacetimedb_table(item: TokenStream) -> syn::Result<TokenStream> {
Expand Down Expand Up @@ -546,27 +529,27 @@ fn spacetimedb_tabletype_impl(item: syn::DeriveInput) -> syn::Result<TokenStream
.try_into()
.map_err(|_| syn::Error::new_spanned(field.ident, "too many columns; the most a table can have is 256"))?;

let mut col_attr = ColumnIndexAttribute::UNSET;
let mut col_attr = ColumnAttribute::UNSET;
for attr in field.original_attrs {
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) => (ColumnIndexAttribute::UNIQUE, span),
ColumnAttr::Autoinc(span) => (ColumnIndexAttribute::AUTO_INC, span),
ColumnAttr::Primarykey(span) => (ColumnIndexAttribute::PRIMARY_KEY, span),
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(ColumnIndexAttribute::INDEXED)
.difference(ColumnAttribute::INDEXED)
.is_empty()
{
return Err(duplicate(span));
}
col_attr |= extra_col_attr;
}

if col_attr.contains(ColumnIndexAttribute::AUTO_INC) {
if col_attr.contains(ColumnAttribute::AUTO_INC) {
let valid_for_autoinc = if let syn::Type::Path(p) = field.ty {
// TODO: this is janky as heck
matches!(
Expand Down Expand Up @@ -613,14 +596,13 @@ fn spacetimedb_tabletype_impl(item: syn::DeriveInput) -> syn::Result<TokenStream
let name = name.as_deref().unwrap_or("default_index");
indexes.push(quote!(spacetimedb::IndexDef {
name: #name,
ty: spacetimedb::spacetimedb_lib::IndexType::#ty,
ty: spacetimedb::sats::db::def::IndexType::#ty,
col_ids: &[#(#col_ids),*],
}));
}

let (unique_columns, nonunique_columns): (Vec<_>, Vec<_>) = columns
.iter()
.partition(|x| x.attr.contains(ColumnIndexAttribute::UNIQUE));
let (unique_columns, nonunique_columns): (Vec<_>, Vec<_>) =
columns.iter().partition(|x| x.attr.contains(ColumnAttribute::UNIQUE));

let has_unique = !unique_columns.is_empty();

Expand Down Expand Up @@ -715,7 +697,7 @@ fn spacetimedb_tabletype_impl(item: syn::DeriveInput) -> syn::Result<TokenStream
let schema_impl = derive_satstype(&sats_ty, false);
let column_attrs = columns.iter().map(|col| {
Ident::new(
ColumnIndexAttribute::FLAGS
ColumnAttribute::FLAGS
.iter()
.find_map(|f| (col.attr == *f.value()).then_some(f.name()))
.expect("Invalid column attribute"),
Expand All @@ -725,8 +707,8 @@ fn spacetimedb_tabletype_impl(item: syn::DeriveInput) -> syn::Result<TokenStream
let tabletype_impl = quote! {
impl spacetimedb::TableType for #original_struct_ident {
const TABLE_NAME: &'static str = #table_name;
const COLUMN_ATTRS: &'static [spacetimedb::spacetimedb_lib::ColumnIndexAttribute] = &[
#(spacetimedb::spacetimedb_lib::ColumnIndexAttribute::#column_attrs),*
const COLUMN_ATTRS: &'static [spacetimedb::sats::db::attr::ColumnAttribute] = &[
#(spacetimedb::sats::db::attr::ColumnAttribute::#column_attrs),*
];
const INDEXES: &'static [spacetimedb::IndexDef<'static>] = &[#(#indexes),*];
type InsertResult = #insert_result;
Expand Down
1 change: 1 addition & 0 deletions crates/bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ spacetimedb-primitives = { path = "../primitives", version = "0.7.1" }

derive_more.workspace = true
log.workspace = true
nonempty.workspace = true
once_cell.workspace = true
scoped-tls.workspace = true

Expand Down
4 changes: 3 additions & 1 deletion crates/bindings/src/impls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use spacetimedb_lib::{Address, DataKey, Hash, Identity};
use spacetimedb_lib::{Address, Identity};

use super::PrimaryKey;
use crate::sats::data_key::DataKey;
use crate::sats::hash::Hash;
use crate::{FilterableValue, UniqueValue};

macro_rules! impl_primitives {
Expand Down
6 changes: 4 additions & 2 deletions crates/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use spacetimedb_lib::buffer::{BufReader, BufWriter, Cursor, DecodeError};
pub use spacetimedb_lib::de::{Deserialize, DeserializeOwned};
use spacetimedb_lib::sats::{impl_deserialize, impl_serialize, impl_st};
pub use spacetimedb_lib::ser::Serialize;
use spacetimedb_lib::{bsatn, ColumnIndexAttribute, IndexType, PrimaryKey, ProductType, ProductValue};
use spacetimedb_lib::{bsatn, PrimaryKey, ProductType, ProductValue};
use std::cell::RefCell;
use std::marker::PhantomData;
use std::{fmt, panic};
Expand All @@ -34,6 +34,8 @@ pub use spacetimedb_bindings_sys as sys;
pub use sys::Errno;
use sys::{Buffer, BufferIter};

use crate::sats::db::attr::ColumnAttribute;
use crate::sats::db::def::IndexType;
pub use log;

pub type Result<T = (), E = Errno> = core::result::Result<T, E>;
Expand Down Expand Up @@ -318,7 +320,7 @@ impl<T: TableType> Iterator for TableIter<T> {
/// 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 COLUMN_ATTRS: &'static [ColumnIndexAttribute];
const COLUMN_ATTRS: &'static [ColumnAttribute];
const INDEXES: &'static [IndexDef<'static>];
type InsertResult: sealed::InsertResult<T = Self>;

Expand Down
26 changes: 14 additions & 12 deletions crates/bindings/src/rt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![deny(unsafe_op_in_unsafe_fn)]

use nonempty::NonEmpty;
use std::any::TypeId;
use std::collections::{btree_map, BTreeMap};
use std::fmt;
Expand All @@ -9,15 +10,16 @@ use std::time::Duration;

use crate::timestamp::with_timestamp_set;
use crate::{sys, ReducerContext, ScheduleToken, SpacetimeType, TableType, Timestamp};
use spacetimedb_lib::auth::{StAccess, StTableType};
use spacetimedb_lib::de::{self, Deserialize, SeqProductAccess};
use spacetimedb_lib::sats::db::auth::{StAccess, StTableType};
use spacetimedb_lib::sats::typespace::TypespaceBuilder;
use spacetimedb_lib::sats::{impl_deserialize, impl_serialize, AlgebraicType, AlgebraicTypeRef, ProductTypeElement};
use spacetimedb_lib::ser::{Serialize, SerializeSeqProduct};
use spacetimedb_lib::{bsatn, Address, Identity, MiscModuleExport, ModuleDef, ReducerDef, TableDef, TypeAlias};
use spacetimedb_primitives::TableId;
use sys::Buffer;

use crate::sats::db::def::IndexDef;
pub use once_cell::sync::{Lazy, OnceCell};

/// The `sender` invokes `reducer` at `timestamp` and provides it with the given `args`.
Expand Down Expand Up @@ -401,28 +403,28 @@ pub fn register_reftype<T: SpacetimeType>() {
pub fn register_table<T: TableType>() {
register_describer(|module| {
let data = *T::make_type(module).as_ref().unwrap();

let indexes = T::COLUMN_ATTRS.iter().zip(T::INDEXES.iter()).map(|(attr, idx)| {
IndexDef::new_cols(
idx.name.into(),
TableId(0),
NonEmpty::from_slice(idx.col_ids).unwrap_or_default().map(Into::into),
attr.has_unique(),
)
});

let schema = TableDef {
name: T::TABLE_NAME.into(),
data,
column_attrs: T::COLUMN_ATTRS.to_owned(),
indexes: T::INDEXES.iter().copied().map(Into::into).collect(),
indexes: indexes.collect(),
table_type: StTableType::User,
table_access: StAccess::for_name(T::TABLE_NAME),
};
module.module.tables.push(schema)
})
}

impl From<crate::IndexDef<'_>> for spacetimedb_lib::IndexDef {
fn from(index: crate::IndexDef<'_>) -> spacetimedb_lib::IndexDef {
spacetimedb_lib::IndexDef {
name: index.name.to_owned(),
index_type: index.ty,
cols: index.col_ids.to_owned(),
}
}
}

/// 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>) {
register_describer(|module| {
Expand Down
1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ is-terminal.workspace = true
itertools.workspace = true
jsonwebtoken.workspace = true
mimalloc.workspace = true
nonempty.workspace = true
reqwest.workspace = true
rustyline.workspace = true
serde = { workspace = true, features = ["derive"] }
Expand Down
Loading

0 comments on commit 7dba4dc

Please sign in to comment.