james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [06/13] james-project git commit: JAMES-1925 Improve CassandraMailboxMapper::save reading
Date Mon, 20 Feb 2017 09:08:50 GMT
JAMES-1925 Improve CassandraMailboxMapper::save reading


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

Branch: refs/heads/master
Commit: 55757ba8a0f2d738d78e04ca077928d6a620db82
Parents: 5acec71
Author: Benoit Tellier <btellier@linagora.com>
Authored: Thu Feb 16 09:13:55 2017 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Mon Feb 20 16:06:31 2017 +0700

----------------------------------------------------------------------
 .../cassandra/mail/CassandraMailboxMapper.java  | 58 ++++++++++----------
 .../SetMailboxesUpdateProcessorTest.java        |  5 +-
 2 files changed, 33 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/55757ba8/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java
----------------------------------------------------------------------
diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java
b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java
index 6850dcb..f863523 100644
--- a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java
+++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java
@@ -129,42 +129,42 @@ public class CassandraMailboxMapper implements MailboxMapper {
         CassandraId cassandraId = retrieveId(cassandraMailbox);
         cassandraMailbox.setMailboxId(cassandraId);
         try {
-            boolean applied = mailboxPathDAO.save(mailbox.generateAssociatedPath(), cassandraId)
-                    .thenCompose(result -> {
-                                if (result) {
-                                    return mailboxDAO.retrieveMailbox(cassandraId)
-                                            .thenCompose(optional -> {
-                                                CompletableFuture<Void> delete = optional
-                                                        .map(storedMailbox -> mailboxPathDAO
-                                                                .delete(storedMailbox.generateAssociatedPath())
-                                                        )
-                                                        .orElse(CompletableFuture.completedFuture(null));
-
-                                                CompletableFuture<Void> save = mailboxDAO.save(cassandraMailbox);
-
-                                                return CompletableFuture.allOf(delete, save).thenApply(any
-> result);
-                                            });
-                                }
-
-                                return CompletableFuture.completedFuture(result);
-                            }
-                    ).join();
-
+            boolean applied = trySave(cassandraMailbox, cassandraId).join();
             if (!applied) {
                 throw new MailboxExistsException(mailbox.generateAssociatedPath().asString());
             }
         } catch (CompletionException e) {
-            if (e.getCause() instanceof InvalidQueryException) {
-                String errorMessage = e.getCause().getMessage();
-                if (StringUtils.containsIgnoreCase(errorMessage, VALUES_MAY_NOT_BE_LARGER_THAN_64_K)
||
-                        StringUtils.containsIgnoreCase(errorMessage, CLUSTERING_COLUMNS_IS_TOO_LONG))
{
-                    throw new TooLongMailboxNameException("too long mailbox name");
+            manageException(e);
+        }
+        return cassandraId;
+    }
+
+    private CompletableFuture<Boolean> trySave(SimpleMailbox cassandraMailbox, CassandraId
cassandraId) {
+        return mailboxPathDAO.save(cassandraMailbox.generateAssociatedPath(), cassandraId)
+            .thenCompose(result -> {
+                if (result) {
+                    return mailboxDAO.retrieveMailbox(cassandraId)
+                        .thenCompose(optional -> CompletableFuture
+                            .allOf(optional
+                                    .map(storedMailbox -> mailboxPathDAO.delete(storedMailbox.generateAssociatedPath()))
+                                    .orElse(CompletableFuture.completedFuture(null)),
+                                mailboxDAO.save(cassandraMailbox))
+                            .thenApply(any -> result));
                 }
-                throw new MailboxException("It has error with cassandra storage", e.getCause());
+                return CompletableFuture.completedFuture(result);
+            });
+    }
+
+    private void manageException(CompletionException e) throws MailboxException {
+        if (e.getCause() instanceof InvalidQueryException) {
+            String errorMessage = e.getCause().getMessage();
+            if (StringUtils.containsIgnoreCase(errorMessage, VALUES_MAY_NOT_BE_LARGER_THAN_64_K)
||
+                    StringUtils.containsIgnoreCase(errorMessage, CLUSTERING_COLUMNS_IS_TOO_LONG))
{
+                throw new TooLongMailboxNameException("too long mailbox name");
             }
-            throw e;
+            throw new MailboxException("It has error with cassandra storage", e.getCause());
         }
-        return cassandraId;
+        throw e;
     }
 
     private CassandraId retrieveId(SimpleMailbox cassandraMailbox) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/55757ba8/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMailboxesUpdateProcessorTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMailboxesUpdateProcessorTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMailboxesUpdateProcessorTest.java
index 9f9c0fc..409ba09 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMailboxesUpdateProcessorTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMailboxesUpdateProcessorTest.java
@@ -20,7 +20,10 @@
 package org.apache.james.jmap.methods;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 import org.mockito.Mockito;
 


---------------------------------------------------------------------
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