Skip to content

Commit 3eb8575

Browse files
authored
Move InternalAggregations to Writeable (#41841)
Relates to #34389
1 parent 8d01747 commit 3eb8575

18 files changed

+46
-59
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/Aggregations.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.Map;
3535
import java.util.Objects;
3636

37+
import static java.util.Collections.emptyMap;
3738
import static java.util.Collections.unmodifiableMap;
3839
import static org.elasticsearch.common.xcontent.XContentParserUtils.parseTypedKeysObject;
3940

@@ -44,14 +45,14 @@ public class Aggregations implements Iterable<Aggregation>, ToXContentFragment {
4445

4546
public static final String AGGREGATIONS_FIELD = "aggregations";
4647

47-
protected List<? extends Aggregation> aggregations = Collections.emptyList();
48-
protected Map<String, Aggregation> aggregationsAsMap;
49-
50-
protected Aggregations() {
51-
}
48+
protected final List<? extends Aggregation> aggregations;
49+
private Map<String, Aggregation> aggregationsAsMap;
5250

5351
public Aggregations(List<? extends Aggregation> aggregations) {
5452
this.aggregations = aggregations;
53+
if (aggregations.isEmpty()) {
54+
aggregationsAsMap = emptyMap();
55+
}
5556
}
5657

5758
/**

server/src/main/java/org/elasticsearch/search/aggregations/InternalAggregations.java

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import org.elasticsearch.common.io.stream.StreamInput;
2222
import org.elasticsearch.common.io.stream.StreamOutput;
23-
import org.elasticsearch.common.io.stream.Streamable;
23+
import org.elasticsearch.common.io.stream.Writeable;
2424
import org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext;
2525
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
2626
import org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator;
@@ -34,14 +34,13 @@
3434
import java.util.Map;
3535
import java.util.Objects;
3636

37-
import static java.util.Collections.emptyMap;
38-
3937
/**
4038
* An internal implementation of {@link Aggregations}.
4139
*/
42-
public final class InternalAggregations extends Aggregations implements Streamable {
40+
public final class InternalAggregations extends Aggregations implements Writeable {
41+
42+
public static final InternalAggregations EMPTY = new InternalAggregations(Collections.emptyList());
4343

44-
public static final InternalAggregations EMPTY = new InternalAggregations();
4544
private static final Comparator<InternalAggregation> INTERNAL_AGG_COMPARATOR = (agg1, agg2) -> {
4645
if (agg1.isMapped() == agg2.isMapped()) {
4746
return 0;
@@ -52,16 +51,14 @@ public final class InternalAggregations extends Aggregations implements Streamab
5251
}
5352
};
5453

55-
private List<SiblingPipelineAggregator> topLevelPipelineAggregators = Collections.emptyList();
56-
57-
private InternalAggregations() {
58-
}
54+
private final List<SiblingPipelineAggregator> topLevelPipelineAggregators;
5955

6056
/**
6157
* Constructs a new aggregation.
6258
*/
6359
public InternalAggregations(List<InternalAggregation> aggregations) {
6460
super(aggregations);
61+
this.topLevelPipelineAggregators = Collections.emptyList();
6562
}
6663

6764
/**
@@ -72,6 +69,19 @@ public InternalAggregations(List<InternalAggregation> aggregations, List<Sibling
7269
this.topLevelPipelineAggregators = Objects.requireNonNull(topLevelPipelineAggregators);
7370
}
7471

72+
public InternalAggregations(StreamInput in) throws IOException {
73+
super(in.readList(stream -> in.readNamedWriteable(InternalAggregation.class)));
74+
this.topLevelPipelineAggregators = in.readList(
75+
stream -> (SiblingPipelineAggregator)in.readNamedWriteable(PipelineAggregator.class));
76+
}
77+
78+
@Override
79+
@SuppressWarnings("unchecked")
80+
public void writeTo(StreamOutput out) throws IOException {
81+
out.writeNamedWriteableList((List<InternalAggregation>)aggregations);
82+
out.writeNamedWriteableList(topLevelPipelineAggregators);
83+
}
84+
7585
/**
7686
* Returns the top-level pipeline aggregators.
7787
* Note that top-level pipeline aggregators become normal aggregation once the final reduction has been performed, after which they
@@ -86,8 +96,7 @@ public List<SiblingPipelineAggregator> getTopLevelPipelineAggregators() {
8696
* {@link InternalAggregations} object found in the list.
8797
* Note that top-level pipeline aggregators are reduced only as part of the final reduction phase, otherwise they are left untouched.
8898
*/
89-
public static InternalAggregations reduce(List<InternalAggregations> aggregationsList,
90-
ReduceContext context) {
99+
public static InternalAggregations reduce(List<InternalAggregations> aggregationsList, ReduceContext context) {
91100
if (aggregationsList.isEmpty()) {
92101
return null;
93102
}
@@ -123,27 +132,4 @@ public static InternalAggregations reduce(List<InternalAggregations> aggregation
123132
}
124133
return new InternalAggregations(reducedAggregations, topLevelPipelineAggregators);
125134
}
126-
127-
public static InternalAggregations readAggregations(StreamInput in) throws IOException {
128-
InternalAggregations result = new InternalAggregations();
129-
result.readFrom(in);
130-
return result;
131-
}
132-
133-
@Override
134-
public void readFrom(StreamInput in) throws IOException {
135-
aggregations = in.readList(stream -> in.readNamedWriteable(InternalAggregation.class));
136-
if (aggregations.isEmpty()) {
137-
aggregationsAsMap = emptyMap();
138-
}
139-
this.topLevelPipelineAggregators = in.readList(
140-
stream -> (SiblingPipelineAggregator)in.readNamedWriteable(PipelineAggregator.class));
141-
}
142-
143-
@Override
144-
@SuppressWarnings("unchecked")
145-
public void writeTo(StreamOutput out) throws IOException {
146-
out.writeNamedWriteableList((List<InternalAggregation>)aggregations);
147-
out.writeNamedWriteableList(topLevelPipelineAggregators);
148-
}
149135
}

server/src/main/java/org/elasticsearch/search/aggregations/bucket/InternalSingleBucketAggregation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected InternalSingleBucketAggregation(String name, long docCount, InternalAg
6060
protected InternalSingleBucketAggregation(StreamInput in) throws IOException {
6161
super(in);
6262
docCount = in.readVLong();
63-
aggregations = InternalAggregations.readAggregations(in);
63+
aggregations = new InternalAggregations(in);
6464
}
6565

6666
@Override

server/src/main/java/org/elasticsearch/search/aggregations/bucket/adjacency/InternalAdjacencyMatrix.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public InternalBucket(String key, long docCount, InternalAggregations aggregatio
5858
public InternalBucket(StreamInput in) throws IOException {
5959
key = in.readOptionalString();
6060
docCount = in.readVLong();
61-
aggregations = InternalAggregations.readAggregations(in);
61+
aggregations = new InternalAggregations(in);
6262
}
6363

6464
@Override

server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/InternalComposite.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public static class InternalBucket extends InternalMultiBucketAggregation.Intern
237237
InternalBucket(StreamInput in, List<String> sourceNames, List<DocValueFormat> formats, int[] reverseMuls) throws IOException {
238238
this.key = new CompositeKey(in);
239239
this.docCount = in.readVLong();
240-
this.aggregations = InternalAggregations.readAggregations(in);
240+
this.aggregations = new InternalAggregations(in);
241241
this.reverseMuls = reverseMuls;
242242
this.sourceNames = sourceNames;
243243
this.formats = formats;

server/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/InternalFilters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public InternalBucket(StreamInput in, boolean keyed) throws IOException {
5757
this.keyed = keyed;
5858
key = in.readOptionalString();
5959
docCount = in.readVLong();
60-
aggregations = InternalAggregations.readAggregations(in);
60+
aggregations = new InternalAggregations(in);
6161
}
6262

6363
@Override

server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/InternalGeoGridBucket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public InternalGeoGridBucket(long hashAsLong, long docCount, InternalAggregation
5151
public InternalGeoGridBucket(StreamInput in) throws IOException {
5252
hashAsLong = in.readLong();
5353
docCount = in.readVLong();
54-
aggregations = InternalAggregations.readAggregations(in);
54+
aggregations = new InternalAggregations(in);
5555
}
5656

5757
@Override

server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalAutoDateHistogram.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Bucket(StreamInput in, DocValueFormat format) throws IOException {
7373
this.format = format;
7474
key = in.readLong();
7575
docCount = in.readVLong();
76-
aggregations = InternalAggregations.readAggregations(in);
76+
aggregations = new InternalAggregations(in);
7777
}
7878

7979
@Override
@@ -175,7 +175,7 @@ static class BucketInfo {
175175
roundingInfos[i] = new RoundingInfo(in);
176176
}
177177
roundingIdx = in.readVInt();
178-
emptySubAggregations = InternalAggregations.readAggregations(in);
178+
emptySubAggregations = new InternalAggregations(in);
179179
}
180180

181181
void writeTo(StreamOutput out) throws IOException {

server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalDateHistogram.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public Bucket(StreamInput in, boolean keyed, DocValueFormat format) throws IOExc
7777
this.keyed = keyed;
7878
key = in.readLong();
7979
docCount = in.readVLong();
80-
aggregations = InternalAggregations.readAggregations(in);
80+
aggregations = new InternalAggregations(in);
8181
}
8282

8383
@Override
@@ -186,7 +186,7 @@ static class EmptyBucketInfo {
186186

187187
EmptyBucketInfo(StreamInput in) throws IOException {
188188
rounding = Rounding.read(in);
189-
subAggregations = InternalAggregations.readAggregations(in);
189+
subAggregations = new InternalAggregations(in);
190190
bounds = in.readOptionalWriteable(ExtendedBounds::new);
191191
}
192192

server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/InternalHistogram.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Bucket(StreamInput in, boolean keyed, DocValueFormat format) throws IOExc
7373
this.keyed = keyed;
7474
key = in.readDouble();
7575
docCount = in.readVLong();
76-
aggregations = InternalAggregations.readAggregations(in);
76+
aggregations = new InternalAggregations(in);
7777
}
7878

7979
@Override
@@ -178,7 +178,7 @@ static class EmptyBucketInfo {
178178
}
179179

180180
EmptyBucketInfo(StreamInput in) throws IOException {
181-
this(in.readDouble(), in.readDouble(), in.readDouble(), in.readDouble(), InternalAggregations.readAggregations(in));
181+
this(in.readDouble(), in.readDouble(), in.readDouble(), in.readDouble(), new InternalAggregations(in));
182182
}
183183

184184
public void writeTo(StreamOutput out) throws IOException {

0 commit comments

Comments
 (0)