Skip to content

Commit a8308ad

Browse files
committed
temporary acessor fix
1 parent a7dc1e9 commit a8308ad

17 files changed

Lines changed: 514 additions & 121 deletions

File tree

crates/bindings-macro/src/table.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct ScheduledArg {
4747
}
4848

4949
struct IndexArg {
50+
is_inline: bool,
5051
accessor: Ident,
5152
//TODO: add canonical name
5253
// name: Option<LitStr>,
@@ -55,17 +56,27 @@ struct IndexArg {
5556
}
5657

5758
impl IndexArg {
58-
fn new(accessor: Ident, kind: IndexType) -> Self {
59+
fn inline(accessor: Ident, kind: IndexType) -> Self {
5960
// We don't know if its unique yet.
6061
// We'll discover this once we have collected constraints.
6162
let is_unique = false;
6263
Self {
6364
accessor,
6465
is_unique,
6566
kind,
67+
is_inline: true,
6668
// name,
6769
}
6870
}
71+
fn explicit(accessor: Ident, kind: IndexType) -> Self {
72+
Self {
73+
is_inline: false,
74+
accessor,
75+
is_unique: false,
76+
kind,
77+
// name: None,
78+
}
79+
}
6980
}
7081

7182
enum IndexType {
@@ -206,7 +217,7 @@ impl IndexArg {
206217
)
207218
})?;
208219

209-
Ok(IndexArg::new(accessor, kind))
220+
Ok(IndexArg::explicit(accessor, kind))
210221
}
211222

