Skip to content

Commit 4b637d6

Browse files
committed
DATACOUCH-588 - Implement pageable and realign repo query
This implements pageable for non-reactive queries and realigns reactive queries with other spring-data projects to facilitate the implementaion of pageable (done) and other types of queries such as projection and distinct (not yet done)
1 parent 05fb9ea commit 4b637d6

27 files changed

+1503
-78
lines changed

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

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,129 @@ interface FindByQueryConsistentWith<T> extends FindByQueryWithQuery<T> {
104104

105105
}
106106

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

109232
}

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ static class ReactiveFindByQuerySupport<T> implements ReactiveFindByQuery<T> {
6060

6161
@Override
6262
public TerminatingFindByQuery<T> matching(Query query) {
63-
return new ReactiveFindByQuerySupport<>(template, domainType, query, scanConsistency);
63+
QueryScanConsistency scanCons;
64+
if (query.getConsistency() != null) {
65+
scanCons = query.getConsistency();
66+
} else {
67+
scanCons = scanConsistency;
68+
}
69+
return new ReactiveFindByQuerySupport<>(template, domainType, query, scanCons);
6470
}
6571

6672
@Override
@@ -94,6 +100,7 @@ public Flux<T> all() {
94100
long cas = row.getLong(TemplateUtils.SELECT_CAS);
95101
row.removeKey(TemplateUtils.SELECT_ID);
96102
row.removeKey(TemplateUtils.SELECT_CAS);
103+
System.out.println("Row -> " + row.toString());
97104
return template.support().decodeEntity(id, row.toString(), cas, domainType);
98105
});
99106
});
@@ -110,8 +117,10 @@ public Mono<Long> count() {
110117
} else {
111118
return throwable;
112119
}
113-
}).flatMapMany(ReactiveQueryResult::rowsAsObject).map(row -> row.getLong(TemplateUtils.SELECT_COUNT))
114-
.next();
120+
}).flatMapMany(ReactiveQueryResult::rowsAsObject).map(row -> {
121+
System.out.println("count: " + row.getLong(TemplateUtils.SELECT_COUNT));
122+
return row.getLong(TemplateUtils.SELECT_COUNT);
123+
}).next();
115124
});
116125
}
117126

@@ -124,6 +133,15 @@ private String assembleEntityQuery(final boolean count) {
124133
return query.toN1qlString(template, this.domainType, count);
125134
}
126135

136+
@Override
137+
public FindWithProjection<T> inCollection(String collection) {
138+
throw new RuntimeException(("not implemented"));
139+
}
140+
141+
@Override
142+
public TerminatingDistinct<Object> distinct(String field) {
143+
throw new RuntimeException(("not implemented"));
144+
}
127145
}
128146

129147
}

src/main/java/org/springframework/data/couchbase/core/query/Query.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class Query {
4747
private long skip;
4848
private int limit;
4949
private Sort sort = Sort.unsorted();
50+
private QueryScanConsistency queryScanConsistency;
5051

5152
static private final Pattern WHERE_PATTERN = Pattern.compile("\\sWHERE\\s");
5253

@@ -104,13 +105,22 @@ public Query skip(long skip) {
104105
* Limit the number of returned documents to {@code limit}.
105106
*
106107
* @param limit
107-
* @return
108+
* @return this
108109
*/
109110
public Query limit(int limit) {
110111
this.limit = limit;
111112
return this;
112113
}
113114

115+
/**
116+
* limit
117+
*
118+
* @return limit
119+
*/
120+
public int getLimit() {
121+
return limit;
122+
}
123+
114124
/**
115125
* Sets the given pagination information on the {@link Query} instance. Will transparently set {@code skip} and
116126
* {@code limit} as well as applying the {@link Sort} instance defined with the {@link Pageable}.
@@ -127,6 +137,27 @@ public Query with(final Pageable pageable) {
127137
return with(pageable.getSort());
128138
}
129139

140+
/**
141+
* limit
142+
*
143+
* @return queryScanConsistency
144+
*/
145+
public QueryScanConsistency getConsistency() {
146+
return queryScanConsistency;
147+
}
148+
149+
/**
150+
* Sets the given pagination information on the {@link Query} instance. Will transparently set {@code skip} and
151+
* {@code limit} as well as applying the {@link Sort} instance defined with the {@link Pageable}.
152+
*
153+
* @param queryScanConsistency
154+
* @return
155+
*/
156+
public Query with(final QueryScanConsistency queryScanConsistency) {
157+
this.queryScanConsistency = queryScanConsistency;
158+
return this;
159+
}
160+
130161
/**
131162
* Adds a {@link Sort} to the {@link Query} instance.
132163
*

0 commit comments

Comments
 (0)