From 7e77ddb88feab4069f0f11b0decaa9d47e6bc456 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Mon, 8 Apr 2013 15:11:31 +0200 Subject: [PATCH] use enum to represent flags and fail if flags are not respected --- .../admin/indices/stats/CommonStatsFlags.java | 234 +++++------------- .../indices/stats/IndicesStatsRequest.java | 54 ++-- .../indices/InternalIndicesService.java | 122 ++++++--- .../node/stats/RestNodesStatsAction.java | 82 +----- .../indices/stats/SimpleIndexStatsTests.java | 218 ++++++++++++++++ 5 files changed, 414 insertions(+), 296 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/admin/indices/stats/CommonStatsFlags.java b/src/main/java/org/elasticsearch/action/admin/indices/stats/CommonStatsFlags.java index ae77845e5d5..10defa43c57 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/stats/CommonStatsFlags.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/stats/CommonStatsFlags.java @@ -24,23 +24,12 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Streamable; import java.io.IOException; +import java.util.EnumSet; /** */ public class CommonStatsFlags implements Streamable { - - private boolean docs = true; - private boolean store = true; - private boolean indexing = true; - private boolean get = true; - private boolean search = true; - private boolean merge = false; - private boolean refresh = false; - private boolean flush = false; - private boolean warmer = false; - private boolean filterCache = false; - private boolean idCache = false; - private boolean fieldData = false; + private EnumSet flags = EnumSet.of(Flag.Docs, Flag.Store, Flag.Indexing, Flag.Get, Flag.Search); private String[] types = null; private String[] groups = null; @@ -48,18 +37,7 @@ public class CommonStatsFlags implements Streamable { * Sets all flags to return all stats. */ public CommonStatsFlags all() { - docs = true; - store = true; - get = true; - indexing = true; - search = true; - merge = true; - refresh = true; - flush = true; - warmer = true; - filterCache = true; - idCache = true; - fieldData = true; + flags = EnumSet.allOf(Flag.class); types = null; groups = null; return this; @@ -69,25 +47,18 @@ public class CommonStatsFlags implements Streamable { * Clears all stats. */ public CommonStatsFlags clear() { - docs = false; - store = false; - get = false; - indexing = false; - search = false; - merge = false; - refresh = false; - flush = false; - warmer = false; - filterCache = false; - idCache = false; - fieldData = false; + flags = EnumSet.noneOf(Flag.class); types = null; groups = null; return this; } public boolean anySet() { - return docs || store || get || indexing || search || merge || refresh || flush || warmer || filterCache || idCache || fieldData; + return !flags.isEmpty(); + } + + public Flag[] getFlags() { + return flags.toArray(new Flag[flags.size()]); } /** @@ -119,115 +90,28 @@ public class CommonStatsFlags implements Streamable { public String[] groups() { return this.groups; } - - public CommonStatsFlags docs(boolean docs) { - this.docs = docs; + + public boolean isSet(Flag flag) { + return flags.contains(flag); + } + + boolean unSet(Flag flag) { + return flags.remove(flag); + } + + void set(Flag flag) { + flags.add(flag); + } + + public CommonStatsFlags set(Flag flag, boolean add) { + if (add) { + set(flag); + } else { + unSet(flag); + } return this; } - public boolean docs() { - return this.docs; - } - - public CommonStatsFlags store(boolean store) { - this.store = store; - return this; - } - - public boolean store() { - return this.store; - } - - public CommonStatsFlags indexing(boolean indexing) { - this.indexing = indexing; - return this; - } - - public boolean indexing() { - return this.indexing; - } - - public CommonStatsFlags get(boolean get) { - this.get = get; - return this; - } - - public boolean get() { - return this.get; - } - - public CommonStatsFlags search(boolean search) { - this.search = search; - return this; - } - - public boolean search() { - return this.search; - } - - public CommonStatsFlags merge(boolean merge) { - this.merge = merge; - return this; - } - - public boolean merge() { - return this.merge; - } - - public CommonStatsFlags refresh(boolean refresh) { - this.refresh = refresh; - return this; - } - - public boolean refresh() { - return this.refresh; - } - - public CommonStatsFlags flush(boolean flush) { - this.flush = flush; - return this; - } - - public boolean flush() { - return this.flush; - } - - public CommonStatsFlags warmer(boolean warmer) { - this.warmer = warmer; - return this; - } - - public boolean warmer() { - return this.warmer; - } - - public CommonStatsFlags filterCache(boolean filterCache) { - this.filterCache = filterCache; - return this; - } - - public boolean filterCache() { - return this.filterCache; - } - - public CommonStatsFlags idCache(boolean idCache) { - this.idCache = idCache; - return this; - } - - public boolean idCache() { - return this.idCache; - } - - public CommonStatsFlags fieldData(boolean fieldData) { - this.fieldData = fieldData; - return this; - } - - public boolean fieldData() { - return this.fieldData; - } - public static CommonStatsFlags readCommonStatsFlags(StreamInput in) throws IOException { CommonStatsFlags flags = new CommonStatsFlags(); flags.readFrom(in); @@ -236,18 +120,11 @@ public class CommonStatsFlags implements Streamable { @Override public void writeTo(StreamOutput out) throws IOException { - out.writeBoolean(docs); - out.writeBoolean(store); - out.writeBoolean(indexing); - out.writeBoolean(get); - out.writeBoolean(search); - out.writeBoolean(merge); - out.writeBoolean(flush); - out.writeBoolean(refresh); - out.writeBoolean(warmer); - out.writeBoolean(filterCache); - out.writeBoolean(idCache); - out.writeBoolean(fieldData); + long longFlags = 0; + for (Flag flag : flags) { + longFlags |= (1 << flag.ordinal()); + } + out.writeLong(longFlags); if (types == null) { out.writeVInt(0); } else { @@ -268,18 +145,13 @@ public class CommonStatsFlags implements Streamable { @Override public void readFrom(StreamInput in) throws IOException { - docs = in.readBoolean(); - store = in.readBoolean(); - indexing = in.readBoolean(); - get = in.readBoolean(); - search = in.readBoolean(); - merge = in.readBoolean(); - flush = in.readBoolean(); - refresh = in.readBoolean(); - warmer = in.readBoolean(); - filterCache = in.readBoolean(); - idCache = in.readBoolean(); - fieldData = in.readBoolean(); + final long longFlags = in.readLong(); + flags.clear(); + for(Flag flag : Flag.values()) { + if ((longFlags & (1 << flag.ordinal())) != 0) { + flags.add(flag); + } + } int size = in.readVInt(); if (size > 0) { types = new String[size]; @@ -295,4 +167,30 @@ public class CommonStatsFlags implements Streamable { } } } + + public static enum Flag { + Store("store"), + Indexing("indexing"), + Get("get"), + Search("search"), + Merge("merge"), + Flush("flush"), + Refresh("refresh"), + FilterCache("filter_cache"), + IdCache("id_cache"), + FieldData("fielddata"), + Docs("docs"), + Warmer("warmer"); + + private final String restName; + + Flag(String restName) { + this.restName = restName; + } + + public String getRestName() { + return restName; + } + + } } diff --git a/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsRequest.java b/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsRequest.java index 8127d26af57..cee162b5f0b 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsRequest.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsRequest.java @@ -19,12 +19,13 @@ package org.elasticsearch.action.admin.indices.stats; +import java.io.IOException; + +import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag; import org.elasticsearch.action.support.broadcast.BroadcastOperationRequest; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import java.io.IOException; - /** * A request to get indices level stats. Allow to enable different stats to be returned. *

@@ -85,111 +86,112 @@ public class IndicesStatsRequest extends BroadcastOperationRequest flags = EnumSet.noneOf(Flag.class); + for (Flag flag : values) { + if (random.nextBoolean()) { + flags.add(flag); + } + } + + + for (Flag flag : values) { + set(flag, builder, false); // clear all + } + + for (Flag flag : flags) { // set the flags + set(flag, builder, true); + } + stats = builder.execute().actionGet(); + for (Flag flag : flags) { // check the flags + assertThat(isSet(flag, stats.getPrimaries()), equalTo(true)); + assertThat(isSet(flag, stats.getTotal()), equalTo(true)); + } + + for (Flag flag : EnumSet.complementOf(flags)) { // check the complement + assertThat(isSet(flag, stats.getPrimaries()), equalTo(false)); + assertThat(isSet(flag, stats.getTotal()), equalTo(false)); + } + + } + + @Test + public void testEncodeDecodeCommonStats() throws IOException { + CommonStatsFlags flags = new CommonStatsFlags(); + Flag[] values = CommonStatsFlags.Flag.values(); + assertThat(flags.anySet(), equalTo(true)); + + for (Flag flag : values) { + flags.set(flag, false); + } + assertThat(flags.anySet(), equalTo(false)); + for (Flag flag : values) { + flags.set(flag, true); + } + assertThat(flags.anySet(), equalTo(true)); + long seed = System.currentTimeMillis(); + System.out.println("seed: " + seed); + Random random = new Random(seed); + flags.set(values[random.nextInt(values.length)], false); + assertThat(flags.anySet(), equalTo(true)); + + { + BytesStreamOutput out = new BytesStreamOutput(); + flags.writeTo(out); + out.close(); + BytesReference bytes = out.bytes(); + CommonStatsFlags readStats = CommonStatsFlags.readCommonStatsFlags(new BytesStreamInput(bytes)); + for (Flag flag : values) { + assertThat(flags.isSet(flag), equalTo(readStats.isSet(flag))); + } + } + + { + for (Flag flag : values) { + flags.set(flag, random.nextBoolean()); + } + BytesStreamOutput out = new BytesStreamOutput(); + flags.writeTo(out); + out.close(); + BytesReference bytes = out.bytes(); + CommonStatsFlags readStats = CommonStatsFlags.readCommonStatsFlags(new BytesStreamInput(bytes)); + for (Flag flag : values) { + assertThat(flags.isSet(flag), equalTo(readStats.isSet(flag))); + } + } + } + + private static void set(Flag flag, IndicesStatsRequestBuilder builder, boolean set) { + switch(flag) { + case Docs: + builder.setDocs(set); + break; + case FieldData: + builder.setFieldData(set); + break; + case FilterCache: + builder.setFilterCache(set); + break; + case Flush: + builder.setFlush(set); + break; + case Get: + builder.setGet(set); + break; + case IdCache: + builder.setIdCache(set); + break; + case Indexing: + builder.setIndexing(set); + break; + case Merge: + builder.setMerge(set); + break; + case Refresh: + builder.setRefresh(set); + break; + case Search: + builder.setSearch(set); + break; + case Store: + builder.setStore(set); + break; + case Warmer: + builder.setWarmer(set); + break; + default: + assert false : "new flag? " + flag; + break; + } + } + + private static boolean isSet(Flag flag, CommonStats response) { + switch(flag) { + case Docs: + return response.getDocs() != null; + case FieldData: + return response.getFieldData() != null; + case FilterCache: + return response.getFilterCache() != null; + case Flush: + return response.getFlush() != null; + case Get: + return response.getGet() != null; + case IdCache: + return response.getIdCache() != null; + case Indexing: + return response.getIndexing() != null; + case Merge: + return response.getMerge() != null; + case Refresh: + return response.getRefresh() != null; + case Search: + return response.getSearch() != null; + case Store: + return response.getStore() != null; + case Warmer: + return response.getWarmer() != null; + default: + assert false : "new flag? " + flag; + return false; + } + } + }