use of org.elasticsearch.common.util.ByteArray in project crate by crate.
the class ReleasableBytesReferenceTests method newBytesReferenceWithOffsetOfZero.
@Override
protected BytesReference newBytesReferenceWithOffsetOfZero(int length) throws IOException {
BytesReference delegate;
String composite = "composite";
String paged = "paged";
String array = "array";
String type = randomFrom(composite, paged, array);
if (array.equals(type)) {
final BytesStreamOutput out = new BytesStreamOutput(length);
for (int i = 0; i < length; i++) {
out.writeByte((byte) random().nextInt(1 << 8));
}
assertThat(length, equalTo(out.size()));
BytesArray ref = new BytesArray(out.bytes().toBytesRef().bytes, 0, length);
assertThat(length, equalTo(ref.length()));
assertThat(ref.length(), Matchers.equalTo(length));
delegate = ref;
} else if (paged.equals(type)) {
ByteArray byteArray = bigarrays.newByteArray(length);
for (int i = 0; i < length; i++) {
byteArray.set(i, (byte) random().nextInt(1 << 8));
}
assertThat(byteArray.size(), Matchers.equalTo((long) length));
BytesReference ref = new PagedBytesReference(byteArray, length);
assertThat(ref.length(), Matchers.equalTo(length));
delegate = ref;
} else {
assert composite.equals(type);
List<BytesReference> referenceList = new ArrayList<>();
for (int i = 0; i < length; ) {
int remaining = length - i;
int sliceLength = randomIntBetween(1, remaining);
try (ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(sliceLength, bigarrays)) {
for (int j = 0; j < sliceLength; j++) {
out.writeByte((byte) random().nextInt(1 << 8));
}
assertThat(sliceLength, equalTo(out.size()));
referenceList.add(out.bytes());
}
i += sliceLength;
}
BytesReference ref = new CompositeBytesReference(referenceList.toArray(new BytesReference[0]));
assertThat(length, equalTo(ref.length()));
delegate = ref;
}
return ReleasableBytesReference.wrap(delegate);
}
Aggregations