james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [3/3] james-project git commit: MAILBOX-268 Async store of multiple attachments
Date Thu, 02 Jun 2016 07:12:50 GMT
MAILBOX-268 Async store of multiple attachments


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/be69ed1c
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/be69ed1c
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/be69ed1c

Branch: refs/heads/master
Commit: be69ed1cff958ffdb4f91737620fc0d83a28a0af
Parents: bbcba2c
Author: Raphael Ouazana <raphael.ouazana@linagora.com>
Authored: Wed Jun 1 12:47:57 2016 +0200
Committer: Raphael Ouazana <raphael.ouazana@linagora.com>
Committed: Wed Jun 1 17:15:09 2016 +0200

----------------------------------------------------------------------
 .../mailbox/exception/MailboxException.java     |  2 +-
 .../mail/CassandraAttachmentMapper.java         | 35 ++++++++++++++++----
 .../inmemory/mail/InMemoryAttachmentMapper.java |  8 +++++
 .../mailbox/store/StoreMessageManager.java      |  4 +--
 .../mailbox/store/mail/AttachmentMapper.java    |  4 +++
 .../store/mail/NoopAttachmentMapper.java        |  6 ++++
 .../model/AbstractAttachmentMapperTest.java     | 18 ++++++++++
 7 files changed, 66 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/be69ed1c/mailbox/api/src/main/java/org/apache/james/mailbox/exception/MailboxException.java
----------------------------------------------------------------------
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/exception/MailboxException.java
b/mailbox/api/src/main/java/org/apache/james/mailbox/exception/MailboxException.java
index 6bdc010..8ab199a 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/exception/MailboxException.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/exception/MailboxException.java
@@ -35,7 +35,7 @@ public class MailboxException extends Exception {
         super(message);
     }
 
