You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2023/12/25 06:32:19 UTC

(james-project) branch postgresql updated (77ca11653c -> 6d1c45fcc6)

This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a change to branch postgresql
in repository https://gitbox.apache.org/repos/asf/james-project.git


    from 77ca11653c JAMES-2586 Remove james-server-cassandra-app direct dependency (#1875)
     new 931fccc8d5 JAMES-2586 Implement AllSearchOverride for Postgresql
     new 4621b0ad62 JAMES-2586 Implement DeletedSearchOverride for Postgresql
     new 6aee4e9297 JAMES-2586 Implement DeletedWithRangeSearchOverride for Postgresql
     new c95f95e221 JAMES-2586 Implement NotDeletedWithRangeSearchOverride for Postgresql
     new bd650deb7e JAMES-2586 Implement UidSearchOverride for Postgresql
     new b367787dae JAMES-2586 Implement UnseenSearchOverrideTest for Postgresql
     new 41f476362d JAMES-2586 Correct search overrides documentation in opensearch.properties
     new a177afee3b JAMES-2586 Refactor search overrides tests for postgresql
     new 6d1c45fcc6 JAMES-2586 Unnecessary join on deleted uid search queries in postgresql

The 9 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../mail/dao/PostgresMailboxMessageDAO.java        |  76 +++++++++++-
 .../postgres}/search/AllSearchOverride.java        |  12 +-
 .../postgres}/search/DeletedSearchOverride.java    |  13 +--
 .../search/DeletedWithRangeSearchOverride.java     |  17 ++-
 .../search/NotDeletedWithRangeSearchOverride.java  |  16 +--
 .../postgres}/search/UidSearchOverride.java        |  12 +-
 .../postgres}/search/UnseenSearchOverride.java     |  14 +--
 .../postgres/search/AllSearchOverrideTest.java     | 128 +++++++++++++++++++++
 .../search/DeletedSearchOverrideTest.java          |  73 ++++++------
 .../search/DeletedWithRangeSearchOverrideTest.java |  79 +++++++------
 .../NotDeletedWithRangeSearchOverrideTest.java     |  93 ++++++---------
 .../postgres/search/SearchOverrideFixture.java     |  71 ++++++++++++
 .../postgres}/search/UidSearchOverrideTest.java    |  91 ++++++---------
 .../postgres}/search/UnseenSearchOverrideTest.java |  83 ++++++-------
 .../opensearch.properties                          |  18 ++-
 15 files changed, 508 insertions(+), 288 deletions(-)
 copy mailbox/{cassandra/src/main/java/org/apache/james/mailbox/cassandra => postgres/src/main/java/org/apache/james/mailbox/postgres}/search/AllSearchOverride.java (87%)
 copy mailbox/{cassandra/src/main/java/org/apache/james/mailbox/cassandra => postgres/src/main/java/org/apache/james/mailbox/postgres}/search/DeletedSearchOverride.java (82%)
 copy mailbox/{cassandra/src/main/java/org/apache/james/mailbox/cassandra => postgres/src/main/java/org/apache/james/mailbox/postgres}/search/DeletedWithRangeSearchOverride.java (81%)
 copy mailbox/{cassandra/src/main/java/org/apache/james/mailbox/cassandra => postgres/src/main/java/org/apache/james/mailbox/postgres}/search/NotDeletedWithRangeSearchOverride.java (84%)
 copy mailbox/{cassandra/src/main/java/org/apache/james/mailbox/cassandra => postgres/src/main/java/org/apache/james/mailbox/postgres}/search/UidSearchOverride.java (87%)
 copy mailbox/{cassandra/src/main/java/org/apache/james/mailbox/cassandra => postgres/src/main/java/org/apache/james/mailbox/postgres}/search/UnseenSearchOverride.java (88%)
 create mode 100644 mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/AllSearchOverrideTest.java
 copy mailbox/{cassandra/src/test/java/org/apache/james/mailbox/cassandra => postgres/src/test/java/org/apache/james/mailbox/postgres}/search/DeletedSearchOverrideTest.java (55%)
 copy mailbox/{cassandra/src/test/java/org/apache/james/mailbox/cassandra => postgres/src/test/java/org/apache/james/mailbox/postgres}/search/DeletedWithRangeSearchOverrideTest.java (59%)
 copy mailbox/{cassandra/src/test/java/org/apache/james/mailbox/cassandra => postgres/src/test/java/org/apache/james/mailbox/postgres}/search/NotDeletedWithRangeSearchOverrideTest.java (55%)
 create mode 100644 mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/SearchOverrideFixture.java
 copy mailbox/{cassandra/src/test/java/org/apache/james/mailbox/cassandra => postgres/src/test/java/org/apache/james/mailbox/postgres}/search/UidSearchOverrideTest.java (51%)
 copy mailbox/{cassandra/src/test/java/org/apache/james/mailbox/cassandra => postgres/src/test/java/org/apache/james/mailbox/postgres}/search/UnseenSearchOverrideTest.java (66%)


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


(james-project) 02/09: JAMES-2586 Implement DeletedSearchOverride for Postgresql

Posted by rc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch postgresql
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 4621b0ad62b907cde1bf9d1e11b83d833e4bd6f5
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Fri Dec 22 13:56:51 2023 +0700

    JAMES-2586 Implement DeletedSearchOverride for Postgresql
---
 .../postgres/search/DeletedSearchOverride.java     |  54 +++++++
 .../postgres/search/DeletedSearchOverrideTest.java | 171 +++++++++++++++++++++
 2 files changed, 225 insertions(+)

diff --git a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/DeletedSearchOverride.java b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/DeletedSearchOverride.java
new file mode 100644
index 0000000000..48f365274e
--- /dev/null
+++ b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/DeletedSearchOverride.java
@@ -0,0 +1,54 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.postgres.search;
+
+import javax.inject.Inject;
+import javax.mail.Flags;
+
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.model.Mailbox;
+import org.apache.james.mailbox.model.SearchQuery;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
+import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex;
+
+import reactor.core.publisher.Flux;
+
+public class DeletedSearchOverride implements ListeningMessageSearchIndex.SearchOverride {
+    private final PostgresMailboxMessageDAO dao;
+
+    @Inject
+    public DeletedSearchOverride(PostgresMailboxMessageDAO dao) {
+        this.dao = dao;
+    }
+
+    @Override
+    public boolean applicable(SearchQuery searchQuery, MailboxSession session) {
+        return searchQuery.getCriteria().size() == 1
+            && searchQuery.getCriteria().get(0).equals(SearchQuery.flagIsSet(Flags.Flag.DELETED))
+            && searchQuery.getSorts().equals(SearchQuery.DEFAULT_SORTS);
+    }
+
+    @Override
+    public Flux<MessageUid> search(MailboxSession session, Mailbox mailbox, SearchQuery searchQuery) {
+        return dao.findDeletedMessagesByMailboxId((PostgresMailboxId) mailbox.getMailboxId());
+    }
+}
diff --git a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/DeletedSearchOverrideTest.java b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/DeletedSearchOverrideTest.java
new file mode 100644
index 0000000000..29e68fdfd5
--- /dev/null
+++ b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/DeletedSearchOverrideTest.java
@@ -0,0 +1,171 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.postgres.search;
+
+import static javax.mail.Flags.Flag.DELETED;
+import static javax.mail.Flags.Flag.SEEN;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Date;
+
+import javax.mail.Flags;
+
+import org.apache.james.backends.postgres.PostgresExtension;
+import org.apache.james.core.Username;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MailboxSessionUtil;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.ModSeq;
+import org.apache.james.mailbox.model.ByteContent;
+import org.apache.james.mailbox.model.Mailbox;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.SearchQuery;
+import org.apache.james.mailbox.model.ThreadId;
+import org.apache.james.mailbox.model.UidValidity;
+import org.apache.james.mailbox.postgres.PostgresMailboxAggregateModule;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.PostgresMessageId;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMessageDAO;
+import org.apache.james.mailbox.store.mail.model.MailboxMessage;
+import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+public class DeletedSearchOverrideTest {
+    private static final MailboxSession MAILBOX_SESSION = MailboxSessionUtil.create(Username.of("benwa"));
+    private static final Mailbox MAILBOX = new Mailbox(MailboxPath.inbox(MAILBOX_SESSION), UidValidity.of(12), PostgresMailboxId.generate());
+    private static final String BLOB_ID = "abc";
+    private static final Charset MESSAGE_CHARSET = StandardCharsets.UTF_8;
+    private static final String MESSAGE_CONTENT = "Simple message content";
+    private static final byte[] MESSAGE_CONTENT_BYTES = MESSAGE_CONTENT.getBytes(MESSAGE_CHARSET);
+    private static final ByteContent CONTENT_STREAM = new ByteContent(MESSAGE_CONTENT_BYTES);
+    private final static long SIZE = MESSAGE_CONTENT_BYTES.length;
+
+    @RegisterExtension
+    static PostgresExtension postgresExtension = PostgresExtension.withoutRowLevelSecurity(PostgresMailboxAggregateModule.MODULE);
+
+    private PostgresMailboxMessageDAO postgresMailboxMessageDAO;
+    private PostgresMessageDAO postgresMessageDAO;
+    private DeletedSearchOverride testee;
+
+    @BeforeEach
+    void setUp() {
+        postgresMessageDAO = new PostgresMessageDAO(postgresExtension.getPostgresExecutor());
+        postgresMailboxMessageDAO = new PostgresMailboxMessageDAO(postgresExtension.getPostgresExecutor());
+        testee = new DeletedSearchOverride(postgresMailboxMessageDAO);
+    }
+
+    @Test
+    void deletedQueryShouldBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.flagIsSet(DELETED))
+                .build(),
+            MAILBOX_SESSION))
+            .isTrue();
+    }
+
+    @Test
+    void sizeQueryShouldNotBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.sizeEquals(12))
+                .build(),
+            MAILBOX_SESSION))
+            .isFalse();
+    }
+
+    @Test
+    void searchShouldReturnEmptyByDefault() {
+        assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.flagIsUnSet(SEEN))
+                .build()).collectList().block())
+            .isEmpty();
+    }
+
+    @Test
+    void searchShouldReturnMailboxEntries() {
+        MessageUid messageUid = MessageUid.of(1);
+        PostgresMessageId messageId = new PostgresMessageId.Factory().generate();
+        MailboxMessage message1 = SimpleMailboxMessage.builder()
+            .messageId(messageId)
+            .threadId(ThreadId.fromBaseMessageId(messageId))
+            .uid(messageUid)
+            .content(CONTENT_STREAM)
+            .size(SIZE)
+            .internalDate(new Date())
+            .bodyStartOctet(18)
+            .flags(new Flags(DELETED))
+            .properties(new PropertyBuilder())
+            .mailboxId(MAILBOX.getMailboxId())
+            .modseq(ModSeq.of(1))
+            .build();
+        postgresMessageDAO.insert(message1, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message1).block();
+
+        MessageUid messageUid2 = MessageUid.of(2);
+        PostgresMessageId messageId2 = new PostgresMessageId.Factory().generate();
+        MailboxMessage message2 = SimpleMailboxMessage.builder()
+            .messageId(messageId2)
+            .threadId(ThreadId.fromBaseMessageId(messageId2))
+            .uid(messageUid2)
+            .content(CONTENT_STREAM)
+            .size(SIZE)
+            .internalDate(new Date())
+            .bodyStartOctet(18)
+            .flags(new Flags(DELETED))
+            .properties(new PropertyBuilder())
+            .mailboxId(MAILBOX.getMailboxId())
+            .modseq(ModSeq.of(1))
+            .build();
+        postgresMessageDAO.insert(message2, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message2).block();
+
+        MessageUid messageUid3 = MessageUid.of(3);
+        PostgresMessageId messageId3 = new PostgresMessageId.Factory().generate();
+        MailboxMessage message3 = SimpleMailboxMessage.builder()
+            .messageId(messageId3)
+            .threadId(ThreadId.fromBaseMessageId(messageId3))
+            .uid(messageUid3)
+            .content(CONTENT_STREAM)
+            .size(SIZE)
+            .internalDate(new Date())
+            .bodyStartOctet(18)
+            .flags(new Flags())
+            .properties(new PropertyBuilder())
+            .mailboxId(MAILBOX.getMailboxId())
+            .modseq(ModSeq.of(1))
+            .build();
+        postgresMessageDAO.insert(message3, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message3).block();
+
+        assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.flagIsSet(DELETED))
+                .build()).collectList().block())
+            .containsOnly(messageUid, messageUid2);
+    }
+}


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


(james-project) 09/09: JAMES-2586 Unnecessary join on deleted uid search queries in postgresql

Posted by rc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch postgresql
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 6d1c45fcc6cdfafa2c20b5788f1e3f73a78c9955
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Fri Dec 22 16:40:24 2023 +0700

    JAMES-2586 Unnecessary join on deleted uid search queries in postgresql
---
 .../mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java      | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java
index 61ff87a764..01dd1c752a 100644
--- a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java
+++ b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java
@@ -298,7 +298,7 @@ public class PostgresMailboxMessageDAO {
 
     public Flux<MessageUid> findDeletedMessagesByMailboxId(PostgresMailboxId mailboxId) {
         return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_UID)
-                .from(MESSAGES_JOIN_MAILBOX_MESSAGES_CONDITION_STEP)
+                .from(TABLE_NAME)
                 .where(MAILBOX_ID.eq(mailboxId.asUuid()))
                 .and(IS_DELETED.eq(true))
                 .orderBy(DEFAULT_SORT_ORDER_BY)))
