Search in sources :

Example 6 with HashValue

use of org.redisson.misc.HashValue in project redisson by redisson.

the class BaseTransactionalSet method scanIterator.

protected ScanResult<Object> scanIterator(String name, RedisClient client, long startPos, String pattern, int count) {
    ScanResult<Object> res = scanIteratorSource(name, client, startPos, pattern, count);
    Map<HashValue, Object> newstate = new HashMap<>(state);
    for (Iterator<Object> iterator = res.getValues().iterator(); iterator.hasNext(); ) {
        Object entry = iterator.next();
        Object value = newstate.remove(toHash(entry));
        if (value == NULL) {
            iterator.remove();
        }
    }
    if (startPos == 0) {
        for (Entry<HashValue, Object> entry : newstate.entrySet()) {
            if (entry.getValue() == NULL) {
                continue;
            }
            res.getValues().add(entry.getValue());
        }
    }
    return res;
}
Also used : RedissonObject(org.redisson.RedissonObject) HashValue(org.redisson.misc.HashValue)

Example 7 with HashValue

use of org.redisson.misc.HashValue in project redisson by redisson.

the class BaseTransactionalSet method moveAsync.

public RFuture<Boolean> moveAsync(String destination, V value) {
    RSet<V> destinationSet = new RedissonSet<V>(object.getCodec(), commandExecutor, destination, null);
    RPromise<Boolean> result = new RedissonPromise<Boolean>();
    RLock destinationLock = getLock(destinationSet, value);
    RLock lock = getLock(set, value);
    RedissonMultiLock multiLock = new RedissonMultiLock(destinationLock, lock);
    long threadId = Thread.currentThread().getId();
    multiLock.lockAsync(timeout, TimeUnit.MILLISECONDS).onComplete((res, e) -> {
        if (e != null) {
            multiLock.unlockAsync(threadId);
            result.tryFailure(e);
            return;
        }
        HashValue keyHash = toHash(value);
        Object currentValue = state.get(keyHash);
        if (currentValue != null) {
            operations.add(createMoveOperation(destination, value, threadId));
            if (currentValue == NULL) {
                result.trySuccess(false);
            } else {
                state.put(keyHash, NULL);
                result.trySuccess(true);
            }
            return;
        }
        set.containsAsync(value).onComplete((r, ex) -> {
            if (ex != null) {
                result.tryFailure(ex);
                return;
            }
            operations.add(createMoveOperation(destination, value, threadId));
            if (r) {
                state.put(keyHash, NULL);
            }
            result.trySuccess(r);
        });
    });
    return result;
}
Also used : RedissonSet(org.redisson.RedissonSet) RedissonPromise(org.redisson.misc.RedissonPromise) RedissonObject(org.redisson.RedissonObject) HashValue(org.redisson.misc.HashValue) RedissonMultiLock(org.redisson.RedissonMultiLock)

Example 8 with HashValue

use of org.redisson.misc.HashValue in project redisson by redisson.

the class BaseTransactionalMap method addAndGetOperationAsync.

