Skip to content

Commit

Permalink
Transport: read/writeGenericValue to support BytesRef
Browse files Browse the repository at this point in the history
Add support for BytesRef to existing StreamInput#readGenericValue and StreamOutput#writeGenericValue

Closes #10878
  • Loading branch information
javanna committed Apr 30, 2015
1 parent 2fd387d commit 77ac452
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
Expand Up @@ -21,7 +21,6 @@

import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.CharsRefBuilder;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -413,6 +412,8 @@ public Object readGenericValue() throws IOException {
return readFloatArray();
case 20:
return readDoubleArray();
case 21:
return readBytesRef();
default:
throw new IOException("Can't read unknown type [" + type + "]");
}
Expand Down
Expand Up @@ -385,6 +385,9 @@ public void writeGenericValue(@Nullable Object value) throws IOException {
} else if (type == double[].class) {
writeByte((byte) 20);
writeDoubleArray((double[]) value);
} else if (value instanceof BytesRef) {
writeByte((byte) 21);
writeBytesRef((BytesRef) value);
} else {
throw new IOException("Can't write type [" + type + "]");
}
Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.apache.lucene.util.Constants;
import org.elasticsearch.common.io.stream.BytesStreamInput;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Ignore;
Expand Down Expand Up @@ -263,8 +264,8 @@ public void testSimpleStreams() throws Exception {
assumeTrue("requires a 64-bit JRE ... ?!", Constants.JRE_IS_64BIT);
BytesStreamOutput out = new BytesStreamOutput();
out.writeBoolean(false);
out.writeByte((byte)1);
out.writeShort((short)-1);
out.writeByte((byte) 1);
out.writeShort((short) -1);
out.writeInt(-1);
out.writeVInt(2);
out.writeLong(-3);
Expand All @@ -281,6 +282,7 @@ public void testSimpleStreams() throws Exception {
out.writeGenericValue(doubleArray);
out.writeString("hello");
out.writeString("goodbye");
out.writeGenericValue(BytesRefs.toBytesRef("bytesref"));
BytesStreamInput in = new BytesStreamInput(out.bytes().toBytes());
assertThat(in.readBoolean(), equalTo(false));
assertThat(in.readByte(), equalTo((byte)1));
Expand All @@ -291,12 +293,13 @@ public void testSimpleStreams() throws Exception {
assertThat(in.readVLong(), equalTo((long)4));
assertThat((double)in.readFloat(), closeTo(1.1, 0.0001));
assertThat(in.readDouble(), closeTo(2.2, 0.0001));
assertThat(in.readGenericValue(), equalTo((Object)intArray));
assertThat(in.readGenericValue(), equalTo((Object) intArray));
assertThat(in.readGenericValue(), equalTo((Object)longArray));
assertThat(in.readGenericValue(), equalTo((Object)floatArray));
assertThat(in.readGenericValue(), equalTo((Object)doubleArray));
assertThat(in.readString(), equalTo("hello"));
assertThat(in.readString(), equalTo("goodbye"));
assertThat(in.readGenericValue(), equalTo((Object)BytesRefs.toBytesRef("bytesref")));
in.close();
out.close();
}
Expand Down

0 comments on commit 77ac452

Please sign in to comment.