@@ -1803,7 +1803,6 @@ public static class Builder {
18031803 private DiffableStringMap hashesOfConsistentSettings = DiffableStringMap .EMPTY ;
18041804
18051805 private final ImmutableOpenMap .Builder <String , IndexMetadata > indices ;
1806- private final ImmutableOpenMap .Builder <String , Set <Index >> aliasedIndices ;
18071806 private final ImmutableOpenMap .Builder <String , IndexTemplateMetadata > templates ;
18081807 private final ImmutableOpenMap .Builder <String , Custom > customs ;
18091808
@@ -1833,7 +1832,6 @@ public Builder() {
18331832 this .hashesOfConsistentSettings = metadata .hashesOfConsistentSettings ;
18341833 this .version = metadata .version ;
18351834 this .indices = ImmutableOpenMap .builder (metadata .indices );
1836- this .aliasedIndices = ImmutableOpenMap .builder (metadata .aliasedIndices );
18371835 this .templates = ImmutableOpenMap .builder (metadata .templates );
18381836 this .customs = ImmutableOpenMap .builder (metadata .customs );
18391837 this .previousIndicesLookup = metadata .indicesLookup ;
@@ -1846,7 +1844,6 @@ public Builder() {
18461844 private Builder (Map <String , MappingMetadata > mappingsByHash , int indexCountHint ) {
18471845 clusterUUID = UNKNOWN_CLUSTER_UUID ;
18481846 indices = ImmutableOpenMap .builder (indexCountHint );
1849- aliasedIndices = ImmutableOpenMap .builder ();
18501847 templates = ImmutableOpenMap .builder ();
18511848 customs = ImmutableOpenMap .builder ();
18521849 reservedStateMetadata = new HashMap <>();
@@ -1861,7 +1858,6 @@ public Builder put(IndexMetadata.Builder indexMetadataBuilder) {
18611858 dedupeMapping (indexMetadataBuilder );
18621859 IndexMetadata indexMetadata = indexMetadataBuilder .build ();
18631860 IndexMetadata previous = indices .put (indexMetadata .getIndex ().getName (), indexMetadata );
1864- updateAliases (previous , indexMetadata );
18651861 if (unsetPreviousIndicesLookup (previous , indexMetadata )) {
18661862 previousIndicesLookup = null ;
18671863 }
@@ -1886,7 +1882,6 @@ public Builder put(IndexMetadata indexMetadata, boolean incrementVersion) {
18861882 return this ;
18871883 }
18881884 }
1889- updateAliases (previous , indexMetadata );
18901885 if (unsetPreviousIndicesLookup (previous , indexMetadata )) {
18911886 previousIndicesLookup = null ;
18921887 }
@@ -1961,8 +1956,7 @@ public IndexMetadata getSafe(Index index) {
19611956 public Builder remove (String index ) {
19621957 previousIndicesLookup = null ;
19631958 checkForUnusedMappings = true ;
1964- IndexMetadata previous = indices .remove (index );
1965- updateAliases (previous , null );
1959+ indices .remove (index );
19661960 return this ;
19671961 }
19681962
@@ -1972,7 +1966,6 @@ public Builder removeAllIndices() {
19721966
19731967 indices .clear ();
19741968 mappingsByHash .clear ();
1975- aliasedIndices .clear ();
19761969 return this ;
19771970 }
19781971
@@ -1983,67 +1976,6 @@ public Builder indices(Map<String, IndexMetadata> indices) {
19831976 return this ;
19841977 }
19851978
1986- void updateAliases (IndexMetadata previous , IndexMetadata current ) {
1987- if (previous == null && current != null ) {
1988- for (var key : current .getAliases ().keySet ()) {
1989- putAlias (key , current .getIndex ());
1990- }
1991- } else if (previous != null && current == null ) {
1992- for (var key : previous .getAliases ().keySet ()) {
1993- removeAlias (key , previous .getIndex ());
1994- }
1995- } else if (previous != null && current != null ) {
1996- if (Objects .equals (previous .getAliases (), current .getAliases ())) {
1997- return ;
1998- }
1999-
2000- for (var key : current .getAliases ().keySet ()) {
2001- if (previous .getAliases ().containsKey (key ) == false ) {
2002- putAlias (key , current .getIndex ());
2003- }
2004- }
2005- for (var key : previous .getAliases ().keySet ()) {
2006- if (current .getAliases ().containsKey (key ) == false ) {
2007- removeAlias (key , current .getIndex ());
2008- }
2009- }
2010- }
2011- }
2012-
2013- private Builder putAlias (String alias , Index index ) {
2014- Objects .requireNonNull (alias );
2015- Objects .requireNonNull (index );
2016-
2017- Set <Index > indices = new HashSet <>(aliasedIndices .getOrDefault (alias , Set .of ()));
2018- if (indices .add (index ) == false ) {
2019- return this ; // indices already contained this index
2020- }
2021- aliasedIndices .put (alias , Collections .unmodifiableSet (indices ));
2022- return this ;
2023- }
2024-
2025- private Builder removeAlias (String alias , Index index ) {
2026- Objects .requireNonNull (alias );
2027- Objects .requireNonNull (index );
2028-
2029- Set <Index > indices = aliasedIndices .get (alias );
2030- if (indices == null || indices .isEmpty ()) {
2031- throw new IllegalStateException ("Cannot remove non-existent alias [" + alias + "] for index [" + index .getName () + "]" );
2032- }
2033-
2034- indices = new HashSet <>(indices );
2035- if (indices .remove (index ) == false ) {
2036- throw new IllegalStateException ("Cannot remove non-existent alias [" + alias + "] for index [" + index .getName () + "]" );
2037- }
2038-
2039- if (indices .isEmpty ()) {
2040- aliasedIndices .remove (alias ); // for consistency, we don't store empty sets, so null it out
2041- } else {
2042- aliasedIndices .put (alias , Collections .unmodifiableSet (indices ));
2043- }
2044- return this ;
2045- }
2046-
20471979 public Builder put (IndexTemplateMetadata .Builder template ) {
20481980 return put (template .build ());
20491981 }
@@ -2365,6 +2297,7 @@ public Metadata build(boolean skipNameCollisionChecks) {
23652297 int totalNumberOfShards = 0 ;
23662298 int totalOpenIndexShards = 0 ;
23672299
2300+ ImmutableOpenMap .Builder <String , Set <Index >> aliasedIndicesBuilder = ImmutableOpenMap .builder ();
23682301 final String [] allIndicesArray = new String [indicesMap .size ()];
23692302 int i = 0 ;
23702303 final Set <String > sha256HashesInUse = checkForUnusedMappings ? Sets .newHashSetWithExpectedSize (mappingsByHash .size ()) : null ;
@@ -2396,9 +2329,19 @@ public Metadata build(boolean skipNameCollisionChecks) {
23962329 sha256HashesInUse .add (mapping .getSha256 ());
23972330 }
23982331 }
2332+ for (var alias : indexMetadata .getAliases ().keySet ()) {
2333+ var indices = aliasedIndicesBuilder .get (alias );
2334+ if (indices == null ) {
2335+ indices = new HashSet <>();
2336+ aliasedIndicesBuilder .put (alias , indices );
2337+ }
2338+ indices .add (indexMetadata .getIndex ());
2339+ }
23992340 }
2400-
2401- var aliasedIndices = this .aliasedIndices .build ();
2341+ for (String alias : aliasedIndicesBuilder .keys ()) {
2342+ aliasedIndicesBuilder .put (alias , Collections .unmodifiableSet (aliasedIndicesBuilder .get (alias )));
2343+ }
2344+ var aliasedIndices = aliasedIndicesBuilder .build ();
24022345 for (var entry : aliasedIndices .entrySet ()) {
24032346 List <IndexMetadata > aliasIndices = entry .getValue ().stream ().map (idx -> indicesMap .get (idx .getName ())).toList ();
24042347 validateAlias (entry .getKey (), aliasIndices );
0 commit comments