Repository: james-project
Updated Branches:
  refs/heads/master 1e19c150b -> f2cca215c


JAMES-2093 remove in clause from AttachmentMapper


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

Branch: refs/heads/master
Commit: 2c0c5f000e3e4167efaefd9c13345ae28a13b7b8
Parents: 1128983
Author: Luc DUZAN <ldu...@linagora.com>
Authored: Thu Jul 6 12:11:38 2017 +0200
Committer: Luc DUZAN <ldu...@linagora.com>
Committed: Thu Jul 6 12:11:38 2017 +0200

----------------------------------------------------------------------
 .../mail/CassandraAttachmentMapper.java         | 44 +++++++++++---------
 1 file changed, 25 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/2c0c5f00/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 cc295ec..61edbba 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
@@ -20,7 +20,6 @@
 package org.apache.james.mailbox.cassandra.mail;
 
 import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
-import static com.datastax.driver.core.querybuilder.QueryBuilder.in;
 import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto;
 import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
 import static 
org.apache.james.mailbox.cassandra.table.CassandraAttachmentTable.FIELDS;
@@ -34,7 +33,9 @@ import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.Collection;
 import java.util.List;
+import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
+import java.util.stream.Stream;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.james.backends.cassandra.utils.CassandraAsyncExecutor;
@@ -43,16 +44,15 @@ import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.model.Attachment;
 import org.apache.james.mailbox.model.AttachmentId;
 import org.apache.james.mailbox.store.mail.AttachmentMapper;
+import org.apache.james.util.FluentFutureStream;
 
-import com.datastax.driver.core.ResultSet;
 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.github.steveash.guavate.Guavate;
 import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableList.Builder;
+import org.apache.james.util.OptionalConverter;
 
 public class CassandraAttachmentMapper implements AttachmentMapper {
 
@@ -98,23 +98,29 @@ public class CassandraAttachmentMapper implements 
AttachmentMapper {
 
     public CompletableFuture<List<Attachment>> 
getAttachmentsAsFuture(Collection<AttachmentId> attachmentIds) {
         Preconditions.checkArgument(attachmentIds != null);
-        if (attachmentIds.isEmpty()) {
-            return CompletableFuture.completedFuture(ImmutableList.of());
-        }
-        List<String> ids = attachmentIds.stream()
-                .map(AttachmentId::getId)
-                .collect(Guavate.toImmutableList());
-        return cassandraAsyncExecutor.execute(
-            select(FIELDS)
-                .from(TABLE_NAME)
-                .where(in(ID, ids)))
-            .thenApply(this::attachments);
+
+        Stream<CompletableFuture<Optional<Attachment>>> attachments = 
attachmentIds
+                .stream()
+                .distinct()
+                .map(this::getAttachmentAsFuture);
+
+        return FluentFutureStream
+            .of(attachments)
+            .flatMap(OptionalConverter::toStream)
+            .completableFuture()
+            .thenApply(stream ->
+                stream.collect(Guavate.toImmutableList()));
     }
 
-    private List<Attachment> attachments(ResultSet resultSet) {
-        Builder<Attachment> builder = ImmutableList.<Attachment> builder();
-        resultSet.forEach(row -> builder.add(attachment(row)));
-        return builder.build();
+    private CompletableFuture<Optional<Attachment>> 
getAttachmentAsFuture(AttachmentId attachmentId) {
+        String id = attachmentId.getId();
+
+        return cassandraAsyncExecutor.executeSingleRow(
+            select(FIELDS)
+                .from(TABLE_NAME)
+                .where(eq(ID, id)))
+            .thenApply(optional ->
+                optional.map(this::attachment));
     }
 
     @Override


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

Reply via email to