Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ public void resetState() {
}
}

public void copy(byte[] srcKey, byte[] dstKey, CopyParams params) {
if (params == null) {
sendCommand(COPY, srcKey, dstKey);
} else {
sendCommand(COPY, params.getByteParams(srcKey, dstKey));
}
}

public void ping() {
sendCommand(PING);
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,21 @@ public int getDB() {
return client.getDB();
}

/**
* COPY source destination [DB destination-db] [REPLACE]
*
* @param srcKey the source key.
* @param dstKey the destination key.
* @param params
* @return
*/
@Override
public Long copy(byte[] srcKey, byte[] dstKey, CopyParams params) {
checkIsInMultiOrPipeline();
client.copy(srcKey, dstKey, params);
return client.getIntegerReply();
}

/**
* @return <code>PONG</code>
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedisCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ public Jedis getConnectionFromSlot(int slot) {
return this.connectionHandler.getConnectionFromSlot(slot);
}

@Override
public Long copy(byte[] srcKey, byte[] dstKey, CopyParams params) {
return new JedisClusterCommand<Long>(connectionHandler, maxAttempts) {
@Override
public Long execute(Jedis connection) {
return connection.copy(srcKey, dstKey, params);
}
}.runBinary(2, srcKey, dstKey);
}

@Override
public String set(final byte[] key, final byte[] value) {
return new JedisClusterCommand<String>(connectionHandler, maxAttempts) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/redis/clients/jedis/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public Client(final JedisSocketFactory jedisSocketFactory) {
super(jedisSocketFactory);
}

@Override
public void copy(String srcKey, String dstKey, CopyParams params) {
copy(SafeEncoder.encode(srcKey), SafeEncoder.encode(dstKey), params);
}

@Override
public void ping(final String message) {
ping(SafeEncoder.encode(message));
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ public Jedis(final JedisSocketFactory jedisSocketFactory) {
super(jedisSocketFactory);
}

/**
* COPY source destination [DB destination-db] [REPLACE]
*
* @param srcKey the source key.
* @param dstKey the destination key.
* @param params
* @return
*/
@Override
public Long copy(String srcKey, String dstKey, CopyParams params) {
checkIsInMultiOrPipeline();
client.copy(srcKey, dstKey, params);
return client.getIntegerReply();
}

/**
* Works same as <tt>ping()</tt> but returns argument message instead of <tt>PONG</tt>.
* @param message
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/JedisCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ public JedisCluster(Set<HostAndPort> nodes, final JedisClientConfig clientConfig
super(nodes, clientConfig, maxAttempts, poolConfig);
}

@Override
public Long copy(String srcKey, String dstKey, CopyParams params) {
return new JedisClusterCommand<Long>(connectionHandler, maxAttempts) {
@Override
public Long execute(Jedis connection) {
return connection.copy(srcKey, dstKey, params);
}
}.run(2, srcKey, dstKey);
}

@Override
public String set(final String key, final String value) {
return new JedisClusterCommand<String>(connectionHandler, maxAttempts) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ public abstract class MultiKeyPipelineBase extends PipelineBase implements

protected Client client = null;

@Override
public Response<Long> copy(byte[] srcKey, byte[] dstKey, CopyParams params) {
client.copy(srcKey, dstKey, params);
return getResponse(BuilderFactory.LONG);
}

@Override
public Response<Long> copy(String srcKey, String dstKey, CopyParams params) {
client.copy(srcKey, dstKey, params);
return getResponse(BuilderFactory.LONG);
}

@Override
public Response<List<String>> brpop(String... args) {
client.brpop(args);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/redis/clients/jedis/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public static enum Command implements ProtocolCommand {
READONLY, GEOADD, GEODIST, GEOHASH, GEOPOS, GEORADIUS, GEORADIUS_RO, GEORADIUSBYMEMBER,
GEORADIUSBYMEMBER_RO, MODULE, BITFIELD, HSTRLEN, TOUCH, SWAPDB, MEMORY, XADD, XLEN, XDEL,
XTRIM, XRANGE, XREVRANGE, XREAD, XACK, XGROUP, XREADGROUP, XPENDING, XCLAIM, ACL, XINFO,
BITFIELD_RO, LPOS, SMISMEMBER, ZMSCORE, BZPOPMIN, BZPOPMAX;
BITFIELD_RO, LPOS, SMISMEMBER, ZMSCORE, BZPOPMIN, BZPOPMAX, COPY;

private final byte[] raw;

Expand All @@ -286,7 +286,7 @@ public static enum Keyword implements Rawable {
NOACK, STREAMS, KEY, CREATE, MKSTREAM, SETID, DESTROY, DELCONSUMER, MAXLEN, GROUP, ID, IDLE,
TIME, RETRYCOUNT, FORCE, USAGE, SAMPLES, STREAM, GROUPS, CONSUMERS, HELP, FREQ, SETUSER,
GETUSER, DELUSER, WHOAMI, CAT, GENPASS, USERS, LOG, INCR, SAVE, JUSTID, WITHVALUES, UNBLOCK,
NOMKSTREAM, MINID;
NOMKSTREAM, MINID, DB;

/**
* @deprecated This will be private in future. Use {@link #getRaw()}.
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/redis/clients/jedis/commands/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import redis.clients.jedis.SortingParams;
import redis.clients.jedis.args.UnblockType;
import redis.clients.jedis.ZParams;
import redis.clients.jedis.params.CopyParams;
import redis.clients.jedis.params.GetExParams;
import redis.clients.jedis.params.MigrateParams;
import redis.clients.jedis.params.ClientKillParams;
Expand All @@ -25,6 +26,8 @@

public interface Commands {

void copy(String srcKey, String dstKey, CopyParams params);

void ping(String message);

void set(String key, String value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import redis.clients.jedis.SortingParams;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.ZParams;
import redis.clients.jedis.params.CopyParams;
import redis.clients.jedis.params.GeoRadiusParam;
import redis.clients.jedis.params.GeoRadiusStoreParam;
import redis.clients.jedis.params.XReadGroupParams;
Expand All @@ -18,6 +19,8 @@
import java.util.Set;

public interface MultiKeyBinaryCommands {
Long copy(byte[] srcKey, byte[] dstKey, CopyParams params);

Long del(byte[]... keys);

Long unlink(byte[]... keys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import redis.clients.jedis.BinaryJedisPubSub;
import redis.clients.jedis.BitOP;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.Response;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.KeyedTuple;
import redis.clients.jedis.SortingParams;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.ZParams;
import redis.clients.jedis.params.CopyParams;
import redis.clients.jedis.params.GeoRadiusParam;
import redis.clients.jedis.params.GeoRadiusStoreParam;
import redis.clients.jedis.params.XReadGroupParams;
Expand All @@ -20,6 +22,8 @@
import java.util.Set;

public interface MultiKeyBinaryJedisClusterCommands {
Long copy(byte[] srcKey, byte[] dstKey, CopyParams params);

Long del(byte[]... keys);

Long unlink(byte[]... keys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Multikey related commands (these are split out because they are non-shardable)
*/
public interface MultiKeyBinaryRedisPipeline {
Response<Long> copy(byte[] srcKey, byte[] dstKey, CopyParams params);

Response<Long> del(byte[]... keys);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import redis.clients.jedis.StreamEntry;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.ZParams;
import redis.clients.jedis.params.CopyParams;
import redis.clients.jedis.params.GeoRadiusParam;
import redis.clients.jedis.params.GeoRadiusStoreParam;
import redis.clients.jedis.params.XReadGroupParams;
Expand All @@ -21,6 +22,9 @@
import java.util.Set;

public interface MultiKeyCommands {

Long copy(String srcKey, String dstKey, CopyParams params);

Long del(String... keys);

Long unlink(String... keys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* Multikey related commands (these are split out because they are non-shardable)
*/
public interface MultiKeyCommandsPipeline {
Response<Long> copy(String srcKey, String dstKey, CopyParams params);

Response<Long> del(String... keys);

Response<Long> unlink(String... keys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import redis.clients.jedis.StreamEntryID;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.ZParams;
import redis.clients.jedis.params.CopyParams;
import redis.clients.jedis.params.GeoRadiusParam;
import redis.clients.jedis.params.GeoRadiusStoreParam;
import redis.clients.jedis.params.XReadGroupParams;
Expand All @@ -21,6 +22,7 @@
import java.util.Set;

public interface MultiKeyJedisClusterCommands {
Long copy(String srcKey, String dstKey, CopyParams params);

Long del(String... keys);

Expand Down
47 changes: 47 additions & 0 deletions src/main/java/redis/clients/jedis/params/CopyParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package redis.clients.jedis.params;

import static redis.clients.jedis.Protocol.Keyword.DB;
import static redis.clients.jedis.Protocol.Keyword.REPLACE;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import redis.clients.jedis.Protocol;

public class CopyParams extends Params {

private Long destinationDb;

private boolean replace;

public static CopyParams copyParams() {
return new CopyParams();
}

public CopyParams destinationDb(long destinationDb) {
this.destinationDb = destinationDb;
return this;
}

public CopyParams replace() {
this.replace = true;
return this;
}

public byte[][] getByteParams(byte[] key, byte[]... args) {
List<byte[]> byteParams = new ArrayList<>();
byteParams.add(key);
Collections.addAll(byteParams, args);

if (destinationDb != null) {
byteParams.add(DB.getRaw());
byteParams.add(Protocol.toByteArray(destinationDb));
}

if (replace) {
byteParams.add(REPLACE.getRaw());
}
return byteParams.toArray(new byte[byteParams.size()][]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.StreamEntryID;
import redis.clients.jedis.args.FlushMode;
import redis.clients.jedis.params.CopyParams;
import redis.clients.jedis.util.SafeEncoder;
import redis.clients.jedis.exceptions.JedisDataException;

Expand Down Expand Up @@ -940,4 +941,22 @@ public void encodeCompleteResponse() {

}

@Test
public void copy() {
jedis.set("foo", "bar");
assertEquals(1, jedis.copy("foo", "bar", null).longValue());
assertEquals(0, jedis.copy("unknown", "bar1", null).longValue());
assertEquals("bar", jedis.get("bar"));

// with destinationDb
assertEquals(1, jedis.copy("foo", "bar1", new CopyParams().destinationDb(2)).longValue());
jedis.select(2);
assertEquals("bar", jedis.get("bar1"));

// replace
jedis.set("foo", "bar");
jedis.set("bar2", "b");
assertEquals(1, jedis.copy("foo", "bar2", new CopyParams().replace()).longValue());
assertEquals("bar", jedis.get("bar2"));
}
}