Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring set command to use SetParams for the optional parameters #878

Merged
merged 10 commits into from
Aug 4, 2015
25 changes: 13 additions & 12 deletions src/main/java/redis/clients/jedis/BinaryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import static redis.clients.jedis.Protocol.Keyword.WITHSCORES;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -100,9 +102,10 @@ public void set(final byte[] key, final byte[] value) {
sendCommand(Command.SET, key, value);
}

public void set(final byte[] key, final byte[] value, final byte[] nxxx, final byte[] expx,
final long time) {
sendCommand(Command.SET, key, value, nxxx, expx, toByteArray(time));
public void set(final byte[] key, final byte[] value, final byte[] nxxx, final byte[] expx, final byte[] time) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nykolaslima
Even BinaryClient uses ZParams, so we can change its signature to public void set(final byte[] key, final byte[] value, final SetParams params).
Same things can be applied to BinaryJedis.

@xetorthio @marcosnils What do you think about it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with that.

This way we will be more flexible if *Command*Parameters have any changes. I'll fix it.

List<byte[]> params = Arrays.asList(key, value, nxxx, expx, time);

sendCommand(Command.SET, removeNulls(params));
}

public void get(final byte[] key) {
Expand Down Expand Up @@ -1092,15 +1095,6 @@ public void psetex(final byte[] key, final int milliseconds, final byte[] value)
sendCommand(PSETEX, key, toByteArray(milliseconds), value);
}

public void set(final byte[] key, final byte[] value, final byte[] nxxx) {
sendCommand(Command.SET, key, value, nxxx);
}

public void set(final byte[] key, final byte[] value, final byte[] nxxx, final byte[] expx,
final int time) {
sendCommand(Command.SET, key, value, nxxx, expx, toByteArray(time));
}

