Search in sources :

Example 1 with SubdocGetRequest

use of com.couchbase.client.core.msg.kv.SubdocGetRequest in project couchbase-jvm-clients by couchbase.

the class AsyncCollection method subdocGetRequest.

/**
 * Helper method to create a get request for a subdoc fetch.
 *
 * @param id the document id which is used to uniquely identify it.
 * @param opts custom options to change the default behavior.
 * @return the subdoc get request.
 */
@Stability.Internal
SubdocGetRequest subdocGetRequest(final String id, final GetOptions.Built opts) {
    try {
        notNullOrEmpty(id, "Id");
        if (opts.withExpiry()) {
            if (opts.projections().size() > 15) {
                throw InvalidArgumentException.fromMessage("Only a maximum of 16 fields can be " + "projected per request due to a server limitation (includes the expiration macro as one field).");
            }
        } else {
            if (opts.projections().size() > 16) {
                throw InvalidArgumentException.fromMessage("Only a maximum of 16 fields can be " + "projected per request due to a server limitation.");
            }
        }
    } catch (Exception cause) {
        throw new InvalidArgumentException("Argument validation failed", cause, ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    }
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().kvTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    List<SubdocGetRequest.Command> commands = new ArrayList<>(16);
    if (!opts.projections().isEmpty()) {
        if (opts.projections().size() > 16) {
            throw new UnsupportedOperationException("Only a maximum of 16 fields can be " + "projected per request.");
        }
        List<String> projections = opts.projections();
        for (int i = 0; i < projections.size(); i++) {
            commands.add(new SubdocGetRequest.Command(SubdocCommandType.GET, projections.get(i), false, commands.size()));
        }
    } else {
        commands.add(new SubdocGetRequest.Command(SubdocCommandType.GET_DOC, "", false, commands.size()));
    }
    if (opts.withExpiry()) {
        // xattrs must go first
        commands.add(0, new SubdocGetRequest.Command(SubdocCommandType.GET, LookupInMacro.EXPIRY_TIME, true, commands.size()));
        // JSON compat flags.
        if (opts.projections().isEmpty()) {
            commands.add(1, new SubdocGetRequest.Command(SubdocCommandType.GET, LookupInMacro.FLAGS, true, commands.size()));
        }
    }
    RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_LOOKUP_IN, opts.parentSpan().orElse(null));
    SubdocGetRequest request = new SubdocGetRequest(timeout, coreContext, collectionIdentifier, retryStrategy, id, (byte) 0x00, commands, span);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : ArrayList(java.util.ArrayList) Duration(java.time.Duration) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) SubdocGetRequest(com.couchbase.client.core.msg.kv.SubdocGetRequest)

Example 2 with SubdocGetRequest

use of com.couchbase.client.core.msg.kv.SubdocGetRequest in project couchbase-jvm-clients by couchbase.

the class ReactiveCollection method lookupIn.

/**
 * Performs lookups to document fragments with custom options.
 *
 * @param id the outer document ID.
 * @param specs the spec which specifies the type of lookups to perform.
 * @param options custom options to modify the lookup options.
 * @return the {@link LookupInResult} once the lookup has been performed or failed.
 */
public Mono<LookupInResult> lookupIn(final String id, List<LookupInSpec> specs, final LookupInOptions options) {
    return Mono.defer(() -> {
        notNull(options, "LookupInOptions", () -> ReducedKeyValueErrorContext.create(id, asyncCollection.collectionIdentifier()));
        LookupInOptions.Built opts = options.build();
        JsonSerializer serializer = opts.serializer() == null ? environment().jsonSerializer() : opts.serializer();
        SubdocGetRequest request = asyncCollection.lookupInRequest(id, specs, opts);
        return Reactor.wrap(request, LookupInAccessor.lookupInAccessor(core, request, serializer), true);
    });
}
Also used : LookupInOptions(com.couchbase.client.java.kv.LookupInOptions) JsonSerializer(com.couchbase.client.java.codec.JsonSerializer) SubdocGetRequest(com.couchbase.client.core.msg.kv.SubdocGetRequest)

