@@ -1796,7 +1796,6 @@ public static class Builder {
17961796 private DiffableStringMap hashesOfConsistentSettings = DiffableStringMap .EMPTY ;
17971797
17981798 private final ImmutableOpenMap .Builder <String , IndexMetadata > indices ;
1799- private final ImmutableOpenMap .Builder <String , Set <Index >> aliasedIndices ;
18001799 private final ImmutableOpenMap .Builder <String , IndexTemplateMetadata > templates ;
18011800 private final ImmutableOpenMap .Builder <String , Custom > customs ;
18021801
@@ -1826,7 +1825,6 @@ public Builder() {
18261825 this .hashesOfConsistentSettings = metadata .hashesOfConsistentSettings ;
18271826 this .version = metadata .version ;
18281827 this .indices = ImmutableOpenMap .builder (metadata .indices );
1829- this .aliasedIndices = ImmutableOpenMap .builder (metadata .aliasedIndices );
18301828 this .templates = ImmutableOpenMap .builder (metadata .templates );
18311829 this .customs = ImmutableOpenMap .builder (metadata .customs );
18321830 this .previousIndicesLookup = metadata .indicesLookup ;
@@ -1839,7 +1837,6 @@ public Builder() {
18391837 private Builder (Map <String , MappingMetadata > mappingsByHash , int indexCountHint ) {
18401838 clusterUUID = UNKNOWN_CLUSTER_UUID ;
18411839 indices = ImmutableOpenMap .builder (indexCountHint );
1842- aliasedIndices = ImmutableOpenMap .builder ();
18431840 templates = ImmutableOpenMap .builder ();
18441841 customs = ImmutableOpenMap .builder ();
18451842 reservedStateMetadata = new HashMap <>();
@@ -1854,7 +1851,6 @@ public Builder put(IndexMetadata.Builder indexMetadataBuilder) {
18541851 dedupeMapping (indexMetadataBuilder );
18551852 IndexMetadata indexMetadata = indexMetadataBuilder .build ();
18561853 IndexMetadata previous = indices .put (indexMetadata .getIndex ().getName (), indexMetadata );
1857- updateAliases (previous , indexMetadata );
18581854 if (unsetPreviousIndicesLookup (previous , indexMetadata )) {
18591855 previousIndicesLookup = null ;
18601856 }
@@ -1879,7 +1875,6 @@ public Builder put(IndexMetadata indexMetadata, boolean incrementVersion) {
18791875 return this ;
18801876 }
18811877 }
1882- updateAliases (previous , indexMetadata );
18831878 if (unsetPreviousIndicesLookup (previous , indexMetadata )) {
18841879 previousIndicesLookup = null ;
18851880 }
@@ -1954,8 +1949,7 @@ public IndexMetadata getSafe(Index index) {
19541949 public Builder remove (String index ) {
19551950 previousIndicesLookup = null ;
19561951 checkForUnusedMappings = true ;
1957- IndexMetadata previous = indices .remove (index );
1958- updateAliases (previous , null );
1952+ indices .remove (index );
19591953 return this ;
19601954 }
19611955
@@ -1965,7 +1959,6 @@ public Builder removeAllIndices() {
19651959
19661960 indices .clear ();
19671961 mappingsByHash .clear ();
1968- aliasedIndices .clear ();
19691962 return this ;
19701963 }
19711964
@@ -1976,67 +1969,6 @@ public Builder indices(Map<String, IndexMetadata> indices) {
19761969 return this ;
19771970 }
19781971
1979- void updateAliases (IndexMetadata previous , IndexMetadata current ) {
1980- if (previous == null && current != null ) {
1981- for (var key : current .getAliases ().keySet ()) {
1982- putAlias (key , current .getIndex ());
1983- }
1984- } else if (previous != null && current == null ) {
1985- for (var key : previous .getAliases ().keySet ()) {
1986- removeAlias (key , previous .getIndex ());
1987- }
1988- } else if (previous != null && current != null ) {
1989- if (Objects .equals (previous .getAliases (), current .getAliases ())) {
1990- return ;
1991- }
1992-
1993- for (var key : current .getAliases ().keySet ()) {
1994- if (previous .getAliases ().containsKey (key ) == false ) {
1995- putAlias (key , current .getIndex ());
1996- }
1997- }
1998- for (var key : previous .getAliases ().keySet ()) {
1999- if (current .getAliases ().containsKey (key ) == false ) {
2000- removeAlias (key , current .getIndex ());
2001- }
2002- }
2003- }
2004- }
2005-
2006- private Builder putAlias (String alias , Index index ) {
2007- Objects .requireNonNull (alias );
2008- Objects .requireNonNull (index );
2009-
2010- Set <Index > indices = new HashSet <>(aliasedIndices .getOrDefault (alias , Set .of ()));
2011- if (indices .add (index ) == false ) {
2012- return this ; // indices already contained this index
2013- }
2014- aliasedIndices .put (alias , Collections .unmodifiableSet (indices ));
2015- return this ;
2016- }
2017-
2018- private Builder removeAlias (String alias , Index index ) {
2019- Objects .requireNonNull (alias );
2020- Objects .requireNonNull (index );
2021-
2022- Set <Index > indices = aliasedIndices .get (alias );
2023- if (indices == null || indices .isEmpty ()) {
2024- throw new IllegalStateException ("Cannot remove non-existent alias [" + alias + "] for index [" + index .getName () + "]" );
2025- }
2026-
2027- indices = new HashSet <>(indices );
2028- if (indices .remove (index ) == false ) {
2029- throw new IllegalStateException ("Cannot remove non-existent alias [" + alias + "] for index [" + index .getName () + "]" );
2030- }
2031-
2032- if (indices .isEmpty ()) {
2033- aliasedIndices .remove (alias ); // for consistency, we don't store empty sets, so null it out
2034- } else {
2035- aliasedIndices .put (alias , Collections .unmodifiableSet (indices ));
2036- }
2037- return this ;
2038- }
2039-
20401972 public Builder put (IndexTemplateMetadata .Builder template ) {
20411973 return put (template .build ());
20421974 }
@@ -2358,6 +2290,7 @@ public Metadata build(boolean skipNameCollisionChecks) {
23582290 int totalNumberOfShards = 0 ;
23592291 int totalOpenIndexShards = 0 ;
23602292
2293+ ImmutableOpenMap .Builder <String , Set <Index >> aliasedIndicesBuilder = ImmutableOpenMap .builder ();
23612294 final String [] allIndicesArray = new String [indicesMap .size ()];
23622295 int i = 0 ;
23632296 final Set <String > sha256HashesInUse = checkForUnusedMappings ? Sets .newHashSetWithExpectedSize (mappingsByHash .size ()) : null ;
@@ -2389,9 +2322,19 @@ public Metadata build(boolean skipNameCollisionChecks) {
23892322 sha256HashesInUse .add (mapping .getSha256 ());
23902323 }
23912324 }
2325+ for (var alias : indexMetadata .getAliases ().keySet ()) {
2326+ var indices = aliasedIndicesBuilder .get (alias );
2327+ if (indices == null ) {
2328+ indices = new HashSet <>();
2329+ aliasedIndicesBuilder .put (alias , indices );
2330+ }
2331+ indices .add (indexMetadata .getIndex ());
2332+ }
23922333 }
2393-
2394- var aliasedIndices = this .aliasedIndices .build ();
2334+ for (String alias : aliasedIndicesBuilder .keys ()) {
2335+ aliasedIndicesBuilder .put (alias , Collections .unmodifiableSet (aliasedIndicesBuilder .get (alias )));
2336+ }
2337+ var aliasedIndices = aliasedIndicesBuilder .build ();
23952338 for (var entry : aliasedIndices .entrySet ()) {
23962339 List <IndexMetadata > aliasIndices = entry .getValue ().stream ().map (idx -> indicesMap .get (idx .getName ())).toList ();
23972340 validateAlias (entry .getKey (), aliasIndices );
0 commit comments