Skip to content

Commit

Permalink
Finish update.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
kazimuth committed Sep 16, 2024
1 parent 75c1f94 commit e5415c3
Show file tree
Hide file tree
Showing 12 changed files with 459 additions and 701 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ use spacetimedb_lib::db::{
use spacetimedb_lib::{Address, Identity};
use spacetimedb_primitives::{ColList, ConstraintId, IndexId, SequenceId, TableId};
use spacetimedb_sats::{bsatn, buffer::BufReader, AlgebraicValue, ProductValue};
use spacetimedb_schema::{def::TableDef, schema::TableSchema};
use spacetimedb_schema::{
def::{IndexDef, TableDef},
schema::TableSchema,
};
use spacetimedb_snapshot::ReconstructedSnapshot;
use spacetimedb_table::{
indexes::RowPointer,
Expand Down Expand Up @@ -392,7 +395,7 @@ impl MutTxDatastore for Locking {
.map(|opt| opt.map(|s| Cow::Owned(s.into())))
}

fn create_index_mut_tx(&self, tx: &mut Self::MutTx, table_id: TableId, index: RawIndexDefV8) -> Result<IndexId> {
fn create_index_mut_tx(&self, tx: &mut Self::MutTx, table_id: TableId, index: &IndexDef) -> Result<IndexId> {
tx.create_index(table_id, index, self.database_address)
}

Expand Down
11 changes: 3 additions & 8 deletions crates/core/src/db/datastore/locking_tx_datastore/mut_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use spacetimedb_lib::{
use spacetimedb_primitives::{ColId, ColList, ConstraintId, Constraints, IndexId, SequenceId, TableId};
use spacetimedb_sats::{AlgebraicValue, ProductType, ProductValue};
use spacetimedb_schema::{
def::TableDef,
def::{IndexDef, TableDef},
schema::{ConstraintSchema, IndexSchema, SequenceSchema, TableSchema},
};
use spacetimedb_table::{
Expand Down Expand Up @@ -322,7 +322,7 @@ impl MutTxId {
&mut self,
ctx: &ExecutionContext,
table_id: TableId,
index: RawIndexDefV8,
index: &IndexDef,
) -> Result<IndexId> {
let columns = index.columns.clone();
log::trace!(
Expand Down Expand Up @@ -393,12 +393,7 @@ impl MutTxId {
Ok(index_id)
}

pub fn create_index(
&mut self,
table_id: TableId,
index: RawIndexDefV8,
database_address: Address,
) -> Result<IndexId> {
pub fn create_index(&mut self, table_id: TableId, index: &IndexDef, database_address: Address) -> Result<IndexId> {
let columns = index.columns.clone();
let is_unique = index.is_unique;
let ctx = ExecutionContext::internal(database_address);
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/db/datastore/system_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ impl From<StConstraintRow> for ConstraintSchema {
constraint_id: x.constraint_id,
constraint_name: x.constraint_name,
table_id: x.table_id,
constraint_data: match x.constraint_data {
data: match x.constraint_data {
StConstraintData::Unique { columns } => ConstraintData::Unique(UniqueConstraintData { columns }),
StConstraintData::Unused(_) => panic!("Someone put a forbidden variant in the system table!"),
},
Expand Down
93 changes: 52 additions & 41 deletions crates/core/src/db/relational_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ use spacetimedb_commitlog as commitlog;
use spacetimedb_durability::{self as durability, Durability, TxOffset};
use spacetimedb_lib::address::Address;
use spacetimedb_lib::db::auth::{StAccess, StTableType};
use spacetimedb_lib::db::raw_def::v9::{RawIndexAlgorithm, RawModuleDefV9Builder};
use spacetimedb_lib::db::raw_def::{RawColumnDefV8, RawIndexDefV8, RawSequenceDefV8, RawTableDefV8};
use spacetimedb_lib::Identity;
use spacetimedb_primitives::*;
use spacetimedb_sats::{AlgebraicType, AlgebraicValue, ProductType, ProductValue};
use spacetimedb_schema::def::TableDef;
use spacetimedb_schema::def::{IndexDef, ModuleDef, SequenceDef, TableDef};
use spacetimedb_schema::schema::TableSchema;
use spacetimedb_snapshot::{SnapshotError, SnapshotRepository};
use spacetimedb_table::indexes::RowPointer;
Expand Down Expand Up @@ -825,25 +826,47 @@ impl RelationalDB {
.collect()
}

pub fn create_table_for_test_with_the_works(
&self,
name: &str,
schema: &[(&str, AlgebraicType)],
indexes: &[(ColList, &str)],
access: StAccess,
) -> Result<TableId, DBError> {
let mut module_def_builder = RawModuleDefV9Builder::new();
let mut table_builder = module_def_builder
.build_table_with_new_type(name, ProductType::from_iter(schema.iter().cloned()), true)
.with_access(access.into());

for (columns, name) in indexes {
table_builder = table_builder.with_index(
RawIndexAlgorithm::BTree {
columns: columns.clone(),
},
"accessor_name_doesnt_matter",
Some((*name).into()),
);
}
table_builder.finish();
let module_def: ModuleDef = module_def_builder.finish().try_into()?;

let table: &TableDef = module_def.table(name).expect("table not found");

self.with_auto_commit(&ExecutionContext::default(), |tx| self.create_table(tx, table))
}

pub fn create_table_for_test_with_access(
&self,
name: &str,
schema: &[(&str, AlgebraicType)],
indexes: &[(ColId, &str)],
access: StAccess,
) -> Result<TableId, DBError> {
let indexes = indexes
.iter()
.copied()
.map(|(col_id, index_name)| RawIndexDefV8::btree(index_name.into(), col_id, false))
let indexes: Vec<(ColList, &str)> = indexes
.into_iter()
.map(|(col_id, index_name)| ((*col_id).into(), *index_name))
.collect();

let schema = RawTableDefV8::new(name.into(), Self::col_def_for_test(schema))
.with_indexes(indexes)
.with_type(StTableType::User)
.with_access(access);

self.with_auto_commit(&ExecutionContext::default(), |tx| self.create_table(tx, schema))
self.create_table_for_test_with_the_works(name, schema, &indexes[..], access)
}

pub fn create_table_for_test(
Expand All @@ -861,12 +884,7 @@ impl RelationalDB {
schema: &[(&str, AlgebraicType)],
idx_cols: ColList,
) -> Result<TableId, DBError> {
let schema = RawTableDefV8::new(name.into(), Self::col_def_for_test(schema))
.with_column_index(idx_cols, false)
.with_type(StTableType::User)
.with_access(StAccess::Public);

self.with_auto_commit(&ExecutionContext::default(), |tx| self.create_table(tx, schema))
self.create_table_for_test_with_the_works(name, schema, &[(idx_cols, "the_index")], StAccess::Public)
}

pub fn create_table_for_test_mix_indexes(
Expand All @@ -876,19 +894,13 @@ impl RelationalDB {
idx_cols_single: &[(ColId, &str)],
idx_cols_multi: ColList,
) -> Result<TableId, DBError> {
let idx_cols_single = idx_cols_single
let indexes: Vec<(ColList, &str)> = idx_cols_single
.iter()
.copied()
.map(|(col_id, index_name)| RawIndexDefV8::btree(index_name.into(), col_id, false))
.map(|(col_id, name)| ((*col_id).into(), *name))
.chain(std::iter::once((idx_cols_multi, "the_only_multi_index")))
.collect();

let schema = RawTableDefV8::new(name.into(), Self::col_def_for_test(schema))
.with_indexes(idx_cols_single)
.with_column_index(idx_cols_multi, false)
.with_type(StTableType::User)
.with_access(StAccess::Public);

self.with_auto_commit(&ExecutionContext::default(), |tx| self.create_table(tx, schema))
self.create_table_for_test_with_the_works(name, schema, &indexes[..], StAccess::Public)
}

pub fn drop_table(&self, ctx: &ExecutionContext, tx: &mut MutTx, table_id: TableId) -> Result<(), DBError> {
Expand Down Expand Up @@ -959,13 +971,17 @@ impl RelationalDB {
) -> Result<Constraints, DBError> {
let table = self.inner.schema_for_table_mut_tx(tx, table_id)?;

let index = unimplemented!();
let unique_constraint = table.constraints.iter().find(|c| c.unique_columns() == Some(cols));
let mut attr = Constraints::unset();
if let Some(is_unique) = unique_index {
attr = attr.push(Constraints::from_is_unique(is_unique));
let index = table.indexes.iter().find(|i| i.columns() == cols);
let cols_set = ColSet::from(cols);
let unique_constraint = table.constraints.iter().find(|c| c.unique_columns() == Some(&cols_set));

if index.is_some() {
Ok(Constraints::from_is_unique(unique_constraint.is_some()))
} else if unique_constraint.is_some() {
Ok(Constraints::unique())
} else {
Ok(Constraints::unset())
}
Ok(attr)
}

pub fn index_id_from_name(&self, tx: &MutTx, index_name: &str) -> Result<Option<IndexId>, DBError> {
Expand All @@ -985,7 +1001,7 @@ impl RelationalDB {
/// Returns the `index_id`
///
/// NOTE: It loads the data from the table into it before returning
pub fn create_index(&self, tx: &mut MutTx, table_id: TableId, index: RawIndexDefV8) -> Result<IndexId, DBError> {
pub fn create_index(&self, tx: &mut MutTx, table_id: TableId, index: &IndexDef) -> Result<IndexId, DBError> {
self.inner.create_index_mut_tx(tx, table_id, index)
}

Expand Down Expand Up @@ -1116,12 +1132,7 @@ impl RelationalDB {
}

/// Add a [Sequence] into the database instance, generates a stable [SequenceId] for it that will persist on restart.
pub fn create_sequence(
&mut self,
tx: &mut MutTx,
table_id: TableId,
seq: RawSequenceDefV8,
) -> Result<SequenceId, DBError> {
pub fn create_sequence(&self, tx: &mut MutTx, table_id: TableId, seq: &SequenceDef) -> Result<SequenceId, DBError> {
self.inner.create_sequence_mut_tx(tx, table_id, seq)
}

Expand Down
Loading

0 comments on commit e5415c3

Please sign in to comment.