Example 3 with SubdocGetRequest

use of com.couchbase.client.core.msg.kv.SubdocGetRequest in project couchbase-jvm-clients by couchbase.

the class SubDocumentGetIntegrationTest method checkExpectedFailure.

/**
 * Perform subdoc operations and assert the result is the expected exception
 */
private void checkExpectedFailure(String input, List<SubdocGetRequest.Command> commands, Class<?> expected) {
    String id = UUID.randomUUID().toString();
    insertContent(id, input);
    SubdocGetRequest request = new SubdocGetRequest(kvTimeout, core.context(), CollectionIdentifier.fromDefault(config().bucketname()), env.retryStrategy(), id, (byte) 0, commands, null);
    core.send(request);
    SubdocGetResponse response = null;
    try {
        response = request.response().get();
    } catch (InterruptedException | ExecutionException e) {
        fail("Failed with " + e);
    }
    assertFalse(response.status().success());
    assertEquals(ResponseStatus.SUBDOC_FAILURE, response.status());
    assertTrue(response.error().isPresent());
    CouchbaseException err = response.error().get();
    assertTrue(expected.isInstance(err));
}
Also used : SubdocGetResponse(com.couchbase.client.core.msg.kv.SubdocGetResponse) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) ExecutionException(java.util.concurrent.ExecutionException) SubdocGetRequest(com.couchbase.client.core.msg.kv.SubdocGetRequest)

Example 4 with SubdocGetRequest

use of com.couchbase.client.core.msg.kv.SubdocGetRequest in project couchbase-jvm-clients by couchbase.

the class SubDocumentGetIntegrationTest method checkExpectedSuccess.

/**
 * Perform subdoc operations and check the overall result was success
 */
private SubdocGetResponse checkExpectedSuccess(String input, List<SubdocGetRequest.Command> commands) {
    String id = UUID.randomUUID().toString();
    insertContent(id, input);
    SubdocGetRequest request = new SubdocGetRequest(kvTimeout, core.context(), CollectionIdentifier.fromDefault(config().bucketname()), env.retryStrategy(), id, (byte) 0, commands, null);
    core.send(request);
    SubdocGetResponse response = null;
    try {
        response = request.response().get();
    } catch (InterruptedException | ExecutionException e) {
        fail("Failed with " + e);
    }
    assertTrue(response.status().success());
    assertFalse(response.error().isPresent());
    return response;
}
Also used : SubdocGetResponse(com.couchbase.client.core.msg.kv.SubdocGetResponse) ExecutionException(java.util.concurrent.ExecutionException) SubdocGetRequest(com.couchbase.client.core.msg.kv.SubdocGetRequest)

Example 5 with SubdocGetRequest

use of com.couchbase.client.core.msg.kv.SubdocGetRequest in project couchbase-jvm-clients by couchbase.

the class AsyncCollection method lookupInRequest.

/**
 * Helper method to create the underlying lookup subdoc request.
 *
 * @param id the outer document ID.
 * @param specs the spec which specifies the type of lookups to perform.
 * @param opts custom options to modify the lookup options.
 * @return the subdoc lookup request.
 */
