WIP: combinations for flattening facets to keys.
This commit is contained in:
parent
63c558581b
commit
e36dded9d2
4 changed files with 76 additions and 12 deletions
|
@ -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;
|
||||
|
@ -36,7 +37,7 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
|
|||
|
||||
private String purpose_;
|
||||
private Stopwatch elapsedTime_;
|
||||
public Stopwatch databaseTime_;
|
||||
public Stopwatch databaseTime_ = Stopwatch.createUnstarted();
|
||||
|
||||
// Cache:
|
||||
private final Table<String, String, Object> cache = HashBasedTable.create();
|
||||
|
@ -48,6 +49,11 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
|
|||
this.parent = parent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stopwatch getExecutionTimer() {
|
||||
return databaseTime_;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addNestedUnitOfWork(UnitOfWork<E> uow) {
|
||||
synchronized (nested) {
|
||||
|
@ -57,6 +63,7 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
|
|||
|
||||
@Override
|
||||
public UnitOfWork<E> begin() {
|
||||
elapsedTime_.start();
|
||||
// log.record(txn::start)
|
||||
return this;
|
||||
}
|
||||
|
@ -91,12 +98,18 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
|
|||
// Be sure to check all enclosing UnitOfWork caches as well, we may be nested.
|
||||
if (parent != null) {
|
||||
return parent.cacheLookup(facets);
|
||||
} else {
|
||||
}/* else {
|
||||
Cache<String, Object> cache = session.getSessionCache();
|
||||
|
||||
cache.getIfPresent(key)
|
||||
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<E extends Exception> implements UnitOfW
|
|||
// Merge UOW cache into parent's cache.
|
||||
if (parent != null) {
|
||||
parent.mergeCache(cache);
|
||||
} else {
|
||||
} /*else {
|
||||
Cache<String, Object> cache = session.getSessionCache();
|
||||
Map<String, Object> 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) {
|
||||
|
|
|
@ -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<X extends Exception> extends AutoCloseable {
|
||||
|
@ -56,4 +57,6 @@ public interface UnitOfWork<X extends Exception> extends AutoCloseable {
|
|||
Optional<Object> cacheLookup(List<Facet> facets);
|
||||
|
||||
void cacheUpdate(Object pojo, List<Facet> facets);
|
||||
|
||||
Stopwatch getExecutionTimer();
|
||||
}
|
||||
|
|
30
src/main/java/net/helenus/core/cache/CacheUtil.java
vendored
Normal file
30
src/main/java/net/helenus/core/cache/CacheUtil.java
vendored
Normal file
|
@ -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<String[]> 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<String[]> out = new ArrayList<String[]>((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<String[]> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<E> {
|
|||
}
|
||||
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue