Skip to content

Commit c64c428

Browse files
authored
Various small cleanups to IngestDocument and some processors (#95262)
1 parent 0525906 commit c64c428

File tree

11 files changed

+45
-61
lines changed

11 files changed

+45
-61
lines changed

libs/dissect/src/main/java/org/elasticsearch/dissect/DissectParser.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.nio.charset.StandardCharsets;
1212
import java.util.ArrayList;
1313
import java.util.Arrays;
14-
import java.util.Collections;
1514
import java.util.EnumSet;
1615
import java.util.Iterator;
1716
import java.util.LinkedHashSet;
@@ -161,7 +160,7 @@ public DissectParser(String pattern, String appendSeparator) {
161160
}
162161

163162
referenceCount = referenceGroupings.size() * 2;
164-
this.matchPairs = Collections.unmodifiableList(dissectPairs);
163+
this.matchPairs = List.copyOf(dissectPairs);
165164
}
166165

167166
/**

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/KeyValueProcessor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88

99
package org.elasticsearch.ingest.common;
1010

11-
import org.elasticsearch.common.util.set.Sets;
1211
import org.elasticsearch.ingest.AbstractProcessor;
1312
import org.elasticsearch.ingest.ConfigurationUtils;
1413
import org.elasticsearch.ingest.IngestDocument;
1514
import org.elasticsearch.ingest.Processor;
1615
import org.elasticsearch.script.ScriptService;
1716
import org.elasticsearch.script.TemplateScript;
1817

19-
import java.util.Collections;
2018
import java.util.List;
2119
import java.util.Map;
2220
import java.util.Set;
@@ -264,11 +262,11 @@ public KeyValueProcessor create(
264262
Set<String> excludeKeys = null;
265263
List<String> includeKeysList = ConfigurationUtils.readOptionalList(TYPE, processorTag, config, "include_keys");
266264
if (includeKeysList != null) {
267-
includeKeys = Collections.unmodifiableSet(Sets.newHashSet(includeKeysList));
265+
includeKeys = Set.copyOf(includeKeysList);
268266
}
269267
List<String> excludeKeysList = ConfigurationUtils.readOptionalList(TYPE, processorTag, config, "exclude_keys");
270268
if (excludeKeysList != null) {
271-
excludeKeys = Collections.unmodifiableSet(Sets.newHashSet(excludeKeysList));
269+
excludeKeys = Set.copyOf(excludeKeysList);
272270
}
273271
boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "ignore_missing", false);
274272
return new KeyValueProcessor(

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/NetworkDirectionProcessor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Arrays;
2323
import java.util.List;
2424
import java.util.Map;
25-
import java.util.stream.Collectors;
2625

2726
import static org.elasticsearch.ingest.ConfigurationUtils.newConfigurationException;
2827
import static org.elasticsearch.ingest.ConfigurationUtils.readBooleanProperty;
@@ -126,7 +125,7 @@ private String getDirection(IngestDocument d) throws Exception {
126125
}
127126
networks.addAll(stringList);
128127
} else {
129-
networks = internalNetworks.stream().map(network -> d.renderTemplate(network)).collect(Collectors.toList());
128+
networks = internalNetworks.stream().map(network -> d.renderTemplate(network)).toList();
130129
}
131130

132131
String sourceIpAddrString = d.getFieldValue(sourceIpField, String.class, ignoreMissing);

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RemoveProcessor.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020
import java.util.Map;
21-
import java.util.stream.Collectors;
2221

2322
import static org.elasticsearch.ingest.ConfigurationUtils.newConfigurationException;
2423

@@ -41,8 +40,8 @@ public final class RemoveProcessor extends AbstractProcessor {
4140
boolean ignoreMissing
4241
) {
4342
super(tag, description);
44-
this.fieldsToRemove = new ArrayList<>(fieldsToRemove);
45-
this.fieldsToKeep = new ArrayList<>(fieldsToKeep);
43+
this.fieldsToRemove = List.copyOf(fieldsToRemove);
44+
this.fieldsToKeep = List.copyOf(fieldsToKeep);
4645
this.ignoreMissing = ignoreMissing;
4746
}
4847

@@ -124,7 +123,7 @@ public RemoveProcessor create(
124123
private List<TemplateScript.Factory> getTemplates(String processorTag, Map<String, Object> config, String propertyName) {
125124
return getFields(processorTag, config, propertyName).stream()
126125
.map(f -> ConfigurationUtils.compileTemplate(TYPE, processorTag, propertyName, f, scriptService))
127-
.collect(Collectors.toList());
126+
.toList();
128127
}
129128

130129
private static List<String> getFields(String processorTag, Map<String, Object> config, String propertyName) {

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SetProcessorFactoryTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void testCreate() throws Exception {
4141
assertThat(setProcessor.getField().newInstance(Map.of()).execute(), equalTo("field1"));
4242
assertThat(setProcessor.getValue().copyAndResolve(Map.of()), equalTo("value1"));
4343
assertThat(setProcessor.isOverrideEnabled(), equalTo(true));
44+
assertThat(setProcessor.isIgnoreEmptyValue(), equalTo(false));
4445
}
4546

4647
public void testCreateWithOverride() throws Exception {
@@ -57,6 +58,20 @@ public void testCreateWithOverride() throws Exception {
5758
assertThat(setProcessor.isOverrideEnabled(), equalTo(overrideEnabled));
5859
}
5960

61+
public void testCreateWithIgnoreEmptyValue() throws Exception {
62+
boolean ignoreEmptyValueEnabled = randomBoolean();
63+
Map<String, Object> config = new HashMap<>();
64+
config.put("field", "field1");
65+
config.put("value", "value1");
66+
config.put("ignore_empty_value", ignoreEmptyValueEnabled);
67+
String processorTag = randomAlphaOfLength(10);
68+
SetProcessor setProcessor = factory.create(null, processorTag, null, config);
69+
assertThat(setProcessor.getTag(), equalTo(processorTag));
70+
assertThat(setProcessor.getField().newInstance(Map.of()).execute(), equalTo("field1"));
71+
assertThat(setProcessor.getValue().copyAndResolve(Map.of()), equalTo("value1"));
72+
assertThat(setProcessor.isIgnoreEmptyValue(), equalTo(ignoreEmptyValueEnabled));
73+
}
74+
6075
public void testCreateNoFieldPresent() throws Exception {
6176
Map<String, Object> config = new HashMap<>();
6277
config.put("value", "value1");

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpProcessor.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.net.InetAddress;
3434
import java.util.ArrayList;
3535
import java.util.Arrays;
36-
import java.util.Collections;
3736
import java.util.EnumSet;
3837
import java.util.HashMap;
3938
import java.util.List;
@@ -388,7 +387,7 @@ public GeoIpDatabase get() throws IOException {
388387
}
389388

390389
public static final class Factory implements Processor.Factory {
391-
static final Set<Property> DEFAULT_CITY_PROPERTIES = Collections.unmodifiableSet(
390+
static final Set<Property> DEFAULT_CITY_PROPERTIES = Set.copyOf(
392391
EnumSet.of(
393392
Property.CONTINENT_NAME,
394393
Property.COUNTRY_NAME,
@@ -399,10 +398,10 @@ public static final class Factory implements Processor.Factory {
399398
Property.LOCATION
400399
)
401400
);
402-
static final Set<Property> DEFAULT_COUNTRY_PROPERTIES = Collections.unmodifiableSet(
401+
static final Set<Property> DEFAULT_COUNTRY_PROPERTIES = Set.copyOf(
403402
EnumSet.of(Property.CONTINENT_NAME, Property.COUNTRY_NAME, Property.COUNTRY_ISO_CODE)
404403
);
405-
static final Set<Property> DEFAULT_ASN_PROPERTIES = Collections.unmodifiableSet(
404+
static final Set<Property> DEFAULT_ASN_PROPERTIES = Set.copyOf(
406405
EnumSet.of(Property.IP, Property.ASN, Property.ORGANIZATION_NAME, Property.NETWORK)
407406
);
408407

@@ -455,7 +454,7 @@ public Processor create(
455454
throw newConfigurationException(TYPE, processorTag, "properties", e.getMessage());
456455
}
457456
}
458-
properties = Collections.unmodifiableSet(modifiableProperties);
457+
properties = Set.copyOf(modifiableProperties);
459458
} else {
460459
if (databaseType.endsWith(CITY_DB_SUFFIX)) {
461460
properties = DEFAULT_CITY_PROPERTIES;

modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderTaskExecutorTests.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,28 @@
2525

2626
public class GeoIpDownloaderTaskExecutorTests extends ESTestCase {
2727
public void testHasAtLeastOneGeoipProcessor() {
28-
Map<String, PipelineConfiguration> configs = new HashMap<>();
29-
IngestMetadata ingestMetadata = new IngestMetadata(configs);
28+
final IngestMetadata[] ingestMetadata = new IngestMetadata[1];
3029
ClusterState clusterState = mock(ClusterState.class);
3130
Metadata metadata = mock(Metadata.class);
32-
when(metadata.custom(IngestMetadata.TYPE)).thenReturn(ingestMetadata);
31+
when(metadata.custom(IngestMetadata.TYPE)).thenAnswer(invocationOnmock -> ingestMetadata[0]);
3332
when(clusterState.getMetadata()).thenReturn(metadata);
3433
List<String> expectHitsInputs = getPipelinesWithGeoIpProcessors();
3534
List<String> expectMissesInputs = getPipelinesWithoutGeoIpProcessors();
3635
{
3736
// Test that hasAtLeastOneGeoipProcessor returns true for any pipeline with a geoip processor:
3837
for (String pipeline : expectHitsInputs) {
39-
configs.clear();
40-
configs.put("_id1", new PipelineConfiguration("_id1", new BytesArray(pipeline), XContentType.JSON));
38+
ingestMetadata[0] = new IngestMetadata(
39+
Map.of("_id1", new PipelineConfiguration("_id1", new BytesArray(pipeline), XContentType.JSON))
40+
);
4141
assertTrue(GeoIpDownloaderTaskExecutor.hasAtLeastOneGeoipProcessor(clusterState));
4242
}
4343
}
4444
{
4545
// Test that hasAtLeastOneGeoipProcessor returns false for any pipeline without a geoip processor:
4646
for (String pipeline : expectMissesInputs) {
47-
configs.clear();
48-
configs.put("_id1", new PipelineConfiguration("_id1", new BytesArray(pipeline), XContentType.JSON));
47+
ingestMetadata[0] = new IngestMetadata(
48+
Map.of("_id1", new PipelineConfiguration("_id1", new BytesArray(pipeline), XContentType.JSON))
49+
);
4950
assertFalse(GeoIpDownloaderTaskExecutor.hasAtLeastOneGeoipProcessor(clusterState));
5051
}
5152
}
@@ -54,7 +55,7 @@ public void testHasAtLeastOneGeoipProcessor() {
5455
* Now test that hasAtLeastOneGeoipProcessor returns true for a mix of pipelines, some which have geoip processors and some
5556
* which do not:
5657
*/
57-
configs.clear();
58+
Map<String, PipelineConfiguration> configs = new HashMap<>();
5859
for (String pipeline : expectHitsInputs) {
5960
String id = randomAlphaOfLength(20);
6061
configs.put(id, new PipelineConfiguration(id, new BytesArray(pipeline), XContentType.JSON));
@@ -63,6 +64,7 @@ public void testHasAtLeastOneGeoipProcessor() {
6364
String id = randomAlphaOfLength(20);
6465
configs.put(id, new PipelineConfiguration(id, new BytesArray(pipeline), XContentType.JSON));
6566
}
67+
ingestMetadata[0] = new IngestMetadata(configs);
6668
assertTrue(GeoIpDownloaderTaskExecutor.hasAtLeastOneGeoipProcessor(clusterState));
6769
}
6870
}

