Search in sources :

Example 1 with BucketsTrySetOperation

use of org.redisson.transaction.operation.bucket.BucketsTrySetOperation in project redisson by redisson.

the class RedissonTransactionalBuckets method trySetAsync.

// Add RKeys.deleteAsync support
// 
// public RFuture<Long> deleteAsync(String... keys) {
// checkState();
// RPromise<Long> result = new RedissonPromise<>();
// long threadId = Thread.currentThread().getId();
// executeLocked(result, new Runnable() {
// @Override
// public void run() {
// AtomicLong counter = new AtomicLong();
// AtomicLong executions = new AtomicLong(keys.length);
// for (String key : keys) {
// Object st = state.get(key);
// if (st != null) {
// operations.add(new DeleteOperation(key, getLockName(key), transactionId, threadId));
// if (st != NULL) {
// state.put(key, NULL);
// counter.incrementAndGet();
// }
// if (executions.decrementAndGet() == 0) {
// result.trySuccess(counter.get());
// }
// continue;
// }
// 
// RedissonKeys ks = new RedissonKeys(commandExecutor);
// ks.countExistsAsync(key).onComplete((res, e) -> {
// if (e != null) {
// result.tryFailure(e);
// return;
// }
// 
// if (res > 0) {
// operations.add(new DeleteOperation(key, getLockName(key), transactionId, threadId));
// state.put(key, NULL);
// counter.incrementAndGet();
// }
// 
// if (executions.decrementAndGet() == 0) {
// result.trySuccess(counter.get());
// }
// });
// }
// }
// }, Arrays.asList(keys));
// return result;
// }
@Override
public RFuture<Boolean> trySetAsync(Map<String, ?> buckets) {
    checkState();
    RPromise<Boolean> result = new RedissonPromise<>();
    executeLocked(result, () -> {
        Set<String> keysToSet = new HashSet<>();
        for (String key : buckets.keySet()) {
            Object value = state.get(key);
            if (value != null) {
                if (value != NULL) {
                    operations.add(new BucketsTrySetOperation(codec, (Map<String, Object>) buckets, transactionId));
                    result.trySuccess(false);
                    return;
                }
            } else {
                keysToSet.add(key);
            }
        }
        if (keysToSet.isEmpty()) {
            operations.add(new BucketsTrySetOperation(codec, (Map<String, Object>) buckets, transactionId));
            state.putAll(buckets);
            result.trySuccess(true);
            return;
        }
        RKeys keys = new RedissonKeys(commandExecutor);
        String[] ks = keysToSet.toArray(new String[keysToSet.size()]);
        keys.countExistsAsync(ks).onComplete((res, e) -> {
            if (e != null) {
                result.tryFailure(e);
                return;
            }
            operations.add(new BucketsTrySetOperation(codec, (Map<String, Object>) buckets, transactionId));
            if (res == 0) {
                state.putAll(buckets);
                result.trySuccess(true);
            } else {
                result.trySuccess(false);
            }
        });
    }, buckets.keySet());
    return result;
}
Also used : RedissonPromise(org.redisson.misc.RedissonPromise) RKeys(org.redisson.api.RKeys) BucketsTrySetOperation(org.redisson.transaction.operation.bucket.BucketsTrySetOperation) RedissonKeys(org.redisson.RedissonKeys) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 RedissonKeys (org.redisson.RedissonKeys)1 RKeys (org.redisson.api.RKeys)1 RedissonPromise (org.redisson.misc.RedissonPromise)1 BucketsTrySetOperation (org.redisson.transaction.operation.bucket.BucketsTrySetOperation)1