@@ -307,7 +307,7 @@ public class PostgresMailboxMessageDAO {
 
     public Flux<MessageUid> findDeletedMessagesByMailboxIdAndBetweenUIDs(PostgresMailboxId mailboxId, MessageUid from, MessageUid to) {
         return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_UID)
-                .from(MESSAGES_JOIN_MAILBOX_MESSAGES_CONDITION_STEP)
+                .from(TABLE_NAME)
                 .where(MAILBOX_ID.eq(mailboxId.asUuid()))
                 .and(IS_DELETED.eq(true))
                 .and(MESSAGE_UID.greaterOrEqual(from.asLong()))
@@ -318,7 +318,7 @@ public class PostgresMailboxMessageDAO {
 
     public Flux<MessageUid> findDeletedMessagesByMailboxIdAndAfterUID(PostgresMailboxId mailboxId, MessageUid from) {
         return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_UID)
-                .from(MESSAGES_JOIN_MAILBOX_MESSAGES_CONDITION_STEP)
+                .from(TABLE_NAME)
                 .where(MAILBOX_ID.eq(mailboxId.asUuid()))
                 .and(IS_DELETED.eq(true))
                 .and(MESSAGE_UID.greaterOrEqual(from.asLong()))
@@ -328,7 +328,7 @@ public class PostgresMailboxMessageDAO {
 
     public Mono<MessageUid> findDeletedMessageByMailboxIdAndUid(PostgresMailboxId mailboxId, MessageUid uid) {
         return postgresExecutor.executeRow(dslContext -> Mono.from(dslContext.select(MESSAGE_UID)
-                .from(MESSAGES_JOIN_MAILBOX_MESSAGES_CONDITION_STEP)
+                .from(TABLE_NAME)
                 .where(MAILBOX_ID.eq(mailboxId.asUuid()))
                 .and(IS_DELETED.eq(true))
                 .and(MESSAGE_UID.eq(uid.asLong()))))


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


(james-project) 01/09: JAMES-2586 Implement AllSearchOverride for Postgresql

Posted by rc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch postgresql
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 931fccc8d544af3a7a3a368e9e7ef65a9534df19
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Thu Dec 21 17:33:45 2023 +0700

    JAMES-2586 Implement AllSearchOverride for Postgresql
---
 .../mailbox/postgres/search/AllSearchOverride.java |  70 ++++++++
 .../postgres/search/AllSearchOverrideTest.java     | 188 +++++++++++++++++++++
 2 files changed, 258 insertions(+)

diff --git a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/AllSearchOverride.java b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/AllSearchOverride.java
new file mode 100644
index 0000000000..f073ae061b
--- /dev/null
+++ b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/AllSearchOverride.java
@@ -0,0 +1,70 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.postgres.search;
+
+import javax.inject.Inject;
+
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.model.Mailbox;
+import org.apache.james.mailbox.model.SearchQuery;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
+import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex;
+
+import reactor.core.publisher.Flux;
+
+public class AllSearchOverride implements ListeningMessageSearchIndex.SearchOverride {
+    private final PostgresMailboxMessageDAO dao;
+
+    @Inject
+    public AllSearchOverride(PostgresMailboxMessageDAO dao) {
+        this.dao = dao;
+    }
+
+    @Override
+    public boolean applicable(SearchQuery searchQuery, MailboxSession session) {
+        return isAll(searchQuery)
+            || isFromOne(searchQuery)
+            || isEmpty(searchQuery);
+    }
+
+    private boolean isAll(SearchQuery searchQuery) {
+        return searchQuery.getCriteria().size() == 1
+            && searchQuery.getCriteria().get(0).equals(SearchQuery.uid(new SearchQuery.UidRange(MessageUid.MIN_VALUE, MessageUid.MAX_VALUE)))
+            && searchQuery.getSorts().equals(SearchQuery.DEFAULT_SORTS);
+    }
+
+    private boolean isFromOne(SearchQuery searchQuery) {
+        return searchQuery.getCriteria().size() == 1
+            && searchQuery.getCriteria().get(0).equals(SearchQuery.all())
+            && searchQuery.getSorts().equals(SearchQuery.DEFAULT_SORTS);
+    }
+
+    private boolean isEmpty(SearchQuery searchQuery) {
+        return searchQuery.getCriteria().isEmpty()
+            && searchQuery.getSorts().equals(SearchQuery.DEFAULT_SORTS);
+    }
+
+    @Override
+    public Flux<MessageUid> search(MailboxSession session, Mailbox mailbox, SearchQuery searchQuery) {
+        return dao.listAllMessageUid((PostgresMailboxId) mailbox.getMailboxId());
+    }
+}
diff --git a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/AllSearchOverrideTest.java b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/AllSearchOverrideTest.java
new file mode 100644
index 0000000000..a96348eb56
--- /dev/null
+++ b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/AllSearchOverrideTest.java
@@ -0,0 +1,188 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.postgres.search;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Date;
+
+import javax.mail.Flags;
+
+import org.apache.james.backends.postgres.PostgresExtension;
+import org.apache.james.core.Username;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MailboxSessionUtil;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.ModSeq;
+import org.apache.james.mailbox.model.ByteContent;
+import org.apache.james.mailbox.model.Mailbox;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.SearchQuery;
+import org.apache.james.mailbox.model.ThreadId;
+import org.apache.james.mailbox.model.UidValidity;
+import org.apache.james.mailbox.postgres.PostgresMailboxAggregateModule;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.PostgresMessageId;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMessageDAO;
+import org.apache.james.mailbox.store.mail.model.MailboxMessage;
+import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+public class AllSearchOverrideTest {
+    private static final MailboxSession MAILBOX_SESSION = MailboxSessionUtil.create(Username.of("benwa"));
+    private static final Mailbox MAILBOX = new Mailbox(MailboxPath.inbox(MAILBOX_SESSION), UidValidity.of(12), PostgresMailboxId.generate());
+    private static final String BLOB_ID = "abc";
+    private static final Charset MESSAGE_CHARSET = StandardCharsets.UTF_8;
+    private static final String MESSAGE_CONTENT = "Simple message content";
+    private static final byte[] MESSAGE_CONTENT_BYTES = MESSAGE_CONTENT.getBytes(MESSAGE_CHARSET);
+    private static final ByteContent CONTENT_STREAM = new ByteContent(MESSAGE_CONTENT_BYTES);
+    private final static long SIZE = MESSAGE_CONTENT_BYTES.length;
+
+    @RegisterExtension
+    static PostgresExtension postgresExtension = PostgresExtension.withoutRowLevelSecurity(PostgresMailboxAggregateModule.MODULE);
+
+    private PostgresMailboxMessageDAO postgresMailboxMessageDAO;
+    private PostgresMessageDAO postgresMessageDAO;
+    private AllSearchOverride testee;
+
+    @BeforeEach
+    void setUp() {
+        postgresMessageDAO = new PostgresMessageDAO(postgresExtension.getPostgresExecutor());
+        postgresMailboxMessageDAO = new PostgresMailboxMessageDAO(postgresExtension.getPostgresExecutor());
+        testee = new AllSearchOverride(postgresMailboxMessageDAO);
+    }
+
+    @Test
+    void emptyQueryShouldBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .build(),
+            MAILBOX_SESSION))
+            .isTrue();
+    }
+
+    @Test
+    void allQueryShouldBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.all())
+                .build(),
+            MAILBOX_SESSION))
+            .isTrue();
+    }
+
+    @Test
+    void fromOneQueryShouldBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.uid(new SearchQuery.UidRange(MessageUid.MIN_VALUE, MessageUid.MAX_VALUE)))
+                .build(),
+            MAILBOX_SESSION))
+            .isTrue();
+    }
+
+    @Test
+    void sizeQueryShouldNotBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.sizeEquals(12))
+                .build(),
+            MAILBOX_SESSION))
+            .isFalse();
+    }
+
+    @Test
+    void searchShouldReturnEmptyByDefault() {
+        assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.all())
+                .build()).collectList().block())
+            .isEmpty();
+    }
+
+    @Test
+    void searchShouldReturnMailboxEntries() {
+        MessageUid messageUid = MessageUid.of(1);
+        PostgresMessageId messageId = new PostgresMessageId.Factory().generate();
+        MailboxMessage message1 = SimpleMailboxMessage.builder()
+            .messageId(messageId)
+            .threadId(ThreadId.fromBaseMessageId(messageId))
+            .uid(messageUid)
+            .content(CONTENT_STREAM)
+            .size(SIZE)
+            .internalDate(new Date())
+            .bodyStartOctet(18)
+            .flags(new Flags())
+            .properties(new PropertyBuilder())
+            .mailboxId(MAILBOX.getMailboxId())
+            .modseq(ModSeq.of(1))
+            .build();
+        postgresMessageDAO.insert(message1, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message1).block();
+
+        MessageUid messageUid2 = MessageUid.of(2);
+        PostgresMessageId messageId2 = new PostgresMessageId.Factory().generate();
+        MailboxMessage message2 = SimpleMailboxMessage.builder()
+            .messageId(messageId2)
+            .threadId(ThreadId.fromBaseMessageId(messageId2))
+            .uid(messageUid2)
+            .content(CONTENT_STREAM)
+            .size(SIZE)
+            .internalDate(new Date())
+            .bodyStartOctet(18)
+            .flags(new Flags())
+            .properties(new PropertyBuilder())
+            .mailboxId(MAILBOX.getMailboxId())
+            .modseq(ModSeq.of(1))
+            .build();
+        postgresMessageDAO.insert(message2, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message2).block();
+
+        MessageUid messageUid3 = MessageUid.of(3);
+        PostgresMessageId messageId3 = new PostgresMessageId.Factory().generate();
+        MailboxMessage message3 = SimpleMailboxMessage.builder()
+            .messageId(messageId3)
+            .threadId(ThreadId.fromBaseMessageId(messageId3))
+            .uid(messageUid3)
+            .content(CONTENT_STREAM)
+            .size(SIZE)
+            .internalDate(new Date())
+            .bodyStartOctet(18)
+            .flags(new Flags())
+            .properties(new PropertyBuilder())
+            .mailboxId(PostgresMailboxId.generate())
+            .modseq(ModSeq.of(1))
+            .build();
+        postgresMessageDAO.insert(message3, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message3).block();
+
+        assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.all())
+                .build()).collectList().block())
+            .containsOnly(messageUid, messageUid2);
+    }
+}


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


(james-project) 04/09: JAMES-2586 Implement NotDeletedWithRangeSearchOverride for Postgresql

Posted by rc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch postgresql
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit c95f95e221e34c7d5283c634d4ae0bf39795d271
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Fri Dec 22 14:57:50 2023 +0700

    JAMES-2586 Implement NotDeletedWithRangeSearchOverride for Postgresql
---
 .../mail/dao/PostgresMailboxMessageDAO.java        |  11 ++
 .../search/NotDeletedWithRangeSearchOverride.java  |  79 ++++++++++
 .../NotDeletedWithRangeSearchOverrideTest.java     | 163 +++++++++++++++++++++
 3 files changed, 253 insertions(+)

diff --git a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java
index c8d4f287a5..049cd40f7b 100644
--- a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java
+++ b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java
@@ -278,6 +278,17 @@ public class PostgresMailboxMessageDAO {
             .map(RECORD_TO_MESSAGE_UID_FUNCTION);
     }
 
