Search in sources :

Example 31 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class TouchRequest method encode.

@Override
public ByteBuf encode(ByteBufAllocator alloc, int opaque, KeyValueChannelContext ctx) {
    ByteBuf key = null;
    ByteBuf extras = null;
    try {
        key = encodedKeyWithCollection(alloc, ctx);
        extras = alloc.buffer(Integer.BYTES);
        extras.writeInt((int) expiry);
        return MemcacheProtocol.request(alloc, MemcacheProtocol.Opcode.TOUCH, noDatatype(), partition(), opaque, noCas(), extras, key, noBody());
    } finally {
        ReferenceCountUtil.release(key);
        ReferenceCountUtil.release(extras);
    }
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Example 32 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class UpsertRequest method encode.

@Override
public ByteBuf encode(ByteBufAllocator alloc, int opaque, KeyValueChannelContext ctx) {
    ByteBuf key = null;
    ByteBuf content = null;
    ByteBuf extras = null;
    ByteBuf flexibleExtras = mutationFlexibleExtras(this, ctx, alloc, syncReplicationType, preserveExpiry);
    try {
        key = encodedKeyWithCollection(alloc, ctx);
        byte datatype = 0;
        CompressionConfig config = ctx.compressionConfig();
        if (config != null && config.enabled() && this.content.length >= config.minSize()) {
            ByteBuf maybeCompressed = MemcacheProtocol.tryCompression(this.content, config.minRatio());
            if (maybeCompressed != null) {
                datatype |= MemcacheProtocol.Datatype.SNAPPY.datatype();
                content = maybeCompressed;
            } else {
                content = Unpooled.wrappedBuffer(this.content);
            }
        } else {
            content = Unpooled.wrappedBuffer(this.content);
        }
        extras = alloc.buffer(Integer.BYTES * 2);
        extras.writeInt(flags);
        extras.writeInt((int) expiration);
        return MemcacheProtocol.flexibleRequest(alloc, MemcacheProtocol.Opcode.SET, datatype, partition(), opaque, noCas(), flexibleExtras, extras, key, content);
    } finally {
        ReferenceCountUtil.release(key);
        ReferenceCountUtil.release(extras);
        ReferenceCountUtil.release(flexibleExtras);
        ReferenceCountUtil.release(content);
    }
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) CompressionConfig(com.couchbase.client.core.env.CompressionConfig)

Example 33 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class ReplaceRequest method encode.

@Override
public ByteBuf encode(ByteBufAllocator alloc, int opaque, KeyValueChannelContext ctx) {
    ByteBuf key = null;
    ByteBuf content = null;
    ByteBuf extras = null;
    ByteBuf flexibleExtras = mutationFlexibleExtras(this, ctx, alloc, syncReplicationType, preserveExpiry);
    try {
        key = encodedKeyWithCollection(alloc, ctx);
        byte datatype = 0;
        CompressionConfig config = ctx.compressionConfig();
        if (config != null && config.enabled() && this.content.length >= config.minSize()) {
            ByteBuf maybeCompressed = MemcacheProtocol.tryCompression(this.content, config.minRatio());
            if (maybeCompressed != null) {
                datatype |= MemcacheProtocol.Datatype.SNAPPY.datatype();
                content = maybeCompressed;
            } else {
                content = Unpooled.wrappedBuffer(this.content);
            }
        } else {
            content = Unpooled.wrappedBuffer(this.content);
        }
        extras = alloc.buffer(Integer.BYTES * 2);
        extras.writeInt(flags);
        extras.writeInt((int) expiration);
        return MemcacheProtocol.flexibleRequest(alloc, MemcacheProtocol.Opcode.REPLACE, datatype, partition(), opaque, cas, flexibleExtras, extras, key, content);
    } finally {
        ReferenceCountUtil.release(key);
        ReferenceCountUtil.release(extras);
        ReferenceCountUtil.release(flexibleExtras);
        ReferenceCountUtil.release(content);
    }
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) CompressionConfig(com.couchbase.client.core.env.CompressionConfig)

Example 34 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class SubdocGetRequest method decode.

@Override
public SubdocGetResponse decode(final ByteBuf response, KeyValueChannelContext ctx) {
    Optional<ByteBuf> maybeBody = body(response);
    SubDocumentField[] values;
    List<CouchbaseException> errors = null;
    if (maybeBody.isPresent()) {
        ByteBuf body = maybeBody.get();
        values = new SubDocumentField[commands.size()];
        for (Command command : commands) {
            short statusRaw = body.readShort();
            SubDocumentOpResponseStatus status = decodeSubDocumentStatus(statusRaw);
            Optional<CouchbaseException> error = Optional.empty();
            if (status != SubDocumentOpResponseStatus.SUCCESS) {
                if (errors == null)
                    errors = new ArrayList<>();
                CouchbaseException err = mapSubDocumentError(this, status, command.path, command.originalIndex());
                errors.add(err);
                error = Optional.of(err);
            }
            int valueLength = body.readInt();
            byte[] value = new byte[valueLength];
            body.readBytes(value, 0, valueLength);
            SubDocumentField op = new SubDocumentField(status, error, value, command.path, command.type);
            values[command.originalIndex] = op;
        }
    } else {
        values = new SubDocumentField[0];
    }
    short rawStatus = status(response);
    ResponseStatus status = decodeStatus(response);
    boolean isDeleted = rawStatus == Status.SUBDOC_MULTI_PATH_FAILURE_DELETED.status() || rawStatus == Status.SUBDOC_SUCCESS_DELETED_DOCUMENT.status();
    Optional<CouchbaseException> error = Optional.empty();
    // Note that we send all subdoc requests as multi currently so always get this back on error
    if (rawStatus == Status.SUBDOC_MULTI_PATH_FAILURE.status() || rawStatus == Status.SUBDOC_MULTI_PATH_FAILURE_DELETED.status()) {
        // Special case logic for CMD_EXISTS
        if (commands.size() == 1 && commands.get(0).type == SubdocCommandType.EXISTS) {
            status = ResponseStatus.SUCCESS;
        } else // If a single subdoc op was tried and failed, return that directly
        if (commands.size() == 1 && errors != null && errors.size() == 1) {
            error = Optional.of(errors.get(0));
        } else {
            // Otherwise return success, as some of the operations have succeeded
            status = ResponseStatus.SUCCESS;
        }
    }
    // Handle any document-level failures here
    if (rawStatus == Status.SUBDOC_DOC_NOT_JSON.status()) {
        SubDocumentErrorContext e = createSubDocumentExceptionContext(SubDocumentOpResponseStatus.DOC_NOT_JSON);
        error = Optional.of(new DocumentNotJsonException(e));
    } else if (rawStatus == Status.SUBDOC_DOC_TOO_DEEP.status()) {
        SubDocumentErrorContext e = createSubDocumentExceptionContext(SubDocumentOpResponseStatus.DOC_TOO_DEEP);
        error = Optional.of(new DocumentTooDeepException(e));
    } else if (rawStatus == Status.SUBDOC_XATTR_INVALID_KEY_COMBO.status()) {
        SubDocumentErrorContext e = createSubDocumentExceptionContext(SubDocumentOpResponseStatus.XATTR_INVALID_KEY_COMBO);
        error = Optional.of(new XattrInvalidKeyComboException(e));
    }
    // Do not handle SUBDOC_INVALID_COMBO here, it indicates a client-side bug
    return new SubdocGetResponse(status, error, values, cas(response), isDeleted);
}
Also used : DocumentNotJsonException(com.couchbase.client.core.error.subdoc.DocumentNotJsonException) ArrayList(java.util.ArrayList) CompositeByteBuf(com.couchbase.client.core.deps.io.netty.buffer.CompositeByteBuf) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) SubDocumentErrorContext(com.couchbase.client.core.error.context.SubDocumentErrorContext) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) DocumentTooDeepException(com.couchbase.client.core.error.subdoc.DocumentTooDeepException) XattrInvalidKeyComboException(com.couchbase.client.core.error.subdoc.XattrInvalidKeyComboException)

