From 7d94cc99a75ccb89dd46ef560d4eb23a5c7b67af Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Tue, 19 Apr 2016 12:31:28 -0400 Subject: [PATCH] Remove more PROTOTYPEs from aggregations Relates to #17085 --- .../GeoDistanceAggregatorBuilder.java | 2 +- .../range/geodistance/GeoDistanceParser.java | 33 ++--- .../SignificantTermsAggregatorBuilder.java | 22 +-- .../bucket/terms/TermsAggregator.java | 42 +++--- .../bucket/terms/TermsAggregatorBuilder.java | 12 +- .../bucket/terms/support/IncludeExclude.java | 131 +++++++++--------- 6 files changed, 103 insertions(+), 139 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/GeoDistanceAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/GeoDistanceAggregatorBuilder.java index cadaab15b58..a72b4fd322e 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/GeoDistanceAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/GeoDistanceAggregatorBuilder.java @@ -75,7 +75,7 @@ public class GeoDistanceAggregatorBuilder extends ValuesSourceAggregatorBuilder< int size = in.readVInt(); ranges = new ArrayList<>(size); for (int i = 0; i < size; i++) { - ranges.add(Range.PROTOTYPE.readFrom(in)); + ranges.add(new Range(in)); } keyed = in.readBoolean(); distanceType = GeoDistance.readFromStream(in); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/GeoDistanceParser.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/GeoDistanceParser.java index bc4353d18b4..ed6d6a67e2a 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/GeoDistanceParser.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/geodistance/GeoDistanceParser.java @@ -54,13 +54,24 @@ public class GeoDistanceParser extends GeoPointValuesSourceParser { } public static class Range extends RangeAggregator.Range { - - static final Range PROTOTYPE = new Range(null, null, null); - public Range(String key, Double from, Double to) { super(key(key, from, to), from == null ? 0 : from, to); } + /** + * Read from a stream. + */ + public Range(StreamInput in) throws IOException { + super(in.readOptionalString(), in.readDouble(), in.readDouble()); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeOptionalString(key); + out.writeDouble(from); + out.writeDouble(to); + } + private static String key(String key, Double from, Double to) { if (key != null) { return key; @@ -71,22 +82,6 @@ public class GeoDistanceParser extends GeoPointValuesSourceParser { sb.append((to == null || Double.isInfinite(to)) ? "*" : to); return sb.toString(); } - - @Override - public Range readFrom(StreamInput in) throws IOException { - String key = in.readOptionalString(); - double from = in.readDouble(); - double to = in.readDouble(); - return new Range(key, from, to); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeOptionalString(key); - out.writeDouble(from); - out.writeDouble(to); - } - } @Override diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsAggregatorBuilder.java index 85dccd94aaf..373d757a22c 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsAggregatorBuilder.java @@ -71,14 +71,10 @@ public class SignificantTermsAggregatorBuilder extends ValuesSourceAggregatorBui */ public SignificantTermsAggregatorBuilder(StreamInput in) throws IOException { super(in, SignificantStringTerms.TYPE, ValuesSourceType.ANY); - bucketCountThresholds = BucketCountThresholds.readFromStream(in); + bucketCountThresholds = new BucketCountThresholds(in); executionHint = in.readOptionalString(); - if (in.readBoolean()) { - filterBuilder = in.readNamedWriteable(QueryBuilder.class); - } - if (in.readBoolean()) { - includeExclude = IncludeExclude.readFromStream(in); - } + filterBuilder = in.readOptionalNamedWriteable(QueryBuilder.class); + includeExclude = in.readOptionalWriteable(IncludeExclude::new); significanceHeuristic = in.readNamedWriteable(SignificanceHeuristic.class); } @@ -86,16 +82,8 @@ public class SignificantTermsAggregatorBuilder extends ValuesSourceAggregatorBui protected void innerWriteTo(StreamOutput out) throws IOException { bucketCountThresholds.writeTo(out); out.writeOptionalString(executionHint); - boolean hasfilterBuilder = filterBuilder != null; - out.writeBoolean(hasfilterBuilder); - if (hasfilterBuilder) { - out.writeNamedWriteable(filterBuilder); - } - boolean hasIncExc = includeExclude != null; - out.writeBoolean(hasIncExc); - if (hasIncExc) { - includeExclude.writeTo(out); - } + out.writeOptionalNamedWriteable(filterBuilder); + out.writeOptionalWriteable(includeExclude); out.writeNamedWriteable(significanceHeuristic); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregator.java index d332bbbca5c..d1a8fcba01a 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregator.java @@ -46,18 +46,11 @@ import java.util.Set; public abstract class TermsAggregator extends BucketsAggregator { public static class BucketCountThresholds implements Writeable, ToXContent { - - private static final BucketCountThresholds PROTOTYPE = new BucketCountThresholds(-1, -1, -1, -1); - private long minDocCount; private long shardMinDocCount; private int requiredSize; private int shardSize; - public static BucketCountThresholds readFromStream(StreamInput in) throws IOException { - return PROTOTYPE.readFrom(in); - } - public BucketCountThresholds(long minDocCount, long shardMinDocCount, int requiredSize, int shardSize) { this.minDocCount = minDocCount; this.shardMinDocCount = shardMinDocCount; @@ -65,6 +58,24 @@ public abstract class TermsAggregator extends BucketsAggregator { this.shardSize = shardSize; } + /** + * Read from a stream. + */ + public BucketCountThresholds(StreamInput in) throws IOException { + requiredSize = in.readInt(); + shardSize = in.readInt(); + minDocCount = in.readLong(); + shardMinDocCount = in.readLong(); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeInt(requiredSize); + out.writeInt(shardSize); + out.writeLong(minDocCount); + out.writeLong(shardMinDocCount); + } + public BucketCountThresholds(BucketCountThresholds bucketCountThresholds) { this(bucketCountThresholds.minDocCount, bucketCountThresholds.shardMinDocCount, bucketCountThresholds.requiredSize, bucketCountThresholds.shardSize); @@ -135,23 +146,6 @@ public abstract class TermsAggregator extends BucketsAggregator { return builder; } - @Override - public BucketCountThresholds readFrom(StreamInput in) throws IOException { - int requiredSize = in.readInt(); - int shardSize = in.readInt(); - long minDocCount = in.readLong(); - long shardMinDocCount = in.readLong(); - return new BucketCountThresholds(minDocCount, shardMinDocCount, requiredSize, shardSize); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeInt(requiredSize); - out.writeInt(shardSize); - out.writeLong(minDocCount); - out.writeLong(shardMinDocCount); - } - @Override public int hashCode() { return Objects.hash(requiredSize, shardSize, minDocCount, shardMinDocCount); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregatorBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregatorBuilder.java index a0efdc529a0..a905c4f5dac 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregatorBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregatorBuilder.java @@ -70,12 +70,10 @@ public class TermsAggregatorBuilder extends ValuesSourceAggregatorBuilder, ToXContent { - - private static final IncludeExclude PROTOTYPE = new IncludeExclude(Collections.emptySortedSet(), Collections.emptySortedSet()); private static final ParseField INCLUDE_FIELD = new ParseField("include"); private static final ParseField EXCLUDE_FIELD = new ParseField("exclude"); private static final ParseField PATTERN_FIELD = new ParseField("pattern"); - public static IncludeExclude readFromStream(StreamInput in) throws IOException { - return PROTOTYPE.readFrom(in); - } - // The includeValue and excludeValue ByteRefs which are the result of the parsing // process are converted into a LongFilter when used on numeric fields // in the index. @@ -257,6 +250,68 @@ public class IncludeExclude implements Writeable, ToXContent { this(convertToBytesRefSet(includeValues), convertToBytesRefSet(excludeValues)); } + /** + * Read from a stream. + */ + public IncludeExclude(StreamInput in) throws IOException { + if (in.readBoolean()) { + includeValues = null; + excludeValues = null; + String includeString = in.readOptionalString(); + include = includeString == null ? null : new RegExp(includeString); + String excludeString = in.readOptionalString(); + exclude = excludeString == null ? null : new RegExp(excludeString); + return; + } + include = null; + exclude = null; + if (in.readBoolean()) { + int size = in.readVInt(); + includeValues = new TreeSet<>(); + for (int i = 0; i < size; i++) { + includeValues.add(in.readBytesRef()); + } + } else { + includeValues = null; + } + if (in.readBoolean()) { + int size = in.readVInt(); + excludeValues = new TreeSet<>(); + for (int i = 0; i < size; i++) { + excludeValues.add(in.readBytesRef()); + } + } else { + excludeValues = null; + } + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + boolean regexBased = isRegexBased(); + out.writeBoolean(regexBased); + if (regexBased) { + out.writeOptionalString(include == null ? null : include.getOriginalString()); + out.writeOptionalString(exclude == null ? null : exclude.getOriginalString()); + } else { + boolean hasIncludes = includeValues != null; + out.writeBoolean(hasIncludes); + if (hasIncludes) { + out.writeVInt(includeValues.size()); + for (BytesRef value : includeValues) { + out.writeBytesRef(value); + } + } + boolean hasExcludes = excludeValues != null; + out.writeBoolean(hasExcludes); + if (hasExcludes) { + out.writeVInt(excludeValues.size()); + for (BytesRef value : excludeValues) { + out.writeBytesRef(value); + } + } + } + } + private static SortedSet convertToBytesRefSet(String[] values) { SortedSet returnSet = null; if (values != null) { @@ -555,68 +610,6 @@ public class IncludeExclude implements Writeable, ToXContent { return builder; } - @Override - public IncludeExclude readFrom(StreamInput in) throws IOException { - if (in.readBoolean()) { - String includeString = in.readOptionalString(); - RegExp include = null; - if (includeString != null) { - include = new RegExp(includeString); - } - String excludeString = in.readOptionalString(); - RegExp exclude = null; - if (excludeString != null) { - exclude = new RegExp(excludeString); - } - return new IncludeExclude(include, exclude); - } else { - SortedSet includes = null; - if (in.readBoolean()) { - int size = in.readVInt(); - includes = new TreeSet<>(); - for (int i = 0; i < size; i++) { - includes.add(in.readBytesRef()); - } - } - SortedSet excludes = null; - if (in.readBoolean()) { - int size = in.readVInt(); - excludes = new TreeSet<>(); - for (int i = 0; i < size; i++) { - excludes.add(in.readBytesRef()); - } - } - return new IncludeExclude(includes, excludes); - } - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - boolean regexBased = isRegexBased(); - out.writeBoolean(regexBased); - if (regexBased) { - out.writeOptionalString(include == null ? null : include.getOriginalString()); - out.writeOptionalString(exclude == null ? null : exclude.getOriginalString()); - } else { - boolean hasIncludes = includeValues != null; - out.writeBoolean(hasIncludes); - if (hasIncludes) { - out.writeVInt(includeValues.size()); - for (BytesRef value : includeValues) { - out.writeBytesRef(value); - } - } - boolean hasExcludes = excludeValues != null; - out.writeBoolean(hasExcludes); - if (hasExcludes) { - out.writeVInt(excludeValues.size()); - for (BytesRef value : excludeValues) { - out.writeBytesRef(value); - } - } - } - } - @Override public int hashCode() { return Objects.hash(include == null ? null : include.getOriginalString(), exclude == null ? null : exclude.getOriginalString(),