SubdocGetRequest lookupInRequest(final String id, final List<LookupInSpec> specs, final LookupInOptions.Built opts) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNullOrEmpty(specs, "LookupInSpecs", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    ArrayList<SubdocGetRequest.Command> commands = new ArrayList<>(specs.size());
    for (int i = 0; i < specs.size(); i++) {
        LookupInSpec spec = specs.get(i);
        commands.add(spec.export(i));
    }
    // xattrs come first
    commands.sort(Comparator.comparing(v -> !v.xattr()));
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().kvTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    byte flags = 0;
    if (opts.accessDeleted()) {
        flags |= SubdocMutateRequest.SUBDOC_DOC_FLAG_ACCESS_DELETED;
    }
    RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_LOOKUP_IN, opts.parentSpan().orElse(null));
    SubdocGetRequest request = new SubdocGetRequest(timeout, coreContext, collectionIdentifier, retryStrategy, id, flags, commands, span);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : MutateInAccessor(com.couchbase.client.java.kv.MutateInAccessor) JsonSerializer(com.couchbase.client.java.codec.JsonSerializer) InsertOptions(com.couchbase.client.java.kv.InsertOptions) MutationResult(com.couchbase.client.java.kv.MutationResult) DEFAULT_REPLACE_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_REPLACE_OPTIONS) Validators.notNull(com.couchbase.client.core.util.Validators.notNull) PersistTo(com.couchbase.client.java.kv.PersistTo) ReplaceAccessor(com.couchbase.client.java.kv.ReplaceAccessor) TouchAccessor(com.couchbase.client.java.kv.TouchAccessor) DEFAULT_GET_AND_LOCK_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_AND_LOCK_OPTIONS) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) ClusterEnvironment(com.couchbase.client.java.env.ClusterEnvironment) GetAndTouchOptions(com.couchbase.client.java.kv.GetAndTouchOptions) LookupInMacro(com.couchbase.client.java.kv.LookupInMacro) BucketConfig(com.couchbase.client.core.config.BucketConfig) GetAllReplicasOptions(com.couchbase.client.java.kv.GetAllReplicasOptions) TouchRequest(com.couchbase.client.core.msg.kv.TouchRequest) RemoveOptions(com.couchbase.client.java.kv.RemoveOptions) ExistsOptions(com.couchbase.client.java.kv.ExistsOptions) InsertAccessor(com.couchbase.client.java.kv.InsertAccessor) DEFAULT_INSERT_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_INSERT_OPTIONS) GetAndTouchRequest(com.couchbase.client.core.msg.kv.GetAndTouchRequest) GetReplicaResult(com.couchbase.client.java.kv.GetReplicaResult) ExistsResult(com.couchbase.client.java.kv.ExistsResult) UnlockAccessor(com.couchbase.client.java.kv.UnlockAccessor) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Validators.notNullOrEmpty(com.couchbase.client.core.util.Validators.notNullOrEmpty) ReplicaHelper(com.couchbase.client.core.service.kv.ReplicaHelper) Expiry(com.couchbase.client.java.kv.Expiry) List(java.util.List) SubdocGetRequest(com.couchbase.client.core.msg.kv.SubdocGetRequest) GetAndLockRequest(com.couchbase.client.core.msg.kv.GetAndLockRequest) LookupInOptions(com.couchbase.client.java.kv.LookupInOptions) SubdocMutateRequest(com.couchbase.client.core.msg.kv.SubdocMutateRequest) GetAccessor(com.couchbase.client.java.kv.GetAccessor) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) Optional(java.util.Optional) ReplaceRequest(com.couchbase.client.core.msg.kv.ReplaceRequest) DEFAULT_GET_ALL_REPLICAS_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_ALL_REPLICAS_OPTIONS) DEFAULT_MUTATE_IN_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_MUTATE_IN_OPTIONS) GetOptions(com.couchbase.client.java.kv.GetOptions) UpsertAccessor(com.couchbase.client.java.kv.UpsertAccessor) RemoveAccessor(com.couchbase.client.java.kv.RemoveAccessor) GetResult(com.couchbase.client.java.kv.GetResult) ExistsAccessor(com.couchbase.client.java.kv.ExistsAccessor) TouchOptions(com.couchbase.client.java.kv.TouchOptions) UnlockOptions(com.couchbase.client.java.kv.UnlockOptions) DEFAULT_GET_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_OPTIONS) RemoveRequest(com.couchbase.client.core.msg.kv.RemoveRequest) StoreSemantics(com.couchbase.client.java.kv.StoreSemantics) CompletableFuture(java.util.concurrent.CompletableFuture) CommonDurabilityOptions(com.couchbase.client.java.kv.CommonDurabilityOptions) GetMetaRequest(com.couchbase.client.core.msg.kv.GetMetaRequest) ArrayList(java.util.ArrayList) DEFAULT_TOUCH_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_TOUCH_OPTIONS) GetAnyReplicaOptions(com.couchbase.client.java.kv.GetAnyReplicaOptions) ReplaceOptions(com.couchbase.client.java.kv.ReplaceOptions) InsertRequest(com.couchbase.client.core.msg.kv.InsertRequest) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) GetAndLockOptions(com.couchbase.client.java.kv.GetAndLockOptions) DEFAULT_REMOVE_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_REMOVE_OPTIONS) MutateInSpec(com.couchbase.client.java.kv.MutateInSpec) Stability(com.couchbase.client.core.annotation.Stability) DEFAULT_EXISTS_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_EXISTS_OPTIONS) DEFAULT_GET_ANY_REPLICA_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_ANY_REPLICA_OPTIONS) UpsertOptions(com.couchbase.client.java.kv.UpsertOptions) LookupInResult(com.couchbase.client.java.kv.LookupInResult) MutateInOptions(com.couchbase.client.java.kv.MutateInOptions) MutateInResult(com.couchbase.client.java.kv.MutateInResult) DEFAULT_UPSERT_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_UPSERT_OPTIONS) UnlockRequest(com.couchbase.client.core.msg.kv.UnlockRequest) LookupInAccessor(com.couchbase.client.java.kv.LookupInAccessor) DEFAULT_GET_AND_TOUCH_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_AND_TOUCH_OPTIONS) UpsertRequest(com.couchbase.client.core.msg.kv.UpsertRequest) LookupInSpec(com.couchbase.client.java.kv.LookupInSpec) DEFAULT_LOOKUP_IN_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_LOOKUP_IN_OPTIONS) Transcoder(com.couchbase.client.java.codec.Transcoder) DurabilityLevel(com.couchbase.client.core.msg.kv.DurabilityLevel) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) Core(com.couchbase.client.core.Core) ReducedKeyValueErrorContext(com.couchbase.client.core.error.context.ReducedKeyValueErrorContext) Comparator(java.util.Comparator) DEFAULT_UNLOCK_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_UNLOCK_OPTIONS) TimeoutConfig(com.couchbase.client.core.env.TimeoutConfig) SubdocCommandType(com.couchbase.client.core.msg.kv.SubdocCommandType) BucketConfigUtil(com.couchbase.client.core.util.BucketConfigUtil) ArrayList(java.util.ArrayList) LookupInSpec(com.couchbase.client.java.kv.LookupInSpec) Duration(java.time.Duration) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) SubdocGetRequest(com.couchbase.client.core.msg.kv.SubdocGetRequest)

