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() {
if (LOG.isInfoEnabled()) {
elapsedTime = Stopwatch.createStarted(); 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()) {
committed = false;
aborted = true;
elapsedTime.stop();
if (LOG.isInfoEnabled()) { if (LOG.isInfoEnabled()) {
LOG.info(logTimers("aborted")); if (elapsedTime.isRunning()) {
elapsedTime.stop();
} }
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,20 +29,18 @@ 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);
}
@Test
public void testFoo() throws Exception {
Supply.Draft draft = null;
draft = Supply.draft("APAC").code("WIDGET-002").description("Our second Widget!") draft = Supply.draft("APAC").code("WIDGET-002").description("Our second Widget!")
.demand(new HashMap<String, Long>() { .demand(new HashMap<String, Long>() {
@ -62,6 +62,15 @@ public class EntityDraftBuilderTest extends AbstractEmbeddedCassandraTest {
}); });
Supply s1 = session.<Supply>insert(draft).sync(); Supply s1 = session.<Supply>insert(draft).sync();
}
@Test
public void testFoo() throws Exception {
Supply s1 = session.<Supply>select(Supply.class).where(supply::id, eq(draft.id()))
.single()
.sync()
.orElse(null);
// 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);
}
} }