From 11a3201a092ed6c5d31516ae4b30dbb618ba348c Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Fri, 16 May 2014 15:58:04 +0200 Subject: [PATCH] Use EnumSet rather than static mutable arrays ClusterBlockLevel uses arrays but should use EnumSets instead --- .../cluster/block/ClusterBlock.java | 18 +++++++++++------- .../cluster/block/ClusterBlockLevel.java | 6 ++++-- .../cluster/metadata/IndexMetaData.java | 9 +++++---- .../cluster/metadata/MetaData.java | 2 +- .../org/elasticsearch/tribe/TribeService.java | 5 +++-- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/elasticsearch/cluster/block/ClusterBlock.java b/src/main/java/org/elasticsearch/cluster/block/ClusterBlock.java index 7e7634469c4..3cfba534798 100644 --- a/src/main/java/org/elasticsearch/cluster/block/ClusterBlock.java +++ b/src/main/java/org/elasticsearch/cluster/block/ClusterBlock.java @@ -28,6 +28,8 @@ import org.elasticsearch.rest.RestStatus; import java.io.IOException; import java.io.Serializable; +import java.util.ArrayList; +import java.util.EnumSet; import java.util.Locale; /** @@ -39,7 +41,7 @@ public class ClusterBlock implements Serializable, Streamable, ToXContent { private String description; - private ClusterBlockLevel[] levels; + private EnumSet levels; private boolean retryable; @@ -50,7 +52,7 @@ public class ClusterBlock implements Serializable, Streamable, ToXContent { ClusterBlock() { } - public ClusterBlock(int id, String description, boolean retryable, boolean disableStatePersistence, RestStatus status, ClusterBlockLevel... levels) { + public ClusterBlock(int id, String description, boolean retryable, boolean disableStatePersistence, RestStatus status, EnumSet levels) { this.id = id; this.description = description; this.retryable = retryable; @@ -71,7 +73,7 @@ public class ClusterBlock implements Serializable, Streamable, ToXContent { return this.status; } - public ClusterBlockLevel[] levels() { + public EnumSet levels() { return this.levels; } @@ -126,10 +128,12 @@ public class ClusterBlock implements Serializable, Streamable, ToXContent { public void readFrom(StreamInput in) throws IOException { id = in.readVInt(); description = in.readString(); - levels = new ClusterBlockLevel[in.readVInt()]; - for (int i = 0; i < levels.length; i++) { - levels[i] = ClusterBlockLevel.fromId(in.readVInt()); + final int len = in.readVInt(); + ArrayList levels = new ArrayList<>(); + for (int i = 0; i < len; i++) { + levels.add(ClusterBlockLevel.fromId(in.readVInt())); } + this.levels = EnumSet.copyOf(levels); retryable = in.readBoolean(); disableStatePersistence = in.readBoolean(); status = RestStatus.readFrom(in); @@ -139,7 +143,7 @@ public class ClusterBlock implements Serializable, Streamable, ToXContent { public void writeTo(StreamOutput out) throws IOException { out.writeVInt(id); out.writeString(description); - out.writeVInt(levels.length); + out.writeVInt(levels.size()); for (ClusterBlockLevel level : levels) { out.writeVInt(level.id()); } diff --git a/src/main/java/org/elasticsearch/cluster/block/ClusterBlockLevel.java b/src/main/java/org/elasticsearch/cluster/block/ClusterBlockLevel.java index b5c426e68e5..2c88da8986d 100644 --- a/src/main/java/org/elasticsearch/cluster/block/ClusterBlockLevel.java +++ b/src/main/java/org/elasticsearch/cluster/block/ClusterBlockLevel.java @@ -21,6 +21,8 @@ package org.elasticsearch.cluster.block; import org.elasticsearch.ElasticsearchIllegalArgumentException; +import java.util.EnumSet; + /** * */ @@ -29,8 +31,8 @@ public enum ClusterBlockLevel { WRITE(1), METADATA(2); - public static final ClusterBlockLevel[] ALL = new ClusterBlockLevel[]{READ, WRITE, METADATA}; - public static final ClusterBlockLevel[] READ_WRITE = new ClusterBlockLevel[]{READ, WRITE}; + public static final EnumSet ALL = EnumSet.of(READ, WRITE, METADATA); + public static final EnumSet READ_WRITE = EnumSet.of(READ, WRITE); private final int id; diff --git a/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java b/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java index 8fb3a592a1a..4aecfe07b9a 100644 --- a/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java +++ b/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java @@ -46,6 +46,7 @@ import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.warmer.IndexWarmersMetaData; import java.io.IOException; +import java.util.EnumSet; import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -113,10 +114,10 @@ public class IndexMetaData { return factory; } - public static final ClusterBlock INDEX_READ_ONLY_BLOCK = new ClusterBlock(5, "index read-only (api)", false, false, RestStatus.FORBIDDEN, ClusterBlockLevel.WRITE, ClusterBlockLevel.METADATA); - public static final ClusterBlock INDEX_READ_BLOCK = new ClusterBlock(7, "index read (api)", false, false, RestStatus.FORBIDDEN, ClusterBlockLevel.READ); - public static final ClusterBlock INDEX_WRITE_BLOCK = new ClusterBlock(8, "index write (api)", false, false, RestStatus.FORBIDDEN, ClusterBlockLevel.WRITE); - public static final ClusterBlock INDEX_METADATA_BLOCK = new ClusterBlock(9, "index metadata (api)", false, false, RestStatus.FORBIDDEN, ClusterBlockLevel.METADATA); + public static final ClusterBlock INDEX_READ_ONLY_BLOCK = new ClusterBlock(5, "index read-only (api)", false, false, RestStatus.FORBIDDEN, EnumSet.of(ClusterBlockLevel.WRITE, ClusterBlockLevel.METADATA)); + public static final ClusterBlock INDEX_READ_BLOCK = new ClusterBlock(7, "index read (api)", false, false, RestStatus.FORBIDDEN, EnumSet.of(ClusterBlockLevel.READ)); + public static final ClusterBlock INDEX_WRITE_BLOCK = new ClusterBlock(8, "index write (api)", false, false, RestStatus.FORBIDDEN, EnumSet.of(ClusterBlockLevel.WRITE)); + public static final ClusterBlock INDEX_METADATA_BLOCK = new ClusterBlock(9, "index metadata (api)", false, false, RestStatus.FORBIDDEN, EnumSet.of(ClusterBlockLevel.METADATA)); public static enum State { OPEN((byte) 0), diff --git a/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java b/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java index 1f2904cdfda..b1c89b101e2 100644 --- a/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java +++ b/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java @@ -111,7 +111,7 @@ public class MetaData implements Iterable { public static final String SETTING_READ_ONLY = "cluster.blocks.read_only"; - public static final ClusterBlock CLUSTER_READ_ONLY_BLOCK = new ClusterBlock(6, "cluster read-only (api)", false, false, RestStatus.FORBIDDEN, ClusterBlockLevel.WRITE, ClusterBlockLevel.METADATA); + public static final ClusterBlock CLUSTER_READ_ONLY_BLOCK = new ClusterBlock(6, "cluster read-only (api)", false, false, RestStatus.FORBIDDEN, EnumSet.of(ClusterBlockLevel.WRITE, ClusterBlockLevel.METADATA)); public static final MetaData EMPTY_META_DATA = builder().build(); diff --git a/src/main/java/org/elasticsearch/tribe/TribeService.java b/src/main/java/org/elasticsearch/tribe/TribeService.java index f1aedcb496c..42106219123 100644 --- a/src/main/java/org/elasticsearch/tribe/TribeService.java +++ b/src/main/java/org/elasticsearch/tribe/TribeService.java @@ -49,6 +49,7 @@ import org.elasticsearch.node.NodeBuilder; import org.elasticsearch.node.internal.InternalNode; import org.elasticsearch.rest.RestStatus; +import java.util.EnumSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -74,8 +75,8 @@ import java.util.concurrent.CountDownLatch; */ public class TribeService extends AbstractLifecycleComponent { - public static final ClusterBlock TRIBE_METADATA_BLOCK = new ClusterBlock(10, "tribe node, metadata not allowed", false, false, RestStatus.BAD_REQUEST, ClusterBlockLevel.METADATA); - public static final ClusterBlock TRIBE_WRITE_BLOCK = new ClusterBlock(11, "tribe node, write not allowed", false, false, RestStatus.BAD_REQUEST, ClusterBlockLevel.WRITE); + public static final ClusterBlock TRIBE_METADATA_BLOCK = new ClusterBlock(10, "tribe node, metadata not allowed", false, false, RestStatus.BAD_REQUEST, EnumSet.of(ClusterBlockLevel.METADATA)); + public static final ClusterBlock TRIBE_WRITE_BLOCK = new ClusterBlock(11, "tribe node, write not allowed", false, false, RestStatus.BAD_REQUEST, EnumSet.of(ClusterBlockLevel.WRITE)); public static Settings processSettings(Settings settings) { if (settings.get(TRIBE_NAME) != null) {