Search in sources :

Example 1 with Transcoder

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

the class AsyncCollection method upsertRequest.

/**
 * Helper method to generate the upsert request.
 *
 * @param id the document id to upsert.
 * @param content the document content to upsert.
 * @param opts custom options to customize the upsert behavior.
 * @return the upsert request.
 */
UpsertRequest upsertRequest(final String id, final Object content, final UpsertOptions.Built opts) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNull(content, "Content", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    Duration timeout = decideKvTimeout(opts, environment.timeoutConfig());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    Transcoder transcoder = opts.transcoder() == null ? environment.transcoder() : opts.transcoder();
    final RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_UPSERT, opts.parentSpan().orElse(null));
    final RequestSpan encodeSpan = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_ENCODING, span);
    long start = System.nanoTime();
    Transcoder.EncodedValue encoded;
    try {
        encoded = transcoder.encode(content);
    } finally {
        encodeSpan.end();
    }
    long end = System.nanoTime();
    long expiry = opts.expiry().encode();
    final UpsertRequest request = new UpsertRequest(id, encoded.encoded(), expiry, opts.preserveExpiry(), encoded.flags(), timeout, coreContext, collectionIdentifier, retryStrategy, opts.durabilityLevel(), span);
    request.context().clientContext(opts.clientContext()).encodeLatency(end - start);
    return request;
}
Also used : UpsertRequest(com.couchbase.client.core.msg.kv.UpsertRequest) Duration(java.time.Duration) Transcoder(com.couchbase.client.java.codec.Transcoder) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 2 with Transcoder

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

the class ReactiveCollection method getAndLock.

/**
 * Fetches a full document and write-locks it for the given duration with custom options.
 * <p>
 * Note that the client does not enforce an upper limit on the {@link Duration} lockTime. The maximum lock time
 * by default on the server is 30 seconds. Any value larger than 30 seconds will be capped down by the server to
 * the default lock time, which is 15 seconds unless modified on the server side.
 *
 * @param id the document id which is used to uniquely identify it.
 * @param lockTime how long to write-lock the document for (any duration > 30s will be capped to server default of 15s).
 * @param options custom options to change the default behavior.
 * @return a {@link Mono} completing once loaded or failed.
 */
public Mono<GetResult> getAndLock(final String id, final Duration lockTime, final GetAndLockOptions options) {
    return Mono.defer(() -> {
        GetAndLockOptions.Built opts = options.build();
        final Transcoder transcoder = opts.transcoder() == null ? environment().transcoder() : opts.transcoder();
        GetAndLockRequest request = asyncCollection.getAndLockRequest(id, lockTime, opts);
        return Reactor.wrap(request, GetAccessor.getAndLock(core, request, transcoder), true);
    });
}
Also used : GetAndLockRequest(com.couchbase.client.core.msg.kv.GetAndLockRequest) GetAndLockOptions(com.couchbase.client.java.kv.GetAndLockOptions) Transcoder(com.couchbase.client.java.codec.Transcoder)

Example 3 with Transcoder

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

the class ReactiveCollection method getAllReplicas.

/**
 * Reads all available replicas, including the active, and returns the results as a flux.
 * <p>
 * Note that individual errors are ignored, so you can think of this API as a best effort
 * approach which explicitly emphasises availability over consistency.
 * <p>
 * If the read requests all fail, the flux emits nothing.
 *
 * @param id the document id.
 * @param options the custom options.
 * @return a flux of results from all replicas
 */
public Flux<GetReplicaResult> getAllReplicas(final String id, final GetAllReplicasOptions options) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, asyncCollection.collectionIdentifier()));
    notNull(options, "GetAllReplicasOptions", () -> ReducedKeyValueErrorContext.create(id, asyncCollection.collectionIdentifier()));
    GetAllReplicasOptions.Built opts = options.build();
    final Transcoder transcoder = Optional.ofNullable(opts.transcoder()).orElse(environment().transcoder());
    return ReplicaHelper.getAllReplicasReactive(core, asyncCollection.collectionIdentifier(), id, opts.timeout().orElse(environment().timeoutConfig().kvTimeout()), opts.retryStrategy().orElse(environment().retryStrategy()), opts.clientContext(), opts.parentSpan().orElse(null)).map(response -> GetReplicaResult.from(response, transcoder));
}
Also used : GetAllReplicasOptions(com.couchbase.client.java.kv.GetAllReplicasOptions) Transcoder(com.couchbase.client.java.codec.Transcoder)

