Skip to content

Commit dc1fc58

Browse files
committed
IngestMetadata -> IngestDocMetadata, remove splitSourceAndMetadta, fix validate javadoc
1 parent 5a45f97 commit dc1fc58

File tree

8 files changed

+125
-130
lines changed

8 files changed

+125
-130
lines changed

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

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88

99
package org.elasticsearch.ingest;
1010

11-
import org.elasticsearch.common.util.Maps;
1211
import org.elasticsearch.index.VersionType;
1312
import org.elasticsearch.script.CtxMap;
1413
import org.elasticsearch.script.Metadata;
1514

1615
import java.time.ZonedDateTime;
17-
import java.util.Arrays;
1816
import java.util.HashMap;
1917
import java.util.Map;
20-
import java.util.stream.Collectors;
2118

2219
/**
2320
* Map containing ingest source and metadata.
@@ -46,7 +43,7 @@ class IngestCtxMap extends CtxMap {
4643
ZonedDateTime timestamp,
4744
Map<String, Object> source
4845
) {
49-
super(new HashMap<>(source), new IngestMetadata(index, id, version, routing, versionType, timestamp));
46+
super(new HashMap<>(source), new IngestDocMetadata(index, id, version, routing, versionType, timestamp));
5047
}
5148

5249
/**
@@ -76,75 +73,4 @@ public static ZonedDateTime getTimestamp(Map<String, Object> ingestMetadata) {
7673
return null;
7774
}
7875

79-
static class IngestMetadata extends Metadata {
80-
private static final FieldProperty<String> UPDATABLE_STRING = new FieldProperty<>(String.class, true, true, null);
81-
static final Map<String, FieldProperty<?>> PROPERTIES = Map.of(
82-
INDEX,
83-
UPDATABLE_STRING,
84-
ID,
85-
UPDATABLE_STRING,
86-
ROUTING,
87-
UPDATABLE_STRING,
88-
VERSION_TYPE,
89-
new FieldProperty<>(String.class, true, true, (k, v) -> {
90-
try {
91-
VersionType.fromString(v);
92-
return;
93-
} catch (IllegalArgumentException ignored) {}
94-
throw new IllegalArgumentException(
95-
k
96-
+ " must be a null or one of ["
97-
+ Arrays.stream(VersionType.values()).map(vt -> VersionType.toString(vt)).collect(Collectors.joining(", "))
98-
+ "] but was ["
99-
+ v
100-
+ "] with type ["
101-
+ v.getClass().getName()
102-
+ "]"
103-
);
104-
}),
105-
VERSION,
106-
new FieldProperty<>(Number.class, false, true, FieldProperty.LONGABLE_NUMBER),
107-
TYPE,
108-
new FieldProperty<>(String.class, true, false, null),
109-
IF_SEQ_NO,
110-
new FieldProperty<>(Number.class, true, true, FieldProperty.LONGABLE_NUMBER),
111-
IF_PRIMARY_TERM,
112-
new FieldProperty<>(Number.class, true, true, FieldProperty.LONGABLE_NUMBER),
113-
DYNAMIC_TEMPLATES,
114-
new FieldProperty<>(Map.class, true, true, null)
115-
);
116-
117-
protected final ZonedDateTime timestamp;
118-
119-
IngestMetadata(String index, String id, long version, String routing, VersionType versionType, ZonedDateTime timestamp) {
120-
this(metadataMap(index, id, version, routing, versionType), timestamp);
121-
}
122-
123-
IngestMetadata(Map<String, Object> metadata, ZonedDateTime timestamp) {
124-
super(metadata, PROPERTIES);
125-
this.timestamp = timestamp;
126-
}
127-
128-
/**
129-
* Create the backing metadata map with the standard contents assuming default validators.
130-
*/
131-
protected static Map<String, Object> metadataMap(String index, String id, long version, String routing, VersionType versionType) {
132-
Map<String, Object> metadata = Maps.newHashMapWithExpectedSize(IngestDocument.Metadata.values().length);
133-
metadata.put(IngestDocument.Metadata.INDEX.getFieldName(), index);
134-
metadata.put(IngestDocument.Metadata.ID.getFieldName(), id);
135-
metadata.put(IngestDocument.Metadata.VERSION.getFieldName(), version);
136-
if (routing != null) {
137-
metadata.put(IngestDocument.Metadata.ROUTING.getFieldName(), routing);
138-
}
139-
if (versionType != null) {
140-
metadata.put(IngestDocument.Metadata.VERSION_TYPE.getFieldName(), VersionType.toString(versionType));
141-
}
142-
return metadata;
143-
}
144-
145-
@Override
146-
public ZonedDateTime getTimestamp() {
147-
return timestamp;
148-
}
149-
}
15076
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.ingest;
10+
11+
import org.elasticsearch.common.util.Maps;
12+
import org.elasticsearch.index.VersionType;
13+
import org.elasticsearch.script.Metadata;
14+
15+
import java.time.ZonedDateTime;
16+
import java.util.Arrays;
17+
import java.util.Map;
18+
import java.util.stream.Collectors;
19+
20+
class IngestDocMetadata extends Metadata {
21+
private static final FieldProperty<String> UPDATABLE_STRING = new FieldProperty<>(String.class, true, true, null);
22+
static final Map<String, FieldProperty<?>> PROPERTIES = Map.of(
23+
INDEX,
24+
UPDATABLE_STRING,
25+
ID,
26+
UPDATABLE_STRING,
27+
ROUTING,
28+
UPDATABLE_STRING,
29+
VERSION_TYPE,
30+
new FieldProperty<>(String.class, true, true, (k, v) -> {
31+
try {
32+
VersionType.fromString(v);
33+
return;
34+
} catch (IllegalArgumentException ignored) {}
35+
throw new IllegalArgumentException(
36+
k
37+
+ " must be a null or one of ["
38+
+ Arrays.stream(VersionType.values()).map(vt -> VersionType.toString(vt)).collect(Collectors.joining(", "))
39+
+ "] but was ["
40+
+ v
41+
+ "] with type ["
42+
+ v.getClass().getName()
43+
+ "]"
44+
);
45+
}),
46+
VERSION,
47+
new FieldProperty<>(Number.class, false, true, FieldProperty.LONGABLE_NUMBER),
48+
TYPE,
49+
new FieldProperty<>(String.class, true, false, null),
50+
IF_SEQ_NO,
51+
new FieldProperty<>(Number.class, true, true, FieldProperty.LONGABLE_NUMBER),
52+
IF_PRIMARY_TERM,
53+
new FieldProperty<>(Number.class, true, true, FieldProperty.LONGABLE_NUMBER),
54+
DYNAMIC_TEMPLATES,
55+
new FieldProperty<>(Map.class, true, true, null)
56+
);
57+
58+
protected final ZonedDateTime timestamp;
59+
60+
IngestDocMetadata(String index, String id, long version, String routing, VersionType versionType, ZonedDateTime timestamp) {
61+
this(metadataMap(index, id, version, routing, versionType), timestamp);
62+
}
63+
64+
IngestDocMetadata(Map<String, Object> metadata, ZonedDateTime timestamp) {
65+
super(metadata, PROPERTIES);
66+
this.timestamp = timestamp;
67+
}
68+
69+
/**
70+
* Create the backing metadata map with the standard contents assuming default validators.
71+
*/
72+
protected static Map<String, Object> metadataMap(String index, String id, long version, String routing, VersionType versionType) {
73+
Map<String, Object> metadata = Maps.newHashMapWithExpectedSize(IngestDocument.Metadata.values().length);
74+
metadata.put(IngestDocument.Metadata.INDEX.getFieldName(), index);
75+
metadata.put(IngestDocument.Metadata.ID.getFieldName(), id);
76+
metadata.put(IngestDocument.Metadata.VERSION.getFieldName(), version);
77+
if (routing != null) {
78+
metadata.put(IngestDocument.Metadata.ROUTING.getFieldName(), routing);
79+
}
80+
if (versionType != null) {
81+
metadata.put(IngestDocument.Metadata.VERSION_TYPE.getFieldName(), VersionType.toString(versionType));
82+
}
83+
return metadata;
84+
}
85+
86+
@Override
87+
public ZonedDateTime getTimestamp() {
88+
return timestamp;
89+
}
90+
}

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.elasticsearch.common.util.LazyMap;
1313
import org.elasticsearch.common.util.Maps;
1414
import org.elasticsearch.common.util.set.Sets;
15-
import org.elasticsearch.core.Tuple;
1615
import org.elasticsearch.index.VersionType;
1716
import org.elasticsearch.index.mapper.IdFieldMapper;
1817
import org.elasticsearch.index.mapper.IndexFieldMapper;
@@ -77,21 +76,22 @@ public IngestDocument(IngestDocument other) {
7776
* Constructor to create an IngestDocument from its constituent maps. The maps are shallow copied.
7877
*/
7978
public IngestDocument(Map<String, Object> sourceAndMetadata, Map<String, Object> ingestMetadata) {
80-
Tuple<Map<String, Object>, Map<String, Object>> sm = IngestCtxMap.splitSourceAndMetadata(
81-
sourceAndMetadata,
82-
Arrays.stream(IngestDocument.Metadata.values()).map(IngestDocument.Metadata::getFieldName).collect(Collectors.toSet())
83-
);
84-
this.sourceAndMetadata = new IngestCtxMap(
85-
sm.v1(),
86-
new IngestCtxMap.IngestMetadata(sm.v2(), IngestCtxMap.getTimestamp(ingestMetadata))
87-
);
88-
this.ingestMetadata = new HashMap<>(ingestMetadata);
89-
this.ingestMetadata.computeIfPresent(TIMESTAMP, (k, v) -> {
90-
if (v instanceof String) {
91-
return this.sourceAndMetadata.getMetadata().getTimestamp();
79+
Map<String, Object> source;
80+
Map<String, Object> metadata;
81+
if (sourceAndMetadata instanceof IngestCtxMap ingestCtxMap) {
82+
source = new HashMap<>(ingestCtxMap.getSource());
83+
metadata = new HashMap<>(ingestCtxMap.getMetadata().getMap());
84+
} else {
85+
metadata = Maps.newHashMapWithExpectedSize(Metadata.METADATA_NAMES.size());
86+
source = new HashMap<>(sourceAndMetadata);
87+
for (String key : Metadata.METADATA_NAMES) {
88+
if (sourceAndMetadata.containsKey(key)) {
89+
metadata.put(key, source.remove(key));
90+
}
9291
}
93-
return v;
94-
});
92+
}
93+
this.ingestMetadata = new HashMap<>(ingestMetadata);
94+
this.sourceAndMetadata = new IngestCtxMap(source, new IngestDocMetadata(metadata, IngestCtxMap.getTimestamp(ingestMetadata)));
9595
}
9696

9797
/**

server/src/main/java/org/elasticsearch/script/CtxMap.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
package org.elasticsearch.script;
1010

11-
import org.elasticsearch.common.util.Maps;
1211
import org.elasticsearch.common.util.set.Sets;
13-
import org.elasticsearch.core.Tuple;
1412

1513
import java.util.AbstractCollection;
1614
import java.util.AbstractMap;
@@ -52,28 +50,6 @@ protected CtxMap(Map<String, Object> source, Metadata metadata) {
5250
}
5351
}
5452

55-
/**
56-
* Returns a new metadata map and the existing source map with metadata removed.
57-
*/
58-
public static Tuple<Map<String, Object>, Map<String, Object>> splitSourceAndMetadata(
59-
Map<String, Object> sourceAndMetadata,
60-
Set<String> metadataKeys
61-
) {
62-
if (sourceAndMetadata instanceof CtxMap ctxMap) {
63-
return new Tuple<>(new HashMap<>(ctxMap.source), new HashMap<>(ctxMap.metadata.getMap()));
64-
}
65-
66-
Map<String, Object> metadata = Maps.newHashMapWithExpectedSize(metadataKeys.size());
67-
Map<String, Object> source = new HashMap<>(sourceAndMetadata);
68-
69-
for (String metadataKey : metadataKeys) {
70-
if (sourceAndMetadata.containsKey(metadataKey)) {
71-
metadata.put(metadataKey, source.remove(metadataKey));
72-
}
73-
}
74-
return new Tuple<>(source, metadata);
75-
}
76-
7753
/**
7854
* get the source map, if externally modified then the guarantees of this class are not enforced
7955
*/

server/src/main/java/org/elasticsearch/script/Metadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public Metadata(Map<String, Object> map, Map<String, FieldProperty<?>> propertie
5757
}
5858

5959
/**
60-
* Check that all metadata map contains only valid metadata and no extraneous keys and source map contains no metadata
60+
* Check that all metadata map contains only valid metadata and no extraneous keys
6161
*/
6262
protected void validateMetadata() {
6363
int numMetadata = 0;

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void testSettersAndGetters() {
3636
metadata.put("_if_primary_term", 10000);
3737
metadata.put("_version_type", "internal");
3838
metadata.put("_dynamic_templates", Map.of("foo", "bar"));
39-
map = new IngestCtxMap(new HashMap<>(), new IngestCtxMap.IngestMetadata(metadata, null));
39+
map = new IngestCtxMap(new HashMap<>(), new IngestDocMetadata(metadata, null));
4040
md = map.getMetadata();
4141
assertEquals("myIndex", md.getIndex());
4242
md.setIndex("myIndex2");
@@ -69,7 +69,7 @@ public void testInvalidMetadata() {
6969
metadata.put("_version", Double.MAX_VALUE);
7070
IllegalArgumentException err = expectThrows(
7171
IllegalArgumentException.class,
72-
() -> new IngestCtxMap(new HashMap<>(), new IngestCtxMap.IngestMetadata(metadata, null))
72+
() -> new IngestCtxMap(new HashMap<>(), new IngestDocMetadata(metadata, null))
7373
);
7474
assertThat(err.getMessage(), containsString("_version may only be set to an int or a long but was ["));
7575
assertThat(err.getMessage(), containsString("] with type [java.lang.Double]"));
@@ -80,7 +80,7 @@ public void testSourceInMetadata() {
8080
source.put("_version", 25);
8181
IllegalArgumentException err = expectThrows(
8282
IllegalArgumentException.class,
83-
() -> new IngestCtxMap(source, new IngestCtxMap.IngestMetadata(source, null))
83+
() -> new IngestCtxMap(source, new IngestDocMetadata(source, null))
8484
);
8585
assertEquals("unexpected metadata [_version:25] in source", err.getMessage());
8686
}
@@ -92,7 +92,7 @@ public void testExtraMetadata() {
9292
metadata.put("routing", "myRouting");
9393
IllegalArgumentException err = expectThrows(
9494
IllegalArgumentException.class,
95-
() -> new IngestCtxMap(new HashMap<>(), new IngestCtxMap.IngestMetadata(metadata, null))
95+
() -> new IngestCtxMap(new HashMap<>(), new IngestDocMetadata(metadata, null))
9696
);
9797
assertEquals("Unexpected metadata keys [routing:myRouting, version:567]", err.getMessage());
9898
}
@@ -101,7 +101,7 @@ public void testPutSource() {
101101
Map<String, Object> metadata = new HashMap<>();
102102
metadata.put("_version", 123);
103103
Map<String, Object> source = new HashMap<>();
104-
map = new IngestCtxMap(source, new IngestCtxMap.IngestMetadata(metadata, null));
104+
map = new IngestCtxMap(source, new IngestDocMetadata(metadata, null));
105105
}
106106

107107
public void testRemove() {
@@ -221,7 +221,7 @@ public void testEntryAndIterator() {
221221
}
222222

223223
public void testContainsValue() {
224-
map = new IngestCtxMap(Map.of("myField", "fieldValue"), new IngestCtxMap.IngestMetadata(Map.of("_version", 5678), null));
224+
map = new IngestCtxMap(Map.of("myField", "fieldValue"), new IngestDocMetadata(Map.of("_version", 5678), null));
225225
assertTrue(map.containsValue(5678));
226226
assertFalse(map.containsValue(5679));
227227
assertTrue(map.containsValue("fieldValue"));
@@ -295,7 +295,7 @@ public void testValidators() {
295295
public void testHandlesAllVersionTypes() {
296296
Map<String, Object> mdRawMap = new HashMap<>();
297297
mdRawMap.put("_version", 1234);
298-
map = new IngestCtxMap(new HashMap<>(), new IngestCtxMap.IngestMetadata(mdRawMap, null));
298+
map = new IngestCtxMap(new HashMap<>(), new IngestDocMetadata(mdRawMap, null));
299299
md = map.getMetadata();
300300
assertNull(md.getVersionType());
301301
for (VersionType vt : VersionType.values()) {

test/framework/src/main/java/org/elasticsearch/ingest/TestIngestCtxMetadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public TestIngestCtxMetadata(Map<String, Object> map, Map<String, FieldProperty<
1919
}
2020

2121
public static TestIngestCtxMetadata withNullableVersion(Map<String, Object> map) {
22-
Map<String, FieldProperty<?>> updatedProperties = new HashMap<>(IngestCtxMap.IngestMetadata.PROPERTIES);
22+
Map<String, FieldProperty<?>> updatedProperties = new HashMap<>(IngestDocMetadata.PROPERTIES);
2323
updatedProperties.replace(VERSION, new Metadata.FieldProperty<>(Number.class, true, true, FieldProperty.LONGABLE_NUMBER));
2424
return new TestIngestCtxMetadata(map, updatedProperties);
2525
}

test/framework/src/main/java/org/elasticsearch/ingest/TestIngestDocument.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
package org.elasticsearch.ingest;
1010

1111
import org.elasticsearch.common.lucene.uid.Versions;
12+
import org.elasticsearch.common.util.Maps;
1213
import org.elasticsearch.core.Tuple;
1314
import org.elasticsearch.index.VersionType;
1415
import org.elasticsearch.script.Metadata;
1516
import org.elasticsearch.test.ESTestCase;
1617

17-
import java.util.Arrays;
1818
import java.util.HashMap;
1919
import java.util.Map;
20-
import java.util.stream.Collectors;
2120

2221
/**
2322
* Construct ingest documents for testing purposes
@@ -38,11 +37,15 @@ public static IngestDocument withNullableVersion(Map<String, Object> sourceAndMe
3837
* _versions. Normally null _version is not allowed, but many tests don't care about that invariant.
3938
*/
4039
public static IngestDocument ofIngestWithNullableVersion(Map<String, Object> sourceAndMetadata, Map<String, Object> ingestMetadata) {
41-
Tuple<Map<String, Object>, Map<String, Object>> sm = IngestCtxMap.splitSourceAndMetadata(
42-
sourceAndMetadata,
43-
Arrays.stream(IngestDocument.Metadata.values()).map(IngestDocument.Metadata::getFieldName).collect(Collectors.toSet())
44-
);
45-
return new IngestDocument(new IngestCtxMap(sm.v1(), TestIngestCtxMetadata.withNullableVersion(sm.v2())), ingestMetadata);
40+
Map<String, Object> source = new HashMap<>(sourceAndMetadata);
41+
Map<String, Object> metadata = Maps.newHashMapWithExpectedSize(IngestDocument.Metadata.values().length);
42+
for (IngestDocument.Metadata m : IngestDocument.Metadata.values()) {
43+
String key = m.getFieldName();
44+
if (sourceAndMetadata.containsKey(key)) {
45+
metadata.put(key, source.remove(key));
46+
}
47+
}
48+
return new IngestDocument(new IngestCtxMap(source, TestIngestCtxMetadata.withNullableVersion(metadata)), ingestMetadata);
4649
}
4750

4851
/**

0 commit comments

Comments
 (0)