+    public Flux<MessageUid> listNotDeletedUids(PostgresMailboxId mailboxId, MessageRange range) {
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_UID, IS_DELETED)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .and(MESSAGE_UID.greaterOrEqual(range.getUidFrom().asLong()))
+                .and(MESSAGE_UID.lessOrEqual(range.getUidTo().asLong()))
+                .orderBy(DEFAULT_SORT_ORDER_BY)))
+            .filter(record -> !record.get(IS_DELETED))
+            .map(RECORD_TO_MESSAGE_UID_FUNCTION);
+    }
+
     public Flux<ComposedMessageIdWithMetaData> findMessagesMetadata(PostgresMailboxId mailboxId, MessageRange range) {
         return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select()
                 .from(TABLE_NAME)
diff --git a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/NotDeletedWithRangeSearchOverride.java b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/NotDeletedWithRangeSearchOverride.java
new file mode 100644
index 0000000000..5e0e342dba
--- /dev/null
+++ b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/NotDeletedWithRangeSearchOverride.java
@@ -0,0 +1,79 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.postgres.search;
+
+import javax.inject.Inject;
+import javax.mail.Flags;
+
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.model.Mailbox;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.mailbox.model.SearchQuery;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
+import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex;
+
+import reactor.core.publisher.Flux;
+
+public class NotDeletedWithRangeSearchOverride implements ListeningMessageSearchIndex.SearchOverride {
+    private final PostgresMailboxMessageDAO dao;
+
+    @Inject
+    public NotDeletedWithRangeSearchOverride(PostgresMailboxMessageDAO dao) {
+        this.dao = dao;
+    }
+
+    @Override
+    public boolean applicable(SearchQuery searchQuery, MailboxSession session) {
+        return isDeletedUnset(searchQuery) || isDeletedNotSet(searchQuery);
+    }
+
+    private boolean isDeletedUnset(SearchQuery searchQuery) {
+        return searchQuery.getCriteria().size() == 2
+            && searchQuery.getCriteria().contains(SearchQuery.flagIsUnSet(Flags.Flag.DELETED))
+            && searchQuery.getCriteria().stream()
+            .anyMatch(criterion -> criterion instanceof SearchQuery.UidCriterion)
+            && searchQuery.getSorts().equals(SearchQuery.DEFAULT_SORTS);
+    }
+
+    private boolean isDeletedNotSet(SearchQuery searchQuery) {
+        return searchQuery.getCriteria().size() == 2
+            && searchQuery.getCriteria().contains(SearchQuery.not(SearchQuery.flagIsSet(Flags.Flag.DELETED)))
+            && searchQuery.getCriteria().stream()
+            .anyMatch(criterion -> criterion instanceof SearchQuery.UidCriterion)
+            && searchQuery.getSorts().equals(SearchQuery.DEFAULT_SORTS);
+    }
+
+    @Override
+    public Flux<MessageUid> search(MailboxSession session, Mailbox mailbox, SearchQuery searchQuery) {
+        SearchQuery.UidCriterion uidArgument = searchQuery.getCriteria().stream()
+            .filter(criterion -> criterion instanceof SearchQuery.UidCriterion)
+            .map(SearchQuery.UidCriterion.class::cast)
+            .findAny()
+            .orElseThrow(() -> new RuntimeException("Missing Uid argument"));
+
+        SearchQuery.UidRange[] uidRanges = uidArgument.getOperator().getRange();
+
+        return Flux.fromArray(uidRanges)
+            .concatMap(range -> dao.listNotDeletedUids((PostgresMailboxId) mailbox.getMailboxId(),
+                MessageRange.range(range.getLowValue(), range.getHighValue())));
+    }
+}
diff --git a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/NotDeletedWithRangeSearchOverrideTest.java b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/NotDeletedWithRangeSearchOverrideTest.java
new file mode 100644
index 0000000000..7ee4d9f9cc
--- /dev/null
+++ b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/NotDeletedWithRangeSearchOverrideTest.java
@@ -0,0 +1,163 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.postgres.search;
+
+import static javax.mail.Flags.Flag.DELETED;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Date;
+
+import javax.mail.Flags;
+
+import org.apache.james.backends.postgres.PostgresExtension;
+import org.apache.james.core.Username;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MailboxSessionUtil;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.ModSeq;
+import org.apache.james.mailbox.model.ByteContent;
+import org.apache.james.mailbox.model.Mailbox;
+import org.apache.james.mailbox.model.MailboxId;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.SearchQuery;
+import org.apache.james.mailbox.model.ThreadId;
+import org.apache.james.mailbox.model.UidValidity;
+import org.apache.james.mailbox.postgres.PostgresMailboxAggregateModule;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.PostgresMessageId;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMessageDAO;
+import org.apache.james.mailbox.store.mail.model.MailboxMessage;
+import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+public class NotDeletedWithRangeSearchOverrideTest {
+    private static final MailboxSession MAILBOX_SESSION = MailboxSessionUtil.create(Username.of("benwa"));
+    private static final Mailbox MAILBOX = new Mailbox(MailboxPath.inbox(MAILBOX_SESSION), UidValidity.of(12), PostgresMailboxId.generate());
+    private static final String BLOB_ID = "abc";
+    private static final Charset MESSAGE_CHARSET = StandardCharsets.UTF_8;
+    private static final String MESSAGE_CONTENT = "Simple message content";
+    private static final byte[] MESSAGE_CONTENT_BYTES = MESSAGE_CONTENT.getBytes(MESSAGE_CHARSET);
+    private static final ByteContent CONTENT_STREAM = new ByteContent(MESSAGE_CONTENT_BYTES);
+    private final static long SIZE = MESSAGE_CONTENT_BYTES.length;
+
+    @RegisterExtension
+    static PostgresExtension postgresExtension = PostgresExtension.withoutRowLevelSecurity(PostgresMailboxAggregateModule.MODULE);
+
+    private PostgresMailboxMessageDAO postgresMailboxMessageDAO;
+    private PostgresMessageDAO postgresMessageDAO;
+    private NotDeletedWithRangeSearchOverride testee;
+
+    @BeforeEach
+    void setUp() {
+        postgresMessageDAO = new PostgresMessageDAO(postgresExtension.getPostgresExecutor());
+        postgresMailboxMessageDAO = new PostgresMailboxMessageDAO(postgresExtension.getPostgresExecutor());
+        testee = new NotDeletedWithRangeSearchOverride(postgresMailboxMessageDAO);
+    }
+
+    @Test
+    void undeletedRangeQueryShouldBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.flagIsUnSet(DELETED))
+                .andCriteria(SearchQuery.uid(new SearchQuery.UidRange(MessageUid.of(4), MessageUid.of(45))))
+                .build(),
+            MAILBOX_SESSION))
+            .isTrue();
+    }
+
+    @Test
+    void notDeletedRangeQueryShouldBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.not(SearchQuery.flagIsSet(DELETED)))
+                .andCriteria(SearchQuery.uid(new SearchQuery.UidRange(MessageUid.of(4), MessageUid.of(45))))
+                .build(),
+            MAILBOX_SESSION))
+            .isTrue();
+    }
+
+    @Test
+    void sizeQueryShouldNotBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.sizeEquals(12))
+                .build(),
+            MAILBOX_SESSION))
+            .isFalse();
+    }
+
+    @Test
+    void searchShouldReturnEmptyByDefault() {
+        assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.not(SearchQuery.flagIsSet(DELETED)))
+                .andCriteria(SearchQuery.uid(new SearchQuery.UidRange(MessageUid.of(34), MessageUid.of(345))))
+                .build()).collectList().block())
+            .isEmpty();
+    }
+
+    @Test
+    void searchShouldReturnMailboxEntries() {
+        MessageUid messageUid = MessageUid.of(1);
+        insert(messageUid, MAILBOX.getMailboxId(), new Flags());
+        MessageUid messageUid2 = MessageUid.of(2);
+        insert(messageUid2, MAILBOX.getMailboxId(), new Flags());
+        MessageUid messageUid3 = MessageUid.of(3);
+        insert(messageUid3, MAILBOX.getMailboxId(), new Flags(DELETED));
+        MessageUid messageUid4 = MessageUid.of(4);
+        insert(messageUid4, MAILBOX.getMailboxId(), new Flags());
+        MessageUid messageUid5 = MessageUid.of(5);
+        insert(messageUid5, MAILBOX.getMailboxId(), new Flags());
+        MessageUid messageUid6 = MessageUid.of(6);
+        insert(messageUid6, PostgresMailboxId.generate(), new Flags(DELETED));
+
+        assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.not(SearchQuery.flagIsSet(DELETED)))
+                .andCriteria(SearchQuery.uid(new SearchQuery.UidRange(messageUid2, messageUid4)))
+                .build()).collectList().block())
+            .containsOnly(messageUid2, messageUid4);
+    }
+
+    private void insert(MessageUid messageUid, MailboxId mailboxId, Flags flags) {
+        PostgresMessageId messageId = new PostgresMessageId.Factory().generate();
+        MailboxMessage message = SimpleMailboxMessage.builder()
+            .messageId(messageId)
+            .threadId(ThreadId.fromBaseMessageId(messageId))
+            .uid(messageUid)
+            .content(CONTENT_STREAM)
+            .size(SIZE)
+            .internalDate(new Date())
+            .bodyStartOctet(18)
+            .flags(flags)
+            .properties(new PropertyBuilder())
+            .mailboxId(mailboxId)
+            .modseq(ModSeq.of(1))
+            .build();
+        postgresMessageDAO.insert(message, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message).block();
+    }
+}


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


(james-project) 08/09: JAMES-2586 Refactor search overrides tests for postgresql

Posted by rc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch postgresql
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit a177afee3bd7d8bd15dab9e45432d80decef9f00
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Fri Dec 22 16:33:34 2023 +0700

    JAMES-2586 Refactor search overrides tests for postgresql
---
 .../postgres/search/AllSearchOverrideTest.java     |  86 +++-----------
 .../postgres/search/DeletedSearchOverrideTest.java |  87 +++------------
 .../search/DeletedWithRangeSearchOverrideTest.java | 123 +++------------------
 .../NotDeletedWithRangeSearchOverrideTest.java     |  48 ++------
 .../postgres/search/SearchOverrideFixture.java     |  71 ++++++++++++
 .../postgres/search/UidSearchOverrideTest.java     |  48 ++------
 .../postgres/search/UnseenSearchOverrideTest.java  |  47 ++------
 7 files changed, 138 insertions(+), 372 deletions(-)

diff --git a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/AllSearchOverrideTest.java b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/AllSearchOverrideTest.java
index a96348eb56..821c1ce05e 100644
--- a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/AllSearchOverrideTest.java
+++ b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/AllSearchOverrideTest.java
@@ -19,48 +19,27 @@
 
 package org.apache.james.mailbox.postgres.search;
 
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.BLOB_ID;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.MAILBOX;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.MAILBOX_SESSION;
 import static org.assertj.core.api.Assertions.assertThat;
 
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.util.Date;
-
 import javax.mail.Flags;
 
 import org.apache.james.backends.postgres.PostgresExtension;
-import org.apache.james.core.Username;
-import org.apache.james.mailbox.MailboxSession;
-import org.apache.james.mailbox.MailboxSessionUtil;
 import org.apache.james.mailbox.MessageUid;
-import org.apache.james.mailbox.ModSeq;
-import org.apache.james.mailbox.model.ByteContent;
-import org.apache.james.mailbox.model.Mailbox;
-import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.SearchQuery;
-import org.apache.james.mailbox.model.ThreadId;
-import org.apache.james.mailbox.model.UidValidity;
 import org.apache.james.mailbox.postgres.PostgresMailboxAggregateModule;
 import org.apache.james.mailbox.postgres.PostgresMailboxId;
-import org.apache.james.mailbox.postgres.PostgresMessageId;
 import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
 import org.apache.james.mailbox.postgres.mail.dao.PostgresMessageDAO;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
-import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
-import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
 public class AllSearchOverrideTest {
-    private static final MailboxSession MAILBOX_SESSION = MailboxSessionUtil.create(Username.of("benwa"));
-    private static final Mailbox MAILBOX = new Mailbox(MailboxPath.inbox(MAILBOX_SESSION), UidValidity.of(12), PostgresMailboxId.generate());
-    private static final String BLOB_ID = "abc";
-    private static final Charset MESSAGE_CHARSET = StandardCharsets.UTF_8;
-    private static final String MESSAGE_CONTENT = "Simple message content";
-    private static final byte[] MESSAGE_CONTENT_BYTES = MESSAGE_CONTENT.getBytes(MESSAGE_CHARSET);
-    private static final ByteContent CONTENT_STREAM = new ByteContent(MESSAGE_CONTENT_BYTES);
-    private final static long SIZE = MESSAGE_CONTENT_BYTES.length;
-
     @RegisterExtension
     static PostgresExtension postgresExtension = PostgresExtension.withoutRowLevelSecurity(PostgresMailboxAggregateModule.MODULE);
 
@@ -126,58 +105,13 @@ public class AllSearchOverrideTest {
     @Test
     void searchShouldReturnMailboxEntries() {
         MessageUid messageUid = MessageUid.of(1);
-        PostgresMessageId messageId = new PostgresMessageId.Factory().generate();
-        MailboxMessage message1 = SimpleMailboxMessage.builder()
-            .messageId(messageId)
-            .threadId(ThreadId.fromBaseMessageId(messageId))
-            .uid(messageUid)
-            .content(CONTENT_STREAM)
-            .size(SIZE)
-            .internalDate(new Date())
-            .bodyStartOctet(18)
-            .flags(new Flags())
-            .properties(new PropertyBuilder())
-            .mailboxId(MAILBOX.getMailboxId())
-            .modseq(ModSeq.of(1))
-            .build();
-        postgresMessageDAO.insert(message1, BLOB_ID).block();
-        postgresMailboxMessageDAO.insert(message1).block();
+        insert(messageUid, MAILBOX.getMailboxId());
 
         MessageUid messageUid2 = MessageUid.of(2);
-        PostgresMessageId messageId2 = new PostgresMessageId.Factory().generate();
-        MailboxMessage message2 = SimpleMailboxMessage.builder()
-            .messageId(messageId2)
-            .threadId(ThreadId.fromBaseMessageId(messageId2))
-            .uid(messageUid2)
-            .content(CONTENT_STREAM)
-            .size(SIZE)
-            .internalDate(new Date())
-            .bodyStartOctet(18)
-            .flags(new Flags())
-            .properties(new PropertyBuilder())
-            .mailboxId(MAILBOX.getMailboxId())
-            .modseq(ModSeq.of(1))
-            .build();
-        postgresMessageDAO.insert(message2, BLOB_ID).block();
-        postgresMailboxMessageDAO.insert(message2).block();
+        insert(messageUid2, MAILBOX.getMailboxId());
 
         MessageUid messageUid3 = MessageUid.of(3);
-        PostgresMessageId messageId3 = new PostgresMessageId.Factory().generate();
-        MailboxMessage message3 = SimpleMailboxMessage.builder()
-            .messageId(messageId3)
-            .threadId(ThreadId.fromBaseMessageId(messageId3))
-            .uid(messageUid3)
-            .content(CONTENT_STREAM)
-            .size(SIZE)
-            .internalDate(new Date())
-            .bodyStartOctet(18)
-            .flags(new Flags())
-            .properties(new PropertyBuilder())
-            .mailboxId(PostgresMailboxId.generate())
-            .modseq(ModSeq.of(1))
-            .build();
-        postgresMessageDAO.insert(message3, BLOB_ID).block();
-        postgresMailboxMessageDAO.insert(message3).block();
+        insert(messageUid3, PostgresMailboxId.generate());
 
         assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
             SearchQuery.builder()
@@ -185,4 +119,10 @@ public class AllSearchOverrideTest {
                 .build()).collectList().block())
             .containsOnly(messageUid, messageUid2);
     }
+
+    private void insert(MessageUid messageUid, MailboxId mailboxId) {
+        MailboxMessage message = SearchOverrideFixture.createMessage(messageUid, mailboxId, new Flags());
+        postgresMessageDAO.insert(message, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message).block();
+    }
 }
