Search in sources :

Example 1 with HashValue

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

the class TasksRunnerService method decode.

@SuppressWarnings("unchecked")
private <T> T decode(TaskParameters params) {
    ByteBuf classBodyBuf = Unpooled.wrappedBuffer(params.getClassBody());
    ByteBuf stateBuf = Unpooled.wrappedBuffer(params.getState());
    try {
        HashValue hash = new HashValue(Hash.hash128(classBodyBuf));
        Codec classLoaderCodec = CODECS.get(hash);
        if (classLoaderCodec == null) {
            RedissonClassLoader cl = new RedissonClassLoader(codec.getClassLoader());
            cl.loadClass(params.getClassName(), params.getClassBody());
            classLoaderCodec = this.codec.getClass().getConstructor(ClassLoader.class).newInstance(cl);
            CODECS.put(hash, classLoaderCodec);
        }
        T task;
        if (params.getLambdaBody() != null) {
            ByteArrayInputStream is = new ByteArrayInputStream(params.getLambdaBody());
            // set thread context class loader to be the classLoaderCodec.getClassLoader() variable as there could be reflection
            // done while reading from input stream which reflection will use thread class loader to load classes on demand
            ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
            try {
                Thread.currentThread().setContextClassLoader(classLoaderCodec.getClassLoader());
                ObjectInput oo = new CustomObjectInputStream(classLoaderCodec.getClassLoader(), is);
                task = (T) oo.readObject();
                oo.close();
            } finally {
                Thread.currentThread().setContextClassLoader(currentThreadClassLoader);
            }
        } else {
            task = (T) classLoaderCodec.getValueDecoder().decode(stateBuf, null);
        }
        Injector.inject(task, RedissonClient.class, redisson);
        Injector.inject(task, String.class, params.getRequestId());
        if (tasksInjector != null) {
            tasksInjector.inject(task);
        }
        return task;
    } catch (Exception e) {
        throw new IllegalStateException("Unable to initialize codec with ClassLoader parameter", e);
    } finally {
        classBodyBuf.release();
        stateBuf.release();
    }
}
Also used : Codec(org.redisson.client.codec.Codec) StringCodec(org.redisson.client.codec.StringCodec) LongCodec(org.redisson.client.codec.LongCodec) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) ByteBuf(io.netty.buffer.ByteBuf) HashValue(org.redisson.misc.HashValue) CustomObjectInputStream(org.redisson.codec.CustomObjectInputStream) RedisException(org.redisson.client.RedisException) RedissonShutdownException(org.redisson.RedissonShutdownException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with HashValue

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

the class BaseTransactionalMap method valueSizeAsync.

public RFuture<Integer> valueSizeAsync(K key) {
    HashValue keyHash = toKeyHash(key);
    MapEntry entry = state.get(keyHash);
    if (entry != null) {
        if (entry == MapEntry.NULL) {
            return RedissonPromise.newSucceededFuture(null);
        } else {
            ByteBuf valueState = ((RedissonObject) map).encodeMapValue(entry.getValue());
            try {
                return RedissonPromise.newSucceededFuture(valueState.readableBytes());
            } finally {
                valueState.release();
            }
        }
    }
    return map.valueSizeAsync(key);
}
Also used : RedissonObject(org.redisson.RedissonObject) HashValue(org.redisson.misc.HashValue) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with HashValue

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

the class BaseTransactionalMap method fastRemoveOperationAsync.

@SuppressWarnings("unchecked")
protected RFuture<Long> fastRemoveOperationAsync(K... keys) {
    RPromise<Long> result = new RedissonPromise<Long>();
    long threadId = Thread.currentThread().getId();
    executeLocked(result, new Runnable() {

        @Override
        public void run() {
            AtomicLong counter = new AtomicLong();
            List<K> keyList = Arrays.asList(keys);
            for (Iterator<K> iterator = keyList.iterator(); iterator.hasNext(); ) {
                K key = iterator.next();
                HashValue keyHash = toKeyHash(key);
                MapEntry currentValue = state.get(keyHash);
                if (currentValue != null && currentValue != MapEntry.NULL) {
                    operations.add(new MapFastRemoveOperation(map, key, transactionId, threadId));
                    state.put(keyHash, MapEntry.NULL);
                    counter.incrementAndGet();
                    iterator.remove();
                }
            }
            // TODO optimize
            map.getAllAsync(new HashSet<K>(keyList)).onComplete((res, e) -> {
                if (e != null) {
                    result.tryFailure(e);
                    return;
                }
                for (K key : res.keySet()) {
                    HashValue keyHash = toKeyHash(key);
                    operations.add(new MapFastRemoveOperation(map, key, transactionId, threadId));
                    counter.incrementAndGet();
                    state.put(keyHash, MapEntry.NULL);
                }
                result.trySuccess(counter.get());
            });
        }
    }, Arrays.asList(keys));
    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) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) HashValue(org.redisson.misc.HashValue)

Example 4 with HashValue

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

the class BaseTransactionalMap method getAllOperationAsync.

protected RFuture<Map<K, V>> getAllOperationAsync(Set<K> keys) {
    RPromise<Map<K, V>> result = new RedissonPromise<>();
    Set<K> keysToLoad = new HashSet<K>();
    Map<K, V> map = new HashMap<K, V>();
    for (K key : keys) {
        HashValue keyHash = toKeyHash(key);
        MapEntry entry = state.get(keyHash);
        if (entry != null) {
            if (entry != MapEntry.NULL) {
                map.put(key, (V) entry.getValue());
            }
        } else {
            keysToLoad.add(key);
        }
    }
    if (keysToLoad.isEmpty()) {
        return RedissonPromise.newSucceededFuture(map);
    }
    RFuture<Map<K, V>> future = ((RedissonMap<K, V>) this.map).getAllOperationAsync(keysToLoad);
    future.onComplete((res, e) -> {
        if (e != null) {
            result.tryFailure(e);
            return;
        }
        map.putAll(res);
        result.trySuccess(map);
    });
    return result;
}
Also used : RedissonPromise(org.redisson.misc.RedissonPromise) HashMap(java.util.HashMap) RedissonMap(org.redisson.RedissonMap) HashValue(org.redisson.misc.HashValue) HashMap(java.util.HashMap) RMap(org.redisson.api.RMap) Map(java.util.Map) RedissonMap(org.redisson.RedissonMap) HashSet(java.util.HashSet)

Example 5 with HashValue

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

the class BaseTransactionalMap method containsKeyAsync.

public RFuture<Boolean> containsKeyAsync(Object key) {
    HashValue keyHash = toKeyHash(key);
    MapEntry currentValue = state.get(keyHash);
    if (currentValue != null) {
        if (currentValue == MapEntry.NULL) {
            return RedissonPromise.newSucceededFuture(false);
        } else {
            return RedissonPromise.newSucceededFuture(true);
        }
    }
    return map.containsKeyAsync(key);
}
Also used : 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