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

    loadComparator();

    commandExecutor.write(
        getName(), StringCodec.INSTANCE, RedisCommands.SETNX, getCurrentVersionKey(), 0L);
  }
Exemplo n.º 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);
         }
       });
 }
Exemplo n.º 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;
         }
       });
 }
Exemplo n.º 8
0
 private void loadComparator() {
   commandExecutor.read(
       getName(),
       codec,
       new SyncOperation<Void>() {
         @Override
         public Void execute(Codec codec, RedisConnection conn) {
           loadComparator(conn);
           return null;
         }
       });
 }
Exemplo n.º 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());
  }
Exemplo n.º 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;
  }
Exemplo n.º 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;
  }
Exemplo n.º 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);
 }
Exemplo n.º 13
0
 private V get(final int index) {
   return commandExecutor.read(getName(), codec, RedisCommands.LINDEX, getName(), index);
 }
Exemplo n.º 14
0
 @Override
 public int size() {
   return commandExecutor.read(getName(), codec, RedisCommands.LLEN_INT, getName());
 }
Exemplo n.º 15
0
 public RedissonBuckets(Redisson redisson, CommandExecutor commandExecutor) {
   this(redisson, commandExecutor.getConnectionManager().getCodec(), commandExecutor);
 }