Example usage for io.netty.buffer ByteBuf writeInt

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

Introduction

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

Prototype

public abstract ByteBuf writeInt(int value);

Source Link

Document

Sets the specified 32-bit integer at the current writerIndex and increases the writerIndex by 4 in this buffer.

Usage

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandler.java

License:Open Source License

/**
 * Encodes a {@link BinaryStoreRequest} into its lower level representation.
 *
 * There are three types of store operations that need to be considered: insert, upsert and replace, which
 * directly translate to the add, set and replace binary memcached opcodes. By convention, only the replace
 * command supports setting a CAS value, even if the others theoretically would do as well (but do not provide
 * benefit in such cases)./*from  w w  w . ja v a2  s  . c  o  m*/
 *
 * Currently, the content is loaded and sent down in one batch, streaming for requests is not supported.
 *
 * @return a ready {@link BinaryMemcacheRequest}.
 */
private static BinaryMemcacheRequest handleStoreRequest(final ChannelHandlerContext ctx,
        final BinaryStoreRequest msg) {
    ByteBuf extras = ctx.alloc().buffer(8);
    extras.writeInt(msg.flags());
    extras.writeInt(msg.expiration());

    String key = msg.key();
    short keyLength = (short) key.length();
    byte extrasLength = (byte) extras.readableBytes();
    FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, extras, msg.content());

    if (msg instanceof InsertRequest) {
        request.setOpcode(OP_INSERT);
    } else if (msg instanceof UpsertRequest) {
        request.setOpcode(OP_UPSERT);
    } else if (msg instanceof ReplaceRequest) {
        request.setOpcode(OP_REPLACE);
        request.setCAS(((ReplaceRequest) msg).cas());
    } else {
        throw new IllegalArgumentException("Unknown incoming BinaryStoreRequest type " + msg.getClass());
    }

    request.setKeyLength(keyLength);
    request.setTotalBodyLength(keyLength + msg.content().readableBytes() + extrasLength);
    request.setExtrasLength(extrasLength);
    return request;
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandler.java

License:Open Source License

/**
 * Encodes a {@link CounterRequest} into its lower level representation.
 *
 * Depending on if the {@link CounterRequest#delta} is positive or negative, either the incr or decr memcached
 * commands are utilized. The value is converted to its absolute variant to conform with the protocol.
 *
 * @return a ready {@link BinaryMemcacheRequest}.
 *///from  w  w  w.j  av  a2 s  .c om
private static BinaryMemcacheRequest handleCounterRequest(final ChannelHandlerContext ctx,
        final CounterRequest msg) {
    ByteBuf extras = ctx.alloc().buffer();
    extras.writeLong(Math.abs(msg.delta()));
    extras.writeLong(msg.initial());
    extras.writeInt(msg.expiry());

    String key = msg.key();
    short keyLength = (short) key.length();
    byte extrasLength = (byte) extras.readableBytes();
    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key, extras);
    request.setOpcode(msg.delta() < 0 ? OP_COUNTER_DECR : OP_COUNTER_INCR);
    request.setKeyLength(keyLength);
    request.setTotalBodyLength(keyLength + extrasLength);
    request.setExtrasLength(extrasLength);
    return request;
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandler.java

License:Open Source License

/**
 * Encodes a {@link TouchRequest} into its lower level representation.
 *
 * @return a ready {@link BinaryMemcacheRequest}.
 *///w  w  w . j a v a 2s  .c o m
private static BinaryMemcacheRequest handleTouchRequest(final ChannelHandlerContext ctx,
        final TouchRequest msg) {
    ByteBuf extras = ctx.alloc().buffer();
    extras.writeInt(msg.expiry());

    String key = msg.key();
    short keyLength = (short) key.length();
    byte extrasLength = (byte) extras.readableBytes();
    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key);
    request.setExtras(extras);
    request.setOpcode(OP_TOUCH);
    request.setKeyLength(keyLength);
    request.setTotalBodyLength(keyLength + extrasLength);
    request.setExtrasLength(extrasLength);
    return request;
}

From source file:com.couchbase.client.core.endpoint.dcp.DCPConnectionHandler.java

License:Apache License

