use of com.couchbase.client.java.kv.MutateInSpec in project kafka-connect-couchbase by couchbase.
the class SubDocumentSinkHandler method handle.
@Override
public SinkAction handle(SinkHandlerParams params) {
String documentId = getDocumentId(params);
SinkDocument doc = params.document().orElse(null);
if (doc == null) {
return SinkAction.remove(params, params.collection(), documentId);
}
SubdocOperation operation = getOperation(documentId, doc);
MutateInSpec mutation;
switch(mode) {
case UPSERT:
mutation = MutateInSpec.upsert(operation.getPath(), operation.getData());
if (createPaths) {
mutation = ((Upsert) mutation).createPath();
}
break;
case ARRAY_APPEND:
mutation = MutateInSpec.arrayAppend(operation.getPath(), singletonList(operation.getData()));
if (createPaths) {
mutation = ((ArrayAppend) mutation).createPath();
}
break;
case ARRAY_PREPEND:
mutation = MutateInSpec.arrayPrepend(operation.getPath(), singletonList(operation.getData()));
if (createPaths) {
mutation = ((ArrayPrepend) mutation).createPath();
}
break;
default:
throw new RuntimeException("Unsupported subdoc mode: " + mode);
}
MutateInOptions options = mutateInOptions().storeSemantics(createDocuments ? UPSERT : REPLACE);
params.expiry().ifPresent(options::expiry);
params.configureDurability(options);
Mono<?> action = params.collection().mutateIn(documentId, singletonList(mutation), options);
return new SinkAction(action, ConcurrencyHint.of(documentId));
}
use of com.couchbase.client.java.kv.MutateInSpec in project couchbase-jvm-clients by couchbase.
the class AsyncCollection method mutateInRequest.
/**
* Helper method to create the underlying subdoc mutate request.
*
* @param id the outer document ID.
* @param specs the spec which specifies the type of mutations to perform.
* @param opts custom options to modify the mutation options.
* @return the subdoc mutate request.
*/
CompletableFuture<SubdocMutateRequest> mutateInRequest(final String id, final List<MutateInSpec> specs, final MutateInOptions.Built opts, final Duration timeout) {
notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
notNullOrEmpty(specs, "MutateInSpecs", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
if (specs.isEmpty()) {
throw SubdocMutateRequest.errIfNoCommands(ReducedKeyValueErrorContext.create(id, collectionIdentifier));
} else if (specs.size() > SubdocMutateRequest.SUBDOC_MAX_FIELDS) {
throw SubdocMutateRequest.errIfTooManyCommands(ReducedKeyValueErrorContext.create(id, collectionIdentifier));
}
final boolean requiresBucketConfig = opts.createAsDeleted() || opts.storeSemantics() == StoreSemantics.REVIVE;
CompletableFuture<BucketConfig> bucketConfigFuture;
if (requiresBucketConfig) {
bucketConfigFuture = BucketConfigUtil.waitForBucketConfig(core, bucketName(), timeout).toFuture();
} else {
// Nothing will be using the bucket config so just provide null
bucketConfigFuture = CompletableFuture.completedFuture(null);
}
return bucketConfigFuture.thenCompose(bucketConfig -> {
RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
JsonSerializer serializer = opts.serializer() == null ? environment.jsonSerializer() : opts.serializer();
final RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_MUTATE_IN, opts.parentSpan().orElse(null));
ArrayList<SubdocMutateRequest.Command> commands = new ArrayList<>(specs.size());
final RequestSpan encodeSpan = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_ENCODING, span);
long start = System.nanoTime();
try {
for (int i = 0; i < specs.size(); i++) {
MutateInSpec spec = specs.get(i);
commands.add(spec.encode(serializer, i));
}
} finally {
encodeSpan.end();
}
long end = System.nanoTime();
// xattrs come first
commands.sort(Comparator.comparing(v -> !v.xattr()));
long expiry = opts.expiry().encode();
SubdocMutateRequest request = new SubdocMutateRequest(timeout, coreContext, collectionIdentifier, bucketConfig, retryStrategy, id, opts.storeSemantics() == StoreSemantics.INSERT, opts.storeSemantics() == StoreSemantics.UPSERT, opts.storeSemantics() == StoreSemantics.REVIVE, opts.accessDeleted(), opts.createAsDeleted(), commands, expiry, opts.preserveExpiry(), opts.cas(), opts.durabilityLevel(), span);
request.context().clientContext(opts.clientContext()).encodeLatency(end - start);
final CompletableFuture<SubdocMutateRequest> future = new CompletableFuture<>();
future.complete(request);
return future;
});
}
Aggregations