Search in sources :

Example 91 with ByteBuf

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

the class MultiObserveViaCasRequest method encode.

@Override
public ByteBuf encode(ByteBufAllocator alloc, int opaque, KeyValueChannelContext ctx) {
    ByteBuf content = null;
    try {
        content = alloc.buffer(keys.size() * (Short.BYTES * 2));
        for (Map.Entry<byte[], Short> key : keys.entrySet()) {
            ByteBuf keyWithCollection = encodedExternalKeyWithCollection(alloc, ctx, key.getKey());
            try {
                content.writeShort(key.getValue());
                content.writeShort(keyWithCollection.readableBytes());
                content.writeBytes(keyWithCollection);
            } finally {
                ReferenceCountUtil.release(keyWithCollection);
            }
        }
        return request(alloc, MemcacheProtocol.Opcode.OBSERVE_CAS, noDatatype(), partition(), opaque, noCas(), noExtras(), noKey(), content);
    } finally {
        ReferenceCountUtil.release(content);
    }
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) HashMap(java.util.HashMap) Map(java.util.Map)

Example 92 with ByteBuf

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

the class SubdocGetRequest method encode.

@Override
public ByteBuf encode(ByteBufAllocator alloc, int opaque, KeyValueChannelContext ctx) {
    ByteBuf key = null;
    ByteBuf extras = null;
    ByteBuf body = null;
    try {
        if (!ctx.vattrEnabled()) {
            // Do a check to see if all vattr commands meet a whitelist of vattrs.
            for (Command c : commands) {
                if (c.xattr() && (c.path.length() > 0 && c.path.charAt(0) == '$') && !(c.path.startsWith("$document") || c.path.startsWith("$XTOC"))) {
                    throw mapSubDocumentError(this, SubDocumentOpResponseStatus.XATTR_UNKNOWN_VATTR, c.path, c.originalIndex());
                }
            }
        }
        key = encodedKeyWithCollection(alloc, ctx);
        if (flags != 0) {
            extras = alloc.buffer(Byte.BYTES).writeByte(flags);
        }
        if (commands.size() == 1) {
            // Note currently the only subdoc error response handled is ERR_SUBDOC_MULTI_PATH_FAILURE.  Make sure to
            // add the others if do the single lookup optimisation.
            // Update: single subdoc optimization will not be supported.  It adds just 3 bytes to the package size and gives
            // minimal performance gains, in return for additional client complexity.
            body = commands.get(0).encode(alloc);
        } else {
            body = alloc.compositeBuffer(commands.size());
            for (Command command : commands) {
                ByteBuf commandBuffer = command.encode(alloc);
                try {
                    ((CompositeByteBuf) body).addComponent(commandBuffer);
                    body.writerIndex(body.writerIndex() + commandBuffer.readableBytes());
                } catch (Exception ex) {
                    ReferenceCountUtil.release(commandBuffer);
                    throw ex;
                }
            }
        }
        return request(alloc, MemcacheProtocol.Opcode.SUBDOC_MULTI_LOOKUP, noDatatype(), partition(), opaque, noCas(), extras == null ? noExtras() : extras, key, body);
    } finally {
        ReferenceCountUtil.release(key);
        ReferenceCountUtil.release(body);
        ReferenceCountUtil.release(extras);
    }
}
Also used : CompositeByteBuf(com.couchbase.client.core.deps.io.netty.buffer.CompositeByteBuf) 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)

Example 93 with ByteBuf

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

the class PrependRequest method encode.

@Override
public ByteBuf encode(ByteBufAllocator alloc, int opaque, KeyValueChannelContext ctx) {
    ByteBuf key = null;
    ByteBuf content = null;
    ByteBuf flexibleExtras = mutationFlexibleExtras(this, ctx, alloc, syncReplicationType);
    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);
        }
        return MemcacheProtocol.flexibleRequest(alloc, MemcacheProtocol.Opcode.PREPEND, datatype, partition(), opaque, cas, flexibleExtras, noExtras(), key, content);
    } finally {
        ReferenceCountUtil.release(key);
        ReferenceCountUtil.release(content);
        ReferenceCountUtil.release(flexibleExtras);
    }
}
Also used : ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) CompressionConfig(com.couchbase.client.core.env.CompressionConfig)

Example 94 with ByteBuf

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

the class SearchMock method loadSearchTestCase.

/**
 * Given JSON in the form expected, e.g. those from https://github.com/chvck/sdk-testcases which contains the
 * returned JSON from the search service in a field "data", returns the completed SearchResult that the API
 * would return.
 */
