Skip to content

Commit

Permalink
Use EnumMap in ClusterBlocks (#29112)
Browse files Browse the repository at this point in the history
By using EnumMap instead of an ImmutableLevelHolder array we can avoid
the using enum ordinals to index into the array.
  • Loading branch information
Christoph Büscher committed Mar 22, 2018
1 parent e86c06a commit 10466d1
Showing 1 changed file with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.cluster.block;

import com.carrotsearch.hppc.cursors.ObjectObjectCursor;

import org.elasticsearch.cluster.AbstractDiffable;
import org.elasticsearch.cluster.Diff;
import org.elasticsearch.cluster.metadata.IndexMetaData;
Expand All @@ -30,6 +31,7 @@
import org.elasticsearch.rest.RestStatus;

import java.io.IOException;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -53,7 +55,7 @@ public class ClusterBlocks extends AbstractDiffable<ClusterBlocks> {

private final ImmutableOpenMap<String, Set<ClusterBlock>> indicesBlocks;

private final ImmutableLevelHolder[] levelHolders;
private final EnumMap<ClusterBlockLevel, ImmutableLevelHolder> levelHolders;

ClusterBlocks(Set<ClusterBlock> global, ImmutableOpenMap<String, Set<ClusterBlock>> indicesBlocks) {
this.global = global;
Expand All @@ -70,20 +72,20 @@ public ImmutableOpenMap<String, Set<ClusterBlock>> indices() {
}

public Set<ClusterBlock> global(ClusterBlockLevel level) {
return levelHolders[level.ordinal()].global();
return levelHolders.get(level).global();
}

public ImmutableOpenMap<String, Set<ClusterBlock>> indices(ClusterBlockLevel level) {
return levelHolders[level.ordinal()].indices();
return levelHolders.get(level).indices();
}

private Set<ClusterBlock> blocksForIndex(ClusterBlockLevel level, String index) {
return indices(level).getOrDefault(index, emptySet());
}

private static ImmutableLevelHolder[] generateLevelHolders(Set<ClusterBlock> global,
ImmutableOpenMap<String, Set<ClusterBlock>> indicesBlocks) {
ImmutableLevelHolder[] levelHolders = new ImmutableLevelHolder[ClusterBlockLevel.values().length];
private static EnumMap<ClusterBlockLevel, ImmutableLevelHolder> generateLevelHolders(Set<ClusterBlock> global,
ImmutableOpenMap<String, Set<ClusterBlock>> indicesBlocks) {
EnumMap<ClusterBlockLevel, ImmutableLevelHolder> levelHolders = new EnumMap<>(ClusterBlockLevel.class);
for (final ClusterBlockLevel level : ClusterBlockLevel.values()) {
Predicate<ClusterBlock> containsLevel = block -> block.contains(level);
Set<ClusterBlock> newGlobal = unmodifiableSet(global.stream()
Expand All @@ -96,8 +98,7 @@ private static ImmutableLevelHolder[] generateLevelHolders(Set<ClusterBlock> glo
.filter(containsLevel)
.collect(toSet())));
}

levelHolders[level.ordinal()] = new ImmutableLevelHolder(newGlobal, indicesBuilder.build());
levelHolders.put(level, new ImmutableLevelHolder(newGlobal, indicesBuilder.build()));
}
return levelHolders;
}
Expand Down

0 comments on commit 10466d1

Please sign in to comment.