Search in sources :

Example 1 with REntity

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

the class RedissonObjectFactory method toReference.

public static RedissonReference toReference(RedissonClient redisson, Object object) {
    if (object != null && object.getClass().isAnnotationPresent(REntity.class)) {
        throw new IllegalArgumentException("REntity should be attached to Redisson before save");
    }
    if (object instanceof RObject && !(object instanceof RLiveObject)) {
        RObject rObject = ((RObject) object);
        redisson.getCodecProvider().registerCodec((Class) rObject.getCodec().getClass(), (Class) rObject.getClass(), rObject.getName(), rObject.getCodec());
        return new RedissonReference(object.getClass(), ((RObject) object).getName(), ((RObject) object).getCodec());
    }
    try {
        if (object instanceof RLiveObject) {
            Class<? extends Object> rEntity = object.getClass().getSuperclass();
            REntity anno = rEntity.getAnnotation(REntity.class);
            NamingScheme ns = anno.namingScheme().getDeclaredConstructor(Codec.class).newInstance(redisson.getCodecProvider().getCodec(anno, (Class) rEntity));
            String name = Introspectior.getFieldsWithAnnotation(rEntity, RId.class).getOnly().getName();
            Class<?> type = rEntity.getDeclaredField(name).getType();
            return new RedissonReference(rEntity, ns.getName(rEntity, type, name, ((RLiveObject) object).getLiveObjectId()));
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
    return null;
}
Also used : Codec(org.redisson.client.codec.Codec) REntity(org.redisson.api.annotation.REntity) RObject(org.redisson.api.RObject) RedissonReference(org.redisson.RedissonReference) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) RLiveObject(org.redisson.api.RLiveObject)

Example 2 with REntity

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

the class RedissonObjectFactory method toReference.

public static RedissonReference toReference(RedissonReactiveClient redissonReactive, Object object) {
    if (object instanceof RObjectReactive && !(object instanceof RLiveObject)) {
        RObjectReactive rObject = ((RObjectReactive) object);
        redissonReactive.getCodecProvider().registerCodec((Class) rObject.getCodec().getClass(), (Class) rObject.getClass(), rObject.getName(), rObject.getCodec());
        return new RedissonReference(object.getClass(), ((RObjectReactive) object).getName(), ((RObjectReactive) object).getCodec());
    }
    try {
        if (object instanceof RLiveObject) {
            Class<? extends Object> rEntity = object.getClass().getSuperclass();
            REntity anno = rEntity.getAnnotation(REntity.class);
            NamingScheme ns = anno.namingScheme().getDeclaredConstructor(Codec.class).newInstance(redissonReactive.getCodecProvider().getCodec(anno, (Class) rEntity));
            String name = Introspectior.getFieldsWithAnnotation(rEntity, RId.class).getOnly().getName();
            Class<?> type = rEntity.getDeclaredField(name).getType();
            return new RedissonReference(rEntity, ns.getName(rEntity, type, name, ((RLiveObject) object).getLiveObjectId()));
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
    return null;
}
Also used : Codec(org.redisson.client.codec.Codec) RObjectReactive(org.redisson.api.RObjectReactive) REntity(org.redisson.api.annotation.REntity) RedissonReference(org.redisson.RedissonReference) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) RLiveObject(org.redisson.api.RLiveObject)

Example 3 with REntity

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

the class RedissonObjectFactory method fromReference.

public static <T> T fromReference(RedissonClient redisson, RedissonReference rr) throws Exception {
    Class<? extends Object> type = rr.getType();
    CodecProvider codecProvider = redisson.getConfig().getCodecProvider();
    if (type != null) {
        if (type.isAnnotationPresent(REntity.class)) {
            RLiveObjectService liveObjectService = redisson.getLiveObjectService();
            REntity anno = type.getAnnotation(REntity.class);
            NamingScheme ns = anno.namingScheme().getDeclaredConstructor(Codec.class).newInstance(codecProvider.getCodec(anno, type));
            return (T) liveObjectService.get(type, ns.resolveId(rr.getKeyName()));
        }
        List<Class<?>> interfaces = Arrays.asList(type.getInterfaces());
        for (Class<?> iType : interfaces) {
            if (builders.containsKey(iType)) {
                // user cache to speed up things a little.
                Method builder = builders.get(iType).get(isDefaultCodec(rr));
                return (T) (isDefaultCodec(rr) ? builder.invoke(redisson, rr.getKeyName()) : builder.invoke(redisson, rr.getKeyName(), codecProvider.getCodec(rr.getCodecType())));
            }
        }
    }
    throw new ClassNotFoundException("No RObject is found to match class type of " + rr.getTypeName() + " with codec type of " + rr.getCodecName());
}
Also used : CodecProvider(org.redisson.codec.CodecProvider) Codec(org.redisson.client.codec.Codec) REntity(org.redisson.api.annotation.REntity) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) Method(java.lang.reflect.Method) RLiveObjectService(org.redisson.api.RLiveObjectService)

Example 4 with REntity

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

the class RedissonObjectBuilder method getNamingScheme.

public NamingScheme getNamingScheme(Class<?> entityClass) {
    REntity anno = ClassUtils.getAnnotation(entityClass, REntity.class);
    Codec codec = codecProvider.getCodec(anno, entityClass, config);
    return getNamingScheme(entityClass, codec);
}
Also used : Codec(org.redisson.client.codec.Codec) REntity(org.redisson.api.annotation.REntity)

Example 5 with REntity

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

the class RedissonObjectBuilder method getFieldNamingScheme.

/**
     * WARNING: rEntity has to be the class of @This object.
     */
private NamingScheme getFieldNamingScheme(Class<?> rEntity, String fieldName, Codec c) throws Exception {
    if (!namingSchemeCache.containsKey(fieldName)) {
        REntity anno = rEntity.getAnnotation(REntity.class);
        namingSchemeCache.putIfAbsent(fieldName, anno.namingScheme().getDeclaredConstructor(Codec.class).newInstance(c));
    }
    return namingSchemeCache.get(fieldName);
}
Also used : REntity(org.redisson.api.annotation.REntity)

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