From 4e9fd9ae0e7109fb188f0b8e26e49729908d3529 Mon Sep 17 00:00:00 2001 From: John Blum Date: Sun, 11 Jun 2017 22:20:44 -0700 Subject: [PATCH] DATACASS-403 - Polish. Resolves gh-105 --- .../CachedPreparedStatementCreator.java | 20 ++++++------- .../support/MapPreparedStatementCache.java | 14 +++++----- .../core/support/PreparedStatementCache.java | 28 +++++++++++++------ ...eparedStatementCreatorIntegrationTest.java | 5 ++-- 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cql/core/support/CachedPreparedStatementCreator.java b/spring-data-cassandra/src/main/java/org/springframework/data/cql/core/support/CachedPreparedStatementCreator.java index 1fd15d091..dd9639fdf 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cql/core/support/CachedPreparedStatementCreator.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cql/core/support/CachedPreparedStatementCreator.java @@ -63,7 +63,7 @@ public class CachedPreparedStatementCreator implements PreparedStatementCreator * CQL test for a cache hit. Otherwise, the statement is likely to be re-prepared. * * @param cache must not be {@literal null}. - * @param cql must not be {@literal null} or empty. + * @param statement must not be {@literal null}. * @return the {@link CachedPreparedStatementCreator} for {@link RegularStatement}. */ public static CachedPreparedStatementCreator of(PreparedStatementCache cache, RegularStatement statement) { @@ -86,7 +86,7 @@ public class CachedPreparedStatementCreator implements PreparedStatementCreator public static CachedPreparedStatementCreator of(PreparedStatementCache cache, String cql) { Assert.notNull(cache, "Cache must not be null"); - Assert.hasText(cql, "CQL statement must not be null"); + Assert.hasText(cql, "CQL statement is required"); return new CachedPreparedStatementCreator(cache, new SimpleStatement(cql)); } @@ -102,24 +102,22 @@ public class CachedPreparedStatementCreator implements PreparedStatementCreator * @param queryOptions must not be {@literal null}. * @return the {@link CachedPreparedStatementCreator} for {@code cql}. */ - public static CachedPreparedStatementCreator of(PreparedStatementCache cache, String cql, QueryOptions queryOptions) { + public static CachedPreparedStatementCreator of(PreparedStatementCache cache, String cql, + QueryOptions queryOptions) { Assert.notNull(cache, "Cache must not be null"); - Assert.hasText(cql, "CQL statement must not be null"); + Assert.hasText(cql, "CQL statement is required"); Assert.notNull(queryOptions, "QueryOptions must not be null"); - SimpleStatement statement = new SimpleStatement(cql); - - QueryOptionsUtil.addQueryOptions(statement, queryOptions); - - return new CachedPreparedStatementCreator(cache, statement); + return new CachedPreparedStatementCreator(cache, + QueryOptionsUtil.addQueryOptions(new SimpleStatement(cql), queryOptions)); } /** * @return the underlying {@link PreparedStatementCache}. */ public PreparedStatementCache getCache() { - return cache; + return this.cache; } /* (non-Javadoc) @@ -127,6 +125,6 @@ public class CachedPreparedStatementCreator implements PreparedStatementCreator */ @Override public PreparedStatement createPreparedStatement(Session session) throws DriverException { - return cache.getPreparedStatement(session, statement, () -> session.prepare(statement)); + return getCache().getPreparedStatement(session, this.statement); } } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cql/core/support/MapPreparedStatementCache.java b/spring-data-cassandra/src/main/java/org/springframework/data/cql/core/support/MapPreparedStatementCache.java index 275a13da5..9f5b12c82 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cql/core/support/MapPreparedStatementCache.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cql/core/support/MapPreparedStatementCache.java @@ -15,12 +15,12 @@ */ package org.springframework.data.cql.core.support; -import lombok.EqualsAndHashCode; - import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Supplier; +import lombok.EqualsAndHashCode; + import org.springframework.util.Assert; import com.datastax.driver.core.Cluster; @@ -74,8 +74,8 @@ public class MapPreparedStatementCache implements PreparedStatementCache { /** * @return the underlying {@link Map cache}. */ - public Map getCache() { - return cache; + protected Map getCache() { + return this.cache; } /* (non-Javadoc) @@ -87,14 +87,14 @@ public class MapPreparedStatementCache implements PreparedStatementCache { CacheKey cacheKey = new CacheKey(session, statement.toString()); - return cache.computeIfAbsent(cacheKey, key -> session.prepare(statement)); + return getCache().computeIfAbsent(cacheKey, key -> preparer.get()); } /** - * Cache key for {@link PreparedStatement} caching. + * {@link CacheKey} for {@link PreparedStatement} caching. */ @EqualsAndHashCode - public static class CacheKey { + protected static class CacheKey { final Cluster cluster; final String keyspace; diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cql/core/support/PreparedStatementCache.java b/spring-data-cassandra/src/main/java/org/springframework/data/cql/core/support/PreparedStatementCache.java index 0f509b3b6..4dbd476e8 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cql/core/support/PreparedStatementCache.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cql/core/support/PreparedStatementCache.java @@ -34,6 +34,26 @@ import com.datastax.driver.core.Session; */ public interface PreparedStatementCache { + /** + * Create a default cache backed by a {@link java.util.concurrent.ConcurrentHashMap}. + * + * @return a new {@link MapPreparedStatementCache}. + */ + static PreparedStatementCache create() { + return MapPreparedStatementCache.create(); + } + + /** + * Obtain a {@link PreparedStatement} by {@link Session} and {@link RegularStatement}. + * + * @param session must not be {@literal null}. + * @param statement must not be {@literal null}. + * @return the {@link PreparedStatement}. + */ + default PreparedStatement getPreparedStatement(Session session, RegularStatement statement) { + return getPreparedStatement(session, statement, () -> session.prepare(statement)); + } + /** * Obtain a {@link PreparedStatement} by {@link Session} and {@link RegularStatement}. * @@ -45,12 +65,4 @@ public interface PreparedStatementCache { PreparedStatement getPreparedStatement(Session session, RegularStatement statement, Supplier preparer); - /** - * Create a default cache backed by a {@link java.util.concurrent.ConcurrentHashMap}. - * - * @return a new {@link MapPreparedStatementCache}. - */ - static PreparedStatementCache create() { - return MapPreparedStatementCache.create(); - } } diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cql/core/support/CachedPreparedStatementCreatorIntegrationTest.java b/spring-data-cassandra/src/test/java/org/springframework/data/cql/core/support/CachedPreparedStatementCreatorIntegrationTest.java index b7fd54849..bacb6caee 100644 --- a/spring-data-cassandra/src/test/java/org/springframework/data/cql/core/support/CachedPreparedStatementCreatorIntegrationTest.java +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cql/core/support/CachedPreparedStatementCreatorIntegrationTest.java @@ -55,8 +55,9 @@ public class CachedPreparedStatementCreatorIntegrationTest extends AbstractKeysp assertThat(insert.isIdempotent()).isTrue(); PreparedStatementCache cache = PreparedStatementCache.create(); - PreparedStatement preparedStatement = CachedPreparedStatementCreator.of(cache, insert) - .createPreparedStatement(session); + + PreparedStatement preparedStatement = + CachedPreparedStatementCreator.of(cache, insert).createPreparedStatement(session); assertThat(preparedStatement.isIdempotent()).isTrue(); }