Skip to content

Commit 4b97e4e

Browse files
authored
Make InternalComposite.InternalBucket leaner (#117368) (#117466)
This commit removes reverseMuls and missingOrder from InternalComposite.InternalBucket .
1 parent b251970 commit 4b97e4e

File tree

3 files changed

+11
-58
lines changed

3 files changed

+11
-58
lines changed

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,7 @@ public InternalAggregation[] buildAggregations(LongArray owningBucketOrds) throw
205205
CompositeKey key = queue.toCompositeKey(slot);
206206
InternalAggregations aggs = subAggsForBuckets.apply(slot);
207207
long docCount = queue.getDocCount(slot);
208-
buckets[(int) queue.size()] = new InternalComposite.InternalBucket(
209-
sourceNames,
210-
formats,
211-
key,
212-
reverseMuls,
213-
missingOrders,
214-
docCount,
215-
aggs
216-
);
208+
buckets[(int) queue.size()] = new InternalComposite.InternalBucket(sourceNames, formats, key, docCount, aggs);
217209
}
218210
CompositeKey lastBucket = num > 0 ? buckets[num - 1].getRawKey() : null;
219211
return new InternalAggregation[] {

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

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.elasticsearch.search.aggregations.InternalAggregation;
2020
import org.elasticsearch.search.aggregations.InternalAggregations;
2121
import org.elasticsearch.search.aggregations.InternalMultiBucketAggregation;
22-
import org.elasticsearch.search.aggregations.KeyComparable;
2322
import org.elasticsearch.search.aggregations.bucket.BucketReducer;
2423
import org.elasticsearch.search.aggregations.bucket.IteratorAndCurrent;
2524
import org.elasticsearch.search.aggregations.support.SamplingContext;
@@ -103,7 +102,7 @@ public InternalComposite(StreamInput in) throws IOException {
103102
}
104103
this.reverseMuls = in.readIntArray();
105104
this.missingOrders = in.readArray(MissingOrder::readFromStream, MissingOrder[]::new);
106-
this.buckets = in.readCollectionAsList((input) -> new InternalBucket(input, sourceNames, formats, reverseMuls, missingOrders));
105+
this.buckets = in.readCollectionAsList((input) -> new InternalBucket(input, sourceNames, formats));
107106
this.afterKey = in.readOptionalWriteable(CompositeKey::new);
108107
this.earlyTerminated = in.readBoolean();
109108
}
@@ -155,15 +154,7 @@ public InternalComposite create(List<InternalBucket> newBuckets) {
155154

156155
@Override
157156
public InternalBucket createBucket(InternalAggregations aggregations, InternalBucket prototype) {
158-
return new InternalBucket(
159-
prototype.sourceNames,
160-
prototype.formats,
161-
prototype.key,
162-
prototype.reverseMuls,
163-
prototype.missingOrders,
164-
prototype.docCount,
165-
aggregations
166-
);
157+
return new InternalBucket(prototype.sourceNames, prototype.formats, prototype.key, prototype.docCount, aggregations);
167158
}
168159

169160
public int getSize() {
@@ -206,7 +197,7 @@ protected AggregatorReducer getLeaderReducer(AggregationReduceContext reduceCont
206197
private final PriorityQueue<IteratorAndCurrent<InternalBucket>> pq = new PriorityQueue<>(size) {
207198
@Override
208199
protected boolean lessThan(IteratorAndCurrent<InternalBucket> a, IteratorAndCurrent<InternalBucket> b) {
209-
return a.current().compareKey(b.current()) < 0;
200+
return a.current().compareKey(b.current(), reverseMuls, missingOrders) < 0;
210201
}
211202
};
212203
private boolean earlyTerminated = false;
@@ -227,7 +218,7 @@ public InternalAggregation get() {
227218
final List<InternalBucket> result = new ArrayList<>();
228219
while (pq.size() > 0) {
229220
IteratorAndCurrent<InternalBucket> top = pq.top();
230-
if (lastBucket != null && top.current().compareKey(lastBucket) != 0) {
221+
if (lastBucket != null && top.current().compareKey(lastBucket, reverseMuls, missingOrders) != 0) {
231222
InternalBucket reduceBucket = reduceBucket(buckets, reduceContext);
232223
buckets.clear();
233224
result.add(reduceBucket);
@@ -306,7 +297,7 @@ private InternalBucket reduceBucket(List<InternalBucket> buckets, AggregationRed
306297
final var reducedFormats = reducer.getProto().formats;
307298
final long docCount = reducer.getDocCount();
308299
final InternalAggregations aggs = reducer.getAggregations();
309-
return new InternalBucket(sourceNames, reducedFormats, reducer.getProto().key, reverseMuls, missingOrders, docCount, aggs);
300+
return new InternalBucket(sourceNames, reducedFormats, reducer.getProto().key, docCount, aggs);
310301
}
311302
}
312303

@@ -329,49 +320,32 @@ public int hashCode() {
329320
return Objects.hash(super.hashCode(), size, buckets, afterKey, Arrays.hashCode(reverseMuls), Arrays.hashCode(missingOrders));
330321
}
331322

332-
public static class InternalBucket extends InternalMultiBucketAggregation.InternalBucket
333-
implements
334-
CompositeAggregation.Bucket,
335-
KeyComparable<InternalBucket> {
323+
public static class InternalBucket extends InternalMultiBucketAggregation.InternalBucket implements CompositeAggregation.Bucket {
336324

337325
private final CompositeKey key;
338326
private final long docCount;
339327
private final InternalAggregations aggregations;
340-
private final transient int[] reverseMuls;
341-
private final transient MissingOrder[] missingOrders;
342328
private final transient List<String> sourceNames;
343329
private final transient List<DocValueFormat> formats;
344330

345331
InternalBucket(
346332
List<String> sourceNames,
347333
List<DocValueFormat> formats,
348334
CompositeKey key,
349-
int[] reverseMuls,
350-
MissingOrder[] missingOrders,
351335
long docCount,
352336
InternalAggregations aggregations
353337
) {
354338
this.key = key;
355339
this.docCount = docCount;
356340
this.aggregations = aggregations;
357-
this.reverseMuls = reverseMuls;
358-
this.missingOrders = missingOrders;
359341
this.sourceNames = sourceNames;
360342
this.formats = formats;
361343
}
362344

363-
InternalBucket(
364-
StreamInput in,
365-
List<String> sourceNames,
366-
List<DocValueFormat> formats,
367-
int[] reverseMuls,
368-
MissingOrder[] missingOrders
369-
) throws IOException {
345+
InternalBucket(StreamInput in, List<String> sourceNames, List<DocValueFormat> formats) throws IOException {
370346
this.key = new CompositeKey(in);
371347
this.docCount = in.readVLong();
372348
this.aggregations = InternalAggregations.readFrom(in);
373-
this.reverseMuls = reverseMuls;
374-
this.missingOrders = missingOrders;
375349
this.sourceNames = sourceNames;
376350
this.formats = formats;
377351
}
@@ -444,8 +418,7 @@ List<DocValueFormat> getFormats() {
444418
return formats;
445419
}
446420

447-
@Override
448-
public int compareKey(InternalBucket other) {
421+
int compareKey(InternalBucket other, int[] reverseMuls, MissingOrder[] missingOrders) {
449422
for (int i = 0; i < key.size(); i++) {
450423
if (key.get(i) == null) {
451424
if (other.key.get(i) == null) {
@@ -470,8 +443,6 @@ InternalBucket finalizeSampling(SamplingContext samplingContext) {
470443
sourceNames,
471444
formats,
472445
key,
473-
reverseMuls,
474-
missingOrders,
475446
samplingContext.scaleUp(docCount),
476447
InternalAggregations.finalizeSampling(aggregations, samplingContext)
477448
);

server/src/test/java/org/elasticsearch/search/aggregations/bucket/composite/InternalCompositeTests.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,10 @@ protected InternalComposite createTestInstance(String name, Map<String, Object>
143143
continue;
144144
}
145145
keys.add(key);
146-
InternalComposite.InternalBucket bucket = new InternalComposite.InternalBucket(
147-
sourceNames,
148-
formats,
149-
key,
150-
reverseMuls,
151-
missingOrders,
152-
1L,
153-
aggregations
154-
);
146+
InternalComposite.InternalBucket bucket = new InternalComposite.InternalBucket(sourceNames, formats, key, 1L, aggregations);
155147
buckets.add(bucket);
156148
}
157-
Collections.sort(buckets, (o1, o2) -> o1.compareKey(o2));
149+
Collections.sort(buckets, (o1, o2) -> o1.compareKey(o2, reverseMuls, missingOrders));
158150
CompositeKey lastBucket = buckets.size() > 0 ? buckets.get(buckets.size() - 1).getRawKey() : null;
159151
return new InternalComposite(
160152
name,
@@ -191,8 +183,6 @@ protected InternalComposite mutateInstance(InternalComposite instance) {
191183
sourceNames,
192184
formats,
193185
createCompositeKey(),
194-
reverseMuls,
195-
missingOrders,
196186
randomLongBetween(1, 100),
197187
InternalAggregations.EMPTY
198188
)

0 commit comments

Comments
 (0)