Search in sources :

Example 1 with RedissonMap

use of org.redisson.RedissonMap 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 2 with RedissonMap

use of org.redisson.RedissonMap in project redisson by redisson.

the class AsyncRemoteProxy method cancelExecution.

private void cancelExecution(RemoteInvocationOptions optionsCopy, boolean mayInterruptIfRunning, RemotePromise<Object> remotePromise, String cancelRequestMapName) {
    RMap<String, RemoteServiceCancelRequest> canceledRequests = new RedissonMap<>(new CompositeCodec(StringCodec.INSTANCE, codec, codec), commandExecutor, cancelRequestMapName, null, null, null);
    canceledRequests.fastPutAsync(remotePromise.getRequestId().toString(), new RemoteServiceCancelRequest(mayInterruptIfRunning, false));
    canceledRequests.expireAsync(60, TimeUnit.SECONDS);
    // subscribe for async result if it's not expected before
    if (!optionsCopy.isResultExpected()) {
        RemoteInvocationOptions options = new RemoteInvocationOptions(optionsCopy);
        options.expectResultWithin(60, TimeUnit.SECONDS);
        CompletionStage<RRemoteServiceResponse> responseFuture = pollResponse(options.getExecutionTimeoutInMillis(), remotePromise.getRequestId(), false);
        awaitResultAsync(options, remotePromise, responseFuture);
    }
}
Also used : CompositeCodec(org.redisson.codec.CompositeCodec) RedissonMap(org.redisson.RedissonMap) RemoteInvocationOptions(org.redisson.api.RemoteInvocationOptions)

Example 3 with RedissonMap

use of org.redisson.RedissonMap 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 4 with RedissonMap

use of org.redisson.RedissonMap in project redisson by redisson.

the class LiveObjectInterceptor method intercept.

@RuntimeType
public Object intercept(@Origin Method method, @AllArguments Object[] args, @This Object me, @FieldValue("liveObjectId") Object id, @FieldProxy("liveObjectId") Setter idSetter, @FieldProxy("liveObjectId") Getter idGetter, @FieldValue("liveObjectLiveMap") RMap<String, ?> map, @FieldProxy("liveObjectLiveMap") Setter mapSetter, @FieldProxy("liveObjectLiveMap") Getter mapGetter) throws Throwable {
    if ("setLiveObjectId".equals(method.getName())) {
        if (args[0].getClass().isArray()) {
            throw new UnsupportedOperationException("RId value cannot be an array.");
        }
        // TODO: distributed locking maybe required.
        String idKey = getMapKey(args[0]);
        if (map != null) {
            if (((RedissonObject) map).getRawName().equals(idKey)) {
                return null;
            }
            try {
                map.rename(getMapKey(args[0]));
            } catch (RedisException e) {
                if (e.getMessage() == null || !e.getMessage().startsWith("ERR no such key")) {
                    throw e;
                }
            // key may already renamed by others.
            }
        }
        RMap<Object, Object> liveMap = new RedissonMap<Object, Object>(namingScheme.getCodec(), commandExecutor, idKey, null, null, null);
        mapSetter.setValue(liveMap);
        return null;
    }
    if ("getLiveObjectId".equals(method.getName())) {
        if (map == null) {
            return null;
        }
        return namingScheme.resolveId(((RedissonObject) map).getRawName());
    }
    if ("delete".equals(method.getName())) {
        CommandBatchService ce;
        if (commandExecutor instanceof CommandBatchService) {
            ce = (CommandBatchService) commandExecutor;
        } else {
            ce = new CommandBatchService(commandExecutor);
        }
        Object idd = ((RLiveObject) me).getLiveObjectId();
        RFuture<Long> deleteFuture = service.delete(idd, me.getClass().getSuperclass(), namingScheme, ce);
        ce.execute();
        return deleteFuture.getNow() > 0;
    }
    try {
        return method.invoke(map, args);
    } catch (InvocationTargetException e) {
        throw e.getCause();
    }
}
Also used : RLiveObject(org.redisson.api.RLiveObject) RedisException(org.redisson.client.RedisException) CommandBatchService(org.redisson.command.CommandBatchService) RedissonObject(org.redisson.RedissonObject) RLiveObject(org.redisson.api.RLiveObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) RedissonMap(org.redisson.RedissonMap)

Aggregations

RedissonMap (org.redisson.RedissonMap)4 HashValue (org.redisson.misc.HashValue)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 RedissonObject (org.redisson.RedissonObject)1 RLiveObject (org.redisson.api.RLiveObject)1 RMap (org.redisson.api.RMap)1 RemoteInvocationOptions (org.redisson.api.RemoteInvocationOptions)1 RedisException (org.redisson.client.RedisException)1 CompositeCodec (org.redisson.codec.CompositeCodec)1 CommandBatchService (org.redisson.command.CommandBatchService)1 RedissonPromise (org.redisson.misc.RedissonPromise)1