Finish implementation of proxy model objects.

This commit is contained in:
Greg Burd 2017-10-30 15:21:43 -04:00
parent dcea254fdb
commit 48545c1e84
3 changed files with 75 additions and 36 deletions

View file

@ -86,7 +86,9 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
@Override @Override
public synchronized UnitOfWork<E> begin() { public synchronized UnitOfWork<E> begin() {
elapsedTime = Stopwatch.createStarted(); if (LOG.isInfoEnabled()) {
elapsedTime = Stopwatch.createStarted();
}
// log.record(txn::start) // log.record(txn::start)
return this; return this;
} }
@ -317,13 +319,11 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
}); });
// log.record(txn::abort) // log.record(txn::abort)
// cache.invalidateSince(txn::start time) // cache.invalidateSince(txn::start time)
if (!hasAborted()) { if (LOG.isInfoEnabled()) {
committed = false; if (elapsedTime.isRunning()) {
aborted = true; elapsedTime.stop();
elapsedTime.stop();
if (LOG.isInfoEnabled()) {
LOG.info(logTimers("aborted"));
} }
LOG.info(logTimers("aborted"));
} }
} }

View file

@ -26,6 +26,7 @@ import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import net.helenus.core.Helenus; import net.helenus.core.Helenus;
import net.helenus.mapping.annotation.Transient; import net.helenus.mapping.annotation.Transient;
@ -64,7 +65,7 @@ public class MapperInvocationHandler<E> implements InvocationHandler, Serializab
} }
private Object writeReplace() { private Object writeReplace() {
return new SerializationProxy(this); return new SerializationProxy<E>(this);
} }
private void readObject(ObjectInputStream stream) throws InvalidObjectException { private void readObject(ObjectInputStream stream) throws InvalidObjectException {
throw new InvalidObjectException("Proxy required."); throw new InvalidObjectException("Proxy required.");
@ -152,25 +153,28 @@ public class MapperInvocationHandler<E> implements InvocationHandler, Serializab
return value; return value;
} }
static class SerializationProxy implements Serializable { static class SerializationProxy<E> implements Serializable {
private static final long serialVersionUID = -5617583940055969353L; private static final long serialVersionUID = -5617583940055969353L;
private final Class<?> iface; private final Class<E> iface;
private final Map<String, Object> src; private final Map<String, Object> src;
public SerializationProxy(MapperInvocationHandler mapper) { public SerializationProxy(MapperInvocationHandler mapper) {
this.iface = mapper.iface; this.iface = mapper.iface;
if (mapper.src instanceof ValueProviderMap) { if (mapper.src instanceof ValueProviderMap) {
this.src = new HashMap<String, Object>(mapper.src.size()); this.src = new HashMap<String, Object>(mapper.src.size());
this.src.putAll(src); Set<String> keys = mapper.src.keySet();
for (String key : keys) {
this.src.put(key, mapper.src.get(key));
}
} else { } else {
this.src = mapper.src; this.src = mapper.src;
} }
} }
Object readResolve() throws ObjectStreamException { Object readResolve() throws ObjectStreamException {
return Helenus.map(iface, src); return new MapperInvocationHandler(iface, src);
} }
} }

View file

@ -15,9 +15,11 @@
*/ */
package net.helenus.test.integration.core.draft; package net.helenus.test.integration.core.draft;
import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.concurrent.TimeoutException;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -27,41 +29,48 @@ import net.helenus.core.Helenus;
import net.helenus.core.HelenusSession; import net.helenus.core.HelenusSession;
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest; import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
import static net.helenus.core.Query.eq;
public class EntityDraftBuilderTest extends AbstractEmbeddedCassandraTest { public class EntityDraftBuilderTest extends AbstractEmbeddedCassandraTest {
static Supply supply; static Supply supply;
static HelenusSession session; static HelenusSession session;
static Supply.Draft draft = null;
@BeforeClass @BeforeClass
public static void beforeTest() { public static void beforeTest() throws TimeoutException {
session = Helenus.init(getSession()).showCql().add(Supply.class).autoCreateDrop().get(); session = Helenus.init(getSession()).showCql().add(Supply.class).autoCreateDrop().get();
supply = session.dsl(Supply.class); supply = session.dsl(Supply.class);
}
draft = Supply.draft("APAC").code("WIDGET-002").description("Our second Widget!")
.demand(new HashMap<String, Long>() {
{
put("APAC", 100L);
put("EMEA", 10000L);
put("NORAM", 2000000L);
}
}).shipments(new HashSet<String>() {
{
add("HMS Puddle in transit to APAC, 100 units.");
add("Frigate Jimmy in transit to EMEA, 10000 units.");
}
}).suppliers(new ArrayList<String>() {
{
add("Puddle, Inc.");
add("Jimmy Town, LTD.");
}
});
Supply s1 = session.<Supply>insert(draft).sync();
}
@Test @Test
public void testFoo() throws Exception { public void testFoo() throws Exception {
Supply.Draft draft = null;
draft = Supply.draft("APAC").code("WIDGET-002").description("Our second Widget!") Supply s1 = session.<Supply>select(Supply.class).where(supply::id, eq(draft.id()))
.demand(new HashMap<String, Long>() { .single()
{ .sync()
put("APAC", 100L); .orElse(null);
put("EMEA", 10000L);
put("NORAM", 2000000L);
}
}).shipments(new HashSet<String>() {
{
add("HMS Puddle in transit to APAC, 100 units.");
add("Frigate Jimmy in transit to EMEA, 10000 units.");
}
}).suppliers(new ArrayList<String>() {
{
add("Puddle, Inc.");
add("Jimmy Town, LTD.");
}
});
Supply s1 = session.<Supply>insert(draft).sync();
// List // List
Supply s2 = session.<Supply>update(s1.update()).prepend(supply::suppliers, "Pignose Supply, LLC.").sync(); Supply s2 = session.<Supply>update(s1.update()).prepend(supply::suppliers, "Pignose Supply, LLC.").sync();
@ -76,4 +85,30 @@ public class EntityDraftBuilderTest extends AbstractEmbeddedCassandraTest {
Supply s4 = session.<Supply>update(s3.update()).put(supply::demand, "NORAM", 10L).sync(); Supply s4 = session.<Supply>update(s3.update()).put(supply::demand, "NORAM", 10L).sync();
Assert.assertEquals((long) s4.demand().get("NORAM"), 10L); Assert.assertEquals((long) s4.demand().get("NORAM"), 10L);
} }
@Test
public void testSerialization() throws Exception {
Supply s1, s2;
s1 = session.<Supply>select(Supply.class).where(supply::id, eq(draft.id()))
.single()
.sync()
.orElse(null);
byte[] data;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos)) {
out.writeObject(s1);
out.flush();
data = bos.toByteArray();
}
try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
ObjectInput in = new ObjectInputStream(bis)) {
s2 = (Supply)in.readObject();
}
Assert.assertEquals(s2.id(), s1.id());
Assert.assertEquals(s2, s1);
}
} }