}
@Override
protected CouchbaseResponse decodeResponse(final ChannelHandlerContext ctx, final FullBinaryMemcacheResponse msg)
throws Exception {
BinaryRequest request = currentRequest();
ResponseStatus status = convertStatus(msg.getStatus());
CouchbaseResponse response;
ByteBuf content = msg.content().copy();
long cas = msg.getCAS();
String bucket = request.bucket();
if (request instanceof GetRequest || request instanceof ReplicaGetRequest) {
int flags = 0;
if (msg.getExtrasLength() > 0) {
final ByteBuf extrasReleased = msg.getExtras();
final ByteBuf extras = ctx.alloc().buffer(msg.getExtrasLength());
extras.writeBytes(extrasReleased, extrasReleased.readerIndex(), extrasReleased.readableBytes());
flags = extras.getInt(0);
extras.release();
}
response = new GetResponse(status, cas, flags, bucket, content, request);
} else if (request instanceof GetBucketConfigRequest) {
response = new GetBucketConfigResponse(status, bucket, content,
((GetBucketConfigRequest) request).hostname());
} else if (request instanceof InsertRequest) {
response = new InsertResponse(status, cas, bucket, content, request);
} else if (request instanceof UpsertRequest) {
response = new UpsertResponse(status, cas, bucket, content, request);
} else if (request instanceof ReplaceRequest) {
response = new ReplaceResponse(status, cas, bucket, content, request);
} else if (request instanceof RemoveRequest) {
response = new RemoveResponse(status, bucket, content, request);
} else if (request instanceof CounterRequest) {
response = new CounterResponse(status, bucket, msg.content().readLong(), cas, request);
} else if (request instanceof UnlockRequest) {
response = new UnlockResponse(status, bucket, content, request);
} else if (request instanceof TouchRequest) {
response = new TouchResponse(status, bucket, content, request);
} else if (request instanceof ObserveRequest) {
byte observed = content.getByte(content.getShort(2) + 4);
response = new ObserveResponse(status, observed, ((ObserveRequest) request).master(), bucket,
content, request);
} else if (request instanceof AppendRequest) {
response = new AppendResponse(status, cas, bucket, content, request);
} else if (request instanceof PrependRequest) {
response = new PrependResponse(status, cas, bucket, content, request);
} else {
throw new IllegalStateException("Unhandled request/response pair: " + request.getClass() + "/"
+ msg.getClass());
}
return response;
}