From 9f511cde7418bbda9b8c84bbc68e7c28866d5e3d Mon Sep 17 00:00:00 2001 From: John de Freitas Date: Sat, 31 Mar 2018 22:21:28 -0400 Subject: [PATCH] Revert "Not-yet-tested tracking of UOW deletes" This reverts commit b27bc7d9a9b2f4066c66e00660aa651d3c4d3684. --- .../net/helenus/core/PostCommitFunction.java | 5 ++- .../java/net/helenus/core/UnitOfWork.java | 37 +++++++++++++------ .../java/net/helenus/core/cache/MapCache.java | 23 +++--------- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/main/java/net/helenus/core/PostCommitFunction.java b/src/main/java/net/helenus/core/PostCommitFunction.java index a7e152c..0c823cf 100644 --- a/src/main/java/net/helenus/core/PostCommitFunction.java +++ b/src/main/java/net/helenus/core/PostCommitFunction.java @@ -4,17 +4,18 @@ import java.util.List; import java.util.Objects; public class PostCommitFunction implements java.util.function.Function { - public static final PostCommitFunction NULL_ABORT = new PostCommitFunction<>(null, null, false); - public static final PostCommitFunction NULL_COMMIT = new PostCommitFunction<>(null, null, true); + private final UnitOfWork uow; private final List commitThunks; private final List abortThunks; private boolean committed; PostCommitFunction( + UnitOfWork uow, List postCommit, List abortThunks, boolean committed) { + this.uow = uow; this.commitThunks = postCommit; this.abortThunks = abortThunks; this.committed = committed; diff --git a/src/main/java/net/helenus/core/UnitOfWork.java b/src/main/java/net/helenus/core/UnitOfWork.java index 9dca27d..454a318 100644 --- a/src/main/java/net/helenus/core/UnitOfWork.java +++ b/src/main/java/net/helenus/core/UnitOfWork.java @@ -26,6 +26,8 @@ import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.cache.Cache; import javax.cache.CacheManager; @@ -46,7 +48,9 @@ import org.slf4j.LoggerFactory; /** Encapsulates the concept of a "transaction" as a unit-of-work. */ public class UnitOfWork implements AutoCloseable { + private static final Logger LOG = LoggerFactory.getLogger(UnitOfWork.class); + private static final Pattern classNameRegex = Pattern.compile("^(?:\\w+\\.)+(?:(\\w+)|(\\w+)\\$.*)$"); public final UnitOfWork parent; private final List nested = new ArrayList<>(); @@ -61,7 +65,7 @@ public class UnitOfWork implements AutoCloseable { protected int databaseLookups = 0; protected final Stopwatch elapsedTime; protected Map databaseTime = new HashMap<>(); - protected double cacheLookupTimeMSecs = 0.0d; + protected double cacheLookupTimeMSecs = 0.0; private List commitThunks = new ArrayList(); private List abortThunks = new ArrayList(); private List> asyncOperationFutures = new ArrayList>(); @@ -70,6 +74,17 @@ public class UnitOfWork implements AutoCloseable { private long committedAt = 0L; private BatchOperation batch; + private String extractClassNameFromStackFrame(String classNameOnStack) { + String name = null; + Matcher m = classNameRegex.matcher(classNameOnStack); + if (m.find()) { + name = (m.group(1) != null) ? m.group(1) : ((m.group(2) != null) ? m.group(2) : name); + } else { + name = classNameOnStack; + } + return name; + } + public UnitOfWork(HelenusSession session) { this(session, null); } @@ -82,7 +97,7 @@ public class UnitOfWork implements AutoCloseable { parent.addNestedUnitOfWork(this); } this.session = session; - CacheLoader cacheLoader = null; + CacheLoader cacheLoader = null; if (parent != null) { cacheLoader = new CacheLoader() { @@ -348,7 +363,7 @@ public class UnitOfWork implements AutoCloseable { throws HelenusException, TimeoutException { if (isDone()) { - return PostCommitFunction.NULL_ABORT; + return new PostCommitFunction(this, null, null, false); } // Only the outer-most UOW batches statements for commit time, execute them. @@ -384,7 +399,7 @@ public class UnitOfWork implements AutoCloseable { } } - return PostCommitFunction.NULL_ABORT; + return new PostCommitFunction(this, null, null, false); } else { committed = true; aborted = false; @@ -438,11 +453,11 @@ public class UnitOfWork implements AutoCloseable { LOG.info(logTimers("committed")); } - return PostCommitFunction.NULL_COMMIT; + return new PostCommitFunction(this, null, null, true); } else { + // Merge cache and statistics into parent if there is one. parent.statementCache.putAll(statementCache.unwrap(Map.class)); - parent.statementCache.removeAll(statementCache.getDeletions()); parent.mergeCache(cache); parent.addBatched(batch); if (purpose != null) { @@ -468,15 +483,15 @@ public class UnitOfWork implements AutoCloseable { // Constructor ctor = clazz.getConstructor(conflictExceptionClass); // T object = ctor.newInstance(new Object[] { String message }); // } - return new PostCommitFunction(commitThunks, abortThunks, true); + return new PostCommitFunction(this, commitThunks, abortThunks, true); } - private void addBatched(BatchOperation batchArg) { - if (batchArg != null) { + private void addBatched(BatchOperation batch) { + if (batch != null) { if (this.batch == null) { - this.batch = batchArg; + this.batch = batch; } else { - this.batch.addAll(batchArg); + this.batch.addAll(batch); } } } diff --git a/src/main/java/net/helenus/core/cache/MapCache.java b/src/main/java/net/helenus/core/cache/MapCache.java index 7418efd..2cef0b6 100644 --- a/src/main/java/net/helenus/core/cache/MapCache.java +++ b/src/main/java/net/helenus/core/cache/MapCache.java @@ -1,6 +1,6 @@ package net.helenus.core.cache; -import java.util.Collections; + import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -22,16 +22,15 @@ import javax.cache.processor.MutableEntry; public class MapCache implements Cache { private final CacheManager manager; private final String name; - private Map map = new ConcurrentHashMap<>(); - private Set deletes = Collections.synchronizedSet(new HashSet<>()); - private Set> cacheEntryRemovedListeners = new HashSet<>(); + private Map map = new ConcurrentHashMap(); + private Set cacheEntryRemovedListeners = new HashSet<>(); private CacheLoader cacheLoader = null; private boolean isReadThrough = false; + private Configuration configuration = new MapConfiguration(); private static class MapConfiguration implements Configuration { - private static final long serialVersionUID = 6093947542772516209L; - @Override + @Override public Class getKeyType() { return null; } @@ -55,11 +54,6 @@ public class MapCache implements Cache { this.isReadThrough = isReadThrough; } - /** Non-interface method; should only be called by UnitOfWork when merging to an enclosing UnitOfWork. */ - public Set getDeletions() { - return new HashSet<>(deletes); - } - /** {@inheritDoc} */ @Override public V get(K key) { @@ -199,7 +193,6 @@ public class MapCache implements Cache { boolean removed = false; synchronized (map) { removed = map.remove(key) != null; - deletes.add(key); notifyRemovedListeners(key); } return removed; @@ -212,7 +205,6 @@ public class MapCache implements Cache { V value = map.get(key); if (value != null && oldValue.equals(value)) { map.remove(key); - deletes.add(key); notifyRemovedListeners(key); return true; } @@ -227,7 +219,6 @@ public class MapCache implements Cache { V oldValue = null; oldValue = map.get(key); map.remove(key); - deletes.add(key); notifyRemovedListeners(key); return oldValue; } @@ -282,7 +273,6 @@ public class MapCache implements Cache { keys.remove(key); } } - deletes.addAll(keys); } notifyRemovedListeners(keys); } @@ -293,7 +283,6 @@ public class MapCache implements Cache { synchronized (map) { Set keys = map.keySet(); map.clear(); - deletes.addAll(keys); notifyRemovedListeners(keys); } } @@ -302,7 +291,6 @@ public class MapCache implements Cache { @Override public void clear() { map.clear(); - deletes.clear(); } /** {@inheritDoc} */ @@ -398,7 +386,6 @@ public class MapCache implements Cache { /** {@inheritDoc} */ @Override - @SuppressWarnings("unchecked") public T unwrap(Class clazz) { return (T) map; }