Skip to content

Commit b7e0958

Browse files
committed
Merge pull request #14087 from jasontedor/vle-integral-arrays
Add methods for variable-length encoding integral arrays
2 parents ceb969a + 2162172 commit b7e0958

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

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

+18
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,15 @@ public int[] readIntArray() throws IOException {
460460
return values;
461461
}
462462

463+
public int[] readVIntArray() throws IOException {
464+
int length = readVInt();
465+
int[] values = new int[length];
466+
for (int i = 0; i < length; i++) {
467+
values[i] = readVInt();
468+
}
469+
return values;
470+
}
471+
463472
public long[] readLongArray() throws IOException {
464473
int length = readVInt();
465474
long[] values = new long[length];
@@ -469,6 +478,15 @@ public long[] readLongArray() throws IOException {
469478
return values;
470479
}
471480

481+
public long[] readVLongArray() throws IOException {
482+
int length = readVInt();
483+
long[] values = new long[length];
484+
for (int i = 0; i < length; i++) {
485+
values[i] = readVLong();
486+
}
487+
return values;
488+
}
489+
472490
public float[] readFloatArray() throws IOException {
473491
int length = readVInt();
474492
float[] values = new float[length];

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

+14
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,27 @@ public void writeIntArray(int[] values) throws IOException {
438438
}
439439
}
440440

441+
public void writeVIntArray(int[] values) throws IOException {
442+
writeVInt(values.length);
443+
for (int value : values) {
444+
writeVInt(value);
445+
}
446+
}
447+
441448
public void writeLongArray(long[] values) throws IOException {
442449
writeVInt(values.length);
443450
for (long value : values) {
444451
writeLong(value);
445452
}
446453
}
447454

455+
public void writeVLongArray(long[] values) throws IOException {
456+
writeVInt(values.length);
457+
for (long value : values) {
458+
writeVLong(value);
459+
}
460+
}
461+
448462
public void writeFloatArray(float[] values) throws IOException {
449463
writeVInt(values.length);
450464
for (float value : values) {

core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java

+6
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,12 @@ public void testSimpleStreams() throws Exception {
276276
out.writeDouble(2.2);
277277
int[] intArray = {1, 2, 3};
278278
out.writeGenericValue(intArray);
279+
int[] vIntArray = {4, 5, 6};
280+
out.writeVIntArray(vIntArray);
279281
long[] longArray = {1, 2, 3};
280282
out.writeGenericValue(longArray);
283+
long[] vLongArray = {4, 5, 6};
284+
out.writeVLongArray(vLongArray);
281285
float[] floatArray = {1.1f, 2.2f, 3.3f};
282286
out.writeGenericValue(floatArray);
283287
double[] doubleArray = {1.1, 2.2, 3.3};
@@ -296,7 +300,9 @@ public void testSimpleStreams() throws Exception {
296300
assertThat((double)in.readFloat(), closeTo(1.1, 0.0001));
297301
assertThat(in.readDouble(), closeTo(2.2, 0.0001));
298302
assertThat(in.readGenericValue(), equalTo((Object) intArray));
303+
assertThat(in.readVIntArray(), equalTo(vIntArray));
299304
assertThat(in.readGenericValue(), equalTo((Object)longArray));
305+
assertThat(in.readVLongArray(), equalTo(vLongArray));
300306
assertThat(in.readGenericValue(), equalTo((Object)floatArray));
301307
assertThat(in.readGenericValue(), equalTo((Object)doubleArray));
302308
assertThat(in.readString(), equalTo("hello"));

0 commit comments

Comments
 (0)