Skip to content

Commit e823da8

Browse files
committed
Use boolean isOpenSearch instead of String distribution in RemoteVersion
Simplifies the RemoteVersion class by replacing the distribution string field with a boolean flag, making the code more readable and maintainable. Signed-off-by: Hyunsang Han <[email protected]>
1 parent b7df3e1 commit e823da8

File tree

3 files changed

+64
-139
lines changed

3 files changed

+64
-139
lines changed

modules/reindex/src/main/java/org/opensearch/index/reindex/remote/RemoteResponseParsers.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,14 @@ public void setCausedBy(Throwable causedBy) {
300300
a -> (RemoteVersion) a[0]
301301
);
302302
static {
303-
ConstructingObjectParser<RemoteVersion, MediaType> versionParser = new ConstructingObjectParser<>("version", true, a -> {
304-
String distribution = (String) a[0];
305-
String number = (String) a[1];
306-
String cleanNumber = number.replace("-SNAPSHOT", "").replaceFirst("-(alpha\\d+|beta\\d+|rc\\d+)", "");
307-
return RemoteVersion.fromString(cleanNumber, distribution);
308-
});
303+
ConstructingObjectParser<RemoteVersion, MediaType> versionParser = new ConstructingObjectParser<>(
304+
"version",
305+
true,
306+
a -> RemoteVersion.fromString(
307+
((String) a[1]).replace("-SNAPSHOT", "").replaceFirst("-(alpha\\d+|beta\\d+|rc\\d+)", ""),
308+
a[0] != null
309+
)
310+
);
309311
versionParser.declareStringOrNull(optionalConstructorArg(), new ParseField("distribution"));
310312
versionParser.declareString(constructorArg(), new ParseField("number"));
311313
MAIN_ACTION_PARSER.declareObject(constructorArg(), versionParser, new ParseField("version"));

modules/reindex/src/main/java/org/opensearch/index/reindex/remote/RemoteVersion.java

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
package org.opensearch.index.reindex.remote;
1010

11-
import java.util.Locale;
1211
import java.util.Objects;
1312

1413
/**
@@ -21,37 +20,37 @@ public final class RemoteVersion implements Comparable<RemoteVersion> {
2120
private final int major;
2221
private final int minor;
2322
private final int revision;
24-
private final String distribution;
23+
final boolean isOpenSearch;
2524

2625
// Common version constants for backward compatibility
27-
public static final RemoteVersion ELASTICSEARCH_0_20_5 = new RemoteVersion(0, 20, 5, null);
28-
public static final RemoteVersion ELASTICSEARCH_0_90_13 = new RemoteVersion(0, 90, 13, null);
29-
public static final RemoteVersion ELASTICSEARCH_1_0_0 = new RemoteVersion(1, 0, 0, null);
30-
public static final RemoteVersion ELASTICSEARCH_1_7_5 = new RemoteVersion(1, 7, 5, null);
31-
public static final RemoteVersion ELASTICSEARCH_2_0_0 = new RemoteVersion(2, 0, 0, null);
32-
public static final RemoteVersion ELASTICSEARCH_2_1_0 = new RemoteVersion(2, 1, 0, null);
33-
public static final RemoteVersion ELASTICSEARCH_2_3_3 = new RemoteVersion(2, 3, 3, null);
34-
public static final RemoteVersion ELASTICSEARCH_5_0_0 = new RemoteVersion(5, 0, 0, null);
35-
public static final RemoteVersion ELASTICSEARCH_6_0_0 = new RemoteVersion(6, 0, 0, null);
36-
public static final RemoteVersion ELASTICSEARCH_6_3_0 = new RemoteVersion(6, 3, 0, null);
37-
public static final RemoteVersion ELASTICSEARCH_7_0_0 = new RemoteVersion(7, 0, 0, null);
26+
public static final RemoteVersion ELASTICSEARCH_0_20_5 = new RemoteVersion(0, 20, 5, false);
27+
public static final RemoteVersion ELASTICSEARCH_0_90_13 = new RemoteVersion(0, 90, 13, false);
28+
public static final RemoteVersion ELASTICSEARCH_1_0_0 = new RemoteVersion(1, 0, 0, false);
29+
public static final RemoteVersion ELASTICSEARCH_1_7_5 = new RemoteVersion(1, 7, 5, false);
30+
public static final RemoteVersion ELASTICSEARCH_2_0_0 = new RemoteVersion(2, 0, 0, false);
31+
public static final RemoteVersion ELASTICSEARCH_2_1_0 = new RemoteVersion(2, 1, 0, false);
32+
public static final RemoteVersion ELASTICSEARCH_2_3_3 = new RemoteVersion(2, 3, 3, false);
33+
public static final RemoteVersion ELASTICSEARCH_5_0_0 = new RemoteVersion(5, 0, 0, false);
34+
public static final RemoteVersion ELASTICSEARCH_6_0_0 = new RemoteVersion(6, 0, 0, false);
35+
public static final RemoteVersion ELASTICSEARCH_6_3_0 = new RemoteVersion(6, 3, 0, false);
36+
public static final RemoteVersion ELASTICSEARCH_7_0_0 = new RemoteVersion(7, 0, 0, false);
3837

3938
// OpenSearch versions
40-
public static final RemoteVersion OPENSEARCH_1_0_0 = new RemoteVersion(1, 0, 0, "opensearch");
41-
public static final RemoteVersion OPENSEARCH_2_0_0 = new RemoteVersion(2, 0, 0, "opensearch");
42-
public static final RemoteVersion OPENSEARCH_3_1_0 = new RemoteVersion(3, 1, 0, "opensearch");
39+
public static final RemoteVersion OPENSEARCH_1_0_0 = new RemoteVersion(1, 0, 0, true);
40+
public static final RemoteVersion OPENSEARCH_2_0_0 = new RemoteVersion(2, 0, 0, true);
41+
public static final RemoteVersion OPENSEARCH_3_1_0 = new RemoteVersion(3, 1, 0, true);
4342

44-
public RemoteVersion(int major, int minor, int revision, String distribution) {
43+
public RemoteVersion(int major, int minor, int revision, boolean isOpenSearch) {
4544
this.major = major;
4645
this.minor = minor;
4746
this.revision = revision;
48-
this.distribution = distribution != null ? distribution.toLowerCase(Locale.ROOT) : "elasticsearch";
47+
this.isOpenSearch = isOpenSearch;
4948
}
5049

5150
/**
5251
* Parse version string like "7.10.2" or "1.0.0"
5352
*/
54-
public static RemoteVersion fromString(String version, String distribution) {
53+
public static RemoteVersion fromString(String version, boolean isOpenSearch) {
5554
if (version == null || version.trim().isEmpty()) {
5655
throw new IllegalArgumentException("Version string cannot be null or empty");
5756
}
@@ -69,16 +68,12 @@ public static RemoteVersion fromString(String version, String distribution) {
6968
int minor = Integer.parseInt(parts[1]);
7069
int revision = parts.length > 2 ? Integer.parseInt(parts[2]) : 0;
7170

72-
return new RemoteVersion(major, minor, revision, distribution);
71+
return new RemoteVersion(major, minor, revision, isOpenSearch);
7372
} catch (NumberFormatException e) {
7473
throw new IllegalArgumentException("Invalid version format: " + version, e);
7574
}
7675
}
7776

78-
public static RemoteVersion fromString(String version) {
79-
return fromString(version, "elasticsearch");
80-
}
81-
8277
public boolean before(RemoteVersion other) {
8378
return this.compareTo(other) < 0;
8479
}
@@ -95,14 +90,6 @@ public boolean after(RemoteVersion other) {
9590
return this.compareTo(other) > 0;
9691
}
9792

98-
public boolean isOpenSearch() {
99-
return "opensearch".equals(distribution);
100-
}
101-
102-
public boolean isElasticsearch() {
103-
return "elasticsearch".equals(distribution);
104-
}
105-
10693
@Override
10794
public int compareTo(RemoteVersion other) {
10895
if (other == null) {
@@ -131,12 +118,12 @@ public boolean equals(Object obj) {
131118
return false;
132119
}
133120
RemoteVersion that = (RemoteVersion) obj;
134-
return major == that.major && minor == that.minor && revision == that.revision && Objects.equals(distribution, that.distribution);
121+
return major == that.major && minor == that.minor && revision == that.revision && Objects.equals(isOpenSearch, that.isOpenSearch);
135122
}
136123

137124
@Override
138125
public int hashCode() {
139-
return Objects.hash(major, minor, revision, distribution);
126+
return Objects.hash(major, minor, revision, isOpenSearch);
140127
}
141128

142129
@Override
@@ -155,8 +142,4 @@ public int getMinor() {
155142
public int getRevision() {
156143
return revision;
157144
}
158-
159-
public String getDistribution() {
160-
return distribution;
161-
}
162145
}

0 commit comments

Comments
 (0)