Java Code Examples for io.airlift.slice.Slice#setInt()

The following examples show how to use io.airlift.slice.Slice#setInt() . 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: RcFileTester.java    From presto with Apache License 2.0 6 votes vote down vote up
private static List<Long> getSyncPositionsBruteForce(RcFileReader recordReader, File file)
{
    Slice slice = Slices.allocate(toIntExact(file.length()));
    try (InputStream in = new FileInputStream(file)) {
        slice.setBytes(0, in, slice.length());
    }
    catch (IOException e) {
        throw new UncheckedIOException(e);
    }

    List<Long> syncPositionsBruteForce = new ArrayList<>();
    Slice sync = Slices.allocate(SIZE_OF_INT + SIZE_OF_LONG + SIZE_OF_LONG);
    sync.setInt(0, -1);
    sync.setBytes(SIZE_OF_INT, recordReader.getSync());

    long syncPosition = 0;
    while (syncPosition >= 0) {
        syncPosition = slice.indexOf(sync, toIntExact(syncPosition));
        if (syncPosition > 0) {
            syncPositionsBruteForce.add(syncPosition);
            syncPosition++;
        }
    }
    return syncPositionsBruteForce;
}
 
Example 2
Source File: VarbinaryFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Encode value as a 32-bit 2's complement big endian varbinary")
@ScalarFunction("to_big_endian_32")
@SqlType(StandardTypes.VARBINARY)
public static Slice toBigEndian32(@SqlType(StandardTypes.INTEGER) long value)
{
    Slice slice = Slices.allocate(Integer.BYTES);
    slice.setInt(0, Integer.reverseBytes((int) value));
    return slice;
}
 
Example 3
Source File: VarbinaryFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Encode value as a big endian varbinary according to IEEE 754 single-precision floating-point format")
@ScalarFunction("to_ieee754_32")
@SqlType(StandardTypes.VARBINARY)
public static Slice toIEEE754Binary32(@SqlType(StandardTypes.REAL) long value)
{
    Slice slice = Slices.allocate(Float.BYTES);
    slice.setInt(0, Integer.reverseBytes((int) value));
    return slice;
}
 
Example 4
Source File: VarbinaryFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Compute SpookyHashV2 32-bit hash")
@ScalarFunction
@SqlType(StandardTypes.VARBINARY)
public static Slice spookyHashV2_32(@SqlType(StandardTypes.VARBINARY) Slice slice)
{
    Slice hash = Slices.allocate(Integer.BYTES);
    hash.setInt(0, Integer.reverseBytes(SpookyHashV2.hash32(slice, 0, slice.length(), 0)));
    return hash;
}
 
Example 5
Source File: ModelUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
/**
 * Serializes the model using the following format
 * int: format version
 * byte[32]: SHA256 hash of all following data
 * int: id of algorithm
 * int: length of hyperparameters section
 * byte[]: hyperparameters (currently not used)
 * long: length of data section
 * byte[]: model data
 * <p>
 * note: all multibyte values are in little endian
 */
public static Slice serialize(Model model)
{
    requireNonNull(model, "model is null");
    Integer id = MODEL_SERIALIZATION_IDS.get(model.getClass());
    requireNonNull(id, "id is null");
    int size = HYPERPARAMETERS_OFFSET;

    // hyperparameters aren't implemented yet
    byte[] hyperparameters = new byte[0];
    size += hyperparameters.length;

    int dataLengthOffset = size;
    size += SIZE_OF_LONG;
    int dataOffset = size;
    byte[] data = model.getSerializedData();
    size += data.length;

    Slice slice = Slices.allocate(size);
    slice.setInt(VERSION_OFFSET, CURRENT_FORMAT_VERSION);
    slice.setInt(ALGORITHM_OFFSET, id);
    slice.setInt(HYPERPARAMETER_LENGTH_OFFSET, hyperparameters.length);
    slice.setBytes(HYPERPARAMETERS_OFFSET, hyperparameters);
    slice.setLong(dataLengthOffset, data.length);
    slice.setBytes(dataOffset, data);

    byte[] modelHash = Hashing.sha256().hashBytes(slice.getBytes(ALGORITHM_OFFSET, slice.length() - ALGORITHM_OFFSET)).asBytes();
    checkState(modelHash.length == 32, "sha256 hash code expected to be 32 bytes");
    slice.setBytes(HASH_OFFSET, modelHash);

    return slice;
}
 
Example 6
Source File: ParquetWriter.java    From presto with Apache License 2.0 5 votes vote down vote up
private void writeFooter()
        throws IOException
{
    checkState(closed);
    Slice footer = getFooter(rowGroupBuilder.build(), messageType);
    createDataOutput(footer).writeData(outputStream);

    Slice footerSize = Slices.allocate(SIZE_OF_INT);
    footerSize.setInt(0, footer.length());
    createDataOutput(footerSize).writeData(outputStream);

    createDataOutput(MAGIC).writeData(outputStream);
}
 
Example 7
Source File: TestTupleDomainParquetPredicate.java    From presto with Apache License 2.0 5 votes vote down vote up
private static byte[] toParquetEncoding(Instant timestamp)
{
    long startOfDay = LocalDate.ofInstant(timestamp, ZoneOffset.UTC).atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli();
    long timeOfDayNanos = (long) ((timestamp.toEpochMilli() - startOfDay) * Math.pow(10, 6));

    Slice slice = Slices.allocate(12);
    slice.setLong(0, timeOfDayNanos);
    slice.setInt(8, millisToJulianDay(timestamp.toEpochMilli()));
    return slice.byteArray();
}
 
Example 8
Source File: UnscaledDecimal128Arithmetic.java    From presto with Apache License 2.0 4 votes vote down vote up
private static void setRawInt(Slice decimal, int index, int value)
{
    decimal.setInt(SIZE_OF_INT * index, value);
}