Skip to content

Commit bbe97b0

Browse files
authored
remove usages of #readOptionalStreamable, #readStreamableList. (#44578)
This commit removes references to Streamable from StreamInput. This is all a part of the effort to remove Streamable usage. relates #34389.
1 parent 78c67dc commit bbe97b0

File tree

9 files changed

+80
-134
lines changed

9 files changed

+80
-134
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/RestoreSnapshotResponse.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void readFrom(StreamInput in) throws IOException {
6969

7070
@Override
7171
public void writeTo(StreamOutput out) throws IOException {
72-
out.writeOptionalStreamable(restoreInfo);
72+
out.writeOptionalWriteable(restoreInfo);
7373
}
7474

7575
public RestStatus status() {

server/src/main/java/org/elasticsearch/action/admin/indices/stats/ShardStats.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void readFrom(StreamInput in) throws IOException {
134134
public void writeTo(StreamOutput out) throws IOException {
135135
shardRouting.writeTo(out);
136136
commonStats.writeTo(out);
137-
out.writeOptionalStreamable(commitStats);
137+
out.writeOptionalWriteable(commitStats);
138138
out.writeString(statePath);
139139
out.writeString(dataPath);
140140
out.writeBoolean(isCustomDataPath);

server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java

-38
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
import java.util.Set;
7676
import java.util.concurrent.TimeUnit;
7777
import java.util.function.IntFunction;
78-
import java.util.function.Supplier;
7978

8079
import static org.elasticsearch.ElasticsearchException.readStackTrace;
8180

@@ -825,20 +824,6 @@ public <T> T[] readOptionalArray(Writeable.Reader<T> reader, IntFunction<T[]> ar
825824
return readBoolean() ? readArray(reader, arraySupplier) : null;
826825
}
827826

828-
/**
829-
* Serializes a potential null value.
830-
*/
831-
@Nullable
832-
public <T extends Streamable> T readOptionalStreamable(Supplier<T> supplier) throws IOException {
833-
if (readBoolean()) {
834-
T streamable = supplier.get();
835-
streamable.readFrom(this);
836-
return streamable;
837-
} else {
838-
return null;
839-
}
840-
}
841-
842827
@Nullable
843828
public <T extends Writeable> T readOptionalWriteable(Writeable.Reader<T> reader) throws IOException {
844829
if (readBoolean()) {
@@ -991,29 +976,6 @@ public <C extends NamedWriteable> C readOptionalNamedWriteable(Class<C> category
991976
return null;
992977
}
993978

994-
/**
995-
* Read a {@link List} of {@link Streamable} objects, using the {@code constructor} to instantiate each instance.
996-
* <p>
997-
* This is expected to take the form:
998-
* <code>
999-
* List&lt;MyStreamableClass&gt; list = in.readStreamList(MyStreamableClass::new);
1000-
* </code>
1001-
*
1002-
* @param constructor Streamable instance creator
1003-
* @return Never {@code null}.
1004-
* @throws IOException if any step fails
1005-
*/
1006-
public <T extends Streamable> List<T> readStreamableList(Supplier<T> constructor) throws IOException {
1007-
int count = readArraySize();
1008-
List<T> builder = new ArrayList<>(count);
1009-
for (int i=0; i<count; i++) {
1010-
T instance = constructor.get();
1011-
instance.readFrom(this);
1012-
builder.add(instance);
1013-
}
1014-
return builder;
1015-
}
1016-
1017979
/**
1018980
* Reads a list of objects. The list is expected to have been written using {@link StreamOutput#writeList(List)} or
1019981
* {@link StreamOutput#writeStreamableList(List)}.

server/src/main/java/org/elasticsearch/index/engine/CommitStats.java

+17-21
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.apache.lucene.index.SegmentInfos;
2222
import org.elasticsearch.common.io.stream.StreamInput;
2323
import org.elasticsearch.common.io.stream.StreamOutput;
24-
import org.elasticsearch.common.io.stream.Streamable;
24+
import org.elasticsearch.common.io.stream.Writeable;
2525
import org.elasticsearch.common.lucene.Lucene;
2626
import org.elasticsearch.common.util.Maps;
2727
import org.elasticsearch.common.xcontent.ToXContentFragment;
@@ -35,12 +35,12 @@
3535
import static java.util.Map.entry;
3636

3737
/** a class the returns dynamic information with respect to the last commit point of this shard */
38-
public final class CommitStats implements Streamable, ToXContentFragment {
38+
public final class CommitStats implements Writeable, ToXContentFragment {
3939

40-
private Map<String, String> userData;
41-
private long generation;
42-
private String id; // lucene commit id in base 64;
43-
private int numDocs;
40+
private final Map<String, String> userData;
41+
private final long generation;
42+
private final String id; // lucene commit id in base 64;
43+
private final int numDocs;
4444

4545
public CommitStats(SegmentInfos segmentInfos) {
4646
// clone the map to protect against concurrent changes
@@ -51,11 +51,20 @@ public CommitStats(SegmentInfos segmentInfos) {
5151
numDocs = Lucene.getNumDocs(segmentInfos);
5252
}
5353

54-
private CommitStats() {
54+
CommitStats(StreamInput in) throws IOException {
55+
final int length = in.readVInt();
56+
final var entries = new ArrayList<Map.Entry<String, String>>(length);
57+
for (int i = length; i > 0; i--) {
58+
entries.add(entry(in.readString(), in.readString()));
59+
}
60+
userData = Maps.ofEntries(entries);
61+
generation = in.readLong();
62+
id = in.readOptionalString();
63+
numDocs = in.readInt();
5564
}
5665

5766
public static CommitStats readOptionalCommitStatsFrom(StreamInput in) throws IOException {
58-
return in.readOptionalStreamable(CommitStats::new);
67+
return in.readOptionalWriteable(CommitStats::new);
5968
}
6069

6170

@@ -93,19 +102,6 @@ public int getNumDocs() {
93102
return numDocs;
94103
}
95104

96-
@Override
97-
public void readFrom(StreamInput in) throws IOException {
98-
final int length = in.readVInt();
99-
final var entries = new ArrayList<Map.Entry<String, String>>(length);
100-
for (int i = length; i > 0; i--) {
101-
entries.add(entry(in.readString(), in.readString()));
102-
}
103-
userData = Maps.ofEntries(entries);
104-
generation = in.readLong();
105-
id = in.readOptionalString();
106-
numDocs = in.readInt();
107-
}
108-
109105
@Override
110106
public void writeTo(StreamOutput out) throws IOException {
111107
out.writeVInt(userData.size());

server/src/main/java/org/elasticsearch/snapshots/RestoreInfo.java

+15-16
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.elasticsearch.common.Strings;
2323
import org.elasticsearch.common.io.stream.StreamInput;
2424
import org.elasticsearch.common.io.stream.StreamOutput;
25-
import org.elasticsearch.common.io.stream.Streamable;
25+
import org.elasticsearch.common.io.stream.Writeable;
2626
import org.elasticsearch.common.xcontent.ObjectParser;
2727
import org.elasticsearch.common.xcontent.ToXContentObject;
2828
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -39,7 +39,7 @@
3939
* <p>
4040
* Returned as part of {@link org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse}
4141
*/
42-
public class RestoreInfo implements ToXContentObject, Streamable {
42+
public class RestoreInfo implements ToXContentObject, Writeable {
4343

4444
private String name;
4545

@@ -59,6 +59,18 @@ public RestoreInfo(String name, List<String> indices, int totalShards, int succe
5959
this.successfulShards = successfulShards;
6060
}
6161

62+
public RestoreInfo(StreamInput in) throws IOException {
63+
name = in.readString();
64+
int size = in.readVInt();
65+
List<String> indicesListBuilder = new ArrayList<>();
66+
for (int i = 0; i < size; i++) {
67+
indicesListBuilder.add(in.readString());
68+
}
69+
indices = Collections.unmodifiableList(indicesListBuilder);
70+
totalShards = in.readVInt();
71+
successfulShards = in.readVInt();
72+
}
73+
6274
/**
6375
* Snapshot name
6476
*
@@ -149,19 +161,6 @@ public static RestoreInfo fromXContent(XContentParser parser) throws IOException
149161
return PARSER.parse(parser, null);
150162
}
151163

152-
@Override
153-
public void readFrom(StreamInput in) throws IOException {
154-
name = in.readString();
155-
int size = in.readVInt();
156-
List<String> indicesListBuilder = new ArrayList<>();
157-
for (int i = 0; i < size; i++) {
158-
indicesListBuilder.add(in.readString());
159-
}
160-
indices = Collections.unmodifiableList(indicesListBuilder);
161-
totalShards = in.readVInt();
162-
successfulShards = in.readVInt();
163-
}
164-
165164
@Override
166165
public void writeTo(StreamOutput out) throws IOException {
167166
out.writeString(name);
@@ -180,7 +179,7 @@ public void writeTo(StreamOutput out) throws IOException {
180179
* @return restore info
181180
*/
182181
public static RestoreInfo readOptionalRestoreInfo(StreamInput in) throws IOException {
183-
return in.readOptionalStreamable(RestoreInfo::new);
182+
return in.readOptionalWriteable(RestoreInfo::new);
184183
}
185184

186185
@Override

server/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -432,18 +432,18 @@ public String getWriteableName() {
432432

433433
public void testWriteStreamableList() throws IOException {
434434
final int size = randomIntBetween(0, 5);
435-
final List<TestStreamable> expected = new ArrayList<>(size);
435+
final List<TestWriteable> expected = new ArrayList<>(size);
436436

437437
for (int i = 0; i < size; ++i) {
438-
expected.add(new TestStreamable(randomBoolean()));
438+
expected.add(new TestWriteable(randomBoolean()));
439439
}
440440

441441
final BytesStreamOutput out = new BytesStreamOutput();
442-
out.writeStreamableList(expected);
442+
out.writeList(expected);
443443

444444
final StreamInput in = StreamInput.wrap(BytesReference.toBytes(out.bytes()));
445445

446-
final List<TestStreamable> loaded = in.readStreamableList(TestStreamable::new);
446+
final List<TestWriteable> loaded = in.readList(TestWriteable::new);
447447

448448
assertThat(loaded, hasSize(expected.size()));
449449

@@ -587,18 +587,15 @@ public void testReadWriteGeoPoint() throws IOException {
587587
}
588588
}
589589

590-
private static class TestStreamable implements Streamable {
590+
private static class TestWriteable implements Writeable {
591591

592592
private boolean value;
593593

594-
TestStreamable() { }
595-
596-
TestStreamable(boolean value) {
594+
TestWriteable(boolean value) {
597595
this.value = value;
598596
}
599597

600-
@Override
601-
public void readFrom(StreamInput in) throws IOException {
598+
TestWriteable(StreamInput in) throws IOException {
602599
value = in.readBoolean();
603600
}
604601

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/QueuedWatch.java

+9-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import org.elasticsearch.common.io.stream.StreamInput;
99
import org.elasticsearch.common.io.stream.StreamOutput;
10-
import org.elasticsearch.common.io.stream.Streamable;
10+
import org.elasticsearch.common.io.stream.Writeable;
1111
import org.elasticsearch.common.xcontent.ToXContentObject;
1212
import org.elasticsearch.common.xcontent.XContentBuilder;
1313

@@ -16,23 +16,27 @@
1616
import java.time.ZoneOffset;
1717
import java.time.ZonedDateTime;
1818

19-
public class QueuedWatch implements Streamable, ToXContentObject {
19+
public class QueuedWatch implements Writeable, ToXContentObject {
2020

2121
private String watchId;
2222
private String watchRecordId;
2323
private ZonedDateTime triggeredTime;
2424
private ZonedDateTime executionTime;
2525

26-
public QueuedWatch() {
27-
}
28-
2926
public QueuedWatch(WatchExecutionContext ctx) {
3027
this.watchId = ctx.id().watchId();
3128
this.watchRecordId = ctx.id().value();
3229
this.triggeredTime = ctx.triggerEvent().triggeredTime();
3330
this.executionTime = ctx.executionTime();
3431
}
3532

33+
public QueuedWatch(StreamInput in) throws IOException {
34+
watchId = in.readString();
35+
watchRecordId = in.readString();
36+
triggeredTime = Instant.ofEpochMilli(in.readVLong()).atZone(ZoneOffset.UTC);
37+
executionTime = Instant.ofEpochMilli(in.readVLong()).atZone(ZoneOffset.UTC);
38+
}
39+
3640
public String watchId() {
3741
return watchId;
3842
}
@@ -53,14 +57,6 @@ public void executionTime(ZonedDateTime executionTime) {
5357
this.executionTime = executionTime;
5458
}
5559

56-
@Override
57-
public void readFrom(StreamInput in) throws IOException {
58-
watchId = in.readString();
59-
watchRecordId = in.readString();
60-
triggeredTime = Instant.ofEpochMilli(in.readVLong()).atZone(ZoneOffset.UTC);
61-
executionTime = Instant.ofEpochMilli(in.readVLong()).atZone(ZoneOffset.UTC);
62-
}
63-
6460
@Override
6561
public void writeTo(StreamOutput out) throws IOException {
6662
out.writeString(watchId);

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionSnapshot.java

+25-29
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import org.elasticsearch.common.io.stream.StreamInput;
99
import org.elasticsearch.common.io.stream.StreamOutput;
10-
import org.elasticsearch.common.io.stream.Streamable;
10+
import org.elasticsearch.common.io.stream.Writeable;
1111
import org.elasticsearch.common.xcontent.ToXContentObject;
1212
import org.elasticsearch.common.xcontent.XContentBuilder;
1313
import org.elasticsearch.xpack.core.watcher.actions.ActionWrapperResult;
@@ -18,18 +18,15 @@
1818
import java.time.ZonedDateTime;
1919
import java.util.Map;
2020

21-
public class WatchExecutionSnapshot implements Streamable, ToXContentObject {
21+
public class WatchExecutionSnapshot implements Writeable, ToXContentObject {
2222

23-
private String watchId;
24-
private String watchRecordId;
25-
private ZonedDateTime triggeredTime;
26-
private ZonedDateTime executionTime;
27-
private ExecutionPhase phase;
23+
private final String watchId;
24+
private final String watchRecordId;
25+
private final ZonedDateTime triggeredTime;
26+
private final ZonedDateTime executionTime;
27+
private final ExecutionPhase phase;
28+
private final StackTraceElement[] executionStackTrace;
2829
private String[] executedActions;
29-
private StackTraceElement[] executionStackTrace;
30-
31-
public WatchExecutionSnapshot() {
32-
}
3330

3431
public WatchExecutionSnapshot(WatchExecutionContext context, StackTraceElement[] executionStackTrace) {
3532
watchId = context.id().watchId();
@@ -48,6 +45,23 @@ public WatchExecutionSnapshot(WatchExecutionContext context, StackTraceElement[]
4845
this.executionStackTrace = executionStackTrace;
4946
}
5047

48+
public WatchExecutionSnapshot(StreamInput in) throws IOException {
49+
watchId = in.readString();
50+
watchRecordId = in.readString();
51+
triggeredTime = Instant.ofEpochMilli(in.readVLong()).atZone(ZoneOffset.UTC);
52+
executionTime = Instant.ofEpochMilli(in.readVLong()).atZone(ZoneOffset.UTC);
53+
phase = ExecutionPhase.resolve(in.readString());
54+
int size = in.readVInt();
55+
executionStackTrace = new StackTraceElement[size];
56+
for (int i = 0; i < size; i++) {
57+
String declaringClass = in.readString();
58+
String methodName = in.readString();
59+
String fileName = in.readOptionalString();
60+
int lineNumber = in.readInt();
61+
executionStackTrace[i] = new StackTraceElement(declaringClass, methodName, fileName, lineNumber);
62+
}
63+
}
64+
5165
public String watchId() {
5266
return watchId;
5367
}
@@ -72,24 +86,6 @@ public StackTraceElement[] executionStackTrace() {
7286
return executionStackTrace;
7387
}
7488

75-
@Override
76-
public void readFrom(StreamInput in) throws IOException {
77-
watchId = in.readString();
78-
watchRecordId = in.readString();
79-
triggeredTime = Instant.ofEpochMilli(in.readVLong()).atZone(ZoneOffset.UTC);
80-
executionTime = Instant.ofEpochMilli(in.readVLong()).atZone(ZoneOffset.UTC);
81-
phase = ExecutionPhase.resolve(in.readString());
82-
int size = in.readVInt();
83-
executionStackTrace = new StackTraceElement[size];
84-
for (int i = 0; i < size; i++) {
85-
String declaringClass = in.readString();
86-
String methodName = in.readString();
87-
String fileName = in.readOptionalString();
88-
int lineNumber = in.readInt();
89-
executionStackTrace[i] = new StackTraceElement(declaringClass, methodName, fileName, lineNumber);
90-
}
91-
}
92-
9389
@Override
9490
public void writeTo(StreamOutput out) throws IOException {
9591
out.writeString(watchId);

0 commit comments

Comments
 (0)