Java Code Examples for io.airlift.slice.Slices#wrappedBuffer()

The following examples show how to use io.airlift.slice.Slices#wrappedBuffer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractDiskOrcDataReader.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public final Slice seekBuffer(int newPosition)
        throws IOException
{
    int newBufferSize = min(dataSize - newPosition, maxBufferSize);
    if (buffer == null || buffer.length < newBufferSize) {
        buffer = new byte[newBufferSize];
    }

    // is the new position withing the current buffer
    if (newPosition > bufferStartPosition && newPosition < bufferStartPosition + bufferSize) {
        // move existing data to the start of the buffer
        int overlapSize = (bufferStartPosition + bufferSize) - newPosition;
        System.arraycopy(buffer, bufferSize - overlapSize, buffer, 0, overlapSize);

        // fill the remaining part of the buffer
        read(newPosition + overlapSize, buffer, overlapSize, newBufferSize - overlapSize);
    }
    else {
        read(newPosition, buffer, 0, newBufferSize);
    }

    bufferSize = newBufferSize;
    bufferStartPosition = newPosition;
    return Slices.wrappedBuffer(buffer, 0, bufferSize);
}
 
Example 2
Source File: BinaryEncoding.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void encodeColumn(Block block, SliceOutput output, EncodeOutput encodeOutput)
{
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (block.isNull(position)) {
            output.writeBytes(nullSequence);
        }
        else {
            Slice slice = type.getSlice(block, position);
            byte[] data = slice.getBytes();
            slice = Slices.wrappedBuffer(base64Encoder.encode(data));
            output.writeBytes(slice);
        }
        encodeOutput.closeEntry();
    }
}
 
Example 3
Source File: ModelUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
public static List<Model> deserializeModels(byte[] bytes)
{
    Slice slice = Slices.wrappedBuffer(bytes);
    int numModels = slice.getInt(0);

    int offset = SIZE_OF_INT + SIZE_OF_INT * numModels;
    ImmutableList.Builder<Model> models = ImmutableList.builder();
    for (int i = 0; i < numModels; i++) {
        int length = slice.getInt(SIZE_OF_INT * (i + 1));
        models.add(deserialize(slice.getBytes(offset, length)));
        offset += length;
    }

    return models.build();
}
 
Example 4
Source File: TypeHelper.java    From presto with Apache License 2.0 5 votes vote down vote up
public static Object getObject(Type type, RowResult row, int field)
{
    if (row.isNull(field)) {
        return null;
    }
    if (type instanceof VarcharType) {
        return row.getString(field);
    }
    if (type.equals(TimestampType.TIMESTAMP)) {
        return row.getLong(field) / 1000;
    }
    if (type == BigintType.BIGINT) {
        return row.getLong(field);
    }
    if (type == IntegerType.INTEGER) {
        return row.getInt(field);
    }
    if (type == SmallintType.SMALLINT) {
        return row.getShort(field);
    }
    if (type == TinyintType.TINYINT) {
        return row.getByte(field);
    }
    if (type == DoubleType.DOUBLE) {
        return row.getDouble(field);
    }
    if (type == RealType.REAL) {
        return row.getFloat(field);
    }
    if (type == BooleanType.BOOLEAN) {
        return row.getBoolean(field);
    }
    if (type instanceof VarbinaryType) {
        return Slices.wrappedBuffer(row.getBinary(field));
    }
    if (type instanceof DecimalType) {
        return row.getDecimal(field);
    }
    throw new IllegalStateException("getObject not implemented for " + type);
}
 
Example 5
Source File: TestDecimals.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Slice sliceFromBytes(int... bytes)
{
    byte[] buffer = new byte[bytes.length];
    for (int i = 0; i < bytes.length; i++) {
        buffer[i] = UnsignedBytes.checkedCast(bytes[i]);
    }
    return Slices.wrappedBuffer(buffer);
}
 
Example 6
Source File: ChunkedSliceOutput.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void reset()
{
    chunkSupplier.reset();
    closedSlices.clear();

    buffer = chunkSupplier.get();
    slice = Slices.wrappedBuffer(buffer);

    closedSlicesRetainedSize = 0;
    streamOffset = 0;
    bufferPosition = 0;
}
 
