Search in sources :

Example 1 with ReplaceRequest

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

the class KeyValueIntegrationTest method replaceThrowsInvalidArgumentForBadPreserveExpiry.

@Test
void replaceThrowsInvalidArgumentForBadPreserveExpiry() {
    byte[] content = "hello, world".getBytes(UTF_8);
    final long expiry = 30;
    final boolean preserveExpiry = true;
    Throwable t = assertThrows(InvalidArgumentException.class, () -> new ReplaceRequest("foo", content, expiry, preserveExpiry, 0, kvTimeout, 0, core.context(), CollectionIdentifier.fromDefault(config().bucketname()), env.retryStrategy(), Optional.empty(), null));
    assertTrue(t.getMessage().contains("preserveExpiry"));
}
Also used : ReplaceRequest(com.couchbase.client.core.msg.kv.ReplaceRequest) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with ReplaceRequest

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

the class MutationTokenIntegrationTest method tokenOnReplace.

@Test
void tokenOnReplace() throws Exception {
    String id = UUID.randomUUID().toString();
    byte[] content = "hello, world".getBytes(UTF_8);
    UpsertRequest upsertRequest = new UpsertRequest(id, content, 0, false, 0, kvTimeout, core.context(), collectionIdentifier, env.retryStrategy(), Optional.empty(), null);
    core.send(upsertRequest);
    UpsertResponse upsertResponse = upsertRequest.response().get();
    assertTrue(upsertResponse.status().success());
    assertMutationToken(upsertResponse.mutationToken());
    ReplaceRequest replaceRequest = new ReplaceRequest(id, content, 0, false, 0, kvTimeout, upsertResponse.cas(), core.context(), collectionIdentifier, env.retryStrategy(), Optional.empty(), null);
    core.send(replaceRequest);
    ReplaceResponse replaceResponse = replaceRequest.response().get();
    assertTrue(replaceResponse.status().success());
    assertMutationToken(replaceResponse.mutationToken());
}
Also used : ReplaceRequest(com.couchbase.client.core.msg.kv.ReplaceRequest) UpsertResponse(com.couchbase.client.core.msg.kv.UpsertResponse) UpsertRequest(com.couchbase.client.core.msg.kv.UpsertRequest) ReplaceResponse(com.couchbase.client.core.msg.kv.ReplaceResponse) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with ReplaceRequest

use of com.couchbase.client.core.msg.kv.ReplaceRequest 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)

Example 4 with ReplaceRequest

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

the class ReactiveCollection method replace.

/**
 * Replaces a full document which already exists with custom options.
 *
 * @param id the document id to replace.
 * @param content the document content to replace.
 * @param options custom options to customize the replace behavior.
 * @return a {@link Mono} completing once replaced or failed.
 */
public Mono<MutationResult> replace(final String id, Object content, final ReplaceOptions options) {
    return Mono.defer(() -> {
        notNull(options, "ReplaceOptions", () -> ReducedKeyValueErrorContext.create(id, asyncCollection.collectionIdentifier()));
        ReplaceOptions.Built opts = options.build();
        ReplaceRequest request = asyncCollection.replaceRequest(id, content, opts);
        return Reactor.wrap(request, ReplaceAccessor.replace(core, request, id, opts.persistTo(), opts.replicateTo()), true);
    });
}
Also used : ReplaceRequest(com.couchbase.client.core.msg.kv.ReplaceRequest) ReplaceOptions(com.couchbase.client.java.kv.ReplaceOptions)

Aggregations

ReplaceRequest (com.couchbase.client.core.msg.kv.ReplaceRequest)4 CoreIntegrationTest (com.couchbase.client.core.util.CoreIntegrationTest)2 Test (org.junit.jupiter.api.Test)2 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)1 ReplaceResponse (com.couchbase.client.core.msg.kv.ReplaceResponse)1 UpsertRequest (com.couchbase.client.core.msg.kv.UpsertRequest)1 UpsertResponse (com.couchbase.client.core.msg.kv.UpsertResponse)1 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)1 Transcoder (com.couchbase.client.java.codec.Transcoder)1 ReplaceOptions (com.couchbase.client.java.kv.ReplaceOptions)1 Duration (java.time.Duration)1