Skip to content

Commit 2e49741

Browse files
committed
Add collections support to N1qlJoin. (#1333)
The scope for the entity can come from an option, a method annotation, an annotation on the repository or an annotation on the entity class. All these possibilities are handle by PseudoArgs in the OperationSupport implementation. That scope/collection are passed into decodeEntity(). The scope/collection of the child can only come from an annotation on the entity class. The scope/collection of the parent and child are uses as follows: 1) Both the parent and the chold have non-default collections It's possible that the scope for the parent was set with an annotation on a repository method, the entity class or the repository class or a query option. Since there is no means to set the scope of the child class by the method, repository class or query option (only the annotation) we assume that the (possibly) dynamic scope of the entity would be a better choice as it is logical to put collections to be joined in the same scope. 2) The parent has a collection (and therefore a scope as well), but the child does not have a collection. Use the lhScope and lhCollection for the entity. The child is just the bucket. 3) The parent does not have a collection (or scope), but child does have a collection. Using the same (default) scope for the child would mean specifying a non-default collection in a default scope - which is not allowed. So use the scope and collection from the child class. 4) Neither have collections, just use the bucket. Closes #1325.
1 parent b04b95a commit 2e49741

20 files changed

+528
-47
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public CouchbaseDocument encodeEntity(final Object entityToEncode) {
8585
}
8686

8787
@Override
88-
public <T> T decodeEntity(String id, String source, long cas, Class<T> entityClass) {
88+
public <T> T decodeEntity(String id, String source, long cas, Class<T> entityClass, String scope, String collection) {
8989
final CouchbaseDocument converted = new CouchbaseDocument(id);
9090
converted.setId(id);
9191
CouchbasePersistentEntity persistentEntity = couldBePersistentEntity(entityClass);
@@ -110,9 +110,10 @@ public <T> T decodeEntity(String id, String source, long cas, Class<T> entityCla
110110
if (persistentEntity.getVersionProperty() != null) {
111111
accessor.setProperty(persistentEntity.getVersionProperty(), cas);
112112
}
113-
N1qlJoinResolver.handleProperties(persistentEntity, accessor, template.reactive(), id);
113+
N1qlJoinResolver.handleProperties(persistentEntity, accessor, template.reactive(), id, scope, collection);
114114
return accessor.getBean();
115115
}
116+
116117
CouchbasePersistentEntity couldBePersistentEntity(Class<?> entityClass) {
117118
if (ClassUtils.isPrimitiveOrWrapper(entityClass) || entityClass == String.class) {
118119
return null;

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 the original author or authors.
2+
* Copyright 2021-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,8 +40,9 @@ public Mono<CouchbaseDocument> encodeEntity(Object entityToEncode) {
4040
}
4141

4242
@Override
43-
public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass) {
44-
return Mono.fromSupplier(() -> support.decodeEntity(id, source, cas, entityClass));
43+
public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass, String scope,
44+
String collection) {
45+
return Mono.fromSupplier(() -> support.decodeEntity(id, source, cas, entityClass, scope, collection));
4546
}
4647

4748
@Override

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public Mono<CouchbaseDocument> encodeEntity(final Object entityToEncode) {
8585
}
8686

8787
@Override
88-
public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass) {
88+
public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass, String scope,
89+
String collection) {
8990
return Mono.fromSupplier(() -> {
9091
final CouchbaseDocument converted = new CouchbaseDocument(id);
9192
converted.setId(id);
@@ -112,7 +113,7 @@ public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> ent
112113
if (persistentEntity.getVersionProperty() != null) {
113114
accessor.setProperty(persistentEntity.getVersionProperty(), cas);
114115
}
115-
N1qlJoinResolver.handleProperties(persistentEntity, accessor, template, id);
116+
N1qlJoinResolver.handleProperties(persistentEntity, accessor, template, id, scope, collection);
116117
return accessor.getBean();
117118
});
118119
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -133,7 +133,7 @@ public Flux<T> all() {
133133
cas = row.getLong(TemplateUtils.SELECT_CAS);
134134
row.removeKey(TemplateUtils.SELECT_ID);
135135
row.removeKey(TemplateUtils.SELECT_CAS);
136-
return support.decodeEntity(id, row.toString(), cas, returnType);
136+
return support.decodeEntity(id, row.toString(), cas, returnType, null, null);
137137
});
138138
});
139139
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -90,8 +90,8 @@ public Mono<T> one(final String id) {
9090
} else {
9191
return reactive.get(docId, (GetOptions) pArgs.getOptions());
9292
}
93-
}).flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), domainType))
94-
.onErrorResume(throwable -> {
93+
}).flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), domainType,
94+
pArgs.getScope(), pArgs.getCollection())).onErrorResume(throwable -> {
9595
if (throwable instanceof RuntimeException) {
9696
if (throwable instanceof DocumentNotFoundException) {
9797
return Mono.empty();

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -202,7 +202,7 @@ public Flux<T> all() {
202202
row.removeKey(TemplateUtils.SELECT_ID);
203203
row.removeKey(TemplateUtils.SELECT_CAS);
204204
}
205-
return support.decodeEntity(id, row.toString(), cas, returnType);
205+
return support.decodeEntity(id, row.toString(), cas, returnType, pArgs.getScope(), pArgs.getCollection());
206206
}));
207207
}
208208

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -77,7 +77,8 @@ public Mono<T> any(final String id) {
7777
return Mono.just(id)
7878
.flatMap(docId -> template.getCouchbaseClientFactory().withScope(pArgs.getScope())
7979
.getCollection(pArgs.getCollection()).reactive().getAnyReplica(docId, pArgs.getOptions()))
80-
.flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), returnType))
80+
.flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), returnType,
81+
pArgs.getScope(), pArgs.getCollection()))
8182
.onErrorMap(throwable -> {
8283
if (throwable instanceof RuntimeException) {
8384
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 the original author or authors
2+
* Copyright 2021-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,11 +20,15 @@
2020
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
2121
import org.springframework.data.couchbase.core.mapping.event.CouchbaseMappingEvent;
2222

23+
/**
24+
*
25+
* @author Michael Reiche
26+
*/
2327
public interface ReactiveTemplateSupport {
2428

2529
Mono<CouchbaseDocument> encodeEntity(Object entityToEncode);
2630

27-
<T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass);
31+
<T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass, String scope, String collection);
2832

2933
<T> Mono<T> applyUpdatedCas(T entity, CouchbaseDocument converted, long cas);
3034

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 the original author or authors
2+
* Copyright 2021-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,11 +18,15 @@
1818
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
1919
import org.springframework.data.couchbase.core.mapping.event.CouchbaseMappingEvent;
2020

21+
/**
22+
*
23+
* @author Michael Reiche
24+
*/
2125
public interface TemplateSupport {
2226

2327
CouchbaseDocument encodeEntity(Object entityToEncode);
2428

25-
<T> T decodeEntity(String id, String source, long cas, Class<T> entityClass);
29+
<T> T decodeEntity(String id, String source, long cas, Class<T> entityClass, String scope, String collection);
2630

2731
<T> T applyUpdatedCas(T entity, CouchbaseDocument converted, long cas);
2832

0 commit comments

Comments
 (0)