Implement missing getFacets() methods. Clean up some logic.

This commit is contained in:
Greg Burd 2017-10-24 21:07:47 -04:00
parent 18f2a057de
commit 8a7dbfdec1
12 changed files with 173 additions and 145 deletions

View file

@ -18,15 +18,14 @@ package net.helenus.core;
import java.io.PrintStream;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import com.google.common.base.Stopwatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.codahale.metrics.MetricRegistry;
import com.datastax.driver.core.*;
import com.datastax.driver.core.querybuilder.BuiltStatement;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Table;
import com.google.common.util.concurrent.ListenableFuture;
@ -78,35 +77,35 @@ public abstract class AbstractSessionOperations {
}
}
public ResultSet execute(Statement statement, boolean showValues) {
return execute(statement, null, null, showValues);
}
public ResultSet execute(Statement statement, boolean showValues) {
return execute(statement, null, null, showValues);
}
public ResultSet execute(Statement statement, Stopwatch timer, boolean showValues) {
return execute(statement, null, timer, showValues);
}
public ResultSet execute(Statement statement, Stopwatch timer, boolean showValues) {
return execute(statement, null, timer, showValues);
}
public ResultSet execute(Statement statement, UnitOfWork uow, boolean showValues) {
return execute(statement, uow, null, showValues);
}
public ResultSet execute(Statement statement, UnitOfWork uow, boolean showValues) {
return execute(statement, uow, null, showValues);
}
public ResultSet execute(Statement statement, UnitOfWork uow, Stopwatch timer, boolean showValues) {
public ResultSet execute(Statement statement, UnitOfWork uow, Stopwatch timer, boolean showValues) {
return executeAsync(statement, uow, timer, showValues).getUninterruptibly();
}
public ResultSetFuture executeAsync(Statement statement, boolean showValues) {
return executeAsync(statement, null, null, showValues);
}
public ResultSetFuture executeAsync(Statement statement, boolean showValues) {
return executeAsync(statement, null, null, showValues);
}
public ResultSetFuture executeAsync(Statement statement, Stopwatch timer, boolean showValues) {
return executeAsync(statement, null, timer, showValues);
}
public ResultSetFuture executeAsync(Statement statement, Stopwatch timer, boolean showValues) {
return executeAsync(statement, null, timer, showValues);
}
public ResultSetFuture executeAsync(Statement statement, UnitOfWork uow, boolean showValues) {
return executeAsync(statement, uow, null, showValues);
}
public ResultSetFuture executeAsync(Statement statement, UnitOfWork uow, boolean showValues) {
return executeAsync(statement, uow, null, showValues);
}
public ResultSetFuture executeAsync(Statement statement, UnitOfWork uow, Stopwatch timer, boolean showValues) {
public ResultSetFuture executeAsync(Statement statement, UnitOfWork uow, Stopwatch timer, boolean showValues) {
try {
log(statement, uow, timer, showValues);
return currentSession().executeAsync(statement);
@ -117,14 +116,14 @@ public abstract class AbstractSessionOperations {
void log(Statement statement, UnitOfWork uow, Stopwatch timer, boolean showValues) {
if (LOG.isInfoEnabled()) {
String timerString = "";
String timerString = "";
String uowString = "";
if (uow != null) {
uowString = (timer != null) ? " " : "" + "UOW(" + uow.hashCode() + ")";
}
if (timer != null) {
timerString = String.format(" %s", timer.toString());
}
timerString = String.format(" %s", timer.toString());
}
LOG.info(String.format("CQL%s%s - %s", uowString, timerString, statement));
}
if (isShowCql()) {

View file

@ -60,19 +60,19 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
}
@Override
public void addDatabaseTime(String name, Stopwatch amount) {
Double time = databaseTime.get(name);
if (time == null) {
databaseTime.put(name, (double)amount.elapsed(TimeUnit.MICROSECONDS));
} else {
databaseTime.put(name, time + amount.elapsed(TimeUnit.MICROSECONDS));
}
}
public void addDatabaseTime(String name, Stopwatch amount) {
Double time = databaseTime.get(name);
if (time == null) {
databaseTime.put(name, (double) amount.elapsed(TimeUnit.MICROSECONDS));
} else {
databaseTime.put(name, time + amount.elapsed(TimeUnit.MICROSECONDS));
}
}
@Override
public void addCacheLookupTime(Stopwatch amount) {
cacheLookupTime += amount.elapsed(TimeUnit.MICROSECONDS);
}
@Override
public void addCacheLookupTime(Stopwatch amount) {
cacheLookupTime += amount.elapsed(TimeUnit.MICROSECONDS);
}
@Override
public void addNestedUnitOfWork(UnitOfWork<E> uow) {

View file

@ -207,11 +207,15 @@ public final class HelenusSession extends AbstractSessionOperations implements C
UnboundFacet unboundFacet = (UnboundFacet) facet;
UnboundFacet.Binder binder = unboundFacet.binder();
for (HelenusProperty prop : unboundFacet.getProperties()) {
Object value;
if (valueMap == null) {
Object value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop, false);
binder.setValueForProperty(prop, value.toString());
value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop, false);
if (value != null) {
binder.setValueForProperty(prop, value.toString());
}
} else {
binder.setValueForProperty(prop, valueMap.get(prop.getPropertyName()).toString());
value = valueMap.get(prop.getPropertyName());
binder.setValueForProperty(prop, value.toString());
}
}
if (binder.isBound()) {

View file

@ -25,8 +25,8 @@ import net.helenus.core.cache.Facet;
public interface UnitOfWork<X extends Exception> extends AutoCloseable {
/**
* Marks the beginning of a transactional section of work. Will write a recordCacheAndDatabaseOperationCount
* to the shared write-ahead log.
* Marks the beginning of a transactional section of work. Will write a
* recordCacheAndDatabaseOperationCount to the shared write-ahead log.
*
* @return the handle used to commit or abort the work.
*/
@ -61,8 +61,8 @@ public interface UnitOfWork<X extends Exception> extends AutoCloseable {
UnitOfWork setPurpose(String purpose);
void addDatabaseTime(String name, Stopwatch amount);
void addCacheLookupTime(Stopwatch amount);
void addDatabaseTime(String name, Stopwatch amount);
void addCacheLookupTime(Stopwatch amount);
// Cache > 0 means "cache hit", < 0 means cache miss.
void recordCacheAndDatabaseOperationCount(int cache, int database);

View file

@ -31,10 +31,6 @@ public abstract class AbstractOperation<E, O extends AbstractOperation<E, O>> ex
public abstract E transform(ResultSet resultSet);
public boolean cacheable() {
return false;
}
public PreparedOperation<E> prepare() {
return new PreparedOperation<E>(prepareStatement(), this);
}

View file

@ -74,9 +74,9 @@ public abstract class AbstractOptionalOperation<E, O extends AbstractOptionalOpe
sessionCacheHits.mark();
cacheHits.mark();
} else {
sessionCacheMiss.mark();
cacheMiss.mark();
}
sessionCacheMiss.mark();
cacheMiss.mark();
}
}
if (!result.isPresent()) {
@ -112,37 +112,37 @@ public abstract class AbstractOptionalOperation<E, O extends AbstractOptionalOpe
boolean updateCache = true;
if (enableCache) {
Stopwatch timer = Stopwatch.createStarted();
try {
List<Facet> facets = bindFacetValues();
cachedResult = checkCache(uow, facets);
if (cachedResult != null) {
result = Optional.of(cachedResult);
updateCache = false;
uowCacheHits.mark();
cacheHits.mark();
uow.recordCacheAndDatabaseOperationCount(1, 0);
} else {
uowCacheMiss.mark();
if (isSessionCacheable()) {
String tableName = CacheUtil.schemaName(facets);
cachedResult = (E) sessionOps.checkCache(tableName, facets);
if (cachedResult != null) {
result = Optional.of(cachedResult);
sessionCacheHits.mark();
cacheHits.mark();
uow.recordCacheAndDatabaseOperationCount(1, 0);
} else {
sessionCacheMiss.mark();
cacheMiss.mark();
uow.recordCacheAndDatabaseOperationCount(-1, 0);
}
}
}
} finally {
timer.stop();
uow.addCacheLookupTime(timer);
}
Stopwatch timer = Stopwatch.createStarted();
try {
List<Facet> facets = bindFacetValues();
cachedResult = checkCache(uow, facets);
if (cachedResult != null) {
result = Optional.of(cachedResult);
updateCache = false;
uowCacheHits.mark();
cacheHits.mark();
uow.recordCacheAndDatabaseOperationCount(1, 0);
} else {
uowCacheMiss.mark();
if (isSessionCacheable()) {
String tableName = CacheUtil.schemaName(facets);
cachedResult = (E) sessionOps.checkCache(tableName, facets);
if (cachedResult != null) {
result = Optional.of(cachedResult);
sessionCacheHits.mark();
cacheHits.mark();
uow.recordCacheAndDatabaseOperationCount(1, 0);
} else {
sessionCacheMiss.mark();
cacheMiss.mark();
uow.recordCacheAndDatabaseOperationCount(-1, 0);
}
}
}
} finally {
timer.stop();
uow.addCacheLookupTime(timer);
}
}
if (!result.isPresent()) {

View file

@ -338,11 +338,17 @@ public abstract class AbstractStatementOperation<E, O extends AbstractStatementO
UnboundFacet unboundFacet = (UnboundFacet) facet;
UnboundFacet.Binder binder = unboundFacet.binder();
for (HelenusProperty prop : unboundFacet.getProperties()) {
Object value;
if (valueMap == null) {
Object value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop, false);
binder.setValueForProperty(prop, value.toString());
value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop, false);
if (value != null) {
binder.setValueForProperty(prop, value.toString());
}
} else {
binder.setValueForProperty(prop, valueMap.get(prop.getPropertyName()).toString());
value = valueMap.get(prop.getPropertyName());
if (value != null) {
binder.setValueForProperty(prop, value.toString());
}
}
}
if (binder.isBound()) {

View file

@ -75,9 +75,9 @@ public abstract class AbstractStreamOperation<E, O extends AbstractStreamOperati
sessionCacheHits.mark();
cacheHits.mark();
} else {
sessionCacheMiss.mark();
cacheMiss.mark();
}
sessionCacheMiss.mark();
cacheMiss.mark();
}
}
if (resultStream == null) {
@ -120,35 +120,35 @@ public abstract class AbstractStreamOperation<E, O extends AbstractStreamOperati
if (enableCache) {
Stopwatch timer = Stopwatch.createStarted();
try {
List<Facet> facets = bindFacetValues();
cachedResult = checkCache(uow, facets);
if (cachedResult != null) {
resultStream = Stream.of(cachedResult);
updateCache = false;
uowCacheHits.mark();
cacheHits.mark();
uow.recordCacheAndDatabaseOperationCount(1, 0);
} else {
uowCacheMiss.mark();
if (isSessionCacheable()) {
String tableName = CacheUtil.schemaName(facets);
cachedResult = (E) sessionOps.checkCache(tableName, facets);
if (cachedResult != null) {
resultStream = Stream.of(cachedResult);
sessionCacheHits.mark();
cacheHits.mark();
uow.recordCacheAndDatabaseOperationCount(1, 0);
} else {
sessionCacheMiss.mark();
cacheMiss.mark();
uow.recordCacheAndDatabaseOperationCount(-1, 0);
}
}
}
} finally {
timer.stop();
uow.addCacheLookupTime(timer);
}
List<Facet> facets = bindFacetValues();
cachedResult = checkCache(uow, facets);
if (cachedResult != null) {
resultStream = Stream.of(cachedResult);
updateCache = false;
uowCacheHits.mark();
cacheHits.mark();
uow.recordCacheAndDatabaseOperationCount(1, 0);
} else {
uowCacheMiss.mark();
if (isSessionCacheable()) {
String tableName = CacheUtil.schemaName(facets);
cachedResult = (E) sessionOps.checkCache(tableName, facets);
if (cachedResult != null) {
resultStream = Stream.of(cachedResult);
sessionCacheHits.mark();
cacheHits.mark();
uow.recordCacheAndDatabaseOperationCount(1, 0);
} else {
sessionCacheMiss.mark();
cacheMiss.mark();
uow.recordCacheAndDatabaseOperationCount(-1, 0);
}
}
}
} finally {
timer.stop();
uow.addCacheLookupTime(timer);
}
}
if (resultStream == null) {

View file

@ -27,6 +27,7 @@ import net.helenus.core.AbstractSessionOperations;
import net.helenus.core.Getter;
import net.helenus.core.Helenus;
import net.helenus.core.UnitOfWork;
import net.helenus.core.cache.Facet;
import net.helenus.core.reflect.DefaultPrimitiveTypes;
import net.helenus.core.reflect.Drafted;
import net.helenus.core.reflect.HelenusPropertyNode;
@ -246,4 +247,14 @@ public final class InsertOperation<T> extends AbstractOperation<T, InsertOperati
}
return result;
}
@Override
public List<Facet> getFacets() {
if (entity != null) {
return entity.getFacets();
} else {
return new ArrayList<Facet>();
}
}
}

