Skip to content

Commit a765e06

Browse files
committed
Merge branch 'datacouch_588_refactoring_part_1' of github.com:spring-projects/spring-data-couchbase
2 parents 6052851 + b5203c8 commit a765e06

37 files changed

+2264
-599
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ interface FindByQueryConsistentWith<T> extends FindByQueryWithQuery<T> {
130130

131131
}
132132

133-
interface ExecutableFindByQuery<T> extends FindByQueryConsistentWith<T> {}
133+
interface FindByQueryInCollection<T> extends FindByQueryConsistentWith<T> {
134+
135+
/**
136+
* Allows to override the default scan consistency.
137+
*
138+
* @param collection the collection to use for this query.
139+
*/
140+
FindByQueryInCollection<T> inCollection(String collection);
141+
142+
}
143+
144+
interface ExecutableFindByQuery<T> extends FindByQueryInCollection<T> {}
134145

135146
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
import com.couchbase.client.java.query.QueryScanConsistency;
2424
import org.springframework.data.couchbase.core.ReactiveFindByQueryOperationSupport.ReactiveFindByQuerySupport;
2525

26+
/**
27+
* {@link ExecutableFindByQueryOperation} implementations for Couchbase.
28+
*
29+
* @author Michael Nitschinger
30+
* @author Michael Reiche
31+
*/
2632
public class ExecutableFindByQueryOperationSupport implements ExecutableFindByQueryOperation {
2733

2834
private static final Query ALL_QUERY = new Query();
@@ -35,7 +41,7 @@ public ExecutableFindByQueryOperationSupport(final CouchbaseTemplate template) {
3541

3642
@Override
3743
public <T> ExecutableFindByQuery<T> findByQuery(final Class<T> domainType) {
38-
return new ExecutableFindByQuerySupport<>(template, domainType, ALL_QUERY, QueryScanConsistency.NOT_BOUNDED);
44+
return new ExecutableFindByQuerySupport<>(template, domainType, ALL_QUERY, QueryScanConsistency.NOT_BOUNDED, null);
3945
}
4046

4147
static class ExecutableFindByQuerySupport<T> implements ExecutableFindByQuery<T> {
@@ -45,14 +51,16 @@ static class ExecutableFindByQuerySupport<T> implements ExecutableFindByQuery<T>
4551
private final Query query;
4652
private final ReactiveFindByQueryOperationSupport.ReactiveFindByQuerySupport<T> reactiveSupport;
4753
private final QueryScanConsistency scanConsistency;
54+
private final String collection;
4855

4956
ExecutableFindByQuerySupport(final CouchbaseTemplate template, final Class<T> domainType, final Query query,
50-
final QueryScanConsistency scanConsistency) {
57+
final QueryScanConsistency scanConsistency, final String collection) {
5158
this.template = template;
5259
this.domainType = domainType;
5360
this.query = query;
54-
this.reactiveSupport = new ReactiveFindByQuerySupport<T>(template.reactive(), domainType, query, scanConsistency);
61+
this.reactiveSupport = new ReactiveFindByQuerySupport<T>(template.reactive(), domainType, query, scanConsistency, collection);
5562
this.scanConsistency = scanConsistency;
63+
this.collection = collection;
5664
}
5765

5866
@Override
@@ -78,12 +86,17 @@ public TerminatingFindByQuery<T> matching(final Query query) {
7886
} else {
7987
scanCons = scanConsistency;
8088
}
81-
return new ExecutableFindByQuerySupport<>(template, domainType, query, scanCons);
89+
return new ExecutableFindByQuerySupport<>(template, domainType, query, scanCons, collection);
8290
}
8391

8492
@Override
8593
public FindByQueryConsistentWith<T> consistentWith(final QueryScanConsistency scanConsistency) {
86-
return new ExecutableFindByQuerySupport<>(template, domainType, query, scanConsistency);
94+
return new ExecutableFindByQuerySupport<>(template, domainType, query, scanConsistency, collection);
95+
}
96+
97+
@Override
98+
public FindByQueryInCollection<T> inCollection(final String collection) {
99+
return new ExecutableFindByQuerySupport<>(template, domainType, query, scanConsistency, collection);
87100
}
88101

89102
@Override

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ interface RemoveByQueryConsistentWith<T> extends RemoveByQueryWithQuery<T> {
4343

4444
}
4545

46-
interface ExecutableRemoveByQuery<T> extends RemoveByQueryConsistentWith<T> {}
46+
interface RemoveByQueryInCollection<T> extends RemoveByQueryConsistentWith<T> {
47+
48+
RemoveByQueryConsistentWith<T> inCollection(String collection);
49+
50+
}
51+
52+
interface ExecutableRemoveByQuery<T> extends RemoveByQueryInCollection<T> {}
4753

4854
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public ExecutableRemoveByQueryOperationSupport(final CouchbaseTemplate template)
3333

3434
@Override
3535
public <T> ExecutableRemoveByQuery<T> removeByQuery(Class<T> domainType) {
36-
return new ExecutableRemoveByQuerySupport<>(template, domainType, ALL_QUERY, QueryScanConsistency.NOT_BOUNDED);
36+
return new ExecutableRemoveByQuerySupport<>(template, domainType, ALL_QUERY, QueryScanConsistency.NOT_BOUNDED, null);
3737
}
3838

3939
static class ExecutableRemoveByQuerySupport<T> implements ExecutableRemoveByQuery<T> {
@@ -43,15 +43,17 @@ static class ExecutableRemoveByQuerySupport<T> implements ExecutableRemoveByQuer
4343
private final Query query;
4444
private final ReactiveRemoveByQueryOperationSupport.ReactiveRemoveByQuerySupport<T> reactiveSupport;
4545
private final QueryScanConsistency scanConsistency;
46+
private final String collection;
4647

4748
ExecutableRemoveByQuerySupport(final CouchbaseTemplate template, final Class<T> domainType, final Query query,
48-
final QueryScanConsistency scanConsistency) {
49+
final QueryScanConsistency scanConsistency, String collection) {
4950
this.template = template;
5051
this.domainType = domainType;
5152
this.query = query;
5253
this.reactiveSupport = new ReactiveRemoveByQueryOperationSupport.ReactiveRemoveByQuerySupport<>(
5354
template.reactive(), domainType, query, scanConsistency);
5455
this.scanConsistency = scanConsistency;
56+
this.collection = collection;
5557
}
5658

5759
@Override
@@ -61,12 +63,16 @@ public List<RemoveResult> all() {
6163

6264
@Override
6365
public TerminatingRemoveByQuery<T> matching(final Query query) {
64-
return new ExecutableRemoveByQuerySupport<>(template, domainType, query, scanConsistency);
66+
return new ExecutableRemoveByQuerySupport<>(template, domainType, query, scanConsistency, collection);
6567
}
6668

6769
@Override
6870
public RemoveByQueryWithQuery<T> consistentWith(final QueryScanConsistency scanConsistency) {
69-
return new ExecutableRemoveByQuerySupport<>(template, domainType, query, scanConsistency);
71+
return new ExecutableRemoveByQuerySupport<>(template, domainType, query, scanConsistency, collection);
72+
}
73+
@Override
74+
public RemoveByQueryInCollection<T> inCollection(final String collection) {
75+
return new ExecutableRemoveByQuerySupport<>(template, domainType, query, scanConsistency, collection);
7076
}
7177

7278
}

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

Lines changed: 126 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@
1515
*/
1616
package org.springframework.data.couchbase.core;
1717

18-
import org.springframework.dao.IncorrectResultSizeDataAccessException;
1918
import reactor.core.publisher.Flux;
2019
import reactor.core.publisher.Mono;
2120

21+
import org.springframework.dao.IncorrectResultSizeDataAccessException;
2222
import org.springframework.data.couchbase.core.query.Query;
2323

2424
import com.couchbase.client.java.query.QueryScanConsistency;
2525

26+
/**
27+
* ReactiveFindByQueryOperation
28+
*
29+
* @author Michael Nitschinger
30+
* @author Michael Reiche
31+
*/
2632
public interface ReactiveFindByQueryOperation {
2733

2834
/**
@@ -104,6 +110,124 @@ interface FindByQueryConsistentWith<T> extends FindByQueryWithQuery<T> {
104110

105111
}
106112

107-
interface ReactiveFindByQuery<T> extends FindByQueryConsistentWith<T> {}
113+
/**
114+
* Collection override (optional).
115+
*/
116+
interface FindInCollection<T> extends FindByQueryWithQuery<T> {
117+
118+
/**
119+
* Explicitly set the name of the collection to perform the query on. <br />
120+
* Skip this step to use the default collection derived from the domain type.
121+
*
122+
* @param collection must not be {@literal null} nor {@literal empty}.
123+
* @return new instance of {@link FindWithProjection}.
124+
* @throws IllegalArgumentException if collection is {@literal null}.
125+
*/
126+
FindInCollection<T> inCollection(String collection);
127+
}
128+
129+
/**
130+
* Result type override (optional).
131+
*/
132+
interface FindWithProjection<T> extends FindInCollection<T>, FindDistinct {
133+
134+
/**
135+
* Define the target type fields should be mapped to. <br />
136+
* Skip this step if you are anyway only interested in the original domain type.
137+
*
138+
* @param resultType must not be {@literal null}.
139+
* @param <R> result type.
140+
* @return new instance of {@link FindWithProjection}.
141+
* @throws IllegalArgumentException if resultType is {@literal null}.
142+
*/
143+
<R> FindByQueryWithQuery<R> as(Class<R> resultType);
144+
}
145+
146+
/**
147+
* Distinct Find support.
148+
*
149+
* @author Michael Reiche
150+
*/
151+
interface FindDistinct {
152+
153+
/**
154+
* Finds the distinct values for a specified {@literal field} across a single {@link } or view.
155+
*
156+
* @param field name of the field. Must not be {@literal null}.
157+
* @return new instance of {@link TerminatingDistinct}.
158+
* @throws IllegalArgumentException if field is {@literal null}.
159+
*/
160+
TerminatingDistinct<Object> distinct(String field);
161+
}
162+
163+
/**
164+
* Result type override. Optional.
165+
*
166+
* @author Michael Reiche
167+
*/
168+
interface DistinctWithProjection {
169+
170+
/**
171+
* Define the target type the result should be mapped to. <br />
172+
* Skip this step if you are anyway fine with the default conversion.
173+
* <dl>
174+
* <dt>{@link Object} (the default)</dt>
175+
* <dd>Result is mapped according to the {@link } converting eg. {@link } into plain {@link String}, {@link } to
176+
* {@link Long}, etc. always picking the most concrete type with respect to the domain types property.<br />
177+
* Any {@link } is run through the {@link org.springframework.data.convert.EntityReader} to obtain the domain type.
178+
* <br />
179+
* Using {@link Object} also works for non strictly typed fields. Eg. a mixture different types like fields using
180+
* {@link String} in one {@link } while {@link Long} in another.</dd>
181+
* <dt>Any Simple type like {@link String}, {@link Long}, ...</dt>
182+
* <dd>The result is mapped directly by the Couchbase Java driver and the {@link } in place. This works only for
183+
* results where all documents considered for the operation use the very same type for the field.</dd>
184+
* <dt>Any Domain type</dt>
185+
* <dd>Domain types can only be mapped if the if the result of the actual {@code distinct()} operation returns
186+
* {@link }.</dd>
187+
* <dt>{@link }</dt>
188+
* <dd>Using {@link } allows retrieval of the raw driver specific format, which returns eg. {@link }.</dd>
189+
* </dl>
190+
*
191+
* @param resultType must not be {@literal null}.
192+
* @param <R> result type.
193+
* @return new instance of {@link TerminatingDistinct}.
194+
* @throws IllegalArgumentException if resultType is {@literal null}.
195+
*/
196+
<R> TerminatingDistinct<R> as(Class<R> resultType);
197+
}
198+
199+
/**
200+
* Result restrictions. Optional.
201+
*
202+
* @author Michael Reiche
203+
*/
204+
interface DistinctWithQuery<T> extends DistinctWithProjection {
205+
206+
/**
207+
* Set the filter {@link Query criteria} to be used.
208+
*
209+
* @param query must not be {@literal null}.
210+
* @return new instance of {@link TerminatingDistinct}.
211+
* @throws IllegalArgumentException if criteria is {@literal null}.
212+
*/
213+
TerminatingDistinct<T> matching(Query query);
214+
}
215+
216+
/**
217+
* Terminating distinct find operations.
218+
*
219+
* @author Michael Reiche
220+
*/
221+
interface TerminatingDistinct<T> extends DistinctWithQuery<T> {
222+
223+
/**
224+
* Get all matching distinct field values.
225+
*
226+
* @return empty {@link Flux} if not match found. Never {@literal null}.
227+
*/
228+
Flux<T> all();
229+
}
230+
231+
interface ReactiveFindByQuery<T> extends FindByQueryConsistentWith<T>, FindInCollection<T>, FindDistinct {}
108232

109233
}

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@
1515
*/
1616
package org.springframework.data.couchbase.core;
1717

18-
import org.springframework.data.couchbase.core.support.TemplateUtils;
19-
import org.springframework.data.couchbase.core.query.Query;
20-
2118
import reactor.core.publisher.Flux;
2219
import reactor.core.publisher.Mono;
2320

21+
import org.springframework.data.couchbase.core.query.Query;
22+
import org.springframework.data.couchbase.core.support.TemplateUtils;
23+
2424
import com.couchbase.client.java.query.QueryScanConsistency;
2525
import com.couchbase.client.java.query.ReactiveQueryResult;
2626

2727
/**
28+
* {@link ReactiveFindByQueryOperation} implementations for Couchbase.
29+
*
2830
* @author Michael Nitschinger
2931
* @author Michael Reiche
3032
*/
@@ -40,7 +42,8 @@ public ReactiveFindByQueryOperationSupport(final ReactiveCouchbaseTemplate templ
4042

4143
@Override
4244
public <T> ReactiveFindByQuery<T> findByQuery(final Class<T> domainType) {
43-
return new ReactiveFindByQuerySupport<>(template, domainType, ALL_QUERY, QueryScanConsistency.NOT_BOUNDED);
45+
return new ReactiveFindByQuerySupport<>(template, domainType, ALL_QUERY, QueryScanConsistency.NOT_BOUNDED,
46+
null);
4447
}
4548

4649
static class ReactiveFindByQuerySupport<T> implements ReactiveFindByQuery<T> {
@@ -49,13 +52,15 @@ static class ReactiveFindByQuerySupport<T> implements ReactiveFindByQuery<T> {
4952
private final Class<T> domainType;
5053
private final Query query;
5154
private final QueryScanConsistency scanConsistency;
55+
private final String collection;
5256

5357
ReactiveFindByQuerySupport(final ReactiveCouchbaseTemplate template, final Class<T> domainType, final Query query,
54-
final QueryScanConsistency scanConsistency) {
58+
final QueryScanConsistency scanConsistency, final String collection) {
5559
this.template = template;
5660
this.domainType = domainType;
5761
this.query = query;
5862
this.scanConsistency = scanConsistency;
63+
this.collection = collection;
5964
}
6065

6166
@Override
@@ -66,12 +71,22 @@ public TerminatingFindByQuery<T> matching(Query query) {
6671
} else {
6772
scanCons = scanConsistency;
6873
}
69-
return new ReactiveFindByQuerySupport<>(template, domainType, query, scanCons);
74+
return new ReactiveFindByQuerySupport<>(template, domainType, query, scanCons, collection);
7075
}
7176

7277
@Override
7378
public FindByQueryConsistentWith<T> consistentWith(QueryScanConsistency scanConsistency) {
74-
return new ReactiveFindByQuerySupport<>(template, domainType, query, scanConsistency);
79+
return new ReactiveFindByQuerySupport<>(template, domainType, query, scanConsistency, collection);
80+
}
81+
82+
@Override
83+
public FindInCollection<T> inCollection(String collection) {
84+
return new ReactiveFindByQuerySupport<>(template, domainType, query, scanConsistency, collection);
85+
}
86+
87+
@Override
88+
public TerminatingDistinct<Object> distinct(String field) {
89+
throw new RuntimeException(("not implemented"));
7590
}
7691

7792
@Override
@@ -129,7 +144,6 @@ public Mono<Boolean> exists() {
129144
private String assembleEntityQuery(final boolean count) {
130145
return query.toN1qlSelectString(template, this.domainType, count);
131146
}
132-
133147
}
134148

135149
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ interface RemoveByQueryConsistentWith<T> extends RemoveByQueryWithQuery<T> {
4141

4242
}
4343

44-
interface ReactiveRemoveByQuery<T> extends RemoveByQueryConsistentWith<T> {}
44+
interface RemoveByQueryInCollection<T> extends RemoveByQueryConsistentWith<T> {
45+
46+
RemoveByQueryConsistentWith<T> inCollection(String collection);
47+
48+
}
49+
50+
interface ReactiveRemoveByQuery<T> extends RemoveByQueryInCollection<T> {}
4551

4652
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public RemoveByQueryWithQuery<T> consistentWith(final QueryScanConsistency scanC
9191
return new ReactiveRemoveByQuerySupport<>(template, domainType, query, scanConsistency);
9292
}
9393

94+
@Override
95+
public RemoveByQueryConsistentWith<T> inCollection(final String collection) {
96+
return new ReactiveRemoveByQuerySupport<>(template, domainType, query, scanConsistency);
97+
}
9498
private String assembleDeleteQuery() {
9599
return query.toN1qlRemoveString(template, this.domainType);
96100
}

0 commit comments

Comments
 (0)