DATACASS-253 - Polish.

Original pull request: #62.
This commit is contained in:
John Blum
2016-06-03 12:22:14 -07:00
committed by Mark Paluch
parent 4667865c5a
commit fe30102bcd
2 changed files with 27 additions and 23 deletions

View File

@@ -39,15 +39,16 @@ import com.datastax.driver.core.exceptions.DriverException;
*/ */
public class CachedPreparedStatementCreator implements PreparedStatementCreator { public class CachedPreparedStatementCreator implements PreparedStatementCreator {
private static final Logger log = LoggerFactory.getLogger(CachedPreparedStatementCreator.class);
private static final Map<Session, Map<String, PreparedStatement>> CACHE = new ConcurrentHashMap<Session, Map<String, PreparedStatement>>(); private static final Map<Session, Map<String, PreparedStatement>> CACHE = new ConcurrentHashMap<Session, Map<String, PreparedStatement>>();
protected final Logger log = LoggerFactory.getLogger(getClass());
private final String cql; private final String cql;
/** /**
* Create a {@link PreparedStatementCreator} from the provided CQL. * Create a {@link PreparedStatementCreator} from the provided CQL.
* *
* @param cql must not be empty and not {@literal null}. * @param cql must not be empty or {@literal null}.
*/ */
public CachedPreparedStatementCreator(String cql) { public CachedPreparedStatementCreator(String cql) {
@@ -56,6 +57,11 @@ public class CachedPreparedStatementCreator implements PreparedStatementCreator
this.cql = cql; this.cql = cql;
} }
/**
* Returns the CQL statement on which the {@link PreparedStatement} will be based.
*
* @return a String containing the CQL of the {@link PreparedStatement}.
*/
public String getCql() { public String getCql() {
return this.cql; return this.cql;
} }
@@ -66,14 +72,16 @@ public class CachedPreparedStatementCreator implements PreparedStatementCreator
@Override @Override
public PreparedStatement createPreparedStatement(Session session) throws DriverException { public PreparedStatement createPreparedStatement(Session session) throws DriverException {
StringBuilder cacheKey = new StringBuilder().append(session.getLoggedKeyspace()).append("|").append(this.cql); String cacheKey = String.valueOf(session.getLoggedKeyspace()).concat("|").concat(this.cql);
log.debug("Cachable PreparedStatement in Keyspace {}", session.getLoggedKeyspace()); log.debug("Cacheable PreparedStatement in Keyspace {}", session.getLoggedKeyspace());
Map<String, PreparedStatement> sessionCache = getOrCreateSessionLocalCache(session); Map<String, PreparedStatement> sessionCache = getOrCreateSessionLocalCache(session);
return getOrPrepareStatement(session, cacheKey.toString(), sessionCache);
return getOrPrepareStatement(session, cacheKey, sessionCache);
} }
@SuppressWarnings("all")
private Map<String, PreparedStatement> getOrCreateSessionLocalCache(Session session) { private Map<String, PreparedStatement> getOrCreateSessionLocalCache(Session session) {
Map<String, PreparedStatement> sessionMap = CACHE.get(session); Map<String, PreparedStatement> sessionMap = CACHE.get(session);
@@ -85,7 +93,6 @@ public class CachedPreparedStatementCreator implements PreparedStatementCreator
if (CACHE.containsKey(session)) { if (CACHE.containsKey(session)) {
sessionMap = CACHE.get(session); sessionMap = CACHE.get(session);
} else { } else {
sessionMap = new ConcurrentHashMap<String, PreparedStatement>(); sessionMap = new ConcurrentHashMap<String, PreparedStatement>();
CACHE.put(session, sessionMap); CACHE.put(session, sessionMap);
} }
@@ -95,32 +102,28 @@ public class CachedPreparedStatementCreator implements PreparedStatementCreator
return sessionMap; return sessionMap;
} }
@SuppressWarnings("all")
private PreparedStatement getOrPrepareStatement(Session session, String cacheKey, private PreparedStatement getOrPrepareStatement(Session session, String cacheKey,
Map<String, PreparedStatement> sessionCache) { Map<String, PreparedStatement> sessionCache) {
PreparedStatement pstmt = sessionCache.get(cacheKey); PreparedStatement preparedStatement = sessionCache.get(cacheKey);
if (pstmt == null) { if (preparedStatement == null) {
synchronized (sessionCache) { synchronized (sessionCache) {
if (sessionCache.containsKey(cacheKey)) { if (sessionCache.containsKey(cacheKey)) {
log.debug("Found cached PreparedStatement"); log.debug("Found cached PreparedStatement");
return sessionCache.get(cacheKey); preparedStatement = sessionCache.get(cacheKey);
}
else {
log.debug("No cached PreparedStatement found... creating and caching");
preparedStatement = session.prepare(this.cql);
sessionCache.put(cacheKey, preparedStatement);
} }
log.debug("No Cached PreparedStatement found...Creating and Caching");
pstmt = session.prepare(this.cql);
sessionCache.put(cacheKey, pstmt);
return pstmt;
} }
} }
log.debug("Found cached PreparedStatement"); return preparedStatement;
return pstmt;
} }
} }

