Search in sources :

Example 16 with Pageable

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);
}
Also used : Pageable(io.micronaut.data.model.Pageable)

Example 17 with 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));
}
Also used : Pageable(io.micronaut.data.model.Pageable) List(java.util.List) Page(io.micronaut.data.model.Page)

Example 18 with Pageable

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());
    }
}
Also used : Pageable(io.micronaut.data.model.Pageable) Slice(io.micronaut.data.model.Slice) PreparedQuery(io.micronaut.data.model.runtime.PreparedQuery)

Example 19 with Pageable

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;
}
Also used : DelegateStoredQuery(io.micronaut.data.runtime.query.internal.DelegateStoredQuery) Sort(io.micronaut.data.model.Sort) MongoFindOptions(io.micronaut.data.mongodb.operations.options.MongoFindOptions) DefaultPreparedQuery(io.micronaut.data.runtime.query.internal.DefaultPreparedQuery) Internal(io.micronaut.core.annotation.Internal) Collectors(java.util.stream.Collectors) PreparedQuery(io.micronaut.data.model.runtime.PreparedQuery) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) Bson(org.bson.conversions.Bson) List(java.util.List) Sorts(com.mongodb.client.model.Sorts) Pageable(io.micronaut.data.model.Pageable) RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) BsonInt32(org.bson.BsonInt32) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) Sort(io.micronaut.data.model.Sort) Bson(org.bson.conversions.Bson)

