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;
}
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;
}
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;
}
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);
}
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);
}
Aggregations