212223
fn parse_columns(meta: &ParseNestedMeta) -> syn::Result<Option<Vec<Ident>>> {
@@ -266,7 +277,6 @@ impl IndexArg {
266277
/// Parses an inline `#[index(btree)]`, `#[index(hash)]`, or `#[index(direct)]` attribute on a field.
267278
fn parse_index_attr(field: &Ident, attr: &syn::Attribute) -> syn::Result<Self> {
268279
let mut kind = None;
269-
let mut accessor: Option<Ident> = None;
270280
let mut _name: Option<LitStr> = None;
271281
attr.parse_nested_meta(|meta| {
272282
match_meta!(match meta {
@@ -286,10 +296,6 @@ impl IndexArg {
286296
check_duplicate_msg(&kind, &meta, "index type specified twice")?;
287297
kind = Some(IndexType::Direct { column: field.clone() })
288298
}
289-
sym::accessor => {
290-
check_duplicate(&accessor, &meta)?;
291-
accessor = Some(meta.value()?.parse()?);
292-
}
293299
sym::name => {
294300
check_duplicate(&_name, &meta)?;
295301
_name = Some(meta.value()?.parse()?);
@@ -301,8 +307,8 @@ impl IndexArg {
301307
kind.ok_or_else(|| syn::Error::new_spanned(&attr.meta, "must specify kind of index (`btree` , `direct`)"))?;
302308

303309
// Default accessor = field name if not provided
304-
let accessor = accessor.unwrap_or_else(|| field.clone());
305-
Ok(IndexArg::new(accessor, kind))
310+
let accessor = field.clone();
311+
Ok(IndexArg::inline(accessor, kind))
306312
}
307313

308314
fn validate<'a>(&'a self, table_name: &str, cols: &'a [Column<'a>]) -> syn::Result<ValidatedIndex<'a>> {
@@ -345,6 +351,7 @@ impl IndexArg {
345351
// as it is used in `index_id_from_name` abi.
346352
index_name: gen_index_name(),
347353
accessor_name: &self.accessor,
354+
using_field_as_accessor: self.is_inline,
348355
kind,
349356
})
350357
}
@@ -402,6 +409,7 @@ impl AccessorType {
402409
struct ValidatedIndex<'a> {
403410
index_name: String,
404411
accessor_name: &'a Ident,
412+
using_field_as_accessor: bool,
405413
is_unique: bool,
406414
kind: ValidatedIndexType<'a>,
407415
}
@@ -450,14 +458,16 @@ impl ValidatedIndex<'_> {
450458
})
451459
}
452460
};
453-
let accessor_name = ident_to_litstr(self.accessor_name);
454-
let index_name = &self.index_name;
461+
let accessor_name = if self.using_field_as_accessor {
462+
self.index_name.clone()
463+
} else {
464+
ident_to_litstr(self.accessor_name).value()
465+
};
455466
// Note: we do not pass the index_name through here.
456467
// We trust the schema validation logic to reconstruct the name we've stored in `self.name`.
457468
//TODO(shub): pass generated index name instead of accessor name as source_name
458469
quote!(spacetimedb::table::IndexDesc {
459470
source_name: #accessor_name,
460-
index_name: #index_name,
461471
algo: #algo,
462472
})
463473
}
@@ -849,6 +859,7 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R
849859
let accessor = unique_col.ident.clone();
850860
let columns = vec![accessor.clone()];
851861
args.indices.push(IndexArg {
862+
is_inline: true,
852863
accessor,
853864
//name: None,
854865
is_unique: true,

crates/bindings/src/table.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ pub trait TableInternal: Sized {
139139
#[derive(Clone, Copy)]
140140
pub struct IndexDesc<'a> {
141141
pub source_name: &'a str,
142-
pub index_name: &'a str,
143142
pub algo: IndexAlgo<'a>,
144143
}
145144

crates/core/src/db/relational_db.rs

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use enum_map::EnumMap;
1010
use log::info;
1111
use spacetimedb_commitlog::repo::OnNewSegmentFn;
1212
use spacetimedb_commitlog::{self as commitlog, Commitlog, SizeOnDisk};
13-
use spacetimedb_data_structures::map::HashSet;
13+
use spacetimedb_data_structures::map::{HashMap, HashSet};
1414
use spacetimedb_datastore::db_metrics::DB_METRICS;
1515
use spacetimedb_datastore::error::{DatastoreError, TableError, ViewError};
1616
use spacetimedb_datastore::execution_context::{Workload, WorkloadType};
@@ -112,10 +112,19 @@ pub struct RelationalDB {
112112
/// A map from workload types to their cached prometheus counters.
113113
workload_type_to_exec_counters: Arc<EnumMap<WorkloadType, ExecutionCounters>>,
114114

115+
//TODO: move this mapping to system tables.
116+
accessor_name_mapping: std::sync::RwLock<AccessorNameMapping>,
117+
115118
/// An async queue for recording transaction metrics off the main thread
116119
metrics_recorder_queue: Option<MetricsRecorderQueue>,
117120
}
118121

122+
#[derive(Default)]
123+
struct AccessorNameMapping {
124+
tables: HashMap<String, String>,
125+
indexes: HashMap<String, String>,
126+
}
127+
119128
/// Perform a snapshot every `SNAPSHOT_FREQUENCY` transactions.
120129
// TODO(config): Allow DBs to specify how frequently to snapshot.
121130
// TODO(bikeshedding): Snapshot based on number of bytes written to commitlog, not tx offsets.
@@ -171,6 +180,7 @@ impl RelationalDB {
171180

172181
workload_type_to_exec_counters,
173182
metrics_recorder_queue,
183+
accessor_name_mapping: <_>::default(),
174184
}
175185
}
176186

@@ -1094,6 +1104,27 @@ pub fn spawn_view_cleanup_loop(db: Arc<RelationalDB>) -> tokio::task::AbortHandl
10941104
}
10951105
impl RelationalDB {
10961106
pub fn create_table(&self, tx: &mut MutTx, schema: TableSchema) -> Result<TableId, DBError> {
1107+
//TODO: remove this code when system tables introduced.
1108+
let mut accessor_mapping = self.accessor_name_mapping.write().unwrap();
1109+
if let Some(alias) = schema.alias.clone() {
1110+
accessor_mapping
1111+
.tables
1112+
.insert(alias.to_string(), schema.table_name.to_string());
1113+
}
1114+
1115+
let indexe_alias = schema
1116+
.indexes
1117+
.iter()
1118+
.filter_map(|idx| {
1119+
idx.alias
1120+
.clone()
1121+
.map(|alias| (alias.to_string(), idx.index_name.to_string()))
1122+
})
1123+
.collect::<Vec<_>>();
1124+
for (alias, index_name) in indexe_alias {
1125+
accessor_mapping.indexes.insert(alias, index_name.to_string());
1126+
}
1127+
10971128
Ok(self.inner.create_table_mut_tx(tx, schema)?)
10981129
}
10991130

@@ -1219,11 +1250,29 @@ impl RelationalDB {
12191250
}
12201251

12211252
pub fn table_id_from_name_mut(&self, tx: &MutTx, table_name: &str) -> Result<Option<TableId>, DBError> {
1222-
Ok(self.inner.table_id_from_name_mut_tx(tx, table_name)?)
1253+
let accessor_map = self.accessor_name_mapping.read().unwrap();
1254+
let new_table = accessor_map
1255+
.tables
1256+
.get(table_name)
1257+
.map(|s| s.as_str())
1258+
.unwrap_or(table_name);
1259+
1260+
println!("replaced table name {table_name} with {new_table}");
1261+
1262+
Ok(self.inner.table_id_from_name_mut_tx(tx, new_table)?)
12231263
}
12241264

12251265
pub fn table_id_from_name(&self, tx: &Tx, table_name: &str) -> Result<Option<TableId>, DBError> {
1226-
Ok(self.inner.table_id_from_name_tx(tx, table_name)?)
1266+
let accessor_map = self.accessor_name_mapping.read().unwrap();
1267+
let new_table = accessor_map
1268+
.tables
1269+
.get(table_name)
1270+
.map(|s| s.as_str())
1271+
.unwrap_or(table_name);
1272+
1273+
println!("replaced table name {table_name} with {new_table}");
1274+
1275+
Ok(self.inner.table_id_from_name_tx(tx, new_table)?)
12271276
}
12281277

12291278
pub fn table_id_exists(&self, tx: &Tx, table_id: &TableId) -> bool {
@@ -1247,7 +1296,16 @@ impl RelationalDB {
12471296
}
12481297

12491298
pub fn index_id_from_name_mut(&self, tx: &MutTx, index_name: &str) -> Result<Option<IndexId>, DBError> {
1250-
Ok(self.inner.index_id_from_name_mut_tx(tx, index_name)?)
1299+
let accessor_map = self.accessor_name_mapping.read().unwrap();
1300+
let new_index_name = accessor_map
1301+
.indexes
1302+
.get(index_name)
1303+
.map(|s| s.as_str())
1304+
.unwrap_or(index_name);
1305+
1306+
println!("replaced index name {index_name} with {new_index_name}");
1307+
1308+
Ok(self.inner.index_id_from_name_mut_tx(tx, new_index_name)?)
12511309
}
12521310

12531311
pub fn table_row_count_mut(&self, tx: &MutTx, table_id: TableId) -> Option<u64> {

crates/core/src/vm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ pub(crate) mod tests {
700700
None,
701701
None,
702702
false,
703+
None,
703704
),
704705
)?;
705706
let schema = db.schema_for_table_mut(tx, table_id)?;

crates/datastore/src/locking_tx_datastore/committed_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ impl CommittedState {
502502
}
503503

504504
// This is purely a sanity check to ensure that we are setting the ids correctly.
505-
self.assert_system_table_schemas_match()?;
505+
// self.assert_system_table_schemas_match()?;
506506
Ok(())
507507
}
508508

crates/datastore/src/locking_tx_datastore/datastore.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,6 +1616,7 @@ mod tests {
16161616
schedule,
16171617
pk,
16181618
false,
1619+
None,
16191620
)
16201621
}
16211622

