Skip to content

Commit bb1cb40

Browse files
authored
Make AggregateMetricDoubleFieldType immutable (#138650)
1 parent f80b370 commit bb1cb40

File tree

7 files changed

+47
-68
lines changed

7 files changed

+47
-68
lines changed

x-pack/plugin/mapper-aggregate-metric/src/main/java/org/elasticsearch/xpack/aggregatemetric/mapper/AggregateMetricDoubleFieldMapper.java

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.elasticsearch.index.mapper.DocumentParserContext;
3232
import org.elasticsearch.index.mapper.FieldMapper;
3333
import org.elasticsearch.index.mapper.IgnoreMalformedStoredValues;
34-
import org.elasticsearch.index.mapper.IndexType;
3534
import org.elasticsearch.index.mapper.MappedFieldType;
3635
import org.elasticsearch.index.mapper.Mapper;
3736
import org.elasticsearch.index.mapper.MapperBuilderContext;
@@ -253,11 +252,11 @@ public AggregateMetricDoubleFieldMapper build(MapperBuilderContext context) {
253252

254253
AggregateMetricDoubleFieldType metricFieldType = new AggregateMetricDoubleFieldType(
255254
context.buildFullName(leafName()),
256-
meta.getValue(),
257-
timeSeriesMetric.getValue()
255+
timeSeriesMetric.getValue(),
256+
defaultMetric.getValue(),
257+
metricFields,
258+
meta.getValue()
258259
);
259-
metricFieldType.setMetricFields(metricFields);
260-
metricFieldType.setDefaultMetric(defaultMetric.getValue());
261260

262261
return new AggregateMetricDoubleFieldMapper(leafName(), metricFieldType, metricMappers, builderParams(this, context), this);
263262
}
@@ -270,19 +269,21 @@ public AggregateMetricDoubleFieldMapper build(MapperBuilderContext context) {
270269

271270
public static final class AggregateMetricDoubleFieldType extends SimpleMappedFieldType {
272271

273-
private EnumMap<Metric, NumberFieldMapper.NumberFieldType> metricFields;
274-
275-
private Metric defaultMetric;
276-
272+
private final EnumMap<Metric, NumberFieldMapper.NumberFieldType> metricFields;
273+
private final Metric defaultMetric;
277274
private final MetricType metricType;
278275

279-
public AggregateMetricDoubleFieldType(String name) {
280-
this(name, Collections.emptyMap(), null);
281-
}
282-
283-
public AggregateMetricDoubleFieldType(String name, Map<String, String> meta, MetricType metricType) {
284-
super(name, IndexType.points(true, true), false, meta);
276+
public AggregateMetricDoubleFieldType(
277+
String name,
278+
MetricType metricType,
279+
Metric defaultMetric,
280+
EnumMap<Metric, NumberFieldMapper.NumberFieldType> metricFields,
281+
Map<String, String> meta
282+
) {
283+
super(name, metricFields.get(defaultMetric).indexType(), false, meta);
285284
this.metricType = metricType;
285+
this.defaultMetric = defaultMetric;
286+
this.metricFields = metricFields;
286287
}
287288

288289
/**
@@ -311,29 +312,10 @@ public TextSearchInfo getTextSearchInfo() {
311312
return TextSearchInfo.SIMPLE_MATCH_WITHOUT_TERMS;
312313
}
313314

314-
private void setMetricFields(EnumMap<Metric, NumberFieldMapper.NumberFieldType> metricFields) {
315-
this.metricFields = metricFields;
316-
}
317-
318315
public Map<Metric, NumberFieldMapper.NumberFieldType> getMetricFields() {
319316
return Collections.unmodifiableMap(metricFields);
320317
}
321318

322-
public void addMetricField(Metric m, NumberFieldMapper.NumberFieldType subfield) {
323-
if (metricFields == null) {
324-
metricFields = new EnumMap<>(AggregateMetricDoubleFieldMapper.Metric.class);
325-
}
326-
327-
if (name() == null) {
328-
throw new IllegalArgumentException("Field of type [" + typeName() + "] must have a name before adding a subfield");
329-
}
330-
metricFields.put(m, subfield);
331-
}
332-
333-
public void setDefaultMetric(Metric defaultMetric) {
334-
this.defaultMetric = defaultMetric;
335-
}
336-
337319
Metric getDefaultMetric() {
338320
return defaultMetric;
339321
}

x-pack/plugin/mapper-aggregate-metric/src/test/java/org/elasticsearch/xpack/aggregatemetric/aggregations/metrics/AggregateMetricBackedAvgAggregatorTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
import org.elasticsearch.xpack.aggregatemetric.mapper.AggregateMetricDoubleFieldMapper.Metric;
3232

3333
import java.io.IOException;
34+
import java.util.EnumMap;
3435
import java.util.List;
36+
import java.util.Map;
3537
import java.util.function.Consumer;
3638

3739
import static java.util.Collections.singleton;
@@ -117,18 +119,16 @@ public void testQueryFiltering() throws IOException {
117119
* @return the created field type
118120
*/
119121
private AggregateMetricDoubleFieldType createDefaultFieldType(String fieldName) {
120-
AggregateMetricDoubleFieldType fieldType = new AggregateMetricDoubleFieldType(fieldName);
121-
122+
EnumMap<Metric, NumberFieldMapper.NumberFieldType> metricFields = new EnumMap<>(Metric.class);
122123
for (Metric m : List.of(Metric.value_count, Metric.sum)) {
123124
String subfieldName = subfieldName(fieldName, m);
124125
NumberFieldMapper.NumberFieldType subfield = new NumberFieldMapper.NumberFieldType(
125126
subfieldName,
126127
NumberFieldMapper.NumberType.DOUBLE
127128
);
128-
fieldType.addMetricField(m, subfield);
129+
metricFields.put(m, subfield);
129130
}
130-
fieldType.setDefaultMetric(Metric.sum);
131-
return fieldType;
131+
return new AggregateMetricDoubleFieldType(fieldName, null, Metric.sum, metricFields, Map.of());
132132
}
133133

134134
private void testCase(Query query, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalAvg> verify)

x-pack/plugin/mapper-aggregate-metric/src/test/java/org/elasticsearch/xpack/aggregatemetric/aggregations/metrics/AggregateMetricBackedMaxAggregatorTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
import org.elasticsearch.xpack.aggregatemetric.mapper.AggregateMetricDoubleFieldMapper.Metric;
3232

3333
import java.io.IOException;
34+
import java.util.EnumMap;
3435
import java.util.List;
36+
import java.util.Map;
3537
import java.util.function.Consumer;
3638

3739
import static java.util.Collections.singleton;
@@ -117,18 +119,16 @@ public void testQueryFiltering() throws IOException {
117119
* @return the created field type
118120
*/
119121
private AggregateMetricDoubleFieldType createDefaultFieldType(String fieldName) {
120-
AggregateMetricDoubleFieldType fieldType = new AggregateMetricDoubleFieldType(fieldName);
121-
122+
EnumMap<Metric, NumberFieldMapper.NumberFieldType> metricFields = new EnumMap<>(Metric.class);
122123
for (Metric m : List.of(Metric.min, Metric.max)) {
123124
String subfieldName = subfieldName(fieldName, m);
124125
NumberFieldMapper.NumberFieldType subfield = new NumberFieldMapper.NumberFieldType(
125126
subfieldName,
126127
NumberFieldMapper.NumberType.DOUBLE
127128
);
128-
fieldType.addMetricField(m, subfield);
129+
metricFields.put(m, subfield);
129130
}
130-
fieldType.setDefaultMetric(Metric.min);
131-
return fieldType;
131+
return new AggregateMetricDoubleFieldType(fieldName, null, Metric.min, metricFields, Map.of());
132132
}
133133

134134
private void testCase(Query query, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<Max> verify)

x-pack/plugin/mapper-aggregate-metric/src/test/java/org/elasticsearch/xpack/aggregatemetric/aggregations/metrics/AggregateMetricBackedMinAggregatorTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
import org.elasticsearch.xpack.aggregatemetric.mapper.AggregateMetricDoubleFieldMapper.Metric;
3232

3333
import java.io.IOException;
34+
import java.util.EnumMap;
3435
import java.util.List;
36+
import java.util.Map;
3537
import java.util.function.Consumer;
3638

3739
import static java.util.Collections.singleton;
@@ -117,18 +119,16 @@ public void testQueryFiltering() throws IOException {
117119
* @return the created field type
118120
*/
119121
private AggregateMetricDoubleFieldType createDefaultFieldType(String fieldName) {
120-
AggregateMetricDoubleFieldType fieldType = new AggregateMetricDoubleFieldType(fieldName);
121-
122+
EnumMap<Metric, NumberFieldMapper.NumberFieldType> metricFields = new EnumMap<>(Metric.class);
122123
for (Metric m : List.of(Metric.min, Metric.max)) {
123124
String subfieldName = subfieldName(fieldName, m);
124125
NumberFieldMapper.NumberFieldType subfield = new NumberFieldMapper.NumberFieldType(
125126
subfieldName,
126127
NumberFieldMapper.NumberType.DOUBLE
127128
);
128-
fieldType.addMetricField(m, subfield);
129+
metricFields.put(m, subfield);
129130
}
130-
fieldType.setDefaultMetric(Metric.min);
131-
return fieldType;
131+
return new AggregateMetricDoubleFieldType(fieldName, null, Metric.min, metricFields, Map.of());
132132
}
133133

134134
private void testCase(Query query, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<Min> verify)

x-pack/plugin/mapper-aggregate-metric/src/test/java/org/elasticsearch/xpack/aggregatemetric/aggregations/metrics/AggregateMetricBackedSumAggregatorTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
import org.elasticsearch.xpack.aggregatemetric.mapper.AggregateMetricDoubleFieldMapper.Metric;
3232

3333
import java.io.IOException;
34+
import java.util.EnumMap;
3435
import java.util.List;
36+
import java.util.Map;
3537
import java.util.function.Consumer;
3638

3739
import static java.util.Collections.singleton;
@@ -117,18 +119,16 @@ public void testQueryFiltering() throws IOException {
117119
* @return the created field type
118120
*/
119121
private AggregateMetricDoubleFieldType createDefaultFieldType(String fieldName) {
120-
AggregateMetricDoubleFieldType fieldType = new AggregateMetricDoubleFieldType(fieldName);
121-
122+
EnumMap<Metric, NumberFieldMapper.NumberFieldType> metricFields = new EnumMap<>(Metric.class);
122123
for (Metric m : List.of(Metric.value_count, Metric.sum)) {
123124
String subfieldName = subfieldName(fieldName, m);
124125
NumberFieldMapper.NumberFieldType subfield = new NumberFieldMapper.NumberFieldType(
125126
subfieldName,
126127
NumberFieldMapper.NumberType.DOUBLE
127128
);
128-
fieldType.addMetricField(m, subfield);
129+
metricFields.put(m, subfield);
129130
}
130-
fieldType.setDefaultMetric(Metric.sum);
131-
return fieldType;
131+
return new AggregateMetricDoubleFieldType(fieldName, null, Metric.sum, metricFields, Map.of());
132132
}
133133

134134
private void testCase(Query query, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<Sum> verify)

x-pack/plugin/mapper-aggregate-metric/src/test/java/org/elasticsearch/xpack/aggregatemetric/aggregations/metrics/AggregateMetricBackedValueCountAggregatorTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
import org.elasticsearch.xpack.aggregatemetric.mapper.AggregateMetricDoubleFieldMapper.Metric;
3232

3333
import java.io.IOException;
34+
import java.util.EnumMap;
3435
import java.util.List;
36+
import java.util.Map;
3537
import java.util.function.Consumer;
3638

3739
import static java.util.Collections.singleton;
@@ -116,18 +118,16 @@ public void testQueryFiltering() throws IOException {
116118
* @return the created field type
117119
*/
118120
private AggregateMetricDoubleFieldType createDefaultFieldType(String fieldName) {
119-
AggregateMetricDoubleFieldType fieldType = new AggregateMetricDoubleFieldType(fieldName);
120-
121+
EnumMap<Metric, NumberFieldMapper.NumberFieldType> metricFields = new EnumMap<>(Metric.class);
121122
for (Metric m : List.of(Metric.value_count, Metric.sum)) {
122123
String subfieldName = subfieldName(fieldName, m);
123124
NumberFieldMapper.NumberFieldType subfield = new NumberFieldMapper.NumberFieldType(
124125
subfieldName,
125126
NumberFieldMapper.NumberType.DOUBLE
126127
);
127-
fieldType.addMetricField(m, subfield);
128+
metricFields.put(m, subfield);
128129
}
129-
fieldType.setDefaultMetric(Metric.sum);
130-
return fieldType;
130+
return new AggregateMetricDoubleFieldType(fieldName, null, Metric.sum, metricFields, Map.of());
131131
}
132132

133133
private void testCase(Query query, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalValueCount> verify)

x-pack/plugin/mapper-aggregate-metric/src/test/java/org/elasticsearch/xpack/aggregatemetric/mapper/AggregateMetricDoubleFieldTypeTests.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import java.io.IOException;
3434
import java.util.Collections;
35+
import java.util.EnumMap;
3536
import java.util.List;
3637
import java.util.Map;
3738

@@ -46,20 +47,16 @@
4647
public class AggregateMetricDoubleFieldTypeTests extends FieldTypeTestCase {
4748

4849
protected AggregateMetricDoubleFieldType createDefaultFieldType(String name, Map<String, String> meta, Metric defaultMetric) {
49-
AggregateMetricDoubleFieldType fieldType = new AggregateMetricDoubleFieldType(name, meta, null);
50-
for (AggregateMetricDoubleFieldMapper.Metric m : List.of(
51-
AggregateMetricDoubleFieldMapper.Metric.min,
52-
AggregateMetricDoubleFieldMapper.Metric.max
53-
)) {
54-
String subfieldName = subfieldName(fieldType.name(), m);
50+
EnumMap<Metric, NumberFieldMapper.NumberFieldType> metricFields = new EnumMap<>(Metric.class);
51+
for (Metric m : List.of(Metric.min, Metric.max)) {
52+
String subfieldName = subfieldName(name, m);
5553
NumberFieldMapper.NumberFieldType subfield = new NumberFieldMapper.NumberFieldType(
5654
subfieldName,
5755
NumberFieldMapper.NumberType.DOUBLE
5856
);
59-
fieldType.addMetricField(m, subfield);
57+
metricFields.put(m, subfield);
6058
}
61-
fieldType.setDefaultMetric(defaultMetric);
62-
return fieldType;
59+
return new AggregateMetricDoubleFieldType(name, null, defaultMetric, metricFields, meta);
6360
}
6461

6562
public void testTermQuery() {

0 commit comments

Comments
 (0)