Skip to content

Commit 2a51685

Browse files
authored
Make InternalCentroid leaner (#116302) (#116334)
We are currently holding to fields to extract values, this commit makes them abstract methods so we don't use any heap.
1 parent 47f6d77 commit 2a51685

File tree

3 files changed

+59
-54
lines changed

3 files changed

+59
-54
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/metrics/InternalCentroid.java

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,20 @@
2323
import java.util.List;
2424
import java.util.Map;
2525
import java.util.Objects;
26-
import java.util.function.Function;
2726

2827
/**
2928
* Serialization and merge logic for {@link GeoCentroidAggregator}.
3029
*/
3130
public abstract class InternalCentroid extends InternalAggregation implements CentroidAggregation {
3231
protected final SpatialPoint centroid;
3332
protected final long count;
34-
private final FieldExtractor firstField;
35-
private final FieldExtractor secondField;
36-
37-
public InternalCentroid(
38-
String name,
39-
SpatialPoint centroid,
40-
long count,
41-
Map<String, Object> metadata,
42-
FieldExtractor firstField,
43-
FieldExtractor secondField
44-
) {
33+
34+
public InternalCentroid(String name, SpatialPoint centroid, long count, Map<String, Object> metadata) {
4535
super(name, metadata);
4636
assert (centroid == null) == (count == 0);
4737
this.centroid = centroid;
4838
assert count >= 0;
4939
this.count = count;
50-
this.firstField = firstField;
51-
this.secondField = secondField;
5240
}
5341

5442
protected abstract SpatialPoint centroidFromStream(StreamInput in) throws IOException;
@@ -59,16 +47,14 @@ public InternalCentroid(
5947
* Read from a stream.
6048
*/
6149
@SuppressWarnings("this-escape")
62-
protected InternalCentroid(StreamInput in, FieldExtractor firstField, FieldExtractor secondField) throws IOException {
50+
protected InternalCentroid(StreamInput in) throws IOException {
6351
super(in);
6452
count = in.readVLong();
6553
if (in.readBoolean()) {
6654
centroid = centroidFromStream(in);
6755
} else {
6856
centroid = null;
6957
}
70-
this.firstField = firstField;
71-
this.secondField = secondField;
7258
}
7359

7460
@Override
@@ -110,11 +96,11 @@ public void accept(InternalAggregation aggregation) {
11096
if (centroidAgg.count > 0) {
11197
totalCount += centroidAgg.count;
11298
if (Double.isNaN(firstSum)) {
113-
firstSum = centroidAgg.count * firstField.extractor.apply(centroidAgg.centroid);
114-
secondSum = centroidAgg.count * secondField.extractor.apply(centroidAgg.centroid);
99+
firstSum = centroidAgg.count * extractFirst(centroidAgg.centroid);
100+
secondSum = centroidAgg.count * extractSecond(centroidAgg.centroid);
115101
} else {
116-
firstSum += centroidAgg.count * firstField.extractor.apply(centroidAgg.centroid);
117-
secondSum += centroidAgg.count * secondField.extractor.apply(centroidAgg.centroid);
102+
firstSum += centroidAgg.count * extractFirst(centroidAgg.centroid);
103+
secondSum += centroidAgg.count * extractSecond(centroidAgg.centroid);
118104
}
119105
}
120106
}
@@ -126,6 +112,14 @@ public InternalAggregation get() {
126112
};
127113
}
128114

115+
protected abstract String nameFirst();
116+
117+
protected abstract double extractFirst(SpatialPoint point);
118+
119+
protected abstract String nameSecond();
120+
121+
protected abstract double extractSecond(SpatialPoint point);
122+
129123
@Override
130124
public InternalAggregation finalizeSampling(SamplingContext samplingContext) {
131125
return copyWith(centroid, samplingContext.scaleUp(count));
@@ -136,16 +130,6 @@ protected boolean mustReduceOnSingleInternalAgg() {
136130
return false;
137131
}
138132

139-
protected static class FieldExtractor {
140-
private final String name;
141-
private final Function<SpatialPoint, Double> extractor;
142-
143-
public FieldExtractor(String name, Function<SpatialPoint, Double> extractor) {
144-
this.name = name;
145-
this.extractor = extractor;
146-
}
147-
}
148-
149133
protected abstract double extractDouble(String name);
150134

151135
@Override
@@ -174,8 +158,8 @@ public XContentBuilder doXContentBody(XContentBuilder builder, Params params) th
174158
if (centroid != null) {
175159
builder.startObject(Fields.CENTROID.getPreferredName());
176160
{
177-
builder.field(firstField.name, firstField.extractor.apply(centroid));
178-
builder.field(secondField.name, secondField.extractor.apply(centroid));
161+
builder.field(nameFirst(), extractFirst(centroid));
162+
builder.field(nameSecond(), extractSecond(centroid));
179163
}
180164
builder.endObject();
181165
}

server/src/main/java/org/elasticsearch/search/aggregations/metrics/InternalGeoCentroid.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.elasticsearch.common.io.stream.StreamOutput;
1616
import org.elasticsearch.search.aggregations.InternalAggregation;
1717
import org.elasticsearch.search.aggregations.support.SamplingContext;
18-
import org.elasticsearch.xcontent.ParseField;
1918

2019
import java.io.IOException;
2120
import java.util.Map;
@@ -26,21 +25,14 @@
2625
public class InternalGeoCentroid extends InternalCentroid implements GeoCentroid {
2726

2827
public InternalGeoCentroid(String name, SpatialPoint centroid, long count, Map<String, Object> metadata) {
29-
super(
30-
name,
31-
centroid,
32-
count,
33-
metadata,
34-
new FieldExtractor("lat", SpatialPoint::getY),
35-
new FieldExtractor("lon", SpatialPoint::getX)
36-
);
28+
super(name, centroid, count, metadata);
3729
}
3830

3931
/**
4032
* Read from a stream.
4133
*/
4234
public InternalGeoCentroid(StreamInput in) throws IOException {
43-
super(in, new FieldExtractor("lat", SpatialPoint::getY), new FieldExtractor("lon", SpatialPoint::getX));
35+
super(in);
4436
}
4537

4638
public static InternalGeoCentroid empty(String name, Map<String, Object> metadata) {
@@ -84,12 +76,27 @@ protected InternalGeoCentroid copyWith(double firstSum, double secondSum, long t
8476
}
8577

8678
@Override
87-
public InternalAggregation finalizeSampling(SamplingContext samplingContext) {
88-
return new InternalGeoCentroid(name, centroid, samplingContext.scaleUp(count), getMetadata());
79+
protected String nameFirst() {
80+
return "lat";
81+
}
82+
83+
@Override
84+
protected double extractFirst(SpatialPoint point) {
85+
return point.getY();
86+
}
87+
88+
@Override
89+
protected String nameSecond() {
90+
return "lon";
91+
}
92+
93+
@Override
94+
protected double extractSecond(SpatialPoint point) {
95+
return point.getX();
8996
}
9097

91-
static class Fields {
92-
static final ParseField CENTROID_LAT = new ParseField("lat");
93-
static final ParseField CENTROID_LON = new ParseField("lon");
98+
@Override
99+
public InternalAggregation finalizeSampling(SamplingContext samplingContext) {
100+
return new InternalGeoCentroid(name, centroid, samplingContext.scaleUp(count), getMetadata());
94101
}
95102
}

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/InternalCartesianCentroid.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.elasticsearch.search.aggregations.InternalAggregation;
1414
import org.elasticsearch.search.aggregations.metrics.InternalCentroid;
1515
import org.elasticsearch.search.aggregations.support.SamplingContext;
16-
import org.elasticsearch.xcontent.ParseField;
1716
import org.elasticsearch.xpack.spatial.common.CartesianPoint;
1817

1918
import java.io.IOException;
@@ -25,14 +24,14 @@
2524
public class InternalCartesianCentroid extends InternalCentroid implements CartesianCentroid {
2625

2726
public InternalCartesianCentroid(String name, SpatialPoint centroid, long count, Map<String, Object> metadata) {
28-
super(name, centroid, count, metadata, new FieldExtractor("x", SpatialPoint::getX), new FieldExtractor("y", SpatialPoint::getY));
27+
super(name, centroid, count, metadata);
2928
}
3029

3130
/**
3231
* Read from a stream.
3332
*/
3433
public InternalCartesianCentroid(StreamInput in) throws IOException {
35-
super(in, new FieldExtractor("x", SpatialPoint::getX), new FieldExtractor("y", SpatialPoint::getY));
34+
super(in);
3635
}
3736

3837
@Override
@@ -80,8 +79,23 @@ public InternalAggregation finalizeSampling(SamplingContext samplingContext) {
8079
return new InternalCartesianCentroid(name, centroid, samplingContext.scaleUp(count), getMetadata());
8180
}
8281

83-
static class Fields {
84-
static final ParseField CENTROID_X = new ParseField("x");
85-
static final ParseField CENTROID_Y = new ParseField("y");
82+
@Override
83+
protected String nameFirst() {
84+
return "x";
85+
}
86+
87+
@Override
88+
protected double extractFirst(SpatialPoint point) {
89+
return point.getX();
90+
}
91+
92+
@Override
93+
protected String nameSecond() {
94+
return "y";
95+
}
96+
97+
@Override
98+
protected double extractSecond(SpatialPoint point) {
99+
return point.getY();
86100
}
87101
}

0 commit comments

Comments
 (0)