Example usage for com.google.common.primitives Longs BYTES

List of usage examples for com.google.common.primitives Longs BYTES

Introduction

In this page you can find the example usage for com.google.common.primitives Longs BYTES.

Prototype

int BYTES

To view the source code for com.google.common.primitives Longs BYTES.

Click Source Link

Document

The number of bytes required to represent a primitive long value.

Usage

From source file:co.cask.cdap.data2.transaction.stream.StreamConsumerStateStore.java

private byte[] getColumn(long groupId, int instanceId) {
    byte[] column = new byte[Longs.BYTES + Ints.BYTES];
    Bytes.putLong(column, 0, groupId);/* w  ww  . j  a  va  2s .c o m*/
    Bytes.putInt(column, Longs.BYTES, instanceId);
    return column;
}

From source file:com.palantir.atlasdb.ptobject.EncodingUtils.java

public static byte[] encodeUUID(UUID uuid) {
    return ByteBuffer.allocate(2 * Longs.BYTES).order(ByteOrder.BIG_ENDIAN)
            .putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits()).array();
}

From source file:co.cask.cdap.data2.transaction.stream.StreamConsumerStateStore.java

/**
 * Decodes the instance id from the column name.
 *///from w  ww. j a  v  a 2s  .c om
private int getInstanceId(byte[] columnName) {
    return Bytes.toInt(columnName, Longs.BYTES);
}

From source file:com.palantir.atlasdb.keyvalue.impl.ProfilingKeyValueService.java

@Override
public Multimap<Cell, Long> getAllTimestamps(String tableName, Set<Cell> cells, long timestamp) {
    if (log.isTraceEnabled()) {
        Stopwatch stopwatch = Stopwatch.createStarted();
        Multimap<Cell, Long> result = delegate.getAllTimestamps(tableName, cells, timestamp);
        logCellsAndSize("getAllTimestamps", tableName, cells.size(), cells.size() * Longs.BYTES, stopwatch);
        return result;
    } else {/*from  www. ja va2s.  co m*/
        return delegate.getAllTimestamps(tableName, cells, timestamp);
    }
}

From source file:com.palantir.atlasdb.ptobject.EncodingUtils.java

public static UUID decodeUUID(byte[] bytes, int offset) {
    ByteBuffer buf = ByteBuffer.wrap(bytes, offset, 2 * Longs.BYTES).order(ByteOrder.BIG_ENDIAN);
    long mostSigBits = buf.getLong();
    long leastSigBits = buf.getLong();
    return new UUID(mostSigBits, leastSigBits);
}

From source file:com.palantir.atlasdb.ptobject.EncodingUtils.java

public static UUID decodeFlippedUUID(byte[] bytes, int offset) {
    ByteBuffer buf = ByteBuffer.wrap(bytes, offset, 2 * Longs.BYTES).order(ByteOrder.BIG_ENDIAN);
    long mostSigBits = -1L ^ buf.getLong();
    long leastSigBits = -1L ^ buf.getLong();
    return new UUID(mostSigBits, leastSigBits);
}

From source file:org.apache.ignite.internal.processors.hadoop.impl.HadoopUtils.java

/**
 * Internal comparison routine.//  w w w.java2 s  .  com
 *
 * @param buf1 Bytes 1.
 * @param len1 Length 1.
 * @param ptr2 Pointer 2.
 * @param len2 Length 2.
 * @return Result.
 */
@SuppressWarnings("SuspiciousNameCombination")
public static int compareBytes(byte[] buf1, int len1, long ptr2, int len2) {
    int minLength = Math.min(len1, len2);

    int minWords = minLength / Longs.BYTES;

    for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) {
        long lw = GridUnsafe.getLong(buf1, GridUnsafe.BYTE_ARR_OFF + i);
        long rw = GridUnsafe.getLong(ptr2 + i);

        long diff = lw ^ rw;

        if (diff != 0) {
            if (GridUnsafe.BIG_ENDIAN)
                return (lw + Long.MIN_VALUE) < (rw + Long.MIN_VALUE) ? -1 : 1;

            // Use binary search
            int n = 0;
            int y;
            int x = (int) diff;

            if (x == 0) {
                x = (int) (diff >>> 32);

                n = 32;
            }

            y = x << 16;

            if (y == 0)
                n += 16;
            else
                x = y;

            y = x << 8;

            if (y == 0)
                n += 8;

            return (int) (((lw >>> n) & 0xFFL) - ((rw >>> n) & 0xFFL));
        }
    }

    // The epilogue to cover the last (minLength % 8) elements.
    for (int i = minWords * Longs.BYTES; i < minLength; i++) {
        int res = UnsignedBytes.compare(buf1[i], GridUnsafe.getByte(ptr2 + i));

        if (res != 0)
            return res;
    }

    return len1 - len2;
}

From source file:org.apache.kudu.util.Slice.java

