Skip to content

Commit

Permalink
Use EnumSet rather than static mutable arrays
Browse files Browse the repository at this point in the history
ClusterBlockLevel uses arrays but should use EnumSets instead
  • Loading branch information
s1monw committed May 16, 2014
1 parent d65e9e9 commit 11a3201
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
18 changes: 11 additions & 7 deletions src/main/java/org/elasticsearch/cluster/block/ClusterBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.Locale;

/**
Expand All @@ -39,7 +41,7 @@ public class ClusterBlock implements Serializable, Streamable, ToXContent {

private String description;

private ClusterBlockLevel[] levels;
private EnumSet<ClusterBlockLevel> levels;

private boolean retryable;

Expand All @@ -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;
Expand All @@ -71,7 +73,7 @@ public RestStatus status() {
return this.status;
}

public ClusterBlockLevel[] levels() {
public EnumSet<ClusterBlockLevel> levels() {
return this.levels;
}

Expand Down Expand Up @@ -126,10 +128,12 @@ public static ClusterBlock readClusterBlock(StreamInput in) throws IOException {
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);
Expand All @@ -139,7 +143,7 @@ public void readFrom(StreamInput in) throws IOException {
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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import org.elasticsearch.ElasticsearchIllegalArgumentException;

import java.util.EnumSet;

/**
*
*/
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
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;
Expand Down Expand Up @@ -113,10 +114,10 @@ public static <T extends Custom> Custom.Factory<T> lookupFactorySafe(String type
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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static <T extends Custom> Custom.Factory<T> lookupFactorySafe(String type

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();

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/elasticsearch/tribe/TribeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
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;
Expand All @@ -74,8 +75,8 @@
*/
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) {
Expand Down

0 comments on commit 11a3201

Please sign in to comment.