Aggregations

SubdocGetRequest (com.couchbase.client.core.msg.kv.SubdocGetRequest)6 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)2 InvalidArgumentException (com.couchbase.client.core.error.InvalidArgumentException)2 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)2 SubdocGetResponse (com.couchbase.client.core.msg.kv.SubdocGetResponse)2 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)2 Transcoder (com.couchbase.client.java.codec.Transcoder)2 GetOptions (com.couchbase.client.java.kv.GetOptions)2 ExecutionException (java.util.concurrent.ExecutionException)2 Core (com.couchbase.client.core.Core)1 CoreContext (com.couchbase.client.core.CoreContext)1 Stability (com.couchbase.client.core.annotation.Stability)1 TracingIdentifiers (com.couchbase.client.core.cnc.TracingIdentifiers)1 BucketConfig (com.couchbase.client.core.config.BucketConfig)1 TimeoutConfig (com.couchbase.client.core.env.TimeoutConfig)1 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)1 ReducedKeyValueErrorContext (com.couchbase.client.core.error.context.ReducedKeyValueErrorContext)1 CollectionIdentifier (com.couchbase.client.core.io.CollectionIdentifier)1 DurabilityLevel (com.couchbase.client.core.msg.kv.DurabilityLevel)1 GetAndLockRequest (com.couchbase.client.core.msg.kv.GetAndLockRequest)1