@Override public <V> Map<String, V> get(String... keys) { if (keys.length == 0) { return Collections.emptyMap(); } RedisCommand<Map<Object, Object>> command = new RedisCommand<Map<Object, Object>>( "MGET", new MapGetAllDecoder(Arrays.<Object>asList(keys), 0), ValueType.OBJECTS); Future<Map<String, V>> future = commandExecutor.readAsync(keys[0], new DelegateDecoderCodec(codec), command, keys); return commandExecutor.get(future); }
@Override public <V> List<RBucket<V>> find(String pattern) { Collection<String> keys = commandExecutor.get( commandExecutor.<List<String>, String>readAllAsync(RedisCommands.KEYS, pattern)); List<RBucket<V>> buckets = new ArrayList<RBucket<V>>(keys.size()); for (String key : keys) { if (key == null) { continue; } buckets.add(redisson.<V>getBucket(key, codec)); } return buckets; }
private void remove(final int index) { commandExecutor.write( getName(), codec, new SyncOperation<V>() { @Override public V execute(Codec codec, RedisConnection conn) { if (index == 0) { return conn.sync(codec, RedisCommands.LPOP, getName()); } while (true) { conn.sync(RedisCommands.WATCH, getName()); List<Object> tail = conn.sync(codec, RedisCommands.LRANGE, getName(), index + 1, size()); conn.sync(RedisCommands.MULTI); conn.sync(codec, RedisCommands.LTRIM, getName(), 0, index - 1); if (tail.isEmpty()) { if (((List<Object>) conn.sync(codec, RedisCommands.EXEC)).size() == 1) { return null; } } else { tail.add(0, getName()); conn.sync(codec, RedisCommands.RPUSH, tail.toArray()); if (((List<Object>) conn.sync(codec, RedisCommands.EXEC)).size() == 2) { return null; } } } } }); }
@Override public V last() { V res = commandExecutor.read(getName(), codec, RedisCommands.LINDEX, getName(), -1); if (res == null) { throw new NoSuchElementException(); } return res; }
public RedissonSortedSet(Codec codec, CommandExecutor commandExecutor, String name) { super(codec, commandExecutor, name); loadComparator(); commandExecutor.write( getName(), StringCodec.INSTANCE, RedisCommands.SETNX, getCurrentVersionKey(), 0L); }
@Override public boolean remove(final Object value) { return commandExecutor.write( getName(), codec, new SyncOperation<Boolean>() { @Override public Boolean execute(Codec codec, RedisConnection conn) { return remove(value, codec, conn); } }); }
@Override public boolean contains(final Object o) { return commandExecutor.read( getName(), codec, new SyncOperation<Boolean>() { @Override public Boolean execute(Codec codec, RedisConnection conn) { return binarySearch((V) o, codec, conn).getIndex() >= 0; } }); }
private void loadComparator() { commandExecutor.read( getName(), codec, new SyncOperation<Void>() { @Override public Void execute(Codec codec, RedisConnection conn) { loadComparator(conn); return null; } }); }
@Override public boolean trySet(Map<String, ?> buckets) { if (buckets.isEmpty()) { return false; } List<Object> params = new ArrayList<Object>(buckets.size()); for (Entry<String, ?> entry : buckets.entrySet()) { params.add(entry.getKey()); try { params.add(codec.getValueEncoder().encode(entry.getValue())); } catch (IOException e) { throw new IllegalArgumentException(e); } } return commandExecutor.write(params.get(0).toString(), RedisCommands.MSETNX, params.toArray()); }
@Override public boolean trySetComparator(Comparator<? super V> comparator) { String className = comparator.getClass().getName(); final String comparatorSign = className + ":" + calcClassSign(className); Boolean res = commandExecutor.evalWrite( getName(), StringCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, "if redis.call('llen', KEYS[1]) == 0 then redis.call('set', KEYS[2], ARGV[1]); return true; " + "else return false; end", Arrays.<Object>asList(getName(), getComparatorKeyName()), comparatorSign); if (res) { this.comparator = comparator; } return res; }
@Override public Future<Boolean> removeAsync(final V value) { EventLoopGroup group = commandExecutor.getConnectionManager().getGroup(); final Promise<Boolean> promise = group.next().newPromise(); group.execute( new Runnable() { @Override public void run() { try { boolean result = remove(value); promise.setSuccess(result); } catch (Exception e) { promise.setFailure(e); } } }); return promise; }
@Override public <T> T[] toArray(T[] a) { List<V> res = commandExecutor.read(getName(), codec, RedisCommands.LRANGE, getName(), 0, -1); return res.toArray(a); }
private V get(final int index) { return commandExecutor.read(getName(), codec, RedisCommands.LINDEX, getName(), index); }
@Override public int size() { return commandExecutor.read(getName(), codec, RedisCommands.LLEN_INT, getName()); }
public RedissonBuckets(Redisson redisson, CommandExecutor commandExecutor) { this(redisson, commandExecutor.getConnectionManager().getCodec(), commandExecutor); }