Skip to content

Commit

Permalink
JAMES-1925 Improve CassandraMailboxMapper::save reading
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa committed Feb 20, 2017
1 parent 5acec71 commit 55757ba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
Expand Up @@ -129,42 +129,42 @@ public MailboxId save(Mailbox mailbox) throws MailboxException {
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) {
Expand Down
Expand Up @@ -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;

Expand Down

0 comments on commit 55757ba

Please sign in to comment.