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

The following examples show how to use io.airlift.slice.Slices#copyOf() . 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: OrcMetadataReader.java    From presto with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
public static Slice maxStringTruncateToValidRange(Slice value, HiveWriterVersion version)
{
    if (value == null) {
        return null;
    }

    if (version != ORIGINAL) {
        return value;
    }

    int index = findStringStatisticTruncationPositionForOriginalOrcWriter(value);
    if (index == value.length()) {
        return value;
    }
    // Append 0xFF so that it is larger than value
    Slice newValue = Slices.copyOf(value, 0, index + 1);
    newValue.setByte(index, 0xFF);
    return newValue;
}
 
Example 2
Source File: OrcMetadataReader.java    From presto with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
public static Slice minStringTruncateToValidRange(Slice value, HiveWriterVersion version)
{
    if (value == null) {
        return null;
    }

    if (version != ORIGINAL) {
        return value;
    }

    int index = findStringStatisticTruncationPositionForOriginalOrcWriter(value);
    if (index == value.length()) {
        return value;
    }
    return Slices.copyOf(value, 0, index);
}
 
Example 3
Source File: TestRcFileDecoderUtils.java    From presto with Apache License 2.0 6 votes vote down vote up
private static Slice writeVintOld(SliceOutput output, long value)
        throws IOException
{
    output.reset();
    WritableUtils.writeVLong(output, value);
    Slice vLongOld = Slices.copyOf(output.slice());

    output.reset();
    RcFileDecoderUtils.writeVLong(output, value);
    Slice vLongNew = Slices.copyOf(output.slice());
    assertEquals(vLongNew, vLongOld);

    if (value == (int) value) {
        output.reset();
        WritableUtils.writeVInt(output, (int) value);
        Slice vIntOld = Slices.copyOf(output.slice());
        assertEquals(vIntOld, vLongOld);

        output.reset();
        RcFileDecoderUtils.writeVInt(output, (int) value);
        Slice vIntNew = Slices.copyOf(output.slice());
        assertEquals(vIntNew, vLongOld);
    }
    return vLongOld;
}
 
Example 4
Source File: StringStatisticsBuilder.java    From presto with Apache License 2.0 5 votes vote down vote up
private Slice dropStringMinMaxIfNecessary(Slice minOrMax)
{
    if (minOrMax == null || minOrMax.length() > stringStatisticsLimitInBytes) {
        return null;
    }

    // Do not hold the entire slice where the actual stats could be small
    if (minOrMax.isCompact()) {
        return minOrMax;
    }
    return Slices.copyOf(minOrMax);
}
 
Example 5
Source File: BenchmarkStringFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Setup
public void setup()
{
    Slice whitespace = createRandomUtf8Slice(ascii ? ASCII_WHITESPACE : ALL_WHITESPACE, length + 1);
    leftWhitespace = Slices.copyOf(whitespace);
    leftWhitespace.setByte(leftWhitespace.length() - 1, 'X');
    rightWhitespace = Slices.copyOf(whitespace);
    rightWhitespace.setByte(0, 'X');
    bothWhitespace = Slices.copyOf(whitespace);
    bothWhitespace.setByte(length / 2, 'X');
}
 
Example 6
Source File: AbstractTestBlock.java    From presto with Apache License 2.0 5 votes vote down vote up
protected static Slice[] createExpectedUniqueValues(int positionCount)
{
    Slice[] expectedValues = new Slice[positionCount];
    for (int position = 0; position < positionCount; position++) {
        expectedValues[position] = Slices.copyOf(createExpectedValue(position));
    }
    return expectedValues;
}
 
