Skip to content

Commit

Permalink
feat(1329): System table for system variables
Browse files Browse the repository at this point in the history
Closes #1329.

Used to persist global parameters for slow query logging and cardinality limits.
  • Loading branch information
joshua-spacetime committed Jun 6, 2024
1 parent 8f3f6bd commit 6e89743
Show file tree
Hide file tree
Showing 8 changed files with 481 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/core/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::util::slow::SlowQueryConfig;
use spacetimedb_sats::relation::{Column, FieldName, Header};
use spacetimedb_lib::relation::{Column, FieldName, Header};
use spacetimedb_sats::{product, AlgebraicType, AlgebraicValue};
use spacetimedb_vm::errors::{ConfigError, ErrorVm};
use spacetimedb_vm::relation::MemTable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
SystemTable, ST_CLIENTS_ID, ST_CLIENT_IDX, ST_COLUMNS_ID, ST_COLUMNS_IDX, ST_COLUMNS_NAME,
ST_CONSTRAINTS_ID, ST_CONSTRAINTS_IDX, ST_CONSTRAINTS_NAME, ST_INDEXES_ID, ST_INDEXES_IDX,
ST_INDEXES_NAME, ST_MODULE_ID, ST_MODULE_IDX, ST_RESERVED_SEQUENCE_RANGE, ST_SEQUENCES_ID,
ST_SEQUENCES_IDX, ST_SEQUENCES_NAME, ST_TABLES_ID, ST_TABLES_IDX,
ST_SEQUENCES_IDX, ST_SEQUENCES_NAME, ST_TABLES_ID, ST_TABLES_IDX, ST_VAR_ID, ST_VAR_IDX,
},
traits::TxData,
},
Expand Down Expand Up @@ -204,6 +204,8 @@ impl CommittedState {

self.create_table(ST_CLIENTS_ID, schemas[ST_CLIENT_IDX].clone());

self.create_table(ST_VAR_ID, schemas[ST_VAR_IDX].clone());

// Insert the sequences into `st_sequences`
let (st_sequences, blob_store) =
self.get_table_and_blob_store_or_create(ST_SEQUENCES_ID, &schemas[ST_SEQUENCES_IDX]);
Expand Down
11 changes: 10 additions & 1 deletion crates/core/src/db/datastore/locking_tx_datastore/datastore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ impl<F: FnMut(u64)> spacetimedb_commitlog::payload::txdata::Visitor for ReplayVi
mod tests {
use super::*;
use crate::db::datastore::system_tables::{
system_tables, StColumnRow, StConstraintRow, StIndexRow, StSequenceRow, StTableRow, ST_COLUMNS_ID,
system_tables, StColumnRow, StConstraintRow, StIndexRow, StSequenceRow, StTableRow, StVarValue, ST_COLUMNS_ID,
ST_CONSTRAINTS_ID, ST_INDEXES_ID, ST_RESERVED_SEQUENCE_RANGE, ST_SEQUENCES_ID,
};
use crate::db::datastore::traits::{IsolationLevel, MutTx};
Expand Down Expand Up @@ -1240,6 +1240,7 @@ mod tests {
TableRow { id: 4, name: "st_constraints", ty: StTableType::System, access: StAccess::Public },
TableRow { id: 5, name: "st_module", ty: StTableType::System, access: StAccess::Public },
TableRow { id: 6, name: "st_clients", ty: StTableType::System, access: StAccess::Public },
TableRow { id: 7, name: "st_var", ty: StTableType::System, access: StAccess::Public },
]));
#[rustfmt::skip]
assert_eq!(query.scan_st_columns()?, map_array([
Expand Down Expand Up @@ -1282,6 +1283,9 @@ mod tests {

ColRow { table: 6, pos: 0, name: "identity", ty: Identity::get_type() },
ColRow { table: 6, pos: 1, name: "address", ty: Address::get_type() },

ColRow { table: 7, pos: 0, name: "name", ty: AlgebraicType::String },
ColRow { table: 7, pos: 1, name: "value", ty: StVarValue::type_of() },
]));
#[rustfmt::skip]
assert_eq!(query.scan_st_indexes()?, map_array([
Expand All @@ -1292,6 +1296,7 @@ mod tests {
IndexRow { id: 4, table: 3, col: col(0), name: "idx_st_indexes_index_id_primary_key_auto_unique", unique: true },
IndexRow { id: 5, table: 4, col: col(0), name: "idx_st_constraints_constraint_id_primary_key_auto_unique", unique: true },
IndexRow { id: 6, table: 6, col: col_list![0, 1], name: "idx_st_clients_identity_address_unique", unique: true },
IndexRow { id: 7, table: 7, col: col(0), name: "idx_st_var_name_primary_key_unique", unique: true },
]));
let start = FIRST_NON_SYSTEM_ID as i128;
#[rustfmt::skip]
Expand All @@ -1316,6 +1321,7 @@ mod tests {
ConstraintRow { constraint_id: 4, table_id: 3, columns: col(0), constraints: Constraints::primary_key_auto(), constraint_name: "ct_st_indexes_index_id_primary_key_auto" },
ConstraintRow { constraint_id: 5, table_id: 4, columns: col(0), constraints: Constraints::primary_key_auto(), constraint_name: "ct_st_constraints_constraint_id_primary_key_auto" },
ConstraintRow { constraint_id: 6, table_id: 6, columns: col_list![0, 1], constraints: Constraints::unique(), constraint_name: "ct_st_clients_identity_address_unique" },
ConstraintRow { constraint_id: 7, table_id: 7, columns: col(0), constraints: Constraints::primary_key(), constraint_name: "ct_st_var_name_primary_key" },
]));

