Example usage for io.netty.buffer ByteBuf writeLong

List of usage examples for io.netty.buffer ByteBuf writeLong

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf writeLong.

Prototype

public abstract ByteBuf writeLong(long value);

Source Link

Document

Sets the specified 64-bit long integer at the current writerIndex and increases the writerIndex by 8 in this buffer.

Usage

From source file:org.apache.bookkeeper.test.BookieClientTest.java

License:Apache License

private ByteBufList createByteBuffer(int i, long lid, long eid) {
    ByteBuf bb = Unpooled.buffer(4 + 24);
    bb.writeLong(lid);
    bb.writeLong(eid);/*ww w  . j  a  va 2s  .c  o  m*/
    bb.writeLong(eid - 1);
    bb.writeInt(i);
    return ByteBufList.get(bb);
}

From source file:org.apache.distributedlog.LogRecord.java

License:Apache License

private void writeToStream(ByteBuf out) {
    out.writeLong(metadata);
    out.writeLong(txid);
    writePayload(out);
}

From source file:org.apache.drill.exec.store.hbase.CompareFunctionsProcessor.java

License:Apache License

@Override
public Boolean visitConvertExpression(ConvertExpression e, LogicalExpression valueArg) throws RuntimeException {
    if (e.getConvertFunction() == ConvertExpression.CONVERT_FROM) {

        String encodingType = e.getEncodingType();
        int prefixLength = 0;

        // Handle scan pruning in the following scenario:
        // The row-key is a composite key and the CONVERT_FROM() function has byte_substr() as input function which is
        // querying for the first few bytes of the row-key(start-offset 1)
        // Example WHERE clause:
        // CONVERT_FROM(BYTE_SUBSTR(row_key, 1, 8), 'DATE_EPOCH_BE') < DATE '2015-06-17'
        if (e.getInput() instanceof FunctionCall) {

            // We can prune scan range only for big-endian encoded data
            if (encodingType.endsWith("_BE") == false) {
                return false;
            }//from   w w  w . j  a v  a 2 s .com

            FunctionCall call = (FunctionCall) e.getInput();
            String functionName = call.getName();
            if (!functionName.equalsIgnoreCase("byte_substr")) {
                return false;
            }

            LogicalExpression nameArg = call.args.get(0);
            LogicalExpression valueArg1 = call.args.size() >= 2 ? call.args.get(1) : null;
            LogicalExpression valueArg2 = call.args.size() >= 3 ? call.args.get(2) : null;

            if (((nameArg instanceof SchemaPath) == false) || (valueArg1 == null)
                    || ((valueArg1 instanceof IntExpression) == false) || (valueArg2 == null)
                    || ((valueArg2 instanceof IntExpression) == false)) {
                return false;
            }

            boolean isRowKey = ((SchemaPath) nameArg).getAsUnescapedPath().equals(DrillHBaseConstants.ROW_KEY);
            int offset = ((IntExpression) valueArg1).getInt();

            if (!isRowKey || (offset != 1)) {
                return false;
            }

            this.path = (SchemaPath) nameArg;
            prefixLength = ((IntExpression) valueArg2).getInt();
            this.isRowKeyPrefixComparison = true;
            return visitRowKeyPrefixConvertExpression(e, prefixLength, valueArg);
        }

        if (e.getInput() instanceof SchemaPath) {
            ByteBuf bb = null;
            switch (encodingType) {
            case "INT_BE":
            case "INT":
            case "UINT_BE":
            case "UINT":
            case "UINT4_BE":
            case "UINT4":
                if (valueArg instanceof IntExpression && (isEqualityFn || encodingType.startsWith("U"))) {
                    bb = newByteBuf(4, encodingType.endsWith("_BE"));
                    bb.writeInt(((IntExpression) valueArg).getInt());
                }
                break;
            case "BIGINT_BE":
            case "BIGINT":
            case "UINT8_BE":
            case "UINT8":
                if (valueArg instanceof LongExpression && (isEqualityFn || encodingType.startsWith("U"))) {
                    bb = newByteBuf(8, encodingType.endsWith("_BE"));
                    bb.writeLong(((LongExpression) valueArg).getLong());
                }
                break;
            case "FLOAT":
                if (valueArg instanceof FloatExpression && isEqualityFn) {
                    bb = newByteBuf(4, true);
                    bb.writeFloat(((FloatExpression) valueArg).getFloat());
                }
                break;
            case "DOUBLE":
                if (valueArg instanceof DoubleExpression && isEqualityFn) {
                    bb = newByteBuf(8, true);
                    bb.writeDouble(((DoubleExpression) valueArg).getDouble());
                }
                break;
            case "TIME_EPOCH":
            case "TIME_EPOCH_BE":
                if (valueArg instanceof TimeExpression) {
                    bb = newByteBuf(8, encodingType.endsWith("_BE"));
                    bb.writeLong(((TimeExpression) valueArg).getTime());
                }
                break;
            case "DATE_EPOCH":
            case "DATE_EPOCH_BE":
                if (valueArg instanceof DateExpression) {
                    bb = newByteBuf(8, encodingType.endsWith("_BE"));
                    bb.writeLong(((DateExpression) valueArg).getDate());
                }
                break;
            case "BOOLEAN_BYTE":
                if (valueArg instanceof BooleanExpression) {
                    bb = newByteBuf(1, false /* does not matter */);
                    bb.writeByte(((BooleanExpression) valueArg).getBoolean() ? 1 : 0);
                }
                break;
            case "DOUBLE_OB":
            case "DOUBLE_OBD":
                if (valueArg instanceof DoubleExpression) {
                    bb = newByteBuf(9, true);
                    PositionedByteRange br = new SimplePositionedMutableByteRange(bb.array(), 0, 9);
                    if (encodingType.endsWith("_OBD")) {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeFloat64(br,
                                ((DoubleExpression) valueArg).getDouble(), Order.DESCENDING);
                        this.sortOrderAscending = false;
                    } else {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeFloat64(br,
                                ((DoubleExpression) valueArg).getDouble(), Order.ASCENDING);
                    }
                }
                break;
            case "FLOAT_OB":
            case "FLOAT_OBD":
                if (valueArg instanceof FloatExpression) {
                    bb = newByteBuf(5, true);
                    PositionedByteRange br = new SimplePositionedMutableByteRange(bb.array(), 0, 5);
                    if (encodingType.endsWith("_OBD")) {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeFloat32(br,
                                ((FloatExpression) valueArg).getFloat(), Order.DESCENDING);
                        this.sortOrderAscending = false;
                    } else {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeFloat32(br,
                                ((FloatExpression) valueArg).getFloat(), Order.ASCENDING);
                    }
                }
                break;
            case "BIGINT_OB":
            case "BIGINT_OBD":
                if (valueArg instanceof LongExpression) {
                    bb = newByteBuf(9, true);
                    PositionedByteRange br = new SimplePositionedMutableByteRange(bb.array(), 0, 9);
                    if (encodingType.endsWith("_OBD")) {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeInt64(br,
                                ((LongExpression) valueArg).getLong(), Order.DESCENDING);
                        this.sortOrderAscending = false;
                    } else {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeInt64(br,
                                ((LongExpression) valueArg).getLong(), Order.ASCENDING);
                    }
                }
                break;
            case "INT_OB":
            case "INT_OBD":
                if (valueArg instanceof IntExpression) {
                    bb = newByteBuf(5, true);
                    PositionedByteRange br = new SimplePositionedMutableByteRange(bb.array(), 0, 5);
                    if (encodingType.endsWith("_OBD")) {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeInt32(br,
                                ((IntExpression) valueArg).getInt(), Order.DESCENDING);
                        this.sortOrderAscending = false;
                    } else {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeInt32(br,
                                ((IntExpression) valueArg).getInt(), Order.ASCENDING);
                    }
                }
                break;
            case "UTF8":
                // let visitSchemaPath() handle this.
                return e.getInput().accept(this, valueArg);
            }

            if (bb != null) {
                this.value = bb.array();
                this.path = (SchemaPath) e.getInput();
                return true;
            }
        }
    }
    return false;
}

