Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private static byte[] writeCommands(final int[] commands, final int type, final
output.writeVInt(24);
output.writeVInt(type);
output.writeVInt(34);
output.writeVInt(Math.toIntExact(counting.size()));
output.writeVInt(Math.toIntExact(counting.position()));
for (int i = 0; i < length; i++) {
output.writeVInt(commands[i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,102 +18,97 @@
* A reusable @link {@link StreamOutput} that just count how many bytes are written.
*/
public class CountingStreamOutput extends StreamOutput {
private long size;
private long position;

/** reset the written byes to 0 */
public void reset() {
size = 0L;
}

/** returns how many bytes would have been written -- TODO replace with position() from StreamOutput interface */
public long size() {
return size;
position = 0L;
}

@Override
public void writeByte(byte b) {
++size;
++position;
}

@Override
public void writeBytes(byte[] b, int offset, int length) {
size += length;
position += length;
}

@Override
public long position() {
return size;
return position;
}

@Override
public void writeShort(short v) throws IOException {
size += Short.BYTES;
position += Short.BYTES;
}

@Override
public void writeInt(int i) {
size += Integer.BYTES;
position += Integer.BYTES;
}

@Override
public void writeIntLE(int i) throws IOException {
size += Integer.BYTES;
position += Integer.BYTES;
}

@Override
public void writeIntArray(int[] values) {
writeVInt(values.length);
size += (long) values.length * Integer.BYTES;
position += (long) values.length * Integer.BYTES;
}

@Override
public void writeLong(long i) {
size += Long.BYTES;
position += Long.BYTES;
}

@Override
public void writeLongLE(long i) {
size += Long.BYTES;
position += Long.BYTES;
}

@Override
public void writeLongArray(long[] values) {
writeVInt(values.length);
size += (long) values.length * Long.BYTES;
position += (long) values.length * Long.BYTES;
}

@Override
public void writeFloat(float v) {
size += Float.BYTES;
position += Float.BYTES;
}

@Override
public void writeFloatArray(float[] values) {
writeVInt(values.length);
size += (long) values.length * Float.BYTES;
position += (long) values.length * Float.BYTES;
}

@Override
public void writeDouble(double v) {
size += Double.BYTES;
position += Double.BYTES;
}

@Override
public void writeDoubleArray(double[] values) {
writeVInt(values.length);
size += (long) values.length * Double.BYTES;
position += (long) values.length * Double.BYTES;
}

@Override
public void writeVInt(int v) {
// set LSB because 0 takes 1 byte
size += (38 - Integer.numberOfLeadingZeros(v | 1)) / 7;
position += (38 - Integer.numberOfLeadingZeros(v | 1)) / 7;
}

@Override
void writeVLongNoCheck(long v) {
// set LSB because 0 takes 1 byte
size += (70 - Long.numberOfLeadingZeros(v | 1L)) / 7;
position += (70 - Long.numberOfLeadingZeros(v | 1L)) / 7;
}

@Override
Expand All @@ -125,26 +120,26 @@ public void writeZLong(long i) {
public void writeString(String str) {
final int charCount = str.length();
writeVInt(charCount);
size += charCount;
position += charCount;
for (int i = 0; i < charCount; i++) {
final int c = str.charAt(i);
if (c > 0x007F) {
size += c > 0x07FF ? 2 : 1;
position += c > 0x07FF ? 2 : 1;
}
}
}

@Override
public void writeOptionalString(@Nullable String str) {
size += 1;
position += 1;
if (str != null) {
writeString(str);
}
}

@Override
public void writeGenericString(String value) {
size += 1;
position += 1;
writeString(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public static long getSerializedSize(Writeable ref) {
try (CountingStreamOutput out = new CountingStreamOutput()) {
out.setTransportVersion(TransportVersion.current());
ref.writeTo(out);
return out.size();
return out.position();
} catch (IOException exc) {
throw new UncheckedIOException(exc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private long nodeSize(boolean includeBox, int parentMaxX, int parentMaxY, Counti
long rightSize = right.nodeSize(true, maxX, maxY, countingBuffer);
countingBuffer.reset();
countingBuffer.writeVLong(rightSize);
size += countingBuffer.size(); // jump size
size += countingBuffer.position(); // jump size
size += rightSize;
}
if (includeBox) {
Expand All @@ -206,7 +206,7 @@ private long nodeSize(boolean includeBox, int parentMaxX, int parentMaxY, Counti
countingBuffer.writeVLong((long) parentMaxX - maxX);
countingBuffer.writeVLong((long) parentMaxY - maxY);
countingBuffer.writeVLong(jumpSize);
size += countingBuffer.size(); // box size
size += countingBuffer.position(); // box size
}
return size;
}
Expand All @@ -229,7 +229,7 @@ private long componentSize(CountingStreamOutput countingBuffer) throws IOExcepti
countingBuffer.writeVLong((long) maxX - component.cX);
countingBuffer.writeVLong((long) maxY - component.cY);
}
return countingBuffer.size();
return countingBuffer.position();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -930,14 +930,14 @@ private static class TestStreamOutput extends BytesStream {
public void writeByte(byte b) {
output.writeByte(b);
counting.writeByte(b);
assertThat((long) output.size(), equalTo(counting.size()));
assertThat((long) output.size(), equalTo(counting.position()));
}

@Override
public void writeBytes(byte[] b, int offset, int length) {
output.writeBytes(b, offset, length);
counting.writeBytes(b, offset, length);
assertThat((long) output.size(), equalTo(counting.size()));
assertThat((long) output.size(), equalTo(counting.position()));
}

@Override
Expand All @@ -951,83 +951,83 @@ public long position() {
public void writeInt(int i) throws IOException {
output.writeInt(i);
counting.writeInt(i);
assertThat((long) output.size(), equalTo(counting.size()));
assertThat((long) output.size(), equalTo(counting.position()));
}

@Override
public void writeIntArray(int[] values) throws IOException {
output.writeIntArray(values);
counting.writeIntArray(values);
assertThat((long) output.size(), equalTo(counting.size()));
assertThat((long) output.size(), equalTo(counting.position()));
}

@Override
public void writeLong(long i) throws IOException {
output.writeLong(i);
counting.writeLong(i);
assertThat((long) output.size(), equalTo(counting.size()));
assertThat((long) output.size(), equalTo(counting.position()));
}

@Override
public void writeLongArray(long[] values) throws IOException {
output.writeLongArray(values);
counting.writeLongArray(values);
assertThat((long) output.size(), equalTo(counting.size()));
assertThat((long) output.size(), equalTo(counting.position()));
}

@Override
public void writeFloat(float v) throws IOException {
output.writeFloat(v);
counting.writeFloat(v);
assertThat((long) output.size(), equalTo(counting.size()));
assertThat((long) output.size(), equalTo(counting.position()));
}

@Override
public void writeFloatArray(float[] values) throws IOException {
output.writeFloatArray(values);
counting.writeFloatArray(values);
assertThat((long) output.size(), equalTo(counting.size()));
assertThat((long) output.size(), equalTo(counting.position()));
}

@Override
public void writeDouble(double v) throws IOException {
output.writeDouble(v);
counting.writeDouble(v);
assertThat((long) output.size(), equalTo(counting.size()));
assertThat((long) output.size(), equalTo(counting.position()));
}

@Override
public void writeDoubleArray(double[] values) throws IOException {
output.writeDoubleArray(values);
counting.writeDoubleArray(values);
assertThat((long) output.size(), equalTo(counting.size()));
assertThat((long) output.size(), equalTo(counting.position()));
}

@Override
public void writeString(String str) throws IOException {
output.writeString(str);
counting.writeString(str);
assertThat((long) output.size(), equalTo(counting.size()));
assertThat((long) output.size(), equalTo(counting.position()));
}

@Override
public void writeOptionalString(@Nullable String str) throws IOException {
output.writeOptionalString(str);
counting.writeOptionalString(str);
assertThat((long) output.size(), equalTo(counting.size()));
assertThat((long) output.size(), equalTo(counting.position()));
}

@Override
public void writeGenericString(String value) throws IOException {
output.writeGenericString(value);
counting.writeGenericString(value);
assertThat((long) output.size(), equalTo(counting.size()));
assertThat((long) output.size(), equalTo(counting.position()));
}

@Override
public BytesReference bytes() {
BytesReference bytesReference = output.bytes();
assertThat((long) bytesReference.length(), equalTo(counting.size()));
assertThat((long) bytesReference.length(), equalTo(counting.position()));
return bytesReference;
}

Expand All @@ -1038,20 +1038,20 @@ public void seek(long position) {

public int size() {
int size = output.size();
assertThat((long) size, equalTo(counting.size()));
assertThat((long) size, equalTo(counting.position()));
return size;
}

@Override
public void flush() {
output.flush();
counting.flush();
assertThat((long) output.size(), equalTo(counting.size()));
assertThat((long) output.size(), equalTo(counting.position()));
}

@Override
public void close() {
assertThat((long) output.size(), equalTo(counting.size()));
assertThat((long) output.size(), equalTo(counting.position()));
output.close();
counting.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ public void write(byte[] b, int off, int len) {
return s -> s.writeGenericString(value);
});

while (countingStream.size() < targetSize) {
while (countingStream.position() < targetSize) {
var writerIndex = between(0, writers.size() - 1);
var writer = writers.get(writerIndex).get();
for (var stream : streams) {
writer.accept(stream);
}
assertEquals("recyclerBytesStream after " + writerIndex, countingStream.size(), recyclerBytesStream.position());
assertEquals("plainBytesStream after " + writerIndex, countingStream.size(), plainBytesStream.position());
assertEquals("bufferedStream after " + writerIndex, countingStream.size(), bufferedStream.position());
assertEquals("wrappedStream after " + writerIndex, countingStream.size(), wrappedStream.position());
assertEquals("recyclerBytesStream after " + writerIndex, countingStream.position(), recyclerBytesStream.position());
assertEquals("plainBytesStream after " + writerIndex, countingStream.position(), plainBytesStream.position());
assertEquals("bufferedStream after " + writerIndex, countingStream.position(), bufferedStream.position());
assertEquals("wrappedStream after " + writerIndex, countingStream.position(), wrappedStream.position());
}

isExpectedWriteSize.set(allOf(lessThanOrEqualTo(bufferLen), greaterThan(0))); // last write may be undersized
Expand Down