/**
 * Creates a DCP Open Connection Request.
 *
 * @param ctx the channel handler context.
 * @return a converted {@link BinaryMemcacheRequest}.
 *//*from   ww w  .j  a  va  2 s.c  o m*/
private BinaryMemcacheRequest handleOpenConnectionRequest(final ChannelHandlerContext ctx) {
    ByteBuf extras = ctx.alloc().buffer(8);
    extras.writeInt(0) // sequence number
            .writeInt(ConnectionType.CONSUMER.flags());

    byte[] key = connectionName.getBytes(CharsetUtil.UTF_8);
    byte extrasLength = (byte) extras.readableBytes();
    short keyLength = (short) key.length;

    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key, extras);
    request.setOpcode(DCPHandler.OP_OPEN_CONNECTION);
    request.setKeyLength(keyLength);
    request.setExtrasLength(extrasLength);
    request.setTotalBodyLength(keyLength + extrasLength);

    return request;
}

From source file:com.couchbase.client.core.endpoint.dcp.DCPHandler.java

License:Apache License

/**
 * Creates a DCP Stream Request.//from  w  w  w . ja va  2s  .com
 *
 * See the [Protocol Description](https://github.com/couchbaselabs/dcp-documentation/blob/master/documentation/
 * commands/stream-request.md) for more details.
 *
 * @param ctx the channel handler context.
 * @param msg the stream request message.
 * @return a converted {@link BinaryMemcacheRequest}.
 */