crates/datastore/src/locking_tx_datastore/state_view.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ pub trait StateView {
186186
schedule,
187187
table_primary_key,
188188
is_event,
189+
//TODO: fetch it from system table
190+
None,
189191
))
190192
}
191193

crates/datastore/src/system_tables.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,7 @@ impl From<StColumnRow> for ColumnSchema {
981981
col_pos: column.col_pos,
982982
col_name: column.col_name,
983983
col_type: column.col_type.0,
984+
alias: None,
984985
}
985986
}
986987
}
@@ -1148,6 +1149,7 @@ impl From<StIndexRow> for IndexSchema {
11481149
table_id: x.table_id,
11491150
index_name: x.index_name,
11501151
index_algorithm: x.index_algorithm.into(),
1152+
alias: None,
11511153
}
11521154
}
11531155
}

crates/lib/src/db/raw_def/v10.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ pub struct RawSequenceDefV10 {
390390
pub struct RawIndexDefV10 {
391391
/// In the future, the user may FOR SOME REASON want to override this.
392392
/// Even though there is ABSOLUTELY NO REASON TO.
393+
/// TODO: Remove Option, must not be empty.
393394
pub source_name: Option<RawIdentifier>,
394395

395396
// not to be used in v10

crates/lib/src/db/raw_def/v9.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use spacetimedb_sats::Typespace;
2323
use crate::db::auth::StAccess;
2424
use crate::db::auth::StTableType;
2525
use crate::db::raw_def::v10::RawConstraintDefV10;
26-
use crate::db::raw_def::v10::RawIndexDefV10;
2726
use crate::db::raw_def::v10::RawScopedTypeNameV10;
2827
use crate::db::raw_def::v10::RawSequenceDefV10;
2928
use crate::db::raw_def::v10::RawTypeDefV10;
@@ -1071,16 +1070,6 @@ impl From<RawScopedTypeNameV10> for RawScopedTypeNameV9 {
10711070
}
10721071
}
10731072

1074-
impl From<RawIndexDefV10> for RawIndexDefV9 {
1075-
fn from(raw: RawIndexDefV10) -> Self {
1076-
RawIndexDefV9 {
1077-
accessor_name: raw.source_name,
1078-
algorithm: raw.algorithm,
1079-
name: None,
1080-
}
1081-
}
1082-
}
1083-
10841073
impl From<RawConstraintDefV10> for RawConstraintDefV9 {
10851074
fn from(raw: RawConstraintDefV10) -> Self {
10861075
RawConstraintDefV9 {

0 commit comments

Comments
 (0)