diff --git a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/DeletedSearchOverrideTest.java b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/DeletedSearchOverrideTest.java
index 29e68fdfd5..0bd37d2dc9 100644
--- a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/DeletedSearchOverrideTest.java
+++ b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/DeletedSearchOverrideTest.java
@@ -21,48 +21,26 @@ package org.apache.james.mailbox.postgres.search;
 
 import static javax.mail.Flags.Flag.DELETED;
 import static javax.mail.Flags.Flag.SEEN;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.BLOB_ID;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.MAILBOX;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.MAILBOX_SESSION;
 import static org.assertj.core.api.Assertions.assertThat;
 
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.util.Date;
-
 import javax.mail.Flags;
 
 import org.apache.james.backends.postgres.PostgresExtension;
-import org.apache.james.core.Username;
-import org.apache.james.mailbox.MailboxSession;
-import org.apache.james.mailbox.MailboxSessionUtil;
 import org.apache.james.mailbox.MessageUid;
-import org.apache.james.mailbox.ModSeq;
-import org.apache.james.mailbox.model.ByteContent;
-import org.apache.james.mailbox.model.Mailbox;
-import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.SearchQuery;
-import org.apache.james.mailbox.model.ThreadId;
-import org.apache.james.mailbox.model.UidValidity;
 import org.apache.james.mailbox.postgres.PostgresMailboxAggregateModule;
-import org.apache.james.mailbox.postgres.PostgresMailboxId;
-import org.apache.james.mailbox.postgres.PostgresMessageId;
 import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
 import org.apache.james.mailbox.postgres.mail.dao.PostgresMessageDAO;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
-import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
-import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
 public class DeletedSearchOverrideTest {
-    private static final MailboxSession MAILBOX_SESSION = MailboxSessionUtil.create(Username.of("benwa"));
-    private static final Mailbox MAILBOX = new Mailbox(MailboxPath.inbox(MAILBOX_SESSION), UidValidity.of(12), PostgresMailboxId.generate());
-    private static final String BLOB_ID = "abc";
-    private static final Charset MESSAGE_CHARSET = StandardCharsets.UTF_8;
-    private static final String MESSAGE_CONTENT = "Simple message content";
-    private static final byte[] MESSAGE_CONTENT_BYTES = MESSAGE_CONTENT.getBytes(MESSAGE_CHARSET);
-    private static final ByteContent CONTENT_STREAM = new ByteContent(MESSAGE_CONTENT_BYTES);
-    private final static long SIZE = MESSAGE_CONTENT_BYTES.length;
-
     @RegisterExtension
     static PostgresExtension postgresExtension = PostgresExtension.withoutRowLevelSecurity(PostgresMailboxAggregateModule.MODULE);
 
@@ -109,58 +87,13 @@ public class DeletedSearchOverrideTest {
     @Test
     void searchShouldReturnMailboxEntries() {
         MessageUid messageUid = MessageUid.of(1);
-        PostgresMessageId messageId = new PostgresMessageId.Factory().generate();
-        MailboxMessage message1 = SimpleMailboxMessage.builder()
-            .messageId(messageId)
-            .threadId(ThreadId.fromBaseMessageId(messageId))
-            .uid(messageUid)
-            .content(CONTENT_STREAM)
-            .size(SIZE)
-            .internalDate(new Date())
-            .bodyStartOctet(18)
-            .flags(new Flags(DELETED))
-            .properties(new PropertyBuilder())
-            .mailboxId(MAILBOX.getMailboxId())
-            .modseq(ModSeq.of(1))
-            .build();
-        postgresMessageDAO.insert(message1, BLOB_ID).block();
-        postgresMailboxMessageDAO.insert(message1).block();
+        insert(messageUid, MAILBOX.getMailboxId(), new Flags(DELETED));
 
         MessageUid messageUid2 = MessageUid.of(2);
-        PostgresMessageId messageId2 = new PostgresMessageId.Factory().generate();
-        MailboxMessage message2 = SimpleMailboxMessage.builder()
-            .messageId(messageId2)
-            .threadId(ThreadId.fromBaseMessageId(messageId2))
-            .uid(messageUid2)
-            .content(CONTENT_STREAM)
-            .size(SIZE)
-            .internalDate(new Date())
-            .bodyStartOctet(18)
-            .flags(new Flags(DELETED))
-            .properties(new PropertyBuilder())
-            .mailboxId(MAILBOX.getMailboxId())
-            .modseq(ModSeq.of(1))
-            .build();
-        postgresMessageDAO.insert(message2, BLOB_ID).block();
-        postgresMailboxMessageDAO.insert(message2).block();
+        insert(messageUid2, MAILBOX.getMailboxId(), new Flags(DELETED));
 
         MessageUid messageUid3 = MessageUid.of(3);
-        PostgresMessageId messageId3 = new PostgresMessageId.Factory().generate();
-        MailboxMessage message3 = SimpleMailboxMessage.builder()
-            .messageId(messageId3)
-            .threadId(ThreadId.fromBaseMessageId(messageId3))
-            .uid(messageUid3)
-            .content(CONTENT_STREAM)
-            .size(SIZE)
-            .internalDate(new Date())
-            .bodyStartOctet(18)
-            .flags(new Flags())
-            .properties(new PropertyBuilder())
-            .mailboxId(MAILBOX.getMailboxId())
-            .modseq(ModSeq.of(1))
-            .build();
-        postgresMessageDAO.insert(message3, BLOB_ID).block();
-        postgresMailboxMessageDAO.insert(message3).block();
+        insert(messageUid3, MAILBOX.getMailboxId(), new Flags());
 
         assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
             SearchQuery.builder()
@@ -168,4 +101,10 @@ public class DeletedSearchOverrideTest {
                 .build()).collectList().block())
             .containsOnly(messageUid, messageUid2);
     }
+
+    private void insert(MessageUid messageUid, MailboxId mailboxId, Flags flags) {
+        MailboxMessage message = SearchOverrideFixture.createMessage(messageUid, mailboxId, flags);
+        postgresMessageDAO.insert(message, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message).block();
+    }
 }
diff --git a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/DeletedWithRangeSearchOverrideTest.java b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/DeletedWithRangeSearchOverrideTest.java
index d7df1955bc..74d97339c1 100644
--- a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/DeletedWithRangeSearchOverrideTest.java
+++ b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/DeletedWithRangeSearchOverrideTest.java
@@ -21,48 +21,26 @@ package org.apache.james.mailbox.postgres.search;
 
 import static javax.mail.Flags.Flag.DELETED;
 import static javax.mail.Flags.Flag.SEEN;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.BLOB_ID;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.MAILBOX;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.MAILBOX_SESSION;
 import static org.assertj.core.api.Assertions.assertThat;
 
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.util.Date;
-
 import javax.mail.Flags;
 
 import org.apache.james.backends.postgres.PostgresExtension;
-import org.apache.james.core.Username;
-import org.apache.james.mailbox.MailboxSession;
-import org.apache.james.mailbox.MailboxSessionUtil;
 import org.apache.james.mailbox.MessageUid;
-import org.apache.james.mailbox.ModSeq;
-import org.apache.james.mailbox.model.ByteContent;
-import org.apache.james.mailbox.model.Mailbox;
-import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.SearchQuery;
-import org.apache.james.mailbox.model.ThreadId;
-import org.apache.james.mailbox.model.UidValidity;
 import org.apache.james.mailbox.postgres.PostgresMailboxAggregateModule;
-import org.apache.james.mailbox.postgres.PostgresMailboxId;
-import org.apache.james.mailbox.postgres.PostgresMessageId;
 import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
 import org.apache.james.mailbox.postgres.mail.dao.PostgresMessageDAO;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
-import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
-import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
 public class DeletedWithRangeSearchOverrideTest {
-    private static final MailboxSession MAILBOX_SESSION = MailboxSessionUtil.create(Username.of("benwa"));
-    private static final Mailbox MAILBOX = new Mailbox(MailboxPath.inbox(MAILBOX_SESSION), UidValidity.of(12), PostgresMailboxId.generate());
-    private static final String BLOB_ID = "abc";
-    private static final Charset MESSAGE_CHARSET = StandardCharsets.UTF_8;
-    private static final String MESSAGE_CONTENT = "Simple message content";
-    private static final byte[] MESSAGE_CONTENT_BYTES = MESSAGE_CONTENT.getBytes(MESSAGE_CHARSET);
-    private static final ByteContent CONTENT_STREAM = new ByteContent(MESSAGE_CONTENT_BYTES);
-    private final static long SIZE = MESSAGE_CONTENT_BYTES.length;
-
     @RegisterExtension
     static PostgresExtension postgresExtension = PostgresExtension.withoutRowLevelSecurity(PostgresMailboxAggregateModule.MODULE);
 
@@ -121,100 +99,31 @@ public class DeletedWithRangeSearchOverrideTest {
     @Test
     void searchShouldReturnMailboxEntries() {
         MessageUid messageUid = MessageUid.of(1);
-        PostgresMessageId messageId = new PostgresMessageId.Factory().generate();
-        MailboxMessage message1 = SimpleMailboxMessage.builder()
-            .messageId(messageId)
-            .threadId(ThreadId.fromBaseMessageId(messageId))
-            .uid(messageUid)
-            .content(CONTENT_STREAM)
-            .size(SIZE)
-            .internalDate(new Date())
-            .bodyStartOctet(18)
-            .flags(new Flags(DELETED))
-            .properties(new PropertyBuilder())
-            .mailboxId(MAILBOX.getMailboxId())
-            .modseq(ModSeq.of(1))
-            .build();
-        postgresMessageDAO.insert(message1, BLOB_ID).block();
-        postgresMailboxMessageDAO.insert(message1).block();
+        insert(messageUid, MAILBOX.getMailboxId(), new Flags(DELETED));
 
         MessageUid messageUid2 = MessageUid.of(2);
-        PostgresMessageId messageId2 = new PostgresMessageId.Factory().generate();
-        MailboxMessage message2 = SimpleMailboxMessage.builder()
-            .messageId(messageId2)
-            .threadId(ThreadId.fromBaseMessageId(messageId2))
-            .uid(messageUid2)
-            .content(CONTENT_STREAM)
-            .size(SIZE)
-            .internalDate(new Date())
-            .bodyStartOctet(18)
-            .flags(new Flags(DELETED))
-            .properties(new PropertyBuilder())
-            .mailboxId(MAILBOX.getMailboxId())
-            .modseq(ModSeq.of(1))
-            .build();
-        postgresMessageDAO.insert(message2, BLOB_ID).block();
-        postgresMailboxMessageDAO.insert(message2).block();
+        insert(messageUid2, MAILBOX.getMailboxId(), new Flags(DELETED));
 
         MessageUid messageUid3 = MessageUid.of(3);
-        PostgresMessageId messageId3 = new PostgresMessageId.Factory().generate();
-        MailboxMessage message3 = SimpleMailboxMessage.builder()
-            .messageId(messageId3)
-            .threadId(ThreadId.fromBaseMessageId(messageId3))
-            .uid(messageUid3)
-            .content(CONTENT_STREAM)
-            .size(SIZE)
-            .internalDate(new Date())
-            .bodyStartOctet(18)
-            .flags(new Flags(DELETED))
-            .properties(new PropertyBuilder())
-            .mailboxId(MAILBOX.getMailboxId())
-            .modseq(ModSeq.of(1))
-            .build();
-        postgresMessageDAO.insert(message3, BLOB_ID).block();
-        postgresMailboxMessageDAO.insert(message3).block();
+        insert(messageUid3, MAILBOX.getMailboxId(), new Flags());
 
         MessageUid messageUid4 = MessageUid.of(4);
-        PostgresMessageId messageId4 = new PostgresMessageId.Factory().generate();
-        MailboxMessage message4 = SimpleMailboxMessage.builder()
-            .messageId(messageId4)
-            .threadId(ThreadId.fromBaseMessageId(messageId4))
-            .uid(messageUid4)
-            .content(CONTENT_STREAM)
-            .size(SIZE)
-            .internalDate(new Date())
-            .bodyStartOctet(18)
-            .flags(new Flags(DELETED))
-            .properties(new PropertyBuilder())
-            .mailboxId(MAILBOX.getMailboxId())
-            .modseq(ModSeq.of(1))
-            .build();
-        postgresMessageDAO.insert(message4, BLOB_ID).block();
-        postgresMailboxMessageDAO.insert(message4).block();
+        insert(messageUid4, MAILBOX.getMailboxId(), new Flags(DELETED));
 
         MessageUid messageUid5 = MessageUid.of(5);
-        PostgresMessageId messageId5 = new PostgresMessageId.Factory().generate();
-        MailboxMessage message5 = SimpleMailboxMessage.builder()
-            .messageId(messageId5)
-            .threadId(ThreadId.fromBaseMessageId(messageId5))
-            .uid(messageUid5)
-            .content(CONTENT_STREAM)
-            .size(SIZE)
-            .internalDate(new Date())
-            .bodyStartOctet(18)
-            .flags(new Flags(DELETED))
-            .properties(new PropertyBuilder())
-            .mailboxId(MAILBOX.getMailboxId())
-            .modseq(ModSeq.of(1))
-            .build();
-        postgresMessageDAO.insert(message5, BLOB_ID).block();
-        postgresMailboxMessageDAO.insert(message5).block();
+        insert(messageUid5, MAILBOX.getMailboxId(), new Flags(DELETED));
 
         assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
             SearchQuery.builder()
                 .andCriteria(SearchQuery.flagIsSet(DELETED))
                 .andCriteria(SearchQuery.uid(new SearchQuery.UidRange(messageUid2, messageUid4)))
                 .build()).collectList().block())
-            .containsOnly(messageUid2, messageUid3, messageUid4);
+            .containsOnly(messageUid2, messageUid4);
+    }
+
+    private void insert(MessageUid messageUid, MailboxId mailboxId, Flags flags) {
+        MailboxMessage message = SearchOverrideFixture.createMessage(messageUid, mailboxId, flags);
+        postgresMessageDAO.insert(message, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message).block();
     }
 }