From source file:org.apache.drill.exec.store.mapr.db.binary.CompareFunctionsProcessor.java

License:Apache License

@Override
public Boolean visitConvertExpression(ConvertExpression e, LogicalExpression valueArg) throws RuntimeException {
    if (e.getConvertFunction() == ConvertExpression.CONVERT_FROM) {

        String encodingType = e.getEncodingType();
        int prefixLength = 0;

        // Handle scan pruning in the following scenario:
        // The row-key is a composite key and the CONVERT_FROM() function has byte_substr() as input function which is
        // querying for the first few bytes of the row-key(start-offset 1)
        // Example WHERE clause:
        // CONVERT_FROM(BYTE_SUBSTR(row_key, 1, 8), 'DATE_EPOCH_BE') < DATE '2015-06-17'
        if (e.getInput() instanceof FunctionCall) {

            // We can prune scan range only for big-endian encoded data
            if (encodingType.endsWith("_BE") == false) {
                return false;
            }/*from   w  w  w.  jav  a2  s. c  om*/

            FunctionCall call = (FunctionCall) e.getInput();
            String functionName = call.getName();
            if (!functionName.equalsIgnoreCase("byte_substr")) {
                return false;
            }

            LogicalExpression nameArg = call.args.get(0);
            LogicalExpression valueArg1 = call.args.size() >= 2 ? call.args.get(1) : null;
            LogicalExpression valueArg2 = call.args.size() >= 3 ? call.args.get(2) : null;

            if (((nameArg instanceof SchemaPath) == false) || (valueArg1 == null)
                    || ((valueArg1 instanceof IntExpression) == false) || (valueArg2 == null)
                    || ((valueArg2 instanceof IntExpression) == false)) {
                return false;
            }

            boolean isRowKey = ((SchemaPath) nameArg).getAsUnescapedPath().equals(DrillHBaseConstants.ROW_KEY);
            int offset = ((IntExpression) valueArg1).getInt();

            if (!isRowKey || (offset != 1)) {
                return false;
            }

            this.path = (SchemaPath) nameArg;
            prefixLength = ((IntExpression) valueArg2).getInt();
            this.isRowKeyPrefixComparison = true;
            return visitRowKeyPrefixConvertExpression(e, prefixLength, valueArg);
        }

        if (e.getInput() instanceof SchemaPath) {
            ByteBuf bb = null;

            switch (encodingType) {
            case "INT_BE":
            case "INT":
            case "UINT_BE":
            case "UINT":
            case "UINT4_BE":
            case "UINT4":
                if (valueArg instanceof IntExpression && (isEqualityFn || encodingType.startsWith("U"))) {
                    bb = newByteBuf(4, encodingType.endsWith("_BE"));
                    bb.writeInt(((IntExpression) valueArg).getInt());
                }
                break;
            case "BIGINT_BE":
            case "BIGINT":
            case "UINT8_BE":
            case "UINT8":
                if (valueArg instanceof LongExpression && (isEqualityFn || encodingType.startsWith("U"))) {
                    bb = newByteBuf(8, encodingType.endsWith("_BE"));
                    bb.writeLong(((LongExpression) valueArg).getLong());
                }
                break;
            case "FLOAT":
                if (valueArg instanceof FloatExpression && isEqualityFn) {
                    bb = newByteBuf(4, true);
                    bb.writeFloat(((FloatExpression) valueArg).getFloat());
                }
                break;
            case "DOUBLE":
                if (valueArg instanceof DoubleExpression && isEqualityFn) {
                    bb = newByteBuf(8, true);
                    bb.writeDouble(((DoubleExpression) valueArg).getDouble());
                }
                break;
            case "TIME_EPOCH":
            case "TIME_EPOCH_BE":
                if (valueArg instanceof TimeExpression) {
                    bb = newByteBuf(8, encodingType.endsWith("_BE"));
                    bb.writeLong(((TimeExpression) valueArg).getTime());
                }
                break;
            case "DATE_EPOCH":
            case "DATE_EPOCH_BE":
                if (valueArg instanceof DateExpression) {
                    bb = newByteBuf(8, encodingType.endsWith("_BE"));
                    bb.writeLong(((DateExpression) valueArg).getDate());
                }
                break;
            case "BOOLEAN_BYTE":
                if (valueArg instanceof BooleanExpression) {
                    bb = newByteBuf(1, false /* does not matter */);
                    bb.writeByte(((BooleanExpression) valueArg).getBoolean() ? 1 : 0);
                }
                break;
            case "DOUBLE_OB":
            case "DOUBLE_OBD":
                if (valueArg instanceof DoubleExpression) {
                    bb = newByteBuf(9, true);
                    PositionedByteRange br = new SimplePositionedMutableByteRange(bb.array(), 0, 9);
                    if (encodingType.endsWith("_OBD")) {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeFloat64(br,
                                ((DoubleExpression) valueArg).getDouble(), Order.DESCENDING);
                        this.sortOrderAscending = false;
                    } else {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeFloat64(br,
                                ((DoubleExpression) valueArg).getDouble(), Order.ASCENDING);
                    }
                }
                break;
            case "FLOAT_OB":
            case "FLOAT_OBD":
                if (valueArg instanceof FloatExpression) {
                    bb = newByteBuf(5, true);
                    PositionedByteRange br = new SimplePositionedMutableByteRange(bb.array(), 0, 5);
                    if (encodingType.endsWith("_OBD")) {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeFloat32(br,
                                ((FloatExpression) valueArg).getFloat(), Order.DESCENDING);
                        this.sortOrderAscending = false;
                    } else {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeFloat32(br,
                                ((FloatExpression) valueArg).getFloat(), Order.ASCENDING);
                    }
                }
                break;
            case "BIGINT_OB":
            case "BIGINT_OBD":
                if (valueArg instanceof LongExpression) {
                    bb = newByteBuf(9, true);
                    PositionedByteRange br = new SimplePositionedMutableByteRange(bb.array(), 0, 9);
                    if (encodingType.endsWith("_OBD")) {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeInt64(br,
                                ((LongExpression) valueArg).getLong(), Order.DESCENDING);
                        this.sortOrderAscending = false;
                    } else {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeInt64(br,
                                ((LongExpression) valueArg).getLong(), Order.ASCENDING);
                    }
                }
                break;
            case "INT_OB":
            case "INT_OBD":
                if (valueArg instanceof IntExpression) {
                    bb = newByteBuf(5, true);
                    PositionedByteRange br = new SimplePositionedMutableByteRange(bb.array(), 0, 5);
                    if (encodingType.endsWith("_OBD")) {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeInt32(br,
                                ((IntExpression) valueArg).getInt(), Order.DESCENDING);
                        this.sortOrderAscending = false;
                    } else {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeInt32(br,
                                ((IntExpression) valueArg).getInt(), Order.ASCENDING);
                    }
                }
                break;
            case "UTF8_OB":
            case "UTF8_OBD":
                if (valueArg instanceof QuotedString) {
                    int stringLen = ((QuotedString) valueArg).value.getBytes(Charsets.UTF_8).length;
                    bb = newByteBuf(stringLen + 2, true);
                    PositionedByteRange br = new SimplePositionedMutableByteRange(bb.array(), 0, stringLen + 2);
                    if (encodingType.endsWith("_OBD")) {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeString(br,
                                ((QuotedString) valueArg).value, Order.DESCENDING);
                        this.sortOrderAscending = false;
                    } else {
                        org.apache.hadoop.hbase.util.OrderedBytes.encodeString(br,
                                ((QuotedString) valueArg).value, Order.ASCENDING);
                    }
                }
                break;
            case "UTF8":
                // let visitSchemaPath() handle this.
                return e.getInput().accept(this, valueArg);
            }

            if (bb != null) {
                this.value = bb.array();
                this.path = (SchemaPath) e.getInput();
                return true;
            }
        }
    }
    return false;
}

