@@ -10,7 +10,7 @@ use enum_map::EnumMap;
1010use log:: info;
1111use spacetimedb_commitlog:: repo:: OnNewSegmentFn ;
1212use spacetimedb_commitlog:: { self as commitlog, Commitlog , SizeOnDisk } ;
13- use spacetimedb_data_structures:: map:: HashSet ;
13+ use spacetimedb_data_structures:: map:: { HashMap , HashSet } ;
1414use spacetimedb_datastore:: db_metrics:: DB_METRICS ;
1515use spacetimedb_datastore:: error:: { DatastoreError , TableError , ViewError } ;
1616use 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}
10951105impl 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 > {
0 commit comments