Search in sources :

Example 1 with NamingScheme

use of org.redisson.liveobject.resolver.NamingScheme 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 NamingScheme

use of org.redisson.liveobject.resolver.NamingScheme 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 NamingScheme

use of org.redisson.liveobject.resolver.NamingScheme 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 NamingScheme

use of org.redisson.liveobject.resolver.NamingScheme in project redisson by redisson.

the class RedissonObjectBuilder method createObject.

public RObject createObject(Object id, Class<?> clazz, Class<?> fieldType, String fieldName) {
    Class<? extends RObject> mappedClass = getMappedClass(fieldType);
    try {
        if (mappedClass != null) {
            Codec fieldCodec = getFieldCodec(clazz, mappedClass, fieldName);
            NamingScheme fieldNamingScheme = getNamingScheme(clazz, fieldCodec);
            String referenceName = fieldNamingScheme.getFieldReferenceName(clazz, id, mappedClass, fieldName);
            return createRObject(redisson, mappedClass, referenceName, fieldCodec);
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
    return null;
}
Also used : Codec(org.redisson.client.codec.Codec) NamingScheme(org.redisson.liveobject.resolver.NamingScheme)

Example 5 with NamingScheme

use of org.redisson.liveobject.resolver.NamingScheme in project redisson by redisson.

the class RedissonObjectBuilder method fromReference.

private Object fromReference(RedissonClient redisson, RedissonReference rr) throws ReflectiveOperationException {
    Class<? extends Object> type = rr.getType();
    if (type != null) {
        if (ClassUtils.isAnnotationPresent(type, REntity.class)) {
            RedissonLiveObjectService liveObjectService = (RedissonLiveObjectService) redisson.getLiveObjectService();
            NamingScheme ns = getNamingScheme(type);
            Object id = ns.resolveId(rr.getKeyName());
            return liveObjectService.createLiveObject(type, id);
        }
    }
    return getObject(redisson, rr, type, codecProvider);
}
Also used : NamingScheme(org.redisson.liveobject.resolver.NamingScheme)

Aggregations

NamingScheme (org.redisson.liveobject.resolver.NamingScheme)13 REntity (org.redisson.api.annotation.REntity)6 RedissonObject (org.redisson.RedissonObject)5 RedissonScoredSortedSet (org.redisson.RedissonScoredSortedSet)4 RedissonSetMultimap (org.redisson.RedissonSetMultimap)4 Codec (org.redisson.client.codec.Codec)4 CommandBatchService (org.redisson.command.CommandBatchService)4 RedissonReference (org.redisson.RedissonReference)3 Field (java.lang.reflect.Field)2 java.util (java.util)2 Entry (java.util.Map.Entry)2 RLiveObject (org.redisson.api.RLiveObject)2 RScoredSortedSet (org.redisson.api.RScoredSortedSet)2 RIndex (org.redisson.api.annotation.RIndex)2 Condition (org.redisson.api.condition.Condition)2 RedisCommand (org.redisson.client.protocol.RedisCommand)2 ListMultiDecoder2 (org.redisson.client.protocol.decoder.ListMultiDecoder2)2 ListScanResult (org.redisson.client.protocol.decoder.ListScanResult)2 ListScanResultReplayDecoder (org.redisson.client.protocol.decoder.ListScanResultReplayDecoder)2 ObjectListReplayDecoder (org.redisson.client.protocol.decoder.ObjectListReplayDecoder)2