Skip to content

Commit a79c5bd

Browse files
authored
Minor clean-up in InternalRange. (#30886)
* Make sure all instance variables are final. * Make generateKey a private static method, instead of protected. * Rename formatter -> format for consistency. * Serialize bucket keys as strings as opposed to optional strings. * Pull the stream serialization logic for buckets into the Bucket class.
1 parent 116d083 commit a79c5bd

File tree

3 files changed

+36
-40
lines changed

3 files changed

+36
-40
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalDateRange.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ public class InternalDateRange extends InternalRange<InternalDateRange.Bucket, I
3636

3737
public static class Bucket extends InternalRange.Bucket {
3838

39-
public Bucket(boolean keyed, DocValueFormat formatter) {
40-
super(keyed, formatter);
41-
}
42-
4339
public Bucket(String key, double from, double to, long docCount, List<InternalAggregation> aggregations, boolean keyed,
4440
DocValueFormat formatter) {
4541
super(key, from, to, docCount, new InternalAggregations(aggregations), keyed, formatter);

server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalGeoDistance.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ public class InternalGeoDistance extends InternalRange<InternalGeoDistance.Bucke
3535

3636
static class Bucket extends InternalRange.Bucket {
3737

38-
Bucket(boolean keyed) {
39-
super(keyed, DocValueFormat.RAW);
40-
}
41-
4238
Bucket(String key, double from, double to, long docCount, List<InternalAggregation> aggregations, boolean keyed) {
4339
this(key, from, to, docCount, new InternalAggregations(aggregations), keyed);
4440
}

server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalRange.java

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.elasticsearch.search.aggregations.bucket.range;
2020

21+
import org.elasticsearch.Version;
2122
import org.elasticsearch.common.io.stream.StreamInput;
2223
import org.elasticsearch.common.io.stream.StreamOutput;
2324
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -44,21 +45,17 @@ public static class Bucket extends InternalMultiBucketAggregation.InternalBucket
4445

4546
protected final transient boolean keyed;
4647
protected final transient DocValueFormat format;
47-
protected double from;
48-
protected double to;
49-
private long docCount;
50-
InternalAggregations aggregations;
51-
private String key;
52-
53-
public Bucket(boolean keyed, DocValueFormat formatter) {
54-
this.keyed = keyed;
55-
this.format = formatter;
56-
}
48+
protected final double from;
49+
protected final double to;
50+
private final long docCount;
51+
private final InternalAggregations aggregations;
52+
private final String key;
5753

5854
public Bucket(String key, double from, double to, long docCount, InternalAggregations aggregations, boolean keyed,
59-
DocValueFormat formatter) {
60-
this(keyed, formatter);
61-
this.key = key != null ? key : generateKey(from, to, formatter);
55+
DocValueFormat format) {
56+
this.keyed = keyed;
57+
this.format = format;
58+
this.key = key != null ? key : generateKey(from, to, format);
6259
this.from = from;
6360
this.to = to;
6461
this.docCount = docCount;
@@ -162,16 +159,25 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
162159
return builder;
163160
}
164161

165-
protected String generateKey(double from, double to, DocValueFormat formatter) {
166-
StringBuilder sb = new StringBuilder();
167-
sb.append(Double.isInfinite(from) ? "*" : formatter.format(from));
168-
sb.append("-");
169-
sb.append(Double.isInfinite(to) ? "*" : formatter.format(to));
170-
return sb.toString();
162+
private static String generateKey(double from, double to, DocValueFormat format) {
163+
StringBuilder builder = new StringBuilder()
164+
.append(Double.isInfinite(from) ? "*" : format.format(from))
165+
.append("-")
166+
.append(Double.isInfinite(to) ? "*" : format.format(to));
167+
return builder.toString();
171168
}
172169

173170
@Override
174171
public void writeTo(StreamOutput out) throws IOException {
172+
if (out.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) {
173+
out.writeString(key);
174+
} else {
175+
out.writeOptionalString(key);
176+
}
177+
out.writeDouble(from);
178+
out.writeDouble(to);
179+
out.writeVLong(docCount);
180+
aggregations.writeTo(out);
175181
}
176182

177183
@Override
@@ -206,15 +212,15 @@ public ValueType getValueType() {
206212
}
207213

208214
@SuppressWarnings("unchecked")
209-
public R create(String name, List<B> ranges, DocValueFormat formatter, boolean keyed, List<PipelineAggregator> pipelineAggregators,
215+
public R create(String name, List<B> ranges, DocValueFormat format, boolean keyed, List<PipelineAggregator> pipelineAggregators,
210216
Map<String, Object> metaData) {
211-
return (R) new InternalRange<B, R>(name, ranges, formatter, keyed, pipelineAggregators, metaData);
217+
return (R) new InternalRange<B, R>(name, ranges, format, keyed, pipelineAggregators, metaData);
212218
}
213219

214220
@SuppressWarnings("unchecked")
215221
public B createBucket(String key, double from, double to, long docCount, InternalAggregations aggregations, boolean keyed,
216-
DocValueFormat formatter) {
217-
return (B) new Bucket(key, from, to, docCount, aggregations, keyed, formatter);
222+
DocValueFormat format) {
223+
return (B) new Bucket(key, from, to, docCount, aggregations, keyed, format);
218224
}
219225

220226
@SuppressWarnings("unchecked")
@@ -230,9 +236,9 @@ public B createBucket(InternalAggregations aggregations, B prototype) {
230236
}
231237
}
232238

233-
private List<B> ranges;
234-
protected DocValueFormat format;
235-
protected boolean keyed;
239+
private final List<B> ranges;
240+
protected final DocValueFormat format;
241+
protected final boolean keyed;
236242

237243
public InternalRange(String name, List<B> ranges, DocValueFormat format, boolean keyed,
238244
List<PipelineAggregator> pipelineAggregators,
@@ -253,7 +259,9 @@ public InternalRange(StreamInput in) throws IOException {
253259
int size = in.readVInt();
254260
List<B> ranges = new ArrayList<>(size);
255261
for (int i = 0; i < size; i++) {
256-
String key = in.readOptionalString();
262+
String key = in.getVersion().onOrAfter(Version.V_7_0_0_alpha1)
263+
? in.readString()
264+
: in.readOptionalString();
257265
ranges.add(getFactory().createBucket(key, in.readDouble(), in.readDouble(), in.readVLong(),
258266
InternalAggregations.readAggregations(in), keyed, format));
259267
}
@@ -266,11 +274,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
266274
out.writeBoolean(keyed);
267275
out.writeVInt(ranges.size());
268276
for (B bucket : ranges) {
269-
out.writeOptionalString(((Bucket) bucket).key);
270-
out.writeDouble(bucket.from);
271-
out.writeDouble(bucket.to);
272-
out.writeVLong(((Bucket) bucket).docCount);
273-
bucket.aggregations.writeTo(out);
277+
bucket.writeTo(out);
274278
}
275279
}
276280

0 commit comments

Comments
 (0)