Skip to content

Commit 73044ac

Browse files
committed
Add writing and reading of String lists to StreamIO classes
1 parent 3ac5271 commit 73044ac

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,21 @@ public String[] readStringArray() throws IOException {
335335
return ret;
336336
}
337337

338+
/**
339+
* Read in a list of strings. List can be empty but not {@code null}.
340+
*/
341+
public List<String> readStringList() throws IOException {
342+
int size = readVInt();
343+
if (size == 0) {
344+
return Collections.emptyList();
345+
}
346+
List<String> ret = new ArrayList<>(size);
347+
for (int i = 0; i < size; i++) {
348+
ret.add(readString());
349+
}
350+
return ret;
351+
}
352+
338353
@Nullable
339354
public Map<String, Object> readMap() throws IOException {
340355
return (Map<String, Object>) readGenericValue();
@@ -427,7 +442,7 @@ public int[] readIntArray() throws IOException {
427442
}
428443
return values;
429444
}
430-
445+
431446
public long[] readLongArray() throws IOException {
432447
int length = readVInt();
433448
long[] values = new long[length];
@@ -436,7 +451,7 @@ public long[] readLongArray() throws IOException {
436451
}
437452
return values;
438453
}
439-
454+
440455
public float[] readFloatArray() throws IOException {
441456
int length = readVInt();
442457
float[] values = new float[length];
@@ -445,7 +460,7 @@ public float[] readFloatArray() throws IOException {
445460
}
446461
return values;
447462
}
448-
463+
449464
public double[] readDoubleArray() throws IOException {
450465
int length = readVInt();
451466
double[] values = new double[length];

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,16 @@ public void writeStringArray(String[] array) throws IOException {
283283
}
284284
}
285285

286+
/**
287+
* Write a list of strings. List can be empty but not {@code null}.
288+
*/
289+
public void writeStringList(List<String> stringList) throws IOException {
290+
writeVInt(stringList.size());
291+
for (String s : stringList) {
292+
writeString(s);
293+
}
294+
}
295+
286296
/**
287297
* Writes a string array, for nullable string, writes it as 0 (empty string).
288298
*/
@@ -399,21 +409,21 @@ public void writeIntArray(int[] value) throws IOException {
399409
writeInt(value[i]);
400410
}
401411
}
402-
412+
403413
public void writeLongArray(long[] value) throws IOException {
404414
writeVInt(value.length);
405415
for (int i=0; i<value.length; i++) {
406416
writeLong(value[i]);
407417
}
408418
}
409-
419+
410420
public void writeFloatArray(float[] value) throws IOException {
411421
writeVInt(value.length);
412422
for (int i=0; i<value.length; i++) {
413423
writeFloat(value[i]);
414424
}
415425
}
416-
426+
417427
public void writeDoubleArray(double[] value) throws IOException {
418428
writeVInt(value.length);
419429
for (int i=0; i<value.length; i++) {

src/main/java/org/elasticsearch/index/query/IdsQueryBuilder.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
123123
builder.startObject(IdsQueryParser.NAME);
124124
if (types != null) {
125125
if (types.size() == 1) {
126-
builder.field("type", types.iterator().next());
126+
builder.field("type", types.get(0));
127127
} else {
128128
builder.startArray("types");
129129
for (String type : types) {
@@ -177,19 +177,18 @@ public QueryValidationException validate() {
177177
return null;
178178
}
179179

180-
@SuppressWarnings("unchecked")
181180
@Override
182181
public void readFrom(StreamInput in) throws IOException {
183-
this.types = (List<String>) in.readGenericValue();
184-
this.ids = (List<String>) in.readGenericValue();
182+
this.types = in.readStringList();
183+
this.ids = in.readStringList();
185184
queryName = in.readOptionalString();
186185
boost = in.readFloat();
187186
}
188187

189188
@Override
190189
public void writeTo(StreamOutput out) throws IOException {
191-
out.writeGenericValue(this.types);
192-
out.writeGenericValue(this.ids);
190+
out.writeStringList(this.types);
191+
out.writeStringList(this.ids);
193192
out.writeOptionalString(queryName);
194193
out.writeFloat(boost);
195194
}

0 commit comments

Comments
 (0)