Skip to content

Commit 4a28bf0

Browse files
committed
Remove StVarTable in furtherance of datastore extraction
1 parent 41fa66f commit 4a28bf0

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,
@@ -32,10 +32,12 @@ use spacetimedb_commitlog as commitlog;
3232
use spacetimedb_durability::{self as durability, TxOffset};
3333
use spacetimedb_lib::db::auth::StAccess;
3434
use spacetimedb_lib::db::raw_def::v9::{btree, RawModuleDefV9Builder, RawSql};
35+
use spacetimedb_lib::st_var::StVarValue;
3536
use spacetimedb_lib::ConnectionId;
3637
use spacetimedb_lib::Identity;
3738
use spacetimedb_paths::server::{CommitLogDir, ReplicaDir, SnapshotsPath};
3839
use spacetimedb_primitives::*;
40+
use spacetimedb_sats::algebraic_type::fmt::fmt_algebraic_type;
3941
use spacetimedb_sats::{AlgebraicType, AlgebraicValue, ProductType, ProductValue};
4042
use spacetimedb_schema::def::{ModuleDef, TableDef};
4143
use spacetimedb_schema::schema::{IndexSchema, RowLevelSecuritySchema, Schema, SequenceSchema, TableSchema};
@@ -44,6 +46,8 @@ use spacetimedb_table::indexes::RowPointer;
4446
use spacetimedb_table::page_pool::PagePool;
4547
use spacetimedb_table::table::RowRef;
4648
use spacetimedb_table::MemoryUsage;
49+
use spacetimedb_vm::errors::{ErrorType, ErrorVm};
50+
use spacetimedb_vm::ops::parse;
4751
use std::borrow::Cow;
4852
use std::collections::HashSet;
4953
use std::fmt;
@@ -1388,6 +1392,78 @@ impl RelationalDB {
13881392
pub fn report(&self, reducer: &str, metrics: &TxMetrics, tx_data: Option<&TxData>) {
13891393
metrics.report(tx_data, reducer, |wl: WorkloadType| self.exec_counters_for(wl));
13901394
}
1395+
1396+
/// Read the value of [ST_VARNAME_ROW_LIMIT] from `st_var`
1397+
pub(crate) fn row_limit(&self, tx: &Tx) -> Result<Option<u64>, DBError> {
1398+
let data = self.read_var(tx, StVarName::RowLimit);
1399+
1400+
if let Some(StVarValue::U64(limit)) = data? {
1401+
return Ok(Some(limit));
1402+
}
1403+
Ok(None)
1404+
}
1405+
1406+
/// Read the value of [ST_VARNAME_SLOW_QRY] from `st_var`
1407+
pub(crate) fn query_limit(&self, tx: &Tx) -> Result<Option<u64>, DBError> {
1408+
if let Some(StVarValue::U64(ms)) = self.read_var(tx, StVarName::SlowQryThreshold)? {
1409+
return Ok(Some(ms));
1410+
}
1411+
Ok(None)
1412+
}
1413+
1414+
/// Read the value of [ST_VARNAME_SLOW_SUB] from `st_var`
1415+
#[allow(dead_code)]
1416+
pub(crate) fn sub_limit(&self, tx: &Tx) -> Result<Option<u64>, DBError> {
1417+
if let Some(StVarValue::U64(ms)) = self.read_var(tx, StVarName::SlowSubThreshold)? {
1418+
return Ok(Some(ms));
1419+
}
1420+
Ok(None)
1421+
}
1422+
1423+
/// Read the value of [ST_VARNAME_SLOW_INC] from `st_var`
1424+
#[allow(dead_code)]
1425+
pub(crate) fn incr_limit(&self, tx: &Tx) -> Result<Option<u64>, DBError> {
1426+
if let Some(StVarValue::U64(ms)) = self.read_var(tx, StVarName::SlowIncThreshold)? {
1427+
return Ok(Some(ms));
1428+
}
1429+
Ok(None)
1430+
}
1431+
1432+
/// Read the value of a system variable from `st_var`
1433+
pub(crate) fn read_var(&self, tx: &Tx, name: StVarName) -> Result<Option<StVarValue>, DBError> {
1434+
if let Some(row_ref) = self
1435+
.iter_by_col_eq(tx, ST_VAR_ID, StVarFields::Name.col_id(), &name.into())?
1436+
.next()
1437+
{
1438+
return Ok(Some(StVarRow::try_from(row_ref)?.value));
1439+
}
1440+
Ok(None)
1441+
}
1442+
1443+
/// Update the value of a system variable in `st_var`
1444+
pub(crate) fn write_var(&self, tx: &mut MutTx, name: StVarName, literal: &str) -> Result<(), DBError> {
1445+
let value = Self::parse_var(name, literal)?;
1446+
if let Some(row_ref) = self
1447+
.iter_by_col_eq_mut(tx, ST_VAR_ID, StVarFields::Name.col_id(), &name.into())?
1448+
.next()
1449+
{
1450+
self.delete(tx, ST_VAR_ID, [row_ref.pointer()]);
1451+
}
1452+
tx.insert_via_serialize_bsatn(ST_VAR_ID, &StVarRow { name, value })?;
1453+
Ok(())
1454+
}
1455+
1456+
/// Parse the literal representation of a system variable
1457+
fn parse_var(name: StVarName, literal: &str) -> Result<StVarValue, DBError> {
1458+
StVarValue::try_from_primitive(parse::parse(literal, &name.type_of())?).map_err(|v| {
1459+
ErrorVm::Type(ErrorType::Parse {
1460+
value: literal.to_string(),
1461+
ty: fmt_algebraic_type(&name.type_of()).to_string(),
1462+
err: format!("error parsing value: {:?}", v),
1463+
})
1464+
.into()
1465+
})
1466+
}
13911467
}
13921468

13931469
#[allow(unused)]
@@ -1948,7 +2024,9 @@ mod tests {
19482024
system_tables, StConstraintRow, StIndexRow, StSequenceRow, StTableRow, ST_CONSTRAINT_ID, ST_INDEX_ID,
19492025
ST_SEQUENCE_ID, ST_TABLE_ID,
19502026
};
1951-
use crate::db::relational_db::tests_utils::{begin_tx, insert, make_snapshot, TestDB};
2027+
use crate::db::relational_db::tests_utils::{
2028+
begin_tx, insert, make_snapshot, with_auto_commit, with_read_only, TestDB,
2029+
};
19522030
use crate::error::IndexError;
19532031
use crate::execution_context::ReducerContext;
19542032
use anyhow::bail;
@@ -2032,6 +2110,18 @@ mod tests {
20322110
Ok(())
20332111
}
20342112

2113+
#[test]
2114+
fn test_system_variables() {
2115+
let db = TestDB::durable().expect("failed to create db");
2116+
let _ = with_auto_commit(&db, |tx| db.write_var(tx, StVarName::RowLimit, "5"));
2117+
assert_eq!(
2118+
5,
2119+
with_read_only(&db, |tx| db.row_limit(tx))
2120+
.expect("failed to read from st_var")
2121+
.expect("row_limit does not exist")
2122+
);
2123+
}
2124+
20352125
#[test]
20362126
fn test_open_twice() -> ResultTest<()> {
20372127
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)