Skip to content

Commit 74c321e

Browse files
committed
Polish
1 parent 6967246 commit 74c321e

File tree

4 files changed

+54
-13
lines changed

4 files changed

+54
-13
lines changed

src/main/java/org/springframework/data/reindexer/repository/query/ReindexerQueryCreator.java

+3-10
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
import ru.rt.restream.reindexer.Query.Condition;
2929
import ru.rt.restream.reindexer.ReindexerIndex;
3030

31-
import org.springframework.dao.InvalidDataAccessApiUsageException;
3231
import org.springframework.data.domain.Pageable;
3332
import org.springframework.data.domain.Sort;
3433
import org.springframework.data.domain.Sort.Order;
34+
import org.springframework.data.reindexer.repository.util.PageableUtils;
3535
import org.springframework.data.repository.query.ParameterAccessor;
3636
import org.springframework.data.repository.query.ReturnedType;
3737
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
@@ -191,7 +191,7 @@ else if (this.tree.isExistsProjection()) {
191191
}
192192
Pageable pageable = this.parameters.getPageable();
193193
if (pageable.isPaged()) {
194-
criteria.limit(pageable.getPageSize()).offset(getOffsetAsInteger(pageable));
194+
criteria.limit(pageable.getPageSize()).offset(PageableUtils.getOffsetAsInteger(pageable));
195195
}
196196
if (sort.isSorted()) {
197197
for (Order order : sort) {
@@ -206,7 +206,7 @@ else if (this.tree.isExistsProjection()) {
206206
* - AND the requested page number > 0
207207
* - AND the requested page size was bigger than the derived result limitation via the First/Top keyword.
208208
*/
209-
int firstResult = getOffsetAsInteger(pageable);
209+
int firstResult = PageableUtils.getOffsetAsInteger(pageable);
210210
if (pageable.getPageSize() > this.tree.getMaxResults() && firstResult > 0) {
211211
criteria.offset(firstResult - (pageable.getPageSize() - this.tree.getMaxResults()));
212212
}
@@ -218,11 +218,4 @@ else if (this.tree.isExistsProjection()) {
218218
}
219219
return criteria;
220220
}
221-
222-
static int getOffsetAsInteger(Pageable pageable) {
223-
if (pageable.getOffset() > Integer.MAX_VALUE) {
224-
throw new InvalidDataAccessApiUsageException("Page offset exceeds Integer.MAX_VALUE (" + Integer.MAX_VALUE + ")");
225-
}
226-
return Math.toIntExact(pageable.getOffset());
227-
}
228221
}

src/main/java/org/springframework/data/reindexer/repository/query/StringBasedReindexerRepositoryQuery.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.data.domain.Sort;
3434
import org.springframework.data.domain.Sort.Order;
3535
import org.springframework.data.expression.ValueExpressionParser;
36+
import org.springframework.data.reindexer.repository.util.PageableUtils;
3637
import org.springframework.data.repository.query.Parameter;
3738
import org.springframework.data.repository.query.QueryMethod;
3839
import org.springframework.data.repository.query.QueryMethodValueEvaluationContextAccessor;
@@ -208,7 +209,7 @@ private String prepareQuery(ReindexerParameterAccessor parameters) {
208209
result.append(" limit ").append(maxResults);
209210
}
210211
if (result.indexOf("offset") == -1) {
211-
int firstResult = ReindexerQueryCreator.getOffsetAsInteger(pageable);
212+
int firstResult = PageableUtils.getOffsetAsInteger(pageable);
212213
if (firstResult > 0) {
213214
/*
214215
* In order to return the correct results, we have to adjust the first result offset to be returned if:

src/main/java/org/springframework/data/reindexer/repository/support/SimpleReindexerRepository.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.data.domain.Sort.Order;
3434
import org.springframework.data.reindexer.repository.ReindexerRepository;
3535
import org.springframework.data.reindexer.repository.query.ReindexerEntityInformation;
36+
import org.springframework.data.reindexer.repository.util.PageableUtils;
3637
import org.springframework.data.support.PageableExecutionUtils;
3738
import org.springframework.util.Assert;
3839

@@ -119,9 +120,9 @@ public Page<T> findAll(Pageable pageable) {
119120
}
120121
Query<T> query = this.namespace.query();
121122
for (Order order : pageable.getSort()) {
122-
query = query.sort(order.getProperty(), order.isDescending());
123+
query.sort(order.getProperty(), order.isDescending());
123124
}
124-
query = query.limit(pageable.getPageSize()).offset((int) pageable.getOffset()).reqTotal();
125+
query.limit(pageable.getPageSize()).offset(PageableUtils.getOffsetAsInteger(pageable)).reqTotal();
125126
try (ResultIterator<T> iterator = query.execute()) {
126127
List<T> content = new ArrayList<>();
127128
while (iterator.hasNext()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2022 evgeniycheban
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.reindexer.repository.util;
17+
18+
import org.springframework.dao.InvalidDataAccessApiUsageException;
19+
import org.springframework.data.domain.Pageable;
20+
21+
/**
22+
* Provide a set of utility methods to support {@link Pageable}s.
23+
*
24+
* @author Greg Turnquist
25+
* @author Evgeniy Cheban
26+
*/
27+
public final class PageableUtils {
28+
29+
private PageableUtils() {
30+
throw new IllegalStateException("Cannot instantiate a utility class!");
31+
}
32+
33+
/**
34+
* Convert a {@link Pageable}'s offset value from {@link Long} to {@link Integer} to support JPA spec methods.
35+
*
36+
* @param pageable the {@link Pageable} to use
37+
* @return integer
38+
* @throws InvalidDataAccessApiUsageException if {@link Pageable}'s offset exceeds {@link Integer#MAX_VALUE}
39+
*/
40+
public static int getOffsetAsInteger(Pageable pageable) {
41+
if (pageable.getOffset() > Integer.MAX_VALUE) {
42+
throw new InvalidDataAccessApiUsageException("Page offset exceeds Integer.MAX_VALUE (" + Integer.MAX_VALUE + ")");
43+
}
44+
return Math.toIntExact(pageable.getOffset());
45+
}
46+
}

0 commit comments

Comments
 (0)