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.queue.QueueEntryRow.java

/**
 * For a queue entry consumer state, serialized to byte array, return whether it is processed and committed.
 *//*from ww  w  . jav  a 2  s.  c o  m*/
public static boolean isCommittedProcessed(byte[] stateBytes, Transaction tx) {
    long writePointer = Bytes.toLong(stateBytes, 0, Longs.BYTES);
    if (!tx.isVisible(writePointer)) {
        return false;
    }
    byte state = stateBytes[Longs.BYTES + Ints.BYTES];
    return state == ConsumerEntryState.PROCESSED.getState();
}

From source file:co.cask.cdap.data2.transaction.queue.QueueEntryRow.java

/**
 * Gets the write pointer for a row./*w  w w  . ja v a2 s. c o  m*/
 */
public static long getWritePointer(byte[] rowKey, int queueRowPrefixLength) {
    // Row key is queue_name + writePointer + counter
    return Bytes.toLong(rowKey, queueRowPrefixLength, Longs.BYTES);
}

From source file:io.druid.query.lookup.KafkaLookupExtractorFactory.java

@Override
public LookupExtractor get() {
    final Map<String, String> map = Preconditions.checkNotNull(mapRef.get(), "Not started");
    final long startCount = doubleEventCount.get();
    return new MapLookupExtractor(map, isInjective()) {
        @Override//from ww w .jav a  2  s  .c  o m
        public byte[] getCacheKey() {
            final byte[] idutf8 = StringUtils.toUtf8(factoryId);
            // If the number of things added has not changed during the course of this extractor's life, we can cache it
            if (startCount == doubleEventCount.get()) {
                return ByteBuffer.allocate(idutf8.length + 1 + Longs.BYTES).put(idutf8).put((byte) 0xFF)
                        .putLong(startCount).array();
            } else {
                // If the number of things added HAS changed during the course of this extractor's life, we CANNOT cache
                final byte[] scrambler = StringUtils.toUtf8(UUID.randomUUID().toString());
                return ByteBuffer.allocate(idutf8.length + 1 + scrambler.length + 1).put(idutf8)
                        .put((byte) 0xFF).put(scrambler).put((byte) 0xFF).array();
            }
        }
    };
}

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

private byte[] encodeStateColumn(ConsumerEntryState state) {
    // State column content is encoded as (writePointer) + (instanceId) + (state)
    byte[] stateContent = new byte[Longs.BYTES + Ints.BYTES + 1];
    Bytes.putLong(stateContent, 0, transaction.getWritePointer());
    Bytes.putInt(stateContent, Longs.BYTES, consumerConfig.getInstanceId());
    Bytes.putByte(stateContent, Longs.BYTES + Ints.BYTES, state.getState());
    return stateContent;
}

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

/**
 * Creates a new byte[] that gives the entry row key for the given enqueue transaction and counter.
 *//*from w  ww. jav  a2s  .  com*/
private byte[] getRowKey(long writePointer, int count) {
    byte[] row = Arrays.copyOf(queueRowPrefix, queueRowPrefix.length + Longs.BYTES + Ints.BYTES);
    Bytes.putLong(row, queueRowPrefix.length, writePointer);
    Bytes.putInt(row, queueRowPrefix.length + Longs.BYTES, count);
    return row;
}

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

/**
 * Get the next row based on the given write pointer and counter. It modifies the given row byte[] in place
 * and returns it.//ww  w.j  a  v a 2  s.c o  m
 */
private byte[] getNextRow(byte[] row, long writePointer, int count) {
    Bytes.putLong(row, queueRowPrefix.length, writePointer);
    Bytes.putInt(row, queueRowPrefix.length + Longs.BYTES, count + 1);
    return row;
}

From source file:co.cask.cdap.data2.transaction.queue.AbstractQueueConsumer.java

private byte[] encodeStateColumn(ConsumerEntryState state) {
    // State column content is encoded as (writePointer) + (instanceId) + (state)
    byte[] stateContent = new byte[Longs.BYTES + Ints.BYTES + 1];
    Bytes.putLong(stateContent, 0, transaction.getWritePointer());
    Bytes.putInt(stateContent, Longs.BYTES, getConfig().getInstanceId());
    Bytes.putByte(stateContent, Longs.BYTES + Ints.BYTES, state.getState());
    return stateContent;
}

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

/**
 * Encodes the value for the state column with the current transaction and consumer information.
 *
 * @param state The state to encode/*from   w ww. jav  a  2s .  c  o m*/
 * @return The stateContent byte array
 */
// TODO: This method is copied from AbstractQueue2Consumer. Future effort is needed to unify them.
private byte[] encodeStateColumn(ConsumerEntryState state) {
    byte[] stateContent = new byte[Longs.BYTES + Ints.BYTES + 1];

    // State column content is encoded as (writePointer) + (instanceId) + (state)
    Bytes.putLong(stateContent, 0, transaction.getWritePointer());
    Bytes.putInt(stateContent, Longs.BYTES, consumerConfig.getInstanceId());
    Bytes.putByte(stateContent, Longs.BYTES + Ints.BYTES, state.getState());

    return stateContent;
}

