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

View file

@ -60,19 +60,19 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
} }
@Override @Override
public void addDatabaseTime(String name, Stopwatch amount) { public void addDatabaseTime(String name, Stopwatch amount) {
Double time = databaseTime.get(name); Double time = databaseTime.get(name);
if (time == null) { if (time == null) {
databaseTime.put(name, (double)amount.elapsed(TimeUnit.MICROSECONDS)); databaseTime.put(name, (double) amount.elapsed(TimeUnit.MICROSECONDS));
} else { } else {
databaseTime.put(name, time + amount.elapsed(TimeUnit.MICROSECONDS)); databaseTime.put(name, time + amount.elapsed(TimeUnit.MICROSECONDS));
} }
} }
@Override @Override
public void addCacheLookupTime(Stopwatch amount) { public void addCacheLookupTime(Stopwatch amount) {
cacheLookupTime += amount.elapsed(TimeUnit.MICROSECONDS); cacheLookupTime += amount.elapsed(TimeUnit.MICROSECONDS);
} }
@Override @Override
public void addNestedUnitOfWork(UnitOfWork<E> uow) { 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 unboundFacet = (UnboundFacet) facet;
UnboundFacet.Binder binder = unboundFacet.binder(); UnboundFacet.Binder binder = unboundFacet.binder();
for (HelenusProperty prop : unboundFacet.getProperties()) { for (HelenusProperty prop : unboundFacet.getProperties()) {
Object value;
if (valueMap == null) { if (valueMap == null) {
Object value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop, false); value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop, false);
binder.setValueForProperty(prop, value.toString()); if (value != null) {
binder.setValueForProperty(prop, value.toString());
}
} else { } else {
binder.setValueForProperty(prop, valueMap.get(prop.getPropertyName()).toString()); value = valueMap.get(prop.getPropertyName());
binder.setValueForProperty(prop, value.toString());
} }
} }
if (binder.isBound()) { if (binder.isBound()) {

View file

@ -25,8 +25,8 @@ import net.helenus.core.cache.Facet;
public interface UnitOfWork<X extends Exception> extends AutoCloseable { public interface UnitOfWork<X extends Exception> extends AutoCloseable {
/** /**
* Marks the beginning of a transactional section of work. Will write a recordCacheAndDatabaseOperationCount * Marks the beginning of a transactional section of work. Will write a
* to the shared write-ahead log. * recordCacheAndDatabaseOperationCount to the shared write-ahead log.
* *
* @return the handle used to commit or abort the work. * @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); UnitOfWork setPurpose(String purpose);
void addDatabaseTime(String name, Stopwatch amount); void addDatabaseTime(String name, Stopwatch amount);
void addCacheLookupTime(Stopwatch amount); void addCacheLookupTime(Stopwatch amount);
// Cache > 0 means "cache hit", < 0 means cache miss. // Cache > 0 means "cache hit", < 0 means cache miss.
void recordCacheAndDatabaseOperationCount(int cache, int database); 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 abstract E transform(ResultSet resultSet);
public boolean cacheable() {
return false;
}
public PreparedOperation<E> prepare() { public PreparedOperation<E> prepare() {
return new PreparedOperation<E>(prepareStatement(), this); return new PreparedOperation<E>(prepareStatement(), this);
} }

View file

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

View file

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

View file

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

View file

@ -27,6 +27,7 @@ import net.helenus.core.AbstractSessionOperations;
import net.helenus.core.Getter; import net.helenus.core.Getter;
import net.helenus.core.Helenus; import net.helenus.core.Helenus;
import net.helenus.core.UnitOfWork; import net.helenus.core.UnitOfWork;
import net.helenus.core.cache.Facet;
import net.helenus.core.reflect.DefaultPrimitiveTypes; import net.helenus.core.reflect.DefaultPrimitiveTypes;
import net.helenus.core.reflect.Drafted; import net.helenus.core.reflect.Drafted;
import net.helenus.core.reflect.HelenusPropertyNode; import net.helenus.core.reflect.HelenusPropertyNode;
@ -246,4 +247,14 @@ public final class InsertOperation<T> extends AbstractOperation<T, InsertOperati
} }
return result; 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; package net.helenus.core.operation;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -36,23 +37,23 @@ import net.helenus.core.cache.Facet;
public abstract class Operation<E> { public abstract class Operation<E> {
protected final AbstractSessionOperations sessionOps; protected final AbstractSessionOperations sessionOps;
protected final Meter uowCacheHits; protected final Meter uowCacheHits;
protected final Meter uowCacheMiss; protected final Meter uowCacheMiss;
protected final Meter sessionCacheHits; protected final Meter sessionCacheHits;
protected final Meter sessionCacheMiss; protected final Meter sessionCacheMiss;
protected final Meter cacheHits; protected final Meter cacheHits;
protected final Meter cacheMiss; protected final Meter cacheMiss;
protected final Timer requestLatency; protected final Timer requestLatency;
Operation(AbstractSessionOperations sessionOperations) { Operation(AbstractSessionOperations sessionOperations) {
this.sessionOps = sessionOperations; this.sessionOps = sessionOperations;
MetricRegistry metrics = sessionOperations.getMetricRegistry(); MetricRegistry metrics = sessionOperations.getMetricRegistry();
this.uowCacheHits = metrics.meter("net.helenus.UOW-cache-hits"); this.uowCacheHits = metrics.meter("net.helenus.UOW-cache-hits");
this.uowCacheMiss = metrics.meter("net.helenus.UOW-cache-miss"); this.uowCacheMiss = metrics.meter("net.helenus.UOW-cache-miss");
this.sessionCacheHits = metrics.meter("net.helenus.session-cache-hits"); this.sessionCacheHits = metrics.meter("net.helenus.session-cache-hits");
this.sessionCacheMiss = metrics.meter("net.helenus.session-cache-miss"); this.sessionCacheMiss = metrics.meter("net.helenus.session-cache-miss");
this.cacheHits = metrics.meter("net.helenus.cache-hits"); this.cacheHits = metrics.meter("net.helenus.cache-hits");
this.cacheMiss = metrics.meter("net.helenus.cache-miss"); this.cacheMiss = metrics.meter("net.helenus.cache-miss");
this.requestLatency = metrics.timer("net.helenus.request-latency"); this.requestLatency = metrics.timer("net.helenus.request-latency");
} }
@ -77,17 +78,17 @@ public abstract class Operation<E> {
Statement statement = options(buildStatement(cached)); Statement statement = options(buildStatement(cached));
Stopwatch timer = Stopwatch.createStarted(); Stopwatch timer = Stopwatch.createStarted();
try { try {
ResultSetFuture futureResultSet = session.executeAsync(statement, uow, timer, showValues); ResultSetFuture futureResultSet = session.executeAsync(statement, uow, timer, showValues);
if (uow != null) if (uow != null)
uow.recordCacheAndDatabaseOperationCount(0, 1); uow.recordCacheAndDatabaseOperationCount(0, 1);
ResultSet resultSet = futureResultSet.getUninterruptibly(); // TODO(gburd): (timeout, units); ResultSet resultSet = futureResultSet.getUninterruptibly(); // TODO(gburd): (timeout, units);
return resultSet; return resultSet;
} finally { } finally {
timer.stop(); timer.stop();
if (uow != null) if (uow != null)
uow.addDatabaseTime("Cassandra", timer); uow.addDatabaseTime("Cassandra", timer);
} }
} finally { } finally {
@ -106,7 +107,7 @@ public abstract class Operation<E> {
} }
public List<Facet> getFacets() { public List<Facet> getFacets() {
return null; return new ArrayList<Facet>();
} }
public List<Facet> bindFacetValues() { 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 com.datastax.driver.core.querybuilder.Update;
import net.helenus.core.*; import net.helenus.core.*;
import net.helenus.core.cache.Facet;
import net.helenus.core.reflect.HelenusPropertyNode; import net.helenus.core.reflect.HelenusPropertyNode;
import net.helenus.mapping.HelenusEntity; import net.helenus.mapping.HelenusEntity;
import net.helenus.mapping.HelenusProperty; import net.helenus.mapping.HelenusProperty;
@ -579,4 +580,14 @@ public final class UpdateOperation<E> extends AbstractFilterOperation<E, UpdateO
} }
return result; 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; primaryKeyProperties = null;
} }
for (ConstraintValidator<?, ?> constraint : MappingUtil.getValidators(prop.getGetterMethod())) { for (ConstraintValidator<?, ?> constraint : MappingUtil.getValidators(prop.getGetterMethod())) {
if (constraint.getClass().isAssignableFrom(DistinctValidator.class)) if (constraint.getClass().isAssignableFrom(DistinctValidator.class)) {
; UnboundFacet facet = new UnboundFacet(prop);
UnboundFacet facet = new UnboundFacet(prop); facetsBuilder.add(facet);
facetsBuilder.add(facet); break;
break; }
} }
} }
} }