server/src/main/java/org/elasticsearch/ingest/CompoundProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public CompoundProcessor(boolean ignoreFailure, List<Processor> processors, List
5353
this.processors = List.copyOf(processors);
5454
this.onFailureProcessors = List.copyOf(onFailureProcessors);
5555
this.relativeTimeProvider = relativeTimeProvider;
56-
this.processorsWithMetrics = processors.stream().map(p -> new Tuple<>(p, new IngestMetric())).toList();
56+
this.processorsWithMetrics = List.copyOf(processors.stream().map(p -> new Tuple<>(p, new IngestMetric())).toList());
5757
this.isAsync = flattenProcessors().stream().anyMatch(Processor::isAsync);
5858
}
5959

server/src/main/java/org/elasticsearch/ingest/IngestDocument.java

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -428,22 +428,6 @@ public void appendFieldValue(String path, Object value, boolean allowDuplicates)
428428
setFieldValue(path, value, true, allowDuplicates);
429429
}
430430

431-
/**
432-
* Appends the provided value to the provided path in the document.
433-
* Any non existing path element will be created.
434-
* If the path identifies a list, the value will be appended to the existing list.
435-
* If the path identifies a scalar, the scalar will be converted to a list and
436-
* the provided value will be added to the newly created list.
437-
* Supports multiple values too provided in forms of list, in that case all the values will be appended to the
438-
* existing (or newly created) list.
439-
* @param fieldPathTemplate Resolves to the path with dot-notation within the document
440-
* @param valueSource The value source that will produce the value or values to append to the existing ones
441-
* @throws IllegalArgumentException if the path is null, empty or invalid.
442-
*/
443-
public void appendFieldValue(TemplateScript.Factory fieldPathTemplate, ValueSource valueSource) {
444-
appendFieldValue(fieldPathTemplate.newInstance(templateModel).execute(), valueSource.copyAndResolve(templateModel));
445-
}
446-
447431
/**
448432
* Appends the provided value to the provided path in the document.
449433
* Any non existing path element will be created.
@@ -476,7 +460,7 @@ public void appendFieldValue(TemplateScript.Factory fieldPathTemplate, ValueSour
476460
* item identified by the provided path.
477461
*/
478462
public void setFieldValue(String path, Object value) {
479-
setFieldValue(path, value, false);
463+
setFieldValue(path, value, false, true);
480464
}
481465

