Skip to content

Commit

Permalink
Add writing and reading of String lists to StreamIO classes
Browse files Browse the repository at this point in the history
  • Loading branch information
cbuescher committed May 7, 2015
1 parent 3ac5271 commit 73044ac
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
21 changes: 18 additions & 3 deletions src/main/java/org/elasticsearch/common/io/stream/StreamInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> readStringList() throws IOException {
int size = readVInt();
if (size == 0) {
return Collections.emptyList();
}
List<String> ret = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
ret.add(readString());
}
return ret;
}

@Nullable
public Map<String, Object> readMap() throws IOException {
return (Map<String, Object>) readGenericValue();
Expand Down Expand Up @@ -427,7 +442,7 @@ public int[] readIntArray() throws IOException {
}
return values;
}

public long[] readLongArray() throws IOException {
int length = readVInt();
long[] values = new long[length];
Expand All @@ -436,7 +451,7 @@ public long[] readLongArray() throws IOException {
}
return values;
}

public float[] readFloatArray() throws IOException {
int length = readVInt();
float[] values = new float[length];
Expand All @@ -445,7 +460,7 @@ public float[] readFloatArray() throws IOException {
}
return values;
}

public double[] readDoubleArray() throws IOException {
int length = readVInt();
double[] values = new double[length];
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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).
*/
Expand Down Expand Up @@ -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<value.length; i++) {
writeLong(value[i]);
}
}

public void writeFloatArray(float[] value) throws IOException {
writeVInt(value.length);
for (int i=0; i<value.length; i++) {
writeFloat(value[i]);
}
}

public void writeDoubleArray(double[] value) throws IOException {
writeVInt(value.length);
for (int i=0; i<value.length; i++) {
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/elasticsearch/index/query/IdsQueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.startObject(IdsQueryParser.NAME);
if (types != null) {
if (types.size() == 1) {
builder.field("type", types.iterator().next());
builder.field("type", types.get(0));
} else {
builder.startArray("types");
for (String type : types) {
Expand Down Expand Up @@ -177,19 +177,18 @@ public QueryValidationException validate() {
return null;
}

@SuppressWarnings("unchecked")
@Override
public void readFrom(StreamInput in) throws IOException {
this.types = (List<String>) in.readGenericValue();
this.ids = (List<String>) 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);
}
Expand Down

0 comments on commit 73044ac

Please sign in to comment.