camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject [03/13] camel git commit: CAMEL-8478: IdempotentRepository - Add clear operation, align CassandraIdempotentRepository
Date Fri, 03 Jul 2015 20:23:47 GMT
CAMEL-8478: IdempotentRepository - Add clear operation, align CassandraIdempotentRepository


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

Branch: refs/heads/master
Commit: b346017ff662a2a656afce15daab603183fcd69a
Parents: c5674e8
Author: Andrea Cosentino <ancosen@gmail.com>
Authored: Fri Jul 3 17:07:36 2015 +0200
Committer: Andrea Cosentino <ancosen@gmail.com>
Committed: Fri Jul 3 17:07:36 2015 +0200

----------------------------------------------------------------------
 .../CassandraIdempotentRepository.java          | 22 ++++++++++++++++++++
 .../camel/utils/cassandra/CassandraUtils.java   | 10 +++++++++
 .../CassandraIdempotentRepositoryTest.java      | 11 ++++++++++
 .../NamedCassandraIdempotentRepositoryTest.java | 13 +++++++++++-
 4 files changed, 55 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b346017f/components/camel-cassandraql/src/main/java/org/apache/camel/processor/idempotent/cassandra/CassandraIdempotentRepository.java
----------------------------------------------------------------------
diff --git a/components/camel-cassandraql/src/main/java/org/apache/camel/processor/idempotent/cassandra/CassandraIdempotentRepository.java
b/components/camel-cassandraql/src/main/java/org/apache/camel/processor/idempotent/cassandra/CassandraIdempotentRepository.java
index 7546f17..d1eced7 100644
--- a/components/camel-cassandraql/src/main/java/org/apache/camel/processor/idempotent/cassandra/CassandraIdempotentRepository.java
+++ b/components/camel-cassandraql/src/main/java/org/apache/camel/processor/idempotent/cassandra/CassandraIdempotentRepository.java
@@ -25,6 +25,8 @@ import com.datastax.driver.core.Session;
 import com.datastax.driver.core.querybuilder.Delete;
 import com.datastax.driver.core.querybuilder.Insert;
 import com.datastax.driver.core.querybuilder.Select;
+import com.datastax.driver.core.querybuilder.Truncate;
+
 import org.apache.camel.spi.IdempotentRepository;
 import org.apache.camel.support.ServiceSupport;
 import org.apache.camel.utils.cassandra.CassandraSessionHolder;
@@ -36,6 +38,7 @@ import static org.apache.camel.utils.cassandra.CassandraUtils.applyConsistencyLe
 import static org.apache.camel.utils.cassandra.CassandraUtils.generateDelete;
 import static org.apache.camel.utils.cassandra.CassandraUtils.generateInsert;
 import static org.apache.camel.utils.cassandra.CassandraUtils.generateSelect;