From source file:org.apache.flink.runtime.query.netty.message.KvStateRequestSerializer.java

License:Apache License

/**
 * Allocates a buffer and serializes the KvState request into it.
 *
 * @param alloc                     ByteBuf allocator for the buffer to
 *                                  serialize message into
 * @param requestId                 ID for this request
 * @param kvStateId                 ID of the requested KvState instance
 * @param serializedKeyAndNamespace Serialized key and namespace to request
 *                                  from the KvState instance.
 * @return Serialized KvState request message
 *//*from   w  w w. j  a  v a 2s.c o  m*/
public static ByteBuf serializeKvStateRequest(ByteBufAllocator alloc, long requestId, KvStateID kvStateId,
        byte[] serializedKeyAndNamespace) {

    // Header + request ID + KvState ID + Serialized namespace
    int frameLength = HEADER_LENGTH + 8 + (8 + 8) + (4 + serializedKeyAndNamespace.length);
    ByteBuf buf = alloc.ioBuffer(frameLength + 4); // +4 for frame length

    buf.writeInt(frameLength);

    writeHeader(buf, KvStateRequestType.REQUEST);

    buf.writeLong(requestId);
    buf.writeLong(kvStateId.getLowerPart());
    buf.writeLong(kvStateId.getUpperPart());
    buf.writeInt(serializedKeyAndNamespace.length);
    buf.writeBytes(serializedKeyAndNamespace);

    return buf;
}

