Skip to content

Commit

Permalink
refactor: Removed RowSetFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Ruaux committed Jun 6, 2023
1 parent e3c28ca commit 1c2a8c8
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import com.redis.smartcache.jdbc.RowSetCodec;
import com.redis.smartcache.jdbc.codec.SerializedResultSetCodec;
import com.redis.smartcache.jdbc.rowset.CachedRowSetFactory;
import com.redis.smartcache.test.RowSetBuilder;

@State(Scope.Benchmark)
Expand All @@ -34,13 +33,13 @@ public class CodecExecutionPlan {

@Setup(Level.Trial)
public void setUpTrial() {
this.codec = new RowSetCodec(new CachedRowSetFactory(), BYTE_BUFFER_CAPACITY);
this.codec = new RowSetCodec(BYTE_BUFFER_CAPACITY);
this.serializedCodec = new SerializedResultSetCodec(BYTE_BUFFER_CAPACITY);
}

@Setup(Level.Invocation)
public void setUpInvocation() throws SQLException {
RowSetBuilder rowSetBuilder = RowSetBuilder.of(new CachedRowSetFactory()).rowCount(rows).columnCount(columns);
RowSetBuilder rowSetBuilder = RowSetBuilder.of(new RowSetFactoryImpl()).rowCount(rows).columnCount(columns);
this.rowSet = rowSetBuilder.build();
rowSet.beforeFirst();
this.byteBuffer = codec.encodeValue(rowSet);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.redis.smartcache.jdbc.rowset;
package com.redis.smartcache;

import java.sql.SQLException;

Expand All @@ -9,7 +9,9 @@
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.WebRowSet;

public class CachedRowSetFactory implements RowSetFactory {
import com.redis.smartcache.jdbc.rowset.CachedRowSetImpl;

public class RowSetFactoryImpl implements RowSetFactory {

private static final String NOT_IMPLEMENTED = "Not implemented";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.regex.Pattern;

import javax.sql.RowSet;
import javax.sql.rowset.RowSetFactory;

import com.redis.smartcache.core.ClientManager;
import com.redis.smartcache.core.Config;
Expand All @@ -28,11 +27,10 @@
import com.redis.smartcache.core.Query;
import com.redis.smartcache.core.QueryRuleSession;
import com.redis.smartcache.core.RuleSessionManager;
import com.redis.smartcache.jdbc.RedisResultSetCache;
import com.redis.smartcache.jdbc.ResultSetCache;
import com.redis.smartcache.jdbc.RedisRowSetCache;
import com.redis.smartcache.jdbc.RowSetCache;
import com.redis.smartcache.jdbc.RowSetCodec;
import com.redis.smartcache.jdbc.SmartConnection;
import com.redis.smartcache.jdbc.rowset.CachedRowSetFactory;

import io.lettuce.core.AbstractRedisClient;
import io.lettuce.core.codec.RedisCodec;
Expand Down Expand Up @@ -64,7 +62,6 @@ public class Driver implements java.sql.Driver {
public static final String KEYSPACE_CACHE = "cache";
private static final String JDBC_URL_REGEX = "jdbc\\:(rediss?(\\-(socket|sentinel))?\\:\\/\\/.*)";
private static final Pattern JDBC_URL_PATTERN = Pattern.compile(JDBC_URL_REGEX);
private static final RowSetFactory ROW_SET_FACTORY = new CachedRowSetFactory();

private static final ClientManager clientManager = new ClientManager();
private static final RuleSessionManager ruleSessionManager = new RuleSessionManager(clientManager);
Expand Down Expand Up @@ -122,18 +119,17 @@ public static Config config(Properties info) throws IOException {
}

private SmartConnection makeConnection(Config config, Connection backendConnection) {
QueryRuleSession ruleSession = ruleSessionManager.getRuleSession(config);
QueryRuleSession session = ruleSessionManager.getRuleSession(config);
KeyBuilder keyBuilder = KeyBuilder.of(config).sub(KEYSPACE_CACHE);
MeterRegistry registry = registryManager.getRegistry(config);
Map<String, Query> queryCache = queryCaches.computeIfAbsent(config, this::createQueryCache);
return new SmartConnection(backendConnection, ruleSession, registry, ROW_SET_FACTORY, rowSetCache(config),
queryCache, keyBuilder);
return new SmartConnection(backendConnection, session, registry, rowSetCache(config), queryCache, keyBuilder);
}

private ResultSetCache rowSetCache(Config config) {
private RowSetCache rowSetCache(Config config) {
AbstractRedisClient client = clientManager.getClient(config);
RedisCodec<String, RowSet> codec = resultSetCodec(config);
return new RedisResultSetCache(ROW_SET_FACTORY, client, codec);
return new RedisRowSetCache(client, codec);
}

private Map<String, Query> createQueryCache(Config config) {
Expand All @@ -142,7 +138,7 @@ private Map<String, Query> createQueryCache(Config config) {

private static RedisCodec<String, RowSet> resultSetCodec(Config config) {
int bufferSize = Math.toIntExact(config.getRedis().getCodecBufferCapacity().toBytes());
return new RowSetCodec(ROW_SET_FACTORY, bufferSize);
return new RowSetCodec(bufferSize);
}

private Connection backendConnection(DriverConfig config, Properties info) throws SQLException {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.redis.smartcache.jdbc;

import java.sql.SQLException;
import java.time.Duration;

import javax.sql.RowSet;

import com.redis.lettucemod.util.RedisModulesUtils;

import io.lettuce.core.AbstractRedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.codec.RedisCodec;

public class RedisRowSetCache implements RowSetCache {

private final StatefulRedisConnection<String, RowSet> connection;

public RedisRowSetCache(AbstractRedisClient client, RedisCodec<String, RowSet> codec) {
this.connection = RedisModulesUtils.connection(client, codec);
}

@Override
public RowSet get(String key) {
return connection.sync().get(key);
}

@Override
public void put(String key, RowSet rowSet) throws SQLException {
connection.sync().set(key, rowSet);
}

@Override
public void put(String key, RowSet rowSet, Duration ttl) throws SQLException {
connection.sync().psetex(key, ttl.toMillis(), rowSet);
}

@Override
public void close() {
connection.close();
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package com.redis.smartcache.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Duration;

public interface ResultSetCache extends AutoCloseable {
import javax.sql.RowSet;

public interface RowSetCache extends AutoCloseable {

/**
*
* @param key the unique key to get the ResultSet for.
* @return RowSet that was retrieved from cache or null if none
*/
ResultSet get(String key);
RowSet get(String key);

void put(String key, RowSet rowSet) throws SQLException;

ResultSet put(String key, Duration ttl, ResultSet resultSet) throws SQLException;
void put(String key, RowSet rowSet, Duration ttl) throws SQLException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import javax.sql.RowSet;
import javax.sql.RowSetMetaData;
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetMetaDataImpl;

import com.redis.smartcache.jdbc.codec.BigDecimalColumnCodec;
Expand All @@ -26,6 +25,7 @@
import com.redis.smartcache.jdbc.codec.StringColumnCodec;
import com.redis.smartcache.jdbc.codec.TimeColumnCodec;
import com.redis.smartcache.jdbc.codec.TimestampColumnCodec;
import com.redis.smartcache.jdbc.rowset.CachedRowSetImpl;

import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.codec.StringCodec;
Expand All @@ -38,11 +38,9 @@ public class RowSetCodec implements RedisCodec<String, RowSet> {
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
public static final String EMPTY_STRING = "";

private final RowSetFactory rowSetFactory;
private final int maxByteBufferCapacity;

public RowSetCodec(RowSetFactory rowSetFactory, int maxByteBufferCapacity) {
this.rowSetFactory = rowSetFactory;
public RowSetCodec(int maxByteBufferCapacity) {
this.maxByteBufferCapacity = maxByteBufferCapacity;
}

Expand All @@ -66,7 +64,7 @@ public RowSet decodeValue(ByteBuffer bytes) {
}

public CachedRowSet decodeRowSet(ByteBuf byteBuf) throws SQLException {
CachedRowSet rowSet = rowSetFactory.createCachedRowSet();
CachedRowSet rowSet = new CachedRowSetImpl();
RowSetMetaData metaData = decodeMetaData(byteBuf);
rowSet.setMetaData(metaData);
int columnCount = metaData.getColumnCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@
import java.util.function.UnaryOperator;
import java.util.logging.Logger;

import javax.sql.rowset.RowSetFactory;

import com.redis.smartcache.Driver;
import com.redis.smartcache.core.KeyBuilder;

import com.redis.smartcache.core.Query;
import com.redis.smartcache.core.QueryRuleSession;

Expand All @@ -38,30 +35,23 @@ public class SmartConnection implements Connection {

private final UnaryOperator<String> hashFunction = Driver::crc32;
private final SQLParser parser = new SQLParser();
private final RowSetFactory rowSetFactory;
private final Connection connection;
private final ResultSetCache rowSetCache;
private final RowSetCache rowSetCache;
private final MeterRegistry meterRegistry;
private final QueryRuleSession session;
private final KeyBuilder keyBuilder;
private final Map<String, Query> queryCache;

public SmartConnection(Connection connection, QueryRuleSession session, MeterRegistry meterRegistry,
RowSetFactory rowSetFactory, ResultSetCache rowSetCache, Map<String, Query> queryCache,
KeyBuilder keyBuilder) {
RowSetCache rowSetCache, Map<String, Query> queryCache, KeyBuilder keyBuilder) {
this.connection = connection;
this.session = session;
this.meterRegistry = meterRegistry;
this.rowSetFactory = rowSetFactory;
this.rowSetCache = rowSetCache;
this.queryCache = queryCache;
this.keyBuilder = keyBuilder;
}

public RowSetFactory getRowSetFactory() {
return rowSetFactory;
}

public KeyBuilder getKeyBuilder() {
return keyBuilder;
}
Expand All @@ -74,7 +64,7 @@ public QueryRuleSession getRuleSession() {
return session;
}

public ResultSetCache getRowSetCache() {
public RowSetCache getRowSetCache() {
return rowSetCache;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

import javax.sql.rowset.CachedRowSet;

import com.redis.smartcache.core.Action;
import com.redis.smartcache.core.Fields;

import com.redis.smartcache.core.Query;
import com.redis.smartcache.jdbc.rowset.CachedRowSetImpl;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer;
Expand Down Expand Up @@ -122,14 +125,14 @@ public ResultSet executeQuery(String sql) throws SQLException {

protected ResultSet executeQuery(Callable<ResultSet> callable) throws SQLException {
return time(Fields.METER_QUERY, () -> {
populateFromCache();
getFromCache();
return getResultSet(() -> executeBackend(callable));
});
}

protected boolean execute(Callable<Boolean> callable) throws SQLException {
return time(Fields.METER_QUERY, () -> {
populateFromCache();
getFromCache();
if (hasResultSet()) {
return true;
}
Expand All @@ -143,11 +146,20 @@ private ResultSet getResultSet(Callable<ResultSet> callable) throws SQLException
}
resultSet = time(METER_BACKEND_RESULTSET, callable);
if (isCaching()) {
resultSet = time(METER_CACHE_PUT, () -> connection.getRowSetCache().put(key(), action.getTtl(), resultSet));
resultSet = time(METER_CACHE_PUT, () -> put(resultSet));
}
return resultSet;
}

private CachedRowSet put(ResultSet resultSet) throws SQLException {
CachedRowSet cached = new CachedRowSetImpl();
cached.populate(resultSet);
cached.beforeFirst();
connection.getRowSetCache().put(key(), cached, action.getTtl());
cached.beforeFirst();
return cached;
}

private <T> T executeBackend(Callable<T> callable) throws Exception {
return getMeter(METER_BACKEND).timer().recordCallable(callable);
}
Expand All @@ -158,7 +170,7 @@ private <T> T executeBackend(Callable<T> callable) throws Exception {
* @return cached result-set for the given query
* @throws SQLException
*/
private void populateFromCache() throws SQLException {
private void getFromCache() throws SQLException {
if (!isCaching()) {
return;
}
Expand Down
Loading

0 comments on commit 1c2a8c8

Please sign in to comment.