Search in sources :

Example 6 with Transcoder

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

the class AsyncCollection 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 CompletableFuture} completing once loaded or failed.
 */
public CompletableFuture<GetResult> getAndLock(final String id, final Duration lockTime, final GetAndLockOptions options) {
    notNull(options, "GetAndLockOptions", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    GetAndLockOptions.Built opts = options.build();
    final Transcoder transcoder = opts.transcoder() == null ? environment.transcoder() : opts.transcoder();
    return GetAccessor.getAndLock(core, getAndLockRequest(id, lockTime, opts), transcoder);
}
Also used : GetAndLockOptions(com.couchbase.client.java.kv.GetAndLockOptions) Transcoder(com.couchbase.client.java.codec.Transcoder)

Example 7 with Transcoder

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

the class AsyncCollection method get.

/**
 * Fetches a full document (or a projection of it) from a collection with custom options.
 *
 * @param id the document id which is used to uniquely identify it.
 * @param options custom options to change the default behavior.
 * @return a {@link CompletableFuture} completing once loaded or failed.
 */
public CompletableFuture<GetResult> get(final String id, final GetOptions options) {
    notNull(options, "GetOptions", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    final GetOptions.Built opts = options.build();
    final Transcoder transcoder = opts.transcoder() == null ? environment.transcoder() : opts.transcoder();
    if (opts.projections().isEmpty() && !opts.withExpiry()) {
        return GetAccessor.get(core, fullGetRequest(id, opts), transcoder);
    } else {
        return GetAccessor.subdocGet(core, subdocGetRequest(id, opts), transcoder);
    }
}
Also used : Transcoder(com.couchbase.client.java.codec.Transcoder) GetOptions(com.couchbase.client.java.kv.GetOptions)

Example 8 with Transcoder

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

the class AsyncCollection method getAllReplicas.

/**
 * Reads from replicas or the active node based on the options and returns the results as a list
 * of futures that might complete or fail.
 *
 * @param id the document id.
 * @return a list of results from the active and the replica.
 */
public CompletableFuture<List<CompletableFuture<GetReplicaResult>>> getAllReplicas(final String id, final GetAllReplicasOptions options) {
    notNull(options, "GetAllReplicasOptions");
    GetAllReplicasOptions.Built opts = options.build();
    Transcoder transcoder = opts.transcoder() == null ? environment.transcoder() : opts.transcoder();
    return ReplicaHelper.getAllReplicasAsync(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 : GetAllReplicasOptions(com.couchbase.client.java.kv.GetAllReplicasOptions) Transcoder(com.couchbase.client.java.codec.Transcoder)

Example 9 with Transcoder

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

the class AsyncCollection method getAndTouch.

/**
 * Fetches a full document and resets its expiration time to the value provided with custom
 * options.
 *
 * @param id the document id which is used to uniquely identify it.
 * @param expiry the new expiration time for the document.
 * @param options custom options to change the default behavior.
 * @return a {@link CompletableFuture} completing once loaded or failed.
 */
public CompletableFuture<GetResult> getAndTouch(final String id, final Duration expiry, final GetAndTouchOptions options) {
    notNull(expiry, "Expiry", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNull(options, "GetAndTouchOptions", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    GetAndTouchOptions.Built opts = options.build();
    final Transcoder transcoder = opts.transcoder() == null ? environment.transcoder() : opts.transcoder();
    return GetAccessor.getAndTouch(core, getAndTouchRequest(id, Expiry.relative(expiry), opts), transcoder);
}
Also used : GetAndTouchOptions(com.couchbase.client.java.kv.GetAndTouchOptions) Transcoder(com.couchbase.client.java.codec.Transcoder)

Example 10 with Transcoder

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

the class AsyncCollection method replaceRequest.

/**
 * Helper method to generate the replace request.
 *
 * @param id the document id to replace.
 * @param content the document content to replace.
 * @param opts custom options to customize the replace behavior.
 * @return the replace request.
 */
ReplaceRequest replaceRequest(final String id, final Object content, final ReplaceOptions.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_REPLACE, 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();
    ReplaceRequest request = new ReplaceRequest(id, encoded.encoded(), expiry, opts.preserveExpiry(), encoded.flags(), timeout, opts.cas(), coreContext, collectionIdentifier, retryStrategy, opts.durabilityLevel(), span);
    request.context().clientContext(opts.clientContext()).encodeLatency(end - start);
    return request;
}
Also used : ReplaceRequest(com.couchbase.client.core.msg.kv.ReplaceRequest) 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)

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