From source file:org.apache.flink.runtime.query.netty.message.KvStateRequestSerializer.java

License:Apache License

/**
 * Allocates a buffer and serializes the KvState request result into it.
 *
 * @param alloc             ByteBuf allocator for the buffer to serialize message into
 * @param requestId         ID for this request
 * @param serializedResult  Serialized Result
 * @return Serialized KvState request result message
 *///from   w w  w  . j a va2s.  c o  m
public static ByteBuf serializeKvStateRequestResult(ByteBufAllocator alloc, long requestId,
        byte[] serializedResult) {

    Preconditions.checkNotNull(serializedResult, "Serialized result");

    // Header + request ID + serialized result
    int frameLength = HEADER_LENGTH + 8 + 4 + serializedResult.length;

    ByteBuf buf = alloc.ioBuffer(frameLength);

    buf.writeInt(frameLength);
    writeHeader(buf, KvStateRequestType.REQUEST_RESULT);
    buf.writeLong(requestId);

    buf.writeInt(serializedResult.length);
    buf.writeBytes(serializedResult);

    return buf;
}

From source file:org.apache.flink.runtime.query.netty.message.KvStateRequestSerializer.java

License:Apache License

/**
 * Allocates a buffer and serializes the KvState request failure into it.
 *
 * @param alloc ByteBuf allocator for the buffer to serialize message into
 * @param requestId ID of the request responding to
 * @param cause Failure cause//from   www  .  j  a  va 2s.  com
 * @return Serialized KvState request failure message
 * @throws IOException Serialization failures are forwarded
 */
