You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2015/09/22 12:25:40 UTC

svn commit: r1704541 - in /james/mailbox/trunk/cassandra/src: main/java/org/apache/james/mailbox/cassandra/ main/java/org/apache/james/mailbox/cassandra/quota/ main/java/org/apache/james/mailbox/cassandra/table/ test/java/org/apache/james/mailbox/cassa...

Author: btellier
Date: Tue Sep 22 10:25:37 2015
New Revision: 1704541

URL: http://svn.apache.org/viewvc?rev=1704541&view=rev
Log:
MAILBOX-64 Cassandra stored CurrentQuotaManager

Added:
    james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/quota/CassandraCurrentQuotaManager.java
    james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/table/CassandraCurrentQuota.java
    james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/quota/CassandraCurrentQuotaManagerTest.java
Modified:
    james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/CassandraTableManager.java

Modified: james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/CassandraTableManager.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/CassandraTableManager.java?rev=1704541&r1=1704540&r2=1704541&view=diff
==============================================================================
--- james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/CassandraTableManager.java (original)
+++ james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/CassandraTableManager.java Tue Sep 22 10:25:37 2015
@@ -36,6 +36,7 @@ import com.datastax.driver.core.schemabu
 import com.datastax.driver.core.schemabuilder.SchemaStatement;
 
 import org.apache.james.mailbox.cassandra.table.CassandraACLTable;
+import org.apache.james.mailbox.cassandra.table.CassandraCurrentQuota;
 import org.apache.james.mailbox.cassandra.table.CassandraDefaultMaxQuota;
 import org.apache.james.mailbox.cassandra.table.CassandraMailboxCountersTable;
 import org.apache.james.mailbox.cassandra.table.CassandraMailboxTable;
@@ -112,6 +113,12 @@ public class CassandraTableManager {
                 .ifNotExists()
                 .addPartitionKey(CassandraMessageModseqTable.MAILBOX_ID, timeuuid())
                 .addColumn(CassandraMessageModseqTable.NEXT_MODSEQ, bigint())),
