DATACOUCH-545 - return null if not found instead of throwing an exception
This commit is contained in:
@@ -67,7 +67,7 @@ public class ReactiveFindByAnalyticsOperationSupport implements ReactiveFindByAn
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<T> one() {
|
public Mono<T> one() {
|
||||||
return all().single();
|
return all().singleOrEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -79,8 +79,8 @@ public class ReactiveFindByAnalyticsOperationSupport implements ReactiveFindByAn
|
|||||||
public Flux<T> all() {
|
public Flux<T> all() {
|
||||||
return Flux.defer(() -> {
|
return Flux.defer(() -> {
|
||||||
String statement = assembleEntityQuery(false);
|
String statement = assembleEntityQuery(false);
|
||||||
return template.getCouchbaseClientFactory().getCluster().reactive().analyticsQuery(statement, buildAnalyticsOptions())
|
return template.getCouchbaseClientFactory().getCluster().reactive()
|
||||||
.onErrorMap(throwable -> {
|
.analyticsQuery(statement, buildAnalyticsOptions()).onErrorMap(throwable -> {
|
||||||
if (throwable instanceof RuntimeException) {
|
if (throwable instanceof RuntimeException) {
|
||||||
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
|
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
|
||||||
} else {
|
} else {
|
||||||
@@ -100,8 +100,8 @@ public class ReactiveFindByAnalyticsOperationSupport implements ReactiveFindByAn
|
|||||||
public Mono<Long> count() {
|
public Mono<Long> count() {
|
||||||
return Mono.defer(() -> {
|
return Mono.defer(() -> {
|
||||||
String statement = assembleEntityQuery(true);
|
String statement = assembleEntityQuery(true);
|
||||||
return template.getCouchbaseClientFactory().getCluster().reactive().analyticsQuery(statement, buildAnalyticsOptions())
|
return template.getCouchbaseClientFactory().getCluster().reactive()
|
||||||
.onErrorMap(throwable -> {
|
.analyticsQuery(statement, buildAnalyticsOptions()).onErrorMap(throwable -> {
|
||||||
if (throwable instanceof RuntimeException) {
|
if (throwable instanceof RuntimeException) {
|
||||||
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
|
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package org.springframework.data.couchbase.core;
|
|||||||
|
|
||||||
import static com.couchbase.client.java.kv.GetOptions.*;
|
import static com.couchbase.client.java.kv.GetOptions.*;
|
||||||
|
|
||||||
|
import com.couchbase.client.core.error.DocumentNotFoundException;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@@ -66,7 +67,14 @@ public class ReactiveFindByIdOperationSupport implements ReactiveFindByIdOperati
|
|||||||
}
|
}
|
||||||
return template.getCollection(collection).reactive().get(docId, options);
|
return template.getCollection(collection).reactive().get(docId, options);
|
||||||
}).map(result -> template.support().decodeEntity(id, result.contentAs(String.class), result.cas(), domainType))
|
}).map(result -> template.support().decodeEntity(id, result.contentAs(String.class), result.cas(), domainType))
|
||||||
.onErrorMap(throwable -> {
|
.onErrorResume(throwable -> {
|
||||||
|
if (throwable instanceof RuntimeException) {
|
||||||
|
if (throwable instanceof DocumentNotFoundException) {
|
||||||
|
return Mono.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Mono.error(throwable);
|
||||||
|
}).onErrorMap(throwable -> {
|
||||||
if (throwable instanceof RuntimeException) {
|
if (throwable instanceof RuntimeException) {
|
||||||
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
|
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -50,8 +50,8 @@ public class ReactiveFindByQueryOperationSupport implements ReactiveFindByQueryO
|
|||||||
private final Query query;
|
private final Query query;
|
||||||
private final QueryScanConsistency scanConsistency;
|
private final QueryScanConsistency scanConsistency;
|
||||||
|
|
||||||
ReactiveFindByQuerySupport(final ReactiveCouchbaseTemplate template, final Class<T> domainType,
|
ReactiveFindByQuerySupport(final ReactiveCouchbaseTemplate template, final Class<T> domainType, final Query query,
|
||||||
final Query query, final QueryScanConsistency scanConsistency) {
|
final QueryScanConsistency scanConsistency) {
|
||||||
this.template = template;
|
this.template = template;
|
||||||
this.domainType = domainType;
|
this.domainType = domainType;
|
||||||
this.query = query;
|
this.query = query;
|
||||||
@@ -70,7 +70,7 @@ public class ReactiveFindByQueryOperationSupport implements ReactiveFindByQueryO
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<T> one() {
|
public Mono<T> one() {
|
||||||
return all().single();
|
return all().singleOrEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -82,20 +82,20 @@ public class ReactiveFindByQueryOperationSupport implements ReactiveFindByQueryO
|
|||||||
public Flux<T> all() {
|
public Flux<T> all() {
|
||||||
return Flux.defer(() -> {
|
return Flux.defer(() -> {
|
||||||
String statement = assembleEntityQuery(false);
|
String statement = assembleEntityQuery(false);
|
||||||
return template.getCouchbaseClientFactory().getCluster().reactive().query(statement,
|
return template.getCouchbaseClientFactory().getCluster().reactive()
|
||||||
query.buildQueryOptions(scanConsistency)).onErrorMap(throwable -> {
|
.query(statement, query.buildQueryOptions(scanConsistency)).onErrorMap(throwable -> {
|
||||||
if (throwable instanceof RuntimeException) {
|
if (throwable instanceof RuntimeException) {
|
||||||
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
|
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
|
||||||
} else {
|
} else {
|
||||||
return throwable;
|
return throwable;
|
||||||
}
|
}
|
||||||
}).flatMapMany(ReactiveQueryResult::rowsAsObject).map(row -> {
|
}).flatMapMany(ReactiveQueryResult::rowsAsObject).map(row -> {
|
||||||
String id = row.getString(TemplateUtils.SELECT_ID);
|
String id = row.getString(TemplateUtils.SELECT_ID);
|
||||||
long cas = row.getLong(TemplateUtils.SELECT_CAS);
|
long cas = row.getLong(TemplateUtils.SELECT_CAS);
|
||||||
row.removeKey(TemplateUtils.SELECT_ID);
|
row.removeKey(TemplateUtils.SELECT_ID);
|
||||||
row.removeKey(TemplateUtils.SELECT_CAS);
|
row.removeKey(TemplateUtils.SELECT_CAS);
|
||||||
return template.support().decodeEntity(id, row.toString(), cas, domainType);
|
return template.support().decodeEntity(id, row.toString(), cas, domainType);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,15 +103,15 @@ public class ReactiveFindByQueryOperationSupport implements ReactiveFindByQueryO
|
|||||||
public Mono<Long> count() {
|
public Mono<Long> count() {
|
||||||
return Mono.defer(() -> {
|
return Mono.defer(() -> {
|
||||||
String statement = assembleEntityQuery(true);
|
String statement = assembleEntityQuery(true);
|
||||||
return template.getCouchbaseClientFactory().getCluster().reactive().query(statement,
|
return template.getCouchbaseClientFactory().getCluster().reactive()
|
||||||
query.buildQueryOptions(scanConsistency)).onErrorMap(throwable -> {
|
.query(statement, query.buildQueryOptions(scanConsistency)).onErrorMap(throwable -> {
|
||||||
if (throwable instanceof RuntimeException) {
|
if (throwable instanceof RuntimeException) {
|
||||||
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
|
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
|
||||||
} else {
|
} else {
|
||||||
return throwable;
|
return throwable;
|
||||||
}
|
}
|
||||||
}).flatMapMany(ReactiveQueryResult::rowsAsObject).map(
|
}).flatMapMany(ReactiveQueryResult::rowsAsObject).map(row -> row.getLong(TemplateUtils.SELECT_COUNT))
|
||||||
row -> row.getLong(TemplateUtils.SELECT_COUNT)).next();
|
.next();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ import org.springframework.data.couchbase.util.IgnoreWhen;
|
|||||||
|
|
||||||
import com.couchbase.client.core.error.IndexExistsException;
|
import com.couchbase.client.core.error.IndexExistsException;
|
||||||
import com.couchbase.client.java.query.QueryScanConsistency;
|
import com.couchbase.client.java.query.QueryScanConsistency;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Query tests Theses tests rely on a cb server running
|
* Query tests Theses tests rely on a cb server running
|
||||||
@@ -58,6 +59,7 @@ class CouchbaseTemplateQueryIntegrationTests extends ClusterAwareIntegrationTest
|
|||||||
|
|
||||||
private static CouchbaseClientFactory couchbaseClientFactory;
|
private static CouchbaseClientFactory couchbaseClientFactory;
|
||||||
private CouchbaseTemplate couchbaseTemplate;
|
private CouchbaseTemplate couchbaseTemplate;
|
||||||
|
private ReactiveCouchbaseTemplate reactiveCouchbaseTemplate;
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
static void beforeAll() {
|
static void beforeAll() {
|
||||||
@@ -79,6 +81,7 @@ class CouchbaseTemplateQueryIntegrationTests extends ClusterAwareIntegrationTest
|
|||||||
void beforeEach() {
|
void beforeEach() {
|
||||||
ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
|
ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
|
||||||
couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE);
|
couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE);
|
||||||
|
reactiveCouchbaseTemplate = (ReactiveCouchbaseTemplate) ac.getBean(REACTIVE_COUCHBASE_TEMPLATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -111,9 +114,17 @@ class CouchbaseTemplateQueryIntegrationTests extends ClusterAwareIntegrationTest
|
|||||||
assertEquals(auditUser, u.getLastModifiedBy());
|
assertEquals(auditUser, u.getLastModifiedBy());
|
||||||
assertEquals(auditMillis, u.getLastModifiedDate());
|
assertEquals(auditMillis, u.getLastModifiedDate());
|
||||||
}
|
}
|
||||||
|
couchbaseTemplate.findById(User.class).one(user1.getId());
|
||||||
|
reactiveCouchbaseTemplate.findById(User.class).one(user1.getId()).block();
|
||||||
} finally {
|
} finally {
|
||||||
couchbaseTemplate.removeByQuery(User.class).all();
|
couchbaseTemplate.removeByQuery(User.class).all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
User usery = couchbaseTemplate.findById(User.class).one("userx");
|
||||||
|
assertNull(usery, "usery should be null");
|
||||||
|
User userz = reactiveCouchbaseTemplate.findById(User.class).one("userx").block();
|
||||||
|
assertNull(userz, "uz should be null");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -128,8 +139,9 @@ class CouchbaseTemplateQueryIntegrationTests extends ClusterAwareIntegrationTest
|
|||||||
|
|
||||||
couchbaseTemplate.removeByQuery(User.class).consistentWith(QueryScanConsistency.REQUEST_PLUS).all();
|
couchbaseTemplate.removeByQuery(User.class).consistentWith(QueryScanConsistency.REQUEST_PLUS).all();
|
||||||
|
|
||||||
assertThrows(DataRetrievalFailureException.class, () -> couchbaseTemplate.findById(User.class).one(user1.getId()));
|
assertNull(couchbaseTemplate.findById(User.class).one(user1.getId()));
|
||||||
assertThrows(DataRetrievalFailureException.class, () -> couchbaseTemplate.findById(User.class).one(user2.getId()));
|
assertNull(couchbaseTemplate.findById(User.class).one(user2.getId()));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user