Skip to content

Commit 8c6ac01

Browse files
authored
Simplify serialization (#138178)
1 parent 0a81875 commit 8c6ac01

File tree

9 files changed

+150
-300
lines changed

9 files changed

+150
-300
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/index/EsIndex.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,18 @@
66
*/
77
package org.elasticsearch.xpack.esql.index;
88

9-
import org.elasticsearch.common.io.stream.StreamInput;
10-
import org.elasticsearch.common.io.stream.StreamOutput;
11-
import org.elasticsearch.common.io.stream.Writeable;
129
import org.elasticsearch.index.IndexMode;
1310
import org.elasticsearch.xpack.esql.core.type.EsField;
1411

15-
import java.io.IOException;
1612
import java.util.Map;
1713
import java.util.Set;
1814

1915
public record EsIndex(
2016
String name,
21-
/** Map of field names to {@link EsField} instances representing that field */
22-
Map<String, EsField> mapping,
17+
Map<String, EsField> mapping, // keyed by field names
2318
Map<String, IndexMode> indexNameWithModes,
24-
/** Fields mapped only in some (but *not* all) indices. Since this is only used by the analyzer, it is not serialized. */
2519
Set<String> partiallyUnmappedFields
26-
) implements Writeable {
20+
) {
2721

2822
public EsIndex {
2923
assert name != null;
@@ -42,22 +36,6 @@ public EsIndex(String name, Map<String, EsField> mapping) {
4236
this(name, mapping, Map.of(), Set.of());
4337
}
4438

45-
public static EsIndex readFrom(StreamInput in) throws IOException {
46-
String name = in.readString();
47-
Map<String, EsField> mapping = in.readImmutableMap(StreamInput::readString, EsField::readFrom);
48-
Map<String, IndexMode> indexNameWithModes = in.readMap(IndexMode::readFrom);
49-
// partially unmapped fields shouldn't pass the coordinator node anyway, since they are only used by the Analyzer.
50-
return new EsIndex(name, mapping, indexNameWithModes, Set.of());
51-
}
52-
53-
@Override
54-
public void writeTo(StreamOutput out) throws IOException {
55-
out.writeString(name());
56-
out.writeMap(mapping(), (o, x) -> x.writeTo(out));
57-
out.writeMap(indexNameWithModes, (o, v) -> IndexMode.writeTo(v, out));
58-
// partially unmapped fields shouldn't pass the coordinator node anyway, since they are only used by the Analyzer.
59-
}
60-
6139
public boolean isPartiallyUnmappedField(String fieldName) {
6240
return partiallyUnmappedFields.contains(fieldName);
6341
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/logical/EsRelation.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
1616
import org.elasticsearch.xpack.esql.core.tree.NodeUtils;
1717
import org.elasticsearch.xpack.esql.core.tree.Source;
18-
import org.elasticsearch.xpack.esql.index.EsIndex;
18+
import org.elasticsearch.xpack.esql.core.type.EsField;
1919
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
2020

2121
import java.io.IOException;
@@ -52,16 +52,12 @@ public EsRelation(
5252

5353
private static EsRelation readFrom(StreamInput in) throws IOException {
5454
Source source = Source.readFrom((PlanStreamInput) in);
55-
String indexPattern;
56-
Map<String, IndexMode> indexNameWithModes;
57-
if (in.getTransportVersion().supports(TransportVersions.V_8_18_0)) {
58-
indexPattern = in.readString();
59-
indexNameWithModes = in.readMap(IndexMode::readFrom);
60-
} else {
61-
var index = EsIndex.readFrom(in);
62-
indexPattern = index.name();
63-
indexNameWithModes = index.indexNameWithModes();
55+
String indexPattern = in.readString();
56+
if (in.getTransportVersion().supports(TransportVersions.V_8_18_0) == false) {
57+
// this used to be part of EsIndex deserialization
58+
in.readImmutableMap(StreamInput::readString, EsField::readFrom);
6459
}
60+
Map<String, IndexMode> indexNameWithModes = in.readMap(IndexMode::readFrom);
6561
List<Attribute> attributes = in.readNamedWriteableCollectionAsList(Attribute.class);
6662
IndexMode indexMode = IndexMode.fromString(in.readString());
6763
if (in.getTransportVersion().supports(TransportVersions.V_8_18_0) == false) {
@@ -73,12 +69,12 @@ private static EsRelation readFrom(StreamInput in) throws IOException {
7369
@Override
7470
public void writeTo(StreamOutput out) throws IOException {
7571
Source.EMPTY.writeTo(out);
76-
if (out.getTransportVersion().supports(TransportVersions.V_8_18_0)) {
77-
out.writeString(indexPattern);
78-
out.writeMap(indexNameWithModes, (o, v) -> IndexMode.writeTo(v, out));
79-
} else {
80-
new EsIndex(indexPattern, Map.of(), indexNameWithModes).writeTo(out);
72+
out.writeString(indexPattern);
73+
if (out.getTransportVersion().supports(TransportVersions.V_8_18_0) == false) {
74+
// this used to be part of EsIndex serialization
75+
out.writeMap(Map.<String, EsField>of(), (o, x) -> x.writeTo(out));
8176
}
77+
out.writeMap(indexNameWithModes, (o, v) -> IndexMode.writeTo(v, out));
8278
out.writeNamedWriteableCollection(attrs);
8379
out.writeString(indexMode.getName());
8480
if (out.getTransportVersion().supports(TransportVersions.V_8_18_0) == false) {

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/physical/EsSourceExec.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
1818
import org.elasticsearch.xpack.esql.core.tree.NodeUtils;
1919
import org.elasticsearch.xpack.esql.core.tree.Source;
20-
import org.elasticsearch.xpack.esql.index.EsIndex;
20+
import org.elasticsearch.xpack.esql.core.type.EsField;
2121
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
2222
import org.elasticsearch.xpack.esql.plan.logical.EsRelation;
2323

@@ -61,15 +61,10 @@ public EsSourceExec(
6161

6262
private static EsSourceExec readFrom(StreamInput in) throws IOException {
6363
var source = Source.readFrom((PlanStreamInput) in);
64-
String indexPattern;
65-
Map<String, IndexMode> indexNameWithModes;
66-
if (in.getTransportVersion().supports(TransportVersions.V_8_18_0)) {
67-
indexPattern = in.readString();
68-
indexNameWithModes = in.readMap(IndexMode::readFrom);
69-
} else {
70-
var index = EsIndex.readFrom(in);
71-
indexPattern = index.name();
72-
indexNameWithModes = index.indexNameWithModes();
64+
String indexPattern = in.readString();
65+
Map<String, IndexMode> indexNameWithModes = in.readMap(IndexMode::readFrom);
66+
if (in.getTransportVersion().supports(TransportVersions.V_8_18_0) == false) {
67+
in.readImmutableMap(StreamInput::readString, EsField::readFrom);
7368
}
7469
var attributes = in.readNamedWriteableCollectionAsList(Attribute.class);
7570
var query = in.readOptionalNamedWriteable(QueryBuilder.class);
@@ -80,12 +75,11 @@ private static EsSourceExec readFrom(StreamInput in) throws IOException {
8075
@Override
8176
public void writeTo(StreamOutput out) throws IOException {
8277
Source.EMPTY.writeTo(out);
83-
if (out.getTransportVersion().supports(TransportVersions.V_8_18_0)) {
84-
out.writeString(indexPattern);
85-
out.writeMap(indexNameWithModes, (o, v) -> IndexMode.writeTo(v, out));
86-
} else {
87-
new EsIndex(indexPattern, Map.of(), indexNameWithModes).writeTo(out);
78+
out.writeString(indexPattern);
79+
if (out.getTransportVersion().supports(TransportVersions.V_8_18_0) == false) {
80+
out.writeMap(Map.<String, EsField>of(), (o, x) -> x.writeTo(out));
8881
}
82+
out.writeMap(indexNameWithModes, (o, v) -> IndexMode.writeTo(v, out));
8983
out.writeNamedWriteableCollection(output());
9084
out.writeOptionalNamedWriteable(query());
9185
out.writeString(indexMode().getName());
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.index;
9+
10+
import org.elasticsearch.index.IndexMode;
11+
import org.elasticsearch.test.ESTestCase;
12+
import org.elasticsearch.xpack.esql.core.type.EsField;
13+
import org.elasticsearch.xpack.esql.type.EsFieldTests;
14+
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
18+
import static org.elasticsearch.core.Tuple.tuple;
19+
20+
public class EsIndexGenerator {
21+
22+
public static EsIndex randomEsIndex() {
23+
String name = ESTestCase.randomIdentifier();
24+
Map<String, EsField> mapping = randomMapping();
25+
return new EsIndex(name, mapping, randomIndexNameWithModes());
26+
}
27+
28+
public static Map<String, EsField> randomMapping() {
29+
int size = ESTestCase.between(0, 10);
30+
Map<String, EsField> result = new HashMap<>(size);
31+
while (result.size() < size) {
32+
result.put(ESTestCase.randomIdentifier(), EsFieldTests.randomAnyEsField(1));
33+
}
34+
return result;
35+
}
36+
37+
public static Map<String, IndexMode> randomIndexNameWithModes() {
38+
return ESTestCase.randomMap(0, 10, () -> tuple(ESTestCase.randomIdentifier(), ESTestCase.randomFrom(IndexMode.values())));
39+
}
40+
}

0 commit comments

Comments
 (0)