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);
}
}
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);
}
}
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);
}
}
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;
}
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());
}
Aggregations