Skip to content

Automatically set readonly query option for read only queries #1730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public Flux<T> all() {

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public JsonObject n1ql() {
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@ public Meta getMeta() {
return meta;
}

public boolean isReadonly() {
return true;
}

public boolean equals(Object o) {
if (!o.getClass().isAssignableFrom(getClass())) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ scopeName, collectionName, converter, typeKey, typeValue, parameterAccessor, new
return sbnqp;
}

@Override
public boolean isReadonly() {
if (this.queryMethod.hasN1qlAnnotation()) {
return this.queryMethod.getN1qlAnnotation().readonly();
}
return false;
}

/**
* toN1qlRemoveString - use toN1qlSelectString
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@
*/
String value() default "";

/**
* Mark query as readonly
*
* @see com.couchbase.client.java.query.QueryOptions#readonly(boolean)
*/
boolean readonly() default false;

}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ private boolean querySettingsEquals(BasicQuery that) {
return super.equals(that);
}

@Override
public boolean isReadonly() {
return true;
}

/*
* (non-Javadoc)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ public void findByIdOptions() { // 3
@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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ public void findByIdOptions() { // 3
@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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;

Expand Down Expand Up @@ -644,7 +645,7 @@ void findBySimplePropertyWithOptions() {
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
Expand Down