Skip to content

Commit e1b0ea9

Browse files
authored
Reinstate the getDefaultConsistency() method in the Configuration. (#1249)
Closes #1243.
1 parent f022181 commit e1b0ea9

12 files changed

+230
-31
lines changed

src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.couchbase.client.java.env.ClusterEnvironment;
5858
import com.couchbase.client.java.json.JacksonTransformers;
5959
import com.couchbase.client.java.json.JsonValueModule;
60+
import com.couchbase.client.java.query.QueryScanConsistency;
6061
import com.fasterxml.jackson.databind.ObjectMapper;
6162

6263
/**
@@ -156,7 +157,8 @@ protected void configureEnvironment(final ClusterEnvironment.Builder builder) {
156157
@Bean(name = BeanNames.COUCHBASE_TEMPLATE)
157158
public CouchbaseTemplate couchbaseTemplate(CouchbaseClientFactory couchbaseClientFactory,
158159
MappingCouchbaseConverter mappingCouchbaseConverter, TranslationService couchbaseTranslationService) {
159-
return new CouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter, couchbaseTranslationService);
160+
return new CouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter, couchbaseTranslationService,
161+
getDefaultConsistency());
160162
}
161163

162164
public CouchbaseTemplate couchbaseTemplate(CouchbaseClientFactory couchbaseClientFactory,
@@ -167,8 +169,8 @@ public CouchbaseTemplate couchbaseTemplate(CouchbaseClientFactory couchbaseClien
167169
@Bean(name = BeanNames.REACTIVE_COUCHBASE_TEMPLATE)
168170
public ReactiveCouchbaseTemplate reactiveCouchbaseTemplate(CouchbaseClientFactory couchbaseClientFactory,
169171
MappingCouchbaseConverter mappingCouchbaseConverter, TranslationService couchbaseTranslationService) {
170-
return new ReactiveCouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter,
171-
couchbaseTranslationService);
172+
return new ReactiveCouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter, couchbaseTranslationService,
173+
getDefaultConsistency());
172174
}
173175

174176
public ReactiveCouchbaseTemplate reactiveCouchbaseTemplate(CouchbaseClientFactory couchbaseClientFactory,
@@ -376,4 +378,8 @@ private boolean nonShadowedJacksonPresent() {
376378
}
377379
}
378380

381+
public QueryScanConsistency getDefaultConsistency() {
382+
return null;
383+
}
384+
379385
}

src/main/java/org/springframework/data/couchbase/core/CouchbaseOperations.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.springframework.data.couchbase.CouchbaseClientFactory;
2020
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
2121

22+
import com.couchbase.client.java.query.QueryScanConsistency;
23+
2224
/**
2325
* Defines common operations on the Couchbase data source, most commonly implemented by {@link CouchbaseTemplate}.
2426
*/
@@ -44,4 +46,8 @@ public interface CouchbaseOperations extends FluentCouchbaseOperations {
4446
*/
4547
CouchbaseClientFactory getCouchbaseClientFactory();
4648

49+
/**
50+
* Returns the default consistency to use for queries
51+
*/
52+
QueryScanConsistency getConsistency();
4753
}

src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.lang.Nullable;
3333

3434
import com.couchbase.client.java.Collection;
35+
import com.couchbase.client.java.query.QueryScanConsistency;
3536

3637
/**
3738
* Implements lower-level couchbase operations on top of the SDK with entity mapping capabilities.
@@ -49,18 +50,25 @@ public class CouchbaseTemplate implements CouchbaseOperations, ApplicationContex
4950
private final MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext;
5051
private final ReactiveCouchbaseTemplate reactiveCouchbaseTemplate;
5152
private @Nullable CouchbasePersistentEntityIndexCreator indexCreator;
53+
private QueryScanConsistency scanConsistency;
5254

5355
public CouchbaseTemplate(final CouchbaseClientFactory clientFactory, final CouchbaseConverter converter) {
5456
this(clientFactory, converter, new JacksonTranslationService());
5557
}
5658

5759
public CouchbaseTemplate(final CouchbaseClientFactory clientFactory, final CouchbaseConverter converter,
5860
final TranslationService translationService) {
61+
this(clientFactory, converter, translationService, null);
62+
}
63+
64+
public CouchbaseTemplate(final CouchbaseClientFactory clientFactory, final CouchbaseConverter converter,
65+
final TranslationService translationService, QueryScanConsistency scanConsistency) {
5966
this.clientFactory = clientFactory;
6067
this.converter = converter;
6168
this.templateSupport = new CouchbaseTemplateSupport(this, converter, translationService);
62-
this.reactiveCouchbaseTemplate = new ReactiveCouchbaseTemplate(clientFactory, converter, translationService);
63-
69+
this.reactiveCouchbaseTemplate = new ReactiveCouchbaseTemplate(clientFactory, converter, translationService,
70+
scanConsistency);
71+
this.scanConsistency = scanConsistency;
6472
this.mappingContext = this.converter.getMappingContext();
6573
if (mappingContext instanceof CouchbaseMappingContext) {
6674
CouchbaseMappingContext cmc = (CouchbaseMappingContext) mappingContext;
@@ -147,6 +155,11 @@ public CouchbaseClientFactory getCouchbaseClientFactory() {
147155
return clientFactory;
148156
}
149157

158+
@Override
159+
public QueryScanConsistency getConsistency() {
160+
return scanConsistency;
161+
}
162+
150163
/**
151164
* Provides access to a {@link Collection} on the configured {@link CouchbaseClientFactory}.
152165
*

src/main/java/org/springframework/data/couchbase/core/ReactiveCouchbaseOperations.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import org.springframework.data.couchbase.CouchbaseClientFactory;
1919
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
2020

21+
import com.couchbase.client.java.query.QueryScanConsistency;
22+
2123
/**
2224
* Defines common operations on the Couchbase data source, most commonly implemented by
2325
* {@link ReactiveCouchbaseTemplate}.
@@ -47,4 +49,8 @@ public interface ReactiveCouchbaseOperations extends ReactiveFluentCouchbaseOper
4749
*/
4850
CouchbaseClientFactory getCouchbaseClientFactory();
4951

52+
/**
53+
* @return the default consistency to use for queries
54+
*/
55+
QueryScanConsistency getConsistency();
5056
}

