DATACASS-814 - Use CQL in MapPreparedStatementCache.CacheKey instead representation of Object.toString().

We now correctly use the CQL text to cache prepared statements. Previously, we used toString() which was a leftover from the driver migration that now defaults to Object.toString() and so the cache key was not stable.

Original pull request: #180.
This commit is contained in:
Aldo Bongio
2020-10-19 09:52:12 +02:00
committed by Mark Paluch
parent fe2b56f9ec
commit 3b058156d4
2 changed files with 16 additions and 1 deletions

View File

@@ -34,6 +34,7 @@ import com.datastax.oss.driver.api.core.cql.SimpleStatement;
* {@code cql} text. Statement options (idempotency, timeouts) apply from the statement that was initially prepared.
*
* @author Mark Paluch
* @author Aldo Bongio
* @since 2.0
*/
public class MapPreparedStatementCache implements PreparedStatementCache {
@@ -84,7 +85,7 @@ public class MapPreparedStatementCache implements PreparedStatementCache {
public PreparedStatement getPreparedStatement(CqlSession session, SimpleStatement statement,
Supplier<PreparedStatement> preparer) {
CacheKey cacheKey = new CacheKey(session, statement.toString());
CacheKey cacheKey = new CacheKey(session, statement.getQuery());
return getCache().computeIfAbsent(cacheKey, key -> preparer.get());
}

View File

@@ -39,6 +39,7 @@ import com.datastax.oss.driver.api.querybuilder.update.Assignment;
* Unit tests for {@link CachedPreparedStatementCreator}.
*
* @author Mark Paluch
* @author Aldo Bongio
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@@ -182,4 +183,17 @@ class CachedPreparedStatementCreatorUnitTests {
verify(session).prepare(firstStatement);
verify(session).prepare(secondStatement);
}
@Test // DATACASS-814
void shouldUseCqlTextInCacheKey() {
String cql = "SELECT foo FROM users;";
MapPreparedStatementCache cache = MapPreparedStatementCache.create();
CachedPreparedStatementCreator creator = CachedPreparedStatementCreator.of(cache, cql);
creator.createPreparedStatement(session);
MapPreparedStatementCache.CacheKey cacheKey = cache.getCache().keySet().iterator().next();
assertThat(cacheKey.cql).isSameAs(cql);
}
}