chibenwa commented on code in PR #2514:
URL: https://github.com/apache/james-project/pull/2514#discussion_r1858161149


##########
mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java:
##########
@@ -273,31 +270,55 @@ public Mono<Pair<Integer, Integer>> 
countTotalAndUnseenMessagesByMailboxId(Postg
     }
 
     public Flux<Pair<SimpleMailboxMessage.Builder, Record>> 
findMessagesByMailboxId(PostgresMailboxId mailboxId, Limit limit, 
MessageMapper.FetchType fetchType) {
-        PostgresMailboxMessageFetchStrategy fetchStrategy = 
FETCH_TYPE_TO_FETCH_STRATEGY.apply(fetchType);
-        Function<DSLContext, SelectSeekStep1<Record, Long>> queryWithoutLimit 
= dslContext -> dslContext.select(fetchStrategy.fetchFields())
-            .from(MESSAGES_JOIN_MAILBOX_MESSAGES_CONDITION_STEP)
-            .where(MAILBOX_ID.eq(mailboxId.asUuid()))
-            .orderBy(DEFAULT_SORT_ORDER_BY);
+        if (limit.isUnlimited()) {
+            return Flux.range(0, OFFSET_UNLIMITED)
+                .concatMap(offsetIndex -> 
findMessagesByMailboxIdBatch(mailboxId, fetchType, offsetIndex * 
queryBatchSize, queryBatchSize))
+                .takeUntil(List::isEmpty)
+                .flatMapIterable(Function.identity());
+        } else {
+            return findMessagesByMailboxIdBatch(mailboxId, fetchType, 0, 
limit.getLimit().get())
+                .flatMapIterable(Function.identity());
+        }
+    }
 
-        return postgresExecutor.executeRows(dslContext -> limit.getLimit()
-                .map(limitValue -> Flux.from(queryWithoutLimit.andThen(step -> 
step.limit(limitValue)).apply(dslContext)))
-                .orElse(Flux.from(queryWithoutLimit.apply(dslContext))))
-            .map(record -> 
Pair.of(fetchStrategy.toMessageBuilder().apply(record), record));
+    private Mono<List<Pair<SimpleMailboxMessage.Builder, Record>>> 
findMessagesByMailboxIdBatch(PostgresMailboxId mailboxId, 
MessageMapper.FetchType fetchType, int offset, int batchSize) {
+        PostgresMailboxMessageFetchStrategy fetchStrategy = 
FETCH_TYPE_TO_FETCH_STRATEGY.apply(fetchType);
+        return postgresExecutor.executeRows(dslContext -> 
Flux.from(dslContext.select(fetchStrategy.fetchFields())
+                .from(MESSAGES_JOIN_MAILBOX_MESSAGES_CONDITION_STEP)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .orderBy(DEFAULT_SORT_ORDER_BY)
+                .limit(batchSize)
+                .offset(offset)))

Review Comment:
   > Should we also implement this for the Postgres BlobStore DAO?
   
   It would rely on alphabetic ordering, it is likely less important.



##########
mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java:
##########
@@ -273,31 +270,55 @@ public Mono<Pair<Integer, Integer>> 
countTotalAndUnseenMessagesByMailboxId(Postg
     }
 
     public Flux<Pair<SimpleMailboxMessage.Builder, Record>> 
findMessagesByMailboxId(PostgresMailboxId mailboxId, Limit limit, 
MessageMapper.FetchType fetchType) {
-        PostgresMailboxMessageFetchStrategy fetchStrategy = 
FETCH_TYPE_TO_FETCH_STRATEGY.apply(fetchType);
-        Function<DSLContext, SelectSeekStep1<Record, Long>> queryWithoutLimit 
= dslContext -> dslContext.select(fetchStrategy.fetchFields())
-            .from(MESSAGES_JOIN_MAILBOX_MESSAGES_CONDITION_STEP)
-            .where(MAILBOX_ID.eq(mailboxId.asUuid()))
-            .orderBy(DEFAULT_SORT_ORDER_BY);
+        if (limit.isUnlimited()) {
+            return Flux.range(0, OFFSET_UNLIMITED)
+                .concatMap(offsetIndex -> 
findMessagesByMailboxIdBatch(mailboxId, fetchType, offsetIndex * 
queryBatchSize, queryBatchSize))
+                .takeUntil(List::isEmpty)
+                .flatMapIterable(Function.identity());
+        } else {
+            return findMessagesByMailboxIdBatch(mailboxId, fetchType, 0, 
limit.getLimit().get())
+                .flatMapIterable(Function.identity());
+        }
+    }
 
-        return postgresExecutor.executeRows(dslContext -> limit.getLimit()
-                .map(limitValue -> Flux.from(queryWithoutLimit.andThen(step -> 
step.limit(limitValue)).apply(dslContext)))
-                .orElse(Flux.from(queryWithoutLimit.apply(dslContext))))
-            .map(record -> 
Pair.of(fetchStrategy.toMessageBuilder().apply(record), record));
+    private Mono<List<Pair<SimpleMailboxMessage.Builder, Record>>> 
findMessagesByMailboxIdBatch(PostgresMailboxId mailboxId, 
MessageMapper.FetchType fetchType, int offset, int batchSize) {
+        PostgresMailboxMessageFetchStrategy fetchStrategy = 
FETCH_TYPE_TO_FETCH_STRATEGY.apply(fetchType);
+        return postgresExecutor.executeRows(dslContext -> 
Flux.from(dslContext.select(fetchStrategy.fetchFields())
+                .from(MESSAGES_JOIN_MAILBOX_MESSAGES_CONDITION_STEP)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .orderBy(DEFAULT_SORT_ORDER_BY)
+                .limit(batchSize)
+                .offset(offset)))

Review Comment:
   > Should we also implement this for the Postgres BlobStore DAO?
   
   It would rely on alphabetic ordering, it is likely less performant.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org
For additional commands, e-mail: notifications-h...@james.apache.org

Reply via email to