src/main/java/org/springframework/data/couchbase/core/ReactiveCouchbaseTemplate.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.data.couchbase.core.support.PseudoArgs;
2929

3030
import com.couchbase.client.java.Collection;
31+
import com.couchbase.client.java.query.QueryScanConsistency;
3132

3233
/**
3334
* template class for Reactive Couchbase operations
@@ -43,18 +44,25 @@ public class ReactiveCouchbaseTemplate implements ReactiveCouchbaseOperations, A
4344
private final CouchbaseConverter converter;
4445
private final PersistenceExceptionTranslator exceptionTranslator;
4546
private final ReactiveCouchbaseTemplateSupport templateSupport;
46-
private ThreadLocal<PseudoArgs<?>> threadLocalArgs = null;
47+
private ThreadLocal<PseudoArgs<?>> threadLocalArgs = new ThreadLocal<>();
48+
private QueryScanConsistency scanConsistency;
4749

4850
public ReactiveCouchbaseTemplate(final CouchbaseClientFactory clientFactory, final CouchbaseConverter converter) {
4951
this(clientFactory, converter, new JacksonTranslationService());
5052
}
5153

5254
public ReactiveCouchbaseTemplate(final CouchbaseClientFactory clientFactory, final CouchbaseConverter converter,
5355
final TranslationService translationService) {
56+
this(clientFactory, converter, translationService, null);
57+
}
58+
59+
public ReactiveCouchbaseTemplate(final CouchbaseClientFactory clientFactory, final CouchbaseConverter converter,
60+
final TranslationService translationService, QueryScanConsistency scanConsistency) {
5461
this.clientFactory = clientFactory;
5562
this.converter = converter;
5663
this.exceptionTranslator = clientFactory.getExceptionTranslator();
5764
this.templateSupport = new ReactiveCouchbaseTemplateSupport(this, converter, translationService);
65+
this.scanConsistency = scanConsistency;
5866
}
5967

6068
@Override
@@ -182,4 +190,12 @@ public void setPseudoArgs(PseudoArgs<?> threadLocalArgs) {
182190
this.threadLocalArgs.set(threadLocalArgs);
183191
}
184192

193+
/**
194+
* {@inheritDoc}
195+
*/
196+
@Override
197+
public QueryScanConsistency getConsistency() {
198+
return scanConsistency;
199+
}
200+
185201
}

src/main/java/org/springframework/data/couchbase/core/ReactiveFindByQueryOperationSupport.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ public Flux<T> all() {
206206
}));
207207
}
208208

