コード例 #1
0
  @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);
  }
コード例 #2
0
 @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;
 }
コード例 #3
0
 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;
               }
             }
           }
         }
       });
 }
コード例 #4
0
 @Override
 public V last() {
   V res = commandExecutor.read(getName(), codec, RedisCommands.LINDEX, getName(), -1);
   if (res == null) {
     throw new NoSuchElementException();
   }
   return res;
 }
コード例 #5
0
  public RedissonSortedSet(Codec codec, CommandExecutor commandExecutor, String name) {
    super(codec, commandExecutor, name);

    loadComparator();

    commandExecutor.write(
        getName(), StringCodec.INSTANCE, RedisCommands.SETNX, getCurrentVersionKey(), 0L);
  }
コード例 #6
0
 @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);
         }
       });
 }
コード例 #7
0
 @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;
         }
       });
 }
コード例 #8
0
 private void loadComparator() {
   commandExecutor.read(
       getName(),
       codec,
       new SyncOperation<Void>() {
         @Override
         public Void execute(Codec codec, RedisConnection conn) {
           loadComparator(conn);
           return null;
         }
       });
 }
コード例 #9
0
  @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());
  }
コード例 #10
0
  @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;
  }
コード例 #11
0
  @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;
  }
コード例 #12
0
 @Override
 public <T> T[] toArray(T[] a) {
   List<V> res = commandExecutor.read(getName(), codec, RedisCommands.LRANGE, getName(), 0, -1);
   return res.toArray(a);
 }
コード例 #13
0
 private V get(final int index) {
   return commandExecutor.read(getName(), codec, RedisCommands.LINDEX, getName(), index);
 }
コード例 #14
0
 @Override
 public int size() {
   return commandExecutor.read(getName(), codec, RedisCommands.LLEN_INT, getName());
 }
コード例 #15
0
 public RedissonBuckets(Redisson redisson, CommandExecutor commandExecutor) {
   this(redisson, commandExecutor.getConnectionManager().getCodec(), commandExecutor);
 }