View file

@ -15,6 +15,7 @@
*/
package net.helenus.core.operation;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
@ -36,23 +37,23 @@ import net.helenus.core.cache.Facet;
public abstract class Operation<E> {
protected final AbstractSessionOperations sessionOps;
protected final Meter uowCacheHits;
protected final Meter uowCacheMiss;
protected final Meter sessionCacheHits;
protected final Meter sessionCacheMiss;
protected final Meter cacheHits;
protected final Meter cacheMiss;
protected final Meter uowCacheHits;
protected final Meter uowCacheMiss;
protected final Meter sessionCacheHits;
protected final Meter sessionCacheMiss;
protected final Meter cacheHits;
protected final Meter cacheMiss;
protected final Timer requestLatency;
Operation(AbstractSessionOperations sessionOperations) {
this.sessionOps = sessionOperations;
MetricRegistry metrics = sessionOperations.getMetricRegistry();
this.uowCacheHits = metrics.meter("net.helenus.UOW-cache-hits");
this.uowCacheMiss = metrics.meter("net.helenus.UOW-cache-miss");
this.sessionCacheHits = metrics.meter("net.helenus.session-cache-hits");
this.sessionCacheMiss = metrics.meter("net.helenus.session-cache-miss");
this.cacheHits = metrics.meter("net.helenus.cache-hits");
this.cacheMiss = metrics.meter("net.helenus.cache-miss");
this.uowCacheHits = metrics.meter("net.helenus.UOW-cache-hits");
this.uowCacheMiss = metrics.meter("net.helenus.UOW-cache-miss");
this.sessionCacheHits = metrics.meter("net.helenus.session-cache-hits");
this.sessionCacheMiss = metrics.meter("net.helenus.session-cache-miss");
this.cacheHits = metrics.meter("net.helenus.cache-hits");
this.cacheMiss = metrics.meter("net.helenus.cache-miss");
this.requestLatency = metrics.timer("net.helenus.request-latency");
}
@ -77,17 +78,17 @@ public abstract class Operation<E> {
Statement statement = options(buildStatement(cached));
Stopwatch timer = Stopwatch.createStarted();
try {
ResultSetFuture futureResultSet = session.executeAsync(statement, uow, timer, showValues);
if (uow != null)
uow.recordCacheAndDatabaseOperationCount(0, 1);
ResultSet resultSet = futureResultSet.getUninterruptibly(); // TODO(gburd): (timeout, units);
return resultSet;
ResultSetFuture futureResultSet = session.executeAsync(statement, uow, timer, showValues);
if (uow != null)
uow.recordCacheAndDatabaseOperationCount(0, 1);
ResultSet resultSet = futureResultSet.getUninterruptibly(); // TODO(gburd): (timeout, units);
return resultSet;
} finally {
timer.stop();
if (uow != null)
uow.addDatabaseTime("Cassandra", timer);
}
} finally {
timer.stop();
if (uow != null)
uow.addDatabaseTime("Cassandra", timer);
}
} finally {
@ -106,7 +107,7 @@ public abstract class Operation<E> {
}
public List<Facet> getFacets() {
return null;
return new ArrayList<Facet>();
}
public List<Facet> bindFacetValues() {

View file

@ -25,6 +25,7 @@ import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Update;
import net.helenus.core.*;
import net.helenus.core.cache.Facet;
import net.helenus.core.reflect.HelenusPropertyNode;
import net.helenus.mapping.HelenusEntity;
import net.helenus.mapping.HelenusProperty;
@ -579,4 +580,14 @@ public final class UpdateOperation<E> extends AbstractFilterOperation<E, UpdateO
}
return result;
}
@Override
public List<Facet> getFacets() {
if (entity != null) {
return entity.getFacets();
} else {
return new ArrayList<Facet>();
}
}
}

View file

@ -129,11 +129,11 @@ public final class HelenusMappingEntity implements HelenusEntity {
primaryKeyProperties = null;
}
for (ConstraintValidator<?, ?> constraint : MappingUtil.getValidators(prop.getGetterMethod())) {
if (constraint.getClass().isAssignableFrom(DistinctValidator.class))
;
UnboundFacet facet = new UnboundFacet(prop);
facetsBuilder.add(facet);
break;
if (constraint.getClass().isAssignableFrom(DistinctValidator.class)) {
UnboundFacet facet = new UnboundFacet(prop);
facetsBuilder.add(facet);
break;
}
}
}
}