use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class TouchRequest method encode.
@Override
public ByteBuf encode(ByteBufAllocator alloc, int opaque, KeyValueChannelContext ctx) {
ByteBuf key = null;
ByteBuf extras = null;
try {
key = encodedKeyWithCollection(alloc, ctx);
extras = alloc.buffer(Integer.BYTES);
extras.writeInt((int) expiry);
return MemcacheProtocol.request(alloc, MemcacheProtocol.Opcode.TOUCH, noDatatype(), partition(), opaque, noCas(), extras, key, noBody());
} finally {
ReferenceCountUtil.release(key);
ReferenceCountUtil.release(extras);
}
}
use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class UpsertRequest method encode.
@Override
public ByteBuf encode(ByteBufAllocator alloc, int opaque, KeyValueChannelContext ctx) {
ByteBuf key = null;
ByteBuf content = null;
ByteBuf extras = null;
ByteBuf flexibleExtras = mutationFlexibleExtras(this, ctx, alloc, syncReplicationType, preserveExpiry);
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);
}
extras = alloc.buffer(Integer.BYTES * 2);
extras.writeInt(flags);
extras.writeInt((int) expiration);
return MemcacheProtocol.flexibleRequest(alloc, MemcacheProtocol.Opcode.SET, datatype, partition(), opaque, noCas(), flexibleExtras, extras, key, content);
} finally {
ReferenceCountUtil.release(key);
ReferenceCountUtil.release(extras);
ReferenceCountUtil.release(flexibleExtras);
ReferenceCountUtil.release(content);
}
}
use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class ReplaceRequest method encode.
@Override
public ByteBuf encode(ByteBufAllocator alloc, int opaque, KeyValueChannelContext ctx) {
ByteBuf key = null;
ByteBuf content = null;
ByteBuf extras = null;
ByteBuf flexibleExtras = mutationFlexibleExtras(this, ctx, alloc, syncReplicationType, preserveExpiry);
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);
}
extras = alloc.buffer(Integer.BYTES * 2);
extras.writeInt(flags);
extras.writeInt((int) expiration);
return MemcacheProtocol.flexibleRequest(alloc, MemcacheProtocol.Opcode.REPLACE, datatype, partition(), opaque, cas, flexibleExtras, extras, key, content);
} finally {
ReferenceCountUtil.release(key);
ReferenceCountUtil.release(extras);
ReferenceCountUtil.release(flexibleExtras);
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 decode.
@Override
public SubdocGetResponse decode(final ByteBuf response, KeyValueChannelContext ctx) {
Optional<ByteBuf> maybeBody = body(response);
SubDocumentField[] values;
List<CouchbaseException> errors = null;
if (maybeBody.isPresent()) {
ByteBuf body = maybeBody.get();
values = new SubDocumentField[commands.size()];
for (Command command : commands) {
short statusRaw = body.readShort();
SubDocumentOpResponseStatus status = decodeSubDocumentStatus(statusRaw);
Optional<CouchbaseException> error = Optional.empty();
if (status != SubDocumentOpResponseStatus.SUCCESS) {
if (errors == null)
errors = new ArrayList<>();
CouchbaseException err = mapSubDocumentError(this, status, command.path, command.originalIndex());
errors.add(err);
error = Optional.of(err);
}
int valueLength = body.readInt();
byte[] value = new byte[valueLength];
body.readBytes(value, 0, valueLength);
SubDocumentField op = new SubDocumentField(status, error, value, command.path, command.type);
values[command.originalIndex] = op;
}
} else {
values = new SubDocumentField[0];
}
short rawStatus = status(response);
ResponseStatus status = decodeStatus(response);
boolean isDeleted = rawStatus == Status.SUBDOC_MULTI_PATH_FAILURE_DELETED.status() || rawStatus == Status.SUBDOC_SUCCESS_DELETED_DOCUMENT.status();
Optional<CouchbaseException> error = Optional.empty();
// Note that we send all subdoc requests as multi currently so always get this back on error
if (rawStatus == Status.SUBDOC_MULTI_PATH_FAILURE.status() || rawStatus == Status.SUBDOC_MULTI_PATH_FAILURE_DELETED.status()) {
// Special case logic for CMD_EXISTS
if (commands.size() == 1 && commands.get(0).type == SubdocCommandType.EXISTS) {
status = ResponseStatus.SUCCESS;
} else // If a single subdoc op was tried and failed, return that directly
if (commands.size() == 1 && errors != null && errors.size() == 1) {
error = Optional.of(errors.get(0));
} else {
// Otherwise return success, as some of the operations have succeeded
status = ResponseStatus.SUCCESS;
}
}
// Handle any document-level failures here
if (rawStatus == Status.SUBDOC_DOC_NOT_JSON.status()) {
SubDocumentErrorContext e = createSubDocumentExceptionContext(SubDocumentOpResponseStatus.DOC_NOT_JSON);
error = Optional.of(new DocumentNotJsonException(e));
} else if (rawStatus == Status.SUBDOC_DOC_TOO_DEEP.status()) {
SubDocumentErrorContext e = createSubDocumentExceptionContext(SubDocumentOpResponseStatus.DOC_TOO_DEEP);
error = Optional.of(new DocumentTooDeepException(e));
} else if (rawStatus == Status.SUBDOC_XATTR_INVALID_KEY_COMBO.status()) {
SubDocumentErrorContext e = createSubDocumentExceptionContext(SubDocumentOpResponseStatus.XATTR_INVALID_KEY_COMBO);
error = Optional.of(new XattrInvalidKeyComboException(e));
}
// Do not handle SUBDOC_INVALID_COMBO here, it indicates a client-side bug
return new SubdocGetResponse(status, error, values, cas(response), isDeleted);
}
use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf in project couchbase-jvm-clients by couchbase.
the class ObserveViaCasRequest method decode.
@Override
public ObserveViaCasResponse decode(final ByteBuf response, KeyValueChannelContext ctx) {
ResponseStatus status = decodeStatus(response);
byte observed = ObserveViaCasResponse.ObserveStatus.UNKNOWN.value();
long observedCas = 0;
ResponseStatusDetails statusDetails = null;
if (status.success()) {
ByteBuf content = body(response).get();
short keyLength = content.getShort(2);
observed = content.getByte(keyLength + 4);
observedCas = content.getLong(keyLength + 5);
} else {
// TODO: implement once xerror is fully implemented
// ResponseStatus.convertDetails(datatype(response), );
statusDetails = null;
}
return new ObserveViaCasResponse(status, observedCas, ObserveViaCasResponse.ObserveStatus.valueOf(observed), active, statusDetails);
}
Aggregations