Search in sources :

Example 1 with RBitSetAsync

use of org.redisson.api.RBitSetAsync in project redisson by redisson.

the class RedissonBloomFilter method count.

@Override
public long count() {
    CommandBatchService executorService = new CommandBatchService(commandExecutor);
    RFuture<Map<String, String>> configFuture = executorService.readAsync(configName, StringCodec.INSTANCE, new RedisCommand<Map<Object, Object>>("HGETALL", new ObjectMapReplayDecoder()), configName);
    RBitSetAsync bs = createBitSet(executorService);
    RFuture<Long> cardinalityFuture = bs.cardinalityAsync();
    executorService.execute();
    readConfig(configFuture.getNow());
    return Math.round(-size / ((double) hashIterations) * Math.log(1 - cardinalityFuture.getNow() / ((double) size)));
}
Also used : RBitSetAsync(org.redisson.api.RBitSetAsync) ObjectMapReplayDecoder(org.redisson.client.protocol.decoder.ObjectMapReplayDecoder) CommandBatchService(org.redisson.command.CommandBatchService) Map(java.util.Map)

Example 2 with RBitSetAsync

use of org.redisson.api.RBitSetAsync in project redisson by redisson.

the class RedissonBloomFilter method contains.

@Override
public boolean contains(T object) {
    long[] hashes = hash(object);
    while (true) {
        if (size == 0) {
            readConfig();
        }
        int hashIterations = this.hashIterations;
        long size = this.size;
        long[] indexes = hash(hashes[0], hashes[1], hashIterations, size);
        CommandBatchService executorService = new CommandBatchService(commandExecutor);
        addConfigCheck(hashIterations, size, executorService);
        RBitSetAsync bs = createBitSet(executorService);
        for (int i = 0; i < indexes.length; i++) {
            bs.getAsync(indexes[i]);
        }
        try {
            List<Boolean> result = (List<Boolean>) executorService.execute().getResponses();
            for (Boolean val : result.subList(1, result.size() - 1)) {
                if (!val) {
                    return false;
                }
            }
            return true;
        } catch (RedisException e) {
            if (e.getMessage() == null || !e.getMessage().contains("Bloom filter config has been changed")) {
                throw e;
            }
        }
    }
}
Also used : RBitSetAsync(org.redisson.api.RBitSetAsync) CommandBatchService(org.redisson.command.CommandBatchService) RedisException(org.redisson.client.RedisException) List(java.util.List)

Example 3 with RBitSetAsync

use of org.redisson.api.RBitSetAsync in project redisson by redisson.

the class RedissonBloomFilter method add.

@Override
public boolean add(T object) {
    long[] hashes = hash(object);
    while (true) {
        if (size == 0) {
            readConfig();
        }
        int hashIterations = this.hashIterations;
        long size = this.size;
        long[] indexes = hash(hashes[0], hashes[1], hashIterations, size);
        CommandBatchService executorService = new CommandBatchService(commandExecutor);
        addConfigCheck(hashIterations, size, executorService);
        RBitSetAsync bs = createBitSet(executorService);
        for (int i = 0; i < indexes.length; i++) {
            bs.setAsync(indexes[i]);
        }
        try {
            List<Boolean> result = (List<Boolean>) executorService.execute().getResponses();
            for (Boolean val : result.subList(1, result.size() - 1)) {
                if (!val) {
                    return true;
                }
            }
            return false;
        } catch (RedisException e) {
            if (e.getMessage() == null || !e.getMessage().contains("Bloom filter config has been changed")) {
                throw e;
            }
        }
    }
}
Also used : RBitSetAsync(org.redisson.api.RBitSetAsync) CommandBatchService(org.redisson.command.CommandBatchService) RedisException(org.redisson.client.RedisException) List(java.util.List)

Aggregations

RBitSetAsync (org.redisson.api.RBitSetAsync)3 CommandBatchService (org.redisson.command.CommandBatchService)3 List (java.util.List)2 RedisException (org.redisson.client.RedisException)2 Map (java.util.Map)1 ObjectMapReplayDecoder (org.redisson.client.protocol.decoder.ObjectMapReplayDecoder)1