Automatically set readonly query option for read only queries (#1730)
Closes #1724
This commit is contained in:
@@ -230,7 +230,7 @@ public class ReactiveFindByQueryOperationSupport implements ReactiveFindByQueryO
|
||||
|
||||
public QueryOptions buildOptions(QueryOptions options) {
|
||||
QueryScanConsistency qsc = scanConsistency != null ? scanConsistency : template.getConsistency();
|
||||
return query.buildQueryOptions(options, qsc);
|
||||
return query.buildQueryOptions(options, qsc).readonly(query.isReadonly());
|
||||
}
|
||||
|
||||
private TransactionQueryOptions buildTransactionOptions(QueryOptions options) {
|
||||
|
||||
@@ -48,6 +48,11 @@ public class N1QLQuery extends Query {
|
||||
return query;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReadonly() {
|
||||
return options.build().readonly();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toN1qlSelectString(CouchbaseConverter template, String bucketName, String scopeName,
|
||||
String collectionName, Class domainClass, Class returnClass, boolean isCount, String[] distinctFields,
|
||||
|
||||
@@ -421,6 +421,10 @@ public class Query {
|
||||
return meta;
|
||||
}
|
||||
|
||||
public boolean isReadonly() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (!o.getClass().isAssignableFrom(getClass())) {
|
||||
return false;
|
||||
|
||||
@@ -134,6 +134,14 @@ public class StringQuery extends Query {
|
||||
return sbnqp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReadonly() {
|
||||
if (this.queryMethod.hasN1qlAnnotation()) {
|
||||
return this.queryMethod.getN1qlAnnotation().readonly();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* toN1qlRemoveString - use toN1qlSelectString
|
||||
*
|
||||
|
||||
@@ -58,4 +58,11 @@ public @interface Query {
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* Mark query as readonly
|
||||
*
|
||||
* @see com.couchbase.client.java.query.QueryOptions#readonly(boolean)
|
||||
*/
|
||||
boolean readonly() default false;
|
||||
|
||||
}
|
||||
|
||||
@@ -104,6 +104,11 @@ public class BasicQuery extends Query {
|
||||
return super.equals(that);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReadonly() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*/
|
||||
|
||||
@@ -700,7 +700,7 @@ class CouchbaseTemplateQueryCollectionIntegrationTests extends CollectionAwareIn
|
||||
@Test
|
||||
public void findByQueryOptions() { // 4
|
||||
QueryOptions options = QueryOptions.queryOptions().timeout(Duration.ofNanos(10));
|
||||
assertThrows(AmbiguousTimeoutException.class, () -> couchbaseTemplate.findByQuery(Airport.class)
|
||||
assertThrows(UnambiguousTimeoutException.class, () -> couchbaseTemplate.findByQuery(Airport.class)
|
||||
.withConsistency(REQUEST_PLUS).inScope(otherScope).inCollection(otherCollection).withOptions(options).all());
|
||||
}
|
||||
|
||||
|
||||
@@ -693,7 +693,7 @@ class ReactiveCouchbaseTemplateQueryCollectionIntegrationTests extends Collectio
|
||||
@Test
|
||||
public void findByQueryOptions() { // 4
|
||||
QueryOptions options = QueryOptions.queryOptions().timeout(Duration.ofNanos(10));
|
||||
assertThrows(AmbiguousTimeoutException.class,
|
||||
assertThrows(UnambiguousTimeoutException.class,
|
||||
() -> template.findByQuery(Airport.class).withConsistency(REQUEST_PLUS).inScope(otherScope)
|
||||
.inCollection(otherCollection).withOptions(options).all().collectList().block());
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.springframework.data.couchbase.config.BeanNames.COUCHBASE_TEMPLATE;
|
||||
|
||||
import com.couchbase.client.core.error.UnambiguousTimeoutException;
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
import junit.framework.AssertionFailedError;
|
||||
|
||||
@@ -662,7 +663,7 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr
|
||||
try {
|
||||
Airport saved = airportRepository.save(vie);
|
||||
// Duration of 1 nano-second will cause timeout
|
||||
assertThrows(AmbiguousTimeoutException.class,
|
||||
assertThrows(UnambiguousTimeoutException.class,
|
||||
() -> airportRepository.withOptions(queryOptions().timeout(Duration.ofNanos(1))).iata(vie.getIata()));
|
||||
|
||||
Airport airport3 = airportRepository
|
||||
|
||||
Reference in New Issue
Block a user