Search in sources :

Example 6 with REntity

use of org.redisson.api.annotation.REntity in project redisson by redisson.

the class RedissonObjectBuilder method toReference.

public RedissonReference toReference(Object object) {
    if (object != null && ClassUtils.isAnnotationPresent(object.getClass(), REntity.class)) {
        throw new IllegalArgumentException("REntity should be attached to Redisson before save");
    }
    if (object instanceof RObject && !(object instanceof RLiveObject)) {
        Class<?> clazz = object.getClass().getInterfaces()[0];
        RObject rObject = (RObject) object;
        if (rObject.getCodec() != null) {
            codecProvider.registerCodec((Class) rObject.getCodec().getClass(), rObject.getCodec());
        }
        return new RedissonReference(clazz, rObject.getName(), rObject.getCodec());
    }
    if (object instanceof RObjectReactive && !(object instanceof RLiveObject)) {
        Class<?> clazz = object.getClass().getInterfaces()[0];
        RObjectReactive rObject = (RObjectReactive) object;
        if (rObject.getCodec() != null) {
            codecProvider.registerCodec((Class) rObject.getCodec().getClass(), rObject.getCodec());
        }
        return new RedissonReference(clazz, rObject.getName(), rObject.getCodec());
    }
    if (object instanceof RObjectRx && !(object instanceof RLiveObject)) {
        Class<?> clazz = object.getClass().getInterfaces()[0];
        RObjectRx rObject = (RObjectRx) object;
        if (rObject.getCodec() != null) {
            codecProvider.registerCodec((Class) rObject.getCodec().getClass(), rObject.getCodec());
        }
        return new RedissonReference(clazz, rObject.getName(), rObject.getCodec());
    }
    try {
        if (object instanceof RLiveObject) {
            Class<? extends Object> rEntity = object.getClass().getSuperclass();
            NamingScheme ns = getNamingScheme(rEntity);
            return new RedissonReference(rEntity, ns.getName(rEntity, ((RLiveObject) object).getLiveObjectId()));
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
    return null;
}
Also used : REntity(org.redisson.api.annotation.REntity) NamingScheme(org.redisson.liveobject.resolver.NamingScheme)

Example 7 with REntity

use of org.redisson.api.annotation.REntity in project redisson by redisson.

the class AccessorInterceptor method intercept.

@RuntimeType
@SuppressWarnings("NestedIfDepth")
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(me.getClass().getSuperclass(), method);
    Field field = ClassUtils.getDeclaredField(me.getClass().getSuperclass(), fieldName);
    Class<?> fieldType = field.getType();
    if (isGetter(method, fieldName)) {
        Object result = liveMap.get(fieldName);
        if (result == null) {
            RObject ar = commandExecutor.getObjectBuilder().createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), fieldType, fieldName);
            if (ar != null) {
                commandExecutor.getObjectBuilder().store(ar, fieldName, liveMap);
                return ar;
            }
        }
        if (result != null && fieldType.isEnum()) {
            if (result instanceof String) {
                return Enum.valueOf((Class) fieldType, (String) result);
            }
            return result;
        }
        if (result instanceof RedissonReference) {
            return commandExecutor.getObjectBuilder().fromReference((RedissonReference) result, RedissonObjectBuilder.ReferenceType.DEFAULT);
        }
        return result;
    }
    if (isSetter(method, fieldName)) {
        Object arg = args[0];
        if (arg != null && ClassUtils.isAnnotationPresent(arg.getClass(), REntity.class)) {
            throw new IllegalStateException("REntity object should be attached to Redisson first");
        }
        if (arg instanceof RLiveObject) {
            RLiveObject liveObject = (RLiveObject) arg;
            removeIndex(liveMap, me, field);
            storeIndex(field, me, liveObject.getLiveObjectId());
            Class<? extends Object> rEntity = liveObject.getClass().getSuperclass();
            NamingScheme ns = commandExecutor.getObjectBuilder().getNamingScheme(rEntity);
            if (commandExecutor instanceof CommandBatchService) {
                liveMap.fastPutAsync(fieldName, new RedissonReference(rEntity, ns.getName(rEntity, liveObject.getLiveObjectId())));
            } else {
                liveMap.fastPut(fieldName, new RedissonReference(rEntity, ns.getName(rEntity, liveObject.getLiveObjectId())));
            }
            return me;
        }
        if (!(arg instanceof RObject) && (arg instanceof Collection || arg instanceof Map) && TransformationMode.ANNOTATION_BASED.equals(ClassUtils.getAnnotation(me.getClass().getSuperclass(), REntity.class).fieldTransformation())) {
            RObject rObject = commandExecutor.getObjectBuilder().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) {
            if (commandExecutor instanceof CommandBatchService) {
                commandExecutor.getObjectBuilder().storeAsync((RObject) arg, fieldName, liveMap);
            } else {
                commandExecutor.getObjectBuilder().store((RObject) arg, fieldName, liveMap);
            }
            return me;
        }
        removeIndex(liveMap, me, field);
        if (arg != null) {
            storeIndex(field, me, arg);
            if (commandExecutor instanceof CommandBatchService) {
                liveMap.fastPutAsync(fieldName, arg);
            } else {
                liveMap.fastPut(fieldName, arg);
            }
        }
        return me;
    }
    return superMethod.call();
}
Also used : RedissonReference(org.redisson.RedissonReference) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) CommandBatchService(org.redisson.command.CommandBatchService) Field(java.lang.reflect.Field) REntity(org.redisson.api.annotation.REntity) RedissonObject(org.redisson.RedissonObject)

Aggregations

REntity (org.redisson.api.annotation.REntity)7 NamingScheme (org.redisson.liveobject.resolver.NamingScheme)5 Codec (org.redisson.client.codec.Codec)4 RedissonReference (org.redisson.RedissonReference)3 RLiveObject (org.redisson.api.RLiveObject)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 RedissonObject (org.redisson.RedissonObject)1 RLiveObjectService (org.redisson.api.RLiveObjectService)1 RObject (org.redisson.api.RObject)1 RObjectReactive (org.redisson.api.RObjectReactive)1 CodecProvider (org.redisson.codec.CodecProvider)1 CommandBatchService (org.redisson.command.CommandBatchService)1