From e36dded9d27cecaa94a40249ee08fbc80e590ca7 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Sun, 22 Oct 2017 21:36:10 -0400 Subject: [PATCH] WIP: combinations for flattening facets to keys. --- .../net/helenus/core/AbstractUnitOfWork.java | 47 ++++++++++++++----- .../java/net/helenus/core/UnitOfWork.java | 3 ++ .../net/helenus/core/cache/CacheUtil.java | 30 ++++++++++++ .../net/helenus/core/operation/Operation.java | 8 +++- 4 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 src/main/java/net/helenus/core/cache/CacheUtil.java diff --git a/src/main/java/net/helenus/core/AbstractUnitOfWork.java b/src/main/java/net/helenus/core/AbstractUnitOfWork.java index bea74ca..89b9f26 100644 --- a/src/main/java/net/helenus/core/AbstractUnitOfWork.java +++ b/src/main/java/net/helenus/core/AbstractUnitOfWork.java @@ -18,6 +18,7 @@ package net.helenus.core; import java.util.*; import com.diffplug.common.base.Errors; +import com.google.common.base.Stopwatch; import com.google.common.cache.Cache; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; @@ -34,9 +35,9 @@ public abstract class AbstractUnitOfWork implements UnitOfW private boolean aborted = false; private boolean committed = false; - private String purpose_; - private Stopwatch elapsedTime_; - public Stopwatch databaseTime_; + private String purpose_; + private Stopwatch elapsedTime_; + public Stopwatch databaseTime_ = Stopwatch.createUnstarted(); // Cache: private final Table cache = HashBasedTable.create(); @@ -48,6 +49,11 @@ public abstract class AbstractUnitOfWork implements UnitOfW this.parent = parent; } + @Override + public Stopwatch getExecutionTimer() { + return databaseTime_; + } + @Override public void addNestedUnitOfWork(UnitOfWork uow) { synchronized (nested) { @@ -57,6 +63,7 @@ public abstract class AbstractUnitOfWork implements UnitOfW @Override public UnitOfWork begin() { + elapsedTime_.start(); // log.record(txn::start) return this; } @@ -91,11 +98,17 @@ public abstract class AbstractUnitOfWork implements UnitOfW // Be sure to check all enclosing UnitOfWork caches as well, we may be nested. if (parent != null) { return parent.cacheLookup(facets); - } else { - Cache cache = session.getSessionCache(); - - cache.getIfPresent(key) - } + }/* else { + Cache cache = session.getSessionCache(); + String[] keys = flattenFacets(facets); + for (String key : keys) { + Object value = cache.getIfPresent(key); + if (value != null) { + result = Optional.of(value); + break; + } + } + }*/ } return result; } @@ -150,10 +163,22 @@ public abstract class AbstractUnitOfWork implements UnitOfW // Merge UOW cache into parent's cache. if (parent != null) { parent.mergeCache(cache); - } else { - Cache cache = session.getSessionCache(); - cache.put + } /*else { + Cache cache = session.getSessionCache(); + Map rowMap = this.cache.rowMap(); + for (String rowKey : rowMap.keySet()) { + String keys = flattenFacets(facets); + for (String key : keys) { + Object value = cache.getIfPresent(key); + if (value != null) { + result = Optional.of(value); + break; + } + } + } + cache.put } + */ // Apply all post-commit functions for if (parent == null) { diff --git a/src/main/java/net/helenus/core/UnitOfWork.java b/src/main/java/net/helenus/core/UnitOfWork.java index 0242a00..2049fb5 100644 --- a/src/main/java/net/helenus/core/UnitOfWork.java +++ b/src/main/java/net/helenus/core/UnitOfWork.java @@ -18,6 +18,7 @@ package net.helenus.core; import java.util.List; import java.util.Optional; +import com.google.common.base.Stopwatch; import net.helenus.core.cache.Facet; public interface UnitOfWork extends AutoCloseable { @@ -56,4 +57,6 @@ public interface UnitOfWork extends AutoCloseable { Optional cacheLookup(List facets); void cacheUpdate(Object pojo, List facets); + + Stopwatch getExecutionTimer(); } diff --git a/src/main/java/net/helenus/core/cache/CacheUtil.java b/src/main/java/net/helenus/core/cache/CacheUtil.java new file mode 100644 index 0000000..ef26818 --- /dev/null +++ b/src/main/java/net/helenus/core/cache/CacheUtil.java @@ -0,0 +1,30 @@ +package net.helenus.core.cache; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class CacheUtil { + public static List comb(String... items) { + int n = items.length; + if (n > 20 || n < 0) throw new IllegalArgumentException(n + " is out of range"); + long e = Math.round(Math.pow(2, n)); + List out = new ArrayList((int) e - 1); + Arrays.sort(items); + for (int k = 1; k <= items.length; k++) { + kcomb(items, 0, k, new String[k], out); + } + return out; + } + + private static void kcomb(String[] items, int n, int k, String[] arr, List out) { + if (k == 0) { + out.add(arr.clone()); + } else { + for (int i = n; i <= items.length - k; i++) { + arr[arr.length - k] = items[i]; + kcomb(items, i + 1, k - 1, arr, out); + } + } + } +} diff --git a/src/main/java/net/helenus/core/operation/Operation.java b/src/main/java/net/helenus/core/operation/Operation.java index 62fb849..64c6a39 100644 --- a/src/main/java/net/helenus/core/operation/Operation.java +++ b/src/main/java/net/helenus/core/operation/Operation.java @@ -29,6 +29,7 @@ import com.datastax.driver.core.Statement; import brave.Span; import brave.Tracer; import brave.propagation.TraceContext; +import com.google.common.base.Stopwatch; import net.helenus.core.AbstractSessionOperations; import net.helenus.core.UnitOfWork; import net.helenus.core.cache.Facet; @@ -67,8 +68,13 @@ public abstract class Operation { } Statement statement = options(buildStatement(cached)); + Stopwatch timer = uow.getExecutionTimer(); + timer.start(); ResultSetFuture futureResultSet = session.executeAsync(statement, showValues); - return futureResultSet.getUninterruptibly(); //TODO(gburd): (timeout, units); + ResultSet resultSet = futureResultSet.getUninterruptibly(); //TODO(gburd): (timeout, units); + timer.stop(); + return resultSet; + } finally { if (span != null) {