use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.
the class AbstractQueryInterceptor method prepareQuery.
/**
* Prepares a query for the given context.
*
* @param <RT> The result generic type
* @param methodKey The method key
* @param context The context
* @param resultType The result type
* @param isCount Is count query
* @return The query
*/
protected final <RT> PreparedQuery<?, RT> prepareQuery(RepositoryMethodKey methodKey, MethodInvocationContext<T, R> context, Class<RT> resultType, boolean isCount) {
validateNullArguments(context);
StoredQuery<?, RT> storedQuery = findStoreQuery(methodKey, context, resultType, isCount);
Pageable pageable = storedQuery.hasPageable() ? getPageable(context) : Pageable.UNPAGED;
return preparedQueryResolver.resolveQuery(context, storedQuery, pageable);
}
use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.
the class FindPageSpecificationInterceptor method intercept.
@Override
public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) {
if (context.getParameterValues().length != 2) {
throw new IllegalStateException("Expected exactly 2 arguments to method");
}
Pageable pageable = getPageable(context);
if (pageable.isUnpaged()) {
PreparedQuery<?, ?> preparedQuery = preparedQueryForCriteria(methodKey, context, Type.FIND_PAGE);
Iterable<?> iterable = operations.findAll(preparedQuery);
List<Object> resultList = (List<Object>) CollectionUtils.iterableToList(iterable);
return Page.of(resultList, pageable, resultList.size());
}
PreparedQuery<?, ?> preparedQuery = preparedQueryForCriteria(methodKey, context, Type.FIND_PAGE);
PreparedQuery<?, Number> countQuery = preparedQueryForCriteria(methodKey, context, Type.COUNT);
Iterable<?> iterable = operations.findAll(preparedQuery);
List<Object> resultList = (List<Object>) CollectionUtils.iterableToList(iterable);
Number count = operations.findOne(countQuery);
Page page = Page.of(resultList, getPageable(context), count != null ? count.longValue() : 0);
Class<Object> rt = context.getReturnType().getType();
if (rt.isInstance(page)) {
return page;
}
return operations.getConversionService().convert(page, rt).orElseThrow(() -> new IllegalStateException("Unsupported page interface type " + rt));
}
use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.
the class DefaultFindSliceReactiveInterceptor method intercept.
@Override
public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) {
if (context.hasAnnotation(Query.class)) {
PreparedQuery<Object, Object> preparedQuery = (PreparedQuery<Object, Object>) prepareQuery(methodKey, context);
Pageable pageable = preparedQuery.getPageable();
Mono<Slice<Object>> publisher = Flux.from(reactiveOperations.findAll(preparedQuery)).collectList().map(objects -> Slice.of(objects, pageable));
return Publishers.convertPublisher(publisher, context.getReturnType().getType());
} else {
PagedQuery<Object> pagedQuery = getPagedQuery(context);
Mono<? extends Slice<?>> result = Flux.from(reactiveOperations.findAll(pagedQuery)).collectList().map(objects -> Slice.of(objects, pagedQuery.getPageable()));
return Publishers.convertPublisher(result, context.getReturnType().getType());
}
}
use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.
the class DefaultMongoPreparedQuery method applyPageable.
private int applyPageable(Pageable pageable, List<Bson> pipeline) {
int limit = 0;
if (pageable != Pageable.UNPAGED) {
int skip = (int) pageable.getOffset();
limit = pageable.getSize();
Sort pageableSort = pageable.getSort();
if (pageableSort.isSorted()) {
Bson sort = pageableSort.getOrderBy().stream().map(order -> order.isAscending() ? Sorts.ascending(order.getProperty()) : Sorts.descending(order.getProperty())).collect(Collectors.collectingAndThen(Collectors.toList(), Sorts::orderBy));
BsonDocument sortStage = new BsonDocument().append("$sort", sort.toBsonDocument());
addStageToPipelineBefore(pipeline, sortStage, "$limit", "$skip");
}
if (skip > 0) {
pipeline.add(new BsonDocument().append("$skip", new BsonInt32(skip)));
}
if (limit > 0) {
pipeline.add(new BsonDocument().append("$limit", new BsonInt32(limit)));
}
}
return limit;
}
use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.
the class DefaultMongoRepositoryOperations method findAllAggregated.
private <T, R> Iterable<R> findAllAggregated(ClientSession clientSession, MongoPreparedQuery<T, R, MongoDatabase> preparedQuery, boolean stream) {
Pageable pageable = preparedQuery.getPageable();
int limit = pageable == Pageable.UNPAGED ? -1 : pageable.getSize();
Class<T> type = preparedQuery.getRootEntity();
Class<R> resultType = preparedQuery.getResultType();
MongoIterable<R> aggregate;
if (!resultType.isAssignableFrom(type)) {
MongoDatabase database = preparedQuery.getDatabase();
aggregate = aggregate(clientSession, preparedQuery, BsonDocument.class).map(result -> convertResult(database.getCodecRegistry(), resultType, result, preparedQuery.isDtoProjection()));
} else {
aggregate = aggregate(clientSession, preparedQuery, resultType);
}
return stream ? aggregate : aggregate.into(new ArrayList<>(limit > 0 ? limit : 20));
}
Aggregations