Skip to content

Commit 716109b

Browse files
authored
Simplify EsIndex class (elastic#119582) (elastic#119628)
This change simplifies EsIndex class by replacing it with a record
1 parent 0ec4a99 commit 716109b

File tree

8 files changed

+32
-71
lines changed

8 files changed

+32
-71
lines changed

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

Lines changed: 21 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515

1616
import java.io.IOException;
1717
import java.util.Map;
18-
import java.util.Objects;
1918
import java.util.Set;
20-
import java.util.stream.Collectors;
2119

22-
public class EsIndex implements Writeable {
20+
import static java.util.stream.Collectors.toMap;
2321

24-
private final String name;
25-
private final Map<String, EsField> mapping;
26-
private final Map<String, IndexMode> indexNameWithModes;
22+
public record EsIndex(String name, Map<String, EsField> mapping, Map<String, IndexMode> indexNameWithModes) implements Writeable {
23+
24+
public EsIndex {
25+
assert name != null;
26+
assert mapping != null;
27+
}
2728

2829
/**
2930
* Intended for tests. Returns an index with an empty index mode map.
@@ -32,56 +33,32 @@ public EsIndex(String name, Map<String, EsField> mapping) {
3233
this(name, mapping, Map.of());
3334
}
3435

35-
public EsIndex(String name, Map<String, EsField> mapping, Map<String, IndexMode> indexNameWithModes) {
36-
assert name != null;
37-
assert mapping != null;
38-
this.name = name;
39-
this.mapping = mapping;
40-
this.indexNameWithModes = indexNameWithModes;
41-
}
42-
43-
public EsIndex(StreamInput in) throws IOException {
44-
this(in.readString(), in.readImmutableMap(StreamInput::readString, EsField::readFrom), readIndexNameWithModes(in));
45-
}
46-
47-
@Override
48-
public void writeTo(StreamOutput out) throws IOException {
49-
out.writeString(name());
50-
out.writeMap(mapping(), (o, x) -> x.writeTo(out));
51-
writeIndexNameWithModes(indexNameWithModes, out);
52-
}
53-
54-
@SuppressWarnings("unchecked")
55-
private static Map<String, IndexMode> readIndexNameWithModes(StreamInput in) throws IOException {
36+
public static EsIndex readFrom(StreamInput in) throws IOException {
37+
String name = in.readString();
38+
Map<String, EsField> mapping = in.readImmutableMap(StreamInput::readString, EsField::readFrom);
39+
Map<String, IndexMode> indexNameWithModes;
5640
if (in.getTransportVersion().onOrAfter(TransportVersions.V_8_16_0)) {
57-
return in.readMap(IndexMode::readFrom);
41+
indexNameWithModes = in.readMap(IndexMode::readFrom);
5842
} else {
43+
@SuppressWarnings("unchecked")
5944
Set<String> indices = (Set<String>) in.readGenericValue();
6045
assert indices != null;
61-
return indices.stream().collect(Collectors.toMap(e -> e, e -> IndexMode.STANDARD));
46+
indexNameWithModes = indices.stream().collect(toMap(e -> e, e -> IndexMode.STANDARD));
6247
}
48+
return new EsIndex(name, mapping, indexNameWithModes);
6349
}
6450

65-
private static void writeIndexNameWithModes(Map<String, IndexMode> concreteIndices, StreamOutput out) throws IOException {
51+
@Override
52+
public void writeTo(StreamOutput out) throws IOException {
53+
out.writeString(name());
54+
out.writeMap(mapping(), (o, x) -> x.writeTo(out));
6655
if (out.getTransportVersion().onOrAfter(TransportVersions.V_8_16_0)) {
67-
out.writeMap(concreteIndices, (o, v) -> IndexMode.writeTo(v, out));
56+
out.writeMap(indexNameWithModes, (o, v) -> IndexMode.writeTo(v, out));
6857
} else {
69-
out.writeGenericValue(concreteIndices.keySet());
58+
out.writeGenericValue(indexNameWithModes.keySet());
7059
}
7160
}
7261

73-
public String name() {
74-
return name;
75-
}
76-
77-
public Map<String, EsField> mapping() {
78-
return mapping;
79-
}
80-
81-
public Map<String, IndexMode> indexNameWithModes() {
82-
return indexNameWithModes;
83-
}
84-
8562
public Set<String> concreteIndices() {
8663
return indexNameWithModes.keySet();
8764
}
@@ -90,25 +67,4 @@ public Set<String> concreteIndices() {
9067
public String toString() {
9168
return name;
9269
}
93-
94-
@Override
95-
public int hashCode() {
96-
return Objects.hash(name, mapping, indexNameWithModes);
97-
}
98-
99-
@Override
100-
public boolean equals(Object obj) {
101-
if (this == obj) {
102-
return true;
103-
}
104-
105-
if (obj == null || getClass() != obj.getClass()) {
106-
return false;
107-
}
108-
109-
EsIndex other = (EsIndex) obj;
110-
return Objects.equals(name, other.name)
111-
&& Objects.equals(mapping, other.mapping)
112-
&& Objects.equals(indexNameWithModes, other.indexNameWithModes);
113-
}
11470
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private static Enrich readFrom(StreamInput in) throws IOException {
116116
if (in.getTransportVersion().onOrAfter(TransportVersions.V_8_13_0)) {
117117
concreteIndices = in.readMap(StreamInput::readString, StreamInput::readString);
118118
} else {
119-
EsIndex esIndex = new EsIndex(in);
119+
EsIndex esIndex = EsIndex.readFrom(in);
120120
if (esIndex.concreteIndices().size() > 1) {
121121
throw new IllegalStateException("expected a single enrich index; got " + esIndex);
122122
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public EsRelation(Source source, EsIndex index, List<Attribute> attributes, Inde
5858

5959
private static EsRelation readFrom(StreamInput in) throws IOException {
6060
Source source = Source.readFrom((PlanStreamInput) in);
61-
EsIndex esIndex = new EsIndex(in);
61+
EsIndex esIndex = EsIndex.readFrom(in);
6262
List<Attribute> attributes = in.readNamedWriteableCollectionAsList(Attribute.class);
6363
if (supportingEsSourceOptions(in.getTransportVersion())) {
6464
// We don't do anything with these strings

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private static EnrichExec readFrom(StreamInput in) throws IOException {
8787
concreteIndices = in.readMap(StreamInput::readString, StreamInput::readString);
8888
} else {
8989
mode = Enrich.Mode.ANY;
90-
EsIndex esIndex = new EsIndex(in);
90+
EsIndex esIndex = EsIndex.readFrom(in);
9191
if (esIndex.concreteIndices().size() != 1) {
9292
throw new IllegalStateException("expected a single concrete enrich index; got " + esIndex.concreteIndices());
9393
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public EsQueryExec(
138138
*/
139139
public static EsQueryExec deserialize(StreamInput in) throws IOException {
140140
var source = Source.readFrom((PlanStreamInput) in);
141-
var index = new EsIndex(in);
141+
var index = EsIndex.readFrom(in);
142142
var indexMode = EsRelation.readIndexMode(in);
143143
var attrs = in.readNamedWriteableCollectionAsList(Attribute.class);
144144
var query = in.readOptionalNamedWriteable(QueryBuilder.class);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public EsSourceExec(Source source, EsIndex index, List<Attribute> attributes, Qu
5151
private EsSourceExec(StreamInput in) throws IOException {
5252
this(
5353
Source.readFrom((PlanStreamInput) in),
54-
new EsIndex(in),
54+
EsIndex.readFrom(in),
5555
in.readNamedWriteableCollectionAsList(Attribute.class),
5656
in.readOptionalNamedWriteable(QueryBuilder.class),
5757
EsRelation.readIndexMode(in)

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/index/EsIndexSerializationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private static Map<String, IndexMode> randomConcreteIndices() {
5656

5757
@Override
5858
protected Writeable.Reader<EsIndex> instanceReader() {
59-
return a -> new EsIndex(new PlanStreamInput(a, a.namedWriteableRegistry(), null));
59+
return a -> EsIndex.readFrom(new PlanStreamInput(a, a.namedWriteableRegistry(), null));
6060
}
6161

6262
@Override

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/tree/EsqlNodeSubclassTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.elasticsearch.xpack.esql.expression.function.scalar.math.Pow;
4141
import org.elasticsearch.xpack.esql.expression.function.scalar.string.Concat;
4242
import org.elasticsearch.xpack.esql.expression.predicate.fulltext.FullTextPredicate;
43+
import org.elasticsearch.xpack.esql.index.EsIndex;
4344
import org.elasticsearch.xpack.esql.plan.logical.Dissect;
4445
import org.elasticsearch.xpack.esql.plan.logical.Grok;
4546
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
@@ -87,6 +88,7 @@
8788
import static java.util.Collections.emptyList;
8889
import static org.elasticsearch.xpack.esql.ConfigurationTestUtils.randomConfiguration;
8990
import static org.elasticsearch.xpack.esql.core.type.DataType.GEO_POINT;
91+
import static org.elasticsearch.xpack.esql.index.EsIndexSerializationTests.randomEsIndex;
9092
import static org.mockito.Mockito.mock;
9193

9294
/**
@@ -491,6 +493,9 @@ public void accept(Page page) {
491493
if (argClass == Configuration.class) {
492494
return randomConfiguration();
493495
}
496+
if (argClass == EsIndex.class) {
497+
return randomEsIndex();
498+
}
494499
if (argClass == JoinConfig.class) {
495500
return new JoinConfig(
496501
JoinTypes.LEFT,

0 commit comments

Comments
 (0)