Example 7
Source File: VarbinaryFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Encode binary data as base64")
@ScalarFunction
@SqlType(StandardTypes.VARCHAR)
public static Slice toBase64(@SqlType(StandardTypes.VARBINARY) Slice slice)
{
    if (slice.hasByteArray()) {
        return Slices.wrappedBuffer(Base64.getEncoder().encode(slice.toByteBuffer()));
    }
    return Slices.wrappedBuffer(Base64.getEncoder().encode(slice.getBytes()));
}
 
Example 8
Source File: CharacterStringCasts.java    From presto with Apache License 2.0 5 votes vote down vote up
public static Slice codePointsToSliceUtf8(List<Integer> codePoints)
{
    int length = codePoints.stream()
            .mapToInt(SliceUtf8::lengthOfCodePoint)
            .sum();

    Slice result = Slices.wrappedBuffer(new byte[length]);
    int offset = 0;
    for (int codePoint : codePoints) {
        setCodePointAt(codePoint, result, offset);
        offset += lengthOfCodePoint(codePoint);
    }

    return result;
}
 
Example 9
Source File: HdfsParquetDataSource.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public Slice read()
{
    checkState(referenceCount > 0, "Chunk reader is already closed");

    if (data == null) {
        byte[] buffer = new byte[range.getLength()];
        readFully(range.getOffset(), buffer, 0, buffer.length);
        data = Slices.wrappedBuffer(buffer);
    }

    return data;
}
 
Example 10
Source File: TestBufferedOutputStreamSliceOutput.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteBytes()
        throws Exception
{
    // fill up some input bytes
    int length = 65536;
    byte[] inputArray = new byte[length];
    for (int i = 0; i < length; i++) {
        inputArray[i] = (byte) (i % 128);
    }

    // pick some offsets to make the inputs into different chunks
    int[] offsets = {0, 100, 545, 1024, 2049, 2050, 2051, 2151, 10480, 20042, 20100, 40001, 65536};

    // check byte array version
    MockOutputStream byteOutputStream = new MockOutputStream(length);
    BufferedOutputStreamSliceOutput output = new BufferedOutputStreamSliceOutput(byteOutputStream);
    for (int i = 0; i < offsets.length - 1; i++) {
        output.writeBytes(inputArray, offsets[i], offsets[i + 1] - offsets[i]);
    }
    // ignore the last flush size check
    output.flush();
    assertEquals(byteOutputStream.toByteArray(), inputArray);
    byteOutputStream.close();

    // check slice version
    byteOutputStream = new MockOutputStream(length);
    Slice inputSlice = Slices.wrappedBuffer(inputArray);
    output = new BufferedOutputStreamSliceOutput(byteOutputStream);
    for (int i = 0; i < offsets.length - 1; i++) {
        output.writeBytes(inputSlice, offsets[i], offsets[i + 1] - offsets[i]);
    }
    // ignore the last flush size check
    output.flush();
    assertEquals(byteOutputStream.toByteArray(), inputArray);
    byteOutputStream.close();
}
 
Example 11
Source File: ChunkedSliceOutput.java    From presto with Apache License 2.0 5 votes vote down vote up
private void closeChunk()
{
    // add trimmed view of slice to closed slices
    closedSlices.add(slice.slice(0, bufferPosition));

    // create a new buffer
    // double size until we hit the max chunk size
    buffer = chunkSupplier.get();
    slice = Slices.wrappedBuffer(buffer);

    streamOffset += bufferPosition;
    bufferPosition = 0;
}
 
Example 12
Source File: ChunkedSliceOutput.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void reset()
{
    chunkSupplier.reset();
    closedSlices.clear();

    buffer = chunkSupplier.get();
    slice = Slices.wrappedBuffer(buffer);

    closedSlicesRetainedSize = 0;
    streamOffset = 0;
    bufferPosition = 0;
}
 
Example 13
Source File: ChunkedSliceOutput.java    From presto with Apache License 2.0 5 votes vote down vote up
public ChunkedSliceOutput(int minChunkSize, int maxChunkSize)
{
    this.chunkSupplier = new ChunkSupplier(minChunkSize, maxChunkSize);

    this.buffer = chunkSupplier.get();
    this.slice = Slices.wrappedBuffer(buffer);
}
 
