Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions crates/datastore/src/locking_tx_datastore/committed_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,32 @@ impl CommittedState {
table.with_mut_schema(|s| s.remove_index(index_id));
self.index_id_map.remove(&index_id);
}
// An index alias/source-name changed. Change it back.
IndexAlterSourceName(table_id, index_id, old_alias) => {
let table = self.tables.get_mut(&table_id)?;
let mut index_schema = table
.get_schema()
.indexes
.iter()
.find(|x| x.index_id == index_id)?
.clone();
index_schema.alias = old_alias;
table.with_mut_schema(|s| s.update_index(index_schema));
}
// A table accessor name alias changed. Change it back.
TableAlterAccessorName(table_id, old_alias) => {
let table = self.tables.get_mut(&table_id)?;
table.with_mut_schema(|s| s.alias = old_alias);
}
// A column accessor name alias changed. Change it back.
ColumnAlterAccessorName(table_id, col_id, old_alias) => {
let table = self.tables.get_mut(&table_id)?;
table.with_mut_schema(|s| {
if let Some(col) = s.columns.iter_mut().find(|x| x.col_pos == col_id) {
col.alias = old_alias;
}
});
}
// A table was removed. Add it back.
TableRemoved(table_id, table) => {
let is_view_table = table.schema.is_view();
Expand Down
56 changes: 56 additions & 0 deletions crates/datastore/src/locking_tx_datastore/datastore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,34 @@ impl Locking {
tx.alter_table_primary_key(table_id, primary_key)
}

pub fn alter_index_source_name_mut_tx(
&self,
tx: &mut MutTxId,
index_id: IndexId,
source_name: spacetimedb_sats::raw_identifier::RawIdentifier,
) -> Result<()> {
tx.alter_index_source_name(index_id, source_name)
}

pub fn alter_table_accessor_name_mut_tx(
&self,
tx: &mut MutTxId,
table_id: TableId,
new_alias: spacetimedb_schema::identifier::Identifier,
) -> Result<()> {
tx.alter_table_accessor_name(table_id, new_alias)
}

pub fn alter_column_accessor_name_mut_tx(
&self,
tx: &mut MutTxId,
table_id: TableId,
col_id: ColId,
new_alias: spacetimedb_schema::identifier::Identifier,
) -> Result<()> {
tx.alter_column_accessor_name(table_id, col_id, new_alias)
}

pub fn alter_table_row_type_mut_tx(
&self,
tx: &mut MutTxId,
Expand Down Expand Up @@ -551,6 +579,34 @@ impl MutTxDatastore for Locking {
tx.drop_index(index_id)
}

fn alter_index_source_name_mut_tx(
&self,
tx: &mut Self::MutTx,
index_id: IndexId,
source_name: spacetimedb_sats::raw_identifier::RawIdentifier,
) -> Result<()> {
tx.alter_index_source_name(index_id, source_name)
}

fn alter_table_accessor_name_mut_tx(
&self,
tx: &mut Self::MutTx,
table_id: TableId,
new_alias: spacetimedb_schema::identifier::Identifier,
) -> Result<()> {
tx.alter_table_accessor_name(table_id, new_alias)
}

fn alter_column_accessor_name_mut_tx(
&self,
tx: &mut Self::MutTx,
table_id: TableId,
col_id: ColId,
new_alias: spacetimedb_schema::identifier::Identifier,
) -> Result<()> {
tx.alter_column_accessor_name(table_id, col_id, new_alias)
}

fn index_id_from_name_mut_tx(&self, tx: &Self::MutTx, index_name: &str) -> Result<Option<IndexId>> {
tx.index_id_from_name_or_alias(index_name)
}
Expand Down
120 changes: 120 additions & 0 deletions crates/datastore/src/locking_tx_datastore/mut_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,126 @@ impl MutTxId {
Ok(())
}

/// Change the runtime source-name alias of the index identified by `index_id`.
pub(crate) fn alter_index_source_name(
&mut self,
index_id: IndexId,
source_name: spacetimedb_sats::raw_identifier::RawIdentifier,
) -> Result<()> {
let st_index_ref = self
.iter_by_col_eq(ST_INDEX_ID, StIndexFields::IndexId, &index_id.into())?
.next()
.ok_or_else(|| TableError::IdNotFound(SystemTable::st_index, index_id.into()))?;
let st_index_row = StIndexRow::try_from(st_index_ref)?;
let table_id = st_index_row.table_id;

let old_alias = self
.find_st_index_accessor_row_by_index_name(st_index_row.index_name.as_ref())?
.map(|row| row.accessor_name);

if old_alias.as_ref() == Some(&source_name) {
return Ok(());
}

let ((tx_table, ..), (commit_table, ..)) = self.get_or_create_insert_table_mut(table_id)?;
let mut index_schema = tx_table
.get_schema()
.indexes
.iter()
.find(|index| index.index_id == index_id)
.cloned()
.ok_or_else(|| TableError::IdNotFound(SystemTable::st_index, index_id.into()))?;
index_schema.alias = Some(source_name.clone());
tx_table.with_mut_schema_and_clone(commit_table, |s| s.update_index(index_schema));

self.drop_st_index_accessor(&st_index_row.index_name)?;
self.insert_st_index_accessor(&st_index_row.index_name, Some(&source_name))?;

self.push_schema_change(PendingSchemaChange::IndexAlterSourceName(table_id, index_id, old_alias));

Ok(())
}

/// Change the runtime accessor name alias of the table identified by `table_id`.
pub(crate) fn alter_table_accessor_name(
&mut self,
table_id: TableId,
new_alias: Identifier,
) -> Result<()> {
let table_name = self.find_st_table_row(table_id)?.table_name;

let old_alias = self
.iter_by_col_eq(ST_TABLE_ACCESSOR_ID, StTableAccessorFields::TableName, &table_name.as_ref().into())?
.next()
.map(|row| StTableAccessorRow::try_from(row).ok())
.flatten()
.map(|row| row.accessor_name);

if old_alias.as_ref() == Some(&new_alias) {
return Ok(());
}

let ((tx_table, ..), (commit_table, ..)) = self.get_or_create_insert_table_mut(table_id)?;
tx_table.with_mut_schema_and_clone(commit_table, |s| s.alias = Some(new_alias.clone()));

self.drop_st_table_accessor(&table_name)?;
self.insert_st_table_accessor(&table_name, Some(&new_alias))?;

self.push_schema_change(PendingSchemaChange::TableAlterAccessorName(table_id, old_alias));

Ok(())
}

/// Change the runtime accessor name alias of the column identified by `table_id` and `col_id`.
pub(crate) fn alter_column_accessor_name(
&mut self,
table_id: TableId,
col_id: ColId,
new_alias: Identifier,
) -> Result<()> {
let table_name = self.find_st_table_row(table_id)?.table_name;

// Read old column info from the current schema
let col_info: Vec<(Identifier, Option<Identifier>, ColId)> = self
.schema_for_table(table_id)?
.columns
.iter()
.map(|c| (c.col_name.clone(), c.alias.clone(), c.col_pos))
.collect();

let old_alias = col_info
.iter()
.find(|(_, _, pos)| *pos == col_id)
.and_then(|(_, alias, _)| alias.clone());

if old_alias.as_ref() == Some(&new_alias) {
return Ok(());
}

// Update system tables: drop and re-insert all column accessor rows for this table
self.drop_st_column_accessor(&table_name)?;
for (col_name, alias, pos) in &col_info {
let name = if *pos == col_id {
&new_alias
} else {
alias.as_ref().unwrap_or(col_name)
};
self.insert_st_column_accessor(&table_name, col_name, Some(name))?;
}

// Update in-memory schema
let ((tx_table, ..), (commit_table, ..)) = self.get_or_create_insert_table_mut(table_id)?;
tx_table.with_mut_schema_and_clone(commit_table, |s| {
if let Some(col) = s.columns.iter_mut().find(|c| c.col_pos == col_id) {
col.alias = Some(new_alias);
}
});

self.push_schema_change(PendingSchemaChange::ColumnAlterAccessorName(table_id, col_id, old_alias));

Ok(())
}

/// Change the row type of the table identified by `table_id`.
///
/// In practice, this should not error,
Expand Down
27 changes: 26 additions & 1 deletion crates/datastore/src/locking_tx_datastore/tx_state.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::{delete_table::DeleteTable, sequence::Sequence};
use spacetimedb_data_structures::map::IntMap;
use spacetimedb_lib::db::auth::StAccess;
use spacetimedb_primitives::{ColList, ConstraintId, IndexId, SequenceId, TableId};
use spacetimedb_primitives::{ColId, ColList, ConstraintId, IndexId, SequenceId, TableId};
use spacetimedb_sats::memory_usage::MemoryUsage;
use spacetimedb_schema::identifier::Identifier;
use spacetimedb_schema::schema::{ColumnSchema, ConstraintSchema, IndexSchema, SequenceSchema};
use spacetimedb_table::{
blob_store::{BlobStore, HashMapBlobStore},
Expand Down Expand Up @@ -111,6 +112,19 @@ pub enum PendingSchemaChange {
/// If adding this index caused the pointer map to be removed,
/// it will be present here.
IndexAdded(TableId, IndexId, Option<PointerMap>),
/// The source-name alias of the index with [`IndexId`] changed.
/// The old alias is stored for rollback.
IndexAlterSourceName(
TableId,
IndexId,
Option<spacetimedb_sats::raw_identifier::RawIdentifier>,
),
/// The accessor name alias of the table with [`TableId`] changed.
/// The old alias is stored for rollback.
TableAlterAccessorName(TableId, Option<Identifier>),
/// The accessor name alias of the column with [`ColId`] in the table with [`TableId`] changed.
/// The old alias is stored for rollback.
ColumnAlterAccessorName(TableId, ColId, Option<Identifier>),
/// The [`Table`] with [`TableId`] was removed.
TableRemoved(TableId, Table),
/// The table with [`TableId`] was added.
Expand Down Expand Up @@ -153,6 +167,9 @@ impl MemoryUsage for PendingSchemaChange {
Self::IndexAdded(table_id, index_id, pointer_map) => {
table_id.heap_usage() + index_id.heap_usage() + pointer_map.heap_usage()
}
Self::IndexAlterSourceName(table_id, index_id, alias) => {
table_id.heap_usage() + index_id.heap_usage() + alias.heap_usage()
}
Self::TableRemoved(table_id, table) => table_id.heap_usage() + table.heap_usage(),
Self::TableAdded(table_id) => table_id.heap_usage(),
Self::TableAlterAccess(table_id, st_access) => table_id.heap_usage() + st_access.heap_usage(),
Expand All @@ -168,6 +185,14 @@ impl MemoryUsage for PendingSchemaChange {
table_id.heap_usage() + sequence.heap_usage() + sequence_schema.heap_usage()
}
Self::SequenceAdded(table_id, sequence_id) => table_id.heap_usage() + sequence_id.heap_usage(),
Self::TableAlterAccessorName(table_id, alias) => {
table_id.heap_usage() + alias.as_ref().map(|a| a.as_raw().heap_usage()).unwrap_or(0)
}
Self::ColumnAlterAccessorName(table_id, col_id, alias) => {
table_id.heap_usage()
+ col_id.heap_usage()
+ alias.as_ref().map(|a| a.as_raw().heap_usage()).unwrap_or(0)
}
Self::ReschemaEventTable(table_id, column_schemas) => table_id.heap_usage() + column_schemas.heap_usage(),
}
}
Expand Down
19 changes: 19 additions & 0 deletions crates/datastore/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,25 @@ pub trait MutTxDatastore: TxDatastore + MutTx {

fn create_index_mut_tx(&self, tx: &mut Self::MutTx, index_schema: IndexSchema, is_unique: bool) -> Result<IndexId>;
fn drop_index_mut_tx(&self, tx: &mut Self::MutTx, index_id: IndexId) -> Result<()>;
fn alter_index_source_name_mut_tx(
&self,
tx: &mut Self::MutTx,
index_id: IndexId,
source_name: spacetimedb_sats::raw_identifier::RawIdentifier,
) -> Result<()>;
fn alter_table_accessor_name_mut_tx(
&self,
tx: &mut Self::MutTx,
table_id: TableId,
new_alias: spacetimedb_schema::identifier::Identifier,
) -> Result<()>;
fn alter_column_accessor_name_mut_tx(
&self,
tx: &mut Self::MutTx,
table_id: TableId,
col_id: ColId,
new_alias: spacetimedb_schema::identifier::Identifier,
) -> Result<()>;
fn index_id_from_name_mut_tx(&self, tx: &Self::MutTx, index_name: &str) -> super::Result<Option<IndexId>>;

// TODO: Index data
Expand Down
28 changes: 28 additions & 0 deletions crates/engine/src/relational_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,34 @@ impl RelationalDB {
Ok(self.inner.alter_table_primary_key_mut_tx(tx, name, primary_key)?)
}

pub(crate) fn alter_index_source_name(
&self,
tx: &mut MutTx,
index_id: IndexId,
source_name: spacetimedb_sats::raw_identifier::RawIdentifier,
) -> Result<(), DBError> {
Ok(self.inner.alter_index_source_name_mut_tx(tx, index_id, source_name)?)
}

pub(crate) fn alter_table_accessor_name(
&self,
tx: &mut MutTx,
table_id: TableId,
new_alias: spacetimedb_schema::identifier::Identifier,
) -> Result<(), DBError> {
Ok(self.inner.alter_table_accessor_name_mut_tx(tx, table_id, new_alias)?)
}

pub(crate) fn alter_column_accessor_name(
&self,
tx: &mut MutTx,
table_id: TableId,
col_id: ColId,
new_alias: spacetimedb_schema::identifier::Identifier,
) -> Result<(), DBError> {
Ok(self.inner.alter_column_accessor_name_mut_tx(tx, table_id, col_id, new_alias)?)
}

pub(crate) fn alter_table_row_type(
&self,
tx: &mut MutTx,
Expand Down
Loading
Loading