+import static org.apache.camel.utils.cassandra.CassandraUtils.generateTruncate;
 
 /**
  * Implementation of {@link IdempotentRepository} using Cassandra table to store
@@ -82,6 +85,7 @@ public class CassandraIdempotentRepository<K> extends ServiceSupport
implements
     private PreparedStatement insertStatement;
     private PreparedStatement selectStatement;
     private PreparedStatement deleteStatement;
+    private PreparedStatement truncateStatement;
 
     public CassandraIdempotentRepository() {
     }
@@ -122,6 +126,7 @@ public class CassandraIdempotentRepository<K> extends ServiceSupport
implements
         initInsertStatement();
         initSelectStatement();
         initDeleteStatement();
+        initClearStatement();
     }
 
     @Override
@@ -183,6 +188,23 @@ public class CassandraIdempotentRepository<K> extends ServiceSupport
implements
         LOGGER.debug("Deleting key {}", (Object) idValues);
         return isApplied(getSession().execute(deleteStatement.bind(idValues)));
     }
+    
+    // -------------------------------------------------------------------------
+    // Clear the repository
+    
+    protected void initClearStatement() {
+        Truncate truncate = generateTruncate(table);
+        truncate = applyConsistencyLevel(truncate, writeConsistencyLevel);
+        LOGGER.debug("Generated truncate for clear operation {}", truncate);
+        truncateStatement = getSession().prepare(truncate);
+    }
+    
+    @Override
+    public void clear() {
+        LOGGER.debug("Clear table {}", table);
+        getSession().execute(truncateStatement.bind());        
+    }
+    
     // -------------------------------------------------------------------------
     // Getters & Setters
 

http://git-wip-us.apache.org/repos/asf/camel/blob/b346017f/components/camel-cassandraql/src/main/java/org/apache/camel/utils/cassandra/CassandraUtils.java
----------------------------------------------------------------------
diff --git a/components/camel-cassandraql/src/main/java/org/apache/camel/utils/cassandra/CassandraUtils.java
b/components/camel-cassandraql/src/main/java/org/apache/camel/utils/cassandra/CassandraUtils.java
index 32b41b3..1af07c7 100644
--- a/components/camel-cassandraql/src/main/java/org/apache/camel/utils/cassandra/CassandraUtils.java
+++ b/components/camel-cassandraql/src/main/java/org/apache/camel/utils/cassandra/CassandraUtils.java
@@ -20,7 +20,9 @@ import com.datastax.driver.core.ConsistencyLevel;
 import com.datastax.driver.core.RegularStatement;
 import com.datastax.driver.core.querybuilder.Delete;
 import com.datastax.driver.core.querybuilder.Insert;
+import com.datastax.driver.core.querybuilder.QueryBuilder;
 import com.datastax.driver.core.querybuilder.Select;
+import com.datastax.driver.core.querybuilder.Truncate;
 
 import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker;
 import static com.datastax.driver.core.querybuilder.QueryBuilder.delete;
@@ -160,6 +162,14 @@ public final class CassandraUtils {
     private static boolean isWhereClause(String[] whereColumns, int whereColumnsMaxIndex)
{
         return !isEmpty(whereColumns) && whereColumnsMaxIndex > 0;
     }
+    
+    /**
+     * Generate delete where columns = ? CQL.
+     */
+    public static Truncate generateTruncate(String table) {
+        Truncate truncate = QueryBuilder.truncate(table);
+        return truncate;
+    }
 
     /**
      * Apply consistency level if provided, else leave default.

http://git-wip-us.apache.org/repos/asf/camel/blob/b346017f/components/camel-cassandraql/src/test/java/org/apache/camel/processor/idempotent/cassandra/CassandraIdempotentRepositoryTest.java
----------------------------------------------------------------------
diff --git a/components/camel-cassandraql/src/test/java/org/apache/camel/processor/idempotent/cassandra/CassandraIdempotentRepositoryTest.java
b/components/camel-cassandraql/src/test/java/org/apache/camel/processor/idempotent/cassandra/CassandraIdempotentRepositoryTest.java
index d4b7b72..f6b8506 100644
--- a/components/camel-cassandraql/src/test/java/org/apache/camel/processor/idempotent/cassandra/CassandraIdempotentRepositoryTest.java
+++ b/components/camel-cassandraql/src/test/java/org/apache/camel/processor/idempotent/cassandra/CassandraIdempotentRepositoryTest.java
@@ -139,5 +139,16 @@ public class CassandraIdempotentRepositoryTest {
         // Then
         assertTrue(result);
     }
+    
+    @Test
+    public void testClear() {
+        // Given
+        String key = "Remove_Exists";
+        assertTrue(exists(key));
+        // When
+        idempotentRepository.clear();
+        // Then
+        assertFalse(idempotentRepository.contains(key));
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/b346017f/components/camel-cassandraql/src/test/java/org/apache/camel/processor/idempotent/cassandra/NamedCassandraIdempotentRepositoryTest.java
----------------------------------------------------------------------
diff --git a/components/camel-cassandraql/src/test/java/org/apache/camel/processor/idempotent/cassandra/NamedCassandraIdempotentRepositoryTest.java
b/components/camel-cassandraql/src/test/java/org/apache/camel/processor/idempotent/cassandra/NamedCassandraIdempotentRepositoryTest.java
index bdb577f..db3498f 100644
--- a/components/camel-cassandraql/src/test/java/org/apache/camel/processor/idempotent/cassandra/NamedCassandraIdempotentRepositoryTest.java
+++ b/components/camel-cassandraql/src/test/java/org/apache/camel/processor/idempotent/cassandra/NamedCassandraIdempotentRepositoryTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.processor.idempotent.cassandra;
 
 import com.datastax.driver.core.Cluster;
 import com.datastax.driver.core.Session;
+
 import org.apache.camel.component.cassandra.CassandraUnitUtils;
 import org.cassandraunit.CassandraCQLUnit;
 import org.junit.After;
@@ -140,5 +141,15 @@ public class NamedCassandraIdempotentRepositoryTest {
         // Then
         assertTrue(result);
     }
-
+    
+    @Test
+    public void testClear() {
+        // Given
+        String key = "Remove_Exists";
+        assertTrue(exists(key));
+        // When
+        idempotentRepository.clear();
+        // Then
+        assertFalse(idempotentRepository.contains(key));
+    }
 }


Mime
View raw message