+        CurrentQuota(CassandraCurrentQuota.TABLE_NAME,
+            SchemaBuilder.createTable(CassandraCurrentQuota.TABLE_NAME)
+                .ifNotExists()
+                .addPartitionKey(CassandraCurrentQuota.QUOTA_ROOT, text())
+                .addColumn(CassandraCurrentQuota.MESSAGE_COUNT, counter())
+                .addColumn(CassandraCurrentQuota.STORAGE, counter())),
         MaxQuota(CassandraMaxQuota.TABLE_NAME,
             SchemaBuilder.createTable(CassandraMaxQuota.TABLE_NAME)
                 .ifNotExists()

Added: james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/quota/CassandraCurrentQuotaManager.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/quota/CassandraCurrentQuotaManager.java?rev=1704541&view=auto
==============================================================================
--- james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/quota/CassandraCurrentQuotaManager.java (added)
+++ james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/quota/CassandraCurrentQuotaManager.java Tue Sep 22 10:25:37 2015
@@ -0,0 +1,98 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.cassandra.quota;
+
+import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker;
+import static com.datastax.driver.core.querybuilder.QueryBuilder.decr;
+import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
+import static com.datastax.driver.core.querybuilder.QueryBuilder.incr;
+import static com.datastax.driver.core.querybuilder.QueryBuilder.update;
+import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
+
+import com.datastax.driver.core.PreparedStatement;
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Session;
+import com.google.common.base.Preconditions;
+import org.apache.james.mailbox.cassandra.table.CassandraCurrentQuota;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.QuotaRoot;
+import org.apache.james.mailbox.store.quota.StoreCurrentQuotaManager;
+
+public class CassandraCurrentQuotaManager implements StoreCurrentQuotaManager {
+
+    private final Session session;
+    private final PreparedStatement increaseStatement;
+    private final PreparedStatement decreaseStatement;
+    private final PreparedStatement getCurrentMessageCountStatement;
+    private final PreparedStatement getCurrentStorageStatement;
+
+    public CassandraCurrentQuotaManager(Session session) {
+        this.session = session;
+        this.increaseStatement = session.prepare(update(CassandraCurrentQuota.TABLE_NAME)
+            .with(incr(CassandraCurrentQuota.MESSAGE_COUNT, bindMarker()))
+            .and(incr(CassandraCurrentQuota.STORAGE, bindMarker()))
+            .where(eq(CassandraCurrentQuota.QUOTA_ROOT, bindMarker())));
+        this.decreaseStatement = session.prepare(update(CassandraCurrentQuota.TABLE_NAME)
+            .with(decr(CassandraCurrentQuota.MESSAGE_COUNT, bindMarker()))
+            .and(decr(CassandraCurrentQuota.STORAGE, bindMarker()))
+            .where(eq(CassandraCurrentQuota.QUOTA_ROOT, bindMarker())));
+        this.getCurrentMessageCountStatement = session.prepare(select(CassandraCurrentQuota.MESSAGE_COUNT)
+            .from(CassandraCurrentQuota.TABLE_NAME)
+            .where(eq(CassandraCurrentQuota.QUOTA_ROOT, bindMarker())));
+        this.getCurrentStorageStatement = session.prepare(select(CassandraCurrentQuota.STORAGE)
+            .from(CassandraCurrentQuota.TABLE_NAME)
+            .where(eq(CassandraCurrentQuota.QUOTA_ROOT, bindMarker())));
+    }
+
+    @Override
+    public void increase(QuotaRoot quotaRoot, long count, long size) throws MailboxException {
+        checkArguments(count, size);
+        session.execute(increaseStatement.bind(count, size, quotaRoot.getValue()));
+    }
+
+    @Override
+    public void decrease(QuotaRoot quotaRoot, long count, long size) throws MailboxException {
+        checkArguments(count, size);
+        session.execute(decreaseStatement.bind(count, size, quotaRoot.getValue()));
+    }
+
+    @Override
+    public long getCurrentMessageCount(QuotaRoot quotaRoot) throws MailboxException {
+        ResultSet resultSet = session.execute(getCurrentMessageCountStatement.bind(quotaRoot.getValue()));
+        if (resultSet.isExhausted()) {
+            return 0L;
+        }
+        return resultSet.one().getLong(CassandraCurrentQuota.MESSAGE_COUNT);
+    }
+
+    @Override
+    public long getCurrentStorage(QuotaRoot quotaRoot) throws MailboxException {
+        ResultSet resultSet = session.execute(getCurrentStorageStatement.bind(quotaRoot.getValue()));
+        if (resultSet.isExhausted()) {
+            return 0L;
+        }
+        return resultSet.one().getLong(CassandraCurrentQuota.STORAGE);
+    }
+
+    private void checkArguments(long count, long size) {
+        Preconditions.checkArgument(count > 0, "Count should be positive");
+        Preconditions.checkArgument(size > 0, "Size should be positive");
+    }
+}

Added: james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/table/CassandraCurrentQuota.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/table/CassandraCurrentQuota.java?rev=1704541&view=auto
==============================================================================
--- james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/table/CassandraCurrentQuota.java (added)
+++ james/mailbox/trunk/cassandra/src/main/java/org/apache/james/mailbox/cassandra/table/CassandraCurrentQuota.java Tue Sep 22 10:25:37 2015
@@ -0,0 +1,29 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.cassandra.table;
+
+public interface CassandraCurrentQuota {
+
+    String TABLE_NAME = "currentQuota";
+
+    String QUOTA_ROOT = "quotaRoot";
+    String MESSAGE_COUNT = "messageCount";
+    String STORAGE = "storage";
+}

Added: james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/quota/CassandraCurrentQuotaManagerTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/quota/CassandraCurrentQuotaManagerTest.java?rev=1704541&view=auto
==============================================================================
--- james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/quota/CassandraCurrentQuotaManagerTest.java (added)
+++ james/mailbox/trunk/cassandra/src/test/java/org/apache/james/mailbox/cassandra/quota/CassandraCurrentQuotaManagerTest.java Tue Sep 22 10:25:37 2015
@@ -0,0 +1,115 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailbox.cassandra.quota;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.james.mailbox.cassandra.CassandraClusterSingleton;
+import org.apache.james.mailbox.model.QuotaRoot;
+import org.apache.james.mailbox.store.quota.QuotaRootImpl;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class CassandraCurrentQuotaManagerTest {
+
+    private static final QuotaRoot QUOTA_ROOT = QuotaRootImpl.quotaRoot("value");
+
+    private CassandraClusterSingleton cassandra;
+    private CassandraCurrentQuotaManager currentQuotaManager;
+
+    @Before
+    public void setUp() {
+        cassandra = CassandraClusterSingleton.build();
+        cassandra.ensureAllTables();
+        currentQuotaManager = new CassandraCurrentQuotaManager(cassandra.getConf());
+    }
+
+    @After
+    public void cleanUp() {
+        cassandra.clearAllTables();
+    }
+
+    @Test
+    public void getCurrentStorageShouldReturnZeroByDefault() throws Exception {
+        assertThat(currentQuotaManager.getCurrentStorage(QUOTA_ROOT)).isEqualTo(0);
+    }
+
+    @Test
+    public void getCurrentMessageCountShouldReturnZeroByDefault() throws Exception {
+        assertThat(currentQuotaManager.getCurrentMessageCount(QUOTA_ROOT)).isEqualTo(0);
+    }
+
+    @Test
+    public void increaseShouldWork() throws Exception {
+        currentQuotaManager.increase(QUOTA_ROOT, 2, 2000);
+        assertThat(currentQuotaManager.getCurrentStorage(QUOTA_ROOT)).isEqualTo(2000);
+        assertThat(currentQuotaManager.getCurrentMessageCount(QUOTA_ROOT)).isEqualTo(2);
+    }
+
+    @Test
+    public void decreaseShouldWork() throws Exception {
+        currentQuotaManager.increase(QUOTA_ROOT, 2, 2000);
+        currentQuotaManager.decrease(QUOTA_ROOT, 1, 1000);
+        assertThat(currentQuotaManager.getCurrentStorage(QUOTA_ROOT)).isEqualTo(1000);
+        assertThat(currentQuotaManager.getCurrentMessageCount(QUOTA_ROOT)).isEqualTo(1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void increaseShouldThrowOnZeroCount() throws Exception {
+        currentQuotaManager.increase(QUOTA_ROOT, 0, 5);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void increaseShouldThrowOnNegativeCount() throws Exception {
+        currentQuotaManager.increase(QUOTA_ROOT, -1, 5);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void increaseShouldThrowOnZeroSize() throws Exception {
+        currentQuotaManager.increase(QUOTA_ROOT, 5, 0);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void increaseShouldThrowOnNegativeSize() throws Exception {
+        currentQuotaManager.increase(QUOTA_ROOT, 5, -1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void decreaseShouldThrowOnZeroCount() throws Exception {
+        currentQuotaManager.decrease(QUOTA_ROOT, 0, 5);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void decreaseShouldThrowOnNegativeCount() throws Exception {
+        currentQuotaManager.decrease(QUOTA_ROOT, -1, 5);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void decreaseShouldThrowOnZeroSize() throws Exception {
+        currentQuotaManager.decrease(QUOTA_ROOT, 5, 0);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void decreaseShouldThrowOnNegativeSize() throws Exception {
+        currentQuotaManager.decrease(QUOTA_ROOT, 5, -1);
+    }
+
+}



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