Skip to content

Commit b4fac72

Browse files
committed
Only throw exception for id and cas not projected if needed. (#1395)
Closes #1394.
1 parent f6944a7 commit b4fac72

7 files changed

+33
-38
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.data.couchbase.core.mapping.event.BeforeConvertEvent;
3737
import org.springframework.data.couchbase.core.mapping.event.BeforeSaveEvent;
3838
import org.springframework.data.couchbase.core.mapping.event.CouchbaseMappingEvent;
39+
import org.springframework.data.couchbase.core.support.TemplateUtils;
3940
import org.springframework.data.couchbase.repository.support.MappingCouchbaseEntityInformation;
4041
import org.springframework.data.mapping.PersistentPropertyAccessor;
4142
import org.springframework.data.mapping.callback.EntityCallbacks;
@@ -44,6 +45,8 @@
4445
import org.springframework.util.Assert;
4546
import org.springframework.util.ClassUtils;
4647

48+
import com.couchbase.client.core.error.CouchbaseException;
49+
4750
/**
4851
* Internal encode/decode support for CouchbaseTemplate.
4952
*
@@ -84,9 +87,7 @@ public CouchbaseDocument encodeEntity(final Object entityToEncode) {
8487
}
8588

8689
@Override
87-
public <T> T decodeEntity(String id, String source, long cas, Class<T> entityClass, String scope, String collection) {
88-
final CouchbaseDocument converted = new CouchbaseDocument(id);
89-
converted.setId(id);
90+
public <T> T decodeEntity(String id, String source, Long cas, Class<T> entityClass, String scope, String collection) {
9091

9192
// this is the entity class defined for the repository. It may not be the class of the document that was read
9293
// we will reset it after reading the document
@@ -106,18 +107,32 @@ public <T> T decodeEntity(String id, String source, long cas, Class<T> entityCla
106107
// to unwrap. This results in List<String[]> being unwrapped past String[] to String, so this may also be a
107108
// Collection (or Array) of entityClass. We have no way of knowing - so just assume it is what we are told.
108109
// if this is a Collection or array, only the first element will be returned.
110+
final CouchbaseDocument converted = new CouchbaseDocument(id);
109111
Set<Map.Entry<String, Object>> set = ((CouchbaseDocument) translationService.decode(source, converted))
110112
.getContent().entrySet();
111113
return (T) set.iterator().next().getValue();
112114
}
113115

116+
if (id == null) {
117+
throw new CouchbaseException(TemplateUtils.SELECT_ID + " was null. Either use #{#n1ql.selectEntity} or project "
118+
+ TemplateUtils.SELECT_ID);
119+
}
120+
121+
final CouchbaseDocument converted = new CouchbaseDocument(id);
122+
114123
// if possible, set the version property in the source so that if the constructor has a long version argument,
115-
// it will have a value an not fail (as null is not a valid argument for a long argument). This possible failure
124+
// it will have a value and not fail (as null is not a valid argument for a long argument). This possible failure
116125
// can be avoid by defining the argument as Long instead of long.
117126
// persistentEntity is still the (possibly abstract) class specified in the repository definition
118127
// it's possible that the abstract class does not have a version property, and this won't be able to set the version
119-
if (cas != 0 && persistentEntity.getVersionProperty() != null) {
120-
converted.put(persistentEntity.getVersionProperty().getName(), cas);
128+
if (persistentEntity.getVersionProperty() != null) {
129+
if (cas == null) {
130+
throw new CouchbaseException("version/cas in the entity but " + TemplateUtils.SELECT_CAS
131+
+ " was not in result. Either use #{#n1ql.selectEntity} or project " + TemplateUtils.SELECT_CAS);
132+
}
133+
if (cas != 0) {
134+
converted.put(persistentEntity.getVersionProperty().getName(), cas);
135+
}
121136
}
122137

123138
// if the constructor has an argument that is long version, then construction will fail if the 'version'

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ 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, String scope,
43+
public <T> Mono<T> decodeEntity(String id, String source, Long cas, Class<T> entityClass, String scope,
4444
String collection) {
4545
return Mono.fromSupplier(() -> support.decodeEntity(id, source, cas, entityClass, scope, collection));
4646
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public Mono<CouchbaseDocument> encodeEntity(final Object entityToEncode) {
8484
}
8585

8686
@Override
87-
public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass, String scope,
87+
public <T> Mono<T> decodeEntity(String id, String source, Long cas, Class<T> entityClass, String scope,
8888
String collection) {
8989
return Mono.fromSupplier(() -> {
9090
final CouchbaseDocument converted = new CouchbaseDocument(id);

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.data.couchbase.core.support.TemplateUtils;
2323
import org.springframework.util.Assert;
2424

25-
import com.couchbase.client.core.error.CouchbaseException;
2625
import com.couchbase.client.java.analytics.AnalyticsOptions;
2726
import com.couchbase.client.java.analytics.AnalyticsScanConsistency;
2827
import com.couchbase.client.java.analytics.ReactiveAnalyticsResult;
@@ -117,23 +116,13 @@ public Flux<T> all() {
117116
return throwable;
118117
}
119118
}).flatMapMany(ReactiveAnalyticsResult::rowsAsObject).flatMap(row -> {
120-
String id = "";
121-
Long cas = Long.valueOf(0);
122-
if (row.getString(TemplateUtils.SELECT_ID) == null && row.getString(TemplateUtils.SELECT_ID_3x) == null) {
123-
return Flux.error(new CouchbaseException("analytics query did not project " + TemplateUtils.SELECT_ID
124-
+ ". Either use #{#n1ql.selectEntity} or project " + TemplateUtils.SELECT_ID + " and "
125-
+ TemplateUtils.SELECT_CAS + " : " + statement));
126-
}
119+
String id = null;
120+
Long cas = null;
127121
id = row.getString(TemplateUtils.SELECT_ID);
128122
if (id == null) {
129123
id = row.getString(TemplateUtils.SELECT_ID_3x);
130124
row.removeKey(TemplateUtils.SELECT_ID_3x);
131125
}
132-
if (row.getLong(TemplateUtils.SELECT_CAS) == null && row.getLong(TemplateUtils.SELECT_CAS_3x) == null) {
133-
return Flux.error(new CouchbaseException("analytics query did not project " + TemplateUtils.SELECT_CAS
134-
+ ". Either use #{#n1ql.selectEntity} or project " + TemplateUtils.SELECT_ID + " and "
135-
+ TemplateUtils.SELECT_CAS + " : " + statement));
136-
}
137126
cas = row.getLong(TemplateUtils.SELECT_CAS);
138127
if (cas == null) {
139128
cas = row.getLong(TemplateUtils.SELECT_CAS_3x);

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.springframework.data.couchbase.core.support.TemplateUtils;
2626
import org.springframework.util.Assert;
2727

28-
import com.couchbase.client.core.error.CouchbaseException;
2928
import com.couchbase.client.java.query.QueryOptions;
3029
import com.couchbase.client.java.query.QueryScanConsistency;
3130
import com.couchbase.client.java.query.ReactiveQueryResult;
@@ -184,24 +183,17 @@ public Flux<T> all() {
184183
return throwable;
185184
}
186185
}).flatMapMany(ReactiveQueryResult::rowsAsObject).flatMap(row -> {
187-
String id = "";
188-
Long cas = Long.valueOf(0);
189-
if (!query.isDistinct() && distinctFields == null) {
190-
if (row.getString(TemplateUtils.SELECT_ID) == null && row.getString(TemplateUtils.SELECT_ID_3x) == null) {
191-
return Flux.error(new CouchbaseException(
192-
"query did not project " + TemplateUtils.SELECT_ID + ". Either use #{#n1ql.selectEntity} or project "
193-
+ TemplateUtils.SELECT_ID + " and " + TemplateUtils.SELECT_CAS + " : " + statement));
194-
}
186+
String id = null;
187+
Long cas = null;
188+
if (query.isDistinct() || distinctFields != null) {
189+
id = "";
190+
cas = Long.valueOf(0);
191+
} else {
195192
id = row.getString(TemplateUtils.SELECT_ID);
196193
if (id == null) {
197194
id = row.getString(TemplateUtils.SELECT_ID_3x);
198195
row.removeKey(TemplateUtils.SELECT_ID_3x);
199196
}
200-
if (row.getLong(TemplateUtils.SELECT_CAS) == null && row.getLong(TemplateUtils.SELECT_CAS_3x) == null) {
201-
return Flux.error(new CouchbaseException(
202-
"query did not project " + TemplateUtils.SELECT_CAS + ". Either use #{#n1ql.selectEntity} or project "
203-
+ TemplateUtils.SELECT_ID + " and " + TemplateUtils.SELECT_CAS + " : " + statement));
204-
}
205197
cas = row.getLong(TemplateUtils.SELECT_CAS);
206198
if (cas == null) {
207199
cas = row.getLong(TemplateUtils.SELECT_CAS_3x);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@
2121
import org.springframework.data.couchbase.core.mapping.event.CouchbaseMappingEvent;
2222

2323
/**
24-
*
2524
* @author Michael Reiche
2625
*/
2726
public interface ReactiveTemplateSupport {
2827

2928
Mono<CouchbaseDocument> encodeEntity(Object entityToEncode);
3029

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

3332
<T> Mono<T> applyUpdatedCas(T entity, CouchbaseDocument converted, long cas);
3433

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public interface TemplateSupport {
2626

2727
CouchbaseDocument encodeEntity(Object entityToEncode);
2828

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

3131
<T> T applyUpdatedCas(T entity, CouchbaseDocument converted, long cas);
3232

0 commit comments

Comments
 (0)