Search in sources :

Example 26 with ByteBuf

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

the class CompressionTest method doesNotCompressIfTooShortUpsert.

@Test
void doesNotCompressIfTooShortUpsert() {
    UpsertRequest request = new UpsertRequest(key, shortContent, expiry, preserveExpiry, flags, timeout, coreContext, cid, retryStrategy, Optional.empty(), null);
    ByteBuf encoded = request.encode(allocator, 0, ctx(true));
    assertEquals(0, datatype(encoded));
    assertEquals(Unpooled.wrappedBuffer(shortContent), body(encoded).get());
    ReferenceCountUtil.release(encoded);
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 27 with ByteBuf

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

the class GetCollectionIdRequest method encode.

@Override
public ByteBuf encode(ByteBufAllocator alloc, int opaque, KeyValueChannelContext ctx) {
    ByteBuf body = null;
    try {
        CollectionIdentifier ci = collectionIdentifier();
        if (!ci.collection().isPresent()) {
            throw InvalidArgumentException.fromMessage("A collection name needs to be present");
        }
        // Note that the scope can be empty, according to spec it is the same as _default scope
        body = Unpooled.copiedBuffer(ci.scope().orElse("") + "." + ci.collection().get(), UTF_8);
        return request(alloc, MemcacheProtocol.Opcode.COLLECTIONS_GET_CID, noDatatype(), noPartition(), opaque, noCas(), noExtras(), noKey(), body);
    } finally {
        ReferenceCountUtil.release(body);
    }
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier)

Example 28 with ByteBuf

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

the class GetCollectionManifestRequest method decode.

@Override
public GetCollectionManifestResponse decode(final ByteBuf response, final KeyValueChannelContext ctx) {
    ResponseStatus status = MemcacheProtocol.decodeStatus(response);
    Optional<String> manifest = Optional.empty();
    if (status.success()) {
        manifest = Optional.of(body(response).map(ByteBufUtil::getBytes).orElse(Bytes.EMPTY_BYTE_ARRAY)).map(b -> new String(b, StandardCharsets.UTF_8));
    }
    return new GetCollectionManifestResponse(status, manifest);
}
Also used : ByteBufUtil(com.couchbase.client.core.deps.io.netty.buffer.ByteBufUtil) MemcacheProtocol.noKey(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.noKey) ByteBufAllocator(com.couchbase.client.core.deps.io.netty.buffer.ByteBufAllocator) MemcacheProtocol.noDatatype(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.noDatatype) Bytes(com.couchbase.client.core.util.Bytes) MemcacheProtocol.noExtras(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.noExtras) StandardCharsets(java.nio.charset.StandardCharsets) MemcacheProtocol.noCas(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.noCas) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus) KeyValueChannelContext(com.couchbase.client.core.io.netty.kv.KeyValueChannelContext) ByteBufUtil(com.couchbase.client.core.deps.io.netty.buffer.ByteBufUtil) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) MemcacheProtocol.noBody(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.noBody) Optional(java.util.Optional) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) MemcacheProtocol(com.couchbase.client.core.io.netty.kv.MemcacheProtocol) MemcacheProtocol.noPartition(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.noPartition) MemcacheProtocol.body(com.couchbase.client.core.io.netty.kv.MemcacheProtocol.body) ResponseStatus(com.couchbase.client.core.msg.ResponseStatus)

Example 29 with ByteBuf

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

the class IncrementRequest method encode.

@Override
public ByteBuf encode(ByteBufAllocator alloc, int opaque, KeyValueChannelContext ctx) {
    ByteBuf key = null;
    ByteBuf extras = null;
    ByteBuf flexibleExtras = mutationFlexibleExtras(this, ctx, alloc, syncReplicationType);
    try {
        key = encodedKeyWithCollection(alloc, ctx);
        extras = alloc.buffer((Long.BYTES * 2) + Integer.BYTES);
        extras.writeLong(delta);
        if (initial.isPresent()) {
            extras.writeLong(initial.get());
            extras.writeInt((int) expiry);
        } else {
            // no initial present, will lead to doc not found
            extras.writeLong(0);
            extras.writeInt(COUNTER_NOT_EXISTS_EXPIRY);
        }
        return MemcacheProtocol.flexibleRequest(alloc, MemcacheProtocol.Opcode.INCREMENT, noDatatype(), partition(), opaque, noCas(), flexibleExtras, extras, key, noBody());
    } finally {
        ReferenceCountUtil.release(key);
        ReferenceCountUtil.release(extras);
        ReferenceCountUtil.release(flexibleExtras);
    }
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Example 30 with ByteBuf

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

the class SubdocMutateRequest method encode.

@Override
public ByteBuf encode(ByteBufAllocator alloc, int opaque, KeyValueChannelContext ctx) {
    ByteBuf key = null;
    ByteBuf extras = null;
    ByteBuf content = null;
    ByteBuf flexibleExtras = mutationFlexibleExtras(this, ctx, alloc, syncReplicationType, preserveExpiry);
    try {
        if (createAsDeleted && !ctx.createAsDeleted()) {
            // It is left purely as an additional safety measure.
            throw new FeatureNotAvailableException("Cannot use createAsDeleted Sub-Document flag, as it is not supported by this version of the cluster");
        }
        key = encodedKeyWithCollection(alloc, ctx);
        extras = alloc.buffer();
        if (expiration != 0) {
            extras.writeInt((int) expiration);
        }
        if (flags != 0) {
            extras.writeByte(flags);
        }
        if (commands.size() == 1) {
            content = commands.get(0).encode(alloc);
        } else {
            content = alloc.compositeBuffer(commands.size());
            for (Command command : commands) {
                ByteBuf commandBuffer = command.encode(alloc);
                try {
                    ((CompositeByteBuf) content).addComponent(commandBuffer);
                    content.writerIndex(content.writerIndex() + commandBuffer.readableBytes());
                } catch (Exception ex) {
                    ReferenceCountUtil.release(commandBuffer);
                    throw ex;
                }
            }
        }
        return flexibleRequest(alloc, Opcode.SUBDOC_MULTI_MUTATE, noDatatype(), partition(), opaque, cas, flexibleExtras, extras, key, content);
    } finally {
        ReferenceCountUtil.release(key);
        ReferenceCountUtil.release(extras);
        ReferenceCountUtil.release(flexibleExtras);
        ReferenceCountUtil.release(content);
    }
}
Also used : CompositeByteBuf(com.couchbase.client.core.deps.io.netty.buffer.CompositeByteBuf) FeatureNotAvailableException(com.couchbase.client.core.error.FeatureNotAvailableException) CompositeByteBuf(com.couchbase.client.core.deps.io.netty.buffer.CompositeByteBuf) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) DocumentTooDeepException(com.couchbase.client.core.error.subdoc.DocumentTooDeepException) XattrInvalidKeyComboException(com.couchbase.client.core.error.subdoc.XattrInvalidKeyComboException) DocumentNotJsonException(com.couchbase.client.core.error.subdoc.DocumentNotJsonException) FeatureNotAvailableException(com.couchbase.client.core.error.FeatureNotAvailableException) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) DocumentAlreadyAliveException(com.couchbase.client.core.error.subdoc.DocumentAlreadyAliveException)

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