Java Code Examples for org.redisson.client.codec.StringCodec

The following examples show how to use org.redisson.client.codec.StringCodec. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: redisson   Source File: TasksRunnerService.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Check shutdown state. If tasksCounter equals <code>0</code>
 * and executor in <code>shutdown</code> state, then set <code>terminated</code> state 
 * and notify terminationTopicName
 * <p>
 * If <code>scheduledRequestId</code> is not null then
 * delete scheduled task
 * 
 * @param requestId
 */
private void finish(String requestId, boolean removeTask) {
    String script = "";
    if (removeTask) {
       script +=  "local scheduled = redis.call('zscore', KEYS[5], ARGV[3]);"
                + "if scheduled == false then "
                    + "redis.call('hdel', KEYS[4], ARGV[3]); "
                + "end;";
    }
    script += "redis.call('zrem', KEYS[5], 'ff' .. ARGV[3]);" +
              "if redis.call('decr', KEYS[1]) == 0 then "
               + "redis.call('del', KEYS[1]);"
                + "if redis.call('get', KEYS[2]) == ARGV[1] then "
                    + "redis.call('del', KEYS[6]);"
                    + "redis.call('set', KEYS[2], ARGV[2]);"
                    + "redis.call('publish', KEYS[3], ARGV[2]);"
                + "end;"
            + "end;";  

    commandExecutor.evalWrite(name, StringCodec.INSTANCE, RedisCommands.EVAL_VOID,
            script,
            Arrays.<Object>asList(tasksCounterName, statusName, terminationTopicName, tasksName, schedulerQueueName, tasksRetryIntervalName),
            RedissonExecutorService.SHUTDOWN_STATE, RedissonExecutorService.TERMINATED_STATE, requestId);
}
 
Example 2
Source Project: redisson   Source File: RedissonConnection.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Long dbSize() {
    if (isQueueing()) {
        return read(null, StringCodec.INSTANCE, RedisCommands.DBSIZE);
    }
    
    RFuture<Long> f = executorService.readAllAsync(RedisCommands.DBSIZE, new SlotCallback<Long, Long>() {
        AtomicLong results = new AtomicLong();
        @Override
        public void onSlotResult(Long result) {
            results.addAndGet(result);
        }

        @Override
        public Long onFinish() {
            return results.get();
        }
    });
    return sync(f);
}
 
Example 3
Source Project: redisson   Source File: RedissonListTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSortTo() {
    RList<String> list = redisson.getList("list", IntegerCodec.INSTANCE);
    list.add("1");
    list.add("2");
    list.add("3");
    
    assertThat(list.sortTo("test3", SortOrder.DESC)).isEqualTo(3);
    RList<String> list2 = redisson.getList("test3", StringCodec.INSTANCE);
    assertThat(list2).containsExactly("3", "2", "1");
    
    assertThat(list.sortTo("test4", SortOrder.ASC)).isEqualTo(3);
    RList<String> list3 = redisson.getList("test4", StringCodec.INSTANCE);
    assertThat(list3).containsExactly("1", "2", "3");

}
 
Example 4
@Test
public void testAddAndGet() throws InterruptedException {
    RScoredSortedSetReactive<Integer> set = redisson.getScoredSortedSet("simple", StringCodec.INSTANCE);
    sync(set.add(1, 100));

    Double res = sync(set.addScore(100, 11));
    Assert.assertEquals(12, (double)res, 0);
    Double score = sync(set.getScore(100));
    Assert.assertEquals(12, (double)score, 0);

    RScoredSortedSetReactive<Integer> set2 = redisson.getScoredSortedSet("simple", StringCodec.INSTANCE);
    sync(set2.add(100.2, 1));

    Double res2 = sync(set2.addScore(1, new Double(12.1)));
    Assert.assertTrue(new Double(112.3).compareTo(res2) == 0);
    res2 = sync(set2.getScore(1));
    Assert.assertTrue(new Double(112.3).compareTo(res2) == 0);
}
 
Example 5
Source Project: redisson   Source File: RedissonConnection.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public <T> T eval(byte[] script, ReturnType returnType, int numKeys, byte[]... keysAndArgs) {
    if (isQueueing()) {
        throw new UnsupportedOperationException();
    }
    if (isPipelined()) {
        throw new UnsupportedOperationException();
    }

    RedisCommand<?> c = toCommand(returnType, "EVAL");
    List<Object> params = new ArrayList<Object>();
    params.add(script);
    params.add(numKeys);
    params.addAll(Arrays.asList(keysAndArgs));
    return write(null, StringCodec.INSTANCE, c, params.toArray());
}
 
Example 6
@Override
public Flux<NumericResponse<PfAddCommand, Long>> pfAdd(Publisher<PfAddCommand> commands) {
    return execute(commands, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notEmpty(command.getValues(), "Values must not be empty!");

        byte[] keyBuf = toByteArray(command.getKey());

        List<Object> params = new ArrayList<Object>(command.getValues().size() + 1);
        params.add(keyBuf);
        params.addAll(command.getValues().stream().map(v -> toByteArray(v)).collect(Collectors.toList()));

        Mono<Long> m = write(keyBuf, StringCodec.INSTANCE, PFADD, params.toArray());
        return m.map(v -> new NumericResponse<>(command, v));
    });
}
 
Example 7
Source Project: redisson   Source File: RedissonConnection.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
    List<Object> args = new ArrayList<Object>(sets.length*2 + 5);
    args.add(destKey);
    args.add(sets.length);
    args.addAll(Arrays.asList(sets));
    if (weights != null) {
        args.add("WEIGHTS");
        for (int weight : weights) {
            args.add(weight);
        }
    }
    if (aggregate != null) {
        args.add("AGGREGATE");
        args.add(aggregate.name());
    }
    return write(destKey, StringCodec.INSTANCE, ZINTERSTORE, args.toArray());
}
 
Example 8
Source Project: redisson   Source File: RedissonConnection.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Long dbSize() {
    if (isQueueing()) {
        return read(null, StringCodec.INSTANCE, RedisCommands.DBSIZE);
    }
    
    RFuture<Long> f = executorService.readAllAsync(RedisCommands.DBSIZE, new SlotCallback<Long, Long>() {
        AtomicLong results = new AtomicLong();
        @Override
        public void onSlotResult(Long result) {
            results.addAndGet(result);
        }

        @Override
        public Long onFinish() {
            return results.get();
        }
    });
    return sync(f);
}
 
Example 9
Source Project: redisson   Source File: RedissonReactiveSetCommands.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Flux<NumericResponse<SUnionStoreCommand, Long>> sUnionStore(Publisher<SUnionStoreCommand> commands) {
    return execute(commands, command -> {

        Assert.notNull(command.getKeys(), "Keys must not be null!");
        Assert.notNull(command.getKey(), "Destination key must not be null!");

        List<Object> args = new ArrayList<Object>(command.getKeys().size() + 1);
        args.add(toByteArray(command.getKey()));
        args.addAll(command.getKeys().stream().map(v -> toByteArray(v)).collect(Collectors.toList()));

        Mono<Long> m = write((byte[])args.get(0), StringCodec.INSTANCE, RedisCommands.SUNIONSTORE, args.toArray());
        return m.map(v -> new NumericResponse<>(command, v));
    });
}
 
Example 10
Source Project: redisson   Source File: RedissonConnection.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Long zAdd(byte[] key, Set<Tuple> tuples) {
    List<Object> params = new ArrayList<Object>(tuples.size()*2+1);
    params.add(key);
    for (Tuple entry : tuples) {
        params.add(BigDecimal.valueOf(entry.getScore()).toPlainString());
        params.add(entry.getValue());
    }
    return write(key, StringCodec.INSTANCE, RedisCommands.ZADD, params.toArray());
}
 
Example 11
Source Project: redisson   Source File: RedissonReactiveHashCommands.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Flux<NumericResponse<KeyCommand, Long>> hLen(Publisher<KeyCommand> commands) {
    return execute(commands, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        
        byte[] keyBuf = toByteArray(command.getKey());
        Mono<Long> m = read(keyBuf, StringCodec.INSTANCE, RedisCommands.HLEN_LONG, keyBuf);
        return m.map(v -> new NumericResponse<>(command, v));
    });
}
 
Example 12
Source Project: redisson   Source File: RedissonReactiveZSetCommands.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Flux<NumericResponse<ZRemRangeByScoreCommand, Long>> zRemRangeByScore(
        Publisher<ZRemRangeByScoreCommand> commands) {
    return execute(commands, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getRange(), "Range must not be null!");

        byte[] keyBuf = toByteArray(command.getKey());
        Mono<Long> m = write(keyBuf, StringCodec.INSTANCE, ZREMRANGEBYSCORE, 
                            keyBuf, toLowerBound(command.getRange()),
                            toUpperBound(command.getRange()));
        return m.map(v -> new NumericResponse<>(command, v));
    });
}
 
Example 13
Source Project: redisson   Source File: RedissonStreamCommands.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Long xTrim(byte[] key, long count) {
    Assert.notNull(key, "Key must not be null!");
    Assert.notNull(count, "Count must not be null!");

    return connection.write(key, StringCodec.INSTANCE, RedisCommands.XTRIM, key, "MAXLEN", count);
}
 
Example 14
Source Project: redisson   Source File: RedissonReactiveSetCommands.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Flux<NumericResponse<SRemCommand, Long>> sRem(Publisher<SRemCommand> commands) {
    return execute(commands, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getValues(), "Values must not be null!");

        List<Object> args = new ArrayList<Object>(command.getValues().size() + 1);
        args.add(toByteArray(command.getKey()));
        args.addAll(command.getValues().stream().map(v -> toByteArray(v)).collect(Collectors.toList()));
        
        Mono<Long> m = write((byte[])args.get(0), StringCodec.INSTANCE, SREM, args.toArray());
        return m.map(v -> new NumericResponse<>(command, v));
    });
}
 
Example 15
Source Project: redisson   Source File: RedissonPriorityQueue.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean trySetComparator(Comparator<? super V> comparator) {
    String className = comparator.getClass().getName();
    String comparatorSign = className + ":" + calcClassSign(className);

    Boolean res = get(commandExecutor.writeAsync(getName(), StringCodec.INSTANCE, RedisCommands.SETNX, getComparatorKeyName(), comparatorSign));
    if (res) {
        this.comparator = comparator;
    }
    return res;
}
 
Example 16
Source Project: redisson   Source File: RedissonReactiveZSetCommands.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Flux<NumericResponse<ZRemCommand, Long>> zRem(Publisher<ZRemCommand> commands) {
    return execute(commands, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getValues(), "Values must not be null!");

        List<Object> args = new ArrayList<Object>(command.getValues().size() + 1);
        args.add(toByteArray(command.getKey()));
        args.addAll(command.getValues().stream().map(v -> toByteArray(v)).collect(Collectors.toList()));
        
        Mono<Long> m = write((byte[])args.get(0), StringCodec.INSTANCE, RedisCommands.ZREM_LONG, args.toArray());
        return m.map(v -> new NumericResponse<>(command, v));
    });
}
 
Example 17
Source Project: redisson   Source File: RedissonClusterConnection.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ClusterInfo clusterGetClusterInfo() {
    RFuture<Map<String, String>> f = executorService.readAsync((String)null, StringCodec.INSTANCE, RedisCommands.CLUSTER_INFO);
    syncFuture(f);

    Properties props = new Properties();
    for (Entry<String, String> entry : f.getNow().entrySet()) {
        props.setProperty(entry.getKey(), entry.getValue());
    }
    return new ClusterInfo(props);
}
 
Example 18
Source Project: redisson   Source File: RedissonReactiveKeyCommands.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Flux<NumericResponse<KeyCommand, Long>> del(Publisher<KeyCommand> keys) {
    Flux<KeyCommand> s = Flux.from(keys);
    return s.concatMap(command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");

        byte[] keyBuf = toByteArray(command.getKey());
        Mono<Long> m = write(keyBuf, StringCodec.INSTANCE, RedisCommands.DEL, keyBuf);
        return m.map(v -> new NumericResponse<>(command, v));
    });
}
 
Example 19
Source Project: redisson   Source File: RedissonAtomicDouble.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public RFuture<Boolean> compareAndSetAsync(double expect, double update) {
    return commandExecutor.evalWriteAsync(getName(), StringCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
              "local value = redis.call('get', KEYS[1]);"
            + "if (value == false and tonumber(ARGV[1]) == 0) or (tonumber(value) == tonumber(ARGV[1])) then "
                 + "redis.call('set', KEYS[1], ARGV[2]); "
                 + "return 1 "
               + "else "
                 + "return 0 end",
            Collections.<Object>singletonList(getName()), BigDecimal.valueOf(expect).toPlainString(), BigDecimal.valueOf(update).toPlainString());
}
 
Example 20
Source Project: redisson   Source File: RedissonConnection.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Long sDiffStore(byte[] destKey, byte[]... keys) {
    List<Object> args = new ArrayList<Object>(keys.length + 1);
    args.add(destKey);
    args.addAll(Arrays.asList(keys));
    return write(keys[0], StringCodec.INSTANCE, RedisCommands.SDIFFSTORE, args.toArray());
}
 
