Skip to content

Commit

Permalink
refactor: Changed ruleset manager into rulesession manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Ruaux committed Jun 5, 2023
1 parent 9261bf0 commit 48dbf05
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -18,6 +20,8 @@

public class QueryRuleSession extends RuleSession<Query, Action> implements PropertyChangeListener {

private static final Logger log = Logger.getLogger(QueryRuleSession.class.getName());

public QueryRuleSession() {
super();
}
Expand All @@ -37,12 +41,13 @@ private static List<Rule<Query, Action>> rules(RulesetConfig ruleset) {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (RulesetConfig.PROPERTY_RULES.equals(evt.getPropertyName())) {
updateRules(Arrays.asList((RuleConfig[]) evt.getNewValue()));
updateRules((RuleConfig[]) evt.getNewValue());
}
}

public void updateRules(List<RuleConfig> ruleConfigs) {
setRules(ruleConfigs.stream().map(QueryRuleSession::rule).collect(Collectors.toList()));
public void updateRules(RuleConfig[] ruleConfigs) {
setRules(Stream.of(ruleConfigs).map(QueryRuleSession::rule).collect(Collectors.toList()));
log.log(Level.INFO, "Updated rules: {0}", Arrays.toString(ruleConfigs));
}

public Action fire(Query query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@

import io.lettuce.core.AbstractRedisClient;

public class RulesetManager implements AutoCloseable {
public class RuleSessionManager implements AutoCloseable {

public static final String KEY_CONFIG = "config";

private final JavaPropsMapper mapper = Mappers.propsMapper();
private final Map<Config, ConfigManager<RulesetConfig>> configManagers = new HashMap<>();
private final Map<Config, QueryRuleSession> ruleSessions = new HashMap<>();
private final ClientManager clientManager;

public RulesetManager(ClientManager clientManager) {
public RuleSessionManager(ClientManager clientManager) {
this.clientManager = clientManager;
}

public RulesetConfig getRuleset(Config config) {
return configManagers.computeIfAbsent(config, this::createConfigManager).get();
public QueryRuleSession getRuleSession(Config config) {
return ruleSessions.computeIfAbsent(config, this::createRuleSession);
}

private ConfigManager<RulesetConfig> createConfigManager(Config config) {
Expand All @@ -45,4 +46,12 @@ public void close() throws Exception {
configManagers.clear();
}

private QueryRuleSession createRuleSession(Config config) {
ConfigManager<RulesetConfig> configManager = configManagers.computeIfAbsent(config, this::createConfigManager);
RulesetConfig ruleset = configManager.get();
QueryRuleSession session = QueryRuleSession.of(ruleset);
ruleset.addPropertyChangeListener(session);
return session;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
import com.redis.smartcache.core.ClientManager;
import com.redis.smartcache.core.Config;
import com.redis.smartcache.core.Config.DriverConfig;
import com.redis.smartcache.core.RulesetConfig;
import com.redis.smartcache.core.EvictingLinkedHashMap;
import com.redis.smartcache.core.HashingFunctions;
import com.redis.smartcache.core.KeyBuilder;
import com.redis.smartcache.core.Mappers;
import com.redis.smartcache.core.MeterRegistryManager;
import com.redis.smartcache.core.RulesetManager;
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.RowSetCodec;
Expand Down Expand Up @@ -68,7 +67,7 @@ public class Driver implements java.sql.Driver {
private static final RowSetFactory ROW_SET_FACTORY = new CachedRowSetFactory();

private static final ClientManager clientManager = new ClientManager();
private static final RulesetManager rulesetManager = new RulesetManager(clientManager);
private static final RuleSessionManager ruleSessionManager = new RuleSessionManager(clientManager);
private static final MeterRegistryManager registryManager = new MeterRegistryManager(clientManager);
private static final Map<Config, Map<String, Query>> queryCaches = new HashMap<>();

Expand Down Expand Up @@ -123,13 +122,11 @@ public static Config config(Properties info) throws IOException {
}

private SmartConnection makeConnection(Config config, Connection backendConnection) {
RulesetConfig ruleset = rulesetManager.getRuleset(config);
QueryRuleSession session = QueryRuleSession.of(ruleset);
ruleset.addPropertyChangeListener(session);
QueryRuleSession ruleSession = 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, session, registry, ROW_SET_FACTORY, rowSetCache(config),
return new SmartConnection(backendConnection, ruleSession, registry, ROW_SET_FACTORY, rowSetCache(config),
queryCache, keyBuilder);
}

Expand Down Expand Up @@ -247,7 +244,7 @@ public static void deregister() throws SQLException {
}

public static void clear() throws Exception {
rulesetManager.close();
ruleSessionManager.close();
registryManager.close();
queryCaches.clear();
clientManager.close();
Expand Down

0 comments on commit 48dbf05

Please sign in to comment.