public static SearchResult loadSearchTestCase(InputStream json) throws ExecutionException, InterruptedException, IOException {
    // The idea is to fake packets that have come from the search service.
    // Start by preparing the packets.
    JsonObject jo = JsonObject.fromJson(toByteArray(json));
    JsonObject data = jo.getObject("data");
    byte[] b = data.toString().getBytes(StandardCharsets.UTF_8);
    ByteBuf bytes = Unpooled.wrappedBuffer(b);
    HttpContent content = new DefaultLastHttpContent(bytes);
    HttpResponse resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    // Fake some core stuff
    Core mockedCore = mock(Core.class);
    CoreEnvironment env = CoreEnvironment.create();
    CoreContext ctx = new CoreContext(mockedCore, 0, env, PasswordAuthenticator.create("Administrator", "password"));
    // Our ChunkedSearchMessageHandler needs to be initialised by pretending we've sent an outbound SearchRequest
    // through it
    SearchRequest req = new SearchRequest(Duration.ofSeconds(10), ctx, BestEffortRetryStrategy.INSTANCE, null, null, null, null);
    // ChunkedSearchMessageHandler will try to encode() the SearchRequest.  Rather than mocking everything required
    // to get that working, just mock the encode method.
    SearchRequest spiedReq = spy(req);
    doReturn(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "localhost")).when(spiedReq).encode();
    doAnswer(v -> {
        EndpointContext endpointContext = new EndpointContext(ctx, new HostAndPort(null, 0), null, null, null, Optional.of("bucket"), null);
        BaseEndpoint endpoint = mock(BaseEndpoint.class);
        when(endpoint.context()).thenReturn(endpointContext);
        when(endpoint.pipelined()).thenReturn(false);
        // ChunkedSearchMessageHandler does most of the work in handling responses from the service
        ChunkedSearchMessageHandler handler = new ChunkedSearchMessageHandler(endpoint, endpointContext);
        // Netty's EmbeddedChannel lets us test ChannelHandlers like ChunkedSearchMessageHandler.  It's a Netty Channel
        // that doesn't touch the network at all.
        final EmbeddedChannel channel = new EmbeddedChannel(handler);
        // Writing the request is necessary to estabish some initial state inChunkedSearchMessageHandler
        channel.writeAndFlush(spiedReq);
        // Finally we can do the interesting bit of passing our fake FTS service response into
        // ChunkedSearchMessageHandler
        channel.writeInbound(resp);
        channel.writeInbound(content);
        return null;
    }).when(mockedCore).send(any());
    CompletableFuture<SearchResult> future = SearchAccessor.searchQueryAsync(mockedCore, req, DefaultJsonSerializer.create());
    SearchResult result = future.get();
    return result;
}
Also used : SearchRequest(com.couchbase.client.core.msg.search.SearchRequest) CoreContext(com.couchbase.client.core.CoreContext) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) JsonObject(com.couchbase.client.java.json.JsonObject) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) SearchResult(com.couchbase.client.java.search.result.SearchResult) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) HostAndPort(com.couchbase.client.core.util.HostAndPort) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) HttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent) Core(com.couchbase.client.core.Core)

Example 95 with ByteBuf

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

the class ErrorMapLoadingHandlerTest method decodeSuccessfulErrorMap.

/**
 * Verify that when a good error map comes back, we parse and return it properly.
 */
@Test
void decodeSuccessfulErrorMap() {
    ErrorMapLoadingHandler handler = new ErrorMapLoadingHandler(endpointContext);
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(ErrorMapLoadingHandler.class));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    assertFalse(connectFuture.isDone());
    channel.pipeline().fireChannelActive();
    channel.runPendingTasks();
    ByteBuf writtenRequest = channel.readOutbound();
    verifyRequest(writtenRequest, MemcacheProtocol.Opcode.ERROR_MAP.opcode(), false, false, true);
    assertNotNull(channel.pipeline().get(ErrorMapLoadingHandler.class));
    ReferenceCountUtil.release(writtenRequest);
    ByteBuf response = decodeHexDump(readResource("success_errormap_response.txt", ErrorMapLoadingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertTrue(connectFuture.isSuccess());
    assertEquals(1, eventBus.publishedEvents().size());
    ErrorMapLoadedEvent event = (ErrorMapLoadedEvent) eventBus.publishedEvents().get(0);
    assertEquals(Event.Severity.DEBUG, event.severity());
    Optional<ErrorMap> maybeMap = event.errorMap();
    assertTrue(maybeMap.isPresent());
    assertNotNull(maybeMap.get());
    ErrorMap errorMap = channel.attr(ChannelAttributes.ERROR_MAP_KEY).get();
    assertEquals(errorMap, maybeMap.get());
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) ErrorMapLoadedEvent(com.couchbase.client.core.cnc.events.io.ErrorMapLoadedEvent) Test(org.junit.jupiter.api.Test)

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