use of com.couchbase.client.core.msg.kv.SubdocGetRequest 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.msg.kv.SubdocGetRequest in project couchbase-jvm-clients by couchbase.
the class ReactiveCollection method lookupIn.
/**
* Performs lookups to document fragments with custom options.
*
* @param id the outer document ID.
* @param specs the spec which specifies the type of lookups to perform.
* @param options custom options to modify the lookup options.
* @return the {@link LookupInResult} once the lookup has been performed or failed.
*/
public Mono<LookupInResult> lookupIn(final String id, List<LookupInSpec> specs, final LookupInOptions options) {
return Mono.defer(() -> {
notNull(options, "LookupInOptions", () -> ReducedKeyValueErrorContext.create(id, asyncCollection.collectionIdentifier()));
LookupInOptions.Built opts = options.build();
JsonSerializer serializer = opts.serializer() == null ? environment().jsonSerializer() : opts.serializer();
SubdocGetRequest request = asyncCollection.lookupInRequest(id, specs, opts);
return Reactor.wrap(request, LookupInAccessor.lookupInAccessor(core, request, serializer), true);
});
}
use of com.couchbase.client.core.msg.kv.SubdocGetRequest in project couchbase-jvm-clients by couchbase.
the class SubDocumentGetIntegrationTest method checkExpectedFailure.
/**
* Perform subdoc operations and assert the result is the expected exception
*/
private void checkExpectedFailure(String input, List<SubdocGetRequest.Command> commands, Class<?> expected) {
String id = UUID.randomUUID().toString();
insertContent(id, input);
SubdocGetRequest request = new SubdocGetRequest(kvTimeout, core.context(), CollectionIdentifier.fromDefault(config().bucketname()), env.retryStrategy(), id, (byte) 0, commands, null);
core.send(request);
SubdocGetResponse response = null;
try {
response = request.response().get();
} catch (InterruptedException | ExecutionException e) {
fail("Failed with " + e);
}
assertFalse(response.status().success());
assertEquals(ResponseStatus.SUBDOC_FAILURE, response.status());
assertTrue(response.error().isPresent());
CouchbaseException err = response.error().get();
assertTrue(expected.isInstance(err));
}
use of com.couchbase.client.core.msg.kv.SubdocGetRequest in project couchbase-jvm-clients by couchbase.
the class SubDocumentGetIntegrationTest method checkExpectedSuccess.
/**
* Perform subdoc operations and check the overall result was success
*/
private SubdocGetResponse checkExpectedSuccess(String input, List<SubdocGetRequest.Command> commands) {
String id = UUID.randomUUID().toString();
insertContent(id, input);
SubdocGetRequest request = new SubdocGetRequest(kvTimeout, core.context(), CollectionIdentifier.fromDefault(config().bucketname()), env.retryStrategy(), id, (byte) 0, commands, null);
core.send(request);
SubdocGetResponse response = null;
try {
response = request.response().get();
} catch (InterruptedException | ExecutionException e) {
fail("Failed with " + e);
}
assertTrue(response.status().success());
assertFalse(response.error().isPresent());
return response;
}
use of com.couchbase.client.core.msg.kv.SubdocGetRequest in project couchbase-jvm-clients by couchbase.
the class AsyncCollection method lookupInRequest.
/**
* Helper method to create the underlying lookup subdoc request.
*
* @param id the outer document ID.
* @param specs the spec which specifies the type of lookups to perform.
* @param opts custom options to modify the lookup options.
* @return the subdoc lookup request.
*/
SubdocGetRequest lookupInRequest(final String id, final List<LookupInSpec> specs, final LookupInOptions.Built opts) {
notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
notNullOrEmpty(specs, "LookupInSpecs", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
ArrayList<SubdocGetRequest.Command> commands = new ArrayList<>(specs.size());
for (int i = 0; i < specs.size(); i++) {
LookupInSpec spec = specs.get(i);
commands.add(spec.export(i));
}
// xattrs come first
commands.sort(Comparator.comparing(v -> !v.xattr()));
Duration timeout = opts.timeout().orElse(environment.timeoutConfig().kvTimeout());
RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
byte flags = 0;
if (opts.accessDeleted()) {
flags |= SubdocMutateRequest.SUBDOC_DOC_FLAG_ACCESS_DELETED;
}
RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_LOOKUP_IN, opts.parentSpan().orElse(null));
SubdocGetRequest request = new SubdocGetRequest(timeout, coreContext, collectionIdentifier, retryStrategy, id, flags, commands, span);
request.context().clientContext(opts.clientContext());
return request;
}
Aggregations