public static ByteBuf serializeKvStateRequestFailure(ByteBufAllocator alloc, long requestId, Throwable cause)
        throws IOException {

    ByteBuf buf = alloc.ioBuffer();

    // Frame length is set at the end
    buf.writeInt(0);

    writeHeader(buf, KvStateRequestType.REQUEST_FAILURE);

    // Message
    buf.writeLong(requestId);

    try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
            ObjectOutputStream out = new ObjectOutputStream(bbos)) {

        out.writeObject(cause);
    }

    // Set frame length
    int frameLength = buf.readableBytes() - 4;
    buf.setInt(0, frameLength);

    return buf;
}

From source file:org.apache.giraph.comm.netty.handler.RequestServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (LOG.isTraceEnabled()) {
        LOG.trace("messageReceived: Got " + msg.getClass());
    }//w w w.j  a  va2 s.c  om

    WritableRequest request = (WritableRequest) msg;

    // Simulate a closed connection on the first request (if desired)
    if (closeFirstRequest && !ALREADY_CLOSED_FIRST_REQUEST) {
        LOG.info("messageReceived: Simulating closing channel on first " + "request " + request.getRequestId()
                + " from " + request.getClientId());
        setAlreadyClosedFirstRequest();
        ctx.close();
        return;
    }

    // Only execute this request exactly once
    int alreadyDone = 1;
    if (workerRequestReservedMap.reserveRequest(request.getClientId(), request.getRequestId())) {
        if (LOG.isDebugEnabled()) {
            startProcessingNanoseconds = TIME.getNanoseconds();
        }
        processRequest((R) request);
        if (LOG.isDebugEnabled()) {
            LOG.debug("messageReceived: Processing client " + request.getClientId() + ", " + "requestId "
                    + request.getRequestId() + ", " + request.getType() + " took "
                    + Times.getNanosSince(TIME, startProcessingNanoseconds) + " ns");
        }
        alreadyDone = 0;
    } else {
        LOG.info("messageReceived: Request id " + request.getRequestId() + " from client "
                + request.getClientId() + " was already processed, " + "not processing again.");
    }

    // Send the response with the request id
    ByteBuf buffer = ctx.alloc().buffer(RESPONSE_BYTES);
    buffer.writeInt(myTaskInfo.getTaskId());
    buffer.writeLong(request.getRequestId());
    buffer.writeByte(alreadyDone);

    ctx.write(buffer);
}

