Skip to content

Commit 5b959e7

Browse files
committed
Copy sort from pageable to query. (#1305)
Closes #1304.
1 parent 82d022a commit 5b959e7

File tree

3 files changed

+73
-19
lines changed

3 files changed

+73
-19
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ public Query with(final Pageable pageable) {
178178
}
179179
this.limit = pageable.getPageSize();
180180
this.skip = pageable.getOffset();
181-
if (!this.sort.equals(pageable.getSort()))
182-
this.sort.and(pageable.getSort());
181+
this.with(pageable.getSort());
183182
return this;
184183
}
185184

src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateQueryIntegrationTests.java

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.data.couchbase.core;
1818

19+
import static com.couchbase.client.java.query.QueryScanConsistency.REQUEST_PLUS;
1920
import static org.junit.jupiter.api.Assertions.assertEquals;
2021
import static org.junit.jupiter.api.Assertions.assertNotNull;
2122
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -26,9 +27,11 @@
2627
import java.time.temporal.TemporalAccessor;
2728
import java.util.Arrays;
2829
import java.util.List;
30+
import java.util.Locale;
2931
import java.util.UUID;
3032
import java.util.stream.Collectors;
3133

34+
import com.couchbase.client.java.query.QueryOptions;
3235
import org.junit.jupiter.api.BeforeEach;
3336
import org.junit.jupiter.api.Test;
3437
import org.springframework.data.couchbase.core.query.Query;
@@ -50,6 +53,9 @@
5053
import org.springframework.data.couchbase.util.JavaIntegrationTests;
5154

5255
import com.couchbase.client.java.query.QueryScanConsistency;
56+
import org.springframework.data.domain.PageRequest;
57+
import org.springframework.data.domain.Pageable;
58+
import org.springframework.data.domain.Sort;
5359

