Search in sources :

Example 1 with CommandBatchService

use of org.redisson.command.CommandBatchService in project redisson by redisson.

the class RedissonBloomFilter method count.

@Override
public int count() {
    CommandBatchService executorService = new CommandBatchService(commandExecutor.getConnectionManager());
    RFuture<Map<String, String>> configFuture = executorService.readAsync(getConfigName(), StringCodec.INSTANCE, new RedisCommand<Map<Object, Object>>("HGETALL", new ObjectMapReplayDecoder()), getConfigName());
    RFuture<Long> cardinalityFuture = executorService.readAsync(getName(), codec, RedisCommands.BITCOUNT, getName());
    executorService.execute();
    readConfig(configFuture.getNow());
    return (int) (-size / ((double) hashIterations) * Math.log(1 - cardinalityFuture.getNow() / ((double) size)));
}
Also used : ObjectMapReplayDecoder(org.redisson.client.protocol.decoder.ObjectMapReplayDecoder) CommandBatchService(org.redisson.command.CommandBatchService) Map(java.util.Map)

Example 2 with CommandBatchService

use of org.redisson.command.CommandBatchService 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)

Example 3 with CommandBatchService

use of org.redisson.command.CommandBatchService in project redisson by redisson.

the class RedissonBloomFilter method contains.

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

Example 4 with CommandBatchService

use of org.redisson.command.CommandBatchService in project redisson by redisson.

the class RedissonKeys method deleteAsync.

@Override
public RFuture<Long> deleteAsync(String... keys) {
    if (!commandExecutor.getConnectionManager().isClusterMode()) {
        return commandExecutor.writeAsync(null, RedisCommands.DEL, keys);
    }
    Map<MasterSlaveEntry, List<String>> range2key = new HashMap<MasterSlaveEntry, List<String>>();
    for (String key : keys) {
        int slot = commandExecutor.getConnectionManager().calcSlot(key);
        for (MasterSlaveEntry entry : commandExecutor.getConnectionManager().getEntrySet()) {
            List<String> list = range2key.get(entry);
            if (list == null) {
                list = new ArrayList<String>();
                range2key.put(entry, list);
            }
            list.add(key);
        }
    }
    final RPromise<Long> result = commandExecutor.getConnectionManager().newPromise();
    final AtomicReference<Throwable> failed = new AtomicReference<Throwable>();
    final AtomicLong count = new AtomicLong();
    final AtomicLong executed = new AtomicLong(range2key.size());
    FutureListener<List<?>> listener = new FutureListener<List<?>>() {

        @Override
        public void operationComplete(Future<List<?>> future) throws Exception {
            if (future.isSuccess()) {
                List<Long> result = (List<Long>) future.get();
                for (Long res : result) {
                    count.addAndGet(res);
                }
            } else {
                failed.set(future.cause());
            }
            checkExecution(result, failed, count, executed);
        }
    };
    for (Entry<MasterSlaveEntry, List<String>> entry : range2key.entrySet()) {
        // executes in batch due to CROSSLOT error
        CommandBatchService executorService = new CommandBatchService(commandExecutor.getConnectionManager());
        for (String key : entry.getValue()) {
            executorService.writeAsync(entry.getKey(), null, RedisCommands.DEL, key);
        }
        RFuture<List<?>> future = executorService.executeAsync();
        future.addListener(listener);
    }
    return result;
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) HashMap(java.util.HashMap) CommandBatchService(org.redisson.command.CommandBatchService) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicLong(java.util.concurrent.atomic.AtomicLong) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) AtomicLong(java.util.concurrent.atomic.AtomicLong) RFuture(org.redisson.api.RFuture) Future(io.netty.util.concurrent.Future) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with CommandBatchService

use of org.redisson.command.CommandBatchService in project redisson by redisson.

the class RedissonBloomFilter method add.

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

Aggregations

CommandBatchService (org.redisson.command.CommandBatchService)5 List (java.util.List)3 RedisException (org.redisson.client.RedisException)3 Future (io.netty.util.concurrent.Future)1 FutureListener (io.netty.util.concurrent.FutureListener)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 RFuture (org.redisson.api.RFuture)1 VoidReplayConvertor (org.redisson.client.protocol.convertor.VoidReplayConvertor)1 ObjectMapReplayDecoder (org.redisson.client.protocol.decoder.ObjectMapReplayDecoder)1 MasterSlaveEntry (org.redisson.connection.MasterSlaveEntry)1