Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ public void resetState() {
}
}

public void copy(byte[] srcKey, byte[] dstKey, boolean replace) {
if (replace) {
sendCommand(COPY, srcKey, dstKey, REPLACE.getRaw());
} else {
sendCommand(COPY, srcKey, dstKey);
}
}

public void copy(byte[] srcKey, byte[] dstKey, int db, boolean replace) {
if (replace) {
sendCommand(COPY, srcKey, dstKey, DB.getRaw(), toByteArray(db), REPLACE.getRaw());
} else {
sendCommand(COPY, srcKey, dstKey, DB.getRaw(), toByteArray(db));
}
}

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

/**
* COPY source destination [DB destination-db] [REPLACE]
*
* @param srcKey the source key.
* @param dstKey the destination key.
* @param db
* @param replace
* @return
*/
@Override
public Boolean copy(byte[] srcKey, byte[] dstKey, int db, boolean replace) {
checkIsInMultiOrPipeline();
client.copy(srcKey, dstKey, db, replace);
return BuilderFactory.BOOLEAN.build(client.getIntegerReply());
}

/**
* COPY source destination [DB destination-db] [REPLACE]
*
* @param srcKey the source key.
* @param dstKey the destination key.
* @param replace
* @return
*/
@Override
public Boolean copy(byte[] srcKey, byte[] dstKey, boolean replace) {
checkIsInMultiOrPipeline();
client.copy(srcKey, dstKey, replace);
return BuilderFactory.BOOLEAN.build(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 @@ -157,6 +157,16 @@ public Jedis getConnectionFromSlot(int slot) {
return this.connectionHandler.getConnectionFromSlot(slot);
}

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

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

@Override
public void copy(String srcKey, String dstKey, int db, boolean replace) {
copy(SafeEncoder.encode(srcKey), SafeEncoder.encode(dstKey), db, replace);
}

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

@Override
public void ping(final String message) {
ping(SafeEncoder.encode(message));
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,37 @@ 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 db
* @param replace
* @return
*/
@Override
public Boolean copy(String srcKey, String dstKey, int db, boolean replace) {
checkIsInMultiOrPipeline();
client.copy(srcKey, dstKey, db, replace);
return BuilderFactory.BOOLEAN.build(client.getIntegerReply());
}

/**
* COPY source destination [DB destination-db] [REPLACE]
*
* @param srcKey the source key.
* @param dstKey the destination key.
* @param replace
* @return
*/
@Override
public Boolean copy(String srcKey, String dstKey, boolean replace) {
checkIsInMultiOrPipeline();
client.copy(srcKey, dstKey, replace);
return BuilderFactory.BOOLEAN.build(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 @@ -248,6 +248,16 @@ public JedisCluster(Set<HostAndPort> nodes, final JedisClientConfig clientConfig
super(nodes, clientConfig, maxAttempts, poolConfig);
}

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

@Override
public String set(final String key, final String value) {
return new JedisClusterCommand<String>(connectionHandler, maxAttempts) {
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ public Response<String> blmove(String srcKey, String dstKey, ListDirection from,
return getResponse(BuilderFactory.STRING);
}

@Override
public Response<Boolean> copy(byte[] srcKey, byte[] dstKey, int db, boolean replace) {
client.copy(srcKey, dstKey, db, replace);
return getResponse(BuilderFactory.BOOLEAN);
}

@Override
public Response<Boolean> copy(byte[] srcKey, byte[] dstKey, boolean replace) {
client.copy(srcKey, dstKey, replace);
return getResponse(BuilderFactory.BOOLEAN);
}

@Override
public Response<Boolean> copy(String srcKey, String dstKey, int db, boolean replace) {
client.copy(srcKey, dstKey, db, replace);
return getResponse(BuilderFactory.BOOLEAN);
}

@Override
public Response<Boolean> copy(String srcKey, String dstKey, boolean replace) {
client.copy(srcKey, dstKey, replace);
return getResponse(BuilderFactory.BOOLEAN);
}

@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, BLMOVE, LMOVE;
BITFIELD_RO, LPOS, SMISMEMBER, ZMSCORE, BZPOPMIN, BZPOPMAX, BLMOVE, LMOVE, 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
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/commands/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

public interface Commands {

void copy(String srcKey, String dstKey, int db, boolean replace);

void copy(String srcKey, String dstKey, boolean replace);

void ping(String message);

void set(String key, String value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import java.util.Set;

public interface MultiKeyBinaryCommands {
Boolean copy(byte[] srcKey, byte[] dstKey, int db, boolean replace);

Boolean copy(byte[] srcKey, byte[] dstKey, boolean replace);

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

Long unlink(byte[]... keys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Set;

public interface MultiKeyBinaryJedisClusterCommands {
Boolean copy(byte[] srcKey, byte[] dstKey, boolean replace);

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

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

Response<Boolean> copy(byte[] srcKey, byte[] dstKey, boolean replace);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
import java.util.Set;

public interface MultiKeyCommands {

Boolean copy(String srcKey, String dstKey, int db, boolean replace);

Boolean copy(String srcKey, String dstKey, boolean replace);

Long del(String... keys);

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

Response<Boolean> copy(String srcKey, String dstKey, boolean replace);

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

Response<Long> unlink(String... keys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Set;

public interface MultiKeyJedisClusterCommands {
Boolean copy(String srcKey, String dstKey, boolean replace);

Long del(String... keys);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -940,4 +940,22 @@ public void encodeCompleteResponse() {

}

@Test
public void copy() {
jedis.set("foo", "bar");
assertTrue(jedis.copy("foo", "bar", false));
assertFalse(jedis.copy("unknown", "bar1", false));
assertEquals("bar", jedis.get("bar"));

// with destinationDb
assertTrue(jedis.copy("foo", "bar1", 2, false));
jedis.select(2);
assertEquals("bar", jedis.get("bar1"));

// replace
jedis.set("foo", "bar");
jedis.set("bar2", "b");
assertTrue(jedis.copy("foo", "bar2", true));
assertEquals("bar", jedis.get("bar2"));
}
}