use of com.couchbase.client.core.cnc.RequestSpan in project couchbase-jvm-clients by couchbase.
the class AsyncCollection method fullGetRequest.
/**
* Helper method to create a get request for a full doc fetch.
*
* @param id the document id which is used to uniquely identify it.
* @param opts custom options to change the default behavior.
* @return the get request.
*/
@Stability.Internal
GetRequest fullGetRequest(final String id, final GetOptions.Built opts) {
notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
Duration timeout = opts.timeout().orElse(environment.timeoutConfig().kvTimeout());
RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_GET, opts.parentSpan().orElse(null));
GetRequest request = new GetRequest(id, timeout, coreContext, collectionIdentifier, retryStrategy, span);
request.context().clientContext(opts.clientContext());
return request;
}
use of com.couchbase.client.core.cnc.RequestSpan in project couchbase-jvm-clients by couchbase.
the class AsyncCollection method subdocGetRequest.
/**
* Helper method to create a get request for a subdoc fetch.
*
* @param id the document id which is used to uniquely identify it.
* @param opts custom options to change the default behavior.
* @return the subdoc get request.
*/
@Stability.Internal
SubdocGetRequest subdocGetRequest(final String id, final GetOptions.Built opts) {
try {
notNullOrEmpty(id, "Id");
if (opts.withExpiry()) {
if (opts.projections().size() > 15) {
throw InvalidArgumentException.fromMessage("Only a maximum of 16 fields can be " + "projected per request due to a server limitation (includes the expiration macro as one field).");
}
} else {
if (opts.projections().size() > 16) {
throw InvalidArgumentException.fromMessage("Only a maximum of 16 fields can be " + "projected per request due to a server limitation.");
}
}
} catch (Exception cause) {
throw new InvalidArgumentException("Argument validation failed", cause, ReducedKeyValueErrorContext.create(id, collectionIdentifier));
}
Duration timeout = opts.timeout().orElse(environment.timeoutConfig().kvTimeout());
RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
List<SubdocGetRequest.Command> commands = new ArrayList<>(16);
if (!opts.projections().isEmpty()) {
if (opts.projections().size() > 16) {
throw new UnsupportedOperationException("Only a maximum of 16 fields can be " + "projected per request.");
}
List<String> projections = opts.projections();
for (int i = 0; i < projections.size(); i++) {
commands.add(new SubdocGetRequest.Command(SubdocCommandType.GET, projections.get(i), false, commands.size()));
}
} else {
commands.add(new SubdocGetRequest.Command(SubdocCommandType.GET_DOC, "", false, commands.size()));
}
if (opts.withExpiry()) {
// xattrs must go first
commands.add(0, new SubdocGetRequest.Command(SubdocCommandType.GET, LookupInMacro.EXPIRY_TIME, true, commands.size()));
// JSON compat flags.
if (opts.projections().isEmpty()) {
commands.add(1, new SubdocGetRequest.Command(SubdocCommandType.GET, LookupInMacro.FLAGS, true, commands.size()));
}
}
RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_LOOKUP_IN, opts.parentSpan().orElse(null));
SubdocGetRequest request = new SubdocGetRequest(timeout, coreContext, collectionIdentifier, retryStrategy, id, (byte) 0x00, commands, span);
request.context().clientContext(opts.clientContext());
return request;
}
use of com.couchbase.client.core.cnc.RequestSpan in project couchbase-jvm-clients by couchbase.
the class AsyncCollection method getAndTouchRequest.
/**
* Helper method for get and touch requests.
*
* @param id the document id which is used to uniquely identify it.
* @param expiry the new expiration time for the document.
* @param opts custom options to change the default behavior.
* @return the get and touch request.
*/
@Stability.Internal
GetAndTouchRequest getAndTouchRequest(final String id, final Expiry expiry, final GetAndTouchOptions.Built opts) {
notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
notNull(expiry, "Expiry", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
Duration timeout = opts.timeout().orElse(environment.timeoutConfig().kvTimeout());
RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_GET_AND_TOUCH, opts.parentSpan().orElse(null));
long encodedExpiry = expiry.encode();
GetAndTouchRequest request = new GetAndTouchRequest(id, timeout, coreContext, collectionIdentifier, retryStrategy, encodedExpiry, span);
request.context().clientContext(opts.clientContext());
return request;
}
use of com.couchbase.client.core.cnc.RequestSpan in project couchbase-jvm-clients by couchbase.
the class AsyncCollection method mutateInRequest.
/**
* Helper method to create the underlying subdoc mutate request.
*
* @param id the outer document ID.
* @param specs the spec which specifies the type of mutations to perform.
* @param opts custom options to modify the mutation options.
* @return the subdoc mutate request.
*/
CompletableFuture<SubdocMutateRequest> mutateInRequest(final String id, final List<MutateInSpec> specs, final MutateInOptions.Built opts, final Duration timeout) {
notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
notNullOrEmpty(specs, "MutateInSpecs", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
if (specs.isEmpty()) {
throw SubdocMutateRequest.errIfNoCommands(ReducedKeyValueErrorContext.create(id, collectionIdentifier));
} else if (specs.size() > SubdocMutateRequest.SUBDOC_MAX_FIELDS) {
throw SubdocMutateRequest.errIfTooManyCommands(ReducedKeyValueErrorContext.create(id, collectionIdentifier));
}
final boolean requiresBucketConfig = opts.createAsDeleted() || opts.storeSemantics() == StoreSemantics.REVIVE;
CompletableFuture<BucketConfig> bucketConfigFuture;
if (requiresBucketConfig) {
bucketConfigFuture = BucketConfigUtil.waitForBucketConfig(core, bucketName(), timeout).toFuture();
} else {
// Nothing will be using the bucket config so just provide null
bucketConfigFuture = CompletableFuture.completedFuture(null);
}
return bucketConfigFuture.thenCompose(bucketConfig -> {
RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
JsonSerializer serializer = opts.serializer() == null ? environment.jsonSerializer() : opts.serializer();
final RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_MUTATE_IN, opts.parentSpan().orElse(null));
ArrayList<SubdocMutateRequest.Command> commands = new ArrayList<>(specs.size());
final RequestSpan encodeSpan = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_ENCODING, span);
long start = System.nanoTime();
try {
for (int i = 0; i < specs.size(); i++) {
MutateInSpec spec = specs.get(i);
commands.add(spec.encode(serializer, i));
}
} finally {
encodeSpan.end();
}
long end = System.nanoTime();
// xattrs come first
commands.sort(Comparator.comparing(v -> !v.xattr()));
long expiry = opts.expiry().encode();
SubdocMutateRequest request = new SubdocMutateRequest(timeout, coreContext, collectionIdentifier, bucketConfig, retryStrategy, id, opts.storeSemantics() == StoreSemantics.INSERT, opts.storeSemantics() == StoreSemantics.UPSERT, opts.storeSemantics() == StoreSemantics.REVIVE, opts.accessDeleted(), opts.createAsDeleted(), commands, expiry, opts.preserveExpiry(), opts.cas(), opts.durabilityLevel(), span);
request.context().clientContext(opts.clientContext()).encodeLatency(end - start);
final CompletableFuture<SubdocMutateRequest> future = new CompletableFuture<>();
future.complete(request);
return future;
});
}
use of com.couchbase.client.core.cnc.RequestSpan in project couchbase-jvm-clients by couchbase.
the class AsyncCollection method removeRequest.
/**
* Helper method to create the remove request.
*
* @param id the id of the document to remove.
* @param opts custom options to change the default behavior.
* @return the remove request.
*/
RemoveRequest removeRequest(final String id, final RemoveOptions.Built opts) {
notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
Duration timeout = decideKvTimeout(opts, environment.timeoutConfig());
RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
final RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_REMOVE, opts.parentSpan().orElse(null));
RemoveRequest request = new RemoveRequest(id, opts.cas(), timeout, coreContext, collectionIdentifier, retryStrategy, opts.durabilityLevel(), span);
request.context().clientContext(opts.clientContext());
return request;
}
Aggregations