Example 20 with Pageable

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));
}
Also used : InsertManyResult(com.mongodb.client.result.InsertManyResult) DataType(io.micronaut.data.model.DataType) BeanContext(io.micronaut.context.BeanContext) Spliterators(java.util.Spliterators) MongoDatabase(com.mongodb.client.MongoDatabase) ExecutorReactiveOperations(io.micronaut.data.runtime.operations.ExecutorReactiveOperations) Internal(io.micronaut.core.annotation.Internal) BsonValue(org.bson.BsonValue) EachBean(io.micronaut.context.annotation.EachBean) StoredQuery(io.micronaut.data.model.runtime.StoredQuery) UpdateResult(com.mongodb.client.result.UpdateResult) Map(java.util.Map) DataAccessException(io.micronaut.data.exceptions.DataAccessException) DeleteOperation(io.micronaut.data.model.runtime.DeleteOperation) RuntimeAssociation(io.micronaut.data.model.runtime.RuntimeAssociation) CursorType(com.mongodb.CursorType) AsyncCapableRepository(io.micronaut.data.operations.async.AsyncCapableRepository) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) PersistentProperty(io.micronaut.data.model.PersistentProperty) InsertOneResult(com.mongodb.client.result.InsertOneResult) RuntimePersistentProperty(io.micronaut.data.model.runtime.RuntimePersistentProperty) Predicate(java.util.function.Predicate) AggregateIterable(com.mongodb.client.AggregateIterable) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) BsonDocumentWrapper(org.bson.BsonDocumentWrapper) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) PreparedQuery(io.micronaut.data.model.runtime.PreparedQuery) Serializable(java.io.Serializable) List(java.util.List) Stream(java.util.stream.Stream) FindIterable(com.mongodb.client.FindIterable) Dialect(io.micronaut.data.model.query.builder.sql.Dialect) Optional(java.util.Optional) MongoSynchronousTransactionManager(io.micronaut.data.mongodb.transaction.MongoSynchronousTransactionManager) Spliterator(java.util.Spliterator) MongoAggregationOptions(io.micronaut.data.mongodb.operations.options.MongoAggregationOptions) Named(jakarta.inject.Named) MongoDatabaseFactory(io.micronaut.data.mongodb.database.MongoDatabaseFactory) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) Parameter(io.micronaut.context.annotation.Parameter) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) BeanProperty(io.micronaut.core.beans.BeanProperty) RuntimeEntityRegistry(io.micronaut.data.model.runtime.RuntimeEntityRegistry) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) RequiresSyncMongo(io.micronaut.data.mongodb.conf.RequiresSyncMongo) Function(java.util.function.Function) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) Filters(com.mongodb.client.model.Filters) Bson(org.bson.conversions.Bson) MongoCursor(com.mongodb.client.MongoCursor) OperationContext(io.micronaut.data.runtime.operations.internal.OperationContext) Nullable(io.micronaut.core.annotation.Nullable) Page(io.micronaut.data.model.Page) Pageable(io.micronaut.data.model.Pageable) ReactiveCapableRepository(io.micronaut.data.operations.reactive.ReactiveCapableRepository) ExecutorAsyncOperations(io.micronaut.data.runtime.operations.ExecutorAsyncOperations) StreamSupport(java.util.stream.StreamSupport) AttributeConverterRegistry(io.micronaut.data.model.runtime.AttributeConverterRegistry) ReactiveRepositoryOperations(io.micronaut.data.operations.reactive.ReactiveRepositoryOperations) DateTimeProvider(io.micronaut.data.runtime.date.DateTimeProvider) ExecutorService(java.util.concurrent.ExecutorService) Collation(com.mongodb.client.model.Collation) MediaTypeCodec(io.micronaut.http.codec.MediaTypeCodec) PagedQuery(io.micronaut.data.model.runtime.PagedQuery) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) MongoFindOptions(io.micronaut.data.mongodb.operations.options.MongoFindOptions) AbstractSyncEntityOperations(io.micronaut.data.runtime.operations.internal.AbstractSyncEntityOperations) DataConversionService(io.micronaut.data.runtime.convert.DataConversionService) InsertBatchOperation(io.micronaut.data.model.runtime.InsertBatchOperation) SyncCascadeOperations(io.micronaut.data.runtime.operations.internal.SyncCascadeOperations) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) NonNull(io.micronaut.core.annotation.NonNull) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) DeleteResult(com.mongodb.client.result.DeleteResult) DeleteBatchOperation(io.micronaut.data.model.runtime.DeleteBatchOperation) InsertOperation(io.micronaut.data.model.runtime.InsertOperation) AbstractSyncEntitiesOperations(io.micronaut.data.runtime.operations.internal.AbstractSyncEntitiesOperations) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) ClientSession(com.mongodb.client.ClientSession) RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) MongoIterable(com.mongodb.client.MongoIterable) UpdateBatchOperation(io.micronaut.data.model.runtime.UpdateBatchOperation) UpdateOperation(io.micronaut.data.model.runtime.UpdateOperation) Collections(java.util.Collections) Pageable(io.micronaut.data.model.Pageable) ArrayList(java.util.ArrayList) MongoDatabase(com.mongodb.client.MongoDatabase)

Aggregations

Pageable (io.micronaut.data.model.Pageable)20 Sort (io.micronaut.data.model.Sort)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 PreparedQuery (io.micronaut.data.model.runtime.PreparedQuery)6 Collectors (java.util.stream.Collectors)6 NonNull (io.micronaut.core.annotation.NonNull)5 RuntimePersistentEntity (io.micronaut.data.model.runtime.RuntimePersistentEntity)5 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)4 Internal (io.micronaut.core.annotation.Internal)4 StoredQuery (io.micronaut.data.model.runtime.StoredQuery)4 MongoFindOptions (io.micronaut.data.mongodb.operations.options.MongoFindOptions)4 DataConversionService (io.micronaut.data.runtime.convert.DataConversionService)4 EachBean (io.micronaut.context.annotation.EachBean)3 Parameter (io.micronaut.context.annotation.Parameter)3 Nullable (io.micronaut.core.annotation.Nullable)3 Page (io.micronaut.data.model.Page)3 Bson (org.bson.conversions.Bson)3 Session (org.hibernate.Session)3 BeanProperty (io.micronaut.core.beans.BeanProperty)2