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);
}
}
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;
}
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;
}
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();
}
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());
}
Aggregations