Skip to content

Fix SimpleCouchbaseRepository not taking into account the default consistency. #1256

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 10 commits into from
Oct 19, 2021
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 @@ -16,14 +16,7 @@

package org.springframework.data.couchbase.repository.support;

import static org.springframework.data.couchbase.repository.support.Util.hasNonZeroVersionProperty;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import com.couchbase.client.java.query.QueryScanConsistency;
import org.springframework.data.couchbase.core.CouchbaseOperations;
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.data.couchbase.repository.CouchbaseRepository;
Expand All @@ -36,14 +29,21 @@
import org.springframework.data.util.Streamable;
import org.springframework.util.Assert;

import com.couchbase.client.java.query.QueryScanConsistency;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.springframework.data.couchbase.repository.support.Util.hasNonZeroVersionProperty;

/**
* Repository base implementation for Couchbase.
*
* @author Michael Nitschinger
* @author Mark Paluch
* @author Jens Schauder
* @author Jonathan Massuchetti
*/
public class SimpleCouchbaseRepository<T, ID> implements CouchbaseRepository<T, ID> {

Expand Down Expand Up @@ -190,7 +190,8 @@ private List<T> findAll(Query query) {
}

private QueryScanConsistency buildQueryScanConsistency() {
QueryScanConsistency scanConsistency = QueryScanConsistency.NOT_BOUNDED;
QueryScanConsistency scanConsistency = null;

if (crudMethodMetadata.getScanConsistency() != null) {
scanConsistency = crudMethodMetadata.getScanConsistency().query();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,7 @@

package org.springframework.data.couchbase.repository.support;

import static org.springframework.data.couchbase.repository.support.Util.hasNonZeroVersionProperty;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.couchbase.client.java.query.QueryScanConsistency;
import org.reactivestreams.Publisher;
import org.springframework.data.couchbase.core.CouchbaseOperations;
import org.springframework.data.couchbase.core.ReactiveCouchbaseOperations;
Expand All @@ -34,8 +26,14 @@
import org.springframework.data.domain.Sort;
import org.springframework.data.util.Streamable;
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import com.couchbase.client.java.query.QueryScanConsistency;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static org.springframework.data.couchbase.repository.support.Util.hasNonZeroVersionProperty;

/**
* Reactive repository base implementation for Couchbase.
Expand All @@ -46,6 +44,7 @@
* @author David Kelly
* @author Douglas Six
* @author Jens Schauder
* @author Jonathan Massuchetti
* @since 3.0
*/
public class SimpleReactiveCouchbaseRepository<T, ID> implements ReactiveCouchbaseRepository<T, ID> {
Expand Down Expand Up @@ -206,7 +205,8 @@ private Flux<T> findAll(Query query) {
}

private QueryScanConsistency buildQueryScanConsistency() {
QueryScanConsistency scanConsistency = QueryScanConsistency.NOT_BOUNDED;
QueryScanConsistency scanConsistency = null;

if (crudMethodMetadata.getScanConsistency() != null) {
scanConsistency = crudMethodMetadata.getScanConsistency().query();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2017-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.data.couchbase.domain;

import org.springframework.data.couchbase.repository.CouchbaseRepository;
import org.springframework.stereotype.Repository;

/**
* Airport repository for testing default consistency<br>
*
* @author Jonathan Massuchetti
*/
@Repository
public interface AirportDefaultConsistencyRepository extends CouchbaseRepository<Airport, String> {

Airport iata(String iata);

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@
@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
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2017-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.data.couchbase.domain;

import com.couchbase.client.java.json.JsonArray;
import com.couchbase.client.java.query.QueryScanConsistency;
import org.springframework.data.couchbase.repository.Query;
import org.springframework.data.couchbase.repository.ScanConsistency;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.reactive.ReactiveSortingRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;

/**
* template class for Reactive Couchbase operations
*
* @author Jonathan Massuchetti
*/
@Repository
public interface ReactiveAirportDefaultConsistencyRepository extends ReactiveSortingRepository<Airport, String> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,9 @@

package org.springframework.data.couchbase.repository;

import static com.couchbase.client.java.query.QueryScanConsistency.NOT_BOUNDED;
import static com.couchbase.client.java.query.QueryScanConsistency.REQUEST_PLUS;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
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 java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

import com.couchbase.client.core.error.CouchbaseException;
import com.couchbase.client.core.error.IndexExistsException;
import com.couchbase.client.java.query.QueryScanConsistency;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -57,15 +36,7 @@
import org.springframework.data.couchbase.core.query.N1QLExpression;
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.data.couchbase.core.query.QueryCriteria;
import org.springframework.data.couchbase.domain.Address;
import org.springframework.data.couchbase.domain.Airport;
import org.springframework.data.couchbase.domain.AirportRepository;
import org.springframework.data.couchbase.domain.Iata;
import org.springframework.data.couchbase.domain.NaiveAuditorAware;
import org.springframework.data.couchbase.domain.Person;
import org.springframework.data.couchbase.domain.PersonRepository;
import org.springframework.data.couchbase.domain.User;
import org.springframework.data.couchbase.domain.UserRepository;
import org.springframework.data.couchbase.domain.*;
import org.springframework.data.couchbase.domain.time.AuditingDateTimeProvider;
import org.springframework.data.couchbase.repository.auditing.EnableCouchbaseAuditing;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
Expand All @@ -82,16 +53,32 @@
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import com.couchbase.client.core.error.CouchbaseException;
import com.couchbase.client.core.error.IndexExistsException;
import com.couchbase.client.java.query.QueryScanConsistency;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

import static com.couchbase.client.java.query.QueryScanConsistency.NOT_BOUNDED;
import static com.couchbase.client.java.query.QueryScanConsistency.REQUEST_PLUS;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.data.couchbase.config.BeanNames.COUCHBASE_TEMPLATE;

/**
* Repository tests
*
* @author Michael Nitschinger
* @author Michael Reiche
* @author Jens Schauder
* @author Jonathan Massuchetti
*/
@SpringJUnitConfig(CouchbaseRepositoryQueryIntegrationTests.Config.class)
@IgnoreWhen(missesCapabilities = Capabilities.QUERY, clusterTypes = ClusterType.MOCKED)
Expand All @@ -101,6 +88,9 @@ public class CouchbaseRepositoryQueryIntegrationTests extends ClusterAwareIntegr

@Autowired AirportRepository airportRepository;

@Autowired
AirportDefaultConsistencyRepository airportDefaultConsistencyRepository;

@Autowired UserRepository userRepository;

@Autowired CouchbaseTemplate couchbaseTemplate;
Expand Down Expand Up @@ -212,9 +202,9 @@ public void saveNotBounded() {
Airport airport2 = null;
for (int i = 1; i <= 100; i++) {
// set version == 0 so save() will be an upsert, not a replace
Airport saved = airportRepository.save(vie.clearVersion());
Airport saved = airportDefaultConsistencyRepository.save(vie.clearVersion());
try {
airport2 = airportRepository.iata(saved.getIata());
airport2 = airportDefaultConsistencyRepository.iata(saved.getIata());
if (airport2 == null) {
break;
}
Expand All @@ -227,14 +217,14 @@ public void saveNotBounded() {
assertEquals(vie.getId(), removeResult.getId());
assertTrue(removeResult.getCas() != 0);
assertTrue(removeResult.getMutationToken().isPresent());
Airport airport3 = airportRepository.iata(vie.getIata());
Airport airport3 = airportDefaultConsistencyRepository.iata(vie.getIata());
assertNull(airport3, "should have been removed");
}
}
assertNull(airport2, "airport2 should have likely been null at least once");
Airport saved = airportRepository.save(vie.clearVersion());
Airport saved = airportDefaultConsistencyRepository.save(vie.clearVersion());
couchbaseTemplate.findByQuery(Airport.class).withConsistency(REQUEST_PLUS).all();
airport2 = airportRepository.iata(vie.getIata());
airport2 = airportDefaultConsistencyRepository.iata(vie.getIata());
RemoveResult removeResult = couchbaseTemplate.removeById().one(saved.getId());
assertNotNull(airport2, "airport2 should have been found");
}
Expand All @@ -244,7 +234,7 @@ public void saveNotBoundedRequestPlus() {
ApplicationContext ac = new AnnotationConfigApplicationContext(ConfigRequestPlus.class);
// the Config class has been modified, these need to be loaded again
CouchbaseTemplate couchbaseTemplateRP = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE);
AirportRepository airportRepositoryRP = (AirportRepository) ac.getBean("airportRepository");
AirportDefaultConsistencyRepository airportRepositoryRP = (AirportDefaultConsistencyRepository) ac.getBean("airportDefaultConsistencyRepository");

// save() followed by query with NOT_BOUNDED will result in not finding the document
Airport vie = new Airport("airports::vie", "vie", "low9");
Expand Down Expand Up @@ -277,6 +267,28 @@ public void saveNotBoundedRequestPlus() {
assertFalse(!airports.isEmpty(), "airports should have been empty");
}

@Test
public void saveNotBoundedRequestPlusWithDefaultRepository() {
ApplicationContext ac = new AnnotationConfigApplicationContext(ConfigRequestPlus.class);
// the Config class has been modified, these need to be loaded again
CouchbaseTemplate couchbaseTemplateRP = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE);
AirportDefaultConsistencyRepository airportRepositoryRP = (AirportDefaultConsistencyRepository) ac.getBean("airportDefaultConsistencyRepository");

List<Airport> sizeBeforeTest = airportRepositoryRP.findAll();
assertEquals(0, sizeBeforeTest.size());

for (int i = 1; i <= 100; i++) {
Airport vie = new Airport("airports::vie" + i, "vie" + i, "low9");
airportRepositoryRP.save(vie);
}

List<Airport> allSaved = airportRepositoryRP.findAll();

airportRepository.deleteAll();

assertEquals(100, allSaved.size());
}

@Test
void findByTypeAlias() {
Airport vie = null;
Expand Down
Loading