Example 4 with Transcoder

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

the class AsyncCollection method insertRequest.

/**
 * Helper method to generate the insert request.
 *
 * @param id the document id to insert.
 * @param content the document content to insert.
 * @param opts custom options to customize the insert behavior.
 * @return the insert request.
 */
InsertRequest insertRequest(final String id, final Object content, final InsertOptions.Built opts) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNull(content, "Content", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    Duration timeout = decideKvTimeout(opts, environment.timeoutConfig());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    Transcoder transcoder = opts.transcoder() == null ? environment.transcoder() : opts.transcoder();
    final RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_INSERT, opts.parentSpan().orElse(null));
    final RequestSpan encodeSpan = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_ENCODING, span);
    long start = System.nanoTime();
    Transcoder.EncodedValue encoded;
    try {
        encoded = transcoder.encode(content);
    } finally {
        encodeSpan.end();
    }
    long end = System.nanoTime();
    long expiry = opts.expiry().encode();
    InsertRequest request = new InsertRequest(id, encoded.encoded(), expiry, encoded.flags(), timeout, coreContext, collectionIdentifier, retryStrategy, opts.durabilityLevel(), span);
    request.context().clientContext(opts.clientContext()).encodeLatency(end - start);
    return request;
}
Also used : InsertRequest(com.couchbase.client.core.msg.kv.InsertRequest) Duration(java.time.Duration) Transcoder(com.couchbase.client.java.codec.Transcoder) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 5 with Transcoder

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

the class AsyncCollection method getAnyReplica.

/**
 * Reads all available replicas, and returns the first found.
 *
 * @param id the document id.
 * @param options the custom options.
 * @return a future containing the first available replica.
 */
public CompletableFuture<GetReplicaResult> getAnyReplica(final String id, final GetAnyReplicaOptions options) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNull(options, "GetAnyReplicaOptions", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    GetAnyReplicaOptions.Built opts = options.build();
    Transcoder transcoder = opts.transcoder() == null ? environment.transcoder() : opts.transcoder();
    return ReplicaHelper.getAnyReplicaAsync(core, collectionIdentifier, id, opts.timeout().orElse(environment.timeoutConfig().kvTimeout()), opts.retryStrategy().orElse(environment().retryStrategy()), opts.clientContext(), opts.parentSpan().orElse(null), response -> GetReplicaResult.from(response, transcoder));
}
Also used : Transcoder(com.couchbase.client.java.codec.Transcoder) GetAnyReplicaOptions(com.couchbase.client.java.kv.GetAnyReplicaOptions)

Aggregations

Transcoder (com.couchbase.client.java.codec.Transcoder)13 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)3 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)3 Duration (java.time.Duration)3 GetAllReplicasOptions (com.couchbase.client.java.kv.GetAllReplicasOptions)2 GetAndLockOptions (com.couchbase.client.java.kv.GetAndLockOptions)2 GetAndTouchOptions (com.couchbase.client.java.kv.GetAndTouchOptions)2 GetAnyReplicaOptions (com.couchbase.client.java.kv.GetAnyReplicaOptions)2 GetOptions (com.couchbase.client.java.kv.GetOptions)2 GetAndLockRequest (com.couchbase.client.core.msg.kv.GetAndLockRequest)1 GetAndTouchRequest (com.couchbase.client.core.msg.kv.GetAndTouchRequest)1 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)1 InsertRequest (com.couchbase.client.core.msg.kv.InsertRequest)1 ReplaceRequest (com.couchbase.client.core.msg.kv.ReplaceRequest)1 SubdocGetRequest (com.couchbase.client.core.msg.kv.SubdocGetRequest)1 UpsertRequest (com.couchbase.client.core.msg.kv.UpsertRequest)1