Skip to content

Commit

Permalink
feat: Added query ID matching rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Ruaux committed Mar 3, 2023
1 parent 944f36e commit d2ec161
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 49 deletions.
33 changes: 33 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ Here is the default rule configuration:
"tablesAny": null,
"tablesAll": null,
"regex": null,
"queryIds": null,
"ttl": "1h"
}
]
Expand Down Expand Up @@ -358,6 +359,16 @@ Triggers if any of the given tables shows up in the SQL query.

Triggers if all the given tables show up in the SQL query.

===== `queryIds`

* Type: <<property_type_list>>
* Example: `a3bb9911,ffff0000`

Triggers if the SQL query ID matches any of the given IDs.

TIP: An ID is the CRC32 hash of the SQL query. You can use an online CRC32 calculator like https://randommer.io/Hash/CRC32[this one] to compute the ID.


===== `regex`

* Type: <<property_type_string>>
Expand All @@ -384,48 +395,70 @@ Use `0s` to disable caching.
[cols="6a,^1",options="header"]
|==========================
|Criteria|Match

|
[source,json]
----
{ "tables": ["orders", "products"] }
----
|image:cross.svg[Check,20]

|
[source,json]
----
{ "tables": ["orders", "products", "customers"] }
----
|image:check.svg[Check,20]

|
[source,json]
----
{ "tablesAny": ["transactions"] }
----
|image:cross.svg[Check,20]

|
[source,json]
----
{ "tablesAny": ["transactions", "orders"] }
----
|image:check.svg[Check,20]

|
[source,json]
----
{ "tablesAll": ["transactions", "orders", "products"] }
----
|image:cross.svg[Check,20]

|
[source,json]
----
{ "tablesAll": ["orders", "products"] }
----
|image:check.svg[Check,20]

|
[source,json]
----
{ "queryIds": ["aaff2b5c", "56789"] }
----
|image:cross.svg[Check,20]

|
[source,json]
----
{ "queryIds": ["a3bb9911", "abcd1233"] }
----
|image:check.svg[Check,20]

|
[source,json]
----
{ "regex": "SELECT .+ FROM trans.*" }
----
|image:cross.svg[Check,20]