/**
 * Sets the specified 64-bit long integer at the specified absolute
 * {@code index} in this buffer.// w  w w.j a va 2s  .c o  m
 *
 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
 * {@code index + 8} is greater than {@code this.capacity}
 */
public void setLong(int index, long value) {
    Preconditions.checkPositionIndexes(index, index + Longs.BYTES, this.length);
    index += offset;
    data[index] = (byte) (value);
    data[index + 1] = (byte) (value >>> 8);
    data[index + 2] = (byte) (value >>> 16);
    data[index + 3] = (byte) (value >>> 24);
    data[index + 4] = (byte) (value >>> 32);
    data[index + 5] = (byte) (value >>> 40);
    data[index + 6] = (byte) (value >>> 48);
    data[index + 7] = (byte) (value >>> 56);
}

From source file:co.cask.tigon.data.transaction.queue.AbstractQueueConsumer.java

private void populateRowCache(Set<byte[]> excludeRows, int maxBatchSize) throws IOException {

    long readPointer = transaction.getReadPointer();

    // Scan the table for queue entries.
    int numRows = Math.max(MIN_FETCH_ROWS, maxBatchSize * PREFETCH_BATCHES);
    if (scanStartRow == null) {
        scanStartRow = Arrays.copyOf(startRow, startRow.length);
    }//from  www.  j av a2 s  . c om
    QueueScanner scanner = getScanner(scanStartRow,
            QueueEntryRow.getStopRowForTransaction(queueRowPrefix, transaction), numRows);
    try {
        // Try fill up the cache
        boolean firstScannedRow = true;

        while (entryCache.size() < numRows) {
            ImmutablePair<byte[], Map<byte[], byte[]>> entry = scanner.next();
            if (entry == null) {
                // No more result, breaking out.
                break;
            }

            byte[] rowKey = entry.getFirst();
            if (excludeRows.contains(rowKey)) {
                continue;
            }

            // Row key is queue_name + writePointer + counter
            long writePointer = Bytes.toLong(rowKey, queueRowPrefix.length, Longs.BYTES);

            // If it is first row returned by the scanner and was written before the earliest in progress,
            // it's safe to advance scanStartRow to current row because nothing can be written before this row.
            if (firstScannedRow && writePointer < transaction.getFirstInProgress()) {
                firstScannedRow = false;
                scanStartRow = Arrays.copyOf(rowKey, rowKey.length);
            }

            // If writes later than the reader pointer, abort the loop, as entries that comes later are all uncommitted.
            // this is probably not needed due to the limit of the scan to the stop row, but to be safe...
            if (writePointer > readPointer) {
                break;
            }
            // If the write is in the excluded list, ignore it.
            if (transaction.isExcluded(writePointer)) {
                continue;
            }

            // Based on the strategy to determine if include the given entry or not.
            byte[] dataBytes = entry.getSecond().get(QueueEntryRow.DATA_COLUMN);
            byte[] metaBytes = entry.getSecond().get(QueueEntryRow.META_COLUMN);

            if (dataBytes == null || metaBytes == null) {
                continue;
            }

            byte[] stateBytes = entry.getSecond().get(stateColumnName);

            int counter = Bytes.toInt(rowKey, rowKey.length - 4, Ints.BYTES);
            if (!shouldInclude(writePointer, counter, metaBytes, stateBytes)) {
                continue;
            }

            entryCache.put(rowKey, new SimpleQueueEntry(rowKey, dataBytes, stateBytes));
        }
    } finally {
        scanner.close();
    }
}

From source file:org.apache.beam.sdk.io.gcp.spanner.OrderedCode.java

/**
 * @see #readSignedNumIncreasing()//from  ww  w.j a v a2  s. c  o  m
 */
public void writeSignedNumIncreasing(long val) {
    long x = val < 0 ? ~val : val;
    if (x < 64) { // Fast path for encoding length == 1.
        byte[] encodedArray = new byte[] { (byte) (LENGTH_TO_HEADER_BITS[1][0] ^ val) };
        encodedArrays.add(encodedArray);
        return;
    }
    // buf = val in network byte order, sign extended to 10 bytes.
    byte signByte = val < 0 ? (byte) 0xff : 0;
    byte[] buf = new byte[2 + Longs.BYTES];
    buf[0] = buf[1] = signByte;
    System.arraycopy(Longs.toByteArray(val), 0, buf, 2, Longs.BYTES);
    int len = getSignedEncodingLength(x);
    if (len < 2) {
        throw new IllegalStateException(
                "Invalid length (" + len + ")" + " returned by getSignedEncodingLength(" + x + ")");
    }
    int beginIndex = buf.length - len;
    buf[beginIndex] ^= LENGTH_TO_HEADER_BITS[len][0];
    buf[beginIndex + 1] ^= LENGTH_TO_HEADER_BITS[len][1];

    byte[] encodedArray = new byte[len];
    System.arraycopy(buf, beginIndex, encodedArray, 0, len);
    encodedArrays.add(encodedArray);
}