From source file:org.apache.jackrabbit.oak.plugins.segment.standby.codec.BlobEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Blob b, ByteBuf out) throws Exception {
    byte[] bytes = null;
    InputStream s = b.getNewStream();
    try {/* w w w.ja  v  a  2 s. co m*/
        bytes = IOUtils.toByteArray(s);
    } finally {
        s.close();
    }

    Hasher hasher = Hashing.murmur3_32().newHasher();
    long hash = hasher.putBytes(bytes).hash().padToLong();

    out.writeInt(bytes.length);
    out.writeByte(Messages.HEADER_BLOB);

    String bid = b.getContentIdentity();
    byte[] id = bid.getBytes(Charset.forName("UTF-8"));
    out.writeInt(id.length);
    out.writeBytes(id);

    out.writeLong(hash);
    out.writeBytes(bytes);
}

From source file:org.apache.jackrabbit.oak.plugins.segment.standby.codec.SegmentEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Segment s, ByteBuf out) throws Exception {
    SegmentId id = s.getSegmentId();//  w  w w.  j  a  v  a2 s .  co  m
    ByteArrayOutputStream baos = new ByteArrayOutputStream(s.size());
    s.writeTo(baos);
    byte[] segment = baos.toByteArray();

    Hasher hasher = Hashing.murmur3_32().newHasher();
    long hash = hasher.putBytes(segment).hash().padToLong();

    int len = segment.length + EXTRA_HEADERS_WO_SIZE;
    out.writeInt(len);
    out.writeByte(Messages.HEADER_SEGMENT);
    out.writeLong(id.getMostSignificantBits());
    out.writeLong(id.getLeastSignificantBits());
    out.writeLong(hash);
    out.writeBytes(segment);
}