|
[source,json]
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ public static class RulesetConfig {
public static final Duration DEFAULT_REFRESH_STEP = new Duration(10, TimeUnit.SECONDS);

private Duration refresh = DEFAULT_REFRESH_STEP;
private final PropertyChangeSupport support = new PropertyChangeSupport(this);
private List<RuleConfig> rules;

/**
*
Expand All @@ -345,8 +347,6 @@ public void setRefresh(Duration seconds) {
this.refresh = seconds;
}

private List<RuleConfig> rules;

public RulesetConfig() {
this(RuleConfig.passthrough().build());
}
Expand All @@ -355,8 +355,6 @@ public RulesetConfig(RuleConfig... rules) {
this.rules = Arrays.asList(rules);
}

private final PropertyChangeSupport support = new PropertyChangeSupport(this);

public void addPropertyChangeListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
rules.forEach(r -> r.addPropertyChangeListener(listener));
Expand Down Expand Up @@ -388,10 +386,11 @@ public static class RuleConfig {

private final PropertyChangeSupport support = new PropertyChangeSupport(this);

private String[] tables;
private String[] tablesAny;
private String[] tablesAll;
private List<String> tables;
private List<String> tablesAny;
private List<String> tablesAll;
private String regex;
private List<String> queryIds;
private Duration ttl = DEFAULT_TTL;

public RuleConfig() {
Expand All @@ -402,6 +401,7 @@ private RuleConfig(Builder builder) {
this.tablesAny = builder.tablesAny;
this.tablesAll = builder.tablesAll;
this.regex = builder.regex;
this.queryIds = builder.queryIds;
this.ttl = builder.ttl;
}

Expand All @@ -413,6 +413,15 @@ public void removePropertyChangeListener(PropertyChangeListener listener) {
support.removePropertyChangeListener(listener);
}

public List<String> getQueryIds() {
return queryIds;
}

public void setQueryIds(List<String> queryIds) {
support.firePropertyChange("queryIds", this.queryIds, queryIds);
this.queryIds = queryIds;
}

public String getRegex() {
return regex;
}
Expand All @@ -422,29 +431,29 @@ public void setRegex(String regex) {
this.regex = regex;
}

public String[] getTables() {
public List<String> getTables() {
return tables;
}

public void setTables(String... tables) {
public void setTables(List<String> tables) {
support.firePropertyChange("tables", this.tables, tables);
this.tables = tables;
}

public String[] getTablesAny() {
public List<String> getTablesAny() {
return tablesAny;
}

public void setTablesAny(String... tablesAny) {
public void setTablesAny(List<String> tablesAny) {
support.firePropertyChange("tablesAny", this.tablesAny, tablesAny);
this.tablesAny = tablesAny;
}

public String[] getTablesAll() {
public List<String> getTablesAll() {
return tablesAll;
}

public void setTablesAll(String... tablesAll) {
public void setTablesAll(List<String> tablesAll) {
support.firePropertyChange("tablesAll", this.tablesAll, tablesAll);
this.tablesAll = tablesAll;
}
Expand All @@ -465,24 +474,30 @@ public void setTtl(Duration ttl) {
@Override
public String toString() {
return "RuleConfig [tables=" + tables + ", tablesAny=" + tablesAny + ", tablesAll=" + tablesAll + ", regex="
+ regex + ", ttl=" + ttl + "]";
+ regex + ", queryIds=" + queryIds + ", ttl=" + ttl + "]";
}

public static Builder tables(String... tables) {
Builder builder = new Builder();
builder.tables = tables;
builder.tables = Arrays.asList(tables);
return builder;
}

public static Builder tablesAny(String... tables) {
Builder builder = new Builder();
builder.tablesAny = tables;
builder.tablesAny = Arrays.asList(tables);
return builder;
}

public static Builder tablesAll(String... tables) {
Builder builder = new Builder();
builder.tablesAll = tables;
builder.tablesAll = Arrays.asList(tables);
return builder;
}

public static Builder queryIds(String... ids) {
Builder builder = new Builder();
builder.queryIds = Arrays.asList(ids);
return builder;
}

Expand All @@ -498,15 +513,41 @@ public static Builder passthrough() {

public static final class Builder {

private String[] tables;
private String[] tablesAny;
private String[] tablesAll;
private List<String> tables;
private List<String> tablesAny;
private List<String> tablesAll;
private String regex;
private List<String> queryIds;
private Duration ttl = DEFAULT_TTL;

private Builder() {
}

public Builder tables(String... tables) {
this.tables = Arrays.asList(tables);
return this;
}

public Builder tablesAny(String... tables) {
this.tablesAny = Arrays.asList(tables);
return this;
}

public Builder tablesAll(String... tables) {
this.tablesAll = Arrays.asList(tables);
return this;
}

public Builder regex(String regex) {
this.regex = regex;
return this;
}

public Builder queryIds(String... ids) {
this.queryIds = Arrays.asList(ids);
return this;
}

public Builder ttl(Duration ttl) {
this.ttl = ttl;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public class HashingFunctions {

private static final Charset CHARSET = StandardCharsets.UTF_8;

public static long crc32(String string) {
public static String crc32(String string) {
CRC32 crc = new CRC32();
crc.update(string.getBytes(CHARSET));
return crc.getValue();
return Long.toHexString(crc.getValue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ private static Rule<Query, Action> rule(RuleConfig rule) {
if (rule.getRegex() != null) {
return new RegexRule<>(Pattern.compile(rule.getRegex()), Query::getSql, action);
}
if (rule.getQueryIds() != null) {
return new PredicateRule<>(q -> rule.getQueryIds().contains(q.getId()), action);
}
return new PredicateRule<>(Predicates.alwaysTrue(), action);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,15 @@ public Builder<T, L, R> control(Control control) {
return control(f -> control);
}

@SuppressWarnings("unchecked")
public CollectionRule<T, L, R> any(T... tableNames) {
public CollectionRule<T, L, R> any(List<T> tableNames) {
return new CollectionRule<>(extractor, new ContainsAnyPredicate<>(tableNames), action, control);
}

@SuppressWarnings("unchecked")
public CollectionRule<T, L, R> all(T... tableNames) {
public CollectionRule<T, L, R> all(List<T> tableNames) {
return new CollectionRule<>(extractor, new ContainsAllPredicate<>(tableNames), action, control);
}

@SuppressWarnings("unchecked")
public CollectionRule<T, L, R> exact(T... tableNames) {
public CollectionRule<T, L, R> exact(List<T> tableNames) {
return new CollectionRule<>(extractor, new EqualsPredicate<>(tableNames), action, control);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.redis.smartcache;
package com.redis.smartcache.core;

import java.sql.SQLException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.redis.smartcache;
package com.redis.smartcache.core;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
Expand All @@ -19,12 +19,10 @@

import com.redis.lettucemod.RedisModulesClient;
import com.redis.lettucemod.api.StatefulRedisModulesConnection;
import com.redis.smartcache.core.Config;
import com.redis.smartcache.Driver;
import com.redis.smartcache.core.Config.KeyConfig;
import com.redis.smartcache.core.Config.RuleConfig;
import com.redis.smartcache.core.Config.RulesetConfig;
import com.redis.smartcache.core.ConfigManager;
import com.redis.smartcache.core.KeyBuilder;
import com.redis.testcontainers.RedisStackContainer;

import io.airlift.units.DataSize;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.redis.smartcache;
package com.redis.smartcache.core;

import java.io.IOException;
import java.sql.DriverManager;
Expand All @@ -11,7 +11,7 @@
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import com.redis.smartcache.core.Config;
import com.redis.smartcache.Driver;
import com.redis.testcontainers.RedisStackContainer;

@Testcontainers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.redis.smartcache;
package com.redis.smartcache.core;

import java.time.Duration;
import java.util.Arrays;
Expand All @@ -12,9 +12,6 @@
import com.redis.lettucemod.RedisModulesClient;
import com.redis.lettucemod.api.StatefulRedisModulesConnection;
import com.redis.smartcache.core.Config.AnalyzerConfig;
import com.redis.smartcache.core.KeyBuilder;
import com.redis.smartcache.core.Query;
import com.redis.smartcache.core.QueryWriter;
import com.redis.testcontainers.RedisStackContainer;

@Testcontainers
Expand Down
Loading

0 comments on commit d2ec161

Please sign in to comment.