Search in sources :

Example 1 with VoidReplayConvertor

use of org.redisson.client.protocol.convertor.VoidReplayConvertor in project redisson by redisson.

the class RedissonBloomFilter method tryInit.

@Override
public boolean tryInit(long expectedInsertions, double falseProbability) {
    if (falseProbability > 1) {
        throw new IllegalArgumentException("Bloom filter false probability can't be greater than 1");
    }
    if (falseProbability < 0) {
        throw new IllegalArgumentException("Bloom filter false probability can't be negative");
    }
    size = optimalNumOfBits(expectedInsertions, falseProbability);
    if (size == 0) {
        throw new IllegalArgumentException("Bloom filter calculated size is " + size);
    }
    if (size > MAX_SIZE) {
        throw new IllegalArgumentException("Bloom filter size can't be greater than " + MAX_SIZE + ". But calculated size is " + size);
    }
    hashIterations = optimalNumOfHashFunctions(expectedInsertions, size);
    CommandBatchService executorService = new CommandBatchService(commandExecutor.getConnectionManager());
    executorService.evalReadAsync(getConfigName(), codec, RedisCommands.EVAL_VOID, "local size = redis.call('hget', KEYS[1], 'size');" + "local hashIterations = redis.call('hget', KEYS[1], 'hashIterations');" + "assert(size == false and hashIterations == false, 'Bloom filter config has been changed')", Arrays.<Object>asList(getConfigName()), size, hashIterations);
    executorService.writeAsync(getConfigName(), StringCodec.INSTANCE, new RedisCommand<Void>("HMSET", new VoidReplayConvertor()), getConfigName(), "size", size, "hashIterations", hashIterations, "expectedInsertions", expectedInsertions, "falseProbability", BigDecimal.valueOf(falseProbability).toPlainString());
    try {
        executorService.execute();
    } catch (RedisException e) {
        if (!e.getMessage().contains("Bloom filter config has been changed")) {
            throw e;
        }
        readConfig();
        return false;
    }
    return true;
}
Also used : VoidReplayConvertor(org.redisson.client.protocol.convertor.VoidReplayConvertor) CommandBatchService(org.redisson.command.CommandBatchService) RedisException(org.redisson.client.RedisException)

Aggregations

RedisException (org.redisson.client.RedisException)1 VoidReplayConvertor (org.redisson.client.protocol.convertor.VoidReplayConvertor)1 CommandBatchService (org.redisson.command.CommandBatchService)1