-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Demonstrated automatic key-prefixing for all subclasses of UnifiedJedis: JedisCluster, JedisPooled, and JedisSentineled - Key-prefixing is possible as long as the underlying CommandObjects can be customized. - CommandObjects cannot use commandArguments in its constructor since in the specific case of key-prefixing, commandArguments depends on the child constructor running first. So we lose caching of argument-less CommandObjects. - Based on this POC, the minimum changes required to jedis would be: - public constructors that allow UnifiedJedis and its subclasses to take a custom CommandObjects. - Consistent use of supplied CommandObjects throughout code (e.g. in Pipeline, Transaction, etc). - Removal of caching of argument-less CommandObjects in the constructor of CommandObjects. - Applications can then supply CommandObjects with custom behavior as necessary. Sample classes CommandObjectsWithPrefixedKeys that implement the behavior of prefixed keys, etc are provided but these can be supplied by the application as long as required constructors are available.
- Loading branch information
R-J Lim
committed
Mar 14, 2024
1 parent
a0efc76
commit d1e4110
Showing
15 changed files
with
233 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/redis/clients/jedis/CommandArgumentsWithPrefixedKeys.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package redis.clients.jedis; | ||
|
||
import redis.clients.jedis.args.Rawable; | ||
import redis.clients.jedis.args.RawableFactory; | ||
import redis.clients.jedis.commands.ProtocolCommand; | ||
import redis.clients.jedis.util.SafeEncoder; | ||
|
||
public class CommandArgumentsWithPrefixedKeys extends CommandArguments { | ||
private final byte[] prefix; | ||
private final String prefixString; | ||
|
||
public CommandArgumentsWithPrefixedKeys(ProtocolCommand command, String prefixString) { | ||
super(command); | ||
this.prefixString = prefixString; | ||
prefix = SafeEncoder.encode(prefixString); | ||
} | ||
|
||
public CommandArguments key(Object key) { | ||
return super.key(namespacedKey(key)); | ||
} | ||
|
||
private Object namespacedKey(Object key) { | ||
if (key instanceof Rawable) { | ||
byte[] raw = ((Rawable) key).getRaw(); | ||
return RawableFactory.from(namespacedKeyBytes(raw)); | ||
} | ||
|
||
if (key instanceof byte[]) { | ||
return namespacedKeyBytes((byte[]) key); | ||
} | ||
|
||
if (key instanceof String) { | ||
String raw = (String) key; | ||
return prefixString + raw; | ||
} | ||
|
||
throw new IllegalArgumentException("\"" + key.toString() + "\" is not a valid argument."); | ||
} | ||
|
||
private byte[] namespacedKeyBytes(byte[] key) { | ||
byte[] namespaced = new byte[prefix.length + key.length]; | ||
System.arraycopy(prefix, 0, namespaced, 0, prefix.length); | ||
System.arraycopy(key, 0, namespaced, prefix.length, key.length); | ||
return namespaced; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/redis/clients/jedis/CommandObjectsWithPrefixedKeys.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package redis.clients.jedis; | ||
|
||
import redis.clients.jedis.commands.ProtocolCommand; | ||
|
||
public class CommandObjectsWithPrefixedKeys extends CommandObjects { | ||
private final String prefixString; | ||
|
||
public CommandObjectsWithPrefixedKeys(String prefixString) { | ||
this.prefixString = prefixString; | ||
} | ||
|
||
@Override | ||
protected CommandArguments commandArguments(ProtocolCommand command) { | ||
return new CommandArgumentsWithPrefixedKeys(command, prefixString); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 0 additions & 61 deletions
61
src/main/java/redis/clients/jedis/JedisClusterWithPrefixedKeys.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/test/java/redis/clients/jedis/JedisClusterPrefixedKeysTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package redis.clients.jedis; | ||
|
||
import org.junit.Before; | ||
import redis.clients.jedis.args.ClusterResetType; | ||
import redis.clients.jedis.executors.ClusterCommandExecutor; | ||
import redis.clients.jedis.providers.ClusterConnectionProvider; | ||
import redis.clients.jedis.util.JedisClusterTestUtil; | ||
|
||
import java.time.Duration; | ||
import java.util.Collections; | ||
|
||
public class JedisClusterPrefixedKeysTest extends PrefixedKeysTest { | ||
private static final DefaultJedisClientConfig DEFAULT_CLIENT_CONFIG = DefaultJedisClientConfig.builder().password("cluster").build(); | ||
private static final int DEFAULT_TIMEOUT = 2000; | ||
private static final int DEFAULT_REDIRECTIONS = 5; | ||
private static final ConnectionPoolConfig DEFAULT_POOL_CONFIG = new ConnectionPoolConfig(); | ||
private static final HostAndPort HOST_AND_PORT = new HostAndPort("127.0.0.1", 7379); | ||
|
||
@Before | ||
public void setUp() throws InterruptedException { | ||
Jedis jedis = new Jedis(HOST_AND_PORT); | ||
jedis.auth("cluster"); | ||
jedis.clusterReset(ClusterResetType.HARD); | ||
jedis.flushAll(); | ||
|
||
int[] slots = new int[Protocol.CLUSTER_HASHSLOTS]; | ||
|
||
for (int i = 0; i < Protocol.CLUSTER_HASHSLOTS; ++i) { | ||
slots[i] = i; | ||
} | ||
|
||
jedis.clusterAddSlots(slots); | ||
JedisClusterTestUtil.waitForClusterReady(jedis); | ||
} | ||
|
||
@Before | ||
public void tearDown() throws InterruptedException { | ||
Jedis jedis = new Jedis(HOST_AND_PORT); | ||
jedis.auth("cluster"); | ||
jedis.clusterReset(ClusterResetType.HARD); | ||
jedis.flushAll(); | ||
} | ||
|
||
@Override | ||
public UnifiedJedis prefixingJedis() { | ||
ClusterConnectionProvider connectionProvider = new ClusterConnectionProvider(Collections.singleton(HOST_AND_PORT), DEFAULT_CLIENT_CONFIG); | ||
ClusterCommandExecutor executor = new ClusterCommandExecutor(connectionProvider, 5, Duration.ofSeconds(5 * DEFAULT_TIMEOUT)); | ||
ClusterCommandObjectsWithPrefixedKeys commandObjects = new ClusterCommandObjectsWithPrefixedKeys("test-prefix:"); | ||
return new JedisCluster(executor, connectionProvider, commandObjects, DEFAULT_CLIENT_CONFIG.getRedisProtocol()); | ||
} | ||
|
||
@Override | ||
public UnifiedJedis nonPrefixingJedis() { | ||
return new JedisCluster(HOST_AND_PORT, DEFAULT_CLIENT_CONFIG, DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG); | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
src/test/java/redis/clients/jedis/JedisClusterWithPrefixedKeysTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.