Use EnumSet rather than static mutable arrays

ClusterBlockLevel uses arrays but should use EnumSets instead
This commit is contained in:
Simon Willnauer 2014-05-16 15:58:04 +02:00
parent d65e9e9bea
commit 11a3201a09
5 changed files with 24 additions and 16 deletions

View File

@ -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<ClusterBlockLevel> 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<ClusterBlockLevel> 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<ClusterBlockLevel> 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<ClusterBlockLevel> 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());
}

View File

@ -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<ClusterBlockLevel> ALL = EnumSet.of(READ, WRITE, METADATA);
public static final EnumSet<ClusterBlockLevel> READ_WRITE = EnumSet.of(READ, WRITE);
private final int id;

View File

@ -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),

View File

@ -111,7 +111,7 @@ public class MetaData implements Iterable<IndexMetaData> {
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();

View File

@ -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<TribeService> {
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) {