Search in sources :

Example 6 with InsertRequest

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

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

the class SubDocumentGetIntegrationTest method insertContent.

private byte[] insertContent(String id, String in) {
    byte[] content = in.getBytes(UTF_8);
    InsertRequest insertRequest = new InsertRequest(id, content, 0, 0, kvTimeout, core.context(), CollectionIdentifier.fromDefault(config().bucketname()), env.retryStrategy(), Optional.empty(), null);
    core.send(insertRequest);
    InsertResponse insertResponse = null;
    try {
        insertResponse = insertRequest.response().get();
    } catch (InterruptedException | ExecutionException e) {
        fail("Failed with " + e);
    }
    assertTrue(insertResponse.status().success());
    return content;
}
Also used : InsertResponse(com.couchbase.client.core.msg.kv.InsertResponse) InsertRequest(com.couchbase.client.core.msg.kv.InsertRequest) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with InsertRequest

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

the class SubDocumentMutateIntegrationTest method insertContent.

private byte[] insertContent(String id, String in) {
    byte[] content = in.getBytes(UTF_8);
    InsertRequest insertRequest = new InsertRequest(id, content, 0, 0, kvTimeout, core.context(), CollectionIdentifier.fromDefault(config().bucketname()), env.retryStrategy(), Optional.empty(), null);
    core.send(insertRequest);
    InsertResponse insertResponse = null;
    try {
        insertResponse = insertRequest.response().get();
    } catch (InterruptedException | ExecutionException e) {
        fail("Failed with " + e);
    }
    assertTrue(insertResponse.status().success());
    return content;
}
Also used : InsertResponse(com.couchbase.client.core.msg.kv.InsertResponse) InsertRequest(com.couchbase.client.core.msg.kv.InsertRequest) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with InsertRequest

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

the class KeyValueIntegrationTest method failFastIfSyncReplicationNotAvailable.

@Test
@IgnoreWhen(hasCapabilities = { Capabilities.SYNC_REPLICATION })
void failFastIfSyncReplicationNotAvailable() {
    String id = UUID.randomUUID().toString();
    byte[] content = "hello, world".getBytes(UTF_8);
    InsertRequest insertRequest = new InsertRequest(id, content, 0, 0, kvTimeout, core.context(), CollectionIdentifier.fromDefault(config().bucketname()), env.retryStrategy(), Optional.of(DurabilityLevel.MAJORITY), null);
    core.send(insertRequest);
    ExecutionException exception = assertThrows(ExecutionException.class, () -> insertRequest.response().get());
    assertTrue(exception.getCause() instanceof FeatureNotAvailableException);
}
Also used : FeatureNotAvailableException(com.couchbase.client.core.error.FeatureNotAvailableException) InsertRequest(com.couchbase.client.core.msg.kv.InsertRequest) ExecutionException(java.util.concurrent.ExecutionException) IgnoreWhen(com.couchbase.client.test.IgnoreWhen) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 10 with InsertRequest

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

the class KeyValueIntegrationTest method insertAndGet.

/**
 * Validate that an inserted document can be read subsequently.
 */
@Test
void insertAndGet() throws Exception {
    String id = UUID.randomUUID().toString();
    byte[] content = "hello, world".getBytes(UTF_8);
    InsertRequest insertRequest = new InsertRequest(id, content, 0, 0, kvTimeout, core.context(), CollectionIdentifier.fromDefault(config().bucketname()), env.retryStrategy(), Optional.empty(), null);
    core.send(insertRequest);
    InsertResponse insertResponse = insertRequest.response().get();
    assertTrue(insertResponse.status().success());
    GetRequest getRequest = new GetRequest(id, kvTimeout, core.context(), CollectionIdentifier.fromDefault(config().bucketname()), env.retryStrategy(), null);
    core.send(getRequest);
    GetResponse getResponse = getRequest.response().get();
    assertTrue(getResponse.status().success());
    assertArrayEquals(content, getResponse.content());
    assertTrue(getResponse.cas() != 0);
}
Also used : InsertResponse(com.couchbase.client.core.msg.kv.InsertResponse) InsertRequest(com.couchbase.client.core.msg.kv.InsertRequest) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) GetResponse(com.couchbase.client.core.msg.kv.GetResponse) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

InsertRequest (com.couchbase.client.core.msg.kv.InsertRequest)12 InsertResponse (com.couchbase.client.core.msg.kv.InsertResponse)8 CoreIntegrationTest (com.couchbase.client.core.util.CoreIntegrationTest)6 Test (org.junit.jupiter.api.Test)6 ExecutionException (java.util.concurrent.ExecutionException)4 FeatureNotAvailableException (com.couchbase.client.core.error.FeatureNotAvailableException)2 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)2 GetResponse (com.couchbase.client.core.msg.kv.GetResponse)2 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)2 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)1 ReplicaNotConfiguredException (com.couchbase.client.core.error.ReplicaNotConfiguredException)1 CollectionIdentifier (com.couchbase.client.core.io.CollectionIdentifier)1 DecrementRequest (com.couchbase.client.core.msg.kv.DecrementRequest)1 DecrementResponse (com.couchbase.client.core.msg.kv.DecrementResponse)1 IncrementRequest (com.couchbase.client.core.msg.kv.IncrementRequest)1 IncrementResponse (com.couchbase.client.core.msg.kv.IncrementResponse)1 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)1 Transcoder (com.couchbase.client.java.codec.Transcoder)1 InsertOptions (com.couchbase.client.java.kv.InsertOptions)1 Duration (java.time.Duration)1