@@ -41,7 +41,6 @@ public class ReactiveFindByQueryOperationSupport implements ReactiveFindByQueryO
4141 private static final Query ALL_QUERY = new Query ();
4242
4343 private final ReactiveCouchbaseTemplate template ;
44-
4544 private static final Logger LOG = LoggerFactory .getLogger (ReactiveFindByQueryOperationSupport .class );
4645
4746 public ReactiveFindByQueryOperationSupport (final ReactiveCouchbaseTemplate template ) {
@@ -62,7 +61,7 @@ static class ReactiveFindByQuerySupport<T> implements ReactiveFindByQuery<T> {
6261 private final Query query ;
6362 private final QueryScanConsistency scanConsistency ;
6463 private final String collection ;
65- private String scope ;
64+ private final String scope ;
6665 private final String [] distinctFields ;
6766 private final QueryOptions options ;
6867 private final ReactiveTemplateSupport support ;
@@ -138,10 +137,14 @@ public <R> FindByQueryWithConsistency<R> as(Class<R> returnType) {
138137 }
139138
140139 @ Override
141- public FindByQueryWithDistinct <T > distinct (String [] distinctFields ) {
140+ public FindByQueryWithDistinct <T > distinct (final String [] distinctFields ) {
142141 Assert .notNull (distinctFields , "distinctFields must not be null!" );
142+ // Coming from an annotation, this cannot be null.
143+ // But a non-null but empty distinctFields means distinct on all fields
144+ // So to indicate do not use distinct, we use {"-"} from the annotation, and here we change it to null.
145+ String [] dFields = distinctFields .length == 1 && "-" .equals (distinctFields [0 ]) ? null : distinctFields ;
143146 return new ReactiveFindByQuerySupport <>(template , domainType , returnType , query , scanConsistency , scope ,
144- collection , options , distinctFields , support );
147+ collection , options , dFields , support );
145148 }
146149
147150 @ Override
@@ -156,73 +159,65 @@ public Mono<T> first() {
156159
157160 @ Override
158161 public Flux <T > all () {
159- return Flux .defer (() -> {
160- PseudoArgs <QueryOptions > pArgs = new PseudoArgs (template , scope , collection ,
161- options != null ? options : QueryOptions .queryOptions (), domainType );
162- String statement = assembleEntityQuery (false , distinctFields , pArgs .getCollection ());
163- LOG .trace ("statement: {} {}" , "findByQuery" , statement );
164- Mono <ReactiveQueryResult > allResult = pArgs .getScope () == null
165- ? template .getCouchbaseClientFactory ().getCluster ().reactive ().query (statement ,
166- buildOptions (pArgs .getOptions ()))
167- : template .getCouchbaseClientFactory ().withScope (pArgs .getScope ()).getScope ().reactive ().query (statement ,
168- buildOptions (pArgs .getOptions ()));
169- return allResult .onErrorMap (throwable -> {
170- if (throwable instanceof RuntimeException ) {
171- return template .potentiallyConvertRuntimeException ((RuntimeException ) throwable );
172- } else {
173- return throwable ;
162+ PseudoArgs <QueryOptions > pArgs = new PseudoArgs (template , scope , collection , options , domainType );
163+ String statement = assembleEntityQuery (false , distinctFields , pArgs .getCollection ());
164+ LOG .trace ("findByQuery {} statement: {}" , pArgs , statement );
165+ Mono <ReactiveQueryResult > allResult = pArgs .getScope () == null
166+ ? template .getCouchbaseClientFactory ().getCluster ().reactive ().query (statement ,
167+ buildOptions (pArgs .getOptions ()))
168+ : template .getCouchbaseClientFactory ().withScope (pArgs .getScope ()).getScope ().reactive ().query (statement ,
169+ buildOptions (pArgs .getOptions ()));
170+ return Flux .defer (() -> allResult .onErrorMap (throwable -> {
171+ if (throwable instanceof RuntimeException ) {
172+ return template .potentiallyConvertRuntimeException ((RuntimeException ) throwable );
173+ } else {
174+ return throwable ;
175+ }
176+ }).flatMapMany (ReactiveQueryResult ::rowsAsObject ).flatMap (row -> {
177+ String id = "" ;
178+ long cas = 0 ;
179+ if (distinctFields == null ) {
180+ if (row .getString (TemplateUtils .SELECT_ID ) == null ) {
181+ return Flux .error (new CouchbaseException (
182+ "query did not project " + TemplateUtils .SELECT_ID + ". Either use #{#n1ql.selectEntity} or project "
183+ + TemplateUtils .SELECT_ID + " and " + TemplateUtils .SELECT_CAS + " : " + statement ));
174184 }
175- }).flatMapMany (ReactiveQueryResult ::rowsAsObject ).flatMap (row -> {
176- String id = "" ;
177- long cas = 0 ;
178- if (distinctFields == null ) {
179- if (row .getString (TemplateUtils .SELECT_ID ) == null ) {
180- return Flux .error (new CouchbaseException (
181- "query did not project " + TemplateUtils .SELECT_ID + ". Either use #{#n1ql.selectEntity} or project "
182- + TemplateUtils .SELECT_ID + " and " + TemplateUtils .SELECT_CAS + " : " + statement ));
183- }
184- id = row .getString (TemplateUtils .SELECT_ID );
185- if (row .getLong (TemplateUtils .SELECT_CAS ) == null ) {
186- return Flux .error (new CouchbaseException (
187- "query did not project " + TemplateUtils .SELECT_CAS + ". Either use #{#n1ql.selectEntity} or project "
188- + TemplateUtils .SELECT_ID + " and " + TemplateUtils .SELECT_CAS + " : " + statement ));
189- }
190- cas = row .getLong (TemplateUtils .SELECT_CAS );
191- row .removeKey (TemplateUtils .SELECT_ID );
192- row .removeKey (TemplateUtils .SELECT_CAS );
185+ id = row .getString (TemplateUtils .SELECT_ID );
186+ if (row .getLong (TemplateUtils .SELECT_CAS ) == null ) {
187+ return Flux .error (new CouchbaseException (
188+ "query did not project " + TemplateUtils .SELECT_CAS + ". Either use #{#n1ql.selectEntity} or project "
189+ + TemplateUtils .SELECT_ID + " and " + TemplateUtils .SELECT_CAS + " : " + statement ));
193190 }
194- return support .decodeEntity (id , row .toString (), cas , returnType );
195- });
196- });
191+ cas = row .getLong (TemplateUtils .SELECT_CAS );
192+ row .removeKey (TemplateUtils .SELECT_ID );
193+ row .removeKey (TemplateUtils .SELECT_CAS );
194+ }
195+ return support .decodeEntity (id , row .toString (), cas , returnType );
196+ }));
197197 }
198198
199- @ Override
200- public QueryOptions buildOptions (QueryOptions options ) {
199+ private QueryOptions buildOptions (QueryOptions options ) {
201200 QueryOptions opts = query .buildQueryOptions (options , scanConsistency );
202201 return opts ;
203202 }
204203
205204 @ Override
206205 public Mono <Long > count () {
207- return Mono .defer (() -> {
208- PseudoArgs <QueryOptions > pArgs = new PseudoArgs (template , scope , collection , options , domainType );
209- String statement = assembleEntityQuery (true , distinctFields , pArgs .getCollection ());
210- LOG .trace ("statement: {} {}" , "findByQuery" , statement );
211- Mono <ReactiveQueryResult > countResult = pArgs .getScope () == null
212- ? template .getCouchbaseClientFactory ().getCluster ().reactive ().query (statement ,
213- buildOptions (pArgs .getOptions ()))
214- : template .getCouchbaseClientFactory ().withScope (pArgs .getScope ()).getScope ().reactive ().query (statement ,
215- buildOptions (pArgs .getOptions ()));
216- return countResult .onErrorMap (throwable -> {
217- if (throwable instanceof RuntimeException ) {
218- return template .potentiallyConvertRuntimeException ((RuntimeException ) throwable );
219- } else {
220- return throwable ;
221- }
222- }).flatMapMany (ReactiveQueryResult ::rowsAsObject ).map (row -> {
223- return row .getLong (TemplateUtils .SELECT_COUNT );
224- }).next ();
225- });
206+ PseudoArgs <QueryOptions > pArgs = new PseudoArgs (template , scope , collection , options , domainType );
207+ String statement = assembleEntityQuery (true , distinctFields , pArgs .getCollection ());
208+ LOG .trace ("findByQuery {} statement: {}" , pArgs , statement );
209+ Mono <ReactiveQueryResult > countResult = pArgs .getScope () == null
210+ ? template .getCouchbaseClientFactory ().getCluster ().reactive ().query (statement ,
211+ buildOptions (pArgs .getOptions ()))
212+ : template .getCouchbaseClientFactory ().withScope (pArgs .getScope ()).getScope ().reactive ().query (statement ,
213+ buildOptions (pArgs .getOptions ()));
214+ return Mono .defer (() -> countResult .onErrorMap (throwable -> {
215+ if (throwable instanceof RuntimeException ) {
216+ return template .potentiallyConvertRuntimeException ((RuntimeException ) throwable );
217+ } else {
218+ return throwable ;
219+ }
220+ }).flatMapMany (ReactiveQueryResult ::rowsAsObject ).map (row -> row .getLong (TemplateUtils .SELECT_COUNT )).next ());
226221 }
227222
228223 @ Override
0 commit comments