private BinaryMemcacheRequest handleStreamRequestRequest(final ChannelHandlerContext ctx,
        final StreamRequestRequest msg) {
    ByteBuf extras = ctx.alloc().buffer(48);
    extras.writeInt(0) // flags
            .writeInt(0) // reserved
            .writeLong(msg.startSequenceNumber()) // start sequence number
            .writeLong(msg.endSequenceNumber()) // end sequence number
            .writeLong(msg.vbucketUUID()) // vbucket UUID
            .writeLong(msg.snapshotStartSequenceNumber()) // snapshot start sequence number
            .writeLong(msg.snapshotEndSequenceNumber()); // snapshot end sequence number

    byte extrasLength = (byte) extras.readableBytes();

    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(extras);
    request.setOpcode(OP_STREAM_REQUEST);
    request.setExtrasLength(extrasLength);
    request.setTotalBodyLength(extrasLength);

    return request;
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java

License:Apache License

/**
 * Encodes a {@link BinaryStoreRequest} into its lower level representation.
 *
 * There are three types of store operations that need to be considered: insert, upsert and replace, which
 * directly translate to the add, set and replace binary memcached opcodes. By convention, only the replace
 * command supports setting a CAS value, even if the others theoretically would do as well (but do not provide
 * benefit in such cases)./*from  www . j  a  v a 2s  .  co m*/
 *
 * Currently, the content is loaded and sent down in one batch, streaming for requests is not supported.
 *
 * @return a ready {@link BinaryMemcacheRequest}.
 */
private static BinaryMemcacheRequest handleStoreRequest(final ChannelHandlerContext ctx,
        final BinaryStoreRequest msg) {
    ByteBuf extras = ctx.alloc().buffer(8);
    extras.writeInt(msg.flags());
    extras.writeInt(msg.expiration());

    byte[] key = msg.keyBytes();
    short keyLength = (short) key.length;
    byte extrasLength = (byte) extras.readableBytes();
    FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, extras, msg.content());

    if (msg instanceof InsertRequest) {
        request.setOpcode(OP_INSERT);
    } else if (msg instanceof UpsertRequest) {
        request.setOpcode(OP_UPSERT);
    } else if (msg instanceof ReplaceRequest) {
        request.setOpcode(OP_REPLACE);
        request.setCAS(((ReplaceRequest) msg).cas());
    } else {
        throw new IllegalArgumentException("Unknown incoming BinaryStoreRequest type " + msg.getClass());
    }

    request.setKeyLength(keyLength);
    request.setTotalBodyLength(keyLength + msg.content().readableBytes() + extrasLength);
    request.setExtrasLength(extrasLength);
    return request;
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java

License:Apache License

/**
 * Encodes a {@link CounterRequest} into its lower level representation.
 *
 * Depending on if the {@link CounterRequest#delta} is positive or negative, either the incr or decr memcached
 * commands are utilized. The value is converted to its absolute variant to conform with the protocol.
 *
 * @return a ready {@link BinaryMemcacheRequest}.
 *///w  w  w.  java 2  s .  c om
private static BinaryMemcacheRequest handleCounterRequest(final ChannelHandlerContext ctx,
        final CounterRequest msg) {
    ByteBuf extras = ctx.alloc().buffer();
    extras.writeLong(Math.abs(msg.delta()));
    extras.writeLong(msg.initial());
    extras.writeInt(msg.expiry());

    byte[] key = msg.keyBytes();
    short keyLength = (short) key.length;
    byte extrasLength = (byte) extras.readableBytes();
    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key, extras);
    request.setOpcode(msg.delta() < 0 ? OP_COUNTER_DECR : OP_COUNTER_INCR);
    request.setKeyLength(keyLength);
    request.setTotalBodyLength(keyLength + extrasLength);
    request.setExtrasLength(extrasLength);
    return request;
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java

License:Apache License

/**
 * Encodes a {@link TouchRequest} into its lower level representation.
 *
 * @return a ready {@link BinaryMemcacheRequest}.
 *//*  w  w w  .j  a  v  a2  s .  c o  m*/
private static BinaryMemcacheRequest handleTouchRequest(final ChannelHandlerContext ctx,
        final TouchRequest msg) {
    ByteBuf extras = ctx.alloc().buffer();
    extras.writeInt(msg.expiry());

    byte[] key = msg.keyBytes();
    short keyLength = (short) key.length;
    byte extrasLength = (byte) extras.readableBytes();
    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key);
    request.setExtras(extras);
    request.setOpcode(OP_TOUCH);
    request.setKeyLength(keyLength);
    request.setTotalBodyLength(keyLength + extrasLength);
    request.setExtrasLength(extrasLength);
    return request;
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java

License:Apache License

private static BinaryMemcacheRequest handleSubdocumentRequest(ChannelHandlerContext ctx,
        BinarySubdocRequest msg) {//from   w w  w .j  a  v a2 s .c o  m
    byte[] key = msg.keyBytes();
    short keyLength = (short) key.length;

    ByteBuf extras = ctx.alloc().buffer(3, 7); //extras can be 7 bytes if there is an expiry
    byte extrasLength = 3; //by default 2 bytes for pathLength + 1 byte for "command" flags
    extras.writeShort(msg.pathLength());

    long cas = 0L;

    if (msg instanceof BinarySubdocMutationRequest) {
        BinarySubdocMutationRequest mut = (BinarySubdocMutationRequest) msg;
        //for now only possible command flag is MKDIR_P (and it makes sense in mutations only)
        if (mut.createIntermediaryPath()) {
            extras.writeByte(0 | SUBDOC_BITMASK_MKDIR_P);
        } else {
            extras.writeByte(0);
        }
        if (mut.expiration() != 0L) {
            extrasLength = 7;
            extras.writeInt(mut.expiration());
        }

        cas = mut.cas();
    } else {
        extras.writeByte(0);
    }

    FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, extras, msg.content());
    request.setOpcode(msg.opcode()).setKeyLength(keyLength).setExtrasLength(extrasLength)
            .setTotalBodyLength(keyLength + msg.content().readableBytes() + extrasLength).setCAS(cas);

    return request;
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java

License:Apache License

private static BinaryMemcacheRequest handleSubdocumentMultiMutationRequest(ChannelHandlerContext ctx,
        BinarySubdocMultiMutationRequest msg) {
    byte[] key = msg.keyBytes();
    short keyLength = (short) key.length;

    byte extrasLength = 0;
    ByteBuf extras = Unpooled.EMPTY_BUFFER;
    if (msg.expiration() != 0L) {
        extrasLength = 4;/*from w ww .  j  a  v  a  2s.  com*/
        extras = ctx.alloc().buffer(4, 4);
        extras.writeInt(msg.expiration());
    }

    FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, extras, msg.content());
    request.setOpcode(OP_SUB_MULTI_MUTATION).setCAS(msg.cas()).setKeyLength(keyLength)
            .setExtrasLength(extrasLength)
            .setTotalBodyLength(keyLength + msg.content().readableBytes() + extrasLength);

    return request;
}