Example 14
Source File: PagesSerde.java    From presto with Apache License 2.0 5 votes vote down vote up
public Page deserialize(SerializedPage serializedPage)
{
    checkArgument(serializedPage != null, "serializedPage is null");

    Slice slice = serializedPage.getSlice();

    if (serializedPage.isEncrypted()) {
        checkState(spillCipher.isPresent(), "Page is encrypted, but spill cipher is missing");

        byte[] decrypted = new byte[spillCipher.get().decryptedMaxLength(slice.length())];
        int decryptedSize = spillCipher.get().decrypt(
                slice.byteArray(),
                slice.byteArrayOffset(),
                slice.length(),
                decrypted,
                0);

        slice = Slices.wrappedBuffer(decrypted, 0, decryptedSize);
    }

    if (serializedPage.isCompressed()) {
        checkState(decompressor.isPresent(), "Page is compressed, but decompressor is missing");

        int uncompressedSize = serializedPage.getUncompressedSizeInBytes();
        byte[] decompressed = new byte[uncompressedSize];
        checkState(decompressor.get().decompress(
                slice.byteArray(),
                slice.byteArrayOffset(),
                slice.length(),
                decompressed,
                0,
                uncompressedSize) == uncompressedSize);

        slice = Slices.wrappedBuffer(decompressed);
    }

    return readRawPage(serializedPage.getPositionCount(), slice.getInput(), blockEncodingSerde);
}
 
Example 15
Source File: AbstractOrcDataSource.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public final Slice readFully(long position, int length)
        throws IOException
{
    byte[] buffer = new byte[length];
    readFully(position, buffer, 0, length);
    return Slices.wrappedBuffer(buffer);
}
 
Example 16
Source File: TestVarBinaryMinAggregation.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected Object getExpectedValue(int start, int length)
{
    if (length == 0) {
        return null;
    }
    Slice min = null;
    for (int i = start; i < start + length; i++) {
        Slice slice = Slices.wrappedBuffer(Ints.toByteArray(i));
        min = (min == null) ? slice : Ordering.natural().min(min, slice);
    }
    return min.toStringUtf8();
}
 
Example 17
Source File: SliceDictionaryStreamReader.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void readDictionary(
            @Nullable ByteArrayStream dictionaryDataStream,
            int dictionarySize,
            int[] dictionaryLength,
            int dictionaryOutputOffset,
            Slice[] dictionary,
            DataType type)
            throws IOException
    {
        // build dictionary slices
        for (int i = 0; i < dictionarySize; i++) {
            int length = dictionaryLength[i];
            if (length == 0) {
                dictionary[dictionaryOutputOffset + i] = Slices.EMPTY_SLICE;
            }
            else {
                Slice value = Slices.wrappedBuffer(dictionaryDataStream.next(length));
                /* DO WE NEED THIS?
                if (isVarcharType(type)) {
                    value = truncateToLength(value, type);
                }
                if (isCharType(type)) {
                    value = trimSpacesAndTruncateToLength(value, type);
                }
                */
//                System.out.println(String.format("Reading Dictionary (%s,%s)",dictionaryOutputOffset + i,value.toStringUtf8()));
                dictionary[dictionaryOutputOffset + i] = value;
            }
        }
    }
 
Example 18
Source File: TestUnscaledDecimal128Arithmetic.java    From presto with Apache License 2.0 5 votes vote down vote up
private static int[] toInt8Array(byte[] bytes)
{
    Slice slice = Slices.wrappedBuffer(bytes);

    int[] ints = new int[8];
    for (int i = 0; i < ints.length; i++) {
        ints[i] = slice.getInt(i * Integer.SIZE / Byte.SIZE);
    }
    return ints;
}
 
Example 19
Source File: PrestoFactory.java    From transport with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public StdBinary createBinary(ByteBuffer value) {
  return new PrestoBinary(Slices.wrappedBuffer(value.array()));
}
 
Example 20
Source File: TestIpAddressType.java    From presto with Apache License 2.0 4 votes vote down vote up
private static Slice getSliceForAddress(String address)
{
    return Slices.wrappedBuffer(InetAddresses.forString(address).getAddress());
}