Search in sources :

Example 1 with RObject

use of org.redisson.api.RObject in project redisson by redisson.

the class RedissonLiveObjectServiceTest method testRObject.

@Test
public void testRObject() {
    RLiveObjectService service = redisson.getLiveObjectService();
    TestClass myObject = new TestClass();
    myObject = service.persist(myObject);
    try {
        ((RObject) myObject).isExists();
    } catch (Exception e) {
        assertEquals("Please use RLiveObjectService instance for this type of functions", e.getMessage());
    }
}
Also used : RObject(org.redisson.api.RObject) RedisException(org.redisson.client.RedisException) RLiveObjectService(org.redisson.api.RLiveObjectService) Test(org.junit.Test)

Example 2 with RObject

use of org.redisson.api.RObject 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 = getFieldNamingScheme(clazz, fieldName, fieldCodec);
            RObject obj = RedissonObjectFactory.createRObject(redisson, mappedClass, fieldNamingScheme.getFieldReferenceName(clazz, id, mappedClass, fieldName, null), fieldCodec);
            return obj;
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
    return null;
}
Also used : Codec(org.redisson.client.codec.Codec) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) RObject(org.redisson.api.RObject)

Example 3 with RObject

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

use of org.redisson.api.RObject in project redisson by redisson.

the class RedissonLiveObjectService method persist.

private <T> T persist(T detachedObject, Map<Object, Object> alreadyPersisted, RCascadeType type) {
    String idFieldName = getRIdFieldName(detachedObject.getClass());
    Object id = ClassUtils.getField(detachedObject, idFieldName);
    if (id == null) {
        try {
            id = generateId(detachedObject.getClass());
        } catch (NoSuchFieldException e) {
            throw new IllegalArgumentException(e);
        }
        ClassUtils.setField(detachedObject, idFieldName, id);
    }
    T attachedObject = attach(detachedObject);
    alreadyPersisted.put(detachedObject, attachedObject);
    RMap<String, Object> liveMap = getMap(attachedObject);
    List<String> excludedFields = new ArrayList<String>();
    excludedFields.add(idFieldName);
    boolean fastResult = liveMap.fastPut("redisson_live_object", "1");
    if (type == RCascadeType.PERSIST && !fastResult) {
        throw new IllegalArgumentException("This REntity already exists.");
    }
    for (FieldDescription.InDefinedShape field : Introspectior.getFieldsDescription(detachedObject.getClass())) {
        Object object = ClassUtils.getField(detachedObject, field.getName());
        if (object == null) {
            continue;
        }
        RObject rObject = objectBuilder.createObject(id, detachedObject.getClass(), object.getClass(), field.getName());
        if (rObject != null) {
            objectBuilder.store(rObject, field.getName(), liveMap);
            if (rObject instanceof SortedSet) {
                ((RSortedSet) rObject).trySetComparator(((SortedSet) object).comparator());
            }
            if (rObject instanceof Collection) {
                for (Object obj : (Collection<Object>) object) {
                    if (obj != null && obj.getClass().isAnnotationPresent(REntity.class)) {
                        Object persisted = alreadyPersisted.get(obj);
                        if (persisted == null) {
                            if (checkCascade(detachedObject, type, field.getName())) {
                                persisted = persist(obj, alreadyPersisted, type);
                            }
                        }
                        obj = persisted;
                    }
                    ((Collection) rObject).add(obj);
                }
            } else if (rObject instanceof Map) {
                Map<Object, Object> rMap = (Map<Object, Object>) rObject;
                Map<?, ?> map = (Map<?, ?>) rObject;
                for (Entry<?, ?> entry : map.entrySet()) {
                    Object key = entry.getKey();
                    Object value = entry.getValue();
                    if (key != null && key.getClass().isAnnotationPresent(REntity.class)) {
                        Object persisted = alreadyPersisted.get(key);
                        if (persisted == null) {
                            if (checkCascade(detachedObject, type, field.getName())) {
                                persisted = persist(key, alreadyPersisted, type);
                            }
                        }
                        key = persisted;
                    }
                    if (value != null && value.getClass().isAnnotationPresent(REntity.class)) {
                        Object persisted = alreadyPersisted.get(value);
                        if (persisted == null) {
                            if (checkCascade(detachedObject, type, field.getName())) {
                                persisted = persist(value, alreadyPersisted, type);
                            }
                        }
                        value = persisted;
                    }
                    rMap.put(key, value);
                }
            }
            excludedFields.add(field.getName());
        } else if (object.getClass().isAnnotationPresent(REntity.class)) {
            Object persisted = alreadyPersisted.get(object);
            if (persisted == null) {
                if (checkCascade(detachedObject, type, field.getName())) {
                    persisted = persist(object, alreadyPersisted, type);
                }
            }
            excludedFields.add(field.getName());
            BeanUtil.pojo.setSimpleProperty(attachedObject, field.getName(), persisted);
        } else {
            validateAnnotation(detachedObject, field.getName());
        }
    }
    copy(detachedObject, attachedObject, excludedFields);
    return attachedObject;
}
Also used : ArrayList(java.util.ArrayList) SortedSet(java.util.SortedSet) RSortedSet(org.redisson.api.RSortedSet) FieldDescription(net.bytebuddy.description.field.FieldDescription) Entry(java.util.Map.Entry) REntity(org.redisson.api.annotation.REntity) RObject(org.redisson.api.RObject) Collection(java.util.Collection) RObject(org.redisson.api.RObject) RLiveObject(org.redisson.api.RLiveObject) RSortedSet(org.redisson.api.RSortedSet) Map(java.util.Map) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) RMap(org.redisson.api.RMap)

Example 5 with RObject

use of org.redisson.api.RObject 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

RObject (org.redisson.api.RObject)5 RLiveObject (org.redisson.api.RLiveObject)3 REntity (org.redisson.api.annotation.REntity)3 Codec (org.redisson.client.codec.Codec)3 NamingScheme (org.redisson.liveobject.resolver.NamingScheme)3 Collection (java.util.Collection)2 Map (java.util.Map)2 RedissonReference (org.redisson.RedissonReference)2 RMap (org.redisson.api.RMap)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Entry (java.util.Map.Entry)1 SortedSet (java.util.SortedSet)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 FieldDescription (net.bytebuddy.description.field.FieldDescription)1 RuntimeType (net.bytebuddy.implementation.bind.annotation.RuntimeType)1 Test (org.junit.Test)1 RLiveObjectService (org.redisson.api.RLiveObjectService)1 RSortedSet (org.redisson.api.RSortedSet)1