Example 7
Source File: TestVariableWidthBlock.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompactBlock()
{
    Slice compactSlice = Slices.copyOf(createExpectedValue(16));
    Slice incompactSlice = Slices.copyOf(createExpectedValue(20)).slice(0, 16);
    int[] offsets = {0, 1, 1, 2, 4, 8, 16};
    boolean[] valueIsNull = {false, true, false, false, false, false};

    testCompactBlock(new VariableWidthBlock(0, EMPTY_SLICE, new int[1], Optional.empty()));
    testCompactBlock(new VariableWidthBlock(valueIsNull.length, compactSlice, offsets, Optional.of(valueIsNull)));

    testIncompactBlock(new VariableWidthBlock(valueIsNull.length - 1, compactSlice, offsets, Optional.of(valueIsNull)));
    // underlying slice is not compact
    testIncompactBlock(new VariableWidthBlock(valueIsNull.length, incompactSlice, offsets, Optional.of(valueIsNull)));
}
 
Example 8
Source File: BlockUtil.java    From presto with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a slice containing values in the specified range of the specified slice.
 * If the range matches the entire slice, the input slice will be returned.
 * Otherwise, a copy will be returned.
 */
static Slice compactSlice(Slice slice, int index, int length)
{
    if (slice.isCompact() && index == 0 && length == slice.length()) {
        return slice;
    }
    return Slices.copyOf(slice, index, length);
}
 
Example 9
Source File: AbstractVariableWidthBlock.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public Block getSingleValueBlock(int position)
{
    if (isNull(position)) {
        return new VariableWidthBlock(0, 1, EMPTY_SLICE, new int[] {0, 0}, new boolean[] {true});
    }

    int offset = getPositionOffset(position);
    int entrySize = getSliceLength(position);

    Slice copy = Slices.copyOf(getRawSlice(position), offset, entrySize);

    return new VariableWidthBlock(0, 1, copy, new int[] {0, copy.length()}, null);
}
 
Example 10
Source File: PagesSerde.java    From presto with Apache License 2.0 4 votes vote down vote up
public SerializedPage serialize(Page page)
{
    SliceOutput serializationBuffer = new DynamicSliceOutput(toIntExact(page.getSizeInBytes() + Integer.BYTES)); // block length is an int
    writeRawPage(page, serializationBuffer, blockEncodingSerde);
    Slice slice = serializationBuffer.slice();
    int uncompressedSize = serializationBuffer.size();
    MarkerSet markers = MarkerSet.empty();

    if (compressor.isPresent()) {
        byte[] compressed = new byte[compressor.get().maxCompressedLength(uncompressedSize)];
        int compressedSize = compressor.get().compress(
                slice.byteArray(),
                slice.byteArrayOffset(),
                uncompressedSize,
                compressed,
                0,
                compressed.length);

        if ((((double) compressedSize) / uncompressedSize) <= MINIMUM_COMPRESSION_RATIO) {
            slice = Slices.wrappedBuffer(compressed, 0, compressedSize);
            markers.add(COMPRESSED);
        }
    }

    if (spillCipher.isPresent()) {
        byte[] encrypted = new byte[spillCipher.get().encryptedMaxLength(slice.length())];
        int encryptedSize = spillCipher.get().encrypt(
                slice.byteArray(),
                slice.byteArrayOffset(),
                slice.length(),
                encrypted,
                0);

        slice = Slices.wrappedBuffer(encrypted, 0, encryptedSize);
        markers.add(ENCRYPTED);
    }

    if (!slice.isCompact()) {
        slice = Slices.copyOf(slice);
    }

    return new SerializedPage(slice, markers, page.getPositionCount(), uncompressedSize);
}
 
Example 11
Source File: UnscaledDecimal128Arithmetic.java    From presto with Apache License 2.0 4 votes vote down vote up
public static Slice unscaledDecimal(Slice decimal)
{
    return Slices.copyOf(decimal);
}
 
Example 12
Source File: UnscaledDecimal128Arithmetic.java    From presto with Apache License 2.0 4 votes vote down vote up
public static Slice multiply(Slice decimal, int multiplier)
{
    Slice result = Slices.copyOf(decimal);
    multiplyDestructive(result, multiplier);
    return result;
}
 
Example 13
Source File: UnscaledDecimal128Arithmetic.java    From presto with Apache License 2.0 4 votes vote down vote up
static Slice shiftLeft(Slice decimal, int leftShifts)
{
    Slice result = Slices.copyOf(decimal);
    shiftLeftDestructive(result, leftShifts);
    return result;
}