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)));
}
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;
}
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;
}
}
}
}
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;
}
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;
}
}
}
}
Aggregations