Skip to content

Commit d3dc2a9

Browse files
committed
Make access pattern optional
1 parent 5a8478e commit d3dc2a9

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.List;
4141
import java.util.Map;
4242
import java.util.Objects;
43+
import java.util.Optional;
4344
import java.util.Set;
4445
import java.util.function.BiConsumer;
4546
import java.util.stream.Collectors;
@@ -202,7 +203,7 @@ public <T> T getFieldValue(String path, Class<T> clazz) {
202203
* or if the field that is found at the provided path is not of the expected type.
203204
*/
204205
public <T> T getFieldValue(String path, Class<T> clazz, boolean ignoreMissing) {
205-
final FieldPath fieldPath = FieldPath.of(path, getCurrentAccessPattern());
206+
final FieldPath fieldPath = FieldPath.of(path, getCurrentAccessPatternSafe());
206207
Object context = fieldPath.initialContext(this);
207208
ResolveResult result = resolve(fieldPath.pathElements, fieldPath.pathElements.length, path, context, getCurrentAccessPatternSafe());
208209
if (result.wasSuccessful) {
@@ -271,7 +272,7 @@ public boolean hasField(String path) {
271272
* @throws IllegalArgumentException if the path is null, empty or invalid.
272273
*/
273274
public boolean hasField(String path, boolean failOutOfRange) {
274-
final FieldPath fieldPath = FieldPath.of(path, getCurrentAccessPattern());
275+
final FieldPath fieldPath = FieldPath.of(path, getCurrentAccessPatternSafe());
275276
Object context = fieldPath.initialContext(this);
276277
int leafKeyIndex = fieldPath.pathElements.length - 1;
277278
int lastContainerIndex = fieldPath.pathElements.length - 2;
@@ -425,7 +426,7 @@ public void removeField(String path) {
425426
* @throws IllegalArgumentException if the path is null, empty, or invalid; or if the field doesn't exist (and ignoreMissing is false).
426427
*/
427428
public void removeField(String path, boolean ignoreMissing) {
428-
final FieldPath fieldPath = FieldPath.of(path, getCurrentAccessPattern());
429+
final FieldPath fieldPath = FieldPath.of(path, getCurrentAccessPatternSafe());
429430
Object context = fieldPath.initialContext(this);
430431
String leafKey = fieldPath.pathElements[fieldPath.pathElements.length - 1];
431432
ResolveResult result = resolve(
@@ -735,7 +736,7 @@ public void setFieldValue(String path, Object value, boolean ignoreEmptyValue) {
735736
}
736737

737738
private void setFieldValue(String path, Object value, boolean append, boolean allowDuplicates) {
738-
final FieldPath fieldPath = FieldPath.of(path, getCurrentAccessPattern());
739+
final FieldPath fieldPath = FieldPath.of(path, getCurrentAccessPatternSafe());
739740
Object context = fieldPath.initialContext(this);
740741
int leafKeyIndex = fieldPath.pathElements.length - 1;
741742
int lastContainerIndex = fieldPath.pathElements.length - 2;
@@ -1154,18 +1155,18 @@ List<String> getPipelineStack() {
11541155
}
11551156

11561157
/**
1157-
* @return The access pattern for any currently executing pipelines, or null if no pipelines are in progress for this doc
1158+
* @return The access pattern for any currently executing pipelines, or empty if no pipelines are in progress for this doc
11581159
*/
1159-
public IngestPipelineFieldAccessPattern getCurrentAccessPattern() {
1160-
return accessPatternStack.peek();
1160+
public Optional<IngestPipelineFieldAccessPattern> getCurrentAccessPattern() {
1161+
return Optional.ofNullable(accessPatternStack.peek());
11611162
}
11621163

11631164
/**
11641165
* @return The access pattern for any currently executing pipelines, or {@link IngestPipelineFieldAccessPattern#CLASSIC} if no
11651166
* pipelines are in progress for this doc for the sake of backwards compatibility
11661167
*/
11671168
private IngestPipelineFieldAccessPattern getCurrentAccessPatternSafe() {
1168-
return Objects.requireNonNullElse(getCurrentAccessPattern(), IngestPipelineFieldAccessPattern.CLASSIC);
1169+
return getCurrentAccessPattern().orElse(IngestPipelineFieldAccessPattern.CLASSIC);
11691170
}
11701171

11711172
/**

server/src/test/java/org/elasticsearch/ingest/IngestDocumentTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,7 +2034,7 @@ public void testNestedAccessPatternPropagation() {
20342034

20352035
// At the end of the test, there should be neither pipeline ids nor access patterns left in the stack.
20362036
assertThat(document.getPipelineStack(), is(empty()));
2037-
assertThat(document.getCurrentAccessPattern(), is(nullValue()));
2037+
assertThat(document.getCurrentAccessPattern().isEmpty(), is(true));
20382038
}
20392039

20402040
/**
@@ -2082,7 +2082,8 @@ void doTestNestedAccessPatternPropagation(int level, int maxCallDepth, IngestDoc
20822082

20832083
// Assert expected state
20842084
assertThat(document.getPipelineStack().getFirst(), is(expectedPipelineId));
2085-
assertThat(document.getCurrentAccessPattern(), is(expectedAccessPattern));
2085+
assertThat(document.getCurrentAccessPattern().isPresent(), is(true));
2086+
assertThat(document.getCurrentAccessPattern().get(), is(expectedAccessPattern));
20862087

20872088
// Randomly recurse: We recurse only one time per level to avoid hogging test time, but we randomize which
20882089
// pipeline to recurse on, eventually requiring a recursion on the last pipeline run if one hasn't happened yet.
@@ -2099,11 +2100,11 @@ void doTestNestedAccessPatternPropagation(int level, int maxCallDepth, IngestDoc
20992100
assertThat(document.getPipelineStack().size(), is(equalTo(level)));
21002101
if (level == 0) {
21012102
// Top level means access pattern should be empty
2102-
assertThat(document.getCurrentAccessPattern(), is(nullValue()));
2103+
assertThat(document.getCurrentAccessPattern().isEmpty(), is(true));
21032104
} else {
21042105
// If we're nested below the top level we should still have an access
21052106
// pattern on the document for the pipeline above us
2106-
assertThat(document.getCurrentAccessPattern(), is(not(nullValue())));
2107+
assertThat(document.getCurrentAccessPattern().isPresent(), is(true));
21072108
}
21082109
}
21092110
logger.debug("LEVEL {}/{}: COMPLETE", level, maxCallDepth);

0 commit comments

Comments
 (0)