Skip to content

Commit b2ec10d

Browse files
committed
Remove StVarTable in furtherance of datastore extraction
1 parent 917fd66 commit b2ec10d

4 files changed

Lines changed: 97 additions & 103 deletions

File tree

crates/core/src/db/datastore/system_tables.rs

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//! - Use [`st_fields_enum`] to define its column enum.
1212
//! - Register its schema in [`system_module_def`], making sure to call `validate_system_table` at the end of the function.
1313
14-
use crate::db::relational_db::RelationalDB;
1514
use crate::error::DBError;
1615
use spacetimedb_lib::db::auth::{StAccess, StTableType};
1716
use spacetimedb_lib::db::raw_def::v9::{btree, RawSql};
@@ -21,7 +20,6 @@ use spacetimedb_lib::ser::Serialize;
2120
use spacetimedb_lib::st_var::StVarValue;
2221
use spacetimedb_lib::{ConnectionId, Identity, ProductValue, SpacetimeType};
2322
use spacetimedb_primitives::*;
24-
use spacetimedb_sats::algebraic_type::fmt::fmt_algebraic_type;
2523
use spacetimedb_sats::algebraic_value::ser::value_serialize;
2624
use spacetimedb_sats::hash::Hash;
2725
use spacetimedb_sats::product_value::InvalidFieldError;
@@ -34,16 +32,11 @@ use spacetimedb_schema::schema::{
3432
TableSchema,
3533
};
3634
use spacetimedb_table::table::RowRef;
37-
use spacetimedb_vm::errors::{ErrorType, ErrorVm};
38-
use spacetimedb_vm::ops::parse;
3935
use std::cell::RefCell;
4036
use std::str::FromStr;
4137
use strum::Display;
4238
use v9::{RawModuleDefV9Builder, TableType};
4339

44-
use super::locking_tx_datastore::tx::TxId;
45-
use super::locking_tx_datastore::MutTxId;
46-
4740
/// The static ID of the table that defines tables
4841
pub(crate) const ST_TABLE_ID: TableId = TableId(1);
4942
/// The static ID of the table that defines columns
@@ -950,81 +943,6 @@ impl TryFrom<RowRef<'_>> for StClientRow {
950943
}
951944
}
952945

