Skip to content

Commit

Permalink
Unified and enriched class-based and interface-based entities.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Feb 16, 2015
1 parent 6ef3288 commit 0b5e8fa
Show file tree
Hide file tree
Showing 23 changed files with 329 additions and 150 deletions.
18 changes: 13 additions & 5 deletions rapidoid-db-impl/src/main/java/org/rapidoid/db/impl/DbProxy.java
Expand Up @@ -23,15 +23,19 @@
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentMap; import java.util.Collections;
import java.util.Map;


import org.rapidoid.annotation.Authors; import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since; import org.rapidoid.annotation.Since;
import org.rapidoid.beany.Beany;
import org.rapidoid.db.DbColumn; import org.rapidoid.db.DbColumn;
import org.rapidoid.db.DbList; import org.rapidoid.db.DbList;
import org.rapidoid.db.DbRef; import org.rapidoid.db.DbRef;
import org.rapidoid.db.DbSet; import org.rapidoid.db.DbSet;
import org.rapidoid.db.EntityCommons;
import org.rapidoid.db.IEntity; import org.rapidoid.db.IEntity;
import org.rapidoid.db.IEntityCommons;
import org.rapidoid.util.Cls; import org.rapidoid.util.Cls;
import org.rapidoid.util.U; import org.rapidoid.util.U;


