use of io.airlift.slice.SliceOutput in project presto by prestodb.
the class PagesSerde method serialize.
public SerializedPage serialize(Page page) {
// block length is an int
SliceOutput serializationBuffer = new DynamicSliceOutput(toIntExact((page.getSizeInBytes() + Integer.BYTES)));
writeRawPage(page, serializationBuffer, blockEncodingSerde);
if (!compressor.isPresent()) {
return new SerializedPage(serializationBuffer.slice(), UNCOMPRESSED, page.getPositionCount(), serializationBuffer.size());
}
int maxCompressedLength = maxCompressedLength(serializationBuffer.size());
byte[] compressionBuffer = new byte[maxCompressedLength];
int actualCompressedLength = compressor.get().compress(serializationBuffer.slice().getBytes(), 0, serializationBuffer.size(), compressionBuffer, 0, maxCompressedLength);
if (((1.0 * actualCompressedLength) / serializationBuffer.size()) > MINIMUM_COMPRESSION_RATIO) {
return new SerializedPage(serializationBuffer.slice(), UNCOMPRESSED, page.getPositionCount(), serializationBuffer.size());
}
return new SerializedPage(Slices.copyOf(Slices.wrappedBuffer(compressionBuffer, 0, actualCompressedLength)), COMPRESSED, page.getPositionCount(), serializationBuffer.size());
}
use of io.airlift.slice.SliceOutput in project presto by prestodb.
the class VariableWidthBlock method copyPositions.
@Override
public Block copyPositions(List<Integer> positions) {
checkValidPositions(positions, positionCount);
int finalLength = positions.stream().mapToInt(this::getSliceLength).sum();
SliceOutput newSlice = Slices.allocate(finalLength).getOutput();
int[] newOffsets = new int[positions.size() + 1];
boolean[] newValueIsNull = new boolean[positions.size()];
for (int i = 0; i < positions.size(); i++) {
int position = positions.get(i);
if (isEntryNull(position)) {
newValueIsNull[i] = true;
} else {
newSlice.appendBytes(slice.getBytes(getPositionOffset(position), getSliceLength(position)));
}
newOffsets[i + 1] = newSlice.size();
}
return new VariableWidthBlock(positions.size(), newSlice.slice(), newOffsets, newValueIsNull);
}
use of io.airlift.slice.SliceOutput in project presto by prestodb.
the class StringEncoding method unescape.
@SuppressWarnings("AssignmentToForLoopParameter")
private static ColumnData unescape(ColumnData columnData, byte escapeByte) {
Slice slice = columnData.getSlice();
// does slice contain escape byte
if (slice.indexOfByte(escapeByte) < 0) {
return columnData;
}
Slice newSlice = Slices.allocate(slice.length());
SliceOutput output = newSlice.getOutput();
int[] newOffsets = new int[columnData.rowCount() + 1];
for (int row = 0; row < columnData.rowCount(); row++) {
int offset = columnData.getOffset(row);
int length = columnData.getLength(row);
for (int i = 0; i < length; i++) {
byte value = slice.getByte(offset + i);
if (value == escapeByte && i + 1 < length) {
// read byte after escape
i++;
value = slice.getByte(offset + i);
}
output.write(value);
}
newOffsets[row + 1] = output.size();
}
return new ColumnData(newOffsets, output.slice());
}
use of io.airlift.slice.SliceOutput in project presto by prestodb.
the class TestByteArrayStream method test.
@Test
public void test() throws IOException {
List<List<Slice>> groups = new ArrayList<>();
for (int groupIndex = 0; groupIndex < 3; groupIndex++) {
List<Slice> group = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
Slice value = Slices.allocate(8);
SliceOutput output = value.getOutput();
output.writeInt(groupIndex);
output.writeInt(i);
group.add(value);
}
groups.add(group);
}
testWriteValue(groups);
}
use of io.airlift.slice.SliceOutput in project presto by prestodb.
the class TestLongDecode method testVInt.
@Test
public void testVInt() throws Exception {
Slice slice = Slices.allocate(100);
SliceOutput output = slice.getOutput();
assertVIntRoundTrip(output, 0);
assertVIntRoundTrip(output, 1);
assertVIntRoundTrip(output, -1);
assertVIntRoundTrip(output, Integer.MAX_VALUE);
assertVIntRoundTrip(output, Integer.MAX_VALUE + 1L);
assertVIntRoundTrip(output, Integer.MAX_VALUE - 1L);
assertVIntRoundTrip(output, Integer.MIN_VALUE);
assertVIntRoundTrip(output, Integer.MIN_VALUE + 1L);
assertVIntRoundTrip(output, Integer.MIN_VALUE - 1L);
assertVIntRoundTrip(output, Long.MAX_VALUE);
assertVIntRoundTrip(output, Long.MAX_VALUE - 1);
assertVIntRoundTrip(output, Long.MIN_VALUE + 1);
for (int value = -100_000; value < 100_000; value++) {
assertVIntRoundTrip(output, value);
}
}
Aggregations