use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.
the class DefaultMongoPreparedQuery method getFind.
@Override
public MongoFind getFind() {
MongoFind find = mongoStoredQuery.getFind(preparedQuery.getContext());
Pageable pageable = preparedQuery.getPageable();
if (pageable != Pageable.UNPAGED) {
MongoFindOptions findOptions = find.getOptions();
MongoFindOptions options = findOptions == null ? new MongoFindOptions() : new MongoFindOptions(findOptions);
options.limit(pageable.getSize()).skip((int) pageable.getOffset());
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));
options.sort(sort);
}
return new MongoFind(options);
}
return find;
}
use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.
the class DefaultMongoRepositoryOperations method findAllFiltered.
private <T, R> Iterable<R> findAllFiltered(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> findIterable;
if (!resultType.isAssignableFrom(type)) {
MongoDatabase database = preparedQuery.getDatabase();
findIterable = find(clientSession, preparedQuery, BsonDocument.class).map(result -> convertResult(database.getCodecRegistry(), resultType, result, preparedQuery.isDtoProjection()));
} else {
findIterable = find(clientSession, preparedQuery);
}
return stream ? findIterable : findIterable.into(new ArrayList<>(limit > 0 ? limit : 20));
}
use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.
the class AbstractQueryInterceptor method prepareCountQuery.
/**
* Prepares a query for the given context.
*
* @param methodKey The method key
* @param context The context
* @return The query
*/
protected final PreparedQuery<?, Number> prepareCountQuery(RepositoryMethodKey methodKey, @NonNull MethodInvocationContext<T, R> context) {
StoredQuery storedQuery = countQueries.get(methodKey);
if (storedQuery == null) {
Class rootEntity = getRequiredRootEntity(context);
storedQuery = storedQueryResolver.resolveCountQuery(context, rootEntity, Long.class);
countQueries.put(methodKey, storedQuery);
}
Pageable pageable = storedQuery.hasPageable() ? getPageable(context) : Pageable.UNPAGED;
// noinspection unchecked
return preparedQueryResolver.resolveCountQuery(context, storedQuery, pageable);
}
use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.
the class AbstractQueryInterceptor method getPageable.
/**
* Resolves the {@link Pageable} for the given context.
*
* @param context The pageable
* @return The pageable or null
*/
@NonNull
protected Pageable getPageable(MethodInvocationContext<?, ?> context) {
Pageable pageable = getParameterInRole(context, TypeRole.PAGEABLE, Pageable.class).orElse(null);
if (pageable == null) {
Sort sort = getParameterInRole(context, TypeRole.SORT, Sort.class).orElse(null);
if (sort != null) {
int max = context.intValue(DataMethod.NAME, META_MEMBER_PAGE_SIZE).orElse(-1);
int pageIndex = context.intValue(DataMethod.NAME, DataMethod.META_MEMBER_PAGE_INDEX).orElse(0);
if (max > 0) {
pageable = Pageable.from(pageIndex, max, sort);
} else {
pageable = Pageable.from(sort);
}
} else {
int max = context.intValue(DataMethod.NAME, META_MEMBER_PAGE_SIZE).orElse(-1);
if (max > -1) {
return Pageable.from(0, max);
}
}
}
return pageable != null ? pageable : Pageable.UNPAGED;
}
use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.
the class DefaultFindSliceAsyncInterceptor method intercept.
@SuppressWarnings("unchecked")
@Override
public CompletionStage<Slice<Object>> intercept(RepositoryMethodKey methodKey, MethodInvocationContext<T, CompletionStage<Slice<Object>>> context) {
if (context.hasAnnotation(Query.class)) {
PreparedQuery<?, ?> preparedQuery = prepareQuery(methodKey, context);
Pageable pageable = preparedQuery.getPageable();
return asyncDatastoreOperations.findAll(preparedQuery).thenApply(objects -> Slice.of((List<Object>) CollectionUtils.iterableToList(objects), pageable));
} else {
PagedQuery<Object> pagedQuery = getPagedQuery(context);
return asyncDatastoreOperations.findAll(pagedQuery).thenApply(objects -> Slice.of(CollectionUtils.iterableToList(objects), pagedQuery.getPageable()));
}
}
Aggregations