Expand All @@ -41,18 +45,21 @@ public class DbProxy implements InvocationHandler, Serializable {


private static final long serialVersionUID = 8876053750757191711L; private static final long serialVersionUID = 8876053750757191711L;


public static <E extends IEntity> E create(Class<E> type, ConcurrentMap<String, Object> values) { @SuppressWarnings("unchecked")
EntityImpl entity = new EntityImpl(type, values); public static <E extends IEntity> E create(Class<E> type, Map<String, ?> values) {
EntityImpl entity = new EntityImpl(type);


E proxy = Cls.createProxy(new DbProxy(entity, type), type); E proxy = Cls.createProxy(new DbProxy(entity, type), type);
entity.setProxy(proxy); entity.setProxy(proxy);


Beany.update(proxy, (Map<String, Object>) values, true);

return proxy; return proxy;
} }


@SuppressWarnings("unchecked")
public static <E extends IEntity> E create(Class<E> type) { public static <E extends IEntity> E create(Class<E> type) {
ConcurrentMap<String, Object> map = U.concurrentMap(); return (E) create(type, Collections.EMPTY_MAP);
return create(type, map);
} }


private final EntityImpl entity; private final EntityImpl entity;
Expand All @@ -70,6 +77,7 @@ public Object invoke(Object target, Method method, Object[] args) throws Throwab
Class<?>[] paramTypes = method.getParameterTypes(); Class<?>[] paramTypes = method.getParameterTypes();


if (methodClass.equals(Object.class) || methodClass.equals(EntityImpl.class) if (methodClass.equals(Object.class) || methodClass.equals(EntityImpl.class)
|| methodClass.equals(EntityCommons.class) || methodClass.equals(IEntityCommons.class)
|| methodClass.equals(IEntity.class)) { || methodClass.equals(IEntity.class)) {
return method.invoke(entity, args); return method.invoke(entity, args);
} }
Expand Down
Expand Up @@ -86,7 +86,7 @@ public <E> E entity(Class<E> clazz, Map<String, ?> properties) {
Class<E> entityType = getEntityTypeFor(clazz); Class<E> entityType = getEntityTypeFor(clazz);
if (entityType.isInterface() && IEntity.class.isAssignableFrom(entityType)) { if (entityType.isInterface() && IEntity.class.isAssignableFrom(entityType)) {
Class<? extends IEntity> cls = (Class<? extends IEntity>) entityType; Class<? extends IEntity> cls = (Class<? extends IEntity>) entityType;
return (E) DbProxy.create(cls, U.concurrentMap(properties, true)); return (E) DbProxy.create(cls, properties);
} else { } else {
E entity = Cls.newInstance(entityType); E entity = Cls.newInstance(entityType);
Beany.update(entity, (Map<String, Object>) properties, true); Beany.update(entity, (Map<String, Object>) properties, true);
Expand Down
Expand Up @@ -33,21 +33,20 @@
import org.rapidoid.db.DbList; import org.rapidoid.db.DbList;
import org.rapidoid.db.DbRef; import org.rapidoid.db.DbRef;
import org.rapidoid.db.DbSet; import org.rapidoid.db.DbSet;
import org.rapidoid.db.EntityCommons;
import org.rapidoid.db.IEntity; import org.rapidoid.db.IEntity;
import org.rapidoid.util.Cls; import org.rapidoid.util.Cls;
import org.rapidoid.util.U; import org.rapidoid.util.U;


@Authors("Nikolche Mihajlovski") @Authors("Nikolche Mihajlovski")
@Since("2.0.0") @Since("2.0.0")
public class EntityImpl implements IEntity { public class EntityImpl extends EntityCommons implements IEntity {


private static final long serialVersionUID = -5556123216690345146L; private static final long serialVersionUID = -5556123216690345146L;


@SuppressWarnings("unused") @SuppressWarnings("unused")
private final Class<?> type; private final Class<?> type;


private final ConcurrentMap<String, Object> values;

private final ConcurrentMap<String, DbColumn<?>> columns = U.concurrentMap(); private final ConcurrentMap<String, DbColumn<?>> columns = U.concurrentMap();


private final ConcurrentMap<String, DbSet<?>> sets = U.concurrentMap(); private final ConcurrentMap<String, DbSet<?>> sets = U.concurrentMap();
Expand All @@ -56,17 +55,12 @@ public class EntityImpl implements IEntity {


private final ConcurrentMap<String, DbRef<?>> refs = U.concurrentMap(); private final ConcurrentMap<String, DbRef<?>> refs = U.concurrentMap();


private final DbColumn<Long> idColumn; private final ConcurrentMap<String, Object> values = U.concurrentMap();

private final DbColumn<Long> versionColumn;


private IEntity proxy; private IEntity proxy;


public EntityImpl(Class<?> type, ConcurrentMap<String, Object> values) { public EntityImpl(Class<?> type) {
this.type = type; this.type = type;
this.values = values;
this.idColumn = DB.column(values, "id", Long.class);
this.versionColumn = DB.column(values, "version", Long.class);
} }


public void setProxy(IEntity proxy) { public void setProxy(IEntity proxy) {
Expand All @@ -78,11 +72,6 @@ public String toString() {
return Beany.beanToStr(proxy, false); return Beany.beanToStr(proxy, false);
} }


private long getId() {
Object id = values.get("id");
return id != null ? ((Number) id).longValue() : 0;
}

public DbColumn<?> column(Method method) { public DbColumn<?> column(Method method) {
U.must(DbColumn.class.isAssignableFrom(method.getReturnType())); U.must(DbColumn.class.isAssignableFrom(method.getReturnType()));


Expand Down Expand Up @@ -152,31 +141,4 @@ public int hashCode() {
return result; return result;
} }


@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof IEntity))
return false;
IEntity other = (IEntity) obj;
if (id().get() == null || other.id().get() == null) {
return false;
}
if (!id().get().equals(other.id().get()))
return false;
return true;
}

@Override
public DbColumn<Long> id() {
return idColumn;
}

@Override
public DbColumn<Long> version() {
return versionColumn;
}

} }
Expand Up @@ -68,15 +68,15 @@ private void testDb(Database db) {
Person p2 = db.get(id2, Person.class); Person p2 = db.get(id2, Person.class);
Person p3 = db.get(id3); Person p3 = db.get(id3);


eq(p1.id, id1); eq(p1.getId(), id1);
eq(p1.name, "abc"); eq(p1.name, "abc");
eq(p1.age, 10); eq(p1.age, 10);


eq(p2.id, id2); eq(p2.getId(), id2);
eq(p2.name, "f"); eq(p2.name, "f");
eq(p2.age, 20); eq(p2.age, 20);


eq(p3.id, id3); eq(p3.getId(), id3);
eq(p3.name, "xy"); eq(p3.name, "xy");
eq(p3.age, 30); eq(p3.age, 30);


Expand All @@ -90,8 +90,8 @@ public boolean eval(Person p) throws Exception {
List<Person> adults = db.find(pr); List<Person> adults = db.find(pr);


eq(adults.size(), 2); eq(adults.size(), 2);
eq(adults.get(0).id, id2); eq(adults.get(0).getId(), id2);
eq(adults.get(1).id, id3); eq(adults.get(1).getId(), id3);


db.shutdown(); db.shutdown();
} }
Expand Down
Expand Up @@ -31,14 +31,14 @@ public class DbClassEqualityTest extends DbTestCommons {


@Test @Test
public void testEntityEquality() { public void testEntityEquality() {
eq(post(0L), post(0L)); neq(post(0L), post(0L));
eq(post(123L), post(123L)); eq(post(123L), post(123L));
neq(post(123L), post(547L)); neq(post(123L), post(547L));
} }


private Post post(Long id) { private Post post(Long id) {
Post p = new Post(); Post p = new Post();
p.id = id; p.setId(id);
return p; return p;
} }


Expand Down
Expand Up @@ -35,10 +35,10 @@ public void testOCCFailure() {
Person p1 = new Person(); Person p1 = new Person();
DB.persist(p1); DB.persist(p1);


eq(p1.version, 1); eq(p1.getVersion(), 1);


Person p2 = new Person(); Person p2 = new Person();
p2.id = p1.id; p2.setId(p1.getId());


DB.persist(p2); DB.persist(p2);
} }
Expand All @@ -48,13 +48,13 @@ public void testOCC() {
Person p1 = new Person(); Person p1 = new Person();
DB.persist(p1); DB.persist(p1);


eq(p1.version, 1); eq(p1.getVersion(), 1);

Person p2 = new Person(); Person p2 = new Person();
p2.id = p1.id; p2.setId(p1.getId());


DB.refresh(p2); DB.refresh(p2);
eq(p2.version, 1); eq(p2.getVersion(), 1);


DB.persist(p2); DB.persist(p2);
} }
Expand Down
Expand Up @@ -60,7 +60,7 @@ public void run() {
public void run() { public void run() {
int id = Rnd.rnd(count) + 1; int id = Rnd.rnd(count) + 1;
Person person = new Person("x", id * 100); Person person = new Person("x", id * 100);
person.version = DB.getVersionOf(id); person.setVersion(DB.getVersionOf(id));


try { try {
DB.update(id, person); DB.update(id, person);
Expand Down Expand Up @@ -89,7 +89,7 @@ private void checkDb(final int count) {


for (int id = 1; id <= count; id++) { for (int id = 1; id <= count; id++) {
Person p = DB.get(id); Person p = DB.get(id);
isTrue(p.id == id); isTrue(p.getId() == id);
isTrue((p.name.equals("abc") && p.age == -1) || (p.name.equals("x") && p.age == id * 100)); isTrue((p.name.equals("abc") && p.age == -1) || (p.name.equals("x") && p.age == id * 100));
} }


Expand Down
Expand Up @@ -45,8 +45,8 @@ public void testInverseRelations1() {
DB.refresh(post1); DB.refresh(post1);
DB.refresh(post2); DB.refresh(post2);


notNull(post1.id); notNull(post1.getId());
notNull(post2.id); notNull(post2.getId());


eq(profile.posts, U.list(post1, post2)); eq(profile.posts, U.list(post1, post2));
eq(post1.postedOn.get(), profile); eq(post1.postedOn.get(), profile);
Expand Down

0 comments on commit 0b5e8fa

Please sign in to comment.