From 23943800fbc47bcadbc6d7f17c4b32d442a637fb Mon Sep 17 00:00:00 2001 From: Cem Asma Date: Mon, 11 Mar 2024 16:25:28 +0300 Subject: [PATCH] Add support for redis command: CLIENT TRACKINGINFO (#3751) Co-authored-by: Cem Asma --- src/main/java/redis/clients/jedis/Jedis.java | 7 ++ .../jedis/commands/ClientCommands.java | 3 + .../clients/jedis/resps/TrackingInfo.java | 70 +++++++++++++++++++ .../commands/jedis/ClientCommandsTest.java | 23 ++++++ 4 files changed, 103 insertions(+) create mode 100644 src/main/java/redis/clients/jedis/resps/TrackingInfo.java diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index 1e25f2ffcb..4e91ed1ca2 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -4325,6 +4325,13 @@ public String clientNoTouchOff() { return connection.getStatusCodeReply(); } + @Override + public TrackingInfo clientTrackingInfo() { + checkIsInMultiOrPipeline(); + connection.sendCommand(CLIENT, "TRACKINGINFO"); + return TrackingInfo.TRACKING_INFO_BUILDER.build(connection.getOne()); + } + public List time() { checkIsInMultiOrPipeline(); connection.sendCommand(Command.TIME); diff --git a/src/main/java/redis/clients/jedis/commands/ClientCommands.java b/src/main/java/redis/clients/jedis/commands/ClientCommands.java index 75bda24a30..912b1b9d08 100644 --- a/src/main/java/redis/clients/jedis/commands/ClientCommands.java +++ b/src/main/java/redis/clients/jedis/commands/ClientCommands.java @@ -5,6 +5,7 @@ import redis.clients.jedis.args.ClientType; import redis.clients.jedis.args.UnblockType; import redis.clients.jedis.params.ClientKillParams; +import redis.clients.jedis.resps.TrackingInfo; /** * The interface contain all the commands about client. @@ -170,4 +171,6 @@ public interface ClientCommands { * @return OK */ String clientNoTouchOff(); + + TrackingInfo clientTrackingInfo(); } diff --git a/src/main/java/redis/clients/jedis/resps/TrackingInfo.java b/src/main/java/redis/clients/jedis/resps/TrackingInfo.java new file mode 100644 index 0000000000..d87276a2b5 --- /dev/null +++ b/src/main/java/redis/clients/jedis/resps/TrackingInfo.java @@ -0,0 +1,70 @@ +package redis.clients.jedis.resps; + +import redis.clients.jedis.Builder; +import redis.clients.jedis.util.KeyValue; + +import java.util.Collections; +import java.util.List; + +import static redis.clients.jedis.BuilderFactory.*; + +public class TrackingInfo { + + private final List flags; + private final long redirect; + private final List prefixes; + + public TrackingInfo(List flags, long redirect, List prefixes) { + this.flags = flags; + this.redirect = redirect; + this.prefixes = prefixes; + } + + + public List getFlags() { + return flags; + } + + public long getRedirect() { + return redirect; + } + + public List getPrefixes() { + return prefixes; + } + + public static final Builder TRACKING_INFO_BUILDER = new Builder() { + @Override + public TrackingInfo build(Object data) { + List commandData = (List) data; + + if (commandData.get(0) instanceof KeyValue) { + List flags = Collections.emptyList(); + long redirect = -1; + List prefixes = Collections.emptyList(); + + for (KeyValue kv : (List) commandData) { + switch (STRING.build(kv.getKey())) { + case "flags": + flags = STRING_LIST.build(kv.getValue()); + break; + case "redirect": + redirect = LONG.build(kv.getValue()); + break; + case "prefixes": + prefixes = STRING_LIST.build(kv.getValue()); + break; + } + } + + return new TrackingInfo(flags, redirect, prefixes); + } else { + List flags = STRING_LIST.build(commandData.get(1)); + long redirect = LONG.build(commandData.get(3)); + List prefixes = STRING_LIST.build(commandData.get(5)); + + return new TrackingInfo(flags, redirect, prefixes); + } + } + }; +} diff --git a/src/test/java/redis/clients/jedis/commands/jedis/ClientCommandsTest.java b/src/test/java/redis/clients/jedis/commands/jedis/ClientCommandsTest.java index cf7760dbf4..40aff1045c 100644 --- a/src/test/java/redis/clients/jedis/commands/jedis/ClientCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/jedis/ClientCommandsTest.java @@ -19,12 +19,15 @@ import org.junit.Before; import org.junit.Test; +import redis.clients.jedis.DefaultJedisClientConfig; import redis.clients.jedis.Jedis; +import redis.clients.jedis.RedisProtocol; import redis.clients.jedis.args.ClientAttributeOption; import redis.clients.jedis.args.ClientType; import redis.clients.jedis.args.UnblockType; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.params.ClientKillParams; +import redis.clients.jedis.resps.TrackingInfo; public class ClientCommandsTest extends JedisCommandsTestBase { @@ -279,6 +282,26 @@ public void listWithType() { assertEquals(1, client.clientList(ClientType.PUBSUB).split("\\n").length); } + @Test + public void trackingInfo() { + TrackingInfo trackingInfo = client.clientTrackingInfo(); + + assertEquals(1, trackingInfo.getFlags().size()); + assertEquals(-1, trackingInfo.getRedirect()); + assertEquals(0, trackingInfo.getPrefixes().size()); + } + + @Test + public void trackingInfoResp3() { + Jedis clientResp3 = new Jedis(hnp, DefaultJedisClientConfig.builder() + .protocol(RedisProtocol.RESP3).password("foobared").build()); + TrackingInfo trackingInfo = clientResp3.clientTrackingInfo(); + + assertEquals(1, trackingInfo.getFlags().size()); + assertEquals(-1, trackingInfo.getRedirect()); + assertEquals(0, trackingInfo.getPrefixes().size()); + } + private void assertDisconnected(Jedis j) { try { j.ping();