Skip to content

Commit af26bb6

Browse files
committed
Polishing.
Introduce limit(Limit) method to limit query results applying the Limit domain type. See #4397 Original pull request: #4398
1 parent d78f47f commit af26bb6

File tree

2 files changed

+42
-7
lines changed
  • spring-data-mongodb/src
    • main/java/org/springframework/data/mongodb/core/query
    • test/java/org/springframework/data/mongodb/core/query

2 files changed

+42
-7
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java

+29-7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import org.bson.Document;
3333
import org.springframework.data.domain.KeysetScrollPosition;
34+
import org.springframework.data.domain.Limit;
3435
import org.springframework.data.domain.OffsetScrollPosition;
3536
import org.springframework.data.domain.Pageable;
3637
import org.springframework.data.domain.ScrollPosition;
@@ -66,7 +67,7 @@ public class Query implements ReadConcernAware, ReadPreferenceAware {
6667
private @Nullable Field fieldSpec = null;
6768
private Sort sort = Sort.unsorted();
6869
private long skip;
69-
private int limit;
70+
private Limit limit = Limit.unlimited();
7071

7172
private KeysetScrollPosition keysetScrollPosition;
7273
private @Nullable ReadConcern readConcern;
@@ -155,10 +156,30 @@ public Query skip(long skip) {
155156
* @return this.
156157
*/
157158
public Query limit(int limit) {
158-
this.limit = limit;
159+
this.limit = limit > 0 ? Limit.of(limit) : Limit.unlimited();
159160
return this;
160161
}
161162

163+
/**
164+
* Limit the number of returned documents to {@link Limit}.
165+
*
166+
* @param limit number of documents to return.
167+
* @return this.
168+
* @since 4.2
169+
*/
170+
public Query limit(Limit limit) {
171+
172+
Assert.notNull(limit, "Limit must not be null");
173+
174+
if (limit.isUnlimited()) {
175+
this.limit = limit;
176+
return this;
177+
}
178+
179+
// retain zero/negative semantics for unlimited.
180+
return limit(limit.max());
181+
}
182+
162183
/**
163184
* Configures the query to use the given hint when being executed. The {@code hint} can either be an index name or a
164185
* json {@link Document} representation.
@@ -254,7 +275,7 @@ public Query with(Pageable pageable) {
254275
return this;
255276
}
256277

257-
this.limit = pageable.getPageSize();
278+
this.limit = pageable.toLimit();
258279
this.skip = pageable.getOffset();
259280

260281
return with(pageable.getSort());
@@ -457,7 +478,7 @@ public long getSkip() {
457478
* @since 4.1
458479
*/
459480
public boolean isLimited() {
460-
return this.limit > 0;
481+
return this.limit.isLimited();
461482
}
462483

463484
/**
@@ -468,7 +489,7 @@ public boolean isLimited() {
468489
* @see #isLimited()
469490
*/
470491
public int getLimit() {
471-
return this.limit;
492+
return limit.isUnlimited() ? 0 : this.limit.max();
472493
}
473494

474495
/**
@@ -683,7 +704,8 @@ public boolean isSorted() {
683704
};
684705

685706
target.skip = source.getSkip();
686-
target.limit = source.getLimit();
707+
708+
target.limit = source.isLimited() ? Limit.of(source.getLimit()) : Limit.unlimited();
687709
target.hint = source.getHint();
688710
target.collation = source.getCollation();
689711
target.restrictedTypes = new HashSet<>(source.getRestrictedTypes());
@@ -746,7 +768,7 @@ public int hashCode() {
746768
result += 31 * nullSafeHashCode(sort);
747769
result += 31 * nullSafeHashCode(hint);
748770
result += 31 * skip;
749-
result += 31 * limit;
771+
result += 31 * limit.hashCode();
750772
result += 31 * nullSafeHashCode(meta);
751773
result += 31 * nullSafeHashCode(collation.orElse(null));
752774

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.bson.Document;
2323
import org.junit.jupiter.api.Test;
2424
import org.springframework.aop.framework.ProxyFactory;
25+
import org.springframework.data.domain.Limit;
2526
import org.springframework.data.domain.Sort;
2627
import org.springframework.data.domain.Sort.Direction;
2728
import org.springframework.data.domain.Sort.Order;
@@ -97,6 +98,18 @@ void testQueryWithLimit() {
9798
assertThat(q.getQueryObject()).isEqualTo(Document
9899
.parse("{ \"name\" : { \"$gte\" : \"M\" , \"$lte\" : \"T\"} , \"age\" : { \"$not\" : { \"$gt\" : 22}}}"));
99100
assertThat(q.getLimit()).isEqualTo(50);
101+
102+
q.limit(Limit.unlimited());
103+
assertThat(q.getLimit()).isZero();
104+
assertThat(q.isLimited()).isFalse();
105+
106+
q.limit(Limit.of(10));
107+
assertThat(q.getLimit()).isEqualTo(10);
108+
assertThat(q.isLimited()).isTrue();
109+
110+
q.limit(Limit.of(-1));
111+
assertThat(q.getLimit()).isZero();
112+
assertThat(q.isLimited()).isFalse();
100113
}
101114

102115
@Test

0 commit comments

Comments
 (0)