List of usage examples for io.netty.buffer Unpooled EMPTY_BUFFER
ByteBuf EMPTY_BUFFER
To view the source code for io.netty.buffer Unpooled EMPTY_BUFFER.
Click Source Link
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;/*w w w .ja v a2s. c o m*/ 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; }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java
License:Apache License
/** * Helper method to decode all simple subdocument response messages. * * @param request the current request./*from w ww. j a v a 2s . c o m*/ * @param msg the current response message. * @param ctx the handler context. * @param status the response status code. * @return the decoded response or null if none did match. */ private static CouchbaseResponse handleSubdocumentResponseMessages(BinaryRequest request, FullBinaryMemcacheResponse msg, ChannelHandlerContext ctx, ResponseStatus status, boolean seqOnMutation) { if (!(request instanceof BinarySubdocRequest)) return null; BinarySubdocRequest subdocRequest = (BinarySubdocRequest) request; long cas = msg.getCAS(); short statusCode = msg.getStatus(); String bucket = request.bucket(); MutationToken mutationToken = null; if (msg.getExtrasLength() > 0) { mutationToken = extractToken(bucket, seqOnMutation, status.isSuccess(), msg.getExtras(), request.partition()); } ByteBuf fragment; if (msg.content() != null && msg.content().readableBytes() > 0) { fragment = msg.content(); } else if (msg.content() != null) { msg.content().release(); fragment = Unpooled.EMPTY_BUFFER; } else { fragment = Unpooled.EMPTY_BUFFER; } return new SimpleSubdocResponse(status, statusCode, bucket, fragment, subdocRequest, cas, mutationToken); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java
License:Apache License
/** * Helper method to decode all multi mutation response messages. * * @param request the current request./* www . j av a2s. co m*/ * @param msg the current response message. * @param ctx the handler context. * @param status the response status code. * @return the decoded response or null if it wasn't a subdocument multi lookup. */ private static CouchbaseResponse handleSubdocumentMultiMutationResponseMessages(BinaryRequest request, FullBinaryMemcacheResponse msg, ChannelHandlerContext ctx, ResponseStatus status, boolean seqOnMutation) { if (!(request instanceof BinarySubdocMultiMutationRequest)) return null; BinarySubdocMultiMutationRequest subdocRequest = (BinarySubdocMultiMutationRequest) request; long cas = msg.getCAS(); short statusCode = msg.getStatus(); String bucket = request.bucket(); MutationToken mutationToken = null; if (msg.getExtrasLength() > 0) { mutationToken = extractToken(bucket, seqOnMutation, status.isSuccess(), msg.getExtras(), request.partition()); } MultiMutationResponse response; ByteBuf body = msg.content(); List<MultiResult<Mutation>> responses; if (status.isSuccess()) { List<MutationCommand> commands = subdocRequest.commands(); responses = new ArrayList<MultiResult<Mutation>>(commands.size()); //MB-17842: Mutations can have a value, so there could be individual results //but only mutation commands that provide a value will have an explicit result in the binary response. //However, we still want MutationResult for all of the commands ListIterator<MutationCommand> it = commands.listIterator(); int explicitResultSize = 0; //as long as there is an explicit response to read... while (msg.content().readableBytes() >= 7) { explicitResultSize++; //...read the data byte responseIndex = body.readByte(); short responseStatus = body.readShort(); //will this always be SUCCESS? int responseLength = body.readInt(); ByteBuf responseValue; if (responseLength > 0) { responseValue = ctx.alloc().buffer(responseLength, responseLength); responseValue.writeBytes(body, responseLength); } else { responseValue = Unpooled.EMPTY_BUFFER; //can an explicit response be 0-length (empty)? } //...sanity check response so subsequent loop don't run forever if (it.nextIndex() > responseIndex) { body.release(); throw new IllegalStateException("Unable to interpret multi mutation response, responseIndex = " + responseIndex + " while next available command was #" + it.nextIndex()); } ///...catch up on all commands before current one that didn't get an explicit response while (it.nextIndex() < responseIndex) { MutationCommand noResultCommand = it.next(); responses.add(MultiResult.create(KeyValueStatus.SUCCESS.code(), ResponseStatus.SUCCESS, noResultCommand.path(), noResultCommand.mutation(), Unpooled.EMPTY_BUFFER)); } //...then process the one that did get an explicit response MutationCommand cmd = it.next(); responses.add(MultiResult.create(responseStatus, ResponseStatusConverter.fromBinary(responseStatus), cmd.path(), cmd.mutation(), responseValue)); } //...and finally the remainder of commands after the last one that got an explicit response: while (it.hasNext()) { MutationCommand noResultCommand = it.next(); responses.add(MultiResult.create(KeyValueStatus.SUCCESS.code(), ResponseStatus.SUCCESS, noResultCommand.path(), noResultCommand.mutation(), Unpooled.EMPTY_BUFFER)); } if (responses.size() != commands.size()) { body.release(); throw new IllegalStateException( "Multi mutation spec size and result size differ: " + commands.size() + " vs " + responses.size() + ", including " + explicitResultSize + " explicit results"); } response = new MultiMutationResponse(bucket, subdocRequest, cas, mutationToken, responses); } else if (ResponseStatus.SUBDOC_MULTI_PATH_FAILURE.equals(status)) { //MB-17842: order of index and status has been swapped byte firstErrorIndex = body.readByte(); short firstErrorCode = body.readShort(); response = new MultiMutationResponse(status, statusCode, bucket, firstErrorIndex, firstErrorCode, subdocRequest, cas, mutationToken); } else { response = new MultiMutationResponse(status, statusCode, bucket, subdocRequest, cas, mutationToken); } body.release(); return response; }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldDecodeSuccessfulGet() { ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER, content.copy());/*from w ww . j a v a2 s . c o m*/ response.setCAS(123456789L); response.setExtras(Unpooled.buffer().writeInt(123)); response.setExtrasLength((byte) 4); GetRequest requestMock = mock(GetRequest.class); when(requestMock.bucket()).thenReturn(BUCKET); requestQueue.add(requestMock); channel.writeInbound(response); assertEquals(1, eventSink.responseEvents().size()); GetResponse event = (GetResponse) eventSink.responseEvents().get(0).getMessage(); assertEquals(123456789L, event.cas()); assertEquals(123, event.flags()); assertEquals("content", event.content().toString(CHARSET)); assertEquals(BUCKET, event.bucket()); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldDecodeNotFoundGet() { ByteBuf content = Unpooled.copiedBuffer("Not Found", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER, content.copy());//from w w w . ja v a 2 s . c om response.setStatus(BinaryMemcacheResponseStatus.KEY_ENOENT); GetRequest requestMock = mock(GetRequest.class); when(requestMock.bucket()).thenReturn(BUCKET); requestQueue.add(requestMock); channel.writeInbound(response); assertEquals(1, eventSink.responseEvents().size()); GetResponse event = (GetResponse) eventSink.responseEvents().get(0).getMessage(); assertEquals(0, event.cas()); assertEquals(0, event.flags()); assertEquals("Not Found", event.content().toString(CHARSET)); assertEquals(BUCKET, event.bucket()); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldDecodeReplicaGetResponse() { ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER, content.copy());//from w w w . j av a 2 s . c o m response.setCAS(123456789L); response.setExtras(Unpooled.buffer().writeInt(123)); response.setExtrasLength((byte) 4); ReplicaGetRequest requestMock = mock(ReplicaGetRequest.class); when(requestMock.bucket()).thenReturn(BUCKET); requestQueue.add(requestMock); channel.writeInbound(response); assertEquals(1, eventSink.responseEvents().size()); GetResponse event = (GetResponse) eventSink.responseEvents().get(0).getMessage(); assertEquals(123456789L, event.cas()); assertEquals(123, event.flags()); assertEquals("content", event.content().toString(CHARSET)); assertEquals(BUCKET, event.bucket()); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldDecodeGetBucketConfigResponse() throws Exception { ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER, content.copy());/*w w w .j av a2s. c o m*/ GetBucketConfigRequest requestMock = mock(GetBucketConfigRequest.class); when(requestMock.bucket()).thenReturn("bucket"); when(requestMock.hostname()).thenReturn(InetAddress.getLocalHost()); requestQueue.add(requestMock); channel.writeInbound(response); assertEquals(1, eventSink.responseEvents().size()); GetBucketConfigResponse event = (GetBucketConfigResponse) eventSink.responseEvents().get(0).getMessage(); assertEquals(BUCKET, event.bucket()); assertEquals(ResponseStatus.SUCCESS, event.status()); assertEquals(InetAddress.getLocalHost(), event.hostname()); assertEquals("content", event.content().toString(CHARSET)); ReferenceCountUtil.release(content); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldDecodeObserveResponseDuringRebalance() throws Exception { ByteBuf content = Unpooled.copiedBuffer("{someconfig...}", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(new byte[] {}, Unpooled.EMPTY_BUFFER, content.copy()); response.setStatus(KeyValueStatus.ERR_NOT_MY_VBUCKET.code()); ObserveRequest requestMock = mock(ObserveRequest.class); requestQueue.add(requestMock);//from ww w .j a v a 2 s.co m channel.writeInbound(response); assertEquals(1, eventSink.responseEvents().size()); ObserveResponse event = (ObserveResponse) eventSink.responseEvents().get(0).getMessage(); assertEquals(ResponseStatus.RETRY, event.status()); assertEquals(ObserveResponse.ObserveStatus.UNKNOWN, event.observeStatus()); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldDecodeCounterResponseWhenNotSuccessful() throws Exception { FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(new byte[] {}, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER); response.setStatus(BinaryMemcacheResponseStatus.DELTA_BADVAL); CounterRequest requestMock = mock(CounterRequest.class); requestQueue.add(requestMock);/*from w w w . jav a2 s. c o m*/ channel.writeInbound(response); assertEquals(1, eventSink.responseEvents().size()); CounterResponse event = (CounterResponse) eventSink.responseEvents().get(0).getMessage(); assertEquals(ResponseStatus.INVALID_ARGUMENTS, event.status()); assertEquals(0, event.value()); }
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldReleaseStoreRequestContentOnSuccess() throws Exception { ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER, content);// ww w . j a v a 2s.c o m response.setStatus(BinaryMemcacheResponseStatus.SUCCESS); UpsertRequest requestMock = mock(UpsertRequest.class); ByteBuf requestContent = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8); when(requestMock.bucket()).thenReturn("bucket"); when(requestMock.observable()).thenReturn(AsyncSubject.<CouchbaseResponse>create()); when(requestMock.content()).thenReturn(requestContent); requestQueue.add(requestMock); assertEquals(1, content.refCnt()); assertEquals(1, requestContent.refCnt()); channel.writeInbound(response); assertEquals(1, content.refCnt()); assertEquals(0, requestContent.refCnt()); }