Formatting

This commit is contained in:
Greg Burd 2017-10-12 16:18:17 -04:00
parent 306c9e68ef
commit 74832a32dd
56 changed files with 1133 additions and 1095 deletions

View file

@ -1,11 +1,9 @@
package net.helenus.core;
import net.helenus.core.reflect.MapExportable;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import net.helenus.core.reflect.MapExportable;
public abstract class AbstractAuditedEntityDraft<E> extends AbstractEntityDraft<E> {
@ -37,5 +35,4 @@ public abstract class AbstractAuditedEntityDraft<E> extends AbstractEntityDraft<
public Date createdAt() {
return (Date) get("createdAt", Date.class);
}
}

View file

@ -1,22 +1,18 @@
package net.helenus.core;
import java.util.*;
import com.google.common.primitives.Primitives;
import java.util.*;
import net.helenus.core.reflect.DefaultPrimitiveTypes;
import net.helenus.core.reflect.Drafted;
import net.helenus.core.reflect.MapExportable;
import net.helenus.mapping.MappingUtil;
public abstract class AbstractEntityDraft<E> implements Drafted<E> {
private final Map<String, Object> backingMap = new HashMap<String, Object>();
private final MapExportable entity;
private final Map<String, Object> entityMap;
public AbstractEntityDraft(MapExportable entity) {
this.entity = entity;
this.entityMap = entity != null ? entity.toMap() : new HashMap<String, Object>();
@ -24,7 +20,9 @@ public abstract class AbstractEntityDraft<E> implements Drafted<E> {
public abstract Class<E> getEntityClass();
public E build() { return Helenus.map(getEntityClass(), toMap()); }
public E build() {
return Helenus.map(getEntityClass(), toMap());
}
@SuppressWarnings("unchecked")
protected <T> T get(Getter<T> getter, Class<?> returnType) {
@ -96,9 +94,7 @@ public abstract class AbstractEntityDraft<E> implements Drafted<E> {
}
private <T> String methodNameFor(Getter<T> getter) {
return MappingUtil.resolveMappingProperty(getter)
.getProperty()
.getPropertyName();
return MappingUtil.resolveMappingProperty(getter).getProperty().getPropertyName();
}
public <T> Object unset(Getter<T> getter) {
@ -135,7 +131,7 @@ public abstract class AbstractEntityDraft<E> implements Drafted<E> {
return toMap(entityMap);
}
public Map<String, Object> toMap(Map<String, Object>entityMap) {
public Map<String, Object> toMap(Map<String, Object> entityMap) {
Map<String, Object> combined;
if (entityMap != null && entityMap.size() > 0) {
combined = new HashMap<String, Object>(entityMap.size());
@ -160,5 +156,4 @@ public abstract class AbstractEntityDraft<E> implements Drafted<E> {
public String toString() {
return backingMap.toString();
}
}

View file

@ -22,7 +22,6 @@ import com.datastax.driver.core.querybuilder.BuiltStatement;
import com.google.common.util.concurrent.ListenableFuture;
import java.io.PrintStream;
import java.util.concurrent.Executor;
import net.helenus.mapping.value.ColumnValuePreparer;
import net.helenus.mapping.value.ColumnValueProvider;
import net.helenus.support.HelenusException;
@ -124,5 +123,4 @@ public abstract class AbstractSessionOperations {
void printCql(String cql) {
getPrintStream().println(cql);
}
}

View file

@ -17,10 +17,8 @@ package net.helenus.core;
import com.diffplug.common.base.Errors;
import com.google.common.collect.TreeTraverser;
import java.util.*;
/** Encapsulates the concept of a "transaction" as a unit-of-work. */
public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfWork, AutoCloseable {
private final List<AbstractUnitOfWork<E>> nested = new ArrayList<>();
@ -70,7 +68,9 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
return null;
}
public Map<String, Set<Object>> getCache() { return cache; }
public Map<String, Set<Object>> getCache() {
return cache;
}
private Iterator<AbstractUnitOfWork<E>> getChildNodes() {
return nested.iterator();
@ -85,7 +85,8 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
public PostCommitFunction<Void, Void> commit() throws E {
// All nested UnitOfWork should be committed (not aborted) before calls to commit, check.
boolean canCommit = true;
TreeTraverser<AbstractUnitOfWork<E>> traverser = TreeTraverser.using(node -> node::getChildNodes);
TreeTraverser<AbstractUnitOfWork<E>> traverser =
TreeTraverser.using(node -> node::getChildNodes);
for (AbstractUnitOfWork<E> uow : traverser.postOrderTraversal(this)) {
if (this != uow) {
canCommit &= (!uow.aborted && uow.committed);
@ -112,7 +113,8 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
if (parentCache.containsKey(key)) {
// merge the sets
Set<Object> ps = parentCache.get(key);
ps.addAll(cache.get(key)); //TODO(gburd): review this, likely not correct in all cases as-is.
ps.addAll(
cache.get(key)); //TODO(gburd): review this, likely not correct in all cases as-is.
} else {
// add the missing set
parentCache.put(key, cache.get(key));
@ -122,7 +124,10 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
// Apply all post-commit functions for
if (parent == null) {
traverser.postOrderTraversal(this).forEach(uow -> {
traverser
.postOrderTraversal(this)
.forEach(
uow -> {
uow.applyPostCommitFunctions();
});
return new PostCommitFunction(this, null);
@ -137,8 +142,12 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
/* Explicitly discard the work and mark it as as such in the log. */
public void abort() {
TreeTraverser<AbstractUnitOfWork<E>> traverser = TreeTraverser.using(node -> node::getChildNodes);
traverser.postOrderTraversal(this).forEach(uow -> {
TreeTraverser<AbstractUnitOfWork<E>> traverser =
TreeTraverser.using(node -> node::getChildNodes);
traverser
.postOrderTraversal(this)
.forEach(
uow -> {
uow.committed = false;
uow.aborted = true;
});
@ -165,5 +174,4 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
public boolean hasCommitted() {
return committed;
}
}

View file

@ -1,6 +1,5 @@
package net.helenus.core;
import java.util.function.Function;
@FunctionalInterface
public interface CommitThunk {

View file

@ -15,10 +15,21 @@
*/
package net.helenus.core;
import static net.helenus.core.Query.eq;
import brave.Tracer;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.datastax.driver.core.*;
import java.io.Closeable;
import java.io.PrintStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.function.Function;
import net.helenus.core.operation.*;
import net.helenus.core.reflect.Drafted;
import net.helenus.core.reflect.HelenusPropertyNode;
@ -33,19 +44,6 @@ import net.helenus.support.Fun.Tuple6;
import net.helenus.support.HelenusException;
import net.helenus.support.HelenusMappingException;
import java.io.Closeable;
import java.io.PrintStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.function.Function;
import static net.helenus.core.Query.eq;
public final class HelenusSession extends AbstractSessionOperations implements Closeable {
private final int MAX_CACHE_SIZE = 10000;
@ -69,7 +67,6 @@ public final class HelenusSession extends AbstractSessionOperations implements C
private final StatementColumnValuePreparer valuePreparer;
private final Metadata metadata;
HelenusSession(
Session session,
String usingKeyspace,
@ -177,9 +174,13 @@ public final class HelenusSession extends AbstractSessionOperations implements C
}
@Override
public boolean getDefaultQueryIdempotency() { return defaultQueryIdempotency; }
public boolean getDefaultQueryIdempotency() {
return defaultQueryIdempotency;
}
public Metadata getMetadata() { return metadata; }
public Metadata getMetadata() {
return metadata;
}
public synchronized <T extends UnitOfWork> T begin() {
return begin(null);
@ -188,20 +189,27 @@ public final class HelenusSession extends AbstractSessionOperations implements C
public synchronized <T extends UnitOfWork> T begin(T parent) {
try {
Class<? extends UnitOfWork> clazz = unitOfWorkClass;
Constructor<? extends UnitOfWork> ctor = clazz.getConstructor(HelenusSession.class, UnitOfWork.class);
Constructor<? extends UnitOfWork> ctor =
clazz.getConstructor(HelenusSession.class, UnitOfWork.class);
UnitOfWork uow = ctor.newInstance(this, parent);
if (parent != null) {
parent.addNestedUnitOfWork(uow);
}
return (T) uow.begin();
}
catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new HelenusException(String.format("Unable to instantiate {} as a UnitOfWork.", unitOfWorkClass.getSimpleName()), e);
} catch (NoSuchMethodException
| InvocationTargetException
| InstantiationException
| IllegalAccessException e) {
throw new HelenusException(
String.format(
"Unable to instantiate {} as a UnitOfWork.", unitOfWorkClass.getSimpleName()),
e);
}
}
public <E> SelectOperation<E> select(E pojo) {
Objects.requireNonNull(pojo, "supplied object must be a dsl for a registered entity but cannot be null");
Objects.requireNonNull(
pojo, "supplied object must be a dsl for a registered entity but cannot be null");
ColumnValueProvider valueProvider = getValueProvider();
HelenusEntity entity = Helenus.resolve(pojo);
Class<?> entityClass = entity.getMappingInterface();
@ -239,7 +247,8 @@ public final class HelenusSession extends AbstractSessionOperations implements C
}
public <E> SelectOperation<Row> selectAll(E pojo) {
Objects.requireNonNull(pojo, "supplied object must be a dsl for a registered entity but cannot be null");
Objects.requireNonNull(
pojo, "supplied object must be a dsl for a registered entity but cannot be null");
HelenusEntity entity = Helenus.resolve(pojo);
return new SelectOperation<Row>(this, entity);
}
@ -411,16 +420,20 @@ public final class HelenusSession extends AbstractSessionOperations implements C
public <E> UpdateOperation<E> update(Drafted<E> drafted) {
if (drafted instanceof AbstractEntityDraft == false) {
throw new HelenusMappingException("update of draft objects that don't inherit from AbstractEntityDraft is not yet supported");
throw new HelenusMappingException(
"update of draft objects that don't inherit from AbstractEntityDraft is not yet supported");
}
AbstractEntityDraft<E> draft = (AbstractEntityDraft<E>)drafted;
AbstractEntityDraft<E> draft = (AbstractEntityDraft<E>) drafted;
UpdateOperation update = new UpdateOperation<E>(this, draft);
Map<String, Object> map = draft.toMap();
Set<String> mutatedProperties = draft.mutated();
HelenusEntity entity = Helenus.entity(draft.getEntityClass());
// Add all the mutated values contained in the draft.
entity.getOrderedProperties().forEach(property -> {
entity
.getOrderedProperties()
.forEach(
property -> {
switch (property.getColumnType()) {
case PARTITION_KEY:
case CLUSTERING_COLUMN:
@ -429,10 +442,12 @@ public final class HelenusSession extends AbstractSessionOperations implements C
String propertyName = property.getPropertyName();
if (mutatedProperties.contains(propertyName)) {
Object value = map.get(propertyName);
Getter<Object> getter = new Getter<Object>() {
Getter<Object> getter =
new Getter<Object>() {
@Override
public Object get() {
throw new DslPropertyException(new HelenusPropertyNode(property, Optional.empty()));
throw new DslPropertyException(
new HelenusPropertyNode(property, Optional.empty()));
}
};
update.set(getter, value);
@ -441,16 +456,21 @@ public final class HelenusSession extends AbstractSessionOperations implements C
});
// Add the partition and clustering keys if they were in the draft (normally the case).
entity.getOrderedProperties().forEach(property -> {
entity
.getOrderedProperties()
.forEach(
property -> {
switch (property.getColumnType()) {
case PARTITION_KEY:
case CLUSTERING_COLUMN:
String propertyName = property.getPropertyName();
Object value = map.get(propertyName);
Getter<Object> getter = new Getter<Object>() {
Getter<Object> getter =
new Getter<Object>() {
@Override
public Object get() {
throw new DslPropertyException(new HelenusPropertyNode(property, Optional.empty()));
throw new DslPropertyException(
new HelenusPropertyNode(property, Optional.empty()));
}
};
update.where(getter, eq(value));
@ -478,9 +498,14 @@ public final class HelenusSession extends AbstractSessionOperations implements C
}
public <T> InsertOperation<T> insert(T pojo) {
Objects.requireNonNull(pojo, "supplied object must be either an instance of the entity class or a dsl for it, but cannot be null");
Objects.requireNonNull(
pojo,
"supplied object must be either an instance of the entity class or a dsl for it, but cannot be null");
HelenusEntity entity = null;
try { entity = Helenus.resolve(pojo); } catch (HelenusMappingException e) {}
try {
entity = Helenus.resolve(pojo);
} catch (HelenusMappingException e) {
}
if (entity != null) {
return new InsertOperation<T>(this, entity.getMappingInterface(), true);
} else {
@ -488,7 +513,9 @@ public final class HelenusSession extends AbstractSessionOperations implements C
}
}
public <T> InsertOperation<T> insert(Drafted draft) { return insert(draft.build(), draft.mutated()); }
public <T> InsertOperation<T> insert(Drafted draft) {
return insert(draft.build(), draft.mutated());
}
private <T> InsertOperation<T> insert(T pojo, Set<String> mutations) {
Objects.requireNonNull(pojo, "pojo is empty");
@ -512,9 +539,14 @@ public final class HelenusSession extends AbstractSessionOperations implements C
}
public <T> InsertOperation<T> upsert(T pojo) {
Objects.requireNonNull(pojo, "supplied object must be either an instance of the entity class or a dsl for it, but cannot be null");
Objects.requireNonNull(
pojo,
"supplied object must be either an instance of the entity class or a dsl for it, but cannot be null");
HelenusEntity entity = null;
try { entity = Helenus.resolve(pojo); } catch (HelenusMappingException e) {}
try {
entity = Helenus.resolve(pojo);
} catch (HelenusMappingException e) {
}
if (entity != null) {
return new InsertOperation<T>(this, entity.getMappingInterface(), false);
} else {
@ -587,5 +619,4 @@ public final class HelenusSession extends AbstractSessionOperations implements C
break;
}
}
}

View file

@ -1,8 +1,7 @@
package net.helenus.core;
import java.util.Objects;
import java.util.*;
import java.util.Objects;
public class PostCommitFunction<T, R> implements java.util.function.Function<T, R> {

View file

@ -25,7 +25,6 @@ import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import net.helenus.core.reflect.DslExportable;
import net.helenus.mapping.HelenusEntity;
import net.helenus.mapping.HelenusEntityType;
@ -185,7 +184,8 @@ public final class SessionInitializer extends AbstractSessionOperations {
PackageUtil.getClasses(packageName)
.stream()
.filter(c -> c.isInterface() && !c.isAnnotation())
.forEach(clazz -> {
.forEach(
clazz -> {
initList.add(Either.right(clazz));
});
} catch (IOException | ClassNotFoundException e) {
@ -267,7 +267,8 @@ public final class SessionInitializer extends AbstractSessionOperations {
Objects.requireNonNull(usingKeyspace, "please define keyspace by 'use' operator");
initList.forEach((either) -> {
initList.forEach(
(either) -> {
Class<?> iface = null;
if (either.isLeft()) {
iface = MappingUtil.getMappingInterface(either.getLeft());

View file

@ -15,7 +15,6 @@
*/
package net.helenus.core;
import net.helenus.support.Either;
import java.util.Map;
import java.util.Set;
@ -41,12 +40,11 @@ public interface UnitOfWork<E extends Exception> extends AutoCloseable {
PostCommitFunction<Void, Void> commit() throws E;
/**
* Explicitly abort the work within this unit of work. Any nested aborted unit of work
* will trigger the entire unit of work to commit.
* Explicitly abort the work within this unit of work. Any nested aborted unit of work will
* trigger the entire unit of work to commit.
*/
void abort();
boolean hasAborted();
boolean hasCommitted();

View file

@ -23,5 +23,4 @@ class UnitOfWorkImpl extends AbstractUnitOfWork<HelenusException> {
public UnitOfWorkImpl(HelenusSession session, UnitOfWork parent) {
super(session, (AbstractUnitOfWork<HelenusException>) parent);
}
}

View file

@ -4,11 +4,7 @@ import net.helenus.mapping.HelenusProperty;
public class EntityIdentifyingFacet extends Facet {
public EntityIdentifyingFacet(HelenusProperty prop) {
public EntityIdentifyingFacet(HelenusProperty prop) {}
}
public EntityIdentifyingFacet(HelenusProperty[]... props) {
}
public EntityIdentifyingFacet(HelenusProperty[]... props) {}
}

View file

@ -1,7 +1,6 @@
package net.helenus.core.cache;
public class Facet {
}
public class Facet {}
/*
An Entity is identifiable via one or more Facets

View file

@ -19,11 +19,11 @@ import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import net.helenus.core.*;
import net.helenus.mapping.HelenusProperty;
public abstract class AbstractFilterOptionalOperation<E, O extends AbstractFilterOptionalOperation<E, O>>
public abstract class AbstractFilterOptionalOperation<
E, O extends AbstractFilterOptionalOperation<E, O>>
extends AbstractOptionalOperation<E, O> {
protected Map<HelenusProperty, Filter<?>> filters = null;

View file

@ -19,11 +19,11 @@ import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import net.helenus.core.*;
import net.helenus.mapping.HelenusProperty;
public abstract class AbstractFilterStreamOperation<E, O extends AbstractFilterStreamOperation<E, O>>
public abstract class AbstractFilterStreamOperation<
E, O extends AbstractFilterStreamOperation<E, O>>
extends AbstractStreamOperation<E, O> {
protected Map<HelenusProperty, Filter<?>> filters = null;

View file

@ -17,8 +17,6 @@ package net.helenus.core.operation;
import com.codahale.metrics.Timer;
import com.datastax.driver.core.ResultSet;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import net.helenus.core.AbstractSessionOperations;
import net.helenus.core.UnitOfWork;
@ -40,7 +38,6 @@ public abstract class AbstractOperation<E, O extends AbstractOperation<E, O>>
return new PreparedOperation<E>(prepareStatement(), this);
}
public E sync() {
final Timer.Context context = requestLatency.time();
try {
@ -72,5 +69,4 @@ public abstract class AbstractOperation<E, O extends AbstractOperation<E, O>>
if (uow == null) return async();
return CompletableFuture.<E>supplyAsync(() -> sync(uow));
}
}

View file

@ -32,7 +32,8 @@ import net.helenus.support.HelenusException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class AbstractStatementOperation<E, O extends AbstractStatementOperation<E, O>> extends Operation<E> {
public abstract class AbstractStatementOperation<E, O extends AbstractStatementOperation<E, O>>
extends Operation<E> {
final Logger logger = LoggerFactory.getLogger(getClass());
@ -55,7 +56,6 @@ public abstract class AbstractStatementOperation<E, O extends AbstractStatementO
this.idempotent = sessionOperations.getDefaultQueryIdempotency();
}
public O ignoreCache(boolean enabled) {
enableCache = enabled;
return (O) this;

View file

@ -21,20 +21,11 @@ import com.datastax.driver.core.ResultSet;
import com.google.common.base.Function;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import net.helenus.core.AbstractSessionOperations;
import net.helenus.core.Helenus;
import net.helenus.core.UnitOfWork;
import net.helenus.mapping.HelenusEntity;
import net.helenus.mapping.value.ColumnValueProvider;
import net.helenus.mapping.value.ValueProviderMap;
public abstract class AbstractStreamOperation<E, O extends AbstractStreamOperation<E, O>>
extends AbstractStatementOperation<E, O> {
@ -113,5 +104,4 @@ public abstract class AbstractStreamOperation<E, O extends AbstractStreamOperati
if (uow == null) return async();
return CompletableFuture.<Stream<E>>supplyAsync(() -> sync(uow));
}
}

View file

@ -34,7 +34,9 @@ public final class BoundOptionalOperation<E>
}
@Override
public Optional<E> transform(ResultSet resultSet) { return delegate.transform(resultSet); }
public Optional<E> transform(ResultSet resultSet) {
return delegate.transform(resultSet);
}
@Override
public Statement buildStatement(boolean cached) {

View file

@ -26,14 +26,17 @@ public final class BoundStreamOperation<E>
private final BoundStatement boundStatement;
private final AbstractStreamOperation<E, ?> delegate;
public BoundStreamOperation(BoundStatement boundStatement, AbstractStreamOperation<E, ?> operation) {
public BoundStreamOperation(
BoundStatement boundStatement, AbstractStreamOperation<E, ?> operation) {
super(operation.sessionOps);
this.boundStatement = boundStatement;
this.delegate = operation;
}
@Override
public String getStatementCacheKey() { return delegate.getStatementCacheKey(); }
public String getStatementCacheKey() {
return delegate.getStatementCacheKey();
}
@Override
public Stream<E> transform(ResultSet resultSet) {
@ -41,5 +44,7 @@ public final class BoundStreamOperation<E>
}
@Override
public Statement buildStatement(boolean cached) { return boundStatement; }
public Statement buildStatement(boolean cached) {
return boundStatement;
}
}

View file

@ -19,10 +19,9 @@ import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.querybuilder.BuiltStatement;
import com.datastax.driver.core.querybuilder.Insert;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.google.common.base.Joiner;
import java.util.*;
import java.util.function.Function;
import com.google.common.base.Joiner;
import net.helenus.core.AbstractSessionOperations;
import net.helenus.core.Getter;
import net.helenus.core.Helenus;
@ -41,7 +40,8 @@ public final class InsertOperation<T> extends AbstractOperation<T, InsertOperati
private HelenusEntity entity;
private final List<Fun.Tuple2<HelenusPropertyNode, Object>> values = new ArrayList<Fun.Tuple2<HelenusPropertyNode, Object>>();
private final List<Fun.Tuple2<HelenusPropertyNode, Object>> values =
new ArrayList<Fun.Tuple2<HelenusPropertyNode, Object>>();
private final T pojo;
private final Class<?> resultType;
private boolean ifNotExists;
@ -57,7 +57,8 @@ public final class InsertOperation<T> extends AbstractOperation<T, InsertOperati
this.resultType = ResultSet.class;
}
public InsertOperation(AbstractSessionOperations sessionOperations, Class<?> resultType, boolean ifNotExists) {
public InsertOperation(
AbstractSessionOperations sessionOperations, Class<?> resultType, boolean ifNotExists) {
super(sessionOperations);
this.ifNotExists = ifNotExists;
@ -248,7 +249,9 @@ public final class InsertOperation<T> extends AbstractOperation<T, InsertOperati
@Override
public T sync(UnitOfWork uow) {
if (uow == null) { return sync(); }
if (uow == null) {
return sync();
}
T result = super.sync(uow);
Class<?> iface = entity.getMappingInterface();
if (resultType == iface) {
@ -261,5 +264,4 @@ public final class InsertOperation<T> extends AbstractOperation<T, InsertOperati
}
return result;
}
}

View file

@ -1,18 +1,16 @@
package net.helenus.core.operation;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import brave.Span;
import brave.Tracer;
import brave.propagation.TraceContext;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;
import com.datastax.driver.core.Statement;
import brave.Span;
import brave.Tracer;
import brave.propagation.TraceContext;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import net.helenus.core.AbstractSessionOperations;
import net.helenus.core.UnitOfWork;
import net.helenus.core.cache.EntityIdentifyingFacet;
@ -32,7 +30,12 @@ public abstract class Operation<E> {
this.requestLatency = metrics.timer("net.helenus.request-latency");
}
public ResultSet execute(AbstractSessionOperations session, UnitOfWork uow, TraceContext traceContext, boolean showValues, boolean cached) {
public ResultSet execute(
AbstractSessionOperations session,
UnitOfWork uow,
TraceContext traceContext,
boolean showValues,
boolean cached) {
// Start recording in a Zipkin sub-span our execution time to perform this operation.
Tracer tracer = session.getZipkinTracer();
@ -61,14 +64,18 @@ public abstract class Operation<E> {
if (span != null) {
span.finish();
}
}
}
public Statement options(Statement statement) { return statement; }
public Statement options(Statement statement) {
return statement;
}
public Statement buildStatement(boolean cached) { return null; }
public Set<EntityIdentifyingFacet> getIdentifyingFacets() { return null; }
public Statement buildStatement(boolean cached) {
return null;
}
public Set<EntityIdentifyingFacet> getIdentifyingFacets() {
return null;
}
}

View file

@ -17,7 +17,6 @@ package net.helenus.core.operation;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.PreparedStatement;
import java.util.regex.Pattern;
public final class PreparedStreamOperation<E> {

View file

@ -38,7 +38,9 @@ public final class SelectFirstOperation<E>
}
@Override
public String getStatementCacheKey() { return delegate.getStatementCacheKey(); }
public String getStatementCacheKey() {
return delegate.getStatementCacheKey();
}
@Override
public BuiltStatement buildStatement(boolean cached) {

View file

@ -36,7 +36,9 @@ public final class SelectFirstTransformingOperation<R, E>
}
@Override
public String getStatementCacheKey() { return delegate.getStatementCacheKey(); }
public String getStatementCacheKey() {
return delegate.getStatementCacheKey();
}
@Override
public BuiltStatement buildStatement(boolean cached) {

View file

@ -22,15 +22,12 @@ import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
import com.datastax.driver.core.querybuilder.Select.Selection;
import com.datastax.driver.core.querybuilder.Select.Where;
import com.google.common.collect.Iterables;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import net.helenus.core.*;
import net.helenus.core.cache.Facet;
import net.helenus.core.cache.EntityIdentifyingFacet;
import net.helenus.core.reflect.HelenusPropertyNode;
import net.helenus.mapping.HelenusEntity;
@ -188,13 +185,18 @@ public final class SelectOperation<E> extends AbstractFilterStreamOperation<E, S
final Set<EntityIdentifyingFacet> facets = new HashSet<>(filters.size());
// Check to see if this select statement has enough information to build one or
// more identifying facets.
entity.getIdentityFacets().forEach((facetName, facet) -> {
entity
.getIdentityFacets()
.forEach(
(facetName, facet) -> {
EntityIdentifyingFacet boundFacet = null;
if (!facet.isFullyBound()) {
boundFacet = new EntityIdentifyingFacet(facet);
for (HelenusProperty prop : facet.getUnboundEntityProperties()) {
Filter filter = filters.get(facet.getProperty());
if (filter == null) { break; }
if (filter == null) {
break;
}
boundFacet.setValueForProperty(prop, filter.toString());
}
}
@ -230,7 +232,7 @@ public final class SelectOperation<E> extends AbstractFilterStreamOperation<E, S
}
if (cached) {
switch(prop.getProperty().getColumnType()) {
switch (prop.getProperty().getColumnType()) {
case PARTITION_KEY:
case CLUSTERING_COLUMN:
break;
@ -288,10 +290,14 @@ public final class SelectOperation<E> extends AbstractFilterStreamOperation<E, S
@Override
public Stream<E> transform(ResultSet resultSet) {
if (rowMapper != null) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(resultSet.iterator(), Spliterator.ORDERED), false).map(rowMapper);
return StreamSupport.stream(
Spliterators.spliteratorUnknownSize(resultSet.iterator(), Spliterator.ORDERED), false)
.map(rowMapper);
} else {
return (Stream<E>)
StreamSupport.stream(Spliterators.spliteratorUnknownSize(resultSet.iterator(), Spliterator.ORDERED),false);
StreamSupport.stream(
Spliterators.spliteratorUnknownSize(resultSet.iterator(), Spliterator.ORDERED),
false);
}
}

View file

@ -36,7 +36,9 @@ public final class SelectTransformingOperation<R, E>
}
@Override
public String getStatementCacheKey() { return delegate.getStatementCacheKey(); }
public String getStatementCacheKey() {
return delegate.getStatementCacheKey();
}
@Override
public BuiltStatement buildStatement(boolean cached) {
@ -47,5 +49,4 @@ public final class SelectTransformingOperation<R, E>
public Stream<R> transform(ResultSet resultSet) {
return delegate.transform(resultSet).map(fn);
}
}

View file

@ -15,15 +15,13 @@
*/
package net.helenus.core.operation;
import java.util.*;
import java.util.function.Function;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.querybuilder.Assignment;
import com.datastax.driver.core.querybuilder.BuiltStatement;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Update;
import java.util.*;
import java.util.function.Function;
import net.helenus.core.*;
import net.helenus.core.reflect.HelenusPropertyNode;
import net.helenus.mapping.HelenusEntity;
@ -43,19 +41,21 @@ public final class UpdateOperation<E> extends AbstractFilterOperation<E, UpdateO
private int[] ttl;
private long[] timestamp;
public UpdateOperation(AbstractSessionOperations sessionOperations){
public UpdateOperation(AbstractSessionOperations sessionOperations) {
super(sessionOperations);
this.draft = null;
this.draftMap = null;
}
public UpdateOperation(AbstractSessionOperations sessionOperations, AbstractEntityDraft<E> draft) {
public UpdateOperation(
AbstractSessionOperations sessionOperations, AbstractEntityDraft<E> draft) {
super(sessionOperations);
this.draft = draft;
this.draftMap = draft.toMap();
}
public UpdateOperation(AbstractSessionOperations sessionOperations, HelenusPropertyNode p, Object v) {
public UpdateOperation(
AbstractSessionOperations sessionOperations, HelenusPropertyNode p, Object v) {
super(sessionOperations);
this.draft = null;
this.draftMap = null;
@ -152,7 +152,7 @@ public final class UpdateOperation<E> extends AbstractFilterOperation<E, UpdateO
if (draft != null) {
String key = p.getProperty().getPropertyName();
List<V> list = (List<V>)draftMap.get(key);
List<V> list = (List<V>) draftMap.get(key);
list.add(0, value);
}
@ -194,7 +194,7 @@ public final class UpdateOperation<E> extends AbstractFilterOperation<E, UpdateO
if (draft != null) {
String key = p.getProperty().getPropertyName();
List<V> list = (List<V>)draftMap.get(key);
List<V> list = (List<V>) draftMap.get(key);
if (idx < 0) {
list.add(0, value);
} else if (idx > list.size()) {
@ -222,7 +222,7 @@ public final class UpdateOperation<E> extends AbstractFilterOperation<E, UpdateO
if (draft != null) {
String key = p.getProperty().getPropertyName();
List<V> list = (List<V>)draftMap.get(key);
List<V> list = (List<V>) draftMap.get(key);
list.add(value);
}
@ -579,7 +579,9 @@ public final class UpdateOperation<E> extends AbstractFilterOperation<E, UpdateO
@Override
public E sync(UnitOfWork uow) {
if (uow == null) { return sync(); }
if (uow == null) {
return sync();
}
E result = super.sync(uow);
if (draft != null) {
String key = getStatementCacheKey();
@ -591,5 +593,4 @@ public final class UpdateOperation<E> extends AbstractFilterOperation<E, UpdateO
}
return result;
}
}

View file

@ -1,6 +1,5 @@
package net.helenus.core.reflect;
import net.helenus.mapping.HelenusEntity;
import java.util.Set;

View file

@ -23,7 +23,6 @@ import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collections;
import java.util.Map;
import net.helenus.core.Helenus;
import net.helenus.mapping.annotation.Transient;
import net.helenus.support.HelenusException;

View file

@ -15,10 +15,9 @@
*/
package net.helenus.mapping;
import net.helenus.core.cache.EntityIdentifyingFacet;
import java.util.Collection;
import java.util.Map;
import net.helenus.core.cache.EntityIdentifyingFacet;
public interface HelenusEntity {
@ -35,5 +34,4 @@ public interface HelenusEntity {
HelenusProperty getProperty(String name);
Map<String, EntityIdentifyingFacet> getIdentityFacets();
}

View file

@ -20,7 +20,6 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.lang.reflect.Method;
import java.util.*;
import net.helenus.config.HelenusSettings;
import net.helenus.core.Helenus;
import net.helenus.core.annotation.Cacheable;
@ -64,7 +63,8 @@ public final class HelenusMappingEntity implements HelenusEntity {
}
for (Class<?> c : ClassUtils.getAllInterfaces(iface)) {
if (c.getDeclaredAnnotation(Table.class) != null || c.getDeclaredAnnotation(InheritedTable.class) != null) {
if (c.getDeclaredAnnotation(Table.class) != null
|| c.getDeclaredAnnotation(InheritedTable.class) != null) {
for (Method m : c.getDeclaredMethods()) {
Method o = methods.get(m.getName());
if (o != null) {
@ -110,24 +110,31 @@ public final class HelenusMappingEntity implements HelenusEntity {
cacheable = (null != iface.getDeclaredAnnotation(Cacheable.class));
ImmutableMap.Builder<String, EntityIdentifyingFacet> allFacetsBuilder = ImmutableMap.builder();
ImmutableMap.Builder<String, EntityIdentifyingFacet> ancillaryFacetsBuilder = ImmutableMap.builder();
ImmutableMap.Builder<String, EntityIdentifyingFacet> ancillaryFacetsBuilder =
ImmutableMap.builder();
EntityIdentifyingFacet primaryFacet = null;
List<HelenusProperty> primaryProperties = new ArrayList<HelenusProperty>(4);
for (HelenusProperty prop : propsLocal) {
switch(prop.getColumnType()) {
switch (prop.getColumnType()) {
case PARTITION_KEY:
case CLUSTERING_COLUMN:
primaryProperties.add(prop);
break;
default:
if (primaryProperties != null) {
primaryFacet = new EntityIdentifyingFacet(keyspace, table, schemaVersion, primaryProperties.toArray(new HelenusProperty[props.size()]));
primaryFacet =
new EntityIdentifyingFacet(
keyspace,
table,
schemaVersion,
primaryProperties.toArray(new HelenusProperty[props.size()]));
allFacetsBuilder.put("*", primaryFacet);
primaryProperties = null;
}
Optional<IdentityName> optionalIndexName = prop.getIndexName();
if (optionalIndexName.isPresent()) {
EntityIdentifyingFacet facet = new EntityIdentifyingFacet(keyspace, table, schemaVersion, prop);
EntityIdentifyingFacet facet =
new EntityIdentifyingFacet(keyspace, table, schemaVersion, prop);
ancillaryFacetsBuilder.put(prop.getPropertyName(), facet);
}
}

View file

@ -2,6 +2,7 @@ package net.helenus.mapping.javatype;
public abstract class AbstractCollectionJavaType extends AbstractJavaType {
public static boolean isCollectionType() { return true; }
public static boolean isCollectionType() {
return true;
}
}

View file

@ -33,7 +33,9 @@ import net.helenus.support.HelenusMappingException;
public abstract class AbstractJavaType {
public static boolean isCollectionType() { return false; }
public static boolean isCollectionType() {
return false;
}
public abstract Class<?> getJavaClass();

View file

@ -18,7 +18,6 @@ package net.helenus.mapping.javatype;
import com.datastax.driver.core.*;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.AbstractCollection;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

View file

@ -8,6 +8,7 @@ public abstract class AbstractCollectionDataType extends AbstractDataType {
super(columnType);
}
public boolean isCollectionType() { return true; }
public boolean isCollectionType() {
return true;
}
}

View file

@ -55,6 +55,7 @@ public abstract class AbstractDataType {
"wrong column type " + columnType + " for UserDefinedType in columnName " + columnName);
}
public boolean isCollectionType() { return false; }
public boolean isCollectionType() {
return false;
}
}

View file

@ -18,8 +18,6 @@ package net.helenus.mapping.type;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.UserType;
import com.datastax.driver.core.schemabuilder.*;
import java.util.AbstractCollection;
import java.util.List;
import net.helenus.mapping.ColumnType;
import net.helenus.mapping.IdentityName;

View file

@ -33,7 +33,7 @@ public enum BeanColumnValueProvider implements ColumnValueProvider {
try {
value = getter.invoke(bean, new Object[] {});
} catch (InvocationTargetException e) {
if (e.getCause() != null ) {
if (e.getCause() != null) {
throw new HelenusException("getter threw an exception", e.getCause());
}
} catch (ReflectiveOperationException e) {

View file

@ -67,11 +67,8 @@ public class CollectionTest extends AbstractEmbeddedCassandraTest {
// read full object
Customer actual = session.<Customer>select(customer)
.where(customer::id, eq(id))
.single()
.sync()
.orElse(null);
Customer actual =
session.<Customer>select(customer).where(customer::id, eq(id)).single().sync().orElse(null);
Assert.assertEquals(id, actual.id());
Assert.assertEquals(aliases, actual.aliases());
Assert.assertNull(actual.names());
@ -90,11 +87,8 @@ public class CollectionTest extends AbstractEmbeddedCassandraTest {
session.update().set(customer::aliases, expected).where(customer::id, eq(id)).sync();
actual = session.<Customer>select(customer)
.where(customer::id, eq(id))
.single()
.sync()
.orElse(null);
actual =
session.<Customer>select(customer).where(customer::id, eq(id)).single().sync().orElse(null);
Assert.assertEquals(id, actual.id());
Assert.assertEquals(expected, actual.aliases());
@ -170,11 +164,8 @@ public class CollectionTest extends AbstractEmbeddedCassandraTest {
// read full object
Customer actual = session.<Customer>select(customer)
.where(customer::id, eq(id))
.single()
.sync()
.orElse(null);
Customer actual =
session.<Customer>select(customer).where(customer::id, eq(id)).single().sync().orElse(null);
Assert.assertEquals(id, actual.id());
Assert.assertEquals(names, actual.names());
@ -200,11 +191,8 @@ public class CollectionTest extends AbstractEmbeddedCassandraTest {
session.update().set(customer::names, expected).where(customer::id, eq(id)).sync();
actual = session.<Customer>select(customer)
.where(customer::id, eq(id))
.single()
.sync()
.orElse(null);
actual =
session.<Customer>select(customer).where(customer::id, eq(id)).single().sync().orElse(null);
Assert.assertEquals(id, actual.id());
Assert.assertEquals(expected, actual.names());
@ -306,10 +294,8 @@ public class CollectionTest extends AbstractEmbeddedCassandraTest {
// read full object
Customer actual = session.<Customer>select(customer)
.where(customer::id, eq(id)).single()
.sync()
.orElse(null);
Customer actual =
session.<Customer>select(customer).where(customer::id, eq(id)).single().sync().orElse(null);
Assert.assertEquals(id, actual.id());
Assert.assertEquals(props, actual.properties());
@ -343,11 +329,8 @@ public class CollectionTest extends AbstractEmbeddedCassandraTest {
session.update().set(customer::properties, expected).where(customer::id, eq(id)).sync();
actual = session.<Customer>select(customer)
.where(customer::id, eq(id))
.single()
.sync()
.orElse(null);
actual =
session.<Customer>select(customer).where(customer::id, eq(id)).single().sync().orElse(null);
Assert.assertEquals(id, actual.id());
Assert.assertEquals(expected, actual.properties());

View file

@ -18,31 +18,22 @@ package net.helenus.test.integration.core.draft;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import net.helenus.core.Helenus;
import net.helenus.core.HelenusSession;
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import net.helenus.core.Helenus;
import net.helenus.core.HelenusSession;
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
import static net.helenus.core.Query.eq;
public class EntityDraftBuilderTest extends AbstractEmbeddedCassandraTest {
static Supply supply;
static HelenusSession session;
@BeforeClass
public static void beforeTest() {
session = Helenus.init(getSession())
.showCql()
.add(Supply.class)
.autoCreateDrop()
.get();
session = Helenus.init(getSession()).showCql().add(Supply.class).autoCreateDrop().get();
supply = session.dsl(Supply.class);
}
@ -50,44 +41,50 @@ public class EntityDraftBuilderTest extends AbstractEmbeddedCassandraTest {
public void testFoo() throws Exception {
Supply.Draft draft = null;
draft = Supply.draft("APAC")
draft =
Supply.draft("APAC")
.code("WIDGET-002")
.description("Our second Widget!")
.demand(new HashMap<String, Long>() {{
.demand(
new HashMap<String, Long>() {
{
put("APAC", 100L);
put("EMEA", 10000L);
put("NORAM", 2000000L);
}})
.shipments(new HashSet<String>() {{
}
})
.shipments(
new HashSet<String>() {
{
add("HMS Puddle in transit to APAC, 100 units.");
add("Frigate Jimmy in transit to EMEA, 10000 units.");
}})
.suppliers(new ArrayList<String>() {{
}
})
.suppliers(
new ArrayList<String>() {
{
add("Puddle, Inc.");
add("Jimmy Town, LTD.");
}});
}
});
Supply s1 = session.<Supply>insert(draft)
.sync();
Supply s1 = session.<Supply>insert(draft).sync();
// List
Supply s2 = session.<Supply>update(s1.update())
Supply s2 =
session
.<Supply>update(s1.update())
.prepend(supply::suppliers, "Pignose Supply, LLC.")
.sync();
Assert.assertEquals(s2.suppliers().get(0), "Pignose Supply, LLC.");
// Set
String shipment = "Pignose, on the way! (1M units)";
Supply s3 = session.<Supply>update(s2.update())
.add(supply::shipments, shipment)
.sync();
Supply s3 = session.<Supply>update(s2.update()).add(supply::shipments, shipment).sync();
Assert.assertTrue(s3.shipments().contains(shipment));
// Map
Supply s4 = session.<Supply>update(s3.update())
.put(supply::demand, "NORAM", 10L)
.sync();
Assert.assertEquals((long)s4.demand().get("NORAM"), 10L);
Supply s4 = session.<Supply>update(s3.update()).put(supply::demand, "NORAM", 10L).sync();
Assert.assertEquals((long) s4.demand().get("NORAM"), 10L);
}
}

View file

@ -1,27 +1,40 @@
package net.helenus.test.integration.core.draft;
import java.util.UUID;
import net.helenus.core.AbstractAuditedEntityDraft;
import net.helenus.core.Helenus;
import net.helenus.core.reflect.MapExportable;
import net.helenus.mapping.annotation.*;
@Table
public interface Inventory {
static Inventory inventory = Helenus.dsl(Inventory.class);
@PartitionKey UUID id();
@Column("emea") @Types.Counter long EMEA();
@Column("noram") @Types.Counter long NORAM();
@Column("apac") @Types.Counter long APAC();
@PartitionKey
UUID id();
@Transient static Draft draft(UUID id) { return new Draft(id); }
@Column("emea")
@Types.Counter
long EMEA();
@Transient default Draft update() { return new Draft(this); }
@Column("noram")
@Types.Counter
long NORAM();
@Column("apac")
@Types.Counter
long APAC();
@Transient
static Draft draft(UUID id) {
return new Draft(id);
}
@Transient
default Draft update() {
return new Draft(this);
}
class Draft extends AbstractAuditedEntityDraft<Inventory> {
@ -37,9 +50,13 @@ public interface Inventory {
super((MapExportable) inventory);
}
public Class<Inventory> getEntityClass() { return Inventory.class; }
public Class<Inventory> getEntityClass() {
return Inventory.class;
}
protected String getCurrentAuditor() { return "unknown"; }
protected String getCurrentAuditor() {
return "unknown";
}
// Immutable properties:
public UUID id() {
@ -72,7 +89,5 @@ public interface Inventory {
mutate(inventory::NORAM, count);
return this;
}
}
}

View file

@ -1,37 +1,52 @@
package net.helenus.test.integration.core.draft;
import java.lang.reflect.Proxy;
import com.datastax.driver.core.utils.UUIDs;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import com.datastax.driver.core.utils.UUIDs;
import net.helenus.core.AbstractEntityDraft;
import net.helenus.core.Helenus;
import net.helenus.core.reflect.MapExportable;
import net.helenus.mapping.annotation.*;
@Table
public interface Supply {
static Supply supply = Helenus.dsl(Supply.class);
@PartitionKey UUID id();
@ClusteringColumn(ordinal=0) default String region() { return "NORAM"; }
@PartitionKey
UUID id();
@Index(caseSensitive = false) String code();
@Index String description(); // @IndexText == lucene index
@Index Map<String, Long> demand();
@Index List<String> suppliers();
@Index Set<String> shipments();
@ClusteringColumn(ordinal = 0)
default String region() {
return "NORAM";
}
@Transient static Draft draft(String region) { return new Draft(region); }
@Index(caseSensitive = false)
String code();
@Transient default Draft update() { return new Draft(this); }
@Index
String description(); // @IndexText == lucene index
@Index
Map<String, Long> demand();
@Index
List<String> suppliers();
@Index
Set<String> shipments();
@Transient
static Draft draft(String region) {
return new Draft(region);
}
@Transient
default Draft update() {
return new Draft(this);
}
class Draft extends AbstractEntityDraft<Supply> {
@ -48,7 +63,9 @@ public interface Supply {
super((MapExportable) supply);
}
public Class<Supply> getEntityClass() { return Supply.class; }
public Class<Supply> getEntityClass() {
return Supply.class;
}
// Immutable properties:
public UUID id() {
@ -124,6 +141,5 @@ public interface Supply {
public Draft setshipments(Set<String> shipments) {
return shipments(shipments);
}
}
}

View file

@ -65,7 +65,8 @@ public class HierarchyTest extends AbstractEmbeddedCassandraTest {
.value(cat::nickname, "garfield")
.value(cat::eatable, false)
.sync();
Optional<Cat> animal = session.select(Cat.class).where(cat::nickname, eq("garfield")).single().sync();
Optional<Cat> animal =
session.select(Cat.class).where(cat::nickname, eq("garfield")).single().sync();
Assert.assertTrue(animal.isPresent());
Cat cat = animal.get();

View file

@ -20,6 +20,7 @@ import net.helenus.mapping.annotation.InheritedTable;
@InheritedTable
public interface Mammal extends Animal {
default boolean warmBlodded() { return true; }
default boolean warmBlodded() {
return true;
}
}

View file

@ -18,21 +18,17 @@ package net.helenus.test.integration.core.simple;
import static net.helenus.core.Query.eq;
import com.datastax.driver.core.ResultSet;
import java.util.*;
import net.helenus.core.Helenus;
import net.helenus.core.HelenusSession;
import net.helenus.core.Operator;
import net.helenus.core.operation.UpdateOperation;
import net.helenus.core.reflect.Drafted;
import net.helenus.mapping.HelenusEntity;
import net.helenus.support.Fun;
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.*;
public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
static User user;
@ -102,11 +98,7 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
// select as object
actual = session.<User>select(user)
.where(user::id, eq(100L))
.single()
.sync()
.orElse(null);
actual = session.<User>select(user).where(user::id, eq(100L)).single().sync().orElse(null);
assertUsers(newUser, actual);
// select by columns
@ -192,11 +184,7 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
Assert.assertEquals("_albert", name);
User u2 = session.<User>select(user)
.where(user::id, eq(100L))
.single()
.sync()
.orElse(null);
User u2 = session.<User>select(user).where(user::id, eq(100L)).single().sync().orElse(null);
Assert.assertEquals(Long.valueOf(100L), u2.id());
Assert.assertEquals("albert", u2.name());
@ -213,11 +201,7 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
.sync();
Optional<User> maybeGreg =
session
.<User>select(user)
.where(user::id, eq(1234L))
.single()
.sync();
session.<User>select(user).where(user::id, eq(1234L)).single().sync();
// INSERT
@ -260,13 +244,8 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
.zipkinContext(null)
.sync();
UpdateOperation<ResultSet> update = session.update();
update
.set(user::name, null)
.zipkinContext(null)
.sync();
update.set(user::name, null).zipkinContext(null).sync();
}
private void assertUsers(User expected, User actual) {

View file

@ -42,11 +42,8 @@ public class TupleMapTest extends TupleCollectionTest {
// read full object
Book actual = session.<Book>select(book)
.where(book::id, Query.eq(id))
.single()
.sync()
.orElse(null);
Book actual =
session.<Book>select(book).where(book::id, Query.eq(id)).single().sync().orElse(null);
Assert.assertEquals(id, actual.id());
assertEqualMaps(writers, actual.writers());
Assert.assertNull(actual.reviewers());
@ -77,11 +74,7 @@ public class TupleMapTest extends TupleCollectionTest {
session.update().set(book::writers, expected).where(book::id, Query.eq(id)).sync();
actual = session.<Book>select(book)
.where(book::id, Query.eq(id))
.single()
.sync()
.orElse(null);
actual = session.<Book>select(book).where(book::id, Query.eq(id)).single().sync().orElse(null);
Assert.assertEquals(id, actual.id());
assertEqualMaps(expected, actual.writers());
@ -96,7 +89,8 @@ public class TupleMapTest extends TupleCollectionTest {
expected.put(third, unk);
session.update().put(book::writers, third, unk).where(book::id, Query.eq(id)).sync();
actualMap = session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
actualMap =
session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
assertEqualMaps(expected, actualMap);
// putAll operation

View file

@ -15,6 +15,9 @@
*/
package net.helenus.test.integration.core.unitofwork;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.helenus.core.Helenus;
import net.helenus.core.HelenusSession;
import net.helenus.core.UnitOfWork;
@ -23,21 +26,13 @@ import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class AndThenOrderTest extends AbstractEmbeddedCassandraTest {
static HelenusSession session;
@BeforeClass
public static void beforeTest() {
session = Helenus.init(getSession())
.showCql()
.autoCreateDrop()
.get();
session = Helenus.init(getSession()).showCql().autoCreateDrop().get();
}
@Test
@ -48,17 +43,37 @@ public class AndThenOrderTest extends AbstractEmbeddedCassandraTest {
uow5 = session.begin();
uow3 = session.begin(uow5);
uow1 = session.begin(uow3);
uow1.commit().andThen(() -> { q.add("1"); });
uow1.commit()
.andThen(
() -> {
q.add("1");
});
uow2 = session.begin(uow3);
uow2.commit().andThen(() -> { q.add("2"); });
uow3.commit().andThen(() -> { q.add("3"); });
uow2.commit()
.andThen(
() -> {
q.add("2");
});
uow3.commit()
.andThen(
() -> {
q.add("3");
});
uow4 = session.begin(uow5);
uow4.commit().andThen(() -> { q.add("4"); });
uow5.commit().andThen(() -> { q.add("5"); });
uow4.commit()
.andThen(
() -> {
q.add("4");
});
uow5.commit()
.andThen(
() -> {
q.add("5");
});
System.out.println(q);
Assert.assertTrue(Arrays.equals(q.toArray(new String[5]), new String[] {"1", "2", "3", "4", "5"}));
Assert.assertTrue(
Arrays.equals(q.toArray(new String[5]), new String[] {"1", "2", "3", "4", "5"}));
}
@Test
@ -71,48 +86,60 @@ public class AndThenOrderTest extends AbstractEmbeddedCassandraTest {
try {
uow3 = session.begin(uow4);
uow1 = session.begin(uow3);
uow1.commit().andThen(() -> {
uow1.commit()
.andThen(
() -> {
q.add("1");
});
uow2 = session.begin(uow3);
uow2.commit().andThen(() -> {
uow2.commit()
.andThen(
() -> {
q.add("2");
});
uow3.commit().andThen(() -> {
uow3.commit()
.andThen(
() -> {
q.add("3");
});
uow4.commit().andThen(() -> {
uow4.commit()
.andThen(
() -> {
q.add("4");
});
throw new Exception();
} catch(Exception e) {
} catch (Exception e) {
uow4.abort();
}
uow5.commit().andThen(() -> { q.add("5"); });
uow5.commit()
.andThen(
() -> {
q.add("5");
});
System.out.println(q);
Assert.assertTrue(q.isEmpty() == true);
}
@Test
public void testClosableWillAbortWhenNotCommitted() throws Exception {
UnitOfWork unitOfWork;
try(UnitOfWork uow = session.begin()) {
try (UnitOfWork uow = session.begin()) {
unitOfWork = uow;
Assert.assertFalse(uow.hasAborted());
}
Assert.assertTrue(unitOfWork.hasAborted());
}
@Test
public void testClosable() throws Exception {
UnitOfWork unitOfWork;
try(UnitOfWork uow = session.begin()) {
try (UnitOfWork uow = session.begin()) {
unitOfWork = uow;
Assert.assertFalse(uow.hasAborted());
uow.commit().andThen(() -> {
uow.commit()
.andThen(
() -> {
Assert.assertFalse(uow.hasAborted());
Assert.assertTrue(uow.hasCommitted());
});
@ -120,5 +147,4 @@ public class AndThenOrderTest extends AbstractEmbeddedCassandraTest {
Assert.assertFalse(unitOfWork.hasAborted());
Assert.assertTrue(unitOfWork.hasCommitted());
}
}

View file

@ -15,16 +15,13 @@
*/
package net.helenus.test.integration.core.unitofwork;
import net.helenus.mapping.annotation.*;
import com.datastax.driver.core.DataType.Name;
import java.util.Set;
import net.helenus.mapping.annotation.*;
@UDT
public interface Directory extends FilesystemNode {
@Types.Set(Name.TIMEUUID)
Set<FilesystemNode> inodes();
}

View file

@ -17,11 +17,9 @@ package net.helenus.test.integration.core.unitofwork;
import net.helenus.mapping.annotation.*;
@UDT
public interface File extends FilesystemNode {
@Column
byte[] data();
}

View file

@ -21,5 +21,4 @@ import net.helenus.mapping.annotation.UDT;
public interface FileAttributes {
String owner();
}

View file

@ -15,9 +15,8 @@
*/
package net.helenus.test.integration.core.unitofwork;
import net.helenus.mapping.annotation.*;
import java.util.UUID;
import net.helenus.mapping.annotation.*;
@Table("fs")
public interface FilesystemNode {
@ -30,5 +29,4 @@ public interface FilesystemNode {
@Column
FileAttributes attr();
}

View file

@ -15,7 +15,10 @@
*/
package net.helenus.test.integration.core.unitofwork;
import static net.helenus.core.Query.eq;
import com.datastax.driver.core.utils.UUIDs;
import java.util.UUID;
import net.bytebuddy.utility.RandomString;
import net.helenus.core.Helenus;
import net.helenus.core.HelenusSession;
@ -29,34 +32,24 @@ import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.UUID;
import static net.helenus.core.Query.eq;
@Table
@Cacheable
interface Widget {
@PartitionKey
UUID id();
@Column
String name();
}
public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
static Widget widget;
static HelenusSession session;
@BeforeClass
public static void beforeTest() {
session = Helenus.init(getSession())
.showCql()
.add(Widget.class)
.autoCreateDrop()
.get();
session = Helenus.init(getSession()).showCql().add(Widget.class).autoCreateDrop().get();
widget = session.dsl(Widget.class);
}
@ -66,7 +59,8 @@ public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
UUID key = UUIDs.timeBased();
// This should inserted Widget, but not cache it.
session.<Widget>insert(widget)
session
.<Widget>insert(widget)
.value(widget::id, key)
.value(widget::name, RandomString.make(20))
.sync();
@ -74,25 +68,19 @@ public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
try (UnitOfWork uow = session.begin()) {
// This should read from the database and return a Widget.
w1 = session.<Widget>select(widget)
.where(widget::id, eq(key))
.single()
.sync(uow)
.orElse(null);
w1 =
session.<Widget>select(widget).where(widget::id, eq(key)).single().sync(uow).orElse(null);
// This should read from the cache and get the same instance of a Widget.
w2 = session.<Widget>select(widget)
.where(widget::id, eq(key))
.single()
.sync(uow)
.orElse(null);
w2 =
session.<Widget>select(widget).where(widget::id, eq(key)).single().sync(uow).orElse(null);
uow.commit()
.andThen(() -> {
.andThen(
() -> {
Assert.assertEquals(w1, w2);
});
}
}
@Test
@ -103,7 +91,9 @@ public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
// This should inserted Widget, and not cache it in uow1.
try (UnitOfWork uow1 = session.begin()) {
w1 = session.<Widget>insert(widget)
w1 =
session
.<Widget>insert(widget)
.value(widget::id, key1)
.value(widget::name, RandomString.make(20))
.sync(uow1);
@ -111,7 +101,9 @@ public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
try (UnitOfWork uow2 = session.begin(uow1)) {
// This should read from uow1's cache and return the same Widget.
w2 = session.<Widget>select(widget)
w2 =
session
.<Widget>select(widget)
.where(widget::id, eq(key1))
.single()
.sync(uow2)
@ -119,32 +111,37 @@ public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
Assert.assertEquals(w1, w2);
w3 = session.<Widget>insert(widget)
w3 =
session
.<Widget>insert(widget)
.value(widget::id, key2)
.value(widget::name, RandomString.make(20))
.sync(uow2);
uow2.commit()
.andThen(() -> {
.andThen(
() -> {
Assert.assertEquals(w1, w2);
});
}
// This should read from the cache and get the same instance of a Widget.
w4 = session.<Widget>select(widget)
w4 =
session
.<Widget>select(widget)
.where(widget::id, eq(key2))
.single()
.sync(uow1)
.orElse(null);
uow1.commit()
.andThen(() -> {
.andThen(
() -> {
Assert.assertEquals(w3, w4);
});
}
}
/*
/*
@Test
public void testSelectAfterInsertProperlyCachesEntity() throws Exception {
Widget w1, w2, w3, w4;
@ -191,5 +188,5 @@ public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
Assert.assertNotEquals(w1, w4);
Assert.assertTrue(w1.equals(w4));
}
*/
*/
}

View file

@ -19,7 +19,6 @@ import java.util.Date;
import java.util.Map;
import java.util.Set;
import net.helenus.core.reflect.Drafted;
import net.helenus.mapping.HelenusEntity;
import net.helenus.mapping.annotation.*;
@Table