209-
private QueryOptions buildOptions(QueryOptions options) {
210-
QueryOptions opts = query.buildQueryOptions(options, scanConsistency);
211-
return opts;
209+
public QueryOptions buildOptions(QueryOptions options) {
210+
QueryScanConsistency qsc = scanConsistency != null ? scanConsistency : template.getConsistency();
211+
return query.buildQueryOptions(options, qsc);
212212
}
213213

214214
@Override

src/main/java/org/springframework/data/couchbase/core/ReactiveRemoveByQueryOperationSupport.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public Flux<RemoveResult> all() {
9090
}
9191

9292
private QueryOptions buildQueryOptions(QueryOptions options) {
93-
return query.buildQueryOptions(options, scanConsistency);
93+
QueryScanConsistency qsc = scanConsistency != null ? scanConsistency : template.getConsistency();
94+
return query.buildQueryOptions(options, qsc);
9495
}
9596

9697
@Override

src/main/java/org/springframework/data/couchbase/core/query/OptionsBuilder.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,14 @@ public static <A extends Annotation, V> V annotationAttribute(Class<A> annotatio
381381
for (AnnotatedElement el : elements) {
382382
A an = AnnotatedElementUtils.findMergedAnnotation(el, annotation);
383383
if (an != null) {
384-
if (defaultValue != null && !defaultValue.equals(an)) {
385-
try {
386-
Method m = an.getClass().getMethod(attributeName);
387-
return (V) m.invoke(an);
388-
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
389-
throw new RuntimeException(e);
384+
try {
385+
Method m = an.getClass().getMethod(attributeName);
386+
V result = (V) m.invoke(an);
387+
if (result != null && !result.equals(defaultValue)) {
388+
return result;
390389
}
390+
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
391+
throw new RuntimeException(e);
391392
}
392393
}
393394
}

src/test/java/org/springframework/data/couchbase/domain/AirportRepository.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
// @ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
6161
public interface AirportRepository extends CouchbaseRepository<Airport, String>, DynamicProxyable<AirportRepository> {
6262

63-
// override an annotate with REQUEST_PLUS
6463
@Override
6564
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
6665
List<Airport> findAll();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2017-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.couchbase.domain;
18+
19+
import org.springframework.data.couchbase.repository.CouchbaseRepository;
20+
import org.springframework.stereotype.Repository;
21+
22+
/**
23+
* Airport repository for testing <br>
24+
*
25+
* @author Michael Reiche
26+
*/
27+
@Repository
28+
public interface AirportRepositoryScanConsistencyTest extends CouchbaseRepository<Airport, String> {
29+
30+
}
31+

src/test/java/org/springframework/data/couchbase/domain/Config.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@
1818

1919
import java.lang.reflect.InvocationTargetException;
2020

21-
import org.springframework.beans.factory.annotation.Autowired;
22-
import org.springframework.context.ApplicationContext;
2321
import org.springframework.context.annotation.Bean;
2422
import org.springframework.context.annotation.Configuration;
2523
import org.springframework.data.auditing.DateTimeProvider;
26-
import org.springframework.data.convert.CustomConversions;
2724
import org.springframework.data.couchbase.CouchbaseClientFactory;
2825
import org.springframework.data.couchbase.SimpleCouchbaseClientFactory;
2926
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
@@ -159,14 +156,16 @@ public void configureRepositoryOperationsMapping(RepositoryOperationsMapping bas
159156
// will be used instead of the result of this call (the client factory arg is different)
160157
public ReactiveCouchbaseTemplate myReactiveCouchbaseTemplate(CouchbaseClientFactory couchbaseClientFactory,
161158
MappingCouchbaseConverter mappingCouchbaseConverter) {
162-
return new ReactiveCouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter);
159+
return new ReactiveCouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter,
160+
new JacksonTranslationService(), getDefaultConsistency());
163161
}
164162

165163
// do not use couchbaseTemplate for the name of this method, otherwise the value of that been
166164
// will be used instead of the result from this call (the client factory arg is different)
167165
public CouchbaseTemplate myCouchbaseTemplate(CouchbaseClientFactory couchbaseClientFactory,
168166
MappingCouchbaseConverter mappingCouchbaseConverter) {
169-
return new CouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter);
167+
return new CouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter, new JacksonTranslationService(),
168+
getDefaultConsistency());
170169
}
171170

172171
// do not use couchbaseClientFactory for the name of this method, otherwise the value of that bean will

0 commit comments

Comments
 (0)