Skip to content

Reinstate the getDefaultConsistency() method in the Configuration. #1244

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
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 @@ -22,6 +22,7 @@
import java.util.HashSet;
import java.util.Set;

import com.couchbase.client.java.query.QueryScanConsistency;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -157,7 +158,8 @@ protected void configureEnvironment(final ClusterEnvironment.Builder builder) {
@Bean(name = BeanNames.COUCHBASE_TEMPLATE)
public CouchbaseTemplate couchbaseTemplate(CouchbaseClientFactory couchbaseClientFactory,
MappingCouchbaseConverter mappingCouchbaseConverter, TranslationService couchbaseTranslationService) {
return new CouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter, couchbaseTranslationService);
return new CouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter, couchbaseTranslationService,
getDefaultConsistency());
}

public CouchbaseTemplate couchbaseTemplate(CouchbaseClientFactory couchbaseClientFactory,
Expand All @@ -168,8 +170,8 @@ public CouchbaseTemplate couchbaseTemplate(CouchbaseClientFactory couchbaseClien
@Bean(name = BeanNames.REACTIVE_COUCHBASE_TEMPLATE)
public ReactiveCouchbaseTemplate reactiveCouchbaseTemplate(CouchbaseClientFactory couchbaseClientFactory,
MappingCouchbaseConverter mappingCouchbaseConverter, TranslationService couchbaseTranslationService) {
return new ReactiveCouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter,
couchbaseTranslationService);
return new ReactiveCouchbaseTemplate(couchbaseClientFactory, mappingCouchbaseConverter, couchbaseTranslationService,
getDefaultConsistency());
}

public ReactiveCouchbaseTemplate reactiveCouchbaseTemplate(CouchbaseClientFactory couchbaseClientFactory,
Expand Down Expand Up @@ -379,4 +381,8 @@ private boolean nonShadowedJacksonPresent() {
}
}

public QueryScanConsistency getDefaultConsistency() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.springframework.data.couchbase.CouchbaseClientFactory;
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;

import com.couchbase.client.java.query.QueryScanConsistency;

/**
* Defines common operations on the Couchbase data source, most commonly implemented by {@link CouchbaseTemplate}.
*/
Expand All @@ -44,4 +46,8 @@ public interface CouchbaseOperations extends FluentCouchbaseOperations {
*/
CouchbaseClientFactory getCouchbaseClientFactory();

/**
* Returns the default consistency to use for queries
*/
QueryScanConsistency getConsistency();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
import org.springframework.data.couchbase.core.support.PseudoArgs;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.lang.Nullable;

import com.couchbase.client.java.Collection;
import com.couchbase.client.java.query.QueryScanConsistency;

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

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

public CouchbaseTemplate(final CouchbaseClientFactory clientFactory, final CouchbaseConverter converter,
final TranslationService translationService) {
this(clientFactory, converter, translationService, null);
}

public CouchbaseTemplate(final CouchbaseClientFactory clientFactory, final CouchbaseConverter converter,
final TranslationService translationService, QueryScanConsistency scanConsistency) {
this.clientFactory = clientFactory;
this.converter = converter;
this.templateSupport = new CouchbaseTemplateSupport(converter, translationService);
this.reactiveCouchbaseTemplate = new ReactiveCouchbaseTemplate(clientFactory, converter, translationService);

this.reactiveCouchbaseTemplate = new ReactiveCouchbaseTemplate(clientFactory, converter, translationService,
scanConsistency);
this.scanConsistency = scanConsistency;
this.mappingContext = this.converter.getMappingContext();
if (mappingContext instanceof CouchbaseMappingContext) {
CouchbaseMappingContext cmc = (CouchbaseMappingContext) mappingContext;
Expand Down Expand Up @@ -136,6 +143,11 @@ public CouchbaseClientFactory getCouchbaseClientFactory() {
return clientFactory;
}

@Override
public QueryScanConsistency getConsistency() {
return scanConsistency;
}

/**
* Provides access to a {@link Collection} on the configured {@link CouchbaseClientFactory}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
import org.springframework.data.couchbase.core.support.PseudoArgs;

import com.couchbase.client.java.query.QueryScanConsistency;

/**
* Defines common operations on the Couchbase data source, most commonly implemented by
* {@link ReactiveCouchbaseTemplate}.
Expand Down Expand Up @@ -47,8 +49,12 @@ public interface ReactiveCouchbaseOperations extends ReactiveFluentCouchbaseOper
CouchbaseClientFactory getCouchbaseClientFactory();

/**
* @@return the pseudoArgs from the ThreadLocal field of the CouchbaseOperations
* @return the pseudoArgs from the ThreadLocal field of the CouchbaseOperations
*/
PseudoArgs<?> getPseudoArgs();

/**
* @return the default consistency to use for queries
*/
QueryScanConsistency getConsistency();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.data.couchbase.core.support.PseudoArgs;

import com.couchbase.client.java.Collection;
import com.couchbase.client.java.query.QueryScanConsistency;

/**
* template class for Reactive Couchbase operations
Expand All @@ -44,17 +45,24 @@ public class ReactiveCouchbaseTemplate implements ReactiveCouchbaseOperations, A
private final PersistenceExceptionTranslator exceptionTranslator;
private final ReactiveCouchbaseTemplateSupport templateSupport;
private ThreadLocal<PseudoArgs<?>> threadLocalArgs = new ThreadLocal<>();
private QueryScanConsistency scanConsistency;

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

public ReactiveCouchbaseTemplate(final CouchbaseClientFactory clientFactory, final CouchbaseConverter converter,
final TranslationService translationService) {
this(clientFactory, converter, translationService, null);
}

public ReactiveCouchbaseTemplate(final CouchbaseClientFactory clientFactory, final CouchbaseConverter converter,
final TranslationService translationService, QueryScanConsistency scanConsistency) {
this.clientFactory = clientFactory;
this.converter = converter;
this.exceptionTranslator = clientFactory.getExceptionTranslator();
this.templateSupport = new ReactiveCouchbaseTemplateSupport(converter, translationService);
this.scanConsistency = scanConsistency;
}

@Override
Expand Down Expand Up @@ -165,4 +173,12 @@ public PseudoArgs<?> getPseudoArgs() {
return threadLocalArgs == null ? null : threadLocalArgs.get();
}

/**
* {@inheritDoc}
*/
@Override
public QueryScanConsistency getConsistency() {
return scanConsistency;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ public Flux<T> all() {

@Override
public QueryOptions buildOptions(QueryOptions options) {
QueryOptions opts = query.buildQueryOptions(options, scanConsistency);
QueryScanConsistency qsc = scanConsistency != null ? scanConsistency : template.getConsistency();
QueryOptions opts = query.buildQueryOptions(options, qsc);
return opts;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.springframework.data.couchbase.core;

import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand All @@ -26,6 +25,7 @@
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.data.couchbase.core.support.PseudoArgs;
import org.springframework.data.couchbase.core.support.TemplateUtils;
import org.springframework.util.Assert;

import com.couchbase.client.java.query.QueryOptions;
import com.couchbase.client.java.query.QueryScanConsistency;
Expand All @@ -45,8 +45,7 @@ public ReactiveRemoveByQueryOperationSupport(final ReactiveCouchbaseTemplate tem

@Override
public <T> ReactiveRemoveByQuery<T> removeByQuery(Class<T> domainType) {
return new ReactiveRemoveByQuerySupport<>(template, domainType, ALL_QUERY,null, null,
null, null);
return new ReactiveRemoveByQuerySupport<>(template, domainType, ALL_QUERY, null, null, null, null);
}

static class ReactiveRemoveByQuerySupport<T> implements ReactiveRemoveByQuery<T> {
Expand Down Expand Up @@ -94,7 +93,8 @@ public Flux<RemoveResult> all() {
}

private QueryOptions buildQueryOptions(QueryOptions options) {
return query.buildQueryOptions(options, scanConsistency);
QueryScanConsistency qsc = scanConsistency != null ? scanConsistency : template.getConsistency();
return query.buildQueryOptions(options, qsc);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
@Repository
public interface AirportRepository extends CouchbaseRepository<Airport, String> {

// NOT_BOUNDED to test ScanConsistency
// @ScanConsistency(query = QueryScanConsistency.NOT_BOUNDED)
Airport iata(String iata);

@Override
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
List<Airport> findAll();
Expand Down
Loading