@@ -1790,7 +1790,6 @@ public static class Builder {
17901790 private DiffableStringMap hashesOfConsistentSettings = DiffableStringMap .EMPTY ;
17911791
17921792 private final ImmutableOpenMap .Builder <String , IndexMetadata > indices ;
1793- private final ImmutableOpenMap .Builder <String , Set <Index >> aliasedIndices ;
17941793 private final ImmutableOpenMap .Builder <String , IndexTemplateMetadata > templates ;
17951794 private final ImmutableOpenMap .Builder <String , Custom > customs ;
17961795
@@ -1820,7 +1819,6 @@ public Builder() {
18201819 this .hashesOfConsistentSettings = metadata .hashesOfConsistentSettings ;
18211820 this .version = metadata .version ;
18221821 this .indices = ImmutableOpenMap .builder (metadata .indices );
1823- this .aliasedIndices = ImmutableOpenMap .builder (metadata .aliasedIndices );
18241822 this .templates = ImmutableOpenMap .builder (metadata .templates );
18251823 this .customs = ImmutableOpenMap .builder (metadata .customs );
18261824 this .previousIndicesLookup = metadata .indicesLookup ;
@@ -1833,7 +1831,6 @@ public Builder() {
18331831 private Builder (Map <String , MappingMetadata > mappingsByHash , int indexCountHint ) {
18341832 clusterUUID = UNKNOWN_CLUSTER_UUID ;
18351833 indices = ImmutableOpenMap .builder (indexCountHint );
1836- aliasedIndices = ImmutableOpenMap .builder ();
18371834 templates = ImmutableOpenMap .builder ();
18381835 customs = ImmutableOpenMap .builder ();
18391836 reservedStateMetadata = new HashMap <>();
@@ -1848,7 +1845,6 @@ public Builder put(IndexMetadata.Builder indexMetadataBuilder) {
18481845 dedupeMapping (indexMetadataBuilder );
18491846 IndexMetadata indexMetadata = indexMetadataBuilder .build ();
18501847 IndexMetadata previous = indices .put (indexMetadata .getIndex ().getName (), indexMetadata );
1851- updateAliases (previous , indexMetadata );
18521848 if (unsetPreviousIndicesLookup (previous , indexMetadata )) {
18531849 previousIndicesLookup = null ;
18541850 }
@@ -1873,7 +1869,6 @@ public Builder put(IndexMetadata indexMetadata, boolean incrementVersion) {
18731869 return this ;
18741870 }
18751871 }
1876- updateAliases (previous , indexMetadata );
18771872 if (unsetPreviousIndicesLookup (previous , indexMetadata )) {
18781873 previousIndicesLookup = null ;
18791874 }
@@ -1948,8 +1943,7 @@ public IndexMetadata getSafe(Index index) {
19481943 public Builder remove (String index ) {
19491944 previousIndicesLookup = null ;
19501945 checkForUnusedMappings = true ;
1951- IndexMetadata previous = indices .remove (index );
1952- updateAliases (previous , null );
1946+ indices .remove (index );
19531947 return this ;
19541948 }
19551949
@@ -1959,7 +1953,6 @@ public Builder removeAllIndices() {
19591953
19601954 indices .clear ();
19611955 mappingsByHash .clear ();
1962- aliasedIndices .clear ();
19631956 return this ;
19641957 }
19651958
@@ -1970,67 +1963,6 @@ public Builder indices(Map<String, IndexMetadata> indices) {
19701963 return this ;
19711964 }
19721965
1973- void updateAliases (IndexMetadata previous , IndexMetadata current ) {
1974- if (previous == null && current != null ) {
1975- for (var key : current .getAliases ().keySet ()) {
1976- putAlias (key , current .getIndex ());
1977- }
1978- } else if (previous != null && current == null ) {
1979- for (var key : previous .getAliases ().keySet ()) {
1980- removeAlias (key , previous .getIndex ());
1981- }
1982- } else if (previous != null && current != null ) {
1983- if (Objects .equals (previous .getAliases (), current .getAliases ())) {
1984- return ;
1985- }
1986-
1987- for (var key : current .getAliases ().keySet ()) {
1988- if (previous .getAliases ().containsKey (key ) == false ) {
1989- putAlias (key , current .getIndex ());
1990- }
1991- }
1992- for (var key : previous .getAliases ().keySet ()) {
1993- if (current .getAliases ().containsKey (key ) == false ) {
1994- removeAlias (key , current .getIndex ());
1995- }
1996- }
1997- }
1998- }
1999-
2000- private Builder putAlias (String alias , Index index ) {
2001- Objects .requireNonNull (alias );
2002- Objects .requireNonNull (index );
2003-
2004- Set <Index > indices = new HashSet <>(aliasedIndices .getOrDefault (alias , Set .of ()));
2005- if (indices .add (index ) == false ) {
2006- return this ; // indices already contained this index
2007- }
2008- aliasedIndices .put (alias , Collections .unmodifiableSet (indices ));
2009- return this ;
2010- }
2011-
2012- private Builder removeAlias (String alias , Index index ) {
2013- Objects .requireNonNull (alias );
2014- Objects .requireNonNull (index );
2015-
2016- Set <Index > indices = aliasedIndices .get (alias );
2017- if (indices == null || indices .isEmpty ()) {
2018- throw new IllegalStateException ("Cannot remove non-existent alias [" + alias + "] for index [" + index .getName () + "]" );
2019- }
2020-
2021- indices = new HashSet <>(indices );
2022- if (indices .remove (index ) == false ) {
2023- throw new IllegalStateException ("Cannot remove non-existent alias [" + alias + "] for index [" + index .getName () + "]" );
2024- }
2025-
2026- if (indices .isEmpty ()) {
2027- aliasedIndices .remove (alias ); // for consistency, we don't store empty sets, so null it out
2028- } else {
2029- aliasedIndices .put (alias , Collections .unmodifiableSet (indices ));
2030- }
2031- return this ;
2032- }
2033-
20341966 public Builder put (IndexTemplateMetadata .Builder template ) {
20351967 return put (template .build ());
20361968 }
@@ -2352,6 +2284,7 @@ public Metadata build(boolean skipNameCollisionChecks) {
23522284 int totalNumberOfShards = 0 ;
23532285 int totalOpenIndexShards = 0 ;
23542286
2287+ ImmutableOpenMap .Builder <String , Set <Index >> aliasedIndicesBuilder = ImmutableOpenMap .builder ();
23552288 final String [] allIndicesArray = new String [indicesMap .size ()];
23562289 int i = 0 ;
23572290 final Set <String > sha256HashesInUse = checkForUnusedMappings ? Sets .newHashSetWithExpectedSize (mappingsByHash .size ()) : null ;
@@ -2383,9 +2316,19 @@ public Metadata build(boolean skipNameCollisionChecks) {
23832316 sha256HashesInUse .add (mapping .getSha256 ());
23842317 }
23852318 }
2319+ for (var alias : indexMetadata .getAliases ().keySet ()) {
2320+ var indices = aliasedIndicesBuilder .get (alias );
2321+ if (indices == null ) {
2322+ indices = new HashSet <>();
2323+ aliasedIndicesBuilder .put (alias , indices );
2324+ }
2325+ indices .add (indexMetadata .getIndex ());
2326+ }
23862327 }
2387-
2388- var aliasedIndices = this .aliasedIndices .build ();
2328+ for (String alias : aliasedIndicesBuilder .keys ()) {
2329+ aliasedIndicesBuilder .put (alias , Collections .unmodifiableSet (aliasedIndicesBuilder .get (alias )));
2330+ }
2331+ var aliasedIndices = aliasedIndicesBuilder .build ();
23892332 for (var entry : aliasedIndices .entrySet ()) {
23902333 List <IndexMetadata > aliasIndices = entry .getValue ().stream ().map (idx -> indicesMap .get (idx .getName ())).toList ();
23912334 validateAlias (entry .getKey (), aliasIndices );
0 commit comments