diff --git a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/NotDeletedWithRangeSearchOverrideTest.java b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/NotDeletedWithRangeSearchOverrideTest.java
index 7ee4d9f9cc..dcbdb7401c 100644
--- a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/NotDeletedWithRangeSearchOverrideTest.java
+++ b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/NotDeletedWithRangeSearchOverrideTest.java
@@ -20,49 +20,27 @@
 package org.apache.james.mailbox.postgres.search;
 
 import static javax.mail.Flags.Flag.DELETED;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.BLOB_ID;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.MAILBOX;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.MAILBOX_SESSION;
 import static org.assertj.core.api.Assertions.assertThat;
 
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.util.Date;
-
 import javax.mail.Flags;
 
 import org.apache.james.backends.postgres.PostgresExtension;
-import org.apache.james.core.Username;
-import org.apache.james.mailbox.MailboxSession;
-import org.apache.james.mailbox.MailboxSessionUtil;
 import org.apache.james.mailbox.MessageUid;
-import org.apache.james.mailbox.ModSeq;
-import org.apache.james.mailbox.model.ByteContent;
-import org.apache.james.mailbox.model.Mailbox;
 import org.apache.james.mailbox.model.MailboxId;
-import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.model.SearchQuery;
-import org.apache.james.mailbox.model.ThreadId;
-import org.apache.james.mailbox.model.UidValidity;
 import org.apache.james.mailbox.postgres.PostgresMailboxAggregateModule;
 import org.apache.james.mailbox.postgres.PostgresMailboxId;
-import org.apache.james.mailbox.postgres.PostgresMessageId;
 import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
 import org.apache.james.mailbox.postgres.mail.dao.PostgresMessageDAO;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