Example 35 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.

the class ObserveViaCasRequest method decode.

@Override
public ObserveViaCasResponse decode(final ByteBuf response, KeyValueChannelContext ctx) {
    ResponseStatus status = decodeStatus(response);
    byte observed = ObserveViaCasResponse.ObserveStatus.UNKNOWN.value();
    long observedCas = 0;
    ResponseStatusDetails statusDetails = null;
    if (status.success()) {
        ByteBuf content = body(response).get();
        short keyLength = content.getShort(2);
        observed = content.getByte(keyLength + 4);
        observedCas = content.getLong(keyLength + 5);
    } else {
        // TODO: implement once xerror is fully implemented
        // ResponseStatus.convertDetails(datatype(response), );
        statusDetails = null;
    }
    return new ObserveViaCasResponse(status, observedCas, ObserveViaCasResponse.ObserveStatus.valueOf(observed), active, statusDetails);
}
Also used : ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Aggregations

ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)107 Test (org.junit.jupiter.api.Test)54 InetSocketAddress (java.net.InetSocketAddress)17 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)12 ChannelFuture (com.couchbase.client.core.deps.io.netty.channel.ChannelFuture)11 Duration (java.time.Duration)10 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)9 CoreContext (com.couchbase.client.core.CoreContext)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 CompressionConfig (com.couchbase.client.core.env.CompressionConfig)6 ResponseStatus (com.couchbase.client.core.msg.ResponseStatus)6 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)6 ChannelHandlerContext (com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext)5 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)5 ArrayList (java.util.ArrayList)5 DurabilityTimeoutCoercedEvent (com.couchbase.client.core.cnc.events.io.DurabilityTimeoutCoercedEvent)4 FeaturesNegotiatedEvent (com.couchbase.client.core.cnc.events.io.FeaturesNegotiatedEvent)4 CompositeByteBuf (com.couchbase.client.core.deps.io.netty.buffer.CompositeByteBuf)4 FullHttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest)4 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)4