changes to DefaultNamingScheme

unified the redisson_live_object naming and redisson_live_object_field
naming.
pull/527/head
Rui Gu 9 years ago
parent 068392d6d3
commit 3e83657842

@ -121,6 +121,7 @@ public class AccessorInterceptor {
return result;
}
if (isSetter(method, fieldName)) {
Class idFieldType = me.getClass().getSuperclass().getDeclaredField(fieldName).getType();
if (args[0].getClass().getSuperclass().isAnnotationPresent(REntity.class)) {
Class<? extends Object> rEntity = args[0].getClass().getSuperclass();
REntity anno = rEntity.getAnnotation(REntity.class);
@ -128,7 +129,7 @@ public class AccessorInterceptor {
.getDeclaredConstructor(Codec.class)
.newInstance(codecProvider.getCodec(anno, (Class) rEntity));
liveMap.put(fieldName, new RedissonReference(rEntity,
ns.getName(rEntity, getREntityIdFieldName(args[0]),
ns.getName(rEntity, idFieldType, getREntityIdFieldName(args[0]),
((RLiveObject) args[0]).getLiveObjectId())));
return me;
}
@ -144,7 +145,13 @@ public class AccessorInterceptor {
RObject obj = RedissonObjectFactory
.create(redisson,
mappedClass,
entry.getKey().getName(mappedClass, fieldName, arg),
entry.getKey().getFieldReferenceName(me.getClass().getSuperclass(),
idFieldType,
getREntityIdFieldName(me),
((RLiveObject) me).getLiveObjectId(),
mappedClass,
fieldName,
arg),
entry.getValue());
if (obj instanceof RBitSet) {
((RBitSet) obj).set((BitSet) args[0]);

@ -51,6 +51,7 @@ public class LiveObjectInterceptor {
private final CodecProvider codecProvider;
private final Class originalClass;
private final String idFieldName;
private final Class idFieldType;
private final NamingScheme namingScheme;
private final Class<? extends Codec> codecClass;
@ -63,6 +64,7 @@ public class LiveObjectInterceptor {
this.codecClass = anno.codec();
try {
this.namingScheme = anno.namingScheme().getDeclaredConstructor(Codec.class).newInstance(codecProvider.getCodec(anno, originalClass));
this.idFieldType = originalClass.getDeclaredField(idFieldName).getType();
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
@ -129,7 +131,7 @@ public class LiveObjectInterceptor {
}
private String getMapKey(Object id) {
return namingScheme.getName(originalClass, idFieldName, id);
return namingScheme.getName(originalClass, idFieldType, idFieldName, id);
}
}

@ -15,14 +15,12 @@
*/
package org.redisson.liveobject.resolver;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.io.IOException;
import org.redisson.client.codec.Codec;
import org.redisson.client.handler.State;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.liveobject.annotation.REntity;
import org.redisson.liveobject.annotation.RId;
import org.redisson.liveobject.misc.Introspectior;
/**
*
@ -38,15 +36,20 @@ public class DefaultNamingScheme extends AbstractNamingScheme implements NamingS
}
@Override
public String getName(Class cls, String fieldName, Object fieldValue) {
public String getName(Class entityClass, Class idFieldClass, String idFieldName, Object idValue) {
try {
String encode = bytesToHex(codec.getMapKeyEncoder().encode(fieldValue));
if (Introspectior.getTypeDescription(cls).getDeclaredAnnotations().isAnnotationPresent(REntity.class)
&& Introspectior.getFieldsWithAnnotation(cls, RId.class).getOnly().getName().equals(fieldName)) {
return "redisson_live_object:{class=" + cls.getName() + ", " + fieldName + "=" + encode + "}";
} else {
return "redisson_live_object_field:{class=" + cls.getName() + ", " + fieldName + "=" + encode + "}";
}
String encode = bytesToHex(codec.getMapKeyEncoder().encode(idValue));
return "redisson_live_object:{"+ encode + "}:" + entityClass.getName() + ":" + idFieldName + ":" + idFieldClass.getName();
} catch (IOException ex) {
throw new IllegalArgumentException("Unable to encode \"" + idFieldName + "\" [" + idValue + "] into byte[]", ex);
}
}
@Override
public String getFieldReferenceName(Class entityClass, Class idFieldClass, String idFieldName, Object idValue, Class cls, String fieldName, Object fieldValue) {
try {
String encode = bytesToHex(codec.getMapKeyEncoder().encode(idValue));
return "redisson_live_object_field:{" + encode + "}:" + entityClass.getName() + ":" + fieldName + ":" + cls.getName();
} catch (IOException ex) {
throw new IllegalArgumentException("Unable to encode \"" + fieldName + "\" [" + fieldValue + "] into byte[]", ex);
}
@ -54,21 +57,25 @@ public class DefaultNamingScheme extends AbstractNamingScheme implements NamingS
@Override
public String resolveClassName(String name) {
return name.substring("redisson_live_object:{class=".length(), name.indexOf(","));
return name.substring(name.lastIndexOf("}:") + 2, name.indexOf(":"));
}
@Override
public String resolveIdFieldName(String name) {
return name.substring(name.indexOf(", ") + 2, name.indexOf("=", name.indexOf("=") + 1));
String s = name.substring(0, name.lastIndexOf(":"));
return s.substring(s.lastIndexOf(":") + 1);
}
@Override
public Object resolveId(String name) {
String decode = name.substring(name.indexOf("=", name.indexOf("=") + 1) + 1, name.length() - 1);
String decode = name.substring(name.indexOf("{") + 1, name.indexOf("}"));
ByteBuf b = Unpooled.wrappedBuffer(hexToBytes(decode));
try {
return codec.getMapKeyDecoder().decode(Unpooled.wrappedBuffer(hexToBytes(decode)), new State(false));
return codec.getMapKeyDecoder().decode(b, new State(false));
} catch (IOException ex) {
throw new IllegalStateException("Unable to decode [" + decode + "] into object", ex);
} finally {
b.release();
}
}

@ -21,7 +21,9 @@ package org.redisson.liveobject.resolver;
*/
public interface NamingScheme {
public String getName(Class cls, String fieldName, Object fieldValue);
public String getName(Class entityClass, Class idFieldClass, String idFieldName, Object idValue);
public String getFieldReferenceName(Class entityClass, Class idFieldClass, String idFieldName, Object idValue, Class cls, String fieldName, Object fieldValue);
public String resolveClassName(String name);

@ -264,15 +264,15 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
RLiveObjectService s = redisson.getLiveObjectService();
TestREntity t = s.<TestREntity, String>getOrCreate(TestREntity.class, "1");
assertEquals("1", t.getName());
assertTrue(!redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, "name", "1")).isExists());
assertTrue(!redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, String.class, "name", "1")).isExists());
t.setName("3333");
assertEquals("3333", t.getName());
assertTrue(!redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, "name", "3333")).isExists());
assertTrue(!redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, String.class, "name", "3333")).isExists());
t.setValue("111");
assertEquals("111", t.getValue());
assertTrue(redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, "name", "3333")).isExists());
assertTrue(!redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, "name", "1")).isExists());
assertEquals("111", redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, "name", "3333")).get("value"));
assertTrue(redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, String.class, "name", "3333")).isExists());
assertTrue(!redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, String.class, "name", "1")).isExists());
assertEquals("111", redisson.getMap(DefaultNamingScheme.INSTANCE.getName(TestREntity.class, String.class, "name", "3333")).get("value"));
// ((RLiveObject) t).getLiveObjectLiveMap().put("value", "555");
// assertEquals("555", redisson.getMap(REntity.DefaultNamingScheme.INSTANCE.getName(TestREntity.class, "name", "3333")).get("value"));
// assertEquals("3333", ((RObject) t).getName());//field access takes priority over the implemented interface.

Loading…
Cancel
Save