Search in sources :

Example 6 with RedissonReference

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

the class CommandBatchService method executeAsync.

public RFuture<List<?>> executeAsync() {
    if (executed) {
        throw new IllegalStateException("Batch already executed!");
    }
    if (commands.isEmpty()) {
        return connectionManager.newSucceededFuture(null);
    }
    executed = true;
    RPromise<Void> voidPromise = connectionManager.newPromise();
    final RPromise<List<?>> promise = connectionManager.newPromise();
    voidPromise.addListener(new FutureListener<Void>() {

        @Override
        public void operationComplete(Future<Void> future) throws Exception {
            if (!future.isSuccess()) {
                promise.tryFailure(future.cause());
                commands = null;
                return;
            }
            List<BatchCommandData> entries = new ArrayList<BatchCommandData>();
            for (Entry e : commands.values()) {
                entries.addAll(e.getCommands());
            }
            Collections.sort(entries);
            List<Object> result = new ArrayList<Object>(entries.size());
            for (BatchCommandData<?, ?> commandEntry : entries) {
                Object entryResult = commandEntry.getPromise().getNow();
                if (isRedissonReferenceSupportEnabled() && entryResult instanceof RedissonReference) {
                    result.add(redisson != null ? RedissonObjectFactory.<Object>fromReference(redisson, (RedissonReference) entryResult) : RedissonObjectFactory.<Object>fromReference(redissonReactive, (RedissonReference) entryResult));
                } else {
                    result.add(entryResult);
                }
            }
            promise.trySuccess(result);
            commands = null;
        }
    });
    AtomicInteger slots = new AtomicInteger(commands.size());
    for (java.util.Map.Entry<MasterSlaveEntry, Entry> e : commands.entrySet()) {
        execute(e.getValue(), new NodeSource(e.getKey()), voidPromise, slots, 0, false);
    }
    return promise;
}
Also used : RedissonReference(org.redisson.RedissonReference) BatchCommandData(org.redisson.client.protocol.BatchCommandData) RedisMovedException(org.redisson.client.RedisMovedException) RedisAskException(org.redisson.client.RedisAskException) WriteRedisConnectionException(org.redisson.client.WriteRedisConnectionException) RedisLoadingException(org.redisson.client.RedisLoadingException) RedisTryAgainException(org.redisson.client.RedisTryAgainException) RedisTimeoutException(org.redisson.client.RedisTimeoutException) NodeSource(org.redisson.connection.NodeSource) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 7 with RedissonReference

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

the class CommandBatchService method async.

@Override
protected <V, R> void async(boolean readOnlyMode, NodeSource nodeSource, Codec codec, RedisCommand<V> command, Object[] params, RPromise<R> mainPromise, int attempt) {
    if (executed) {
        throw new IllegalStateException("Batch already has been executed!");
    }
    Entry entry = commands.get(nodeSource.getEntry());
    if (entry == null) {
        entry = new Entry();
        Entry oldEntry = commands.putIfAbsent(nodeSource.getEntry(), entry);
        if (oldEntry != null) {
            entry = oldEntry;
        }
    }
    if (!readOnlyMode) {
        entry.setReadOnlyMode(false);
    }
    if (isRedissonReferenceSupportEnabled()) {
        for (int i = 0; i < params.length; i++) {
            RedissonReference reference = redisson != null ? RedissonObjectFactory.toReference(redisson, params[i]) : RedissonObjectFactory.toReference(redissonReactive, params[i]);
            if (reference != null) {
                params[i] = reference;
            }
        }
    }
    BatchCommandData<V, R> commandData = new BatchCommandData<V, R>(mainPromise, codec, command, params, index.incrementAndGet());
    entry.getCommands().add(commandData);
}
Also used : MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) RedissonReference(org.redisson.RedissonReference) BatchCommandData(org.redisson.client.protocol.BatchCommandData)

Example 8 with RedissonReference

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

the class AccessorInterceptor method intercept.

