Skip to content

Commit 37c9aa4

Browse files
christophstroblmp911de
authored andcommitted
DATAKV-142 - Consider PartTree.isLimiting() and PartTree.getMaxResults() when creating queries.
We now pick up result size limiting constraints from the query method name. This allows usage of `findTopN` and `findFirst` keywords. Original pull request: #22.
1 parent 734b85c commit 37c9aa4

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

src/main/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQuery.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,13 @@ protected KeyValueQuery<?> prepareQuery(KeyValueQuery<?> instance, Object[] para
150150
Sort sort = accessor.getSort();
151151

152152
query.setOffset(pageable == null ? -1 : pageable.getOffset());
153-
query.setRows(pageable == null ? -1 : pageable.getPageSize());
153+
154+
if (pageable != null) {
155+
query.setRows(pageable.getPageSize());
156+
} else if (instance.getRows() >= 0) {
157+
query.setRows(instance.getRows());
158+
}
159+
154160
query.setSort(sort == null ? instance.getSort() : sort);
155161

156162
return query;
@@ -175,7 +181,12 @@ public KeyValueQuery<?> createQuery(ParameterAccessor accessor) {
175181

176182
Constructor<? extends AbstractQueryCreator<?, ?>> constructor = (Constructor<? extends AbstractQueryCreator<?, ?>>) ClassUtils
177183
.getConstructorIfAvailable(queryCreator, PartTree.class, ParameterAccessor.class);
178-
return (KeyValueQuery<?>) BeanUtils.instantiateClass(constructor, tree, accessor).createQuery();
184+
KeyValueQuery<?> query = (KeyValueQuery<?>) BeanUtils.instantiateClass(constructor, tree, accessor).createQuery();
185+
186+
if (tree.isLimiting()) {
187+
query.setRows(tree.getMaxResults());
188+
}
189+
return query;
179190
}
180191

181192
/*

src/test/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQueryUnitTests.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package org.springframework.data.keyvalue.repository.query;
1717

18+
import static org.hamcrest.core.Is.*;
1819
import static org.hamcrest.core.IsNot.*;
20+
import static org.hamcrest.core.IsNull.*;
1921
import static org.hamcrest.core.IsSame.*;
2022
import static org.junit.Assert.*;
2123
import static org.mockito.Matchers.*;
@@ -24,12 +26,17 @@
2426
import java.lang.reflect.Method;
2527
import java.util.List;
2628

29+
import org.hamcrest.core.IsInstanceOf;
2730
import org.junit.Test;
2831
import org.junit.runner.RunWith;
2932
import org.mockito.Mock;
3033
import org.mockito.runners.MockitoJUnitRunner;
34+
import org.springframework.data.domain.PageRequest;
35+
import org.springframework.data.domain.Pageable;
3136
import org.springframework.data.keyvalue.Person;
3237
import org.springframework.data.keyvalue.core.KeyValueOperations;
38+
import org.springframework.data.keyvalue.core.SpelCriteria;
39+
import org.springframework.data.keyvalue.core.query.KeyValueQuery;
3340
import org.springframework.data.projection.ProjectionFactory;
3441
import org.springframework.data.repository.core.RepositoryMetadata;
3542
import org.springframework.data.repository.query.DefaultEvaluationContextProvider;
@@ -69,8 +76,81 @@ public void spelExpressionAndContextShouldNotBeReused() throws NoSuchMethodExcep
6976
assertThat(first, not(sameInstance(second)));
7077
}
7178

79+
/**
80+
* @see DATAKV-142
81+
*/
82+
@Test
83+
@SuppressWarnings({ "unchecked", "rawtypes" })
84+
public void shouldApplyPageableParameterToCollectionQuery() throws SecurityException, NoSuchMethodException {
85+
86+
when(metadataMock.getDomainType()).thenReturn((Class) Person.class);
87+
when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class);
88+
89+
QueryMethod qm = new QueryMethod(Repo.class.getMethod("findBy", Pageable.class), metadataMock,
90+
projectionFactoryMock);
91+
92+
KeyValuePartTreeQuery partTreeQuery = new KeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE,
93+
kvOpsMock, SpelQueryCreator.class);
94+
95+
KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] { new PageRequest(2, 3) });
96+
97+
assertThat(query.getOffset(), is(6));
98+
assertThat(query.getRows(), is(3));
99+
}
100+
101+
/**
102+
* @see DATAKV-142
103+
*/
104+
@Test
105+
@SuppressWarnings({ "unchecked", "rawtypes" })
106+
public void shouldApplyDerivedMaxResultsToQuery() throws SecurityException, NoSuchMethodException {
107+
108+
when(metadataMock.getDomainType()).thenReturn((Class) Person.class);
109+
when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class);
110+
111+
QueryMethod qm = new QueryMethod(Repo.class.getMethod("findTop3By"), metadataMock, projectionFactoryMock);
112+
113+
KeyValuePartTreeQuery partTreeQuery = new KeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE,
114+
kvOpsMock, SpelQueryCreator.class);
115+
116+
KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] {});
117+
118+
assertThat(query.getRows(), is(3));
119+
}
120+
121+
/**
122+
* @see DATAKV-142
123+
*/
124+
@Test
125+
@SuppressWarnings({ "unchecked", "rawtypes" })
126+
public void shouldApplyDerivedMaxResultsToQueryWithParameters() throws SecurityException, NoSuchMethodException {
127+
128+
when(metadataMock.getDomainType()).thenReturn((Class) Person.class);
129+
when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class);
130+
131+
QueryMethod qm = new QueryMethod(Repo.class.getMethod("findTop3ByFirstname", String.class), metadataMock,
132+
projectionFactoryMock);
133+
134+
KeyValuePartTreeQuery partTreeQuery = new KeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE,
135+
kvOpsMock, SpelQueryCreator.class);
136+
137+
KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] { "firstname" });
138+
139+
assertThat(query.getCritieria(), is(notNullValue()));
140+
assertThat(query.getCritieria(), IsInstanceOf.instanceOf(SpelCriteria.class));
141+
assertThat(((SpelCriteria) query.getCritieria()).getExpression().getExpressionString(),
142+
is("#it?.firstname?.equals([0])"));
143+
assertThat(query.getRows(), is(3));
144+
}
145+
72146
static interface Repo {
73147

74148
List<Person> findByFirstname(String firstname);
149+
150+
List<Person> findBy(Pageable page);
151+
152+
List<Person> findTop3By();
153+
154+
List<Person> findTop3ByFirstname(String firstname);
75155
}
76156
}

0 commit comments

Comments
 (0)