From 73044accaa9657c74b9941f6c8f7e9fb657bb5b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Thu, 7 May 2015 16:25:15 +0200 Subject: [PATCH] Add writing and reading of String lists to StreamIO classes --- .../common/io/stream/StreamInput.java | 21 ++++++++++++++++--- .../common/io/stream/StreamOutput.java | 16 +++++++++++--- .../index/query/IdsQueryBuilder.java | 11 +++++----- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index c529bc850ff95..5a19b12be0a13 100644 --- a/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -335,6 +335,21 @@ public String[] readStringArray() throws IOException { return ret; } + /** + * Read in a list of strings. List can be empty but not {@code null}. + */ + public List readStringList() throws IOException { + int size = readVInt(); + if (size == 0) { + return Collections.emptyList(); + } + List ret = new ArrayList<>(size); + for (int i = 0; i < size; i++) { + ret.add(readString()); + } + return ret; + } + @Nullable public Map readMap() throws IOException { return (Map) readGenericValue(); @@ -427,7 +442,7 @@ public int[] readIntArray() throws IOException { } return values; } - + public long[] readLongArray() throws IOException { int length = readVInt(); long[] values = new long[length]; @@ -436,7 +451,7 @@ public long[] readLongArray() throws IOException { } return values; } - + public float[] readFloatArray() throws IOException { int length = readVInt(); float[] values = new float[length]; @@ -445,7 +460,7 @@ public float[] readFloatArray() throws IOException { } return values; } - + public double[] readDoubleArray() throws IOException { int length = readVInt(); double[] values = new double[length]; diff --git a/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java b/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java index ad9af359fadf1..b77419073e0c6 100644 --- a/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -283,6 +283,16 @@ public void writeStringArray(String[] array) throws IOException { } } + /** + * Write a list of strings. List can be empty but not {@code null}. + */ + public void writeStringList(List stringList) throws IOException { + writeVInt(stringList.size()); + for (String s : stringList) { + writeString(s); + } + } + /** * Writes a string array, for nullable string, writes it as 0 (empty string). */ @@ -399,21 +409,21 @@ public void writeIntArray(int[] value) throws IOException { writeInt(value[i]); } } - + public void writeLongArray(long[] value) throws IOException { writeVInt(value.length); for (int i=0; i) in.readGenericValue(); - this.ids = (List) in.readGenericValue(); + this.types = in.readStringList(); + this.ids = in.readStringList(); queryName = in.readOptionalString(); boost = in.readFloat(); } @Override public void writeTo(StreamOutput out) throws IOException { - out.writeGenericValue(this.types); - out.writeGenericValue(this.ids); + out.writeStringList(this.types); + out.writeStringList(this.ids); out.writeOptionalString(queryName); out.writeFloat(boost); }