Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Limit parameter. #4398

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4397-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand All @@ -26,7 +26,7 @@
<properties>
<project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id>
<springdata.commons>3.2.0-SNAPSHOT</springdata.commons>
<springdata.commons>3.2.x-2827-SNAPSHOT</springdata.commons>
<mongo>4.9.1</mongo>
<mongo.reactivestreams>${mongo}</mongo.reactivestreams>
<jmh.version>1.19</jmh.version>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4397-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4397-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4397-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Iterator;
import java.util.List;

import org.springframework.data.domain.Limit;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Range;
import org.springframework.data.domain.ScrollPosition;
Expand Down Expand Up @@ -117,6 +118,11 @@ public UpdateDefinition getUpdate() {
return delegate.getUpdate();
}

@Override
public Limit getLimit() {
return delegate.getLimit();
}

/**
* Converts the given value with the underlying {@link MongoWriter}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ void appliesScrollPositionCorrectly() {
assertThat(page).contains(carter);
}

@Test // GH-4397
void appliesLimitToScrollingCorrectly() {

Window<Person> page = repository.findByLastnameLikeOrderByLastnameAscFirstnameAsc("*a*",
ScrollPosition.keyset(), Limit.of(2));

assertThat(page.isLast()).isFalse();
assertThat(page.size()).isEqualTo(2);
assertThat(page).contains(carter);
}

@Test // GH-4308
void appliesScrollPositionWithProjectionCorrectly() {

Expand All @@ -236,6 +247,14 @@ void executesPagedFinderCorrectly() {
assertThat(page).contains(carter, stefan);
}

@Test // GH-4397
void executesFinderCorrectlyWithSortAndLimit() {

List<Person> page = repository.findByLastnameLike("*a*", Sort.by(Direction.ASC, "lastname", "firstname"), Limit.of(2));

assertThat(page).containsExactly(carter, stefan);
}

@Test
void executesPagedFinderWithAnnotatedQueryCorrectly() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.springframework.data.domain.Limit;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Range;
Expand Down Expand Up @@ -126,6 +127,9 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
Window<Person> findTop2ByLastnameLikeOrderByLastnameAscFirstnameAsc(String lastname,
ScrollPosition scrollPosition);

Window<Person> findByLastnameLikeOrderByLastnameAscFirstnameAsc(String lastname,
ScrollPosition scrollPosition, Limit limit);

/**
* Returns a scroll of {@link Person}s applying projections with a lastname matching the given one (*-wildcards
* supported).
Expand All @@ -145,6 +149,8 @@ Window<Person> findTop2ByLastnameLikeOrderByLastnameAscFirstnameAsc(String lastn
*/
Page<Person> findByLastnameLike(String lastname, Pageable pageable);

List<Person> findByLastnameLike(String lastname, Sort sort, Limit limit);

@Query("{ 'lastname' : { '$regex' : '?0', '$options' : 'i'}}")
Page<Person> findByLastnameLikeWithPageable(String lastname, Pageable pageable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.data.domain.Limit;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -493,6 +494,30 @@ void updateShouldApplyHint() {
assertThat(captor.getValue().getHint()).isEqualTo("idx-ln");
}

@Test // GH-4397
void limitShouldBeAppliedToQuery() {

createQueryForMethod("findWithLimit", String.class, Limit.class).execute(new Object[] { "dalinar", Limit.of(42) });

ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class);
verify(withQueryMock).matching(captor.capture());

assertThat(captor.getValue().getLimit()).isEqualTo(42);
}

@Test // GH-4397
void sortAndLimitShouldBeAppliedToQuery() {

createQueryForMethod("findWithSortAndLimit", String.class, Sort.class, Limit.class)
.execute(new Object[] { "dalinar", Sort.by("fn"), Limit.of(42) });

ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class);
verify(withQueryMock).matching(captor.capture());

assertThat(captor.getValue().getLimit()).isEqualTo(42);
assertThat(captor.getValue().getSortObject()).isEqualTo(new Document("fn", 1));
}

private MongoQueryFake createQueryForMethod(String methodName, Class<?>... paramTypes) {
return createQueryForMethod(Repo.class, methodName, paramTypes);
}
Expand Down Expand Up @@ -614,6 +639,10 @@ private interface Repo extends MongoRepository<Person, Long> {

@Hint("idx-fn")
void findWithHintByFirstname(String firstname);

List<Person> findWithLimit(String firstname, Limit limit);

List<Person> findWithSortAndLimit(String firstname, Sort sort, Limit limit);
}

// DATAMONGO-1872
Expand Down