953-
/// A handle for reading system variables from `st_var`
954-
pub struct StVarTable;
955-
956-
impl StVarTable {
957-
/// Read the value of [ST_VARNAME_ROW_LIMIT] from `st_var`
958-
pub fn row_limit(db: &RelationalDB, tx: &TxId) -> Result<Option<u64>, DBError> {
959-
let data = Self::read_var(db, tx, StVarName::RowLimit);
960-
961-
if let Some(StVarValue::U64(limit)) = data? {
962-
return Ok(Some(limit));
963-
}
964-
Ok(None)
965-
}
966-
967-
/// Read the value of [ST_VARNAME_SLOW_QRY] from `st_var`
968-
pub fn query_limit(db: &RelationalDB, tx: &TxId) -> Result<Option<u64>, DBError> {
969-
if let Some(StVarValue::U64(ms)) = Self::read_var(db, tx, StVarName::SlowQryThreshold)? {
970-
return Ok(Some(ms));
971-
}
972-
Ok(None)
973-
}
974-
975-
/// Read the value of [ST_VARNAME_SLOW_SUB] from `st_var`
976-
pub fn sub_limit(db: &RelationalDB, tx: &TxId) -> Result<Option<u64>, DBError> {
977-
if let Some(StVarValue::U64(ms)) = Self::read_var(db, tx, StVarName::SlowSubThreshold)? {
978-
return Ok(Some(ms));
979-
}
980-
Ok(None)
981-
}
982-
983-
/// Read the value of [ST_VARNAME_SLOW_INC] from `st_var`
984-
pub fn incr_limit(db: &RelationalDB, tx: &TxId) -> Result<Option<u64>, DBError> {
985-
if let Some(StVarValue::U64(ms)) = Self::read_var(db, tx, StVarName::SlowIncThreshold)? {
986-
return Ok(Some(ms));
987-
}
988-
Ok(None)
989-
}
990-
991-
/// Read the value of a system variable from `st_var`
992-
pub fn read_var(db: &RelationalDB, tx: &TxId, name: StVarName) -> Result<Option<StVarValue>, DBError> {
993-
if let Some(row_ref) = db
994-
.iter_by_col_eq(tx, ST_VAR_ID, StVarFields::Name.col_id(), &name.into())?
995-
.next()
996-
{
997-
return Ok(Some(StVarRow::try_from(row_ref)?.value));
998-
}
999-
Ok(None)
1000-
}
1001-
1002-
/// Update the value of a system variable in `st_var`
1003-
pub fn write_var(db: &RelationalDB, tx: &mut MutTxId, name: StVarName, literal: &str) -> Result<(), DBError> {
1004-
let value = Self::parse_var(name, literal)?;
1005-
if let Some(row_ref) = db
1006-
.iter_by_col_eq_mut(tx, ST_VAR_ID, StVarFields::Name.col_id(), &name.into())?
1007-
.next()
1008-
{
1009-
db.delete(tx, ST_VAR_ID, [row_ref.pointer()]);
1010-
}
1011-
tx.insert_via_serialize_bsatn(ST_VAR_ID, &StVarRow { name, value })?;
1012-
Ok(())
1013-
}
1014-
1015-
/// Parse the literal representation of a system variable
1016-
fn parse_var(name: StVarName, literal: &str) -> Result<StVarValue, DBError> {
1017-
StVarValue::try_from_primitive(parse::parse(literal, &name.type_of())?).map_err(|v| {
1018-
ErrorVm::Type(ErrorType::Parse {
1019-
value: literal.to_string(),
1020-
ty: fmt_algebraic_type(&name.type_of()).to_string(),
1021-
err: format!("error parsing value: {:?}", v),
1022-
})
1023-
.into()
1024-
})
1025-
}
1026-
}
1027-
1028946
/// System table [ST_VAR_NAME]
1029947
///
1030948
/// | name | value |
@@ -1210,19 +1128,6 @@ fn to_product_value<T: Serialize>(value: &T) -> ProductValue {
12101128
#[cfg(test)]
12111129
mod tests {
12121130
use super::*;
1213-
use crate::db::relational_db::tests_utils::{with_auto_commit, with_read_only, TestDB};
1214-
1215-
#[test]
1216-
fn test_system_variables() {
1217-
let db = TestDB::durable().expect("failed to create db");
1218-
let _ = with_auto_commit(&db, |tx| StVarTable::write_var(&db, tx, StVarName::RowLimit, "5"));
1219-
assert_eq!(
1220-
5,
1221-
with_read_only(&db, |tx| StVarTable::row_limit(&db, tx))
1222-
.expect("failed to read from st_var")
1223-
.expect("row_limit does not exist")
1224-
);
1225-
}
12261131

12271132
#[test]
12281133
fn test_sequences_within_reserved_range() {

crates/core/src/db/relational_db.rs

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::datastore::locking_tx_datastore::datastore::TxMetrics;
33
use super::datastore::locking_tx_datastore::state_view::{
44
IterByColEqMutTx, IterByColRangeMutTx, IterMutTx, IterTx, StateView,
55
};
6-
use super::datastore::system_tables::ST_MODULE_ID;
6+
use super::datastore::system_tables::{StFields, StVarFields, StVarName, StVarRow, ST_MODULE_ID, ST_VAR_ID};
77
use super::datastore::traits::{
88
InsertFlags, IsolationLevel, Metadata, MutTx as _, MutTxDatastore, Program, RowTypeForTable, Tx as _, TxDatastore,
99
UpdateFlags,
@@ -31,10 +31,12 @@ use spacetimedb_commitlog as commitlog;
3131
use spacetimedb_durability::{self as durability, TxOffset};
3232
use spacetimedb_lib::db::auth::StAccess;
3333
use spacetimedb_lib::db::raw_def::v9::{btree, RawModuleDefV9Builder, RawSql};
34+
use spacetimedb_lib::st_var::StVarValue;
3435
use spacetimedb_lib::ConnectionId;
3536
use spacetimedb_lib::Identity;
3637
use spacetimedb_paths::server::{CommitLogDir, ReplicaDir, SnapshotsPath};
3738
use spacetimedb_primitives::*;
39+
use spacetimedb_sats::algebraic_type::fmt::fmt_algebraic_type;
3840
use spacetimedb_sats::{AlgebraicType, AlgebraicValue, ProductType, ProductValue};
3941
use spacetimedb_schema::def::{ModuleDef, TableDef};
4042
use spacetimedb_schema::schema::{IndexSchema, RowLevelSecuritySchema, Schema, SequenceSchema, TableSchema};
@@ -43,6 +45,8 @@ use spacetimedb_table::indexes::RowPointer;
4345
use spacetimedb_table::page_pool::PagePool;
4446
use spacetimedb_table::table::RowRef;
4547
use spacetimedb_table::MemoryUsage;
48+
use spacetimedb_vm::errors::{ErrorType, ErrorVm};
49+
use spacetimedb_vm::ops::parse;
4650
use std::borrow::Cow;
4751
use std::collections::HashSet;
4852
use std::fmt;
@@ -1381,6 +1385,78 @@ impl RelationalDB {
13811385
pub fn report(&self, reducer: &str, metrics: &TxMetrics, tx_data: Option<&TxData>) {
13821386
metrics.report(tx_data, reducer, |wl: WorkloadType| self.exec_counters_for(wl));
13831387
}
1388+
1389+
/// Read the value of [ST_VARNAME_ROW_LIMIT] from `st_var`
1390+
pub(crate) fn row_limit(&self, tx: &Tx) -> Result<Option<u64>, DBError> {
1391+
let data = self.read_var(tx, StVarName::RowLimit);
1392+
1393+
if let Some(StVarValue::U64(limit)) = data? {
1394+
return Ok(Some(limit));
1395+
}
1396+
Ok(None)
1397+
}
1398+
1399+
/// Read the value of [ST_VARNAME_SLOW_QRY] from `st_var`
1400+
pub(crate) fn query_limit(&self, tx: &Tx) -> Result<Option<u64>, DBError> {
1401+
if let Some(StVarValue::U64(ms)) = self.read_var(tx, StVarName::SlowQryThreshold)? {
1402+
return Ok(Some(ms));
1403+
}
1404+
Ok(None)
1405+
}
1406+
1407+
/// Read the value of [ST_VARNAME_SLOW_SUB] from `st_var`
1408+
#[allow(dead_code)]
1409+
pub(crate) fn sub_limit(&self, tx: &Tx) -> Result<Option<u64>, DBError> {
1410+
if let Some(StVarValue::U64(ms)) = self.read_var(tx, StVarName::SlowSubThreshold)? {
1411+
return Ok(Some(ms));
1412+
}
1413+
Ok(None)
1414+
}
1415+
1416+
/// Read the value of [ST_VARNAME_SLOW_INC] from `st_var`
1417+
#[allow(dead_code)]
1418+
pub(crate) fn incr_limit(&self, tx: &Tx) -> Result<Option<u64>, DBError> {
1419+
if let Some(StVarValue::U64(ms)) = self.read_var(tx, StVarName::SlowIncThreshold)? {
1420+
return Ok(Some(ms));
1421+
}
1422+
Ok(None)
1423+
}
1424+
1425+
/// Read the value of a system variable from `st_var`
1426+
pub(crate) fn read_var(&self, tx: &Tx, name: StVarName) -> Result<Option<StVarValue>, DBError> {
1427+
if let Some(row_ref) = self
1428+
.iter_by_col_eq(tx, ST_VAR_ID, StVarFields::Name.col_id(), &name.into())?
1429+
.next()
1430+
{
1431+
return Ok(Some(StVarRow::try_from(row_ref)?.value));
1432+
}
1433+
Ok(None)
1434+
}
1435+
1436+
/// Update the value of a system variable in `st_var`
1437+
pub(crate) fn write_var(&self, tx: &mut MutTx, name: StVarName, literal: &str) -> Result<(), DBError> {
1438+
let value = Self::parse_var(name, literal)?;
1439+
if let Some(row_ref) = self
1440+
.iter_by_col_eq_mut(tx, ST_VAR_ID, StVarFields::Name.col_id(), &name.into())?
1441+
.next()
1442+
{
1443+
self.delete(tx, ST_VAR_ID, [row_ref.pointer()]);
1444+
}
1445+
tx.insert_via_serialize_bsatn(ST_VAR_ID, &StVarRow { name, value })?;
1446+
Ok(())
1447+
}
1448+
1449+
/// Parse the literal representation of a system variable
1450+
fn parse_var(name: StVarName, literal: &str) -> Result<StVarValue, DBError> {
1451+
StVarValue::try_from_primitive(parse::parse(literal, &name.type_of())?).map_err(|v| {
1452+
ErrorVm::Type(ErrorType::Parse {
1453+
value: literal.to_string(),
1454+
ty: fmt_algebraic_type(&name.type_of()).to_string(),
1455+
err: format!("error parsing value: {:?}", v),
1456+
})
1457+
.into()
1458+
})
1459+
}
13841460
}
13851461

13861462
#[allow(unused)]
@@ -1941,7 +2017,9 @@ mod tests {
19412017
system_tables, StConstraintRow, StIndexRow, StSequenceRow, StTableRow, ST_CONSTRAINT_ID, ST_INDEX_ID,
19422018
ST_SEQUENCE_ID, ST_TABLE_ID,
19432019
};
1944-
use crate::db::relational_db::tests_utils::{begin_tx, insert, make_snapshot, TestDB};
2020+
use crate::db::relational_db::tests_utils::{
2021+
begin_tx, insert, make_snapshot, with_auto_commit, with_read_only, TestDB,
2022+
};
19452023
use crate::error::IndexError;
19462024
use crate::execution_context::ReducerContext;
19472025
use anyhow::bail;
@@ -2025,6 +2103,18 @@ mod tests {
20252103
Ok(())
20262104
}
20272105

2106+
#[test]
2107+
fn test_system_variables() {
2108+
let db = TestDB::durable().expect("failed to create db");
2109+
let _ = with_auto_commit(&db, |tx| db.write_var(tx, StVarName::RowLimit, "5"));
2110+
assert_eq!(
2111+
5,
2112+
with_read_only(&db, |tx| db.row_limit(tx))
2113+
.expect("failed to read from st_var")
2114+
.expect("row_limit does not exist")
2115+
);
2116+
}
2117+
20282118
#[test]
20292119
fn test_open_twice() -> ResultTest<()> {
20302120
let stdb = TestDB::durable()?;

crates/core/src/sql/execute.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::time::Duration;
22

33
use super::ast::SchemaViewer;
44
use crate::db::datastore::locking_tx_datastore::state_view::StateView;
5-
use crate::db::datastore::system_tables::StVarTable;
65
use crate::db::datastore::traits::IsolationLevel;
76
use crate::db::relational_db::{RelationalDB, Tx};
87
use crate::energy::EnergyQuanta;
@@ -72,7 +71,7 @@ fn execute(
7271
updates: &mut Vec<DatabaseTableUpdate>,
7372
) -> Result<Vec<MemTable>, DBError> {
7473
let slow_query_threshold = if let TxMode::Tx(tx) = p.tx {
75-
StVarTable::query_limit(p.db, tx)?.map(Duration::from_millis)
74+
p.db.query_limit(tx)?.map(Duration::from_millis)
7675
} else {
7776
None
7877
};

crates/core/src/vm.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::db::datastore::locking_tx_datastore::state_view::IterByColRangeMutTx;
44
use crate::db::datastore::locking_tx_datastore::tx::TxId;
55
use crate::db::datastore::locking_tx_datastore::IterByColRangeTx;
6-
use crate::db::datastore::system_tables::{st_var_schema, StVarName, StVarRow, StVarTable};
6+
use crate::db::datastore::system_tables::{st_var_schema, StVarName, StVarRow};
77
use crate::db::relational_db::{MutTx, RelationalDB, Tx};
88
use crate::error::DBError;
99
use crate::estimation;
@@ -467,7 +467,7 @@ pub fn check_row_limit<Query>(
467467
auth: &AuthCtx,
468468
) -> Result<(), DBError> {
469469
if auth.caller != auth.owner {
470-
if let Some(limit) = StVarTable::row_limit(db, tx)? {
470+
if let Some(limit) = db.row_limit(tx)? {
471471
let mut estimate: u64 = 0;
472472
for query in queries {
473473
estimate = estimate.saturating_add(row_est(query, tx));
@@ -603,15 +603,15 @@ impl<'db, 'tx> DbProgram<'db, 'tx> {
603603

604604
fn _set_var(&mut self, name: String, literal: String) -> Result<Code, ErrorVm> {
605605
let tx = self.tx.unwrap_mut();
606-
StVarTable::write_var(self.db, tx, StVarName::from_str(&name)?, &literal)?;
606+
self.db.write_var(tx, StVarName::from_str(&name)?, &literal)?;
607607
Ok(Code::Pass(None))
608608
}
609609

610610
fn _read_var(&self, name: String) -> Result<Code, ErrorVm> {
611611
fn read_key_into_table(env: &DbProgram, name: &str) -> Result<MemTable, ErrorVm> {
612612
if let TxMode::Tx(tx) = &env.tx {
613613
let name = StVarName::from_str(name)?;
614-
if let Some(value) = StVarTable::read_var(env.db, tx, name)? {
614+
if let Some(value) = env.db.read_var(tx, name)? {
615615
return Ok(MemTable::from_iter(
616616
Arc::new(st_var_schema().into()),
617617
[ProductValue::from(StVarRow { name, value })],

0 commit comments

Comments
 (0)