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