Search in sources :

Example 6 with Pageable

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;
}
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) Pageable(io.micronaut.data.model.Pageable) MongoFindOptions(io.micronaut.data.mongodb.operations.options.MongoFindOptions) Sort(io.micronaut.data.model.Sort) Bson(org.bson.conversions.Bson)

Example 7 with Pageable

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));
}
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)

Example 8 with Pageable

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

Example 9 with 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;
}
Also used : Pageable(io.micronaut.data.model.Pageable) Sort(io.micronaut.data.model.Sort) NonNull(io.micronaut.core.annotation.NonNull)

Example 10 with Pageable

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

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