james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject svn commit: r1689016 - /james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java
Date Fri, 03 Jul 2015 14:34:01 GMT
Author: btellier
Date: Fri Jul  3 14:34:01 2015
New Revision: 1689016

URL: http://svn.apache.org/r1689016
Log:
MAILBOX-208 Improve Cassandra Mailbox Mapper

Modified:
    james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java

Modified: james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java?rev=1689016&r1=1689015&r2=1689016&view=diff
==============================================================================
--- james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java
(original)
+++ james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java
Fri Jul  3 14:34:01 2015
@@ -55,8 +55,6 @@ import com.datastax.driver.core.Row;
 import com.datastax.driver.core.Session;
 import com.datastax.driver.core.querybuilder.QueryBuilder;
 import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableList.Builder;
 
 /**
  * Data access management for mailbox.
@@ -77,7 +75,7 @@ public class CassandraMailboxMapper impl
     @Override
     public void delete(Mailbox<CassandraId> mailbox) throws MailboxException {
         session.execute(
-                QueryBuilder.delete()
+            QueryBuilder.delete()
                 .from(TABLE_NAME)
                 .where(eq(ID, mailbox.getMailboxId().asUuid())));
     }
@@ -92,6 +90,56 @@ public class CassandraMailboxMapper impl
         }
     }
 
+    @Override
+    public List<Mailbox<CassandraId>> findMailboxWithPathLike(MailboxPath path)
throws MailboxException {
+        Pattern regex = Pattern.compile(constructEscapedRegexForMailboxNameMatching(path));
+        return getMailboxFilteredByNamespaceAndUserStream(path.getNamespace(), path.getUser())
+            .filter((row) -> regex.matcher(row.getString(NAME)).matches())
+            .map(this::mailbox)
+            .collect(Collectors.toList());
+    }
+
+    @Override
+    public void save(Mailbox<CassandraId> mailbox) throws MailboxException {
+        Preconditions.checkArgument(mailbox instanceof SimpleMailbox);
+        SimpleMailbox<CassandraId> cassandraMailbox = (SimpleMailbox<CassandraId>)
mailbox;
+        if (cassandraMailbox.getMailboxId() == null) {
+            cassandraMailbox.setMailboxId(CassandraId.timeBased());
+        }
+        upsertMailbox(cassandraMailbox);
+    }
+
+    @Override
+    public boolean hasChildren(Mailbox<CassandraId> mailbox, char delimiter) {
+        final Pattern regex = Pattern.compile(Pattern.quote( mailbox.getName() + String.valueOf(delimiter))
+ ".*");
+        return getMailboxFilteredByNamespaceAndUserStream(mailbox.getNamespace(), mailbox.getUser())
+            .anyMatch((row) -> regex.matcher(row.getString(NAME)).matches());
+    }
+
+    @Override
+    public List<Mailbox<CassandraId>> list() throws MailboxException {
+        return convertToStream(
+            session.execute(select(FIELDS)
+                .from(TABLE_NAME)))
+            .map(this::mailbox)
+            .collect(Collectors.toList());
+    }
+
+    @Override
+    public <T> T execute(Transaction<T> transaction) throws MailboxException
{
+        return transaction.run();
+    }
+
+    @Override
+    public void updateACL(Mailbox<CassandraId> mailbox, MailboxACL.MailboxACLCommand
mailboxACLCommand) throws MailboxException {
+        new CassandraACLMapper(mailbox, session, maxRetry).updateACL(mailboxACLCommand);
+    }
+
+    @Override
+    public void endRequest() {
+        // Do nothing
+    }
+
     private SimpleMailbox<CassandraId> mailbox(Row row) {
         SimpleMailbox<CassandraId> mailbox = new SimpleMailbox<>(
             new MailboxPath(
@@ -104,15 +152,6 @@ public class CassandraMailboxMapper impl
         return mailbox;
     }
 
-    @Override
-    public List<Mailbox<CassandraId>> findMailboxWithPathLike(MailboxPath path)
throws MailboxException {
-        Pattern regex = Pattern.compile(constructEscapedRegexForMailboxNameMatching(path));
-        return getMailboxFilteredByNamespaceAndUserStream(path.getNamespace(), path.getUser())
-            .filter((row) -> regex.matcher(row.getString(NAME)).matches())
-            .map(this::mailbox)
-            .collect(Collectors.toList());
-    }
-
     private String constructEscapedRegexForMailboxNameMatching(MailboxPath path) {
         return Collections
             .list(new StringTokenizer(path.getName(), WILDCARD, true))
@@ -127,16 +166,6 @@ public class CassandraMailboxMapper impl
             ).collect(Collectors.joining());
     }
 
-    @Override
-    public void save(Mailbox<CassandraId> mailbox) throws MailboxException {
-        Preconditions.checkArgument(mailbox instanceof SimpleMailbox);
-        SimpleMailbox<CassandraId> cassandraMailbox = (SimpleMailbox<CassandraId>)
mailbox;
-        if (cassandraMailbox.getMailboxId() == null) {
-            cassandraMailbox.setMailboxId(CassandraId.timeBased());
-        }
-        upsertMailbox(cassandraMailbox);
-    }
-
     private void upsertMailbox(SimpleMailbox<CassandraId> mailbox) throws MailboxException
{
         session.execute(
             insertInto(TABLE_NAME)
@@ -155,47 +184,18 @@ public class CassandraMailboxMapper impl
         return new MailboxPath(mailbox.getNamespace(), mailbox.getUser(), mailbox.getName());
     }
 
-    @Override
-    public void endRequest() {
-        // Do nothing
-    }
-
-    @Override
-    public boolean hasChildren(Mailbox<CassandraId> mailbox, char delimiter) {
-        final Pattern regex = Pattern.compile(Pattern.quote( mailbox.getName() + String.valueOf(delimiter))
+ ".*");
-        return getMailboxFilteredByNamespaceAndUserStream(mailbox.getNamespace(), mailbox.getUser())
-            .anyMatch((row) -> regex.matcher(row.getString(NAME)).matches());
+    private Stream<Row> convertToStream(ResultSet resultSet) {
+        return StreamSupport.stream(resultSet.spliterator(), true);
     }
 
     private Stream<Row> getMailboxFilteredByNamespaceAndUserStream (String namespace,
String user) {
-        return StreamSupport.stream(session.execute(
-                select(FIELDS)
-                    .from(TABLE_NAME)
-                    .where(eq(MAILBOX_BASE, typesProvider.getDefinedUserType(TYPE.MailboxBase)
-                        .newValue()
-                        .setString(MailboxBase.NAMESPACE, namespace)
-                        .setString(MailboxBase.USER, user))))
-                .spliterator(),
-            true);
-    }
-
-    @Override
-    public List<Mailbox<CassandraId>> list() throws MailboxException {
-        Builder<Mailbox<CassandraId>> result = ImmutableList.<Mailbox<CassandraId>>
builder();
-        for (Row row : session.execute(select(FIELDS).from(TABLE_NAME))) {
-            result.add(mailbox(row));
-        }
-        return result.build();
-    }
-
-    @Override
-    public <T> T execute(Transaction<T> transaction) throws MailboxException
{
-        return transaction.run();
-    }
-
-    @Override
-    public void updateACL(Mailbox<CassandraId> mailbox, MailboxACL.MailboxACLCommand
mailboxACLCommand) throws MailboxException {
-        new CassandraACLMapper(mailbox, session, maxRetry).updateACL(mailboxACLCommand);
+        return convertToStream(session.execute(
+            select(FIELDS)
+                .from(TABLE_NAME)
+                .where(eq(MAILBOX_BASE, typesProvider.getDefinedUserType(TYPE.MailboxBase)
+                    .newValue()
+                    .setString(MailboxBase.NAMESPACE, namespace)
+                    .setString(MailboxBase.USER, user)))));
     }
 
 }



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