@@ -61,7 +61,6 @@ use indexing::{
61
61
Index ,
62
62
} ,
63
63
} ;
64
- use itertools:: Itertools ;
65
64
use value:: {
66
65
ResolvedDocumentId ,
67
66
TableMapping ,
@@ -113,14 +112,15 @@ impl<'a, RT: Runtime> IndexModel<'a, RT> {
113
112
/// transaction has committed.
114
113
pub async fn add_application_index (
115
114
& mut self ,
115
+ namespace : TableNamespace ,
116
116
index : IndexMetadata < TableName > ,
117
117
) -> anyhow:: Result < ResolvedDocumentId > {
118
118
anyhow:: ensure!(
119
119
self . tx. identity( ) . is_admin( ) || self . tx. identity( ) . is_system( ) ,
120
120
unauthorized_error( "add_index" )
121
121
) ;
122
122
anyhow:: ensure!( !index. name. is_system_owned( ) , "Can't change system indexes" ) ;
123
- let application_indexes = self . get_application_indexes ( ) . await ?;
123
+ let application_indexes = self . get_application_indexes ( namespace ) . await ?;
124
124
// Indexes may exist in both a pending and an enabled state. If we're at or over
125
125
// the index limit, we should still be able to add a new pending copy of
126
126
// an enabled index with the expectation that the pending index will
@@ -137,8 +137,7 @@ impl<'a, RT: Runtime> IndexModel<'a, RT> {
137
137
|| index_names_in_table. len( ) < MAX_INDEXES_PER_TABLE ,
138
138
index_validation_error:: too_many_indexes( index. name. table( ) , MAX_INDEXES_PER_TABLE )
139
139
) ;
140
- self . _add_index ( TableNamespace :: by_component_TODO ( ) , index)
141
- . await
140
+ self . _add_index ( namespace, index) . await
142
141
}
143
142
144
143
/// Add system index.
@@ -281,7 +280,7 @@ impl<'a, RT: Runtime> IndexModel<'a, RT> {
281
280
. await ?;
282
281
283
282
tracing:: info!(
284
- "Committed indexes: (added {}. dropped {})" ,
283
+ "Committed indexes for {namespace:?} : (added {}. dropped {})" ,
285
284
index_diff. added. len( ) ,
286
285
index_diff. dropped. len( ) ,
287
286
) ;
@@ -312,7 +311,8 @@ impl<'a, RT: Runtime> IndexModel<'a, RT> {
312
311
added : vec ! [ ] ,
313
312
dropped : dropped. clone ( ) ,
314
313
} ;
315
- self . apply_index_diff ( & only_dropped_tables) . await ?;
314
+ self . apply_index_diff ( namespace, & only_dropped_tables)
315
+ . await ?;
316
316
317
317
// Added indexes should have backfilled via build_indexes
318
318
// (for < 0.14.0 CLIs) or in apply_config (for >= 0.14.0 CLIs).
@@ -339,15 +339,19 @@ impl<'a, RT: Runtime> IndexModel<'a, RT> {
339
339
Ok ( ( ) )
340
340
}
341
341
342
- pub async fn apply_index_diff ( & mut self , diff : & LegacyIndexDiff ) -> anyhow:: Result < ( ) > {
342
+ pub async fn apply_index_diff (
343
+ & mut self ,
344
+ namespace : TableNamespace ,
345
+ diff : & LegacyIndexDiff ,
346
+ ) -> anyhow:: Result < ( ) > {
343
347
if !( self . tx . identity ( ) . is_admin ( ) || self . tx . identity ( ) . is_system ( ) ) {
344
348
anyhow:: bail!( unauthorized_error( "modify_indexes" ) ) ;
345
349
}
346
350
for index in & diff. dropped {
347
351
self . drop_index ( index. id ( ) ) . await ?;
348
352
}
349
353
for index in & diff. added {
350
- self . add_application_index ( index. clone ( ) ) . await ?;
354
+ self . add_application_index ( namespace , index. clone ( ) ) . await ?;
351
355
}
352
356
353
357
Ok ( ( ) )
@@ -398,7 +402,7 @@ impl<'a, RT: Runtime> IndexModel<'a, RT> {
398
402
399
403
let mut remaining_indexes: HashMap < IndexName , Vec < ParsedDocument < DeveloperIndexMetadata > > > =
400
404
HashMap :: new ( ) ;
401
- for index in self . get_application_indexes ( ) . await ? {
405
+ for index in self . get_application_indexes ( namespace ) . await ? {
402
406
remaining_indexes
403
407
. entry ( index. name . clone ( ) )
404
408
. or_default ( )
@@ -513,7 +517,8 @@ impl<'a, RT: Runtime> IndexModel<'a, RT> {
513
517
// Dropped will be removed in apply_config when the rest of the schema is
514
518
// committed.
515
519
if !only_new_and_mutated. is_empty ( ) {
516
- self . apply_index_diff ( & only_new_and_mutated) . await ?;
520
+ self . apply_index_diff ( namespace, & only_new_and_mutated)
521
+ . await ?;
517
522
}
518
523
Ok ( diff)
519
524
}
@@ -527,7 +532,7 @@ impl<'a, RT: Runtime> IndexModel<'a, RT> {
527
532
if diff. is_empty ( ) {
528
533
return Ok ( diff) ;
529
534
}
530
- self . apply_index_diff ( & diff) . await ?;
535
+ self . apply_index_diff ( namespace , & diff) . await ?;
531
536
Ok ( diff)
532
537
}
533
538
@@ -775,8 +780,9 @@ impl<'a, RT: Runtime> IndexModel<'a, RT> {
775
780
/// name (but different configurations).
776
781
pub async fn get_system_indexes (
777
782
& mut self ,
783
+ namespace : TableNamespace ,
778
784
) -> anyhow:: Result < Vec < ParsedDocument < DeveloperIndexMetadata > > > {
779
- self . get_indexes ( IndexCategory :: System ) . await
785
+ self . get_indexes ( IndexCategory :: System , namespace ) . await
780
786
}
781
787
782
788
/// Returns all registered indexes that aren't system owned including both
@@ -786,23 +792,32 @@ impl<'a, RT: Runtime> IndexModel<'a, RT> {
786
792
/// name (but different configurations).
787
793
pub async fn get_application_indexes (
788
794
& mut self ,
795
+ namespace : TableNamespace ,
789
796
) -> anyhow:: Result < Vec < ParsedDocument < DeveloperIndexMetadata > > > {
790
- self . get_indexes ( IndexCategory :: Application ) . await
797
+ self . get_indexes ( IndexCategory :: Application , namespace)
798
+ . await
791
799
}
792
800
793
801
async fn get_indexes (
794
802
& mut self ,
795
803
category : IndexCategory ,
804
+ namespace : TableNamespace ,
796
805
) -> anyhow:: Result < Vec < ParsedDocument < DeveloperIndexMetadata > > > {
797
- let indexes_with_id = self . get_all_indexes ( ) . await ?;
806
+ let indexes = self . get_all_indexes ( ) . await ?;
798
807
let table_mapping = self . tx . table_mapping ( ) ;
799
- indexes_with_id
800
- . into_iter ( )
801
- . filter ( |doc| category. belongs ( doc, table_mapping) )
802
- . map ( |doc : ParsedDocument < TabletIndexMetadata > | {
803
- doc. map ( |metadata| metadata. map_table ( & table_mapping. tablet_to_name ( ) ) )
804
- } )
805
- . try_collect ( )
808
+ let mut result = vec ! [ ] ;
809
+ for doc in indexes {
810
+ if !category. belongs ( & doc, table_mapping) {
811
+ continue ;
812
+ }
813
+ let tablet_id = * doc. name . table ( ) ;
814
+ if table_mapping. tablet_namespace ( tablet_id) ? != namespace {
815
+ continue ;
816
+ }
817
+ let doc = doc. map ( |metadata| metadata. map_table ( & table_mapping. tablet_to_name ( ) ) ) ?;
818
+ result. push ( doc) ;
819
+ }
820
+ Ok ( result)
806
821
}
807
822
808
823
pub async fn drop_index ( & mut self , index_id : ResolvedDocumentId ) -> anyhow:: Result < ( ) > {
@@ -812,10 +827,14 @@ impl<'a, RT: Runtime> IndexModel<'a, RT> {
812
827
Ok ( ( ) )
813
828
}
814
829
815
- pub async fn drop_system_index ( & mut self , index_name : IndexName ) -> anyhow:: Result < ( ) > {
830
+ pub async fn drop_system_index (
831
+ & mut self ,
832
+ namespace : TableNamespace ,
833
+ index_name : IndexName ,
834
+ ) -> anyhow:: Result < ( ) > {
816
835
anyhow:: ensure!( index_name. table( ) . is_system( ) ) ;
817
836
let system_index = self
818
- . get_system_indexes ( )
837
+ . get_system_indexes ( namespace )
819
838
. await ?
820
839
. into_iter ( )
821
840
. find ( |index| index. name == index_name) ;
@@ -828,14 +847,15 @@ impl<'a, RT: Runtime> IndexModel<'a, RT> {
828
847
829
848
pub async fn copy_indexes_to_table (
830
849
& mut self ,
850
+ namespace : TableNamespace ,
831
851
source_table : & TableName ,
832
852
target_table : TabletId ,
833
853
) -> anyhow:: Result < ( ) > {
834
854
// Copy over enabled indexes from existing active table, if any.
835
855
let Some ( active_table_id) = self
836
856
. tx
837
857
. table_mapping ( )
838
- . namespace ( TableNamespace :: by_component_TODO ( ) )
858
+ . namespace ( namespace )
839
859
. id_if_exists ( source_table)
840
860
else {
841
861
return Ok ( ( ) ) ;
0 commit comments