Skip to content

Commit 5047e40

Browse files
committed
Liberalize StreamOutput#writeStringList (#37768)
In some cases we only have a string collection instead of a string list that we want to serialize out. We have a convenience method for writing a list of strings, but no such method for writing a collection of strings. Yet, a list of strings is a collection of strings, so we can simply liberalize StreamOutput#writeStringList to be more generous in the collections that it accepts and write out collections of strings too. On the other side, we do not have a convenience method for reading a list of strings. This commit addresses both of these issues.
1 parent c364891 commit 5047e40

File tree

29 files changed

+114
-81
lines changed

29 files changed

+114
-81
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ public void readFrom(StreamInput in) throws IOException {
462462
name = in.readString();
463463

464464
if (in.getVersion().onOrAfter(Version.V_6_0_0_alpha1)) {
465-
indexPatterns = in.readList(StreamInput::readString);
465+
indexPatterns = in.readStringList();
466466
} else {
467467
indexPatterns = Collections.singletonList(in.readString());
468468
}
@@ -501,7 +501,7 @@ public void writeTo(StreamOutput out) throws IOException {
501501
out.writeString(cause);
502502
out.writeString(name);
503503
if (out.getVersion().onOrAfter(Version.V_6_0_0_alpha1)) {
504-
out.writeStringList(indexPatterns);
504+
out.writeStringCollection(indexPatterns);
505505
} else {
506506
out.writeString(indexPatterns.size() > 0 ? indexPatterns.get(0) : "");
507507
}

server/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateMetaData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public static IndexTemplateMetaData readFrom(StreamInput in) throws IOException
198198
Builder builder = new Builder(in.readString());
199199
builder.order(in.readInt());
200200
if (in.getVersion().onOrAfter(Version.V_6_0_0_alpha1)) {
201-
builder.patterns(in.readList(StreamInput::readString));
201+
builder.patterns(in.readStringList());
202202
} else {
203203
builder.patterns(Collections.singletonList(in.readString()));
204204
}
@@ -235,7 +235,7 @@ public void writeTo(StreamOutput out) throws IOException {
235235
out.writeString(name);
236236
out.writeInt(order);
237237
if (out.getVersion().onOrAfter(Version.V_6_0_0_alpha1)) {
238-
out.writeStringList(patterns);
238+
out.writeStringCollection(patterns);
239239
} else {
240240
out.writeString(patterns.get(0));
241241
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -935,12 +935,26 @@ public <T extends Streamable> List<T> readStreamableList(Supplier<T> constructor
935935
}
936936

937937
/**
938-
* Reads a list of objects
938+
* Reads a list of objects. The list is expected to have been written using {@link StreamOutput#writeList(List)} or
939+
* {@link StreamOutput#writeStreamableList(List)}.
940+
*
941+
* @return the list of objects
942+
* @throws IOException if an I/O exception occurs reading the list
939943
*/
940-
public <T> List<T> readList(Writeable.Reader<T> reader) throws IOException {
944+
public <T> List<T> readList(final Writeable.Reader<T> reader) throws IOException {
941945
return readCollection(reader, ArrayList::new);
942946
}
943947

948+
/**
949+
* Reads a list of strings. The list is expected to have been written using {@link StreamOutput#writeStringCollection(Collection)}.
950+
*
951+
* @return the list of strings
952+
* @throws IOException if an I/O exception occurs reading the list
953+
*/
954+
public List<String> readStringList() throws IOException {
955+
return readList(StreamInput::readString);
956+
}
957+
944958
/**
945959
* Reads a set of objects
946960
*/

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,23 +1043,27 @@ public void writeList(List<? extends Writeable> list) throws IOException {
10431043
}
10441044

10451045
/**
1046-
* Writes a collection of generic objects via a {@link Writer}
1046+
* Writes a collection of objects via a {@link Writer}.
1047+
*
1048+
* @param collection the collection of objects
1049+
* @throws IOException if an I/O exception occurs writing the collection
10471050
*/
1048-
public <T> void writeCollection(Collection<T> collection, Writer<T> writer) throws IOException {
1051+
public <T> void writeCollection(final Collection<T> collection, final Writer<T> writer) throws IOException {
10491052
writeVInt(collection.size());
1050-
for (T val: collection) {
1053+
for (final T val: collection) {
10511054
writer.write(this, val);
10521055
}
10531056
}
10541057

10551058
/**
1056-
* Writes a list of strings
1059+
* Writes a collection of a strings. The corresponding collection can be read from a stream input using
1060+
* {@link StreamInput#readList(Writeable.Reader)}.
1061+
*
1062+
* @param collection the collection of strings
1063+
* @throws IOException if an I/O exception occurs writing the collection
10571064
*/
1058-
public void writeStringList(List<String> list) throws IOException {
1059-
writeVInt(list.size());
1060-
for (String string: list) {
1061-
this.writeString(string);
1062-
}
1065+
public void writeStringCollection(final Collection<String> collection) throws IOException {
1066+
writeCollection(collection, StreamOutput::writeString);
10631067
}
10641068

10651069
/**

server/src/main/java/org/elasticsearch/indices/recovery/RecoveryResponse.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public final class RecoveryResponse extends TransportResponse {
6060

6161
RecoveryResponse(StreamInput in) throws IOException {
6262
super(in);
63-
phase1FileNames = in.readList(StreamInput::readString);
63+
phase1FileNames = in.readStringList();
6464
phase1FileSizes = in.readList(StreamInput::readVLong);
65-
phase1ExistingFileNames = in.readList(StreamInput::readString);
65+
phase1ExistingFileNames = in.readStringList();
6666
phase1ExistingFileSizes = in.readList(StreamInput::readVLong);
6767
phase1TotalSize = in.readVLong();
6868
phase1ExistingTotalSize = in.readVLong();
@@ -76,9 +76,9 @@ public final class RecoveryResponse extends TransportResponse {
7676
@Override
7777
public void writeTo(StreamOutput out) throws IOException {
7878
super.writeTo(out);
79-
out.writeStringList(phase1FileNames);
79+
out.writeStringCollection(phase1FileNames);
8080
out.writeCollection(phase1FileSizes, StreamOutput::writeVLong);
81-
out.writeStringList(phase1ExistingFileNames);
81+
out.writeStringCollection(phase1ExistingFileNames);
8282
out.writeCollection(phase1ExistingFileSizes, StreamOutput::writeVLong);
8383
out.writeVLong(phase1TotalSize);
8484
out.writeVLong(phase1ExistingTotalSize);

server/src/main/java/org/elasticsearch/plugins/PluginInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public PluginInfo(final StreamInput in) throws IOException {
103103
}
104104
this.classname = in.readString();
105105
if (in.getVersion().onOrAfter(Version.V_6_2_0)) {
106-
extendedPlugins = in.readList(StreamInput::readString);
106+
extendedPlugins = in.readStringList();
107107
} else {
108108
extendedPlugins = Collections.emptyList();
109109
}
@@ -132,7 +132,7 @@ public void writeTo(final StreamOutput out) throws IOException {
132132
}
133133
out.writeString(classname);
134134
if (out.getVersion().onOrAfter(Version.V_6_2_0)) {
135-
out.writeStringList(extendedPlugins);
135+
out.writeStringCollection(extendedPlugins);
136136
}
137137
if (out.getVersion().onOrAfter(Version.V_5_4_0)) {
138138
out.writeBoolean(hasNativeController);

server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/InternalComposite.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class InternalComposite
6969
public InternalComposite(StreamInput in) throws IOException {
7070
super(in);
7171
this.size = in.readVInt();
72-
this.sourceNames = in.readList(StreamInput::readString);
72+
this.sourceNames = in.readStringList();
7373
this.formats = new ArrayList<>(sourceNames.size());
7474
for (int i = 0; i < sourceNames.size(); i++) {
7575
if (in.getVersion().onOrAfter(Version.V_6_3_0)) {
@@ -90,7 +90,7 @@ public InternalComposite(StreamInput in) throws IOException {
9090
@Override
9191
protected void doWriteTo(StreamOutput out) throws IOException {
9292
out.writeVInt(size);
93-
out.writeStringList(sourceNames);
93+
out.writeStringCollection(sourceNames);
9494
if (out.getVersion().onOrAfter(Version.V_6_3_0)) {
9595
for (DocValueFormat format : formats) {
9696
out.writeNamedWriteable(format);

server/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public SearchSourceBuilder(StreamInput in) throws IOException {
238238
}
239239
}
240240
if (in.readBoolean()) {
241-
stats = in.readList(StreamInput::readString);
241+
stats = in.readStringList();
242242
}
243243
suggestBuilder = in.readOptionalWriteable(SuggestBuilder::new);
244244
terminateAfter = in.readVInt();
@@ -303,7 +303,7 @@ public void writeTo(StreamOutput out) throws IOException {
303303
boolean hasStats = stats != null;
304304
out.writeBoolean(hasStats);
305305
if (hasStats) {
306-
out.writeStringList(stats);
306+
out.writeStringCollection(stats);
307307
}
308308
out.writeOptionalWriteable(suggestBuilder);
309309
out.writeVInt(terminateAfter);

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package org.elasticsearch.common.io.stream;
2121

2222
import org.apache.lucene.util.BytesRef;
23+
import org.elasticsearch.common.CheckedBiConsumer;
24+
import org.elasticsearch.common.CheckedFunction;
2325
import org.elasticsearch.common.bytes.BytesArray;
2426
import org.elasticsearch.common.bytes.BytesReference;
2527
import org.elasticsearch.common.collect.Tuple;
@@ -39,6 +41,7 @@
3941
import java.util.Map;
4042
import java.util.Objects;
4143
import java.util.Set;
44+
import java.util.function.Supplier;
4245
import java.util.stream.Collectors;
4346
import java.util.stream.IntStream;
4447

@@ -293,15 +296,27 @@ public int hashCode() {
293296

294297
}
295298

296-
final int length = randomIntBetween(0, 16);
297-
final Collection<FooBar> fooBars = new ArrayList<>(length);
299+
runWriteReadCollectionTest(
300+
() -> new FooBar(randomInt(), randomInt()), StreamOutput::writeCollection, in -> in.readList(FooBar::new));
301+
}
302+
303+
public void testStringCollection() throws IOException {
304+
runWriteReadCollectionTest(() -> randomUnicodeOfLength(16), StreamOutput::writeStringCollection, StreamInput::readStringList);
305+
}
306+
307+
private <T> void runWriteReadCollectionTest(
308+
final Supplier<T> supplier,
309+
final CheckedBiConsumer<StreamOutput, Collection<T>, IOException> writer,
310+
final CheckedFunction<StreamInput, Collection<T>, IOException> reader) throws IOException {
311+
final int length = randomIntBetween(0, 10);
312+
final Collection<T> collection = new ArrayList<>(length);
298313
for (int i = 0; i < length; i++) {
299-
fooBars.add(new FooBar(randomInt(), randomInt()));
314+
collection.add(supplier.get());
300315
}
301316
try (BytesStreamOutput out = new BytesStreamOutput()) {
302-
out.writeCollection(fooBars);
317+
writer.accept(out, collection);
303318
try (StreamInput in = out.bytes().streamInput()) {
304-
assertThat(fooBars, equalTo(in.readList(FooBar::new)));
319+
assertThat(collection, equalTo(reader.apply(in)));
305320
}
306321
}
307322
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowMetadata.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public AutoFollowPattern(String remoteCluster,
273273

274274
public AutoFollowPattern(StreamInput in) throws IOException {
275275
remoteCluster = in.readString();
276-
leaderIndexPatterns = in.readList(StreamInput::readString);
276+
leaderIndexPatterns = in.readStringList();
277277
followIndexPattern = in.readOptionalString();
278278
maxReadRequestOperationCount = in.readOptionalVInt();
279279
maxReadRequestSize = in.readOptionalWriteable(ByteSizeValue::new);
@@ -350,7 +350,7 @@ public TimeValue getPollTimeout() {
350350
@Override
351351
public void writeTo(StreamOutput out) throws IOException {
352352
out.writeString(remoteCluster);
353-
out.writeStringList(leaderIndexPatterns);
353+
out.writeStringCollection(leaderIndexPatterns);
354354
out.writeOptionalString(followIndexPattern);
355355
out.writeOptionalVInt(maxReadRequestOperationCount);
356356
out.writeOptionalWriteable(maxReadRequestSize);

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/PutAutoFollowPatternAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public Request(StreamInput in) throws IOException {
283283
super(in);
284284
name = in.readString();
285285
remoteCluster = in.readString();
286-
leaderIndexPatterns = in.readList(StreamInput::readString);
286+
leaderIndexPatterns = in.readStringList();
287287
followIndexNamePattern = in.readOptionalString();
288288
maxReadRequestOperationCount = in.readOptionalVInt();
289289
maxReadRequestSize = in.readOptionalWriteable(ByteSizeValue::new);
@@ -302,7 +302,7 @@ public void writeTo(StreamOutput out) throws IOException {
302302
super.writeTo(out);
303303
out.writeString(name);
304304
out.writeString(remoteCluster);
305-
out.writeStringList(leaderIndexPatterns);
305+
out.writeStringCollection(leaderIndexPatterns);
306306
out.writeOptionalString(followIndexNamePattern);
307307
out.writeOptionalVInt(maxReadRequestOperationCount);
308308
out.writeOptionalWriteable(maxReadRequestSize);

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/action/RemoveIndexLifecyclePolicyAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
8686
@Override
8787
public void readFrom(StreamInput in) throws IOException {
8888
super.readFrom(in);
89-
failedIndexes = in.readList(StreamInput::readString);
89+
failedIndexes = in.readStringList();
9090
}
9191

9292
@Override
9393
public void writeTo(StreamOutput out) throws IOException {
9494
super.writeTo(out);
95-
out.writeStringList(failedIndexes);
95+
out.writeStringCollection(failedIndexes);
9696
}
9797

9898
@Override

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/FindFileStructureAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public void readFrom(StreamInput in) throws IOException {
333333
timeout = in.readOptionalTimeValue();
334334
charset = in.readOptionalString();
335335
format = in.readBoolean() ? in.readEnum(FileStructure.Format.class) : null;
336-
columnNames = in.readBoolean() ? in.readList(StreamInput::readString) : null;
336+
columnNames = in.readBoolean() ? in.readStringList() : null;
337337
hasHeaderRow = in.readOptionalBoolean();
338338
delimiter = in.readBoolean() ? (char) in.readVInt() : null;
339339
quote = in.readBoolean() ? (char) in.readVInt() : null;

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetJobsStatsAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public Request() {}
8484
public Request(StreamInput in) throws IOException {
8585
super(in);
8686
jobId = in.readString();
87-
expandedJobsIds = in.readList(StreamInput::readString);
87+
expandedJobsIds = in.readStringList();
8888
if (in.getVersion().onOrAfter(Version.V_6_1_0)) {
8989
allowNoJobs = in.readBoolean();
9090
}
@@ -94,7 +94,7 @@ public Request(StreamInput in) throws IOException {
9494
public void writeTo(StreamOutput out) throws IOException {
9595
super.writeTo(out);
9696
out.writeString(jobId);
97-
out.writeStringList(expandedJobsIds);
97+
out.writeStringCollection(expandedJobsIds);
9898
if (out.getVersion().onOrAfter(Version.V_6_1_0)) {
9999
out.writeBoolean(allowNoJobs);
100100
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StartDatafeedAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public DatafeedParams(StreamInput in) throws IOException {
203203
timeout = TimeValue.timeValueMillis(in.readVLong());
204204
if (in.getVersion().onOrAfter(Version.V_6_6_0)) {
205205
jobId = in.readOptionalString();
206-
datafeedIndices = in.readList(StreamInput::readString);
206+
datafeedIndices = in.readStringList();
207207
}
208208
}
209209

@@ -280,7 +280,7 @@ public void writeTo(StreamOutput out) throws IOException {
280280
out.writeVLong(timeout.millis());
281281
if (out.getVersion().onOrAfter(Version.V_6_6_0)) {
282282
out.writeOptionalString(jobId);
283-
out.writeStringList(datafeedIndices);
283+
out.writeStringCollection(datafeedIndices);
284284
}
285285
}
286286

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfig.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,12 @@ public DatafeedConfig(StreamInput in) throws IOException {
254254
this.queryDelay = in.readOptionalTimeValue();
255255
this.frequency = in.readOptionalTimeValue();
256256
if (in.readBoolean()) {
257-
this.indices = Collections.unmodifiableList(in.readList(StreamInput::readString));
257+
this.indices = Collections.unmodifiableList(in.readStringList());
258258
} else {
259259
this.indices = null;
260260
}
261261
if (in.readBoolean()) {
262-
this.types = Collections.unmodifiableList(in.readList(StreamInput::readString));
262+
this.types = Collections.unmodifiableList(in.readStringList());
263263
} else {
264264
this.types = null;
265265
}
@@ -423,13 +423,13 @@ public void writeTo(StreamOutput out) throws IOException {
423423
out.writeOptionalTimeValue(frequency);
424424
if (indices != null) {
425425
out.writeBoolean(true);
426-
out.writeStringList(indices);
426+
out.writeStringCollection(indices);
427427
} else {
428428
out.writeBoolean(false);
429429
}
430430
if (types != null) {
431431
out.writeBoolean(true);
432-
out.writeStringList(types);
432+
out.writeStringCollection(types);
433433
} else {
434434
out.writeBoolean(false);
435435
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedUpdate.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ public DatafeedUpdate(StreamInput in) throws IOException {
110110
this.queryDelay = in.readOptionalTimeValue();
111111
this.frequency = in.readOptionalTimeValue();
112112
if (in.readBoolean()) {
113-
this.indices = in.readList(StreamInput::readString);
113+
this.indices = in.readStringList();
114114
} else {
115115
this.indices = null;
116116
}
117117
if (in.readBoolean()) {
118-
this.types = in.readList(StreamInput::readString);
118+
this.types = in.readStringList();
119119
} else {
120120
this.types = null;
121121
}
@@ -154,13 +154,13 @@ public void writeTo(StreamOutput out) throws IOException {
154154
out.writeOptionalTimeValue(frequency);
155155
if (indices != null) {
156156
out.writeBoolean(true);
157-
out.writeStringList(indices);
157+
out.writeStringCollection(indices);
158158
} else {
159159
out.writeBoolean(false);
160160
}
161161
if (types != null) {
162162
out.writeBoolean(true);
163-
out.writeStringList(types);
163+
out.writeStringCollection(types);
164164
} else {
165165
out.writeBoolean(false);
166166
}

0 commit comments

Comments
 (0)