-    public MailboxException(String msg, Exception cause) {
+    public MailboxException(String msg, Throwable cause) {
         super(msg, cause);
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/be69ed1c/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java
----------------------------------------------------------------------
diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java
b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java
index 68cacfa..865c5fb 100644
--- a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java
+++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java
@@ -32,6 +32,8 @@ import static org.apache.james.mailbox.cassandra.table.CassandraAttachmentTable.
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.util.Collection;
+import java.util.concurrent.CompletableFuture;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.james.backends.cassandra.utils.CassandraAsyncExecutor;
@@ -44,6 +46,8 @@ import org.apache.james.mailbox.store.mail.model.AttachmentId;
 import com.datastax.driver.core.PreparedStatement;
 import com.datastax.driver.core.Row;
 import com.datastax.driver.core.Session;
+import com.github.fge.lambdas.Throwing;
+import com.github.fge.lambdas.ThrownByLambdaException;
 import com.google.common.base.Preconditions;
 
 public class CassandraAttachmentMapper implements AttachmentMapper {
@@ -95,15 +99,32 @@ public class CassandraAttachmentMapper implements AttachmentMapper {
     @Override
     public void storeAttachment(Attachment attachment) throws MailboxException {
         try {
-            cassandraAsyncExecutor.execute(
-                    insert.bind()
-                        .setString(ID, attachment.getAttachmentId().getId())
-                        .setBytes(PAYLOAD, ByteBuffer.wrap(IOUtils.toByteArray(attachment.getStream())))
-                        .setString(TYPE, attachment.getType())
-                        .setLong(SIZE, attachment.getSize())
-                ).join();
+            asyncStoreAttachment(attachment).join();
         } catch (IOException e) {
             throw new MailboxException(e.getMessage(), e);
         }
     }
+
+    private CompletableFuture<Void> asyncStoreAttachment(Attachment attachment) throws
IOException {
+        return cassandraAsyncExecutor.executeVoid(
+                insert.bind()
+                    .setString(ID, attachment.getAttachmentId().getId())
+                    .setBytes(PAYLOAD, ByteBuffer.wrap(IOUtils.toByteArray(attachment.getStream())))
+                    .setString(TYPE, attachment.getType())
+                    .setLong(SIZE, attachment.getSize())
+            );
+    }
+
+    @Override
+    public void storeAttachments(Collection<Attachment> attachments) throws MailboxException
{
+        try {
+            CompletableFuture.allOf(
+                    attachments.stream()
+                        .map(Throwing.function(this::asyncStoreAttachment))
+                        .toArray(CompletableFuture[]::new)
+                ).join();
+        } catch (ThrownByLambdaException e) {
+            throw new MailboxException(e.getCause().getMessage(), e.getCause());
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/be69ed1c/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryAttachmentMapper.java
----------------------------------------------------------------------
diff --git a/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryAttachmentMapper.java
b/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryAttachmentMapper.java
index 2b0339d..d4decc9 100644
--- a/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryAttachmentMapper.java
+++ b/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryAttachmentMapper.java
@@ -18,6 +18,7 @@
  ****************************************************************/
 package org.apache.james.mailbox.inmemory.mail;
 
+import java.util.Collection;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -55,4 +56,11 @@ public class InMemoryAttachmentMapper implements AttachmentMapper {
     public <T> T execute(Transaction<T> transaction) throws MailboxException
{
         return transaction.run();
     }
+
+    @Override
+    public void storeAttachments(Collection<Attachment> attachments) throws MailboxException
{
+        for (Attachment attachment: attachments) {
+            storeAttachment(attachment);
+        }
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/be69ed1c/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
index 02f480f..752fa41 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
@@ -641,9 +641,7 @@ public class StoreMessageManager implements org.apache.james.mailbox.MessageMana
         return mapperFactory.getMessageMapper(session).execute(new Mapper.Transaction<MessageMetaData>()
{
 
             public MessageMetaData run() throws MailboxException {
-                for (Attachment attachment: attachments) {
-                    attachmentMapper.storeAttachment(attachment);
-                }
+                attachmentMapper.storeAttachments(attachments);
                 return messageMapper.add(getMailboxEntity(), message);
             }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/be69ed1c/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/AttachmentMapper.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/AttachmentMapper.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/AttachmentMapper.java
index e720a80..e7386da 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/AttachmentMapper.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/AttachmentMapper.java
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.james.mailbox.store.mail;
 
+import java.util.Collection;
+
 import org.apache.james.mailbox.exception.AttachmentNotFoundException;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.store.mail.model.Attachment;
@@ -29,4 +31,6 @@ public interface AttachmentMapper extends Mapper {
     Attachment getAttachment(AttachmentId attachmentId) throws AttachmentNotFoundException;
 
     void storeAttachment(Attachment attachment) throws MailboxException;
+
+    void storeAttachments(Collection<Attachment> attachments) throws MailboxException;
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/be69ed1c/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/NoopAttachmentMapper.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/NoopAttachmentMapper.java
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/NoopAttachmentMapper.java
index b6aeb59..e783174 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/NoopAttachmentMapper.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/NoopAttachmentMapper.java
@@ -19,6 +19,8 @@
 
 package org.apache.james.mailbox.store.mail;
 
+import java.util.Collection;
+
 import org.apache.james.mailbox.exception.AttachmentNotFoundException;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.store.mail.model.Attachment;
@@ -44,4 +46,8 @@ public class NoopAttachmentMapper implements AttachmentMapper {
     @Override
     public void storeAttachment(Attachment attachment) throws MailboxException {
     }
+
+    @Override
+    public void storeAttachments(Collection<Attachment> attachments) throws MailboxException
{
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/be69ed1c/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AbstractAttachmentMapperTest.java
----------------------------------------------------------------------
diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AbstractAttachmentMapperTest.java
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AbstractAttachmentMapperTest.java
index ab31166..2030cfb 100644
--- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AbstractAttachmentMapperTest.java
+++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/AbstractAttachmentMapperTest.java
@@ -28,6 +28,8 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
+
 public abstract class AbstractAttachmentMapperTest {
 
     private MapperProvider mapperProvider;
@@ -69,4 +71,20 @@ public abstract class AbstractAttachmentMapperTest {
         //Then
         assertThat(attachment).isEqualTo(expected);
     }
+
+    @Test
+    public void getAttachmentShouldReturnTheAttachmentsWhenMultipleStored() throws Exception
{
+        //Given
+        Attachment expected1 = Attachment.from("payload1".getBytes(), "content1");
+        Attachment expected2 = Attachment.from("payload2".getBytes(), "content2");
+        AttachmentId attachmentId1 = expected1.getAttachmentId();
+        AttachmentId attachmentId2 = expected2.getAttachmentId();
+        //When
+        attachmentMapper.storeAttachments(ImmutableList.of(expected1, expected2));
+        //Then
+        Attachment attachment1 = attachmentMapper.getAttachment(attachmentId1);
+        Attachment attachment2 = attachmentMapper.getAttachment(attachmentId2);
+        assertThat(attachment1).isEqualTo(expected1);
+        assertThat(attachment2).isEqualTo(expected2);
+    }
 }


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


Mime
View raw message