Search in sources :

Example 1 with JsonSerializer

use of com.couchbase.client.java.codec.JsonSerializer in project couchbase-jvm-clients by couchbase.

the class RawManager method callManagement.

private static Mono<RawManagerResponse> callManagement(final Cluster cluster, final RawManagerRequest request, final RawManagerOptions options) {
    final ClusterEnvironment environment = cluster.environment();
    final RawManagerOptions.Built opts = options.build();
    JsonSerializer serializer = opts.serializer() != null ? opts.serializer() : environment.jsonSerializer();
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().managementTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    final GenericManagerRequest req = new GenericManagerRequest(timeout, cluster.core().context(), retryStrategy, () -> {
        FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, request.method(), request.uri());
        for (Map.Entry<String, Object> e : opts.httpHeaders().entrySet()) {
            httpRequest.headers().set(e.getKey(), e.getValue());
        }
        return httpRequest;
    }, request.method().equals(HttpMethod.GET), null);
    cluster.core().send(req);
    return Reactor.wrap(req, req.response(), true).map(res -> new RawManagerResponse(request.serviceType(), serializer, res.httpStatus(), res.content()));
}
Also used : FullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultFullHttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultFullHttpRequest) Duration(java.time.Duration) JsonSerializer(com.couchbase.client.java.codec.JsonSerializer) ClusterEnvironment(com.couchbase.client.java.env.ClusterEnvironment) GenericManagerRequest(com.couchbase.client.core.msg.manager.GenericManagerRequest) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) Map(java.util.Map)

Example 2 with JsonSerializer

use of com.couchbase.client.java.codec.JsonSerializer 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;
    });
}
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) MutateInSpec(com.couchbase.client.java.kv.MutateInSpec) JsonSerializer(com.couchbase.client.java.codec.JsonSerializer) BucketConfig(com.couchbase.client.core.config.BucketConfig) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) CompletableFuture(java.util.concurrent.CompletableFuture) SubdocMutateRequest(com.couchbase.client.core.msg.kv.SubdocMutateRequest) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy)

Example 3 with JsonSerializer

use of com.couchbase.client.java.codec.JsonSerializer in project couchbase-jvm-clients by couchbase.

the class AsyncScope method query.

/**
 * Performs a N1QL query with custom {@link QueryOptions} in a Scope.
 *
 * @param statement the N1QL query statement as a raw string.
 * @param options the custom options for this query.
 * @return the {@link QueryResult} once the response arrives successfully.
 */
public CompletableFuture<QueryResult> query(final String statement, final QueryOptions options) {
    notNull(options, "QueryOptions", () -> new ReducedQueryErrorContext(statement));
    final QueryOptions.Built opts = options.build();
    JsonSerializer serializer = opts.serializer() == null ? environment.jsonSerializer() : opts.serializer();
    return queryAccessor.queryAsync(queryRequest(bucketName(), scopeName, statement, opts, core, environment()), opts, serializer);
}
Also used : JsonSerializer(com.couchbase.client.java.codec.JsonSerializer) ReducedQueryErrorContext(com.couchbase.client.core.error.context.ReducedQueryErrorContext) QueryOptions(com.couchbase.client.java.query.QueryOptions)

Example 4 with JsonSerializer

use of com.couchbase.client.java.codec.JsonSerializer 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 5 with JsonSerializer

use of com.couchbase.client.java.codec.JsonSerializer in project couchbase-jvm-clients by couchbase.

the class ReactiveScope method analyticsQuery.

/**
 * Performs an Analytics query with custom {@link AnalyticsOptions} on a scope
 *
 * @param statement the Analytics query statement as a raw string.
 * @param options the custom options for this analytics query.
 * @return the {@link ReactiveAnalyticsResult} once the response arrives successfully.
 */
public Mono<ReactiveAnalyticsResult> analyticsQuery(final String statement, final AnalyticsOptions options) {
    notNull(options, "AnalyticsOptions", () -> new ReducedAnalyticsErrorContext(statement));
    AnalyticsOptions.Built opts = options.build();
    JsonSerializer serializer = opts.serializer() == null ? environment().jsonSerializer() : opts.serializer();
    return Mono.defer(() -> {
        return AnalyticsAccessor.analyticsQueryReactive(asyncScope.core(), asyncScope.analyticsRequest(statement, opts), serializer);
    });
}
Also used : ReducedAnalyticsErrorContext(com.couchbase.client.core.error.context.ReducedAnalyticsErrorContext) JsonSerializer(com.couchbase.client.java.codec.JsonSerializer) AnalyticsOptions(com.couchbase.client.java.analytics.AnalyticsOptions)

Aggregations

JsonSerializer (com.couchbase.client.java.codec.JsonSerializer)18 ReducedAnalyticsErrorContext (com.couchbase.client.core.error.context.ReducedAnalyticsErrorContext)4 ReducedQueryErrorContext (com.couchbase.client.core.error.context.ReducedQueryErrorContext)4 AnalyticsOptions (com.couchbase.client.java.analytics.AnalyticsOptions)4 ClusterEnvironment (com.couchbase.client.java.env.ClusterEnvironment)4 QueryOptions (com.couchbase.client.java.query.QueryOptions)4 LookupInOptions (com.couchbase.client.java.kv.LookupInOptions)3 ReducedSearchErrorContext (com.couchbase.client.core.error.context.ReducedSearchErrorContext)2 ReducedViewErrorContext (com.couchbase.client.core.error.context.ReducedViewErrorContext)2 SubdocGetRequest (com.couchbase.client.core.msg.kv.SubdocGetRequest)2 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)2 JacksonJsonSerializer (com.couchbase.client.java.codec.JacksonJsonSerializer)2 SearchOptions (com.couchbase.client.java.search.SearchOptions)2 Duration (java.time.Duration)2 Test (org.junit.jupiter.api.Test)2 Core (com.couchbase.client.core.Core)1 CoreContext (com.couchbase.client.core.CoreContext)1 Stability (com.couchbase.client.core.annotation.Stability)1 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)1 TracingIdentifiers (com.couchbase.client.core.cnc.TracingIdentifiers)1