-import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
-import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
 public class NotDeletedWithRangeSearchOverrideTest {
-    private static final MailboxSession MAILBOX_SESSION = MailboxSessionUtil.create(Username.of("benwa"));
-    private static final Mailbox MAILBOX = new Mailbox(MailboxPath.inbox(MAILBOX_SESSION), UidValidity.of(12), PostgresMailboxId.generate());
-    private static final String BLOB_ID = "abc";
-    private static final Charset MESSAGE_CHARSET = StandardCharsets.UTF_8;
-    private static final String MESSAGE_CONTENT = "Simple message content";
-    private static final byte[] MESSAGE_CONTENT_BYTES = MESSAGE_CONTENT.getBytes(MESSAGE_CHARSET);
-    private static final ByteContent CONTENT_STREAM = new ByteContent(MESSAGE_CONTENT_BYTES);
-    private final static long SIZE = MESSAGE_CONTENT_BYTES.length;
-
     @RegisterExtension
     static PostgresExtension postgresExtension = PostgresExtension.withoutRowLevelSecurity(PostgresMailboxAggregateModule.MODULE);
 
@@ -123,14 +101,19 @@ public class NotDeletedWithRangeSearchOverrideTest {
     void searchShouldReturnMailboxEntries() {
         MessageUid messageUid = MessageUid.of(1);
         insert(messageUid, MAILBOX.getMailboxId(), new Flags());
+
         MessageUid messageUid2 = MessageUid.of(2);
         insert(messageUid2, MAILBOX.getMailboxId(), new Flags());
+
         MessageUid messageUid3 = MessageUid.of(3);
         insert(messageUid3, MAILBOX.getMailboxId(), new Flags(DELETED));
+
         MessageUid messageUid4 = MessageUid.of(4);
         insert(messageUid4, MAILBOX.getMailboxId(), new Flags());
+
         MessageUid messageUid5 = MessageUid.of(5);
         insert(messageUid5, MAILBOX.getMailboxId(), new Flags());
+
         MessageUid messageUid6 = MessageUid.of(6);
         insert(messageUid6, PostgresMailboxId.generate(), new Flags(DELETED));
 
@@ -143,20 +126,7 @@ public class NotDeletedWithRangeSearchOverrideTest {
     }
 
     private void insert(MessageUid messageUid, MailboxId mailboxId, Flags flags) {
-        PostgresMessageId messageId = new PostgresMessageId.Factory().generate();
-        MailboxMessage message = SimpleMailboxMessage.builder()
-            .messageId(messageId)
-            .threadId(ThreadId.fromBaseMessageId(messageId))
-            .uid(messageUid)
-            .content(CONTENT_STREAM)
-            .size(SIZE)
-            .internalDate(new Date())
-            .bodyStartOctet(18)
-            .flags(flags)
-            .properties(new PropertyBuilder())
-            .mailboxId(mailboxId)
-            .modseq(ModSeq.of(1))
-            .build();
+        MailboxMessage message = SearchOverrideFixture.createMessage(messageUid, mailboxId, flags);
         postgresMessageDAO.insert(message, BLOB_ID).block();
         postgresMailboxMessageDAO.insert(message).block();
     }
diff --git a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/SearchOverrideFixture.java b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/SearchOverrideFixture.java
new file mode 100644
index 0000000000..b64043d7b3
--- /dev/null
+++ b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/SearchOverrideFixture.java
@@ -0,0 +1,71 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.postgres.search;
+
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Date;
+
+import javax.mail.Flags;
+
+import org.apache.james.core.Username;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MailboxSessionUtil;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.ModSeq;
+import org.apache.james.mailbox.model.ByteContent;
+import org.apache.james.mailbox.model.Mailbox;
+import org.apache.james.mailbox.model.MailboxId;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.ThreadId;
+import org.apache.james.mailbox.model.UidValidity;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.PostgresMessageId;
+import org.apache.james.mailbox.store.mail.model.MailboxMessage;
+import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
+
+interface SearchOverrideFixture {
+    MailboxSession MAILBOX_SESSION = MailboxSessionUtil.create(Username.of("benwa"));
+    Mailbox MAILBOX = new Mailbox(MailboxPath.inbox(MAILBOX_SESSION), UidValidity.of(12), PostgresMailboxId.generate());
+    String BLOB_ID = "abc";
+    Charset MESSAGE_CHARSET = StandardCharsets.UTF_8;
+    String MESSAGE_CONTENT = "Simple message content";
+    byte[] MESSAGE_CONTENT_BYTES = MESSAGE_CONTENT.getBytes(MESSAGE_CHARSET);
+    ByteContent CONTENT_STREAM = new ByteContent(MESSAGE_CONTENT_BYTES);
+    long SIZE = MESSAGE_CONTENT_BYTES.length;
+
+    static MailboxMessage createMessage(MessageUid messageUid, MailboxId mailboxId, Flags flags) {
+        PostgresMessageId messageId = new PostgresMessageId.Factory().generate();
+        return SimpleMailboxMessage.builder()
+            .messageId(messageId)
+            .threadId(ThreadId.fromBaseMessageId(messageId))
+            .uid(messageUid)
+            .content(CONTENT_STREAM)
+            .size(SIZE)
+            .internalDate(new Date())
+            .bodyStartOctet(18)
+            .flags(flags)
+            .properties(new PropertyBuilder())
+            .mailboxId(mailboxId)
+            .modseq(ModSeq.of(1))
+            .build();
+    }
+}
diff --git a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/UidSearchOverrideTest.java b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/UidSearchOverrideTest.java
index aa7e4a0993..a42a3300d0 100644
--- a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/UidSearchOverrideTest.java
+++ b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/UidSearchOverrideTest.java
@@ -19,49 +19,27 @@
 
 package org.apache.james.mailbox.postgres.search;
 
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.BLOB_ID;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.MAILBOX;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.MAILBOX_SESSION;
 import static org.assertj.core.api.Assertions.assertThat;
 
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.util.Date;
-
 import javax.mail.Flags;
 
 import org.apache.james.backends.postgres.PostgresExtension;
-import org.apache.james.core.Username;
-import org.apache.james.mailbox.MailboxSession;
-import org.apache.james.mailbox.MailboxSessionUtil;
 import org.apache.james.mailbox.MessageUid;
-import org.apache.james.mailbox.ModSeq;
-import org.apache.james.mailbox.model.ByteContent;
-import org.apache.james.mailbox.model.Mailbox;
 import org.apache.james.mailbox.model.MailboxId;
-import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.model.SearchQuery;
-import org.apache.james.mailbox.model.ThreadId;
-import org.apache.james.mailbox.model.UidValidity;
 import org.apache.james.mailbox.postgres.PostgresMailboxAggregateModule;
 import org.apache.james.mailbox.postgres.PostgresMailboxId;
-import org.apache.james.mailbox.postgres.PostgresMessageId;
 import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
 import org.apache.james.mailbox.postgres.mail.dao.PostgresMessageDAO;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
-import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
-import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
 public class UidSearchOverrideTest {
-    private static final MailboxSession MAILBOX_SESSION = MailboxSessionUtil.create(Username.of("benwa"));
-    private static final Mailbox MAILBOX = new Mailbox(MailboxPath.inbox(MAILBOX_SESSION), UidValidity.of(12), PostgresMailboxId.generate());
-    private static final String BLOB_ID = "abc";
-    private static final Charset MESSAGE_CHARSET = StandardCharsets.UTF_8;
-    private static final String MESSAGE_CONTENT = "Simple message content";
-    private static final byte[] MESSAGE_CONTENT_BYTES = MESSAGE_CONTENT.getBytes(MESSAGE_CHARSET);
-    private static final ByteContent CONTENT_STREAM = new ByteContent(MESSAGE_CONTENT_BYTES);
-    private final static long SIZE = MESSAGE_CONTENT_BYTES.length;
-
     @RegisterExtension
     static PostgresExtension postgresExtension = PostgresExtension.withoutRowLevelSecurity(PostgresMailboxAggregateModule.MODULE);
 
@@ -109,14 +87,19 @@ public class UidSearchOverrideTest {
     void searchShouldReturnMailboxEntries() {
         MessageUid messageUid = MessageUid.of(1);
         insert(messageUid, MAILBOX.getMailboxId());
+
         MessageUid messageUid2 = MessageUid.of(2);
         insert(messageUid2, MAILBOX.getMailboxId());
+
         MessageUid messageUid3 = MessageUid.of(3);
         insert(messageUid3, MAILBOX.getMailboxId());
+
         MessageUid messageUid4 = MessageUid.of(4);
         insert(messageUid4, MAILBOX.getMailboxId());
+
         MessageUid messageUid5 = MessageUid.of(5);
         insert(messageUid5, MAILBOX.getMailboxId());
+
         MessageUid messageUid6 = MessageUid.of(6);
         insert(messageUid6, PostgresMailboxId.generate());
 
@@ -128,20 +111,7 @@ public class UidSearchOverrideTest {
     }
 
     private void insert(MessageUid messageUid, MailboxId mailboxId) {
-        PostgresMessageId messageId = new PostgresMessageId.Factory().generate();
-        MailboxMessage message = SimpleMailboxMessage.builder()
-            .messageId(messageId)
-            .threadId(ThreadId.fromBaseMessageId(messageId))
-            .uid(messageUid)
-            .content(CONTENT_STREAM)
-            .size(SIZE)
-            .internalDate(new Date())
-            .bodyStartOctet(18)
-            .flags(new Flags())
-            .properties(new PropertyBuilder())
-            .mailboxId(mailboxId)
-            .modseq(ModSeq.of(1))
-            .build();
+        MailboxMessage message = SearchOverrideFixture.createMessage(messageUid, mailboxId, new Flags());
         postgresMessageDAO.insert(message, BLOB_ID).block();
         postgresMailboxMessageDAO.insert(message).block();
     }
diff --git a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/UnseenSearchOverrideTest.java b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/UnseenSearchOverrideTest.java
index 51d8825096..84bd658a4e 100644
--- a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/UnseenSearchOverrideTest.java
+++ b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/UnseenSearchOverrideTest.java
@@ -20,49 +20,26 @@
 package org.apache.james.mailbox.postgres.search;
 
 import static javax.mail.Flags.Flag.SEEN;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.BLOB_ID;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.MAILBOX;
+import static org.apache.james.mailbox.postgres.search.SearchOverrideFixture.MAILBOX_SESSION;
 import static org.assertj.core.api.Assertions.assertThat;
 
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.util.Date;
-
 import javax.mail.Flags;
 
 import org.apache.james.backends.postgres.PostgresExtension;
-import org.apache.james.core.Username;
-import org.apache.james.mailbox.MailboxSession;
-import org.apache.james.mailbox.MailboxSessionUtil;
 import org.apache.james.mailbox.MessageUid;
-import org.apache.james.mailbox.ModSeq;
-import org.apache.james.mailbox.model.ByteContent;
-import org.apache.james.mailbox.model.Mailbox;
 import org.apache.james.mailbox.model.MailboxId;
-import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.model.SearchQuery;
-import org.apache.james.mailbox.model.ThreadId;
-import org.apache.james.mailbox.model.UidValidity;
 import org.apache.james.mailbox.postgres.PostgresMailboxAggregateModule;
-import org.apache.james.mailbox.postgres.PostgresMailboxId;
-import org.apache.james.mailbox.postgres.PostgresMessageId;
 import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
 import org.apache.james.mailbox.postgres.mail.dao.PostgresMessageDAO;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
-import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
-import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
 public class UnseenSearchOverrideTest {
-    private static final MailboxSession MAILBOX_SESSION = MailboxSessionUtil.create(Username.of("benwa"));
-    private static final Mailbox MAILBOX = new Mailbox(MailboxPath.inbox(MAILBOX_SESSION), UidValidity.of(12), PostgresMailboxId.generate());
-    private static final String BLOB_ID = "abc";
-    private static final Charset MESSAGE_CHARSET = StandardCharsets.UTF_8;
-    private static final String MESSAGE_CONTENT = "Simple message content";
-    private static final byte[] MESSAGE_CONTENT_BYTES = MESSAGE_CONTENT.getBytes(MESSAGE_CHARSET);
-    private static final ByteContent CONTENT_STREAM = new ByteContent(MESSAGE_CONTENT_BYTES);
-    private final static long SIZE = MESSAGE_CONTENT_BYTES.length;
-
     @RegisterExtension
     static PostgresExtension postgresExtension = PostgresExtension.withoutRowLevelSecurity(PostgresMailboxAggregateModule.MODULE);
 
@@ -180,10 +157,13 @@ public class UnseenSearchOverrideTest {
     void searchShouldSupportRanges() {
         MessageUid messageUid = MessageUid.of(1);
         insert(messageUid, MAILBOX.getMailboxId(), new Flags());
+
         MessageUid messageUid2 = MessageUid.of(2);
         insert(messageUid2, MAILBOX.getMailboxId(), new Flags());
+
         MessageUid messageUid3 = MessageUid.of(3);
         insert(messageUid3, MAILBOX.getMailboxId(), new Flags(SEEN));
+
         MessageUid messageUid4 = MessageUid.of(4);
         insert(messageUid4, MAILBOX.getMailboxId(), new Flags());
 
@@ -196,20 +176,7 @@ public class UnseenSearchOverrideTest {
     }
 
     private void insert(MessageUid messageUid, MailboxId mailboxId, Flags flags) {
-        PostgresMessageId messageId = new PostgresMessageId.Factory().generate();
-        MailboxMessage message = SimpleMailboxMessage.builder()
-            .messageId(messageId)
-            .threadId(ThreadId.fromBaseMessageId(messageId))
-            .uid(messageUid)
-            .content(CONTENT_STREAM)
-            .size(SIZE)
-            .internalDate(new Date())
-            .bodyStartOctet(18)
-            .flags(flags)
-            .properties(new PropertyBuilder())
-            .mailboxId(mailboxId)
-            .modseq(ModSeq.of(1))
-            .build();
+        MailboxMessage message = SearchOverrideFixture.createMessage(messageUid, mailboxId, flags);
         postgresMessageDAO.insert(message, BLOB_ID).block();
         postgresMailboxMessageDAO.insert(message).block();
     }


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


(james-project) 06/09: JAMES-2586 Implement UnseenSearchOverrideTest for Postgresql

Posted by rc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch postgresql
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit b367787dae294d519cd588250882e3219405dd9c
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Fri Dec 22 16:13:43 2023 +0700

    JAMES-2586 Implement UnseenSearchOverrideTest for Postgresql
---
 .../mail/dao/PostgresMailboxMessageDAO.java        |  40 ++++
 .../postgres/search/UnseenSearchOverride.java      |  93 +++++++++
 .../postgres/search/UnseenSearchOverrideTest.java  | 216 +++++++++++++++++++++
 3 files changed, 349 insertions(+)

diff --git a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java
index 1c8f032c41..61ff87a764 100644
--- a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java
+++ b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java
@@ -118,6 +118,46 @@ public class PostgresMailboxMessageDAO {
             .map(RECORD_TO_MESSAGE_UID_FUNCTION);
     }
 
+    public Flux<MessageUid> listUnseen(PostgresMailboxId mailboxId) {
+        return postgresExecutor.executeRows(dslContext -> Flux.from(selectMessageUidByMailboxIdAndExtraConditionQuery(mailboxId,
+                IS_SEEN.eq(false), Limit.unlimited(), dslContext)))
+            .map(RECORD_TO_MESSAGE_UID_FUNCTION);
+    }
+
+    public Flux<MessageUid> listUnseen(PostgresMailboxId mailboxId, MessageRange range) {
+        switch (range.getType()) {
+            case ALL:
+                return listUnseen(mailboxId);
+            case FROM:
+                return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_UID)
+                        .from(TABLE_NAME)
+                        .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                        .and(IS_SEEN.eq(false))
+                        .and(MESSAGE_UID.greaterOrEqual(range.getUidFrom().asLong()))
+                        .orderBy(DEFAULT_SORT_ORDER_BY)))
+                    .map(RECORD_TO_MESSAGE_UID_FUNCTION);
+            case RANGE:
+                return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_UID)
+                        .from(TABLE_NAME)
+                        .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                        .and(IS_SEEN.eq(false))
+                        .and(MESSAGE_UID.greaterOrEqual(range.getUidFrom().asLong()))
+                        .and(MESSAGE_UID.lessOrEqual(range.getUidTo().asLong()))
+                        .orderBy(DEFAULT_SORT_ORDER_BY)))
+                    .map(RECORD_TO_MESSAGE_UID_FUNCTION);
+            case ONE:
+                return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_UID)
+                        .from(TABLE_NAME)
+                        .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                        .and(IS_SEEN.eq(false))
+                        .and(MESSAGE_UID.eq(range.getUidFrom().asLong()))
+                        .orderBy(DEFAULT_SORT_ORDER_BY)))
+                    .map(RECORD_TO_MESSAGE_UID_FUNCTION);
+            default:
+                throw new RuntimeException("Unsupported range type " + range.getType());
+        }
+    }
+
     public Flux<MessageUid> findAllRecentMessageUid(PostgresMailboxId mailboxId) {
         return postgresExecutor.executeRows(dslContext -> Flux.from(selectMessageUidByMailboxIdAndExtraConditionQuery(mailboxId,
                 IS_RECENT.eq(true), Limit.unlimited(), dslContext)))
diff --git a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/UnseenSearchOverride.java b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/UnseenSearchOverride.java
new file mode 100644
index 0000000000..2bef17a9f9
--- /dev/null
+++ b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/UnseenSearchOverride.java
@@ -0,0 +1,93 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.postgres.search;
+
+import java.util.Optional;
+
+import javax.inject.Inject;
+import javax.mail.Flags;
+
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.model.Mailbox;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.mailbox.model.SearchQuery;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
+import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex;
+
+import com.google.common.collect.ImmutableList;
+
+import reactor.core.publisher.Flux;
+
+public class UnseenSearchOverride implements ListeningMessageSearchIndex.SearchOverride {
+    private final PostgresMailboxMessageDAO dao;
+
+    @Inject
+    public UnseenSearchOverride(PostgresMailboxMessageDAO dao) {
+        this.dao = dao;
+    }
+
+    @Override
+    public boolean applicable(SearchQuery searchQuery, MailboxSession session) {
+        return isUnseenWithAll(searchQuery)
+            || isNotSeenWithAll(searchQuery);
+    }
+
+    private boolean isUnseenWithAll(SearchQuery searchQuery) {
+        return searchQuery.getCriteria().contains(SearchQuery.flagIsUnSet(Flags.Flag.SEEN))
+            && allMessages(searchQuery)
+            && searchQuery.getSorts().equals(SearchQuery.DEFAULT_SORTS);
+    }
+
+    private boolean isNotSeenWithAll(SearchQuery searchQuery) {
+        return searchQuery.getCriteria().contains(SearchQuery.not(SearchQuery.flagIsSet(Flags.Flag.SEEN)))
+            && allMessages(searchQuery)
+            && searchQuery.getSorts().equals(SearchQuery.DEFAULT_SORTS);
+    }
+
+    private boolean allMessages(SearchQuery searchQuery) {
+        if (searchQuery.getCriteria().size() == 1) {
+            // Only the unseen critrion
+            return true;
+        }
+        if (searchQuery.getCriteria().size() == 2) {
+            return searchQuery.getCriteria().stream()
+                .anyMatch(criterion -> criterion instanceof SearchQuery.UidCriterion) ||
+                searchQuery.getCriteria().stream()
+                    .anyMatch(criterion -> criterion instanceof SearchQuery.AllCriterion);
+        }
+        return false;
+    }
+
+    @Override
+    public Flux<MessageUid> search(MailboxSession session, Mailbox mailbox, SearchQuery searchQuery) {
+        final Optional<SearchQuery.UidCriterion> maybeUidCriterion = searchQuery.getCriteria().stream()
+            .filter(criterion -> criterion instanceof SearchQuery.UidCriterion)
+            .map(SearchQuery.UidCriterion.class::cast)
+            .findFirst();
+
+        return maybeUidCriterion
+            .map(uidCriterion -> Flux.fromIterable(ImmutableList.copyOf(uidCriterion.getOperator().getRange()))
+                .concatMap(range -> dao.listUnseen((PostgresMailboxId) mailbox.getMailboxId(),
+                    MessageRange.range(range.getLowValue(), range.getHighValue()))))
+            .orElseGet(() -> dao.listUnseen((PostgresMailboxId) mailbox.getMailboxId()));
+    }
+}
diff --git a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/UnseenSearchOverrideTest.java b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/UnseenSearchOverrideTest.java
new file mode 100644
index 0000000000..51d8825096
--- /dev/null
+++ b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/UnseenSearchOverrideTest.java
@@ -0,0 +1,216 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.postgres.search;
+
+import static javax.mail.Flags.Flag.SEEN;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Date;
+
+import javax.mail.Flags;
+
+import org.apache.james.backends.postgres.PostgresExtension;
+import org.apache.james.core.Username;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MailboxSessionUtil;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.ModSeq;
+import org.apache.james.mailbox.model.ByteContent;
+import org.apache.james.mailbox.model.Mailbox;
+import org.apache.james.mailbox.model.MailboxId;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.SearchQuery;
+import org.apache.james.mailbox.model.ThreadId;
+import org.apache.james.mailbox.model.UidValidity;
+import org.apache.james.mailbox.postgres.PostgresMailboxAggregateModule;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.PostgresMessageId;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMessageDAO;
+import org.apache.james.mailbox.store.mail.model.MailboxMessage;
+import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+public class UnseenSearchOverrideTest {
+    private static final MailboxSession MAILBOX_SESSION = MailboxSessionUtil.create(Username.of("benwa"));
+    private static final Mailbox MAILBOX = new Mailbox(MailboxPath.inbox(MAILBOX_SESSION), UidValidity.of(12), PostgresMailboxId.generate());
+    private static final String BLOB_ID = "abc";
+    private static final Charset MESSAGE_CHARSET = StandardCharsets.UTF_8;
+    private static final String MESSAGE_CONTENT = "Simple message content";
+    private static final byte[] MESSAGE_CONTENT_BYTES = MESSAGE_CONTENT.getBytes(MESSAGE_CHARSET);
+    private static final ByteContent CONTENT_STREAM = new ByteContent(MESSAGE_CONTENT_BYTES);
+    private final static long SIZE = MESSAGE_CONTENT_BYTES.length;
+
+    @RegisterExtension
+    static PostgresExtension postgresExtension = PostgresExtension.withoutRowLevelSecurity(PostgresMailboxAggregateModule.MODULE);
+
+    private PostgresMailboxMessageDAO postgresMailboxMessageDAO;
+    private PostgresMessageDAO postgresMessageDAO;
+    private UnseenSearchOverride testee;
+
+    @BeforeEach
+    void setUp() {
+        postgresMessageDAO = new PostgresMessageDAO(postgresExtension.getPostgresExecutor());
+        postgresMailboxMessageDAO = new PostgresMailboxMessageDAO(postgresExtension.getPostgresExecutor());
+        testee = new UnseenSearchOverride(postgresMailboxMessageDAO);
+    }
+
+    @Test
+    void unseenQueryShouldBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.flagIsUnSet(SEEN))
+                .build(),
+            MAILBOX_SESSION))
+            .isTrue();
+    }
+
+    @Test
+    void notSeenQueryShouldBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.not(SearchQuery.flagIsSet(SEEN)))
+                .build(),
+            MAILBOX_SESSION))
+            .isTrue();
+    }
+
+    @Test
+    void unseenAndAllQueryShouldBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.flagIsUnSet(SEEN))
+                .andCriteria(SearchQuery.all())
+                .build(),
+            MAILBOX_SESSION))
+            .isTrue();
+    }
+
+    @Test
+    void notSeenAndAllQueryShouldBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.not(SearchQuery.flagIsSet(SEEN)))
+                .andCriteria(SearchQuery.all())
+                .build(),
+            MAILBOX_SESSION))
+            .isTrue();
+    }
+
+    @Test
+    void unseenAndFromOneQueryShouldBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.flagIsUnSet(SEEN))
+                .andCriteria(SearchQuery.uid(new SearchQuery.UidRange(MessageUid.MIN_VALUE, MessageUid.MAX_VALUE)))
+                .build(),
+            MAILBOX_SESSION))
+            .isTrue();
+    }
+
+    @Test
+    void notSeenFromOneQueryShouldBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.not(SearchQuery.flagIsSet(SEEN)))
+                .andCriteria(SearchQuery.uid(new SearchQuery.UidRange(MessageUid.MIN_VALUE, MessageUid.MAX_VALUE)))
+                .build(),
+            MAILBOX_SESSION))
+            .isTrue();
+    }
+
+    @Test
+    void sizeQueryShouldNotBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.sizeEquals(12))
+                .build(),
+            MAILBOX_SESSION))
+            .isFalse();
+    }
+
+    @Test
+    void searchShouldReturnEmptyByDefault() {
+        assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.flagIsUnSet(SEEN))
+                .build()).collectList().block())
+            .isEmpty();
+    }
+
+    @Test
+    void searchShouldReturnMailboxEntries() {
+        MessageUid messageUid = MessageUid.of(1);
+        insert(messageUid, MAILBOX.getMailboxId(), new Flags());
+        MessageUid messageUid2 = MessageUid.of(2);
+        insert(messageUid2, MAILBOX.getMailboxId(), new Flags());
+        MessageUid messageUid3 = MessageUid.of(3);
+        insert(messageUid3, MAILBOX.getMailboxId(), new Flags(SEEN));
+
+        assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.flagIsUnSet(SEEN))
+                .build()).collectList().block())
+            .containsOnly(messageUid, messageUid2);
+    }
+
+    @Test
+    void searchShouldSupportRanges() {
+        MessageUid messageUid = MessageUid.of(1);
+        insert(messageUid, MAILBOX.getMailboxId(), new Flags());
+        MessageUid messageUid2 = MessageUid.of(2);
+        insert(messageUid2, MAILBOX.getMailboxId(), new Flags());
+        MessageUid messageUid3 = MessageUid.of(3);
+        insert(messageUid3, MAILBOX.getMailboxId(), new Flags(SEEN));
+        MessageUid messageUid4 = MessageUid.of(4);
+        insert(messageUid4, MAILBOX.getMailboxId(), new Flags());
+
+        assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.flagIsUnSet(SEEN))
+                .andCriterion(SearchQuery.uid(new SearchQuery.UidRange(MessageUid.of(2), MessageUid.of(4))))
+                .build()).collectList().block())
+            .containsOnly(messageUid2, messageUid4);
+    }
+
+    private void insert(MessageUid messageUid, MailboxId mailboxId, Flags flags) {
+        PostgresMessageId messageId = new PostgresMessageId.Factory().generate();
+        MailboxMessage message = SimpleMailboxMessage.builder()
+            .messageId(messageId)
+            .threadId(ThreadId.fromBaseMessageId(messageId))
+            .uid(messageUid)
+            .content(CONTENT_STREAM)
+            .size(SIZE)
+            .internalDate(new Date())
+            .bodyStartOctet(18)
+            .flags(flags)
+            .properties(new PropertyBuilder())
+            .mailboxId(mailboxId)
+            .modseq(ModSeq.of(1))
+            .build();
+        postgresMessageDAO.insert(message, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message).block();
+    }
+}


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


