Skip to content

Commit

Permalink
Merge pull request apache#3 from phufool/cluster-scripting-commands
Browse files Browse the repository at this point in the history
-Pass keyCount to runScript method
  • Loading branch information
phufool committed Apr 8, 2014
2 parents b55b79f + e062f9f commit 4003ce2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 68 deletions.
78 changes: 15 additions & 63 deletions src/main/java/redis/clients/jedis/JedisCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -1492,32 +1492,29 @@ public Object eval(final String script, final int keyCount, final String... para
public Object execute(Jedis connection) {
return connection.eval(script, keyCount, params);
}
}.runScript(params);
}.runScript(keyCount, params);
}

@Override
public Object eval(final String script, final List<String> keys, final List<String> args) {
public Object eval(final String script, final String key) {
return new JedisClusterCommand<Object>(connectionHandler,
timeout, maxRedirections) {
@Override
public Object execute(Jedis connection) {
return connection.eval(script, keys, args);
return connection.eval(script);
}
}.runScript(keys.toArray(new String[keys.size()]));
}.runScript(1, key);
}

/**
* This method will not execute the script because no key is provided.
*/
@Override
public Object eval(final String script) {
public Object eval(final String script, final List<String> keys, final List<String> args) {
return new JedisClusterCommand<Object>(connectionHandler,
timeout, maxRedirections) {
@Override
public Object execute(Jedis connection) {
return connection.eval(script);
return connection.eval(script, keys, args);
}
}.runScript(null);
}.runScript(keys.size(), keys.toArray(new String[keys.size()]));
}

@Override
Expand All @@ -1528,7 +1525,7 @@ public Object evalsha(final String sha1, final int keyCount, final String... par
public Object execute(Jedis connection) {
return connection.evalsha(sha1, keyCount, params);
}
}.runScript(params);
}.runScript(keyCount, params);
}

@Override
Expand All @@ -1539,35 +1536,18 @@ public Object evalsha(final String sha1, final List<String> keys, final List<Str
public Object execute(Jedis connection) {
return connection.evalsha(sha1, keys, args);
}
}.runScript(keys.toArray(new String[keys.size()]));
}.runScript(keys.size(), keys.toArray(new String[keys.size()]));
}

/**
* This method will not execute the script because no key is provided.
*/
@Override
public Object evalsha(final String sha1) {
public Object evalsha(final String script, final String key) {
return new JedisClusterCommand<Object>(connectionHandler,
timeout, maxRedirections) {
@Override
public Object execute(Jedis connection) {
return connection.evalsha(sha1);
}
}.runScript(null);
}

/**
* This method will not execute the script because no key is provided.
*/
@Override
public Boolean scriptExists(final String sha1) {
return new JedisClusterCommand<Boolean>(connectionHandler,
timeout, maxRedirections) {
@Override
public Boolean execute(Jedis connection) {
return connection.scriptExists(sha1);
return connection.evalsha(script);
}
}.runScript(null);
}.runScript(1, key);
}

@Override
Expand All @@ -1578,21 +1558,7 @@ public Boolean scriptExists(final String sha1, final String key) {
public Boolean execute(Jedis connection) {
return connection.scriptExists(sha1);
}
}.runScript(key);
}

/**
* This method will not execute the script because no key is provided.
*/
@Override
public List<Boolean> scriptExists(final String... sha1) {
return new JedisClusterCommand<List<Boolean>>(connectionHandler,
timeout, maxRedirections) {
@Override
public List<Boolean> execute(Jedis connection) {
return connection.scriptExists(sha1);
}
}.runScript(null);
}.runScript(1,key);
}

@Override
Expand All @@ -1603,21 +1569,7 @@ public List<Boolean> scriptExists(final String key, final String... sha1) {
public List<Boolean> execute(Jedis connection) {
return connection.scriptExists(sha1);
}
}.runScript(key);
}

/**
* This method will not execute the script because no key is provided.
*/
@Override
public String scriptLoad(final String script) {
return new JedisClusterCommand<String>(connectionHandler,
timeout, maxRedirections) {
@Override
public String execute(Jedis connection) {
return connection.scriptLoad(script);
}
}.runScript(null);
}.runScript(1, key);
}

@Override
Expand All @@ -1628,7 +1580,7 @@ public String scriptLoad(final String script, final String key) {
public String execute(Jedis connection) {
return connection.scriptLoad(script);
}
}.runScript(key);
}.runScript(1, key);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/redis/clients/jedis/JedisClusterCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ public T run(String key) {
return runWithRetries(key, this.redirections, false, false);
}

public T runScript(String... keys) {
public T runScript(int keyCount, String... keys) {
if (keys == null || keys.length == 0) {
throw new JedisClusterException(
"No way to dispatch this command to Redis Cluster.");
}

// For multiple keys, only execute if they all share the
// For multiple keys, only execute if they all share the
// same connection slot.
if (keys.length > 1) {
int slot = JedisClusterCRC16.getSlot(keys[0]);
for (int i = 1; i < keys.length; i++) {
for (int i = 1; i < keyCount; i++) {
int nextSlot = JedisClusterCRC16.getSlot(keys[i]);
if (slot != nextSlot) {
throw new JedisClusterException(
"No way to dispatch this command to Redis Cluster" +
"No way to dispatch this command to Redis Cluster " +
"because keys have different slots.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@

import java.util.List;

public interface JedisClusterScriptingCommands extends ScriptingCommands{
public interface JedisClusterScriptingCommands {
Object eval(String script, int keyCount, String... params);

Object eval(String script, List<String> keys, List<String> args);

Object eval(String script, String key);

Object evalsha(String script, String key);

Object evalsha(String sha1, List<String> keys, List<String> args);

Object evalsha(String sha1, int keyCount, String... params);

Boolean scriptExists(String sha1, String key);

List<Boolean> scriptExists(String key, String... sha1);
Expand Down

0 comments on commit 4003ce2

Please sign in to comment.