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 ro...@apache.org on 2018/01/25 15:36:42 UTC

[09/14] james-project git commit: JAMES-2291 Introduce Mail repositories count DAO

JAMES-2291 Introduce Mail repositories count DAO


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

Branch: refs/heads/master
Commit: d4d0c8fda078476492c8039a6c03464c967b21ba
Parents: 203b3d2
Author: benwa <bt...@linagora.com>
Authored: Wed Jan 24 10:42:17 2018 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Jan 25 16:28:45 2018 +0700

----------------------------------------------------------------------
 .../CassandraMailRepositoryCountDAO.java        |  95 +++++++++++++++++
 .../CassandraMailRepositoryCountDAOTest.java    | 102 +++++++++++++++++++
 2 files changed, 197 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/d4d0c8fd/server/mailrepository/mailrepository-cassandra/src/main/java/org/apache/james/mailrepository/cassandra/CassandraMailRepositoryCountDAO.java
----------------------------------------------------------------------
diff --git a/server/mailrepository/mailrepository-cassandra/src/main/java/org/apache/james/mailrepository/cassandra/CassandraMailRepositoryCountDAO.java b/server/mailrepository/mailrepository-cassandra/src/main/java/org/apache/james/mailrepository/cassandra/CassandraMailRepositoryCountDAO.java
new file mode 100644
index 0000000..6954097
--- /dev/null
+++ b/server/mailrepository/mailrepository-cassandra/src/main/java/org/apache/james/mailrepository/cassandra/CassandraMailRepositoryCountDAO.java
@@ -0,0 +1,95 @@
+/****************************************************************
+ * 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.mailrepository.cassandra;
+
+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.select;
+import static com.datastax.driver.core.querybuilder.QueryBuilder.update;
+import static org.apache.james.mailrepository.cassandra.MailRepositoryTable.COUNT;
+import static org.apache.james.mailrepository.cassandra.MailRepositoryTable.COUNT_TABLE;
+import static org.apache.james.mailrepository.cassandra.MailRepositoryTable.REPOSITORY_NAME;
+
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+
+import org.apache.james.backends.cassandra.utils.CassandraAsyncExecutor;
+
+import com.datastax.driver.core.PreparedStatement;
+import com.datastax.driver.core.Row;
+import com.datastax.driver.core.Session;
+
+public class CassandraMailRepositoryCountDAO {
+
+    private final CassandraAsyncExecutor executor;
+    private final PreparedStatement increment;
+    private final PreparedStatement decrement;
+    private final PreparedStatement select;
+
+    public CassandraMailRepositoryCountDAO(Session session) {
+        this.executor = new CassandraAsyncExecutor(session);
+
+        this.increment = prepareIncrement(session);
+        this.decrement = prepareDecrement(session);
+        this.select = prepareSelect(session);
+    }
+
+    private PreparedStatement prepareDecrement(Session session) {
+        return session.prepare(update(COUNT_TABLE)
+            .with(decr(COUNT))
+            .where(eq(REPOSITORY_NAME, bindMarker(REPOSITORY_NAME))));
+    }
+
+    private PreparedStatement prepareIncrement(Session session) {
+        return session.prepare(update(COUNT_TABLE)
+            .with(incr(COUNT))
+            .where(eq(REPOSITORY_NAME, bindMarker(REPOSITORY_NAME))));
+    }
+
+    private PreparedStatement prepareSelect(Session session) {
+        return session.prepare(select(COUNT)
+            .from(COUNT_TABLE)
+            .where(eq(REPOSITORY_NAME, bindMarker(REPOSITORY_NAME))));
+    }
+
+    public CompletableFuture<Void> increment(String url) {
+        return executor.executeVoid(increment.bind()
+            .setString(REPOSITORY_NAME, url));
+    }
+
+    public CompletableFuture<Void> decrement(String url) {
+        return executor.executeVoid(decrement.bind()
+            .setString(REPOSITORY_NAME, url));
+    }
+
+    public CompletableFuture<Long> getCount(String url) {
+        return executor.executeSingleRow(select.bind()
+                .setString(REPOSITORY_NAME, url))
+            .thenApply(this::toCount);
+    }
+
+    private Long toCount(Optional<Row> rowOptional) {
+        return rowOptional
+            .map(row -> row.getLong(COUNT))
+            .orElse(0L);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/d4d0c8fd/server/mailrepository/mailrepository-cassandra/src/test/java/org/apache/james/mailrepository/cassandra/CassandraMailRepositoryCountDAOTest.java
----------------------------------------------------------------------
diff --git a/server/mailrepository/mailrepository-cassandra/src/test/java/org/apache/james/mailrepository/cassandra/CassandraMailRepositoryCountDAOTest.java b/server/mailrepository/mailrepository-cassandra/src/test/java/org/apache/james/mailrepository/cassandra/CassandraMailRepositoryCountDAOTest.java
new file mode 100644
index 0000000..9f3a7e3
--- /dev/null
+++ b/server/mailrepository/mailrepository-cassandra/src/test/java/org/apache/james/mailrepository/cassandra/CassandraMailRepositoryCountDAOTest.java
@@ -0,0 +1,102 @@
+/****************************************************************
+ * 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.mailrepository.cassandra;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.james.backends.cassandra.CassandraCluster;
+import org.apache.james.backends.cassandra.DockerCassandraExtension;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+@ExtendWith(DockerCassandraExtension.class)
+public class CassandraMailRepositoryCountDAOTest {
+    static final String URL = "url";
+    static final String URL2 = "url2";
+
+    CassandraCluster cassandra;
+    CassandraMailRepositoryCountDAO testee;
+
+    @BeforeEach
+    public void setUp(DockerCassandraExtension.DockerCassandra dockerCassandra) {
+        cassandra = CassandraCluster.create(
+            new CassandraMailRepositoryModule(), dockerCassandra.getIp(), dockerCassandra.getBindingPort());
+
+        testee = new CassandraMailRepositoryCountDAO(cassandra.getConf());
+    }
+
+    @AfterEach
+    public void tearDown() {
+        cassandra.close();
+    }
+
+    @Test
+    public void getCountShouldReturnZeroWhenEmpty() {
+        assertThat(testee.getCount(URL).join())
+            .isEqualTo(0L);
+    }
+
+    @Test
+    public void getCountShouldReturnOneWhenIncrementedOneTime() {
+        testee.increment(URL).join();
+
+        assertThat(testee.getCount(URL).join())
+            .isEqualTo(1L);
+    }
+
+    @Test
+    public void incrementShouldNotAffectOtherUrls() {
+        testee.increment(URL).join();
+
+        assertThat(testee.getCount(URL2).join())
+            .isEqualTo(0L);
+    }
+
+    @Test
+    public void incrementCanBeAppliedSeveralTime() {
+        testee.increment(URL).join();
+        testee.increment(URL).join();
+
+        assertThat(testee.getCount(URL).join())
+            .isEqualTo(2L);
+    }
+
+    @Test
+    public void decrementShouldDecreaseCount() {
+        testee.increment(URL).join();
+        testee.increment(URL).join();
+        testee.increment(URL).join();
+
+        testee.decrement(URL).join();
+
+        assertThat(testee.getCount(URL).join())
+            .isEqualTo(2L);
+    }
+
+    @Test
+    public void decrementCanLeadToNegativeCount() {
+        testee.decrement(URL).join();
+
+        assertThat(testee.getCount(URL).join())
+            .isEqualTo(-1L);
+    }
+}
\ No newline at end of file


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