@Override public RFuture<V> peekAsync() { return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_OBJECT, "local v = redis.call('lindex', KEYS[1], 0); " + "if v ~= false then " + "local randomId, value = struct.unpack('dLc0', v);" + "return value; " + "end " + "return nil;", Arrays.<Object>asList(queueName)); }
@Override public RFuture<List<V>> readAllAsync() { return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_LIST, "local result = {}; " + "local items = redis.call('lrange', KEYS[1], 0, -1); " + "for i, v in ipairs(items) do " + "local randomId, value = struct.unpack('dLc0', v); " + "table.insert(result, value);" + "end; " + "return result; ", Collections.<Object>singletonList(queueName)); }
@Override public RFuture<V> peekAsync() { return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_OBJECT, "local v = redis.call('lindex', KEYS[1], 0); " + "if v ~= false then " + "local randomId, value = struct.unpack('dLc0', v);" + "return value; " + "end " + "return nil;", Arrays.<Object>asList(queueName)); }
@Override public RFuture<List<V>> readAllAsync() { return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_LIST, "local result = {}; " + "local items = redis.call('lrange', KEYS[1], 0, -1); " + "for i, v in ipairs(items) do " + "local randomId, value = struct.unpack('dLc0', v); " + "table.insert(result, value);" + "end; " + "return result; ", Collections.<Object>singletonList(queueName)); }
V getValue(int index) { return (V)get(commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_OBJECT, "local v = redis.call('lindex', KEYS[1], ARGV[1]); " + "if v ~= false then " + "local randomId, value = struct.unpack('dLc0', v);" + "return value; " + "end " + "return nil;", Arrays.<Object>asList(queueName), index)); }
V getValue(int index) { return (V)get(commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_OBJECT, "local v = redis.call('lindex', KEYS[1], ARGV[1]); " + "if v ~= false then " + "local randomId, value = struct.unpack('dLc0', v);" + "return value; " + "end " + "return nil;", Arrays.<Object>asList(queueName), index)); }
@Override public RFuture<Long> lengthAsync() { return commandExecutor.evalReadAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_LONG, "local fromBit = redis.call('bitpos', KEYS[1], 1, -1);" + "local toBit = 8*(fromBit/8 + 1) - fromBit % 8;" + "for i = toBit, fromBit, -1 do " + "if redis.call('getbit', KEYS[1], i) == 1 then " + "return i+1;" + "end;" + "end;" + "return fromBit+1", Collections.<Object>singletonList(getName())); }
private <T, R> R evalRead(String key, Codec codec, RedisCommand<T> evalCommandType, String script, List<Object> keys, Object ... params) { RFuture<R> future = commandExecutor.evalReadAsync(key, codec, evalCommandType, script, keys, params); try { return get(future); } catch (Exception e) { throw new CacheException(e); } }
private <T, R> R evalRead(String key, Codec codec, RedisCommand<T> evalCommandType, String script, List<Object> keys, Object ... params) { RFuture<R> future = commandExecutor.evalReadAsync(key, codec, evalCommandType, script, keys, params); try { return get(future); } catch (Exception e) { throw new CacheException(e); } }
@Override public <R> RFuture<R> evalAsync(String key, Mode mode, String luaScript, ReturnType returnType, List<Object> keys, Object... values) { if (mode == Mode.READ_ONLY) { return commandExecutor.evalReadAsync(key, codec, returnType.getCommand(), luaScript, keys, encode(Arrays.asList(values), codec).toArray()); } return commandExecutor.evalWriteAsync(key, codec, returnType.getCommand(), luaScript, keys, encode(Arrays.asList(values), codec).toArray()); }
@Override public <R> RFuture<R> evalShaAsync(String key, Mode mode, String shaDigest, ReturnType returnType, List<Object> keys, Object... values) { RedisCommand command = new RedisCommand(returnType.getCommand(), "EVALSHA"); if (mode == Mode.READ_ONLY) { return commandExecutor.evalReadAsync(key, codec, command, shaDigest, keys, encode(Arrays.asList(values), codec).toArray()); } return commandExecutor.evalWriteAsync(key, codec, command, shaDigest, keys, encode(Arrays.asList(values), codec).toArray()); }
public <R> RFuture<R> indexOfAsync(Object o, Convertor<R> convertor) { return commandExecutor.evalReadAsync(getName(), codec, new RedisCommand<R>("EVAL", convertor), "local items = redis.call('lrange', KEYS[1], tonumber(ARGV[2]), tonumber(ARGV[3])) " + "for i=1,#items do " + "if items[i] == ARGV[1] then " + "return tonumber(ARGV[2]) + i - 1; " + "end; " + "end; " + "return -1; ", Collections.<Object>singletonList(getName()), encode(o), fromIndex, toIndex.get()-1); }
@Override public <R> RFuture<R> evalShaAsync(String key, Mode mode, String shaDigest, ReturnType returnType, List<Object> keys, Object... values) { RedisCommand command = new RedisCommand(returnType.getCommand(), "EVALSHA"); if (mode == Mode.READ_ONLY) { return commandExecutor.evalReadAsync(key, codec, command, shaDigest, keys, encode(Arrays.asList(values), codec).toArray()); } return commandExecutor.evalWriteAsync(key, codec, command, shaDigest, keys, encode(Arrays.asList(values), codec).toArray()); }
@Override public <R> RFuture<R> evalAsync(String key, Mode mode, String luaScript, ReturnType returnType, List<Object> keys, Object... values) { if (mode == Mode.READ_ONLY) { return commandExecutor.evalReadAsync(key, codec, returnType.getCommand(), luaScript, keys, encode(Arrays.asList(values), codec).toArray()); } return commandExecutor.evalWriteAsync(key, codec, returnType.getCommand(), luaScript, keys, encode(Arrays.asList(values), codec).toArray()); }
public <R> RFuture<R> indexOfAsync(Object o, Convertor<R> convertor) { return commandExecutor.evalReadAsync(getName(), codec, new RedisCommand<R>("EVAL", convertor), "local items = redis.call('lrange', KEYS[1], tonumber(ARGV[2]), tonumber(ARGV[3])) " + "for i=1,#items do " + "if items[i] == ARGV[1] then " + "return tonumber(ARGV[2]) + i - 1; " + "end; " + "end; " + "return -1; ", Collections.<Object>singletonList(getName()), encode(o), fromIndex, toIndex.get()-1); }
@Override public RFuture<Long> sizeAsync() { return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_LONG, "local parts = redis.call('get', KEYS[2]); " + "local lastPartName = KEYS[1];" + "if parts ~= false then " + "lastPartName = KEYS[1] .. ':' .. (tonumber(parts)-1);" + "local lastPartSize = redis.call('strlen', lastPartName);" + "return ((tonumber(parts)-1) * 536870912) + lastPartSize;" + "end;" + "return redis.call('strlen', lastPartName);", Arrays.<Object>asList(getName(), getPartsName())); }
@Override public RFuture<Long> sizeAsync() { return commandExecutor.evalReadAsync(getName(), codec, RedisCommands.EVAL_LONG, "local parts = redis.call('get', KEYS[2]); " + "local lastPartName = KEYS[1];" + "if parts ~= false then " + "lastPartName = KEYS[1] .. ':' .. (tonumber(parts)-1);" + "local lastPartSize = redis.call('strlen', lastPartName);" + "return ((tonumber(parts)-1) * 536870912) + lastPartSize;" + "end;" + "return redis.call('strlen', lastPartName);", Arrays.<Object>asList(getName(), getPartsName())); }
public <R> RFuture<R> lastIndexOfAsync(Object o, Convertor<R> convertor) { return commandExecutor.evalReadAsync(getName(), codec, new RedisCommand<R>("EVAL", convertor), "local key = KEYS[1] " + "local obj = ARGV[1] " + "local items = redis.call('lrange', key, 0, -1) " + "for i = #items, 1, -1 do " + "if items[i] == obj then " + "return i - 1 " + "end " + "end " + "return -1", Collections.<Object>singletonList(getName()), encode(o)); }
public <R> RFuture<R> indexOfAsync(Object o, Convertor<R> convertor) { return commandExecutor.evalReadAsync(getName(), codec, new RedisCommand<R>("EVAL", convertor), "local key = KEYS[1] " + "local obj = ARGV[1] " + "local items = redis.call('lrange', key, 0, -1) " + "for i=1,#items do " + "if items[i] == obj then " + "return i - 1 " + "end " + "end " + "return -1", Collections.<Object>singletonList(getName()), encode(o)); }
public <R> RFuture<R> lastIndexOfAsync(Object o, Convertor<R> convertor) { return commandExecutor.evalReadAsync(getName(), codec, new RedisCommand<R>("EVAL", convertor), "local key = KEYS[1] " + "local obj = ARGV[1] " + "local items = redis.call('lrange', key, 0, -1) " + "for i = #items, 1, -1 do " + "if items[i] == obj then " + "return i - 1 " + "end " + "end " + "return -1", Collections.<Object>singletonList(getName()), encode(o)); }