From source file:co.cask.tigon.internal.io.DatumWriterGenerator.java

/**
 * Generates method body for encoding simple schema type by calling corresponding write method in Encoder.
 * @param mg Method body generator//from w  w  w.j  ava2  s.  c  o  m
 * @param type Data type to encode
 * @param encodeMethod Name of the encode method to invoke on the given encoder.
 * @param value Argument index of the value to encode.
 * @param encoder Method argument index of the encoder
 */
private void encodeSimple(GeneratorAdapter mg, TypeToken<?> type, Schema schema, String encodeMethod, int value,
        int encoder) {
    // encoder.writeXXX(value);
    TypeToken<?> encodeType = type;
    mg.loadArg(encoder);
    mg.loadArg(value);
    if (Primitives.isWrapperType(encodeType.getRawType())) {
        encodeType = TypeToken.of(Primitives.unwrap(encodeType.getRawType()));
        mg.unbox(Type.getType(encodeType.getRawType()));
        // A special case since INT type represents (byte, char, short and int).
        if (schema.getType() == Schema.Type.INT && !int.class.equals(encodeType.getRawType())) {
            encodeType = TypeToken.of(int.class);
        }
    } else if (schema.getType() == Schema.Type.STRING && !String.class.equals(encodeType.getRawType())) {
        // For non-string object that has a String schema, invoke toString().
        mg.invokeVirtual(Type.getType(encodeType.getRawType()), getMethod(String.class, "toString"));
        encodeType = TypeToken.of(String.class);
    } else if (schema.getType() == Schema.Type.BYTES && UUID.class.equals(encodeType.getRawType())) {
        // Special case UUID, encode as byte array

        // ByteBuffer buf = ByteBuffer.allocate(Longs.BYTES * 2)
        //                            .putLong(uuid.getMostSignificantBits())
        //                            .putLong(uuid.getLeastSignificantBits());
        // encoder.writeBytes((ByteBuffer) buf.flip());

        Type byteBufferType = Type.getType(ByteBuffer.class);
        Type uuidType = Type.getType(UUID.class);

        mg.push(Longs.BYTES * 2);
        mg.invokeStatic(byteBufferType, getMethod(ByteBuffer.class, "allocate", int.class));
        mg.swap();

        mg.invokeVirtual(uuidType, getMethod(long.class, "getMostSignificantBits"));
        mg.invokeVirtual(byteBufferType, getMethod(ByteBuffer.class, "putLong", long.class));

        mg.loadArg(value);
        mg.invokeVirtual(uuidType, getMethod(long.class, "getLeastSignificantBits"));
        mg.invokeVirtual(byteBufferType, getMethod(ByteBuffer.class, "putLong", long.class));

        mg.invokeVirtual(Type.getType(Buffer.class), getMethod(Buffer.class, "flip"));
        mg.checkCast(byteBufferType);

        encodeType = TypeToken.of(ByteBuffer.class);
    }
    mg.invokeInterface(Type.getType(Encoder.class),
            getMethod(Encoder.class, encodeMethod, encodeType.getRawType()));
    mg.pop();
}

From source file:org.deephacks.confit.internal.hbase.BytesUtils.java

/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1 left operand/*from  w w w  . j a v a 2  s .co m*/
 * @param buffer2 right operand
 * @param offset1 Where to start comparing in the left buffer
 * @param offset2 Where to start comparing in the right buffer
 * @param length1 How much to compare from the left buffer
 * @param length2 How much to compare from the right buffer
 * @return 0 if equal, < 0 if left is less than right, etc.
 */
public static int compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2,
        int length2) {
    // Short circuit equal case
    if (buffer1 == buffer2 && offset1 == offset2 && length1 == length2) {
        return 0;
    }
    int minLength = Math.min(length1, length2);
    int minWords = minLength / Longs.BYTES;
    int offset1Adj = offset1 + BYTE_ARRAY_BASE_OFFSET;
    int offset2Adj = offset2 + BYTE_ARRAY_BASE_OFFSET;

    /*
     * Compare 8 bytes at a time. Benchmarking shows comparing 8 bytes at a
     * time is no slower than comparing 4 bytes at a time even on 32-bit.
     * On the other hand, it is substantially faster on 64-bit.
     */
    for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) {
        long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i);
        long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i);
        long diff = lw ^ rw;

        if (diff != 0) {
            if (!littleEndian) {
                return lessThanUnsigned(lw, rw) ? -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 result = UnsignedBytes.compare(buffer1[offset1 + i], buffer2[offset2 + i]);
        if (result != 0) {
            return result;
        }
    }
    return length1 - length2;
}