View File

@@ -41,6 +41,7 @@ import edu.umd.cs.mtc.MultithreadedTestCase;
* Unit tests for {@link CachedPreparedStatementCreator}. * Unit tests for {@link CachedPreparedStatementCreator}.
* *
* @author Mark Paluch * @author Mark Paluch
* @see DATACASS-253
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class CachedPreparedStatementCreatorUnitTests { public class CachedPreparedStatementCreatorUnitTests {
@@ -95,6 +96,7 @@ public class CachedPreparedStatementCreatorUnitTests {
cachedPreparedStatementCreator.createPreparedStatement(sessionMock); cachedPreparedStatementCreator.createPreparedStatement(sessionMock);
cachedPreparedStatementCreator.createPreparedStatement(sessionMock); cachedPreparedStatementCreator.createPreparedStatement(sessionMock);
PreparedStatement result = cachedPreparedStatementCreator.createPreparedStatement(sessionMock); PreparedStatement result = cachedPreparedStatementCreator.createPreparedStatement(sessionMock);
assertThat(result, is(sameInstance(preparedStatement))); assertThat(result, is(sameInstance(preparedStatement)));
@@ -117,9 +119,9 @@ public class CachedPreparedStatementCreatorUnitTests {
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static class CreatePreparedStatementIsThreadSafe extends MultithreadedTestCase { private static class CreatePreparedStatementIsThreadSafe extends MultithreadedTestCase {
final AtomicInteger atomicInteger = new AtomicInteger();
final CachedPreparedStatementCreator preparedStatementCreator; final CachedPreparedStatementCreator preparedStatementCreator;
final Session session; final Session session;
final AtomicInteger atomicInteger = new AtomicInteger();
public CreatePreparedStatementIsThreadSafe(final PreparedStatement preparedStatement, public CreatePreparedStatementIsThreadSafe(final PreparedStatement preparedStatement,
CachedPreparedStatementCreator preparedStatementCreator) { CachedPreparedStatementCreator preparedStatementCreator) {
@@ -132,7 +134,6 @@ public class CachedPreparedStatementCreatorUnitTests {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("prepare") && args.length == 1) { if (method.getName().equals("prepare") && args.length == 1) {
waitForTick(2); waitForTick(2);
atomicInteger.incrementAndGet(); atomicInteger.incrementAndGet();
return preparedStatement; return preparedStatement;
@@ -150,7 +151,6 @@ public class CachedPreparedStatementCreatorUnitTests {
preparedStatementCreator.createPreparedStatement(session); preparedStatementCreator.createPreparedStatement(session);
assertThat(atomicInteger.get(), is(1)); assertThat(atomicInteger.get(), is(1));
} }
public void thread2() { public void thread2() {
@@ -180,6 +180,7 @@ public class CachedPreparedStatementCreatorUnitTests {
} }
} }
@SuppressWarnings("unchecked")
private static <T> T newProxy(Class<T> theClass, InvocationHandler invocationHandler) { private static <T> T newProxy(Class<T> theClass, InvocationHandler invocationHandler) {
return (T) Proxy.newProxyInstance(CachedPreparedStatementCreatorUnitTests.class.getClassLoader(), return (T) Proxy.newProxyInstance(CachedPreparedStatementCreatorUnitTests.class.getClassLoader(),
new Class[] { theClass }, invocationHandler); new Class[] { theClass }, invocationHandler);