Search in sources :

Example 1 with UpsertRequest

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

the class MutationTokenIntegrationTest method tokenOnSubdocMutate.

@Test
void tokenOnSubdocMutate() throws Exception {
    String id = UUID.randomUUID().toString();
    byte[] content = "{}".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());
    List<SubdocMutateRequest.Command> commands = new ArrayList<>();
    commands.add(new SubdocMutateRequest.Command(SubdocCommandType.DICT_ADD, "foo", "\"bar\"".getBytes(UTF_8), true, false, false, 0));
    SubdocMutateRequest subdocMutateRequest = new SubdocMutateRequest(kvTimeout, core.context(), collectionIdentifier, null, env.retryStrategy(), id, false, false, false, false, false, commands, 0, false, 0, Optional.empty(), null);
    core.send(subdocMutateRequest);
    SubdocMutateResponse subdocMutateResponse = subdocMutateRequest.response().get();
    assertTrue(subdocMutateResponse.status().success());
    assertMutationToken(subdocMutateResponse.mutationToken());
}
Also used : SubdocMutateRequest(com.couchbase.client.core.msg.kv.SubdocMutateRequest) SubdocMutateResponse(com.couchbase.client.core.msg.kv.SubdocMutateResponse) UpsertResponse(com.couchbase.client.core.msg.kv.UpsertResponse) UpsertRequest(com.couchbase.client.core.msg.kv.UpsertRequest) ArrayList(java.util.ArrayList) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with UpsertRequest

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

the class MutationTokenIntegrationTest method tokenOnPrepend.

@Test
void tokenOnPrepend() throws Exception {
    String id = UUID.randomUUID().toString();
    byte[] content = "hello".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());
    PrependRequest prependRequest = new PrependRequest(kvTimeout, core.context(), collectionIdentifier, env.retryStrategy(), id, ", world".getBytes(UTF_8), upsertResponse.cas(), Optional.empty(), null);
    core.send(prependRequest);
    PrependResponse prependResponse = prependRequest.response().get();
    assertTrue(prependResponse.status().success());
    assertMutationToken(prependResponse.mutationToken());
}
Also used : UpsertResponse(com.couchbase.client.core.msg.kv.UpsertResponse) UpsertRequest(com.couchbase.client.core.msg.kv.UpsertRequest) PrependRequest(com.couchbase.client.core.msg.kv.PrependRequest) PrependResponse(com.couchbase.client.core.msg.kv.PrependResponse) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with UpsertRequest

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

the class MutationTokenIntegrationTest method tokenOnRemove.

@Test
void tokenOnRemove() 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());
    RemoveRequest removeRequest = new RemoveRequest(id, upsertResponse.cas(), kvTimeout, core.context(), collectionIdentifier, env.retryStrategy(), Optional.empty(), null);
    core.send(removeRequest);
    RemoveResponse removeResponse = removeRequest.response().get();
    assertTrue(removeResponse.status().success());
    assertMutationToken(removeResponse.mutationToken());
}
Also used : RemoveResponse(com.couchbase.client.core.msg.kv.RemoveResponse) RemoveRequest(com.couchbase.client.core.msg.kv.RemoveRequest) UpsertResponse(com.couchbase.client.core.msg.kv.UpsertResponse) UpsertRequest(com.couchbase.client.core.msg.kv.UpsertRequest) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with UpsertRequest

use of com.couchbase.client.core.msg.kv.UpsertRequest 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 5 with UpsertRequest

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

the class ReactiveCollection method upsert.

/**
 * Upserts a full document which might or might not exist yet with custom options.
 *
 * @param id the document id to upsert.
 * @param content the document content to upsert.
 * @param options custom options to customize the upsert behavior.
 * @return a {@link Mono} completing once upserted or failed.
 */
public Mono<MutationResult> upsert(final String id, Object content, final UpsertOptions options) {
    return Mono.defer(() -> {
        notNull(options, "UpsertOptions", () -> ReducedKeyValueErrorContext.create(id, asyncCollection.collectionIdentifier()));
        UpsertOptions.Built opts = options.build();
        UpsertRequest request = asyncCollection.upsertRequest(id, content, opts);
        return Reactor.wrap(request, UpsertAccessor.upsert(core, request, id, opts.persistTo(), opts.replicateTo()), true);
    });
}
Also used : UpsertRequest(com.couchbase.client.core.msg.kv.UpsertRequest) UpsertOptions(com.couchbase.client.java.kv.UpsertOptions)

Aggregations

UpsertRequest (com.couchbase.client.core.msg.kv.UpsertRequest)9 UpsertResponse (com.couchbase.client.core.msg.kv.UpsertResponse)7 CoreIntegrationTest (com.couchbase.client.core.util.CoreIntegrationTest)7 Test (org.junit.jupiter.api.Test)7 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)1 AppendRequest (com.couchbase.client.core.msg.kv.AppendRequest)1 AppendResponse (com.couchbase.client.core.msg.kv.AppendResponse)1 PrependRequest (com.couchbase.client.core.msg.kv.PrependRequest)1 PrependResponse (com.couchbase.client.core.msg.kv.PrependResponse)1 RemoveRequest (com.couchbase.client.core.msg.kv.RemoveRequest)1 RemoveResponse (com.couchbase.client.core.msg.kv.RemoveResponse)1 ReplaceRequest (com.couchbase.client.core.msg.kv.ReplaceRequest)1 ReplaceResponse (com.couchbase.client.core.msg.kv.ReplaceResponse)1 SubdocMutateRequest (com.couchbase.client.core.msg.kv.SubdocMutateRequest)1 SubdocMutateResponse (com.couchbase.client.core.msg.kv.SubdocMutateResponse)1 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)1 Transcoder (com.couchbase.client.java.codec.Transcoder)1 UpsertOptions (com.couchbase.client.java.kv.UpsertOptions)1 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)1 Duration (java.time.Duration)1