482466
/**
@@ -489,7 +473,7 @@ public void setFieldValue(String path, Object value) {
489473
* item identified by the provided path.
490474
*/
491475
public void setFieldValue(TemplateScript.Factory fieldPathTemplate, ValueSource valueSource) {
492-
setFieldValue(fieldPathTemplate.newInstance(templateModel).execute(), valueSource.copyAndResolve(templateModel), false);
476+
setFieldValue(fieldPathTemplate.newInstance(templateModel).execute(), valueSource.copyAndResolve(templateModel));
493477
}
494478

495479
/**
@@ -514,7 +498,7 @@ public void setFieldValue(TemplateScript.Factory fieldPathTemplate, ValueSource
514498
}
515499
}
516500

517-
setFieldValue(fieldPathTemplate.newInstance(templateModel).execute(), value, false);
501+
setFieldValue(fieldPathTemplate.newInstance(templateModel).execute(), value);
518502
}
519503

520504
/**
@@ -539,11 +523,7 @@ public void setFieldValue(TemplateScript.Factory fieldPathTemplate, Object value
539523
}
540524
}
541525

542-
setFieldValue(fieldPathTemplate.newInstance(templateModel).execute(), value, false);
543-
}
544-
545-
private void setFieldValue(String path, Object value, boolean append) {
546-
setFieldValue(path, value, append, true);
526+
setFieldValue(fieldPathTemplate.newInstance(templateModel).execute(), value);
547527
}
548528

549529
private void setFieldValue(String path, Object value, boolean append, boolean allowDuplicates) {
@@ -969,11 +949,11 @@ private FieldPath(String path) {
969949
String newPath;
970950
if (path.startsWith(INGEST_KEY_PREFIX)) {
971951
initialContext = ingestMetadata;
972-
newPath = path.substring(INGEST_KEY_PREFIX.length(), path.length());
952+
newPath = path.substring(INGEST_KEY_PREFIX.length());
973953
} else {
974954
initialContext = ctxMap;
975955
if (path.startsWith(SOURCE_PREFIX)) {
976-
newPath = path.substring(SOURCE_PREFIX.length(), path.length());
956+
newPath = path.substring(SOURCE_PREFIX.length());
977957
} else {
978958
newPath = path;
979959
}

server/src/main/java/org/elasticsearch/ingest/IngestMetadata.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import java.io.IOException;
2626
import java.util.ArrayList;
27-
import java.util.Collections;
2827
import java.util.EnumSet;
2928
import java.util.HashMap;
3029
import java.util.Iterator;
@@ -52,7 +51,7 @@ public final class IngestMetadata implements Metadata.Custom {
5251
private final Map<String, PipelineConfiguration> pipelines;
5352

5453
public IngestMetadata(Map<String, PipelineConfiguration> pipelines) {
55-
this.pipelines = Collections.unmodifiableMap(pipelines);
54+
this.pipelines = Map.copyOf(pipelines);
5655
}
5756

5857
@Override
@@ -76,7 +75,7 @@ public IngestMetadata(StreamInput in) throws IOException {
7675
PipelineConfiguration pipeline = PipelineConfiguration.readFrom(in);
7776
pipelines.put(pipeline.getId(), pipeline);
7877
}
79-
this.pipelines = Collections.unmodifiableMap(pipelines);
78+
this.pipelines = Map.copyOf(pipelines);
8079
}
8180

8281
@Override

0 commit comments

Comments
 (0)