@RuntimeType
public Object intercept(@Origin Method method, @SuperCall Callable<?> superMethod, @AllArguments Object[] args, @This Object me, @FieldValue("liveObjectLiveMap") RMap<String, Object> liveMap) throws Exception {
    if (isGetter(method, getREntityIdFieldName(me))) {
        return ((RLiveObject) me).getLiveObjectId();
    }
    if (isSetter(method, getREntityIdFieldName(me))) {
        ((RLiveObject) me).setLiveObjectId(args[0]);
        return null;
    }
    String fieldName = getFieldName(method);
    Class<?> fieldType = me.getClass().getSuperclass().getDeclaredField(fieldName).getType();
    if (isGetter(method, fieldName)) {
        Object result = liveMap.get(fieldName);
        if (result == null) {
            RObject ar = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), fieldType, fieldName);
            if (ar != null) {
                objectBuilder.store(ar, fieldName, liveMap);
                return ar;
            }
        }
        if (result instanceof RedissonReference) {
            return RedissonObjectFactory.fromReference(redisson, (RedissonReference) result);
        }
        return result;
    }
    if (isSetter(method, fieldName)) {
        Object arg = args[0];
        if (arg != null && arg.getClass().isAnnotationPresent(REntity.class)) {
            throw new IllegalStateException("REntity object should be attached to Redisson first");
        }
        if (arg instanceof RLiveObject) {
            RLiveObject liveObject = (RLiveObject) arg;
            Class<? extends Object> rEntity = liveObject.getClass().getSuperclass();
            REntity anno = rEntity.getAnnotation(REntity.class);
            NamingScheme ns = anno.namingScheme().getDeclaredConstructor(Codec.class).newInstance(codecProvider.getCodec(anno, (Class) rEntity));
            liveMap.fastPut(fieldName, new RedissonReference(rEntity, ns.getName(rEntity, fieldType, getREntityIdFieldName(liveObject), liveObject.getLiveObjectId())));
            return me;
        }
        if (!(arg instanceof RObject) && (arg instanceof Collection || arg instanceof Map) && TransformationMode.ANNOTATION_BASED.equals(me.getClass().getSuperclass().getAnnotation(REntity.class).fieldTransformation())) {
            RObject rObject = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), arg.getClass(), fieldName);
            if (arg != null) {
                if (rObject instanceof Collection) {
                    Collection c = (Collection) rObject;
                    c.clear();
                    c.addAll((Collection) arg);
                } else {
                    Map m = (Map) rObject;
                    m.clear();
                    m.putAll((Map) arg);
                }
            }
            if (rObject != null) {
                arg = rObject;
            }
        }
        if (arg instanceof RObject) {
            objectBuilder.store((RObject) arg, fieldName, liveMap);
            return me;
        }
        if (arg == null) {
            liveMap.remove(fieldName);
        } else {
            liveMap.fastPut(fieldName, arg);
        }
        return me;
    }
    return superMethod.call();
}
Also used : RedissonReference(org.redisson.RedissonReference) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) Codec(org.redisson.client.codec.Codec) REntity(org.redisson.api.annotation.REntity) RObject(org.redisson.api.RObject) RLiveObject(org.redisson.api.RLiveObject) Collection(java.util.Collection) RLiveObject(org.redisson.api.RLiveObject) RObject(org.redisson.api.RObject) RMap(org.redisson.api.RMap) Map(java.util.Map) RuntimeType(net.bytebuddy.implementation.bind.annotation.RuntimeType)

Aggregations

RedissonReference (org.redisson.RedissonReference)8 Codec (org.redisson.client.codec.Codec)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 RLiveObject (org.redisson.api.RLiveObject)3 REntity (org.redisson.api.annotation.REntity)3 RedisAskException (org.redisson.client.RedisAskException)3 RedisLoadingException (org.redisson.client.RedisLoadingException)3 RedisMovedException (org.redisson.client.RedisMovedException)3 RedisTimeoutException (org.redisson.client.RedisTimeoutException)3 RedisTryAgainException (org.redisson.client.RedisTryAgainException)3 WriteRedisConnectionException (org.redisson.client.WriteRedisConnectionException)3 NamingScheme (org.redisson.liveobject.resolver.NamingScheme)3 Map (java.util.Map)2 RedissonShutdownException (org.redisson.RedissonShutdownException)2 RObject (org.redisson.api.RObject)2 RedisException (org.redisson.client.RedisException)2 BatchCommandData (org.redisson.client.protocol.BatchCommandData)2 MasterSlaveEntry (org.redisson.connection.MasterSlaveEntry)2 ChannelFuture (io.netty.channel.ChannelFuture)1