Example 21
Source Project: redisson   Source File: RedissonReactiveKeyCommands.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Flux<BooleanResponse<MoveCommand>> move(Publisher<MoveCommand> commands) {
    return execute(commands, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");
        Assert.notNull(command.getDatabase(), "Database must not be null!");

        byte[] keyBuf = toByteArray(command.getKey());
        Mono<Boolean> m = write(keyBuf, StringCodec.INSTANCE, RedisCommands.MOVE, keyBuf, command.getDatabase());
        return m.map(v -> new BooleanResponse<>(command, v));
    });
}
 
Example 22
Source Project: redisson   Source File: RedissonReactiveStringCommands.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Flux<NumericResponse<KeyCommand, Long>> strLen(Publisher<KeyCommand> keys) {
    return execute(keys, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");

        byte[] keyBuf = toByteArray(command.getKey());
        Mono<Long> m = read(keyBuf, StringCodec.INSTANCE, RedisCommands.STRLEN, keyBuf);
        return m.map(v -> new NumericResponse<>(command, v));
    });
}
 
Example 23
Source Project: redisson   Source File: RedissonReactiveKeyCommands.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Flux<BooleanResponse<ExpireCommand>> pExpire(Publisher<ExpireCommand> commands) {
    return execute(commands, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");

        byte[] keyBuf = toByteArray(command.getKey());
        Mono<Boolean> m = read(keyBuf, StringCodec.INSTANCE, RedisCommands.PEXPIRE, keyBuf);
        return m.map(v -> new BooleanResponse<>(command, v));
    });
}
 
Example 24
Source Project: redisson   Source File: RedissonReactiveKeyCommands.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Flux<NumericResponse<KeyCommand, Long>> pTtl(Publisher<KeyCommand> commands) {
    return execute(commands, command -> {

        Assert.notNull(command.getKey(), "Key must not be null!");

        byte[] keyBuf = toByteArray(command.getKey());
        Mono<Long> m = write(keyBuf, StringCodec.INSTANCE, RedisCommands.PTTL, keyBuf);
        return m.map(v -> new NumericResponse<>(command, v));
    });
}
 
Example 25
Source Project: redisson   Source File: RedissonConnection.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Long pfAdd(byte[] key, byte[]... values) {
    List<Object> params = new ArrayList<Object>(values.length + 1);
    params.add(key);
    for (byte[] member : values) {
        params.add(member);
    }

    return write(key, StringCodec.INSTANCE, PFADD, params.toArray());
}
 
Example 26
Source Project: redisson   Source File: RedissonConnection.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Long geoAdd(byte[] key, Map<byte[], Point> memberCoordinateMap) {
    List<Object> params = new ArrayList<Object>(memberCoordinateMap.size()*3 + 1);
    params.add(key);
    for (Entry<byte[], Point> entry : memberCoordinateMap.entrySet()) {
        params.add(entry.getValue().getX());
        params.add(entry.getValue().getY());
        params.add(entry.getKey());
    }
    return write(key, StringCodec.INSTANCE, RedisCommands.GEOADD, params.toArray());
}
 
Example 27
Source Project: redisson   Source File: RedissonReactiveKeyCommands.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Mono<ValueEncoding> encodingOf(ByteBuffer key) {
    Assert.notNull(key, "Key must not be null!");

    byte[] keyBuf = toByteArray(key);
    return read(keyBuf, StringCodec.INSTANCE, OBJECT_ENCODING, keyBuf);
}
 
Example 28
Source Project: redisson   Source File: RedissonConnection.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Long sAdd(byte[] key, byte[]... values) {
    List<Object> args = new ArrayList<Object>(values.length + 1);
    args.add(key);
    args.addAll(Arrays.asList(values));
    
    return write(key, StringCodec.INSTANCE, SADD, args.toArray());
}
 
Example 29
Source Project: redisson   Source File: RedissonClusterConnection.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ClusterInfo clusterGetClusterInfo() {
    RFuture<Map<String, String>> f = executorService.readAsync((String)null, StringCodec.INSTANCE, RedisCommands.CLUSTER_INFO);
    syncFuture(f);

    Properties props = new Properties();
    for (Entry<String, String> entry : f.getNow().entrySet()) {
        props.setProperty(entry.getKey(), entry.getValue());
    }
    return new ClusterInfo(props);
}
 
Example 30
Source Project: redisson   Source File: RedissonClusterConnection.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Long clusterCountKeysInSlot(int slot) {
    RedisClusterNode node = clusterGetNodeForSlot(slot);
    MasterSlaveEntry entry = executorService.getConnectionManager().getEntry(new InetSocketAddress(node.getHost(), node.getPort()));
    RFuture<Long> f = executorService.readAsync(entry, StringCodec.INSTANCE, RedisCommands.CLUSTER_COUNTKEYSINSLOT, slot);
    return syncFuture(f);
}