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 2024/01/22 03:38:16 UTC

(james-project) 02/03: JAMES-2586 Implement Postgres Attachment Blob reference source

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 0c072806ae22af4c5252acfe39c3ad73e1cf51b3
Author: vttran <vt...@linagora.com>
AuthorDate: Thu Jan 18 11:06:35 2024 +0700

    JAMES-2586 Implement Postgres Attachment Blob reference source
---
 .../PostgresAttachmentBlobReferenceSource.java     |  53 ++++++++++
 .../PostgresAttachmentBlobReferenceSourceTest.java | 111 +++++++++++++++++++++
 .../modules/mailbox/PostgresMailboxModule.java     |   4 +-
 .../james/modules/data/PostgresCommonModule.java   |   2 +-
 4 files changed, 168 insertions(+), 2 deletions(-)

diff --git a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/PostgresAttachmentBlobReferenceSource.java b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/PostgresAttachmentBlobReferenceSource.java
new file mode 100644
index 0000000000..79bc754707
--- /dev/null
+++ b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/PostgresAttachmentBlobReferenceSource.java
@@ -0,0 +1,53 @@
+/****************************************************************
+ * 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.mail;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import org.apache.james.backends.postgres.utils.PostgresExecutor;
+import org.apache.james.blob.api.BlobId;
+import org.apache.james.blob.api.BlobReferenceSource;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresAttachmentDAO;
+
+import reactor.core.publisher.Flux;
+
+public class PostgresAttachmentBlobReferenceSource implements BlobReferenceSource {
+
+    private final PostgresAttachmentDAO postgresAttachmentDAO;
+
+    @Inject
+    @Singleton
+    public PostgresAttachmentBlobReferenceSource(@Named(PostgresExecutor.NON_RLS_INJECT) PostgresExecutor postgresExecutor,
+                                                 BlobId.Factory bloIdFactory) {
+        this(new PostgresAttachmentDAO(postgresExecutor, bloIdFactory));
+    }
+
+    public PostgresAttachmentBlobReferenceSource(PostgresAttachmentDAO postgresAttachmentDAO) {
+        this.postgresAttachmentDAO = postgresAttachmentDAO;
+    }
+
+    @Override
+    public Flux<BlobId> listReferencedBlobs() {
+        return postgresAttachmentDAO.listBlobs();
+    }
+
+}
diff --git a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/mail/PostgresAttachmentBlobReferenceSourceTest.java b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/mail/PostgresAttachmentBlobReferenceSourceTest.java
new file mode 100644
index 0000000000..cfe0be5600
--- /dev/null
+++ b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/mail/PostgresAttachmentBlobReferenceSourceTest.java
@@ -0,0 +1,111 @@
+/****************************************************************
+ * 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.mail;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.james.backends.postgres.PostgresExtension;
+import org.apache.james.blob.api.BlobId;
+import org.apache.james.blob.api.HashBlobId;
+import org.apache.james.mailbox.model.AttachmentId;
+import org.apache.james.mailbox.model.AttachmentMetadata;
+import org.apache.james.mailbox.postgres.PostgresMailboxAggregateModule;
+import org.apache.james.mailbox.postgres.PostgresMessageId;
+import org.apache.james.mailbox.postgres.mail.dao.PostgresAttachmentDAO;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+class PostgresAttachmentBlobReferenceSourceTest {
+
+    private static final AttachmentId ATTACHMENT_ID = AttachmentId.from("id1");
+    private static final AttachmentId ATTACHMENT_ID_2 = AttachmentId.from("id2");
+    private static final HashBlobId.Factory BLOB_ID_FACTORY = new HashBlobId.Factory();
+
+    @RegisterExtension
+    static PostgresExtension postgresExtension = PostgresExtension.withoutRowLevelSecurity(PostgresMailboxAggregateModule.MODULE);
+
+    private PostgresAttachmentBlobReferenceSource testee;
+
+    private PostgresAttachmentDAO postgresAttachmentDAO;
+
+    @BeforeEach
+    void beforeEach() {
+        HashBlobId.Factory blobIdFactory = new HashBlobId.Factory();
+        postgresAttachmentDAO = new PostgresAttachmentDAO(postgresExtension.getPostgresExecutor(),
+            blobIdFactory);
+        testee = new PostgresAttachmentBlobReferenceSource(postgresAttachmentDAO);
+    }
+
+    @Test
+    void blobReferencesShouldBeEmptyByDefault() {
+        assertThat(testee.listReferencedBlobs().collectList().block())
+            .isEmpty();
+    }
+
+    @Test
+    void blobReferencesShouldReturnAllValues() {
+        AttachmentMetadata attachment1 = AttachmentMetadata.builder()
+            .attachmentId(ATTACHMENT_ID)
+            .messageId(new PostgresMessageId.Factory().generate())
+            .type("application/json")
+            .size(36)
+            .build();
+        BlobId blobId1 = BLOB_ID_FACTORY.from("blobId");
+
+        postgresAttachmentDAO.storeAttachment(attachment1, blobId1).block();
+
+        AttachmentMetadata attachment2 = AttachmentMetadata.builder()
+            .attachmentId(ATTACHMENT_ID_2)
+            .messageId(new PostgresMessageId.Factory().generate())
+            .type("application/json")
+            .size(36)
+            .build();
+        BlobId blobId2 = BLOB_ID_FACTORY.from("blobId");
+        postgresAttachmentDAO.storeAttachment(attachment2, blobId2).block();
+
+        assertThat(testee.listReferencedBlobs().collectList().block())
+            .containsOnly(blobId1, blobId2);
+    }
+
+    @Test
+    void blobReferencesShouldReturnDuplicates() {
+        AttachmentMetadata attachment1 = AttachmentMetadata.builder()
+            .attachmentId(ATTACHMENT_ID)
+            .messageId(new PostgresMessageId.Factory().generate())
+            .type("application/json")
+            .size(36)
+            .build();
+        BlobId blobId = BLOB_ID_FACTORY.from("blobId");
+        postgresAttachmentDAO.storeAttachment(attachment1, blobId).block();
+
+        AttachmentMetadata attachment2 = AttachmentMetadata.builder()
+            .attachmentId(ATTACHMENT_ID_2)
+            .messageId(new PostgresMessageId.Factory().generate())
+            .type("application/json")
+            .size(36)
+            .build();
+        postgresAttachmentDAO.storeAttachment(attachment2, blobId).block();
+
+        assertThat(testee.listReferencedBlobs().collectList().block())
+            .hasSize(2)
+            .containsOnly(blobId);
+    }
+}
diff --git a/server/container/guice/mailbox-postgres/src/main/java/org/apache/james/modules/mailbox/PostgresMailboxModule.java b/server/container/guice/mailbox-postgres/src/main/java/org/apache/james/modules/mailbox/PostgresMailboxModule.java
index b248153e62..58767aba19 100644
--- a/server/container/guice/mailbox-postgres/src/main/java/org/apache/james/modules/mailbox/PostgresMailboxModule.java
+++ b/server/container/guice/mailbox-postgres/src/main/java/org/apache/james/modules/mailbox/PostgresMailboxModule.java
@@ -52,6 +52,8 @@ import org.apache.james.mailbox.postgres.PostgresMailboxId;
 import org.apache.james.mailbox.postgres.PostgresMailboxManager;
 import org.apache.james.mailbox.postgres.PostgresMailboxSessionMapperFactory;
 import org.apache.james.mailbox.postgres.PostgresMessageId;
+import org.apache.james.mailbox.postgres.mail.PostgresAttachmentBlobReferenceSource;
+import org.apache.james.mailbox.postgres.mail.PostgresMailboxManager;
 import org.apache.james.mailbox.postgres.mail.PostgresMessageBlobReferenceSource;
 import org.apache.james.mailbox.postgres.mail.dao.PostgresMessageDAO;
 import org.apache.james.mailbox.store.MailboxManagerConfiguration;
@@ -124,7 +126,6 @@ public class PostgresMailboxModule extends AbstractModule {
         bind(Authorizator.class).to(UserRepositoryAuthorizator.class);
         bind(MailboxId.Factory.class).to(PostgresMailboxId.Factory.class);
         bind(MailboxACLResolver.class).to(UnionMailboxACLResolver.class);
-        bind(AttachmentContentLoader.class).to(PostgresAttachmentContentLoader.class);
         bind(MessageIdManager.class).to(StoreMessageIdManager.class);
         bind(RightManager.class).to(StoreRightManager.class);
         bind(AttachmentManager.class).to(StoreAttachmentManager.class);
@@ -162,6 +163,7 @@ public class PostgresMailboxModule extends AbstractModule {
 
         Multibinder<BlobReferenceSource> blobReferenceSourceMultibinder = Multibinder.newSetBinder(binder(), BlobReferenceSource.class);
         blobReferenceSourceMultibinder.addBinding().to(PostgresMessageBlobReferenceSource.class);
+        blobReferenceSourceMultibinder.addBinding().to(PostgresAttachmentBlobReferenceSource.class);
     }
 
     @Singleton
diff --git a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java
index 5a2950e484..3715e59efc 100644
--- a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java
+++ b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java
@@ -90,7 +90,7 @@ public class PostgresCommonModule extends AbstractModule {
     @Singleton
     JamesPostgresConnectionFactory provideJamesPostgresConnectionFactoryWithRLSBypass(PostgresConfiguration postgresConfiguration,
                                                                                       @Named(JamesPostgresConnectionFactory.NON_RLS_INJECT) ConnectionFactory connectionFactory) {
-        LOGGER.info("Implementation for PostgreSQL connection factory: {}", SinglePostgresConnectionFactory.class.getName());
+        LOGGER.info("Implementation for PostgresSQL connection factory: {}", SinglePostgresConnectionFactory.class.getName());
         return new SinglePostgresConnectionFactory(Mono.from(connectionFactory.create()).block());
     }
 


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