RScript object added. #73

pull/139/head
Nikita 10 years ago
parent c88ed841ab
commit e60084c88c

@ -305,6 +305,13 @@ public class RedisAsyncConnection<K, V> extends ChannelInboundHandlerAdapter {
return dispatch(EVAL, output, args);
}
public <T> Future<T> eval(V script, ScriptOutputType type, List<K> keys, V... values) {
CommandArgs<K, V> args = new CommandArgs<K, V>(codec);
args.add(script.toString()).add(keys.size()).addKeys(keys).addValues(values);
CommandOutput<K, V, T> output = newScriptOutput(codec, type);
return dispatch(EVAL, output, args);
}
public <T> Future<T> evalsha(String digest, ScriptOutputType type, K[] keys, V... values) {
CommandArgs<K, V> args = new CommandArgs<K, V>(codec);
args.add(digest).add(keys.length).addKeys(keys).addValues(values);

@ -31,7 +31,7 @@ public class NestedMultiOutput<K, V> extends CommandOutput<K, V, List<Object>> {
@Override
public void set(ByteBuffer bytes) {
output.add(bytes == null ? null : codec.decodeValue(bytes));
output.add(bytes == null ? null : codec.decodeKey(bytes));
}
@Override

@ -6,6 +6,7 @@ import com.lambdaworks.redis.codec.RedisCodec;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import static java.lang.Math.max;
@ -51,6 +52,13 @@ public class CommandArgs<K, V> {
return write(codec.encodeKey(key));
}
public CommandArgs<K, V> addKeys(List<K> keys) {
for (K key : keys) {
addKey(key);
}
return this;
}
public CommandArgs<K, V> addKeys(K... keys) {
for (K key : keys) {
addKey(key);

@ -170,6 +170,15 @@ public class Redisson implements RedissonClient {
return new RedissonSet<V>(connectionManager, name);
}
/**
* Returns script with eval-operations support
*
* @return
*/
public RScript getScript() {
return new RedissonScript(connectionManager);
}
/**
* Returns distributed sorted set instance by name.
*

@ -0,0 +1,53 @@
/**
* Copyright 2014 Nikita Koksharov, Nickolay Borbit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.redisson;
import io.netty.util.concurrent.Future;
import java.util.List;
import org.redisson.async.ResultOperation;
import org.redisson.connection.ConnectionManager;
import org.redisson.core.RScript;
import com.lambdaworks.redis.RedisAsyncConnection;
import com.lambdaworks.redis.ScriptOutputType;
public class RedissonScript implements RScript {
private final ConnectionManager connectionManager;
public RedissonScript(ConnectionManager connectionManager) {
this.connectionManager = connectionManager;
}
@Override
public <R> R eval(final String luaScript, final ReturnType returnType, final List<Object> keys, final Object... values) {
return (R) connectionManager.get(evalAsync(luaScript, returnType, keys, values));
}
@Override
public <R> Future<R> evalAsync(final String luaScript, final ReturnType returnType, final List<Object> keys, final Object... values) {
return connectionManager.writeAsync(new ResultOperation<R, Object>() {
@Override
protected Future<R> execute(RedisAsyncConnection<Object, Object> async) {
return async.eval(luaScript, ScriptOutputType.valueOf(returnType.toString()), keys, values);
}
});
}
}

@ -0,0 +1,30 @@
/**
* Copyright 2014 Nikita Koksharov, Nickolay Borbit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.redisson.core;
import io.netty.util.concurrent.Future;
import java.util.List;
public interface RScript {
enum ReturnType {BOOLEAN, INTEGER, MULTI, STATUS, VALUE};
<R> Future<R> evalAsync(String luaScript, ReturnType returnType, List<Object> keys, Object... values);
<R> R eval(String luaScript, ReturnType returnType, List<Object> keys, Object... values);
}

@ -0,0 +1,30 @@
package org.redisson;
import io.netty.util.concurrent.Future;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.redisson.core.RScript;
public class RedissonScriptTest extends BaseTest {
@Test
public void testEval() {
RScript script = redisson.getScript();
List<Object> res = script.eval("return {1,2,3.3333,'foo',nil,'bar'}", RScript.ReturnType.MULTI, Collections.emptyList());
MatcherAssert.assertThat(res, Matchers.<Object>contains(1L, 2L, 3L, "foo"));
}
@Test
public void testEvalAsync() {
RScript script = redisson.getScript();
Future<List<Object>> res = script.evalAsync("return {1,2,3.3333,'foo',nil,'bar'}", RScript.ReturnType.MULTI, Collections.emptyList());
MatcherAssert.assertThat(res.awaitUninterruptibly().getNow(), Matchers.<Object>contains(1L, 2L, 3L, "foo"));
}
}
Loading…
Cancel
Save