// Verify we get back the tables correctly with the proper ids...
Expand Down Expand Up @@ -1697,6 +1703,7 @@ mod tests {
IndexRow { id: 4, table: 3, col: col(0), name: "idx_st_indexes_index_id_primary_key_auto_unique", unique: true },
IndexRow { id: 5, table: 4, col: col(0), name: "idx_st_constraints_constraint_id_primary_key_auto_unique", unique: true },
IndexRow { id: 6, table: 6, col: col_list![0, 1], name: "idx_st_clients_identity_address_unique", unique: true },
IndexRow { id: 7, table: 7, col: col(0), name: "idx_st_var_name_primary_key_unique", unique: true },
IndexRow { id: seq_start, table: FIRST_NON_SYSTEM_ID, col: col(0), name: "id_idx", unique: true },
IndexRow { id: seq_start + 1, table: FIRST_NON_SYSTEM_ID, col: col(1), name: "name_idx", unique: true },
IndexRow { id: seq_start + 2, table: FIRST_NON_SYSTEM_ID, col: col(2), name: "age_idx", unique: true },
Expand Down Expand Up @@ -1742,6 +1749,7 @@ mod tests {
IndexRow { id: 4, table: 3, col: col(0), name: "idx_st_indexes_index_id_primary_key_auto_unique", unique: true },
IndexRow { id: 5, table: 4, col: col(0), name: "idx_st_constraints_constraint_id_primary_key_auto_unique", unique: true },
IndexRow { id: 6, table: 6, col: col_list![0, 1], name: "idx_st_clients_identity_address_unique", unique: true },
IndexRow { id: 7, table: 7, col: col(0), name: "idx_st_var_name_primary_key_unique", unique: true },
IndexRow { id: seq_start , table: FIRST_NON_SYSTEM_ID, col: col(0), name: "id_idx", unique: true },
IndexRow { id: seq_start + 1, table: FIRST_NON_SYSTEM_ID, col: col(1), name: "name_idx", unique: true },
IndexRow { id: seq_start + 2, table: FIRST_NON_SYSTEM_ID, col: col(2), name: "age_idx", unique: true },
Expand Down Expand Up @@ -1787,6 +1795,7 @@ mod tests {
IndexRow { id: 4, table: 3, col: col(0), name: "idx_st_indexes_index_id_primary_key_auto_unique", unique: true },
IndexRow { id: 5, table: 4, col: col(0), name: "idx_st_constraints_constraint_id_primary_key_auto_unique", unique: true },
IndexRow { id: 6, table: 6, col: col_list![0, 1], name: "idx_st_clients_identity_address_unique", unique: true },
IndexRow { id: 7, table: 7, col: col(0), name: "idx_st_var_name_primary_key_unique", unique: true },
IndexRow { id: seq_start, table: FIRST_NON_SYSTEM_ID, col: col(0), name: "id_idx", unique: true },
IndexRow { id: seq_start + 1, table: FIRST_NON_SYSTEM_ID, col: col(1), name: "name_idx", unique: true },
].map(Into::into));
Expand Down
Loading

0 comments on commit 6e89743

Please sign in to comment.