From 2162172f30edd481e4a7b457926311e6a4a6fd1f Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Tue, 13 Oct 2015 08:47:42 -0400 Subject: [PATCH] Add methods for variable-length encoding integral arrays MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds methods to serialize the elements of int and long arrays using variable-length encodings. This can be useful for serializing int and long arrays containing mostly non-negative “not large” values in a compressed form. --- .../common/io/stream/StreamInput.java | 18 ++++++++++++++++++ .../common/io/stream/StreamOutput.java | 14 ++++++++++++++ .../common/io/stream/BytesStreamsTests.java | 6 ++++++ 3 files changed, 38 insertions(+) diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index a6fc0914dbe..4b56027e2a2 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -460,6 +460,15 @@ public abstract class StreamInput extends InputStream { return values; } + public int[] readVIntArray() throws IOException { + int length = readVInt(); + int[] values = new int[length]; + for (int i = 0; i < length; i++) { + values[i] = readVInt(); + } + return values; + } + public long[] readLongArray() throws IOException { int length = readVInt(); long[] values = new long[length]; @@ -469,6 +478,15 @@ public abstract class StreamInput extends InputStream { return values; } + public long[] readVLongArray() throws IOException { + int length = readVInt(); + long[] values = new long[length]; + for (int i = 0; i < length; i++) { + values[i] = readVLong(); + } + return values; + } + public float[] readFloatArray() throws IOException { int length = readVInt(); float[] values = new float[length]; diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java index 3e4aabb6284..71558ff49c4 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -438,6 +438,13 @@ public abstract class StreamOutput extends OutputStream { } } + public void writeVIntArray(int[] values) throws IOException { + writeVInt(values.length); + for (int value : values) { + writeVInt(value); + } + } + public void writeLongArray(long[] values) throws IOException { writeVInt(values.length); for (long value : values) { @@ -445,6 +452,13 @@ public abstract class StreamOutput extends OutputStream { } } + public void writeVLongArray(long[] values) throws IOException { + writeVInt(values.length); + for (long value : values) { + writeVLong(value); + } + } + public void writeFloatArray(float[] values) throws IOException { writeVInt(values.length); for (float value : values) { diff --git a/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java b/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java index 2b37359b2f6..50e51bab22e 100644 --- a/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java +++ b/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java @@ -276,8 +276,12 @@ public class BytesStreamsTests extends ESTestCase { out.writeDouble(2.2); int[] intArray = {1, 2, 3}; out.writeGenericValue(intArray); + int[] vIntArray = {4, 5, 6}; + out.writeVIntArray(vIntArray); long[] longArray = {1, 2, 3}; out.writeGenericValue(longArray); + long[] vLongArray = {4, 5, 6}; + out.writeVLongArray(vLongArray); float[] floatArray = {1.1f, 2.2f, 3.3f}; out.writeGenericValue(floatArray); double[] doubleArray = {1.1, 2.2, 3.3}; @@ -296,7 +300,9 @@ public class BytesStreamsTests extends ESTestCase { 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.readVIntArray(), equalTo(vIntArray)); assertThat(in.readGenericValue(), equalTo((Object)longArray)); + assertThat(in.readVLongArray(), equalTo(vLongArray)); assertThat(in.readGenericValue(), equalTo((Object)floatArray)); assertThat(in.readGenericValue(), equalTo((Object)doubleArray)); assertThat(in.readString(), equalTo("hello"));