5460
/**
5561
* Query tests Theses tests rely on a cb server running
@@ -73,7 +79,7 @@ public void beforeEach() {
7379
// ensure each test starts with clean state
7480

7581
couchbaseTemplate.removeByQuery(User.class).all();
76-
couchbaseTemplate.findByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all();
82+
couchbaseTemplate.findByQuery(User.class).withConsistency(REQUEST_PLUS).all();
7783
}
7884

7985
@Test
@@ -85,7 +91,7 @@ void findByQueryAll() {
8591
couchbaseTemplate.upsertById(User.class).all(Arrays.asList(user1, user2));
8692

8793
final List<User> foundUsers = couchbaseTemplate.findByQuery(User.class)
88-
.withConsistency(QueryScanConsistency.REQUEST_PLUS).all();
94+
.withConsistency(REQUEST_PLUS).all();
8995

9096
for (User u : foundUsers) {
9197
if (!(u.equals(user1) || u.equals(user2))) {
@@ -108,7 +114,7 @@ void findByQueryAll() {
108114
couchbaseTemplate.findById(User.class).one(user1.getId());
109115
reactiveCouchbaseTemplate.findById(User.class).one(user1.getId()).block();
110116
} finally {
111-
couchbaseTemplate.removeByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all();
117+
couchbaseTemplate.removeByQuery(User.class).withConsistency(REQUEST_PLUS).all();
112118
}
113119

114120
User usery = couchbaseTemplate.findById(User.class).one("user1");
@@ -128,7 +134,7 @@ void findByMatchingQuery() {
128134

129135
Query specialUsers = new Query(QueryCriteria.where(i("firstname")).like("special"));
130136
final List<User> foundUsers = couchbaseTemplate.findByQuery(User.class)
131-
.withConsistency(QueryScanConsistency.REQUEST_PLUS).matching(specialUsers).all();
137+
.withConsistency(REQUEST_PLUS).matching(specialUsers).all();
132138

133139
assertEquals(1, foundUsers.size());
134140
}
@@ -142,7 +148,7 @@ void findAssessmentDO() {
142148

143149
Query specialUsers = new Query(QueryCriteria.where(i("id")).is(ado.getId()));
144150
final List<AssessmentDO> foundUsers = couchbaseTemplate.findByQuery(AssessmentDO.class)
145-
.withConsistency(QueryScanConsistency.REQUEST_PLUS).matching(specialUsers).all();
151+
.withConsistency(REQUEST_PLUS).matching(specialUsers).all();
146152
assertEquals("123", foundUsers.get(0).getId(), "id");
147153
assertEquals("44444444", foundUsers.get(0).getDocumentId(), "documentId");
148154
assertEquals(ado, foundUsers.get(0));
@@ -169,7 +175,7 @@ void findByMatchingQueryProjected() {
169175
Query daveUsers = new Query(QueryCriteria.where("username").like("dave"));
170176

171177
final List<UserSubmissionProjected> foundUserSubmissions = couchbaseTemplate.findByQuery(UserSubmission.class)
172-
.as(UserSubmissionProjected.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).matching(daveUsers).all();
178+
.as(UserSubmissionProjected.class).withConsistency(REQUEST_PLUS).matching(daveUsers).all();
173179
assertEquals(1, foundUserSubmissions.size());
174180
assertEquals(user.getUsername(), foundUserSubmissions.get(0).getUsername());
175181
assertEquals(user.getId(), foundUserSubmissions.get(0).getId());
@@ -186,11 +192,11 @@ void findByMatchingQueryProjected() {
186192

187193
Query specialUsers = new Query(QueryCriteria.where("firstname").like("special"));
188194
final List<UserJustLastName> foundUsers = couchbaseTemplate.findByQuery(User.class).as(UserJustLastName.class)
189-
.withConsistency(QueryScanConsistency.REQUEST_PLUS).matching(specialUsers).all();
195+
.withConsistency(REQUEST_PLUS).matching(specialUsers).all();
190196
assertEquals(1, foundUsers.size());
191197

192198
final List<UserJustLastName> foundUsersReactive = reactiveCouchbaseTemplate.findByQuery(User.class)
193-
.as(UserJustLastName.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).matching(specialUsers).all()
199+
.as(UserJustLastName.class).withConsistency(REQUEST_PLUS).matching(specialUsers).all()
194200
.collectList().block();
195201
assertEquals(1, foundUsersReactive.size());
196202

@@ -206,7 +212,7 @@ void removeByQueryAll() {
206212
assertTrue(couchbaseTemplate.existsById().one(user1.getId()));
207213
assertTrue(couchbaseTemplate.existsById().one(user2.getId()));
208214

209-
couchbaseTemplate.removeByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all();
215+
couchbaseTemplate.removeByQuery(User.class).withConsistency(REQUEST_PLUS).all();
210216

211217
assertNull(couchbaseTemplate.findById(User.class).one(user1.getId()));
212218
assertNull(couchbaseTemplate.findById(User.class).one(user2.getId()));
@@ -227,7 +233,7 @@ void removeByMatchingQuery() {
227233

228234
Query nonSpecialUsers = new Query(QueryCriteria.where(i("firstname")).notLike("special"));
229235

230-
couchbaseTemplate.removeByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS)
236+
couchbaseTemplate.removeByQuery(User.class).withConsistency(REQUEST_PLUS)
231237
.matching(nonSpecialUsers).all();
232238

233239
assertNull(couchbaseTemplate.findById(User.class).one(user1.getId()));
@@ -252,17 +258,17 @@ void distinct() {
252258

253259
// distinct icao
254260
List<Airport> airports1 = couchbaseTemplate.findByQuery(Airport.class).distinct(new String[] { "icao" })
255-
.as(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all();
261+
.as(Airport.class).withConsistency(REQUEST_PLUS).all();
256262
assertEquals(2, airports1.size());
257263

258264
// distinct all-fields-in-Airport.class
259265
List<Airport> airports2 = couchbaseTemplate.findByQuery(Airport.class).distinct(new String[] {}).as(Airport.class)
260-
.withConsistency(QueryScanConsistency.REQUEST_PLUS).all();
266+
.withConsistency(REQUEST_PLUS).all();
261267
assertEquals(7, airports2.size());
262268

263269
// count( distinct { iata, icao } )
264270
long count1 = couchbaseTemplate.findByQuery(Airport.class).distinct(new String[] { "iata", "icao" })
265-
.as(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).count();
271+
.as(Airport.class).withConsistency(REQUEST_PLUS).count();
266272
assertEquals(7, count1);
267273

268274
} finally {
@@ -287,22 +293,22 @@ void distinctReactive() {
287293

288294
// distinct icao
289295
List<Airport> airports1 = reactiveCouchbaseTemplate.findByQuery(Airport.class).distinct(new String[] { "icao" })
290-
.as(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all().collectList().block();
296+
.as(Airport.class).withConsistency(REQUEST_PLUS).all().collectList().block();
291297
assertEquals(2, airports1.size());
292298

293299
// distinct all-fields-in-Airport.class
294300
List<Airport> airports2 = reactiveCouchbaseTemplate.findByQuery(Airport.class).distinct(new String[] {})
295-
.as(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all().collectList().block();
301+
.as(Airport.class).withConsistency(REQUEST_PLUS).all().collectList().block();
296302
assertEquals(7, airports2.size());
297303

298304
// count( distinct icao )
299305
Long count1 = reactiveCouchbaseTemplate.findByQuery(Airport.class).distinct(new String[] { "icao" })
300-
.as(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).count().block();
306+
.as(Airport.class).withConsistency(REQUEST_PLUS).count().block();
301307
assertEquals(2, count1);
302308

303309
// count( distinct { icao, iata } )
304310
Long count2 = reactiveCouchbaseTemplate.findByQuery(Airport.class).distinct(new String[] { "icao", "iata" })
305-
.withConsistency(QueryScanConsistency.REQUEST_PLUS).count().block();
311+
.withConsistency(REQUEST_PLUS).count().block();
306312
assertEquals(7, count2);
307313

308314
} finally {
@@ -312,4 +318,31 @@ void distinctReactive() {
312318
}
313319
}
314320

321+
@Test
322+
void sortedTemplate() {
323+
couchbaseTemplate.removeByQuery(Airport.class).withConsistency(REQUEST_PLUS).all();
324+
String[] iatas = { "JFK", "IAD", "SFO", "SJC", "SEA", "LAX", "PHX" };
325+
326+
try {
327+
couchbaseTemplate.insertById(Airport.class).all(
328+
Arrays.stream(iatas).map((iata) -> new Airport("airports::" + iata, iata, iata.toLowerCase(Locale.ROOT)))
329+
.collect(Collectors.toSet()));
330+
331+
org.springframework.data.couchbase.core.query.Query query = org.springframework.data.couchbase.core.query.Query.query(QueryCriteria.where("iata").isNotNull());
332+
Pageable pageableWithSort = PageRequest.of(0, 7, Sort.by("iata"));
333+
query.with(pageableWithSort);
334+
List<Airport> airports = couchbaseTemplate.findByQuery(Airport.class).withConsistency(REQUEST_PLUS).matching(query).all();
335+
336+
String[] sortedIatas = iatas.clone();
337+
System.out.println(""+iatas.length+" "+sortedIatas.length);
338+
Arrays.sort(sortedIatas);
339+
for(int i=0; i< pageableWithSort.getPageSize(); i++){
340+
System.out.println(airports.get(i).getIata());
341+
assertEquals(sortedIatas[i], airports.get(i).getIata());
342+
}
343+
} finally {
344+
couchbaseTemplate.removeById(Airport.class).all(Arrays.stream(iatas).map((iata) -> "airports::" + iata).collect(Collectors.toSet()));
345+
}
346+
}
347+
315348
}

src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,28 @@ void stringQueryReturnsSimpleType() {
576576
airportRepository.deleteById(airport2.getId());
577577
}
578578

579+
@Test
580+
void sortedRepository() {
581+
airportRepository.withOptions(QueryOptions.queryOptions().scanConsistency(REQUEST_PLUS)).deleteAll();
582+
String[] iatas = { "JFK", "IAD", "SFO", "SJC", "SEA", "LAX", "PHX" };
583+
584+
try {
585+
airportRepository.saveAll(
586+
Arrays.stream(iatas).map((iata) -> new Airport("airports::" + iata, iata, iata.toLowerCase(Locale.ROOT)))
587+
.collect(Collectors.toSet()));
588+
List<Airport> airports = airportRepository.withOptions(QueryOptions.queryOptions().scanConsistency(REQUEST_PLUS)).findAll(Sort.by("iata"));
589+
String[] sortedIatas = iatas.clone();
590+
System.out.println(""+iatas.length+" "+sortedIatas.length);
591+
Arrays.sort(sortedIatas);
592+
for(int i=0; i< sortedIatas.length; i++){
593+
assertEquals(sortedIatas[i], airports.get(i).getIata());
594+
}
595+
} finally {
596+
airportRepository
597+
.deleteAllById(Arrays.stream(iatas).map((iata) -> "airports::" + iata).collect(Collectors.toSet()));
598+
}
599+
}
600+
579601
@Test
580602
void count() {
581603
airportRepository.withOptions(QueryOptions.queryOptions().scanConsistency(REQUEST_PLUS)).deleteAll();

0 commit comments

Comments
 (0)