(james-project) 05/09: JAMES-2586 Implement UidSearchOverride for Postgresql

Posted by rc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch postgresql
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit bd650deb7ea71da27e675f0143b727ce6e2efed2
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Fri Dec 22 15:17:28 2023 +0700

    JAMES-2586 Implement UidSearchOverride for Postgresql
---
 .../mail/dao/PostgresMailboxMessageDAO.java        |  17 +++
 .../mailbox/postgres/search/UidSearchOverride.java |  66 +++++++++
 .../postgres/search/UidSearchOverrideTest.java     | 148 +++++++++++++++++++++
 3 files changed, 231 insertions(+)

diff --git a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java
index 049cd40f7b..1c8f032c41 100644
--- a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java
+++ b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java
@@ -130,6 +130,23 @@ public class PostgresMailboxMessageDAO {
             .map(RECORD_TO_MESSAGE_UID_FUNCTION);
     }
 
+    public Flux<MessageUid> listUids(PostgresMailboxId mailboxId, MessageRange range) {
+        if (range.getType() == MessageRange.Type.ALL) {
+            return listAllMessageUid(mailboxId);
+        }
+        return doListUids(mailboxId, range);
+    }
+
+    private Flux<MessageUid> doListUids(PostgresMailboxId mailboxId, MessageRange range) {
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_UID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .and(MESSAGE_UID.greaterOrEqual(range.getUidFrom().asLong()))
+                .and(MESSAGE_UID.lessOrEqual(range.getUidTo().asLong()))
+                .orderBy(DEFAULT_SORT_ORDER_BY)))
+            .map(RECORD_TO_MESSAGE_UID_FUNCTION);
+    }
+
     public Mono<MessageMetaData> deleteByMailboxIdAndMessageUid(PostgresMailboxId mailboxId, MessageUid messageUid) {
         return postgresExecutor.executeRow(dslContext -> Mono.from(dslContext.deleteFrom(TABLE_NAME)
                 .where(MAILBOX_ID.eq(mailboxId.asUuid()))
diff --git a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/UidSearchOverride.java b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/UidSearchOverride.java
new file mode 100644
index 0000000000..9827bc65f8
--- /dev/null
+++ b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/UidSearchOverride.java
@@ -0,0 +1,66 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.postgres.search;
+
+import javax.inject.Inject;
+
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.model.Mailbox;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.mailbox.model.SearchQuery;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
+import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex;
+
+import com.google.common.collect.ImmutableList;
+
+import reactor.core.publisher.Flux;
+
+public class UidSearchOverride implements ListeningMessageSearchIndex.SearchOverride {
+    private final PostgresMailboxMessageDAO dao;
+
+    @Inject
+    public UidSearchOverride(PostgresMailboxMessageDAO dao) {
+        this.dao = dao;
+    }
+
+    @Override
+    public boolean applicable(SearchQuery searchQuery, MailboxSession session) {
+        return searchQuery.getCriteria().size() == 1
+            && searchQuery.getCriteria().get(0) instanceof SearchQuery.UidCriterion
+            && searchQuery.getSorts().equals(SearchQuery.DEFAULT_SORTS);
+    }
+
+    @Override
+    public Flux<MessageUid> search(MailboxSession session, Mailbox mailbox, SearchQuery searchQuery) {
+        SearchQuery.UidCriterion uidArgument = searchQuery.getCriteria().stream()
+            .filter(criterion -> criterion instanceof SearchQuery.UidCriterion)
+            .map(SearchQuery.UidCriterion.class::cast)
+            .findAny()
+            .orElseThrow(() -> new RuntimeException("Missing Uid argument"));
+
+        SearchQuery.UidRange[] uidRanges = uidArgument.getOperator().getRange();
+
+        return Flux.fromIterable(ImmutableList.copyOf(uidRanges))
+            .concatMap(range -> dao.listUids((PostgresMailboxId) mailbox.getMailboxId(),
+                MessageRange.range(range.getLowValue(), range.getHighValue())));
+    }
+}
diff --git a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/UidSearchOverrideTest.java b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/UidSearchOverrideTest.java
new file mode 100644
index 0000000000..aa7e4a0993
--- /dev/null
+++ b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/UidSearchOverrideTest.java
@@ -0,0 +1,148 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.postgres.search;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Date;
+
+import javax.mail.Flags;
+
+import org.apache.james.backends.postgres.PostgresExtension;
+import org.apache.james.core.Username;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MailboxSessionUtil;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.ModSeq;
+import org.apache.james.mailbox.model.ByteContent;
+import org.apache.james.mailbox.model.Mailbox;
+import org.apache.james.mailbox.model.MailboxId;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.SearchQuery;
+import org.apache.james.mailbox.model.ThreadId;
+import org.apache.james.mailbox.model.UidValidity;
+import org.apache.james.mailbox.postgres.PostgresMailboxAggregateModule;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.PostgresMessageId;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMessageDAO;
+import org.apache.james.mailbox.store.mail.model.MailboxMessage;
+import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+public class UidSearchOverrideTest {
+    private static final MailboxSession MAILBOX_SESSION = MailboxSessionUtil.create(Username.of("benwa"));
+    private static final Mailbox MAILBOX = new Mailbox(MailboxPath.inbox(MAILBOX_SESSION), UidValidity.of(12), PostgresMailboxId.generate());
+    private static final String BLOB_ID = "abc";
+    private static final Charset MESSAGE_CHARSET = StandardCharsets.UTF_8;
+    private static final String MESSAGE_CONTENT = "Simple message content";
+    private static final byte[] MESSAGE_CONTENT_BYTES = MESSAGE_CONTENT.getBytes(MESSAGE_CHARSET);
+    private static final ByteContent CONTENT_STREAM = new ByteContent(MESSAGE_CONTENT_BYTES);
+    private final static long SIZE = MESSAGE_CONTENT_BYTES.length;
+
+    @RegisterExtension
+    static PostgresExtension postgresExtension = PostgresExtension.withoutRowLevelSecurity(PostgresMailboxAggregateModule.MODULE);
+
+    private PostgresMailboxMessageDAO postgresMailboxMessageDAO;
+    private PostgresMessageDAO postgresMessageDAO;
+    private UidSearchOverride testee;
+
+    @BeforeEach
+    void setUp() {
+        postgresMessageDAO = new PostgresMessageDAO(postgresExtension.getPostgresExecutor());
+        postgresMailboxMessageDAO = new PostgresMailboxMessageDAO(postgresExtension.getPostgresExecutor());
+        testee = new UidSearchOverride(postgresMailboxMessageDAO);
+    }
+
+    @Test
+    void rangeQueryShouldBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.uid(new SearchQuery.UidRange(MessageUid.of(4), MessageUid.of(45))))
+                .build(),
+            MAILBOX_SESSION))
+            .isTrue();
+    }
+
+    @Test
+    void sizeQueryShouldNotBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.sizeEquals(12))
+                .build(),
+            MAILBOX_SESSION))
+            .isFalse();
+    }
+
+    @Test
+    void searchShouldReturnEmptyByDefault() {
+        assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.uid(new SearchQuery.UidRange(MessageUid.of(34), MessageUid.of(345))))
+                .build()).collectList().block())
+            .isEmpty();
+    }
+
+    @Test
+    void searchShouldReturnMailboxEntries() {
+        MessageUid messageUid = MessageUid.of(1);
+        insert(messageUid, MAILBOX.getMailboxId());
+        MessageUid messageUid2 = MessageUid.of(2);
+        insert(messageUid2, MAILBOX.getMailboxId());
+        MessageUid messageUid3 = MessageUid.of(3);
+        insert(messageUid3, MAILBOX.getMailboxId());
+        MessageUid messageUid4 = MessageUid.of(4);
+        insert(messageUid4, MAILBOX.getMailboxId());
+        MessageUid messageUid5 = MessageUid.of(5);
+        insert(messageUid5, MAILBOX.getMailboxId());
+        MessageUid messageUid6 = MessageUid.of(6);
+        insert(messageUid6, PostgresMailboxId.generate());
+
+        assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.uid(new SearchQuery.UidRange(messageUid2, messageUid4)))
+                .build()).collectList().block())
+            .containsOnly(messageUid2, messageUid3, messageUid4);
+    }
+
+    private void insert(MessageUid messageUid, MailboxId mailboxId) {
+        PostgresMessageId messageId = new PostgresMessageId.Factory().generate();
+        MailboxMessage message = SimpleMailboxMessage.builder()
+            .messageId(messageId)
+            .threadId(ThreadId.fromBaseMessageId(messageId))
+            .uid(messageUid)
+            .content(CONTENT_STREAM)
+            .size(SIZE)
+            .internalDate(new Date())
+            .bodyStartOctet(18)
+            .flags(new Flags())
+            .properties(new PropertyBuilder())
+            .mailboxId(mailboxId)
+            .modseq(ModSeq.of(1))
+            .build();
+        postgresMessageDAO.insert(message, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message).block();
+    }
+}


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


(james-project) 07/09: JAMES-2586 Correct search overrides documentation in opensearch.properties

Posted by rc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch postgresql
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 41f476362d345b99dec24c79d5e2963cf2f2deb0
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Fri Dec 22 16:14:50 2023 +0700

    JAMES-2586 Correct search overrides documentation in opensearch.properties
---
 .../opensearch.properties                              | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/server/apps/postgres-app/sample-configuration-distributed/opensearch.properties b/server/apps/postgres-app/sample-configuration-distributed/opensearch.properties
index 7b48defef8..df261c5dee 100644
--- a/server/apps/postgres-app/sample-configuration-distributed/opensearch.properties
+++ b/server/apps/postgres-app/sample-configuration-distributed/opensearch.properties
@@ -80,23 +80,21 @@ opensearch.indexAttachments=false
 # are simple enough to be resolved against Cassandra.
 #
 # Possible values are:
-#  - `org.apache.james.mailbox.cassandra.search.AllSearchOverride` Some IMAP clients uses SEARCH ALL to fully list messages in
+#  - `org.apache.james.mailbox.postgres.search.AllSearchOverride` Some IMAP clients uses SEARCH ALL to fully list messages in
 # a mailbox and detect deletions. This is typically done by clients not supporting QRESYNC and from an IMAP perspective
 # is considered an optimisation as less data is transmitted compared to a FETCH command. Resolving such requests against