protected RFuture<V> addAndGetOperationAsync(K key, Number value) {
    RPromise<V> result = new RedissonPromise<V>();
    long threadId = Thread.currentThread().getId();
    executeLocked(result, key, new Runnable() {

        @Override
        public void run() {
            HashValue keyHash = toKeyHash(key);
            MapEntry entry = state.get(keyHash);
            if (entry != null) {
                BigDecimal currentValue = BigDecimal.ZERO;
                if (entry != MapEntry.NULL) {
                    currentValue = (BigDecimal) entry.getValue();
                }
                BigDecimal res = currentValue.add(new BigDecimal(value.toString()));
                operations.add(new MapAddAndGetOperation(map, key, value, transactionId, threadId));
                state.put(keyHash, new MapEntry(key, res));
                if (deleted != null) {
                    deleted = false;
                }
                NumberConvertor convertor = new NumberConvertor(value.getClass());
                result.trySuccess((V) convertor.convert(res.toPlainString()));
                return;
            }
            map.getAsync(key).onComplete((r, e) -> {
                if (e != null) {
                    result.tryFailure(e);
                    return;
                }
                BigDecimal currentValue = new BigDecimal(r.toString());
                BigDecimal res = currentValue.add(new BigDecimal(value.toString()));
                operations.add(new MapAddAndGetOperation(map, key, value, transactionId, threadId));
                state.put(keyHash, new MapEntry(key, res));
                if (deleted != null) {
                    deleted = false;
                }
                NumberConvertor convertor = new NumberConvertor(value.getClass());
                result.trySuccess((V) convertor.convert(res.toPlainString()));
            });
        }
    });
    return result;
}
Also used : HashValue(org.redisson.misc.HashValue) RPromise(org.redisson.misc.RPromise) TransactionalOperation(org.redisson.transaction.operation.TransactionalOperation) Arrays(java.util.Arrays) CommandAsyncExecutor(org.redisson.command.CommandAsyncExecutor) UnlinkOperation(org.redisson.transaction.operation.UnlinkOperation) RedissonMultiLock(org.redisson.RedissonMultiLock) HashMap(java.util.HashMap) Hash(org.redisson.misc.Hash) org.redisson.transaction.operation.map(org.redisson.transaction.operation.map) RedisClient(org.redisson.client.RedisClient) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RFuture(org.redisson.api.RFuture) RMap(org.redisson.api.RMap) BigDecimal(java.math.BigDecimal) RedissonObject(org.redisson.RedissonObject) ByteBuf(io.netty.buffer.ByteBuf) ScanResult(org.redisson.ScanResult) TouchOperation(org.redisson.transaction.operation.TouchOperation) RLock(org.redisson.api.RLock) Map(java.util.Map) RedissonMap(org.redisson.RedissonMap) MapScanResult(org.redisson.client.protocol.decoder.MapScanResult) Iterator(java.util.Iterator) Collection(java.util.Collection) RedissonPromise(org.redisson.misc.RedissonPromise) Set(java.util.Set) DeleteOperation(org.redisson.transaction.operation.DeleteOperation) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) NumberConvertor(org.redisson.client.protocol.convertor.NumberConvertor) Entry(java.util.Map.Entry) RedissonPromise(org.redisson.misc.RedissonPromise) NumberConvertor(org.redisson.client.protocol.convertor.NumberConvertor) HashValue(org.redisson.misc.HashValue) BigDecimal(java.math.BigDecimal)

Example 9 with HashValue

use of org.redisson.misc.HashValue in project redisson by redisson.

the class BaseTransactionalMap method getOperationAsync.

protected RFuture<V> getOperationAsync(K key) {
    HashValue keyHash = toKeyHash(key);
    MapEntry entry = state.get(keyHash);
    if (entry != null) {
        if (entry == MapEntry.NULL) {
            return RedissonPromise.newSucceededFuture(null);
        } else {
            return RedissonPromise.newSucceededFuture((V) entry.getValue());
        }
    }
    return ((RedissonMap<K, V>) map).getOperationAsync(key);
}
Also used : HashValue(org.redisson.misc.HashValue) RedissonMap(org.redisson.RedissonMap)

Example 10 with HashValue

use of org.redisson.misc.HashValue in project redisson by redisson.

the class BaseTransactionalSet method readAllAsync.

public RFuture<Set<V>> readAllAsync() {
    RPromise<Set<V>> result = new RedissonPromise<>();
    RFuture<Set<V>> future = readAllAsyncSource();
    future.onComplete((res, e) -> {
        if (e != null) {
            result.tryFailure(e);
            return;
        }
        Map<HashValue, Object> newstate = new HashMap<>(state);
        for (Iterator<V> iterator = res.iterator(); iterator.hasNext(); ) {
            V key = iterator.next();
            Object value = newstate.remove(toHash(key));
            if (value == NULL) {
                iterator.remove();
            }
        }
        for (Object value : newstate.values()) {
            if (value == NULL) {
                continue;
            }
            res.add((V) value);
        }
        result.trySuccess(res);
    });
    return result;
}
Also used : RedissonPromise(org.redisson.misc.RedissonPromise) RedissonSet(org.redisson.RedissonSet) RedissonObject(org.redisson.RedissonObject) HashValue(org.redisson.misc.HashValue)

Aggregations

HashValue (org.redisson.misc.HashValue)10 RedissonObject (org.redisson.RedissonObject)6 RedissonPromise (org.redisson.misc.RedissonPromise)5 ByteBuf (io.netty.buffer.ByteBuf)4 RedissonMap (org.redisson.RedissonMap)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 RedissonMultiLock (org.redisson.RedissonMultiLock)3 RMap (org.redisson.api.RMap)3 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 Set (java.util.Set)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2