DATACASS-403 - Polish.
Resolves gh-105
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<CacheKey, PreparedStatement> getCache() {
|
||||
return cache;
|
||||
protected Map<CacheKey, PreparedStatement> 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;
|
||||
|
||||
@@ -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<PreparedStatement> preparer);
|
||||
|
||||
/**
|
||||
* Create a default cache backed by a {@link java.util.concurrent.ConcurrentHashMap}.
|
||||
*
|
||||
* @return a new {@link MapPreparedStatementCache}.
|
||||
*/
|
||||
static PreparedStatementCache create() {
|
||||
return MapPreparedStatementCache.create();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user