Skip to content

Commit f850bb3

Browse files
committed
Remove StVarTable in furtherance of datastore extraction
1 parent 14c570d commit f850bb3

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::{report_tx_metricses, TxM
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;
@@ -1365,6 +1369,78 @@ impl RelationalDB {
13651369
pub fn report(&self, reducer: &str, metrics: &TxMetrics, tx_data: Option<&TxData>) {
13661370
metrics.report(tx_data, reducer, |wl: WorkloadType| self.exec_counters_for(wl));
13671371
}
1372+
1373+
/// Read the value of [ST_VARNAME_ROW_LIMIT] from `st_var`
1374+
pub(crate) fn row_limit(&self, tx: &Tx) -> Result<Option<u64>, DBError> {
1375+
let data = self.read_var(tx, StVarName::RowLimit);
1376+
1377+
if let Some(StVarValue::U64(limit)) = data? {
1378+
return Ok(Some(limit));
1379+
}
1380+
Ok(None)
1381+
}
1382+
1383+
/// Read the value of [ST_VARNAME_SLOW_QRY] from `st_var`
1384+
pub(crate) fn query_limit(&self, tx: &Tx) -> Result<Option<u64>, DBError> {
1385+
if let Some(StVarValue::U64(ms)) = self.read_var(tx, StVarName::SlowQryThreshold)? {
1386+
return Ok(Some(ms));
1387+
}
1388+
Ok(None)
1389+
}
1390+
1391+
/// Read the value of [ST_VARNAME_SLOW_SUB] from `st_var`
1392+
#[allow(dead_code)]
1393+
pub(crate) fn sub_limit(&self, tx: &Tx) -> Result<Option<u64>, DBError> {
1394+
if let Some(StVarValue::U64(ms)) = self.read_var(tx, StVarName::SlowSubThreshold)? {
1395+
return Ok(Some(ms));
1396+
}
1397+
Ok(None)
1398+
}
1399+
1400+
/// Read the value of [ST_VARNAME_SLOW_INC] from `st_var`
1401+
#[allow(dead_code)]
1402+
pub(crate) fn incr_limit(&self, tx: &Tx) -> Result<Option<u64>, DBError> {
1403+
if let Some(StVarValue::U64(ms)) = self.read_var(tx, StVarName::SlowIncThreshold)? {
1404+
return Ok(Some(ms));
1405+
}
1406+
Ok(None)
1407+
}
1408+
1409+
/// Read the value of a system variable from `st_var`
1410+
pub(crate) fn read_var(&self, tx: &Tx, name: StVarName) -> Result<Option<StVarValue>, DBError> {
1411+
if let Some(row_ref) = self
1412+
.iter_by_col_eq(tx, ST_VAR_ID, StVarFields::Name.col_id(), &name.into())?
1413+
.next()
1414+
{
1415+
return Ok(Some(StVarRow::try_from(row_ref)?.value));
1416+
}
1417+
Ok(None)
1418+
}
1419+
1420+
/// Update the value of a system variable in `st_var`
1421+
pub(crate) fn write_var(&self, tx: &mut MutTx, name: StVarName, literal: &str) -> Result<(), DBError> {
1422+
let value = Self::parse_var(name, literal)?;
1423+
if let Some(row_ref) = self
1424+
.iter_by_col_eq_mut(tx, ST_VAR_ID, StVarFields::Name.col_id(), &name.into())?
1425+
.next()
1426+
{
1427+
self.delete(tx, ST_VAR_ID, [row_ref.pointer()]);
1428+
}
1429+
tx.insert_via_serialize_bsatn(ST_VAR_ID, &StVarRow { name, value })?;
1430+
Ok(())
1431+
}
1432+
1433+
/// Parse the literal representation of a system variable
1434+
fn parse_var(name: StVarName, literal: &str) -> Result<StVarValue, DBError> {
1435+
StVarValue::try_from_primitive(parse::parse(literal, &name.type_of())?).map_err(|v| {
1436+
ErrorVm::Type(ErrorType::Parse {
1437+
value: literal.to_string(),
1438+
ty: fmt_algebraic_type(&name.type_of()).to_string(),
1439+
err: format!("error parsing value: {:?}", v),
1440+
})
1441+
.into()
1442+
})
1443+
}
13681444
}
13691445

13701446
#[allow(unused)]
@@ -1925,7 +2001,9 @@ mod tests {
19252001
system_tables, StConstraintRow, StIndexRow, StSequenceRow, StTableRow, ST_CONSTRAINT_ID, ST_INDEX_ID,
19262002
ST_SEQUENCE_ID, ST_TABLE_ID,
19272003
};
1928-
use crate::db::relational_db::tests_utils::{begin_tx, insert, make_snapshot, TestDB};
2004+
use crate::db::relational_db::tests_utils::{
2005+
begin_tx, insert, make_snapshot, with_auto_commit, with_read_only, TestDB,
2006+
};
19292007
use crate::error::IndexError;
19302008
use crate::execution_context::ReducerContext;
19312009
use anyhow::bail;
@@ -2009,6 +2087,18 @@ mod tests {
20092087
Ok(())
20102088
}
20112089

2090+
#[test]
2091+
fn test_system_variables() {
2092+
let db = TestDB::durable().expect("failed to create db");
2093+
let _ = with_auto_commit(&db, |tx| db.write_var(tx, StVarName::RowLimit, "5"));
2094+
assert_eq!(
2095+
5,
2096+
with_read_only(&db, |tx| db.row_limit(tx))
2097+
.expect("failed to read from st_var")
2098+
.expect("row_limit does not exist")
2099+
);
2100+
}
2101+
20122102
#[test]
20132103
fn test_open_twice() -> ResultTest<()> {
20142104
let stdb = TestDB::durable()?;

crates/core/src/sql/execute.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::time::Duration;
33
use super::ast::SchemaViewer;
44
use crate::db::datastore::locking_tx_datastore::datastore::report_tx_metricses;
55
use crate::db::datastore::locking_tx_datastore::state_view::StateView;
6-
use crate::db::datastore::system_tables::StVarTable;
76
use crate::db::datastore::traits::IsolationLevel;
87
use crate::db::relational_db::{RelationalDB, Tx};
98
use crate::energy::EnergyQuanta;
@@ -73,7 +72,7 @@ fn execute(
7372
updates: &mut Vec<DatabaseTableUpdate>,
7473
) -> Result<Vec<MemTable>, DBError> {
7574
let slow_query_threshold = if let TxMode::Tx(tx) = p.tx {
76-
StVarTable::query_limit(p.db, tx)?.map(Duration::from_millis)
75+
p.db.query_limit(tx)?.map(Duration::from_millis)
7776
} else {
7877
None
7978
};

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)