Skip to content

Commit 39de085

Browse files
authored
Ingest: Start separating Metadata from IngestSourceAndMetadata (#88401)
Pull out the implementation of `Metadata` from `IngestSourceAndMetadata`. `Metadata` will become a base class extended by the update contexts: ingest, update, update by query and reindex. `Metadata` implements a map-like interface, making it easy for a class containing `Metadata` to implement the full `Map` interface.
1 parent c271d39 commit 39de085

File tree

17 files changed

+660
-526
lines changed

17 files changed

+660
-526
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void testEscapeFields() throws Exception {
4848
processor = new DotExpanderProcessor("_tag", null, null, "foo.bar");
4949
processor.execute(document);
5050
assertThat(document.getSource().size(), equalTo(1));
51-
assertThat(document.getMetadataMap().size(), equalTo(1)); // the default version
51+
assertThat(document.getMetadata().size(), equalTo(1)); // the default version
5252
assertThat(document.getFieldValue("foo.bar", List.class).size(), equalTo(2));
5353
assertThat(document.getFieldValue("foo.bar.0", String.class), equalTo("baz2"));
5454
assertThat(document.getFieldValue("foo.bar.1", String.class), equalTo("baz1"));
@@ -60,7 +60,7 @@ public void testEscapeFields() throws Exception {
6060
processor = new DotExpanderProcessor("_tag", null, null, "foo.bar");
6161
processor.execute(document);
6262
assertThat(document.getSource().size(), equalTo(1));
63-
assertThat(document.getMetadataMap().size(), equalTo(1)); // the default version
63+
assertThat(document.getMetadata().size(), equalTo(1)); // the default version
6464
assertThat(document.getFieldValue("foo.bar", List.class).size(), equalTo(2));
6565
assertThat(document.getFieldValue("foo.bar.0", Integer.class), equalTo(1));
6666
assertThat(document.getFieldValue("foo.bar.1", String.class), equalTo("2"));

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ public void testRenameAtomicOperationSetFails() throws Exception {
140140
Map<String, Object> metadata = new HashMap<>();
141141
metadata.put("list", Collections.singletonList("item"));
142142

143-
IngestDocument ingestDocument = TestIngestDocument.ofMetadataWithValidator(metadata, Map.of("new_field", (k, v) -> {
143+
IngestDocument ingestDocument = TestIngestDocument.ofMetadataWithValidator(metadata, Map.of("new_field", (o, k, v) -> {
144144
if (v != null) {
145145
throw new UnsupportedOperationException();
146146
}
147-
}, "list", (k, v) -> {}));
147+
}, "list", (o, k, v) -> {}));
148148
Processor processor = createRenameProcessor("list", "new_field", false);
149149
try {
150150
processor.execute(ingestDocument);
@@ -160,7 +160,7 @@ public void testRenameAtomicOperationRemoveFails() throws Exception {
160160
Map<String, Object> metadata = new HashMap<>();
161161
metadata.put("list", Collections.singletonList("item"));
162162

163-
IngestDocument ingestDocument = TestIngestDocument.ofMetadataWithValidator(metadata, Map.of("list", (k, v) -> {
163+
IngestDocument ingestDocument = TestIngestDocument.ofMetadataWithValidator(metadata, Map.of("list", (o, k, v) -> {
164164
if (v == null) {
165165
throw new UnsupportedOperationException();
166166
}

server/src/internalClusterTest/java/org/elasticsearch/ingest/IngestClientIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void testSimulate() throws Exception {
115115
source.put("processed", true);
116116
IngestDocument ingestDocument = new IngestDocument("index", "id", Versions.MATCH_ANY, null, null, source);
117117
assertThat(simulateDocumentBaseResult.getIngestDocument().getSource(), equalTo(ingestDocument.getSource()));
118-
assertThat(simulateDocumentBaseResult.getIngestDocument().getMetadataMap(), equalTo(ingestDocument.getMetadataMap()));
118+
assertThat(simulateDocumentBaseResult.getIngestDocument().getMetadata().getMap(), equalTo(ingestDocument.getMetadata().getMap()));
119119
assertThat(simulateDocumentBaseResult.getFailure(), nullValue());
120120

121121
// cleanup

server/src/main/java/org/elasticsearch/action/ingest/WriteableIngestDocument.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,11 @@ IngestDocument getIngestDocument() {
107107
@Override
108108
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
109109
builder.startObject(DOC_FIELD);
110-
Map<String, Object> metadataMap = ingestDocument.getMetadataMap();
111-
for (Map.Entry<String, Object> metadata : metadataMap.entrySet()) {
112-
if (metadata.getValue() != null) {
113-
builder.field(metadata.getKey(), metadata.getValue().toString());
110+
org.elasticsearch.script.Metadata metadata = ingestDocument.getMetadata();
111+
for (String key : metadata.keySet()) {
112+
Object value = metadata.get(key);
113+
if (value != null) {
114+
builder.field(key, value.toString());
114115
}
115116
}
116117
if (builder.getRestApiVersion() == RestApiVersion.V_7) {

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

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,15 @@ public IngestDocument(String index, String id, long version, String routing, Ver
6868
source
6969
);
7070
this.ingestMetadata = new HashMap<>();
71-
this.ingestMetadata.put(TIMESTAMP, sourceAndMetadata.getTimestamp());
71+
this.ingestMetadata.put(TIMESTAMP, sourceAndMetadata.getMetadata().getTimestamp());
7272
}
7373

7474
/**
7575
* Copy constructor that creates a new {@link IngestDocument} which has exactly the same properties as the one provided as argument
7676
*/
7777
public IngestDocument(IngestDocument other) {
7878
this(
79-
new IngestSourceAndMetadata(
80-
deepCopyMap(other.sourceAndMetadata.getSource()),
81-
deepCopyMap(other.sourceAndMetadata.getMetadata()),
82-
other.getIngestSourceAndMetadata().timestamp,
83-
other.getIngestSourceAndMetadata().validators
84-
),
79+
new IngestSourceAndMetadata(deepCopyMap(other.sourceAndMetadata.getSource()), other.sourceAndMetadata.getMetadata().clone()),
8580
deepCopyMap(other.ingestMetadata)
8681
);
8782
}
@@ -93,14 +88,12 @@ public IngestDocument(Map<String, Object> sourceAndMetadata, Map<String, Object>
9388
Tuple<Map<String, Object>, Map<String, Object>> sm = IngestSourceAndMetadata.splitSourceAndMetadata(sourceAndMetadata);
9489
this.sourceAndMetadata = new IngestSourceAndMetadata(
9590
sm.v1(),
96-
sm.v2(),
97-
IngestSourceAndMetadata.getTimestamp(ingestMetadata),
98-
IngestSourceAndMetadata.VALIDATORS
91+
new org.elasticsearch.script.Metadata(sm.v2(), IngestSourceAndMetadata.getTimestamp(ingestMetadata))
9992
);
10093
this.ingestMetadata = new HashMap<>(ingestMetadata);
10194
this.ingestMetadata.computeIfPresent(TIMESTAMP, (k, v) -> {
10295
if (v instanceof String) {
103-
return this.sourceAndMetadata.getTimestamp();
96+
return this.sourceAndMetadata.getMetadata().getTimestamp();
10497
}
10598
return v;
10699
});
@@ -737,18 +730,11 @@ public IngestSourceAndMetadata getIngestSourceAndMetadata() {
737730
return sourceAndMetadata;
738731
}
739732

740-
/**
741-
* Get all Metadata values in a Map
742-
*/
743-
public Map<String, Object> getMetadataMap() {
744-
return sourceAndMetadata.getMetadata();
745-
}
746-
747733
/**
748734
* Get the strongly typed metadata
749735
*/
750736
public org.elasticsearch.script.Metadata getMetadata() {
751-
return sourceAndMetadata;
737+
return sourceAndMetadata.getMetadata();
752738
}
753739

754740
/**

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -897,27 +897,27 @@ private void innerExecute(
897897
itemDroppedHandler.accept(slot);
898898
handler.accept(null);
899899
} else {
900-
IngestSourceAndMetadata sourceAndMetadata = ingestDocument.getIngestSourceAndMetadata();
900+
org.elasticsearch.script.Metadata metadata = ingestDocument.getMetadata();
901901

902902
// it's fine to set all metadata fields all the time, as ingest document holds their starting values
903903
// before ingestion, which might also get modified during ingestion.
904-
indexRequest.index(sourceAndMetadata.getIndex());
905-
indexRequest.id(sourceAndMetadata.getId());
906-
indexRequest.routing(sourceAndMetadata.getRouting());
907-
indexRequest.version(sourceAndMetadata.getVersion());
908-
if (sourceAndMetadata.getVersionType() != null) {
909-
indexRequest.versionType(VersionType.fromString(sourceAndMetadata.getVersionType()));
904+
indexRequest.index(metadata.getIndex());
905+
indexRequest.id(metadata.getId());
906+
indexRequest.routing(metadata.getRouting());
907+
indexRequest.version(metadata.getVersion());
908+
if (metadata.getVersionType() != null) {
909+
indexRequest.versionType(VersionType.fromString(metadata.getVersionType()));
910910
}
911911
Number number;
912-
if ((number = sourceAndMetadata.getIfSeqNo()) != null) {
912+
if ((number = metadata.getIfSeqNo()) != null) {
913913
indexRequest.setIfSeqNo(number.longValue());
914914
}
915-
if ((number = sourceAndMetadata.getIfPrimaryTerm()) != null) {
915+
if ((number = metadata.getIfPrimaryTerm()) != null) {
916916
indexRequest.setIfPrimaryTerm(number.longValue());
917917
}
918918
try {
919919
boolean ensureNoSelfReferences = ingestDocument.doNoSelfReferencesCheck();
920-
indexRequest.source(sourceAndMetadata.getSource(), indexRequest.getContentType(), ensureNoSelfReferences);
920+
indexRequest.source(ingestDocument.getSource(), indexRequest.getContentType(), ensureNoSelfReferences);
921921
} catch (IllegalArgumentException ex) {
922922
// An IllegalArgumentException can be thrown when an ingest
923923
// processor creates a source map that is self-referencing.
@@ -933,7 +933,7 @@ private void innerExecute(
933933
return;
934934
}
935935
Map<String, String> map;
936-
if ((map = sourceAndMetadata.getDynamicTemplates()) != null) {
936+
if ((map = metadata.getDynamicTemplates()) != null) {
937937
Map<String, String> mergedDynamicTemplates = new HashMap<>(indexRequest.getDynamicTemplates());
938938
mergedDynamicTemplates.putAll(map);
939939
indexRequest.setDynamicTemplates(mergedDynamicTemplates);

0 commit comments

Comments
 (0)