Skip to content

Commit

Permalink
JCBC-1921: Make sure buildDeferredIndexes works aginst 6.0
Browse files Browse the repository at this point in the history
This changeset splits up the deferred building again into two
individual calls since earlier query versions do not support
nested statements when building deferred indexes.

Change-Id: I789162b256e4d980e8522a93a4c7678de70f3b31
Reviewed-on: https://review.couchbase.org/c/couchbase-jvm-clients/+/171081
Tested-by: Build Bot <build@couchbase.com>
Reviewed-by: Michael Reiche <michael.reiche@couchbase.com>
  • Loading branch information
daschl authored and Michael Nitschinger committed Feb 23, 2022
1 parent 8fb5d5d commit da1786c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,7 @@ void buildTwoDeferredIndexes() {

@Test
void buildDeferredIndexOnAbsentBucket() {
assertThrows(
IndexFailureException.class,
() -> indexes.buildDeferredIndexes("noSuchBucket", enrich(buildDeferredQueryIndexesOptions()))
);
indexes.buildDeferredIndexes("noSuchBucket", enrich(buildDeferredQueryIndexesOptions()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void buildTwoDeferredIndexes() {

@Test
void buildDeferredIndexOnAbsentBucket() {
assertThrows(IndexFailureException.class, () -> indexes.buildDeferredIndexes("noSuchBucket"));
indexes.buildDeferredIndexes("noSuchBucket");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.couchbase.client.java.manager.query;

import com.couchbase.client.core.Reactor;
import com.couchbase.client.core.annotation.Stability;
import com.couchbase.client.core.cnc.RequestSpan;
import com.couchbase.client.core.cnc.TracingIdentifiers;
Expand Down Expand Up @@ -409,33 +410,38 @@ public CompletableFuture<Void> buildDeferredIndexes(final String bucketName, fin
notNull(options, "Options");
final BuildQueryIndexOptions.Built builtOpts = options.build();

String statement;
JsonArray parameters;
if (builtOpts.collectionName().isPresent() && builtOpts.scopeName().isPresent()) {
String keyspace = buildKeyspace(bucketName, builtOpts.scopeName(), builtOpts.collectionName());

statement = "BUILD INDEX ON " + keyspace + " (" +
" (" +
" SELECT RAW name FROM system:indexes " +
" WHERE bucket_id = ?" +
" AND scope_id = ?" +
" AND keyspace_id = ?" +
" AND state = \"deferred\"" +
" )" +
")";
parameters = JsonArray.from(bucketName, builtOpts.scopeName().get(), builtOpts.collectionName().get());
} else {
statement = "BUILD INDEX ON `" + bucketName + "` (" +
" (" +
" SELECT RAW name FROM system:indexes " +
" WHERE keyspace_id = ? AND bucket_id IS MISSING AND state = \"deferred\"" +
" )" +
")";
parameters = JsonArray.from(bucketName);
}

return exec(WRITE, statement, builtOpts, TracingIdentifiers.SPAN_REQUEST_MQ_BUILD_DEFERRED_INDEXES, bucketName, parameters)
.thenApply(result -> null);
GetAllQueryIndexesOptions getAllOptions = getAllQueryIndexesOptions();
builtOpts.collectionName().ifPresent(getAllOptions::collectionName);
builtOpts.scopeName().ifPresent(getAllOptions::scopeName);
builtOpts.timeout().ifPresent(getAllOptions::timeout);

return Reactor
.toMono(() -> getAllIndexes(bucketName, getAllOptions))
.map(indexes -> indexes
.stream()
.filter(idx -> idx.state().equals("deferred"))
.map(idx -> quote(idx.name()))
.collect(Collectors.toList())
)
.flatMap(indexNames -> {
if (indexNames.isEmpty()) {
return Mono.empty();
}

String keyspace = builtOpts.collectionName().isPresent() && builtOpts.scopeName().isPresent()
? buildKeyspace(bucketName, builtOpts.scopeName(), builtOpts.collectionName())
: quote(bucketName);

String statement = "BUILD INDEX ON " + keyspace + " (" + String.join(",", indexNames) + ")";

return Reactor.toMono(
() -> exec(WRITE, statement, builtOpts, TracingIdentifiers.SPAN_REQUEST_MQ_BUILD_DEFERRED_INDEXES, bucketName, null)
.thenApply(result -> null)
);
})
.then()
.toFuture();
}

/**
Expand Down

0 comments on commit da1786c

Please sign in to comment.