DATACOUCH-538 - Add scan consistency to analytics query template
This commit is contained in:
@@ -19,6 +19,8 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.couchbase.client.java.analytics.AnalyticsScanConsistency;
|
||||
import com.couchbase.client.java.query.QueryScanConsistency;
|
||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
||||
import org.springframework.data.couchbase.core.query.AnalyticsQuery;
|
||||
import org.springframework.lang.Nullable;
|
||||
@@ -112,6 +114,17 @@ public interface ExecutableFindByAnalyticsOperation {
|
||||
|
||||
}
|
||||
|
||||
interface ExecutableFindByAnalytics<T> extends FindByAnalyticsWithQuery<T> {}
|
||||
interface FindByAnalyticsConsistentWith<T> extends FindByAnalyticsWithQuery<T> {
|
||||
|
||||
/**
|
||||
* Allows to override the default scan consistency.
|
||||
*
|
||||
* @param scanConsistency the custom scan consistency to use for this analytics query.
|
||||
*/
|
||||
FindByAnalyticsWithQuery<T> consistentWith(AnalyticsScanConsistency scanConsistency);
|
||||
|
||||
}
|
||||
|
||||
interface ExecutableFindByAnalytics<T> extends FindByAnalyticsConsistentWith<T> {}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,7 +18,10 @@ package org.springframework.data.couchbase.core;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.couchbase.client.java.analytics.AnalyticsScanConsistency;
|
||||
import com.couchbase.client.java.query.QueryScanConsistency;
|
||||
import org.springframework.data.couchbase.core.query.AnalyticsQuery;
|
||||
import org.springframework.data.couchbase.core.query.Query;
|
||||
|
||||
public class ExecutableFindByAnalyticsOperationSupport implements ExecutableFindByAnalyticsOperation {
|
||||
|
||||
@@ -32,7 +35,7 @@ public class ExecutableFindByAnalyticsOperationSupport implements ExecutableFind
|
||||
|
||||
@Override
|
||||
public <T> ExecutableFindByAnalytics<T> findByAnalytics(final Class<T> domainType) {
|
||||
return new ExecutableFindByAnalyticsSupport<>(template, domainType, ALL_QUERY);
|
||||
return new ExecutableFindByAnalyticsSupport<>(template, domainType, ALL_QUERY, AnalyticsScanConsistency.NOT_BOUNDED);
|
||||
}
|
||||
|
||||
static class ExecutableFindByAnalyticsSupport<T> implements ExecutableFindByAnalytics<T> {
|
||||
@@ -40,13 +43,17 @@ public class ExecutableFindByAnalyticsOperationSupport implements ExecutableFind
|
||||
private final CouchbaseTemplate template;
|
||||
private final Class<T> domainType;
|
||||
private final ReactiveFindByAnalyticsOperationSupport.ReactiveFindByAnalyticsSupport<T> reactiveSupport;
|
||||
private final AnalyticsQuery query;
|
||||
private final AnalyticsScanConsistency scanConsistency;
|
||||
|
||||
ExecutableFindByAnalyticsSupport(final CouchbaseTemplate template, final Class<T> domainType,
|
||||
final AnalyticsQuery query) {
|
||||
final AnalyticsQuery query, final AnalyticsScanConsistency scanConsistency) {
|
||||
this.template = template;
|
||||
this.domainType = domainType;
|
||||
this.query = query;
|
||||
this.reactiveSupport = new ReactiveFindByAnalyticsOperationSupport.ReactiveFindByAnalyticsSupport<>(
|
||||
template.reactive(), domainType, query);
|
||||
template.reactive(), domainType, query, scanConsistency);
|
||||
this.scanConsistency = scanConsistency;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -66,7 +73,12 @@ public class ExecutableFindByAnalyticsOperationSupport implements ExecutableFind
|
||||
|
||||
@Override
|
||||
public TerminatingFindByAnalytics<T> matching(final AnalyticsQuery query) {
|
||||
return new ExecutableFindByAnalyticsSupport<>(template, domainType, query);
|
||||
return new ExecutableFindByAnalyticsSupport<>(template, domainType, query, scanConsistency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FindByAnalyticsWithQuery<T> consistentWith(final AnalyticsScanConsistency scanConsistency) {
|
||||
return new ExecutableFindByAnalyticsSupport<>(template, domainType, query, scanConsistency);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -50,7 +50,7 @@ public class ExecutableFindByQueryOperationSupport implements ExecutableFindByQu
|
||||
this.template = template;
|
||||
this.domainType = domainType;
|
||||
this.query = query;
|
||||
this.reactiveSupport = new ReactiveFindByQueryOperationSupport.ReactiveFindByQuerySupport<T>(template.reactive(),
|
||||
this.reactiveSupport = new ReactiveFindByQueryOperationSupport.ReactiveFindByQuerySupport<>(template.reactive(),
|
||||
domainType, query, scanConsistency);
|
||||
this.scanConsistency = scanConsistency;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.couchbase.core;
|
||||
|
||||
import com.couchbase.client.java.analytics.AnalyticsScanConsistency;
|
||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -87,6 +88,17 @@ public interface ReactiveFindByAnalyticsOperation {
|
||||
|
||||
}
|
||||
|
||||
interface ReactiveFindByAnalytics<T> extends FindByAnalyticsWithQuery<T> {}
|
||||
interface FindByAnalyticsConsistentWith<T> extends FindByAnalyticsWithQuery<T> {
|
||||
|
||||
/**
|
||||
* Allows to override the default scan consistency.
|
||||
*
|
||||
* @param scanConsistency the custom scan consistency to use for this analytics query.
|
||||
*/
|
||||
FindByAnalyticsWithQuery<T> consistentWith(AnalyticsScanConsistency scanConsistency);
|
||||
|
||||
}
|
||||
|
||||
interface ReactiveFindByAnalytics<T> extends FindByAnalyticsConsistentWith<T> {}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package org.springframework.data.couchbase.core;
|
||||
|
||||
import com.couchbase.client.java.analytics.AnalyticsOptions;
|
||||
import com.couchbase.client.java.analytics.AnalyticsScanConsistency;
|
||||
import com.couchbase.client.java.query.QueryOptions;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@@ -34,7 +37,7 @@ public class ReactiveFindByAnalyticsOperationSupport implements ReactiveFindByAn
|
||||
|
||||
@Override
|
||||
public <T> ReactiveFindByAnalytics<T> findByAnalytics(final Class<T> domainType) {
|
||||
return new ReactiveFindByAnalyticsSupport<>(template, domainType, ALL_QUERY);
|
||||
return new ReactiveFindByAnalyticsSupport<>(template, domainType, ALL_QUERY, AnalyticsScanConsistency.NOT_BOUNDED);
|
||||
}
|
||||
|
||||
static class ReactiveFindByAnalyticsSupport<T> implements ReactiveFindByAnalytics<T> {
|
||||
@@ -42,17 +45,24 @@ public class ReactiveFindByAnalyticsOperationSupport implements ReactiveFindByAn
|
||||
private final ReactiveCouchbaseTemplate template;
|
||||
private final Class<T> domainType;
|
||||
private final AnalyticsQuery query;
|
||||
private final AnalyticsScanConsistency scanConsistency;
|
||||
|
||||
ReactiveFindByAnalyticsSupport(final ReactiveCouchbaseTemplate template, final Class<T> domainType,
|
||||
final AnalyticsQuery query) {
|
||||
final AnalyticsQuery query, final AnalyticsScanConsistency scanConsistency) {
|
||||
this.template = template;
|
||||
this.domainType = domainType;
|
||||
this.query = query;
|
||||
this.scanConsistency = scanConsistency;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TerminatingFindByAnalytics<T> matching(AnalyticsQuery query) {
|
||||
return new ReactiveFindByAnalyticsSupport<>(template, domainType, query);
|
||||
return new ReactiveFindByAnalyticsSupport<>(template, domainType, query, scanConsistency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FindByAnalyticsWithQuery<T> consistentWith(AnalyticsScanConsistency scanConsistency) {
|
||||
return new ReactiveFindByAnalyticsSupport<>(template, domainType, query, scanConsistency);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,7 +79,7 @@ public class ReactiveFindByAnalyticsOperationSupport implements ReactiveFindByAn
|
||||
public Flux<T> all() {
|
||||
return Flux.defer(() -> {
|
||||
String statement = assembleEntityQuery(false);
|
||||
return template.getCouchbaseClientFactory().getCluster().reactive().analyticsQuery(statement)
|
||||
return template.getCouchbaseClientFactory().getCluster().reactive().analyticsQuery(statement, buildAnalyticsOptions())
|
||||
.onErrorMap(throwable -> {
|
||||
if (throwable instanceof RuntimeException) {
|
||||
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
|
||||
@@ -90,7 +100,7 @@ public class ReactiveFindByAnalyticsOperationSupport implements ReactiveFindByAn
|
||||
public Mono<Long> count() {
|
||||
return Mono.defer(() -> {
|
||||
String statement = assembleEntityQuery(true);
|
||||
return template.getCouchbaseClientFactory().getCluster().reactive().analyticsQuery(statement)
|
||||
return template.getCouchbaseClientFactory().getCluster().reactive().analyticsQuery(statement, buildAnalyticsOptions())
|
||||
.onErrorMap(throwable -> {
|
||||
if (throwable instanceof RuntimeException) {
|
||||
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
|
||||
@@ -123,6 +133,14 @@ public class ReactiveFindByAnalyticsOperationSupport implements ReactiveFindByAn
|
||||
query.appendSkipAndLimit(statement);
|
||||
return statement.toString();
|
||||
}
|
||||
|
||||
private AnalyticsOptions buildAnalyticsOptions() {
|
||||
final AnalyticsOptions options = AnalyticsOptions.analyticsOptions();
|
||||
if (scanConsistency != null) {
|
||||
options.scanConsistency(scanConsistency);
|
||||
}
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user