-# Cassandra is enabled by this search override and likely desirable.
-#  - `org.apache.james.mailbox.cassandra.search.UidSearchOverride`. Same as above but restricted by ranges.
-#  - `org.apache.james.mailbox.cassandra.search.DeletedSearchOverride`. Find deleted messages by looking up in the relevant Cassandra
+# Postgresql is enabled by this search override and likely desirable.
+#  - `org.apache.james.mailbox.postgres.search.UidSearchOverride`. Same as above but restricted by ranges.
+#  - `org.apache.james.mailbox.postgres.search.DeletedSearchOverride`. Find deleted messages by looking up in the relevant Postgresql
 # table.
-#  - `org.apache.james.mailbox.cassandra.search.DeletedWithRangeSearchOverride`. Same as above but limited by ranges.
-#  - `org.apache.james.mailbox.cassandra.search.NotDeletedWithRangeSearchOverride`. List non deleted messages in a given range.
+#  - `org.apache.james.mailbox.postgres.search.DeletedWithRangeSearchOverride`. Same as above but limited by ranges.
+#  - `org.apache.james.mailbox.postgres.search.NotDeletedWithRangeSearchOverride`. List non deleted messages in a given range.
 # Lists all messages and filters out deleted message thus this is based on the following heuristic: most messages are not marked as deleted.
-#  - `org.apache.james.mailbox.cassandra.search.UnseenSearchOverride`. List unseen messages in the corresponding cassandra projection.
+#  - `org.apache.james.mailbox.postgres.search.UnseenSearchOverride`. List unseen messages in the corresponding Postgresql index.
 #
 # Please note that custom overrides can be defined here.
 #
-# Note: Search overrides not implemented yet for postgresql.
-#
-# opensearch.search.overrides=org.apache.james.mailbox.cassandra.search.AllSearchOverride,org.apache.james.mailbox.cassandra.search.DeletedSearchOverride, org.apache.james.mailbox.cassandra.search.DeletedWithRangeSearchOverride,org.apache.james.mailbox.cassandra.search.NotDeletedWithRangeSearchOverride,org.apache.james.mailbox.cassandra.search.UidSearchOverride,org.apache.james.mailbox.cassandra.search.UnseenSearchOverride
+# opensearch.search.overrides=org.apache.james.mailbox.postgres.search.AllSearchOverride,org.apache.james.mailbox.postgres.search.DeletedSearchOverride, org.apache.james.mailbox.postgres.search.DeletedWithRangeSearchOverride,org.apache.james.mailbox.postgres.search.NotDeletedWithRangeSearchOverride,org.apache.james.mailbox.postgres.search.UidSearchOverride,org.apache.james.mailbox.postgres.search.UnseenSearchOverride
 
 # Optional. Default is `false`
 # When set to true, James will attempt to reindex from the indexed message when moved. If the message is not found, it will fall back to the old behavior (The message will be indexed from the blobStore source)


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


(james-project) 03/09: JAMES-2586 Implement DeletedWithRangeSearchOverride for Postgresql

Posted by rc...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch postgresql
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 6aee4e9297e0e47c2fdb580f11c96882f6984c11
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Fri Dec 22 14:12:28 2023 +0700

    JAMES-2586 Implement DeletedWithRangeSearchOverride for Postgresql
---
 .../search/DeletedWithRangeSearchOverride.java     |  68 +++++++
 .../search/DeletedWithRangeSearchOverrideTest.java | 220 +++++++++++++++++++++
 2 files changed, 288 insertions(+)

diff --git a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/DeletedWithRangeSearchOverride.java b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/DeletedWithRangeSearchOverride.java
new file mode 100644
index 0000000000..c463d56104
--- /dev/null
+++ b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/search/DeletedWithRangeSearchOverride.java
@@ -0,0 +1,68 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.postgres.search;
+
+import javax.inject.Inject;
+import javax.mail.Flags;
+
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.model.Mailbox;
+import org.apache.james.mailbox.model.SearchQuery;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
+import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex;
+
+import com.google.common.collect.ImmutableList;
+
+import reactor.core.publisher.Flux;
+
+public class DeletedWithRangeSearchOverride implements ListeningMessageSearchIndex.SearchOverride {
+    private final PostgresMailboxMessageDAO dao;
+
+    @Inject
+    public DeletedWithRangeSearchOverride(PostgresMailboxMessageDAO dao) {
+        this.dao = dao;
+    }
+
+    @Override
+    public boolean applicable(SearchQuery searchQuery, MailboxSession session) {
+        return searchQuery.getCriteria().size() == 2
+            && searchQuery.getCriteria().contains(SearchQuery.flagIsSet(Flags.Flag.DELETED))
+            && searchQuery.getCriteria().stream()
+            .anyMatch(criterion -> criterion instanceof SearchQuery.UidCriterion)
+            && searchQuery.getSorts().equals(SearchQuery.DEFAULT_SORTS);
+    }
+
+    @Override
+    public Flux<MessageUid> search(MailboxSession session, Mailbox mailbox, SearchQuery searchQuery) {
+        SearchQuery.UidCriterion uidArgument = searchQuery.getCriteria().stream()
+            .filter(criterion -> criterion instanceof SearchQuery.UidCriterion)
+            .map(SearchQuery.UidCriterion.class::cast)
+            .findAny()
+            .orElseThrow(() -> new RuntimeException("Missing Uid argument"));
+
+        SearchQuery.UidRange[] uidRanges = uidArgument.getOperator().getRange();
+
+        return Flux.fromIterable(ImmutableList.copyOf(uidRanges))
+            .concatMap(range -> dao.findDeletedMessagesByMailboxIdAndBetweenUIDs((PostgresMailboxId) mailbox.getMailboxId(),
+                range.getLowValue(), range.getHighValue()));
+    }
+}
diff --git a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/DeletedWithRangeSearchOverrideTest.java b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/DeletedWithRangeSearchOverrideTest.java
new file mode 100644
index 0000000000..d7df1955bc
--- /dev/null
+++ b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/search/DeletedWithRangeSearchOverrideTest.java
@@ -0,0 +1,220 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.postgres.search;
+
+import static javax.mail.Flags.Flag.DELETED;
+import static javax.mail.Flags.Flag.SEEN;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Date;
+
+import javax.mail.Flags;
+
+import org.apache.james.backends.postgres.PostgresExtension;
+import org.apache.james.core.Username;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MailboxSessionUtil;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.ModSeq;
+import org.apache.james.mailbox.model.ByteContent;
+import org.apache.james.mailbox.model.Mailbox;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.SearchQuery;
+import org.apache.james.mailbox.model.ThreadId;
+import org.apache.james.mailbox.model.UidValidity;
+import org.apache.james.mailbox.postgres.PostgresMailboxAggregateModule;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.PostgresMessageId;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxMessageDAO;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresMessageDAO;
+import org.apache.james.mailbox.store.mail.model.MailboxMessage;
+import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
+import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+public class DeletedWithRangeSearchOverrideTest {
+    private static final MailboxSession MAILBOX_SESSION = MailboxSessionUtil.create(Username.of("benwa"));
+    private static final Mailbox MAILBOX = new Mailbox(MailboxPath.inbox(MAILBOX_SESSION), UidValidity.of(12), PostgresMailboxId.generate());
+    private static final String BLOB_ID = "abc";
+    private static final Charset MESSAGE_CHARSET = StandardCharsets.UTF_8;
+    private static final String MESSAGE_CONTENT = "Simple message content";
+    private static final byte[] MESSAGE_CONTENT_BYTES = MESSAGE_CONTENT.getBytes(MESSAGE_CHARSET);
+    private static final ByteContent CONTENT_STREAM = new ByteContent(MESSAGE_CONTENT_BYTES);
+    private final static long SIZE = MESSAGE_CONTENT_BYTES.length;
+
+    @RegisterExtension
+    static PostgresExtension postgresExtension = PostgresExtension.withoutRowLevelSecurity(PostgresMailboxAggregateModule.MODULE);
+
+    private PostgresMailboxMessageDAO postgresMailboxMessageDAO;
+    private PostgresMessageDAO postgresMessageDAO;
+    private DeletedWithRangeSearchOverride testee;
+
+    @BeforeEach
+    void setUp() {
+        postgresMessageDAO = new PostgresMessageDAO(postgresExtension.getPostgresExecutor());
+        postgresMailboxMessageDAO = new PostgresMailboxMessageDAO(postgresExtension.getPostgresExecutor());
+        testee = new DeletedWithRangeSearchOverride(postgresMailboxMessageDAO);
+    }
+
+    @Test
+    void deletedWithRangeQueryShouldBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.flagIsSet(DELETED))
+                .andCriteria(SearchQuery.uid(new SearchQuery.UidRange(MessageUid.of(4), MessageUid.of(45))))
+                .build(),
+            MAILBOX_SESSION))
+            .isTrue();
+    }
+
+    @Test
+    void deletedQueryShouldNotBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.flagIsSet(DELETED))
+                .build(),
+            MAILBOX_SESSION))
+            .isFalse();
+    }
+
+    @Test
+    void sizeQueryShouldNotBeApplicable() {
+        assertThat(testee.applicable(
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.sizeEquals(12))
+                .build(),
+            MAILBOX_SESSION))
+            .isFalse();
+    }
+
+    @Test
+    void searchShouldReturnEmptyByDefault() {
+        assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.flagIsUnSet(SEEN))
+                .andCriteria(SearchQuery.uid(new SearchQuery.UidRange(MessageUid.MIN_VALUE, MessageUid.of(45))))
+                .build()).collectList().block())
+            .isEmpty();
+    }
+
+    @Test
+    void searchShouldReturnMailboxEntries() {
+        MessageUid messageUid = MessageUid.of(1);
+        PostgresMessageId messageId = new PostgresMessageId.Factory().generate();
+        MailboxMessage message1 = SimpleMailboxMessage.builder()
+            .messageId(messageId)
+            .threadId(ThreadId.fromBaseMessageId(messageId))
+            .uid(messageUid)
+            .content(CONTENT_STREAM)
+            .size(SIZE)
+            .internalDate(new Date())
+            .bodyStartOctet(18)
+            .flags(new Flags(DELETED))
+            .properties(new PropertyBuilder())
+            .mailboxId(MAILBOX.getMailboxId())
+            .modseq(ModSeq.of(1))
+            .build();
+        postgresMessageDAO.insert(message1, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message1).block();
+
+        MessageUid messageUid2 = MessageUid.of(2);
+        PostgresMessageId messageId2 = new PostgresMessageId.Factory().generate();
+        MailboxMessage message2 = SimpleMailboxMessage.builder()
+            .messageId(messageId2)
+            .threadId(ThreadId.fromBaseMessageId(messageId2))
+            .uid(messageUid2)
+            .content(CONTENT_STREAM)
+            .size(SIZE)
+            .internalDate(new Date())
+            .bodyStartOctet(18)
+            .flags(new Flags(DELETED))
+            .properties(new PropertyBuilder())
+            .mailboxId(MAILBOX.getMailboxId())
+            .modseq(ModSeq.of(1))
+            .build();
+        postgresMessageDAO.insert(message2, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message2).block();
+
+        MessageUid messageUid3 = MessageUid.of(3);
+        PostgresMessageId messageId3 = new PostgresMessageId.Factory().generate();
+        MailboxMessage message3 = SimpleMailboxMessage.builder()
+            .messageId(messageId3)
+            .threadId(ThreadId.fromBaseMessageId(messageId3))
+            .uid(messageUid3)
+            .content(CONTENT_STREAM)
+            .size(SIZE)
+            .internalDate(new Date())
+            .bodyStartOctet(18)
+            .flags(new Flags(DELETED))
+            .properties(new PropertyBuilder())
+            .mailboxId(MAILBOX.getMailboxId())
+            .modseq(ModSeq.of(1))
+            .build();
+        postgresMessageDAO.insert(message3, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message3).block();
+
+        MessageUid messageUid4 = MessageUid.of(4);
+        PostgresMessageId messageId4 = new PostgresMessageId.Factory().generate();
+        MailboxMessage message4 = SimpleMailboxMessage.builder()
+            .messageId(messageId4)
+            .threadId(ThreadId.fromBaseMessageId(messageId4))
+            .uid(messageUid4)
+            .content(CONTENT_STREAM)
+            .size(SIZE)
+            .internalDate(new Date())
+            .bodyStartOctet(18)
+            .flags(new Flags(DELETED))
+            .properties(new PropertyBuilder())
+            .mailboxId(MAILBOX.getMailboxId())
+            .modseq(ModSeq.of(1))
+            .build();
+        postgresMessageDAO.insert(message4, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message4).block();
+
+        MessageUid messageUid5 = MessageUid.of(5);
+        PostgresMessageId messageId5 = new PostgresMessageId.Factory().generate();
+        MailboxMessage message5 = SimpleMailboxMessage.builder()
+            .messageId(messageId5)
+            .threadId(ThreadId.fromBaseMessageId(messageId5))
+            .uid(messageUid5)
+            .content(CONTENT_STREAM)
+            .size(SIZE)
+            .internalDate(new Date())
+            .bodyStartOctet(18)
+            .flags(new Flags(DELETED))
+            .properties(new PropertyBuilder())
+            .mailboxId(MAILBOX.getMailboxId())
+            .modseq(ModSeq.of(1))
+            .build();
+        postgresMessageDAO.insert(message5, BLOB_ID).block();
+        postgresMailboxMessageDAO.insert(message5).block();
+
+        assertThat(testee.search(MAILBOX_SESSION, MAILBOX,
+            SearchQuery.builder()
+                .andCriteria(SearchQuery.flagIsSet(DELETED))
+                .andCriteria(SearchQuery.uid(new SearchQuery.UidRange(messageUid2, messageUid4)))
+                .build()).collectList().block())
+            .containsOnly(messageUid2, messageUid3, messageUid4);
+    }
+}


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