public void srandmember(final byte[] key, final int count) {
sendCommand(SRANDMEMBER, key, toByteArray(count));
}
Expand Down Expand Up @@ -1193,4 +1187,11 @@ public void pfcount(final byte[]... keys) {
public void pfmerge(final byte[] destkey, final byte[]... sourcekeys) {
sendCommand(PFMERGE, joinParameters(destkey, sourcekeys));
}

private byte[][] removeNulls(List<byte[]> list) {
ArrayList<byte[]> noNullsList = new ArrayList<byte[]>(list);
noNullsList.removeAll(Collections.singleton(null));

return noNullsList.toArray(new byte[noNullsList.size()][]);
}
}
15 changes: 1 addition & 14 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public String set(final byte[] key, final byte[] value) {
* @return Status code reply
*/
public String set(final byte[] key, final byte[] value, final byte[] nxxx, final byte[] expx,
final long time) {
final byte[] time) {
checkIsInMulti();
client.set(key, value, nxxx, expx, time);
return client.getStatusCodeReply();
Expand Down Expand Up @@ -3066,19 +3066,6 @@ public String psetex(final byte[] key, final int milliseconds, final byte[] valu
return client.getStatusCodeReply();
}

public String set(final byte[] key, final byte[] value, final byte[] nxxx) {
checkIsInMulti();
client.set(key, value, nxxx);
return client.getStatusCodeReply();
}

public String set(final byte[] key, final byte[] value, final byte[] nxxx, final byte[] expx,
final int time) {
checkIsInMulti();
client.set(key, value, nxxx, expx, time);
return client.getStatusCodeReply();
}

public String clientKill(final byte[] client) {
checkIsInMulti();
this.client.clientKill(client);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/redis/clients/jedis/BinaryJedisCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public interface BinaryJedisCommands {
String set(byte[] key, byte[] value);

String set(byte[] key, byte[] value, byte[] nxxx, byte[] expx, long time);
String set(byte[] key, byte[] value, byte[] nxxx, byte[] expx, byte[] time);

byte[] get(byte[] key);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/redis/clients/jedis/BinaryShardedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public String set(byte[] key, byte[] value) {
return j.set(key, value);
}

public String set(byte[] key, byte[] value, byte[] nxxx, byte[] expx, long time) {
public String set(byte[] key, byte[] value, byte[] nxxx, byte[] expx, byte[] time) {
Jedis j = getShard(key);
return j.set(key, value, nxxx, expx, time);
}
Expand Down
32 changes: 18 additions & 14 deletions src/main/java/redis/clients/jedis/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Map.Entry;

import redis.clients.jedis.JedisCluster.Reset;
import redis.clients.jedis.params.set.SetParams;
import redis.clients.util.SafeEncoder;

public class Client extends BinaryClient implements Commands {
Expand All @@ -29,10 +30,23 @@ public void set(final String key, final String value) {
set(SafeEncoder.encode(key), SafeEncoder.encode(value));
}

public void set(final String key, final String value, final String nxxx, final String expx,
final long time) {
set(SafeEncoder.encode(key), SafeEncoder.encode(value), SafeEncoder.encode(nxxx),
SafeEncoder.encode(expx), time);
public void set(final String key, final String value, final SetParams params) {

byte[] nxxxBytes = null;
String nxxx = params.getNxxx();
if(nxxx != null) {
nxxxBytes = SafeEncoder.encode(nxxx);
}

byte[] expxBytes = null;
byte[] expxValue = null;
String expx = params.getExpx();
if(expx != null) {
expxBytes = SafeEncoder.encode(expx);
expxValue = SafeEncoder.encode(String.valueOf(params.getParam(expx)));
}

set(SafeEncoder.encode(key), SafeEncoder.encode(value), nxxxBytes, expxBytes, expxValue);
}

public void get(final String key) {
Expand Down Expand Up @@ -793,16 +807,6 @@ public void psetex(final String key, final int milliseconds, final String value)
psetex(SafeEncoder.encode(key), milliseconds, SafeEncoder.encode(value));
}

public void set(final String key, final String value, final String nxxx) {
set(SafeEncoder.encode(key), SafeEncoder.encode(value), SafeEncoder.encode(nxxx));
}

public void set(final String key, final String value, final String nxxx, final String expx,
final int time) {
set(SafeEncoder.encode(key), SafeEncoder.encode(value), SafeEncoder.encode(nxxx),
SafeEncoder.encode(expx), time);
}

public void srandmember(final String key, final int count) {
srandmember(SafeEncoder.encode(key), count);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/redis/clients/jedis/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import java.util.Map;

import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.params.set.SetParams;

public interface Commands {

public void set(final String key, final String value);

public void set(final String key, final String value, final String nxxx, final String expx,
final long time);
public void set(final String key, final String value, SetParams params);

public void get(final String key);

Expand Down
39 changes: 16 additions & 23 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package redis.clients.jedis;

import java.net.URI;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.JedisCluster.Reset;
import redis.clients.jedis.params.set.SetParams;
import redis.clients.util.Pool;
import redis.clients.util.SafeEncoder;
import redis.clients.util.Slowlog;

import java.net.URI;
import java.util.*;
import java.util.Map.Entry;

public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands,
AdvancedJedisCommands, ScriptingCommands, BasicCommands, ClusterCommands, SentinelCommands {

Expand Down Expand Up @@ -71,16 +79,14 @@ public String set(final String key, String value) {
* GB).
* @param key
* @param value
* @param nxxx NX|XX, NX -- Only set the key if it does not already exist. XX -- Only set the key
* @param params NX|XX, NX -- Only set the key if it does not already exist. XX -- Only set the key
* if it already exist.
* @param expx EX|PX, expire time units: EX = seconds; PX = milliseconds
* @param time expire time in the units of {@param #expx}
* EX|PX, expire time units: EX = seconds; PX = milliseconds
* @return Status code reply
*/
public String set(final String key, final String value, final String nxxx, final String expx,
final long time) {
public String set(final String key, final String value, final SetParams params) {
checkIsInMulti();
client.set(key, value, nxxx, expx, time);
client.set(key, value, params);
return client.getStatusCodeReply();
}

Expand Down Expand Up @@ -2906,19 +2912,6 @@ public String psetex(final String key, final int milliseconds, final String valu
return client.getStatusCodeReply();
}

public String set(final String key, final String value, final String nxxx) {
checkIsInMulti();
client.set(key, value, nxxx);
return client.getStatusCodeReply();
}

public String set(final String key, final String value, final String nxxx, final String expx,
final int time) {
checkIsInMulti();
client.set(key, value, nxxx, expx, time);
return client.getStatusCodeReply();
}

public String clientKill(final String client) {
checkIsInMulti();
this.client.clientKill(client);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/redis/clients/jedis/JedisCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.params.set.SetParams;

public class JedisCluster implements JedisCommands, BasicCommands, Closeable {
public static final short HASHSLOTS = 16384;
Expand Down Expand Up @@ -77,12 +78,11 @@ public String execute(Jedis connection) {
}

@Override
public String set(final String key, final String value, final String nxxx, final String expx,
final long time) {
public String set(final String key, final String value, final SetParams params) {
return new JedisClusterCommand<String>(connectionHandler, timeout, maxRedirections) {
@Override
public String execute(Jedis connection) {
return connection.set(key, value, nxxx, expx, time);
return connection.set(key, value, params);
}
}.run(key);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/redis/clients/jedis/JedisCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.params.set.SetParams;

/**
* Common interface for sharded and non-sharded Jedis
*/
public interface JedisCommands {
String set(String key, String value);

String set(String key, String value, String nxxx, String expx, long time);
String set(String key, String value, SetParams params);

String get(String key);

Expand Down
31 changes: 11 additions & 20 deletions src/main/java/redis/clients/jedis/PipelineBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Set;

import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.params.set.SetParams;

abstract class PipelineBase extends Queable implements BinaryRedisPipeline, RedisPipeline {

Expand Down Expand Up @@ -499,6 +500,16 @@ public Response<String> set(byte[] key, byte[] value) {
getClient(key).set(key, value);
return getResponse(BuilderFactory.STRING);
}

public Response<String> set(String key, String value, SetParams params) {
getClient(key).set(key, value, params);
return getResponse(BuilderFactory.STRING);
}

public Response<String> set(byte[] key, byte[] value, byte[] nxxx, byte[] expx, byte[] time) {
getClient(key).set(key, value, nxxx, expx, time);
return getResponse(BuilderFactory.STRING);
}

public Response<Boolean> setbit(String key, long offset, boolean value) {
getClient(key).setbit(key, offset, value);
Expand Down Expand Up @@ -1200,26 +1211,6 @@ public Response<String> psetex(byte[] key, int milliseconds, byte[] value) {
return getResponse(BuilderFactory.STRING);
}

public Response<String> set(String key, String value, String nxxx) {
getClient(key).set(key, value, nxxx);
return getResponse(BuilderFactory.STRING);
}

public Response<String> set(byte[] key, byte[] value, byte[] nxxx) {
getClient(key).set(key, value, nxxx);
return getResponse(BuilderFactory.STRING);
}

public Response<String> set(String key, String value, String nxxx, String expx, int time) {
getClient(key).set(key, value, nxxx, expx, time);
return getResponse(BuilderFactory.STRING);
}

public Response<String> set(byte[] key, byte[] value, byte[] nxxx, byte[] expx, int time) {
getClient(key).set(key, value, nxxx, expx, time);
return getResponse(BuilderFactory.STRING);
}

public Response<Double> hincrByFloat(String key, String field, double increment) {
getClient(key).hincrByFloat(key, field, increment);
return getResponse(BuilderFactory.DOUBLE);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/redis/clients/jedis/ShardedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.regex.Pattern;

import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.params.set.SetParams;
import redis.clients.util.Hashing;
import redis.clients.util.Pool;

Expand Down Expand Up @@ -37,9 +38,9 @@ public String set(String key, String value) {
}

@Override
public String set(String key, String value, String nxxx, String expx, long time) {
public String set(String key, String value, SetParams params) {
Jedis j = getShard(key);
return j.set(key, value, nxxx, expx, time);
return j.set(key, value, params);
}

public String get(String key) {
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/redis/clients/jedis/params/Params.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package redis.clients.jedis.params;

import java.util.HashMap;
import java.util.Map;

public abstract class Params {

private Map<String, Object> params;

@SuppressWarnings("unchecked")
public <T> T getParam(String name) {
if(params == null) return null;

return (T) params.get(name);
}

public boolean contains(String name) {
if(params == null) return false;

return params.containsKey(name);
}

protected void addParam(String name, Object value) {
if(params == null) {
params = new HashMap<String, Object>();
}
params.put(name, value);
}

protected void addParam(String name) {
if(params == null) {
params = new HashMap<String, Object>();
}
params.put(name, true);
}

}
Loading