Search in sources :

Example 1 with RLiveObject

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

the class RedissonLiveObjectServiceTest method testAsLiveObject.

@Test
public void testAsLiveObject() {
    RLiveObjectService service = redisson.getLiveObjectService();
    TestClass instance = new TestClass(new ObjectId(100));
    instance = service.persist(instance);
    RLiveObject liveObject = service.asLiveObject(instance);
    assertEquals(new ObjectId(100), liveObject.getLiveObjectId());
    try {
        service.asLiveObject(new Object());
        fail("Should not be here");
    } catch (Exception e) {
        assertTrue(e instanceof ClassCastException);
    }
}
Also used : RLiveObject(org.redisson.api.RLiveObject) RObject(org.redisson.api.RObject) RLiveObject(org.redisson.api.RLiveObject) RedisException(org.redisson.client.RedisException) RLiveObjectService(org.redisson.api.RLiveObjectService) Test(org.junit.Test)

Example 2 with RLiveObject

use of org.redisson.api.RLiveObject 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 3 with RLiveObject

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

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

Example 5 with RLiveObject

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

the class RedissonReferenceTest method testBasic.

@Test
public void testBasic() {
    RBucket<Object> b1 = redisson.getBucket("b1");
    RBucket<Object> b2 = redisson.getBucket("b2");
    RBucket<Object> b3 = redisson.getBucket("b3");
    b2.set(b3);
    b1.set(redisson.getBucket("b2"));
    assertTrue(b1.get().getClass().equals(RedissonBucket.class));
    assertEquals("b3", ((RBucket) ((RBucket) b1.get()).get()).getName());
    RBucket<Object> b4 = redisson.getBucket("b4");
    b4.set(redisson.getMapCache("testCache"));
    assertTrue(b4.get() instanceof RedissonMapCache);
    ((RedissonMapCache) b4.get()).fastPut(b1, b2, 1, TimeUnit.MINUTES);
    assertEquals("b2", ((RBucket) ((RedissonMapCache) b4.get()).get(b1)).getName());
    RBucket<Object> b5 = redisson.getBucket("b5");
    RLiveObjectService service = redisson.getLiveObjectService();
    RedissonLiveObjectServiceTest.TestREntity rlo = new RedissonLiveObjectServiceTest.TestREntity("123");
    rlo = service.persist(rlo);
    rlo.setName("t1");
    rlo.setValue("t2");
    b5.set(rlo);
    assertTrue(redisson.getBucket("b5").get() instanceof RLiveObject);
    assertEquals("t1", ((RedissonLiveObjectServiceTest.TestREntity) redisson.getBucket("b5").get()).getName());
    assertEquals("t2", ((RedissonLiveObjectServiceTest.TestREntity) redisson.getBucket("b5").get()).getValue());
}
Also used : RBucket(org.redisson.api.RBucket) RLiveObject(org.redisson.api.RLiveObject) RLiveObject(org.redisson.api.RLiveObject) RLiveObjectService(org.redisson.api.RLiveObjectService) Test(org.junit.Test)

Aggregations

RLiveObject (org.redisson.api.RLiveObject)5 RedissonReference (org.redisson.RedissonReference)3 RObject (org.redisson.api.RObject)3 REntity (org.redisson.api.annotation.REntity)3 Codec (org.redisson.client.codec.Codec)3 NamingScheme (org.redisson.liveobject.resolver.NamingScheme)3 Test (org.junit.Test)2 RLiveObjectService (org.redisson.api.RLiveObjectService)2 Collection (java.util.Collection)1 Map (java.util.Map)1 RuntimeType (net.bytebuddy.implementation.bind.annotation.RuntimeType)1 RBucket (org.redisson.api.RBucket)1 RMap (org.redisson.api.RMap)1 RObjectReactive (org.redisson.api.RObjectReactive)1 RedisException (org.redisson.client.RedisException)1