DATAREDIS-334 - Add binary alternative for RedisScriptingCommands.evalSha.
Introduced new evalSha method that accepts a byte[] as scriptSha as well as a variable number of byte[] as keyValueArgs. Original pull request: #99.
This commit is contained in:
committed by
Thomas Darimont
parent
9ecad07587
commit
4711693669
@@ -1230,6 +1230,14 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
|
||||
return result;
|
||||
}
|
||||
|
||||
public <T> T evalSha(byte[] scriptSha1, ReturnType returnType, int numKeys, byte[]... keysAndArgs) {
|
||||
T result = delegate.evalSha(scriptSha1, returnType, numKeys, keysAndArgs);
|
||||
if (isFutureConversion()) {
|
||||
addResultConverter(identityConverter);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// String methods
|
||||
//
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.List;
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author Christoph Strobl
|
||||
* @author David Liu
|
||||
*/
|
||||
public interface RedisScriptingCommands {
|
||||
|
||||
@@ -81,4 +82,17 @@ public interface RedisScriptingCommands {
|
||||
* @return
|
||||
*/
|
||||
<T> T evalSha(String scriptSha, ReturnType returnType, int numKeys, byte[]... keysAndArgs);
|
||||
|
||||
/**
|
||||
* Evaluate given {@code scriptSha}.
|
||||
*
|
||||
* @see http://redis.io/commands/evalsha
|
||||
* @param script
|
||||
* @param returnType
|
||||
* @param numKeys
|
||||
* @param keysAndArgs
|
||||
* @return
|
||||
* @since 1.5
|
||||
*/
|
||||
<T> T evalSha(byte[] scriptSha, ReturnType returnType, int numKeys, byte[]... keysAndArgs);
|
||||
}
|
||||
|
||||
@@ -87,6 +87,7 @@ import redis.clients.util.Pool;
|
||||
* @author Thomas Darimont
|
||||
* @author Jungtaek Lim
|
||||
* @author Konstantin Shchepanovskyi
|
||||
* @author David Liu
|
||||
*/
|
||||
public class JedisConnection extends AbstractRedisConnection {
|
||||
|
||||
@@ -2799,8 +2800,13 @@ public class JedisConnection extends AbstractRedisConnection {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T evalSha(String scriptSha1, ReturnType returnType, int numKeys, byte[]... keysAndArgs) {
|
||||
return evalSha(JedisConverters.toBytes(scriptSha1), returnType, numKeys, keysAndArgs);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T evalSha(byte[] scriptSha1, ReturnType returnType, int numKeys, byte[]... keysAndArgs) {
|
||||
|
||||
if (isQueueing()) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
@@ -2808,8 +2814,7 @@ public class JedisConnection extends AbstractRedisConnection {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
try {
|
||||
return (T) new JedisScriptReturnConverter(returnType).convert(jedis.evalsha(JedisConverters.toBytes(scriptSha1),
|
||||
numKeys, keysAndArgs));
|
||||
return (T) new JedisScriptReturnConverter(returnType).convert(jedis.evalsha(scriptSha1, numKeys, keysAndArgs));
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
|
||||
@@ -58,6 +58,7 @@ import org.springframework.util.ReflectionUtils;
|
||||
* @author Jennifer Hickey
|
||||
* @author Christoph Strobl
|
||||
* @author Thomas Darimont
|
||||
* @author David Liu
|
||||
*/
|
||||
public class JredisConnection extends AbstractRedisConnection {
|
||||
|
||||
@@ -1183,6 +1184,10 @@ public class JredisConnection extends AbstractRedisConnection {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public <T> T evalSha(byte[] scriptSha1, ReturnType returnType, int numKeys, byte[]... keysAndArgs) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.RedisServerCommands#time()
|
||||
|
||||
@@ -99,6 +99,7 @@ import com.lambdaworks.redis.pubsub.RedisPubSubConnection;
|
||||
* @author Jennifer Hickey
|
||||
* @author Christoph Strobl
|
||||
* @author Thomas Darimont
|
||||
* @author David Liu
|
||||
*/
|
||||
public class LettuceConnection extends AbstractRedisConnection {
|
||||
|
||||
@@ -2849,6 +2850,10 @@ public class LettuceConnection extends AbstractRedisConnection {
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T evalSha(byte[] scriptSha1, ReturnType returnType, int numKeys, byte[]... keysAndArgs) {
|
||||
return evalSha(LettuceConverters.toString(scriptSha1), returnType, numKeys, keysAndArgs);
|
||||
}
|
||||
|
||||
//
|
||||
// Pub/Sub functionality
|
||||
//
|
||||
|
||||
@@ -68,6 +68,7 @@ import com.google.common.util.concurrent.ListenableFuture;
|
||||
* @author Jennifer Hickey
|
||||
* @author Christoph Strobl
|
||||
* @author Thomas Darimont
|
||||
* @author David Liu
|
||||
*/
|
||||
public class SrpConnection extends AbstractRedisConnection {
|
||||
|
||||
@@ -2100,6 +2101,10 @@ public class SrpConnection extends AbstractRedisConnection {
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T evalSha(byte[] scriptSha1, ReturnType returnType, int numKeys, byte[]... keysAndArgs) {
|
||||
return evalSha(SrpConverters.toString(scriptSha1), returnType, numKeys, keysAndArgs);
|
||||
}
|
||||
|
||||
//
|
||||
// Pub/Sub functionality
|
||||
//
|
||||
|
||||
@@ -188,6 +188,20 @@ public abstract class AbstractConnectionIntegrationTests {
|
||||
Arrays.asList(new Object[] { new String(scriptResults.get(0)), new String(scriptResults.get(1)) }));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
public void testEvalShaArrayBytes() {
|
||||
getResults();
|
||||
byte[] sha1 = connection.scriptLoad("return {KEYS[1],ARGV[1]}").getBytes();
|
||||
initConnection();
|
||||
actual.add(byteConnection.evalSha(sha1, ReturnType.MULTI, 1, "key1".getBytes(), "arg1".getBytes()));
|
||||
List<Object> results = getResults();
|
||||
List<byte[]> scriptResults = (List<byte[]>) results.get(0);
|
||||
assertEquals(Arrays.asList(new Object[] { "key1", "arg1" }),
|
||||
Arrays.asList(new Object[] { new String(scriptResults.get(0)), new String(scriptResults.get(1)) }));
|
||||
}
|
||||
|
||||
@Test(expected = RedisSystemException.class)
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
public void testEvalShaArrayError() {
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @author David Liu
|
||||
*/
|
||||
public class RedisConnectionUnitTests {
|
||||
|
||||
@@ -784,5 +785,10 @@ public class RedisConnectionUnitTests {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T evalSha(byte[] scriptSha, ReturnType returnType, int numKeys, byte[]... keysAndArgs) {
|
||||
return delegate.evalSha(scriptSha, returnType, numKeys, keysAndArgs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.data.redis.connection.jedis;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -87,6 +88,20 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
|
||||
connection = null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
public void testEvalShaArrayBytes() {
|
||||
getResults();
|
||||
byte[] sha1 = connection.scriptLoad("return {KEYS[1],ARGV[1]}").getBytes();
|
||||
initConnection();
|
||||
actual.add(byteConnection.evalSha(sha1, ReturnType.MULTI, 1, "key1".getBytes(), "arg1".getBytes()));
|
||||
List<Object> results = getResults();
|
||||
List<byte[]> scriptResults = (List<byte[]>) results.get(0);
|
||||
assertEquals(Arrays.asList(new Object[] { "key1", "arg1" }),
|
||||
Arrays.asList(new Object[] { new String(scriptResults.get(0)), new String(scriptResults.get(1)) }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateConnectionWithDb() {
|
||||
JedisConnectionFactory factory2 = new JedisConnectionFactory();
|
||||
|
||||
@@ -135,6 +135,12 @@ public class JedisConnectionPipelineIntegrationTests extends AbstractConnectionP
|
||||
public void testEvalShaArrayStrings() {
|
||||
super.testEvalShaArrayStrings();
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
public void testEvalShaArrayBytes() {
|
||||
super.testEvalShaArrayBytes();
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
|
||||
@@ -19,6 +19,7 @@ import org.junit.After;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.redis.connection.AbstractConnectionTransactionIntegrationTests;
|
||||
import org.springframework.data.redis.test.util.RelaxedJUnit4ClassRunner;
|
||||
@@ -66,6 +67,12 @@ public class JedisConnectionTransactionIntegrationTests extends AbstractConnecti
|
||||
public void testEvalShaArrayStrings() {
|
||||
super.testEvalShaArrayStrings();
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
public void testEvalShaArrayBytes() {
|
||||
super.testEvalShaArrayBytes();
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
|
||||
@@ -435,6 +435,12 @@ public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrat
|
||||
public void testEvalShaArrayStrings() {
|
||||
super.testEvalShaArrayStrings();
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
public void testEvalShaArrayBytes() {
|
||||
super.testEvalShaArrayBytes();
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
|
||||
@@ -297,4 +297,19 @@ public class LettuceConnectionIntegrationTests extends AbstractConnectionIntegra
|
||||
AllOf.allOf(IsInstanceOf.instanceOf(List.class), IsCollectionContaining.hasItems("awesome".getBytes(),
|
||||
"cool".getBytes(), "supercalifragilisticexpialidocious".getBytes())));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
public void testEvalShaArrayBytes() {
|
||||
getResults();
|
||||
byte[] sha1 = connection.scriptLoad("return {KEYS[1],ARGV[1]}").getBytes();
|
||||
initConnection();
|
||||
actual.add(byteConnection.evalSha(sha1, ReturnType.MULTI, 1, "key1".getBytes(), "arg1".getBytes()));
|
||||
List<Object> results = getResults();
|
||||
List<byte[]> scriptResults = (List<byte[]>) results.get(0);
|
||||
assertEquals(Arrays.asList(new Object[] { "key1", "arg1" }),
|
||||
Arrays.asList(new Object[] { new String(scriptResults.get(0)), new String(scriptResults.get(1)) }));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,7 +16,10 @@
|
||||
|
||||
package org.springframework.data.redis.connection.srp;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.core.Is;
|
||||
import org.hamcrest.core.IsInstanceOf;
|
||||
@@ -99,4 +102,19 @@ public class SrpConnectionIntegrationTests extends AbstractConnectionIntegration
|
||||
Assert.assertThat(replies[1].data(), Is.<Object> is("cool".getBytes()));
|
||||
Assert.assertThat(replies[2].data(), Is.<Object> is("supercalifragilisticexpialidocious".getBytes()));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
public void testEvalShaArrayBytes() {
|
||||
getResults();
|
||||
byte[] sha1 = connection.scriptLoad("return {KEYS[1],ARGV[1]}").getBytes();
|
||||
initConnection();
|
||||
actual.add(connection.evalSha(sha1, ReturnType.MULTI, 1, "key1".getBytes(), "arg1".getBytes()));
|
||||
List<Object> results = getResults();
|
||||
List<byte[]> scriptResults = (List<byte[]>) results.get(0);
|
||||
assertEquals(Arrays.asList(new Object[] { "key1", "arg1" }),
|
||||
Arrays.asList(new Object[] { new String(scriptResults.get(0)), new String(scriptResults.get(1)) }));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,7 +15,10 @@
|
||||
*/
|
||||
package org.springframework.data.redis.connection.srp;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -68,4 +71,19 @@ public class SrpConnectionPipelineIntegrationTests extends AbstractConnectionPip
|
||||
public void testZUnionStoreAggWeights() {
|
||||
super.testZUnionStoreAggWeights();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
public void testEvalShaArrayBytes() {
|
||||
getResults();
|
||||
byte[] sha1 = connection.scriptLoad("return {KEYS[1],ARGV[1]}").getBytes();
|
||||
initConnection();
|
||||
actual.add(byteConnection.evalSha(sha1, ReturnType.MULTI, 1, "key1".getBytes(), "arg1".getBytes()));
|
||||
List<Object> results = getResults();
|
||||
List<byte[]> scriptResults = (List<byte[]>) results.get(0);
|
||||
assertEquals(Arrays.asList(new Object[] { "key1", "arg1" }),
|
||||
Arrays.asList(new Object[] { new String(scriptResults.get(0)), new String(scriptResults.get(1)) }));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user