Improved logging output. Fixed caching for insert, update, delete cases.
This commit is contained in:
parent
8a7dbfdec1
commit
7535e9ade7
99 changed files with 3696 additions and 3815 deletions
|
@ -17,20 +17,22 @@ package net.helenus.core;
|
|||
|
||||
import java.io.PrintStream;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import net.helenus.support.Either;
|
||||
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;
|
||||
|
||||
import brave.Tracer;
|
||||
import net.helenus.core.cache.Facet;
|
||||
import net.helenus.core.operation.Operation;
|
||||
import net.helenus.mapping.value.ColumnValuePreparer;
|
||||
import net.helenus.mapping.value.ColumnValueProvider;
|
||||
import net.helenus.support.HelenusException;
|
||||
|
@ -61,7 +63,7 @@ public abstract class AbstractSessionOperations {
|
|||
|
||||
public PreparedStatement prepare(RegularStatement statement) {
|
||||
try {
|
||||
log(statement, null, null, false);
|
||||
logStatement(statement, false);
|
||||
return currentSession().prepare(statement);
|
||||
} catch (RuntimeException e) {
|
||||
throw translateException(e);
|
||||
|
@ -70,7 +72,7 @@ public abstract class AbstractSessionOperations {
|
|||
|
||||
public ListenableFuture<PreparedStatement> prepareAsync(RegularStatement statement) {
|
||||
try {
|
||||
log(statement, null, null, false);
|
||||
logStatement(statement, false);
|
||||
return currentSession().prepareAsync(statement);
|
||||
} catch (RuntimeException e) {
|
||||
throw translateException(e);
|
||||
|
@ -107,42 +109,20 @@ public abstract class AbstractSessionOperations {
|
|||
|
||||
public ResultSetFuture executeAsync(Statement statement, UnitOfWork uow, Stopwatch timer, boolean showValues) {
|
||||
try {
|
||||
log(statement, uow, timer, showValues);
|
||||
logStatement(statement, showValues);
|
||||
return currentSession().executeAsync(statement);
|
||||
} catch (RuntimeException e) {
|
||||
throw translateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
void log(Statement statement, UnitOfWork uow, Stopwatch timer, boolean showValues) {
|
||||
if (LOG.isInfoEnabled()) {
|
||||
String timerString = "";
|
||||
String uowString = "";
|
||||
if (uow != null) {
|
||||
uowString = (timer != null) ? " " : "" + "UOW(" + uow.hashCode() + ")";
|
||||
}
|
||||
if (timer != null) {
|
||||
timerString = String.format(" %s", timer.toString());
|
||||
}
|
||||
LOG.info(String.format("CQL%s%s - %s", uowString, timerString, statement));
|
||||
}
|
||||
if (isShowCql()) {
|
||||
if (statement instanceof BuiltStatement) {
|
||||
BuiltStatement builtStatement = (BuiltStatement) statement;
|
||||
if (showValues) {
|
||||
RegularStatement regularStatement = builtStatement.setForceNoValues(true);
|
||||
printCql(regularStatement.getQueryString());
|
||||
} else {
|
||||
printCql(builtStatement.getQueryString());
|
||||
}
|
||||
} else if (statement instanceof RegularStatement) {
|
||||
RegularStatement regularStatement = (RegularStatement) statement;
|
||||
printCql(regularStatement.getQueryString());
|
||||
} else {
|
||||
printCql(statement.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
private void logStatement(Statement statement, boolean showValues) {
|
||||
if (isShowCql()) {
|
||||
printCql(Operation.queryString(statement, showValues));
|
||||
} else if (LOG.isInfoEnabled()) {
|
||||
LOG.info("CQL> " + Operation.queryString(statement, showValues));
|
||||
}
|
||||
}
|
||||
|
||||
public Tracer getZipkinTracer() {
|
||||
return null;
|
||||
|
@ -152,8 +132,7 @@ public abstract class AbstractSessionOperations {
|
|||
return null;
|
||||
}
|
||||
|
||||
public void mergeCache(Table<String, String, Object> cache) {
|
||||
}
|
||||
public void mergeCache(Table<String, String, Either<Object, List<Facet>>> uowCache) { }
|
||||
|
||||
RuntimeException translateException(RuntimeException e) {
|
||||
if (e instanceof HelenusException) {
|
||||
|
@ -172,4 +151,7 @@ public abstract class AbstractSessionOperations {
|
|||
void printCql(String cql) {
|
||||
getPrintStream().println(cql);
|
||||
}
|
||||
|
||||
public void cacheEvict(List<Facet> facets) {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,10 @@ import java.util.*;
|
|||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import net.helenus.core.cache.UnboundFacet;
|
||||
import net.helenus.core.reflect.HelenusNamedProperty;
|
||||
import net.helenus.mapping.HelenusProperty;
|
||||
import net.helenus.support.Either;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -31,6 +35,8 @@ import com.google.common.collect.TreeTraverser;
|
|||
import net.helenus.core.cache.CacheUtil;
|
||||
import net.helenus.core.cache.Facet;
|
||||
|
||||
import static net.helenus.core.HelenusSession.deleted;
|
||||
|
||||
/** Encapsulates the concept of a "transaction" as a unit-of-work. */
|
||||
public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfWork<E>, AutoCloseable {
|
||||
|
||||
|
@ -39,8 +45,11 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
|
|||
private final List<AbstractUnitOfWork<E>> nested = new ArrayList<>();
|
||||
private final HelenusSession session;
|
||||
private final AbstractUnitOfWork<E> parent;
|
||||
// Cache:
|
||||
private final Table<String, String, Object> cache = HashBasedTable.create();
|
||||
private List<CommitThunk> postCommit = new ArrayList<CommitThunk>();
|
||||
private boolean aborted = false;
|
||||
private boolean committed = false;
|
||||
|
||||
private final Table<String, String, Either<Object, List<Facet>>> cache = HashBasedTable.create();
|
||||
protected String purpose;
|
||||
protected int cacheHits = 0;
|
||||
protected int cacheMisses = 0;
|
||||
|
@ -48,9 +57,6 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
|
|||
protected Stopwatch elapsedTime;
|
||||
protected Map<String, Double> databaseTime = new HashMap<>();
|
||||
protected double cacheLookupTime = 0.0;
|
||||
private List<CommitThunk> postCommit = new ArrayList<CommitThunk>();
|
||||
private boolean aborted = false;
|
||||
private boolean committed = false;
|
||||
|
||||
protected AbstractUnitOfWork(HelenusSession session, AbstractUnitOfWork<E> parent) {
|
||||
Objects.requireNonNull(session, "containing session cannot be null");
|
||||
|
@ -82,7 +88,7 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
|
|||
}
|
||||
|
||||
@Override
|
||||
public UnitOfWork<E> begin() {
|
||||
public synchronized UnitOfWork<E> begin() {
|
||||
elapsedTime = Stopwatch.createStarted();
|
||||
// log.recordCacheAndDatabaseOperationCount(txn::start)
|
||||
return this;
|
||||
|
@ -108,19 +114,38 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
|
|||
|
||||
public String logTimers(String what) {
|
||||
double e = (double) elapsedTime.elapsed(TimeUnit.MICROSECONDS) / 1000.0;
|
||||
double d = databaseTime.containsKey("Cassandra") ? databaseTime.get("Cassandra") / 1000.0 : 0.0;
|
||||
double d = 0.0;
|
||||
double c = cacheLookupTime / 1000.0;
|
||||
double fd = (d / e) * 100.0;
|
||||
double fc = (c / e) * 100.0;
|
||||
double dat = d + c;
|
||||
double daf = (dat / e) * 100;
|
||||
String nested = this.nested.stream().map(uow -> String.valueOf(uow.hashCode()))
|
||||
.collect(Collectors.joining(", "));
|
||||
return String.format(Locale.US,
|
||||
"UOW(%s%s) %s (total: %,.3fms cache: %,.3fms %,2.2f%% (%,d hit, %,d miss) %,d Cassandra operation%s took %,.3fms %,2.2f%% [%,.3fms %,2.2f%%])%s",
|
||||
hashCode(), (this.nested.size() > 0 ? ", [" + nested + "]" : ""), what, e, c, fc, cacheHits,
|
||||
cacheMisses, databaseLookups, (databaseLookups > 1) ? "s" : "", d, fd, dat, daf,
|
||||
(purpose == null ? "" : " in " + purpose));
|
||||
String database = "";
|
||||
if (databaseTime.size() > 0) {
|
||||
List<String> dbt = new ArrayList<>(databaseTime.size());
|
||||
for (String name : databaseTime.keySet()) {
|
||||
double t = databaseTime.get(name) / 1000.0;
|
||||
d += t;
|
||||
dbt.add(String.format("%s took %,.3fms %,2.2f%%", name, t, (t / e) * 100.0));
|
||||
}
|
||||
double fd = (d / e) * 100.0;
|
||||
database = String.format(", %d quer%s (%,.3fms %,2.2f%% - %s)", databaseLookups,
|
||||
(databaseLookups > 1) ? "ies" : "y", d, fd, String.join(", ", dbt));
|
||||
}
|
||||
String cache = "";
|
||||
if (cacheLookupTime > 0) {
|
||||
int cacheLookups = cacheHits + cacheMisses;
|
||||
cache = String.format(" with %d cache lookup%s (%,.3fms %,2.2f%% - %,d hit, %,d miss)", cacheLookups,
|
||||
cacheLookups > 1 ? "s" : "", c, fc, cacheHits, cacheMisses);
|
||||
}
|
||||
String da = "";
|
||||
if (databaseTime.size() > 0 || cacheLookupTime > 0) {
|
||||
double dat = d + c;
|
||||
double daf = (dat / e) * 100;
|
||||
da = String.format(" consuming %,.3fms for data access, or %,2.2f%% of total UOW time.", dat, daf);
|
||||
}
|
||||
String n = nested.stream().map(uow -> String.valueOf(uow.hashCode())).collect(Collectors.joining(", "));
|
||||
String s = String.format(Locale.US, "UOW(%s%s) %s in %,.3fms%s%s%s%s", hashCode(),
|
||||
(nested.size() > 0 ? ", [" + n + "]" : ""), what, e, cache, database, da,
|
||||
(purpose == null ? "" : " " + purpose));
|
||||
return s;
|
||||
}
|
||||
|
||||
private void applyPostCommitFunctions() {
|
||||
|
@ -141,15 +166,14 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
|
|||
for (Facet facet : facets) {
|
||||
if (!facet.fixed()) {
|
||||
String columnName = facet.name() + "==" + facet.value();
|
||||
Object value = cache.get(tableName, columnName);
|
||||
if (value != null) {
|
||||
if (result.isPresent() && result.get() != value) {
|
||||
// One facet matched, but another did not.
|
||||
result = Optional.empty();
|
||||
break;
|
||||
} else {
|
||||
result = Optional.of(value);
|
||||
}
|
||||
Either<Object, List<Facet>> eitherValue = cache.get(tableName, columnName);
|
||||
if (eitherValue != null) {
|
||||
Object value = deleted;
|
||||
if (eitherValue.isLeft()) {
|
||||
value = eitherValue.getLeft();
|
||||
}
|
||||
result = Optional.of(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -162,13 +186,46 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
|
|||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Facet> cacheEvict(List<Facet> facets) {
|
||||
Either<Object, List<Facet>> deletedObjectFacets = Either.right(facets);
|
||||
String tableName = CacheUtil.schemaName(facets);
|
||||
Optional<Object> optionalValue = cacheLookup(facets);
|
||||
if (optionalValue.isPresent()) {
|
||||
Object value = optionalValue.get();
|
||||
|
||||
for (Facet facet : facets) {
|
||||
if (!facet.fixed()) {
|
||||
String columnKey = facet.name() + "==" + facet.value();
|
||||
// mark the value identified by the facet to `deleted`
|
||||
cache.put(tableName, columnKey, deletedObjectFacets);
|
||||
}
|
||||
}
|
||||
// look for other row/col pairs that referenced the same object, mark them
|
||||
// `deleted`
|
||||
cache.columnKeySet().forEach(columnKey -> {
|
||||
Either<Object, List<Facet>> eitherCachedValue = cache.get(tableName, columnKey);
|
||||
if (eitherCachedValue.isLeft()) {
|
||||
Object cachedValue = eitherCachedValue.getLeft();
|
||||
if (cachedValue == value) {
|
||||
cache.put(tableName, columnKey, deletedObjectFacets);
|
||||
String[] parts = columnKey.split("==");
|
||||
facets.add(new Facet<String>(parts[0], parts[1]));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return facets;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cacheUpdate(Object value, List<Facet> facets) {
|
||||
Facet table = facets.remove(0);
|
||||
String tableName = table.value().toString();
|
||||
String tableName = CacheUtil.schemaName(facets);
|
||||
for (Facet facet : facets) {
|
||||
String columnName = facet.name() + "==" + facet.value();
|
||||
cache.put(tableName, columnName, value);
|
||||
if (!facet.fixed()) {
|
||||
String columnName = facet.name() + "==" + facet.value();
|
||||
cache.put(tableName, columnName, Either.left(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,32 +265,46 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
|
|||
aborted = false;
|
||||
|
||||
nested.forEach((uow) -> Errors.rethrow().wrap(uow::commit));
|
||||
elapsedTime.stop();
|
||||
|
||||
// Merge UOW cache into parent's cache.
|
||||
if (parent != null) {
|
||||
parent.mergeCache(cache);
|
||||
} else {
|
||||
session.mergeCache(cache);
|
||||
}
|
||||
elapsedTime.stop();
|
||||
|
||||
// Apply all post-commit functions for
|
||||
if (parent == null) {
|
||||
// Apply all post-commit functions, this is the outter-most UnitOfWork.
|
||||
traverser.postOrderTraversal(this).forEach(uow -> {
|
||||
uow.applyPostCommitFunctions();
|
||||
});
|
||||
return new PostCommitFunction(this, null);
|
||||
}
|
||||
}
|
||||
|
||||
// Merge our cache into the session cache.
|
||||
session.mergeCache(cache);
|
||||
|
||||
return new PostCommitFunction(this, null);
|
||||
} else {
|
||||
|
||||
// Merge cache and statistics into parent if there is one.
|
||||
parent.mergeCache(cache);
|
||||
|
||||
parent.cacheHits += cacheHits;
|
||||
parent.cacheMisses += cacheMisses;
|
||||
parent.databaseLookups += databaseLookups;
|
||||
parent.cacheLookupTime += cacheLookupTime;
|
||||
for (String name : databaseTime.keySet()) {
|
||||
if (parent.databaseTime.containsKey(name)) {
|
||||
double t = parent.databaseTime.get(name);
|
||||
parent.databaseTime.put(name, t + databaseTime.get(name));
|
||||
} else {
|
||||
parent.databaseTime.put(name, databaseTime.get(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// else {
|
||||
// Constructor<T> ctor = clazz.getConstructor(conflictExceptionClass);
|
||||
// T object = ctor.newInstance(new Object[] { String message });
|
||||
// }
|
||||
return new PostCommitFunction(this, postCommit);
|
||||
return new PostCommitFunction(this, postCommit);
|
||||
}
|
||||
|
||||
/* Explicitly discard the work and mark it as as such in the log. */
|
||||
public void abort() {
|
||||
public synchronized void abort() {
|
||||
TreeTraverser<AbstractUnitOfWork<E>> traverser = TreeTraverser.using(node -> node::getChildNodes);
|
||||
traverser.postOrderTraversal(this).forEach(uow -> {
|
||||
uow.committed = false;
|
||||
|
@ -249,12 +320,13 @@ public abstract class AbstractUnitOfWork<E extends Exception> implements UnitOfW
|
|||
}
|
||||
}
|
||||
|
||||
private void mergeCache(Table<String, String, Object> from) {
|
||||
Table<String, String, Object> to = this.cache;
|
||||
private void mergeCache(Table<String, String, Either<Object, List<Facet>>> from) {
|
||||
Table<String, String, Either<Object, List<Facet>>> to = this.cache;
|
||||
from.rowMap().forEach((rowKey, columnMap) -> {
|
||||
columnMap.forEach((columnKey, value) -> {
|
||||
if (to.contains(rowKey, columnKey)) {
|
||||
to.put(rowKey, columnKey, CacheUtil.merge(to.get(rowKey, columnKey), from.get(rowKey, columnKey)));
|
||||
//TODO(gburd):...
|
||||
to.put(rowKey, columnKey, Either.left(CacheUtil.merge(to.get(rowKey, columnKey).getLeft(), from.get(rowKey, columnKey).getLeft())));
|
||||
} else {
|
||||
to.put(rowKey, columnKey, from.get(rowKey, columnKey));
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ import java.util.concurrent.TimeUnit;
|
|||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import net.helenus.support.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -48,17 +49,14 @@ import net.helenus.mapping.HelenusEntity;
|
|||
import net.helenus.mapping.HelenusProperty;
|
||||
import net.helenus.mapping.MappingUtil;
|
||||
import net.helenus.mapping.value.*;
|
||||
import net.helenus.support.DslPropertyException;
|
||||
import net.helenus.support.Fun;
|
||||
import net.helenus.support.Fun.Tuple1;
|
||||
import net.helenus.support.Fun.Tuple2;
|
||||
import net.helenus.support.Fun.Tuple6;
|
||||
import net.helenus.support.HelenusException;
|
||||
import net.helenus.support.HelenusMappingException;
|
||||
|
||||
public final class HelenusSession extends AbstractSessionOperations implements Closeable {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(HelenusSession.class);
|
||||
public static final Object deleted = new Object();
|
||||
|
||||
private final int MAX_CACHE_SIZE = 10000;
|
||||
private final int MAX_CACHE_EXPIRE_SECONDS = 600;
|
||||
|
@ -74,7 +72,7 @@ public final class HelenusSession extends AbstractSessionOperations implements C
|
|||
private final SessionRepository sessionRepository;
|
||||
private final Executor executor;
|
||||
private final boolean dropSchemaOnClose;
|
||||
private final Cache sessionCache;
|
||||
private final Cache<String, Object> sessionCache;
|
||||
private final RowColumnValueProvider valueProvider;
|
||||
private final StatementColumnValuePreparer valuePreparer;
|
||||
private final Metadata metadata;
|
||||
|
@ -101,7 +99,8 @@ public final class HelenusSession extends AbstractSessionOperations implements C
|
|||
this.zipkinTracer = tracer;
|
||||
|
||||
this.sessionCache = CacheBuilder.newBuilder().maximumSize(MAX_CACHE_SIZE)
|
||||
.expireAfterAccess(MAX_CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS).recordStats().build();
|
||||
.expireAfterAccess(MAX_CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS)
|
||||
.expireAfterWrite(MAX_CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS).recordStats().build();
|
||||
|
||||
this.valueProvider = new RowColumnValueProvider(this.sessionRepository);
|
||||
this.valuePreparer = new StatementColumnValuePreparer(this.sessionRepository);
|
||||
|
@ -198,6 +197,16 @@ public final class HelenusSession extends AbstractSessionOperations implements C
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cacheEvict(List<Facet> facets) {
|
||||
String tableName = CacheUtil.schemaName(facets);
|
||||
List<String[]> facetCombinations = CacheUtil.flattenFacets(facets);
|
||||
for (String[] combination : facetCombinations) {
|
||||
String cacheKey = tableName + "." + Arrays.toString(combination);
|
||||
sessionCache.invalidate(cacheKey);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateCache(Object pojo, List<Facet> facets) {
|
||||
Map<String, Object> valueMap = pojo instanceof MapExportable ? ((MapExportable) pojo).toMap() : null;
|
||||
|
@ -225,71 +234,72 @@ public final class HelenusSession extends AbstractSessionOperations implements C
|
|||
boundFacets.add(facet);
|
||||
}
|
||||
}
|
||||
String tableName = CacheUtil.schemaName(facets);
|
||||
List<String[]> facetCombinations = CacheUtil.flattenFacets(boundFacets);
|
||||
Object value = sessionCache.getIfPresent(pojo);
|
||||
Object mergedValue = null;
|
||||
for (String[] combination : facetCombinations) {
|
||||
String cacheKey = tableName + "." + Arrays.toString(combination);
|
||||
if (value == null) {
|
||||
sessionCache.put(cacheKey, pojo);
|
||||
} else {
|
||||
if (mergedValue == null) {
|
||||
mergedValue = pojo;
|
||||
} else {
|
||||
mergedValue = CacheUtil.merge(value, pojo);
|
||||
}
|
||||
sessionCache.put(mergedValue, pojo);
|
||||
}
|
||||
}
|
||||
|
||||
String tableName = CacheUtil.schemaName(facets);
|
||||
List<String[]> facetCombinations = CacheUtil.flattenFacets(boundFacets);
|
||||
mergeAndUpdateCacheValues(pojo, tableName, facetCombinations);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mergeCache(Table<String, String, Object> uowCache) {
|
||||
List<Object> pojos = uowCache.values().stream().distinct().collect(Collectors.toList());
|
||||
for (Object pojo : pojos) {
|
||||
HelenusEntity entity = Helenus.resolve(MappingUtil.getMappingInterface(pojo));
|
||||
Map<String, Object> valueMap = pojo instanceof MapExportable ? ((MapExportable) pojo).toMap() : null;
|
||||
if (entity.isCacheable()) {
|
||||
List<Facet> boundFacets = new ArrayList<>();
|
||||
for (Facet facet : entity.getFacets()) {
|
||||
if (facet instanceof UnboundFacet) {
|
||||
UnboundFacet unboundFacet = (UnboundFacet) facet;
|
||||
UnboundFacet.Binder binder = unboundFacet.binder();
|
||||
unboundFacet.getProperties().forEach(prop -> {
|
||||
if (valueMap == null) {
|
||||
Object value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop, false);
|
||||
binder.setValueForProperty(prop, value.toString());
|
||||
} else {
|
||||
binder.setValueForProperty(prop, valueMap.get(prop.getPropertyName()).toString());
|
||||
}
|
||||
});
|
||||
if (binder.isBound()) {
|
||||
boundFacets.add(binder.bind());
|
||||
}
|
||||
} else {
|
||||
boundFacets.add(facet);
|
||||
}
|
||||
}
|
||||
String tableName = entity.getName().toCql();
|
||||
// NOTE: should equal `String tableName = CacheUtil.schemaName(facets);`
|
||||
List<String[]> facetCombinations = CacheUtil.flattenFacets(boundFacets);
|
||||
Object value = sessionCache.getIfPresent(pojo);
|
||||
Object mergedValue = null;
|
||||
for (String[] combination : facetCombinations) {
|
||||
String cacheKey = tableName + "." + Arrays.toString(combination);
|
||||
if (value == null) {
|
||||
sessionCache.put(cacheKey, pojo);
|
||||
} else {
|
||||
if (mergedValue == null) {
|
||||
mergedValue = pojo;
|
||||
} else {
|
||||
mergedValue = CacheUtil.merge(value, pojo);
|
||||
}
|
||||
sessionCache.put(mergedValue, pojo);
|
||||
}
|
||||
public void mergeCache(Table<String, String, Either<Object, List<Facet>>> uowCache) {
|
||||
List<Either<Object, List<Facet>>> items = uowCache.values().stream().distinct().collect(Collectors.toList());
|
||||
for (Either<Object, List<Facet>> item : items) {
|
||||
if (item.isRight()) {
|
||||
List<Facet> facets = item.getRight();
|
||||
String tableName = CacheUtil.schemaName(facets);
|
||||
List<String[]> combinations = CacheUtil.flattenFacets(facets);
|
||||
for (String[] combination : combinations) {
|
||||
String cacheKey = tableName + "." + Arrays.toString(combination);
|
||||
sessionCache.invalidate(cacheKey);
|
||||
}
|
||||
} else {
|
||||
Object pojo = item.getLeft();
|
||||
HelenusEntity entity = Helenus.resolve(MappingUtil.getMappingInterface(pojo));
|
||||
Map<String, Object> valueMap = pojo instanceof MapExportable ? ((MapExportable) pojo).toMap() : null;
|
||||
if (entity.isCacheable()) {
|
||||
List<Facet> boundFacets = new ArrayList<>();
|
||||
for (Facet facet : entity.getFacets()) {
|
||||
if (facet instanceof UnboundFacet) {
|
||||
UnboundFacet unboundFacet = (UnboundFacet) facet;
|
||||
UnboundFacet.Binder binder = unboundFacet.binder();
|
||||
unboundFacet.getProperties().forEach(prop -> {
|
||||
if (valueMap == null) {
|
||||
Object value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop,
|
||||
false);
|
||||
binder.setValueForProperty(prop, value.toString());
|
||||
} else {
|
||||
binder.setValueForProperty(prop, valueMap.get(prop.getPropertyName()).toString());
|
||||
}
|
||||
});
|
||||
if (binder.isBound()) {
|
||||
boundFacets.add(binder.bind());
|
||||
}
|
||||
} else {
|
||||
boundFacets.add(facet);
|
||||
}
|
||||
}
|
||||
// NOTE: should equal `String tableName = CacheUtil.schemaName(facets);`
|
||||
List<String[]> facetCombinations = CacheUtil.flattenFacets(boundFacets);
|
||||
String tableName = CacheUtil.schemaName(boundFacets);
|
||||
mergeAndUpdateCacheValues(pojo, tableName, facetCombinations);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void mergeAndUpdateCacheValues(Object pojo, String tableName, List<String[]> facetCombinations) {
|
||||
Object merged = null;
|
||||
for (String[] combination : facetCombinations) {
|
||||
String cacheKey = tableName + "." + Arrays.toString(combination);
|
||||
Object value = sessionCache.getIfPresent(cacheKey);
|
||||
if (value == null) {
|
||||
sessionCache.put(cacheKey, pojo);
|
||||
} else {
|
||||
if (merged == null) {
|
||||
merged = pojo;
|
||||
} else {
|
||||
merged = CacheUtil.merge(value, pojo);
|
||||
}
|
||||
sessionCache.put(cacheKey, merged);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -318,7 +328,7 @@ public final class HelenusSession extends AbstractSessionOperations implements C
|
|||
Class<? extends UnitOfWork> clazz = unitOfWorkClass;
|
||||
Constructor<? extends UnitOfWork> ctor = clazz.getConstructor(HelenusSession.class, UnitOfWork.class);
|
||||
UnitOfWork uow = ctor.newInstance(this, parent);
|
||||
if (LOG.isInfoEnabled()) {
|
||||
if (LOG.isInfoEnabled() && purpose != null) {
|
||||
uow.setPurpose(purpose.toString());
|
||||
}
|
||||
if (parent != null) {
|
||||
|
|
|
@ -59,6 +59,8 @@ public interface UnitOfWork<X extends Exception> extends AutoCloseable {
|
|||
|
||||
void cacheUpdate(Object pojo, List<Facet> facets);
|
||||
|
||||
List<Facet> cacheEvict(List<Facet> facets);
|
||||
|
||||
UnitOfWork setPurpose(String purpose);
|
||||
|
||||
void addDatabaseTime(String name, Stopwatch amount);
|
||||
|
|
|
@ -107,4 +107,5 @@ public abstract class AbstractFilterOperation<E, O extends AbstractFilterOperati
|
|||
}
|
||||
ifFilters.add(filter);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,6 +32,8 @@ import net.helenus.core.UnitOfWork;
|
|||
import net.helenus.core.cache.CacheUtil;
|
||||
import net.helenus.core.cache.Facet;
|
||||
|
||||
import static net.helenus.core.HelenusSession.deleted;
|
||||
|
||||
public abstract class AbstractOptionalOperation<E, O extends AbstractOptionalOperation<E, O>>
|
||||
extends
|
||||
AbstractStatementOperation<E, O> {
|
||||
|
@ -109,41 +111,48 @@ public abstract class AbstractOptionalOperation<E, O extends AbstractOptionalOpe
|
|||
|
||||
Optional<E> result = Optional.empty();
|
||||
E cachedResult = null;
|
||||
boolean updateCache = true;
|
||||
final boolean updateCache;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (facets != null) {
|
||||
cachedResult = checkCache(uow, facets);
|
||||
if (cachedResult != null) {
|
||||
updateCache = false;
|
||||
result = Optional.of(cachedResult);
|
||||
uowCacheHits.mark();
|
||||
cacheHits.mark();
|
||||
uow.recordCacheAndDatabaseOperationCount(1, 0);
|
||||
} else {
|
||||
updateCache = true;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
updateCache = false;
|
||||
}
|
||||
} finally {
|
||||
timer.stop();
|
||||
uow.addCacheLookupTime(timer);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
updateCache = false;
|
||||
}
|
||||
|
||||
if (!result.isPresent()) {
|
||||
// Formulate the query and execute it against the Cassandra cluster.
|
||||
|
@ -154,14 +163,16 @@ public abstract class AbstractOptionalOperation<E, O extends AbstractOptionalOpe
|
|||
result = transform(resultSet);
|
||||
}
|
||||
|
||||
// If we have a result, it wasn't from the UOW cache, and we're caching things
|
||||
// then we
|
||||
// need to put this result into the cache for future requests to find.
|
||||
if (updateCache && result.isPresent()) {
|
||||
updateCache(uow, result.get(), getFacets());
|
||||
}
|
||||
|
||||
return result;
|
||||
if (result.get() == deleted) {
|
||||
return Optional.empty();
|
||||
} else {
|
||||
// If we have a result, it wasn't from the UOW cache, and we're caching things
|
||||
// then we need to put this result into the cache for future requests to find.
|
||||
if (updateCache && result.isPresent()) {
|
||||
cacheUpdate(uow, result.get(), getFacets());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} finally {
|
||||
context.stop();
|
||||
}
|
||||
|
|
|
@ -329,7 +329,7 @@ public abstract class AbstractStatementOperation<E, O extends AbstractStatementO
|
|||
return result;
|
||||
}
|
||||
|
||||
protected void updateCache(UnitOfWork<?> uow, E pojo, List<Facet> identifyingFacets) {
|
||||
protected void cacheUpdate(UnitOfWork<?> uow, E pojo, List<Facet> identifyingFacets) {
|
||||
List<Facet> facets = new ArrayList<>();
|
||||
Map<String, Object> valueMap = pojo instanceof MapExportable ? ((MapExportable) pojo).toMap() : null;
|
||||
|
||||
|
|
|
@ -33,6 +33,8 @@ import net.helenus.core.UnitOfWork;
|
|||
import net.helenus.core.cache.CacheUtil;
|
||||
import net.helenus.core.cache.Facet;
|
||||
|
||||
import static net.helenus.core.HelenusSession.deleted;
|
||||
|
||||
public abstract class AbstractStreamOperation<E, O extends AbstractStreamOperation<E, O>>
|
||||
extends
|
||||
AbstractStatementOperation<E, O> {
|
||||
|
@ -115,41 +117,48 @@ public abstract class AbstractStreamOperation<E, O extends AbstractStreamOperati
|
|||
try {
|
||||
Stream<E> resultStream = null;
|
||||
E cachedResult = null;
|
||||
boolean updateCache = true;
|
||||
final boolean updateCache;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (facets != null) {
|
||||
cachedResult = checkCache(uow, facets);
|
||||
if (cachedResult != null) {
|
||||
updateCache = false;
|
||||
resultStream = Stream.of(cachedResult);
|
||||
uowCacheHits.mark();
|
||||
cacheHits.mark();
|
||||
uow.recordCacheAndDatabaseOperationCount(1, 0);
|
||||
} else {
|
||||
updateCache = true;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
updateCache = false;
|
||||
}
|
||||
} finally {
|
||||
timer.stop();
|
||||
uow.addCacheLookupTime(timer);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
updateCache = false;
|
||||
}
|
||||
|
||||
if (resultStream == null) {
|
||||
ResultSet resultSet = execute(sessionOps, uow, traceContext, queryExecutionTimeout, queryTimeoutUnits,
|
||||
|
@ -159,17 +168,20 @@ public abstract class AbstractStreamOperation<E, O extends AbstractStreamOperati
|
|||
|
||||
// If we have a result and we're caching then we need to put it into the cache
|
||||
// for future requests to find.
|
||||
if (updateCache && resultStream != null) {
|
||||
List<E> again = new ArrayList<>();
|
||||
List<Facet> facets = getFacets();
|
||||
resultStream.forEach(result -> {
|
||||
updateCache(uow, result, facets);
|
||||
again.add(result);
|
||||
});
|
||||
resultStream = again.stream();
|
||||
if (resultStream != null) {
|
||||
List<E> again = new ArrayList<>();
|
||||
List<Facet> facets = getFacets();
|
||||
resultStream.forEach(result -> {
|
||||
if (result != deleted) {
|
||||
if (updateCache) {
|
||||
cacheUpdate(uow, result, facets);
|
||||
}
|
||||
again.add(result);
|
||||
}
|
||||
});
|
||||
resultStream = again.stream();
|
||||
}
|
||||
|
||||
return resultStream;
|
||||
return resultStream;
|
||||
} finally {
|
||||
context.stop();
|
||||
}
|
||||
|
|
|
@ -15,6 +15,11 @@
|
|||
*/
|
||||
package net.helenus.core.operation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.datastax.driver.core.ResultSet;
|
||||
import com.datastax.driver.core.querybuilder.BuiltStatement;
|
||||
import com.datastax.driver.core.querybuilder.Delete;
|
||||
|
@ -23,8 +28,12 @@ import com.datastax.driver.core.querybuilder.QueryBuilder;
|
|||
|
||||
import net.helenus.core.AbstractSessionOperations;
|
||||
import net.helenus.core.Filter;
|
||||
import net.helenus.core.UnitOfWork;
|
||||
import net.helenus.core.cache.Facet;
|
||||
import net.helenus.core.cache.UnboundFacet;
|
||||
import net.helenus.core.reflect.HelenusPropertyNode;
|
||||
import net.helenus.mapping.HelenusEntity;
|
||||
import net.helenus.mapping.HelenusProperty;
|
||||
import net.helenus.support.HelenusMappingException;
|
||||
|
||||
public final class DeleteOperation extends AbstractFilterOperation<ResultSet, DeleteOperation> {
|
||||
|
@ -122,4 +131,65 @@ public final class DeleteOperation extends AbstractFilterOperation<ResultSet, De
|
|||
+ entity.getMappingInterface() + " or " + p.getEntity().getMappingInterface());
|
||||
}
|
||||
}
|
||||
|
||||
public List<Facet> bindFacetValues(List<Facet> facets) {
|
||||
if (facets == null) {
|
||||
return new ArrayList<Facet>();
|
||||
}
|
||||
List<Facet> boundFacets = new ArrayList<>();
|
||||
Map<HelenusProperty, Filter> filterMap = new HashMap<>(filters.size());
|
||||
filters.forEach(f -> filterMap.put(f.getNode().getProperty(), f));
|
||||
|
||||
for (Facet facet : facets) {
|
||||
if (facet instanceof UnboundFacet) {
|
||||
UnboundFacet unboundFacet = (UnboundFacet) facet;
|
||||
UnboundFacet.Binder binder = unboundFacet.binder();
|
||||
if (filters != null) {
|
||||
for (HelenusProperty prop : unboundFacet.getProperties()) {
|
||||
|
||||
Filter filter = filterMap.get(prop);
|
||||
if (filter != null) {
|
||||
Object[] postulates = filter.postulateValues();
|
||||
for (Object p : postulates) {
|
||||
binder.setValueForProperty(prop, p.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (binder.isBound()) {
|
||||
boundFacets.add(binder.bind());
|
||||
}
|
||||
} else {
|
||||
boundFacets.add(facet);
|
||||
}
|
||||
}
|
||||
return boundFacets;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultSet sync() {// throws TimeoutException {
|
||||
ResultSet result = super.sync();
|
||||
if (entity.isCacheable()) {
|
||||
sessionOps.cacheEvict(bindFacetValues());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Facet> getFacets() {
|
||||
return entity.getFacets();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultSet sync(UnitOfWork uow) {// throws TimeoutException {
|
||||
if (uow == null) {
|
||||
return sync();
|
||||
}
|
||||
ResultSet result = super.sync(uow);
|
||||
List<Facet> facets = getFacets();
|
||||
uow.cacheEvict(bindFacetValues(facets));
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -235,6 +235,15 @@ public final class InsertOperation<T> extends AbstractOperation<T, InsertOperati
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public T sync() {// throws TimeoutException {
|
||||
T result = super.sync();
|
||||
if (entity.isCacheable() && result != null) {
|
||||
sessionOps.updateCache(result, entity.getFacets());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T sync(UnitOfWork uow) {// throws TimeoutException {
|
||||
if (uow == null) {
|
||||
|
@ -243,7 +252,7 @@ public final class InsertOperation<T> extends AbstractOperation<T, InsertOperati
|
|||
T result = super.sync(uow);
|
||||
Class<?> iface = entity.getMappingInterface();
|
||||
if (resultType == iface) {
|
||||
updateCache(uow, result, entity.getFacets());
|
||||
cacheUpdate(uow, result, entity.getFacets());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -19,6 +19,11 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.datastax.driver.core.RegularStatement;
|
||||
import com.datastax.driver.core.querybuilder.BuiltStatement;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.codahale.metrics.Meter;
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import com.codahale.metrics.Timer;
|
||||
|
@ -36,6 +41,8 @@ import net.helenus.core.cache.Facet;
|
|||
|
||||
public abstract class Operation<E> {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Operation.class);
|
||||
|
||||
protected final AbstractSessionOperations sessionOps;
|
||||
protected final Meter uowCacheHits;
|
||||
protected final Meter uowCacheMiss;
|
||||
|
@ -46,16 +53,19 @@ public abstract class Operation<E> {
|
|||
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.requestLatency = metrics.timer("net.helenus.request-latency");
|
||||
}
|
||||
this.sessionOps = sessionOperations;
|
||||
MetricRegistry metrics = sessionOperations.getMetricRegistry();
|
||||
if (metrics == null) {
|
||||
metrics = new MetricRegistry();
|
||||
}
|
||||
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");
|
||||
}
|
||||
|
||||
public ResultSet execute(AbstractSessionOperations session, UnitOfWork uow, TraceContext traceContext, long timeout,
|
||||
TimeUnit units, boolean showValues, boolean cached) { // throws TimeoutException {
|
||||
|
@ -88,6 +98,7 @@ public abstract class Operation<E> {
|
|||
timer.stop();
|
||||
if (uow != null)
|
||||
uow.addDatabaseTime("Cassandra", timer);
|
||||
log(statement, uow, timer, showValues);
|
||||
}
|
||||
|
||||
} finally {
|
||||
|
@ -98,6 +109,43 @@ public abstract class Operation<E> {
|
|||
}
|
||||
}
|
||||
|
||||
public static String queryString(Statement statement, boolean includeValues) {
|
||||
String query = null;
|
||||
if (statement instanceof BuiltStatement) {
|
||||
BuiltStatement builtStatement = (BuiltStatement) statement;
|
||||
if (includeValues) {
|
||||
RegularStatement regularStatement = builtStatement.setForceNoValues(true);
|
||||
query = regularStatement.getQueryString();
|
||||
} else {
|
||||
query = builtStatement.getQueryString();
|
||||
}
|
||||
} else if (statement instanceof RegularStatement) {
|
||||
RegularStatement regularStatement = (RegularStatement) statement;
|
||||
query = regularStatement.getQueryString();
|
||||
} else {
|
||||
query = statement.toString();
|
||||
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
void log(Statement statement, UnitOfWork uow, Stopwatch timer, boolean showValues) {
|
||||
if (LOG.isInfoEnabled()) {
|
||||
String uowString = "";
|
||||
if (uow != null) {
|
||||
uowString = "UOW(" + uow.hashCode() + ")";
|
||||
}
|
||||
String timerString = "";
|
||||
if (timer != null) {
|
||||
timerString = String.format(" %s ", timer.toString());
|
||||
}
|
||||
LOG.info(String.format("%s%s%s",
|
||||
uowString,
|
||||
timerString,
|
||||
Operation.queryString(statement, false)));
|
||||
}
|
||||
}
|
||||
|
||||
public Statement options(Statement statement) {
|
||||
return statement;
|
||||
}
|
||||
|
|
|
@ -250,7 +250,8 @@ public final class SelectOperation<E> extends AbstractFilterStreamOperation<E, S
|
|||
+ entity.getMappingInterface() + " or " + prop.getEntity().getMappingInterface());
|
||||
}
|
||||
|
||||
if (cached) {
|
||||
//TODO(gburd): writeTime and ttl will be useful on merge() but cause object identity to fail.
|
||||
if (false && cached) {
|
||||
switch (prop.getProperty().getColumnType()) {
|
||||
case PARTITION_KEY :
|
||||
case CLUSTERING_COLUMN :
|
||||
|
|
|
@ -567,16 +567,23 @@ public final class UpdateOperation<E> extends AbstractFilterOperation<E, UpdateO
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E sync() {// throws TimeoutException {
|
||||
E result = super.sync();
|
||||
if (entity.isCacheable() && draft != null) {
|
||||
sessionOps.updateCache(result, getFacets());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E sync(UnitOfWork uow) {// throws TimeoutException {
|
||||
if (uow == null) {
|
||||
return sync();
|
||||
}
|
||||
E result = super.sync(uow);
|
||||
// TODO(gburd): Only drafted entity objects are updated in the cache at this
|
||||
// time.
|
||||
if (draft != null) {
|
||||
updateCache(uow, result, getFacets());
|
||||
cacheUpdate(uow, result, getFacets());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ package net.helenus.core.reflect;
|
|||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
@ -63,34 +64,37 @@ public class DslInvocationHandler<E> implements InvocationHandler {
|
|||
|
||||
private HelenusEntity init(Metadata metadata) {
|
||||
HelenusEntity entity = new HelenusMappingEntity(iface, metadata);
|
||||
Collection<HelenusProperty> properties = entity.getOrderedProperties();
|
||||
if (properties != null) {
|
||||
for (HelenusProperty prop : properties) {
|
||||
|
||||
for (HelenusProperty prop : entity.getOrderedProperties()) {
|
||||
map.put(prop.getGetterMethod(), prop);
|
||||
|
||||
map.put(prop.getGetterMethod(), prop);
|
||||
AbstractDataType type = prop.getDataType();
|
||||
Class<?> javaType = prop.getJavaType();
|
||||
|
||||
AbstractDataType type = prop.getDataType();
|
||||
Class<?> javaType = prop.getJavaType();
|
||||
if (type instanceof UDTDataType && !UDTValue.class.isAssignableFrom(javaType)) {
|
||||
|
||||
if (type instanceof UDTDataType && !UDTValue.class.isAssignableFrom(javaType)) {
|
||||
Object childDsl = Helenus.dsl(javaType, classLoader,
|
||||
Optional.of(new HelenusPropertyNode(prop, parent)),
|
||||
metadata);
|
||||
|
||||
Object childDsl = Helenus.dsl(javaType, classLoader, Optional.of(new HelenusPropertyNode(prop, parent)),
|
||||
metadata);
|
||||
udtMap.put(prop.getGetterMethod(), childDsl);
|
||||
}
|
||||
|
||||
udtMap.put(prop.getGetterMethod(), childDsl);
|
||||
}
|
||||
if (type instanceof DTDataType) {
|
||||
DTDataType dataType = (DTDataType) type;
|
||||
|
||||
if (type instanceof DTDataType) {
|
||||
DTDataType dataType = (DTDataType) type;
|
||||
if (dataType.getDataType() instanceof TupleType && !TupleValue.class.isAssignableFrom(javaType)) {
|
||||
|
||||
if (dataType.getDataType() instanceof TupleType && !TupleValue.class.isAssignableFrom(javaType)) {
|
||||
Object childDsl = Helenus.dsl(javaType, classLoader,
|
||||
Optional.of(new HelenusPropertyNode(prop, parent)), metadata);
|
||||
|
||||
Object childDsl = Helenus.dsl(javaType, classLoader,
|
||||
Optional.of(new HelenusPropertyNode(prop, parent)), metadata);
|
||||
|
||||
tupleMap.put(prop.getGetterMethod(), childDsl);
|
||||
}
|
||||
}
|
||||
}
|
||||
tupleMap.put(prop.getGetterMethod(), childDsl);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
|
|
@ -15,87 +15,83 @@
|
|||
*/
|
||||
package net.helenus.test.integration.build;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.KeyspaceMetadata;
|
||||
import com.datastax.driver.core.Session;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.cassandra.exceptions.ConfigurationException;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.KeyspaceMetadata;
|
||||
import com.datastax.driver.core.Session;
|
||||
|
||||
/** AbstractEmbeddedCassandraTest */
|
||||
public abstract class AbstractEmbeddedCassandraTest {
|
||||
|
||||
private static Cluster cluster;
|
||||
private static Cluster cluster;
|
||||
|
||||
private static String keyspace;
|
||||
private static String keyspace;
|
||||
|
||||
private static Session session;
|
||||
private static Session session;
|
||||
|
||||
private static boolean keep;
|
||||
private static boolean keep;
|
||||
|
||||
public static boolean isConnected() {
|
||||
return session != null;
|
||||
}
|
||||
public static boolean isConnected() {
|
||||
return session != null;
|
||||
}
|
||||
|
||||
public static Cluster getCluster() {
|
||||
return cluster;
|
||||
}
|
||||
public static Cluster getCluster() {
|
||||
return cluster;
|
||||
}
|
||||
|
||||
public static Session getSession() {
|
||||
return session;
|
||||
}
|
||||
public static Session getSession() {
|
||||
return session;
|
||||
}
|
||||
|
||||
public static String getKeyspace() {
|
||||
return keyspace;
|
||||
}
|
||||
public static String getKeyspace() {
|
||||
return keyspace;
|
||||
}
|
||||
|
||||
public static void setKeep(boolean enable) {
|
||||
keep = enable;
|
||||
}
|
||||
public static void setKeep(boolean enable) {
|
||||
keep = enable;
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void startCassandraEmbeddedServer()
|
||||
throws TTransportException, IOException, InterruptedException, ConfigurationException {
|
||||
keyspace = "test" + UUID.randomUUID().toString().replace("-", "");
|
||||
EmbeddedCassandraServerHelper.startEmbeddedCassandra(
|
||||
EmbeddedCassandraServerHelper.CASSANDRA_RNDPORT_YML_FILE);
|
||||
@BeforeClass
|
||||
public static void startCassandraEmbeddedServer()
|
||||
throws TTransportException, IOException, InterruptedException, ConfigurationException {
|
||||
keyspace = "test" + UUID.randomUUID().toString().replace("-", "");
|
||||
EmbeddedCassandraServerHelper.startEmbeddedCassandra(EmbeddedCassandraServerHelper.CASSANDRA_RNDPORT_YML_FILE);
|
||||
|
||||
cluster =
|
||||
Cluster.builder()
|
||||
.addContactPoint(EmbeddedCassandraServerHelper.getHost())
|
||||
.withPort(EmbeddedCassandraServerHelper.getNativeTransportPort())
|
||||
.build();
|
||||
cluster = Cluster.builder().addContactPoint(EmbeddedCassandraServerHelper.getHost())
|
||||
.withPort(EmbeddedCassandraServerHelper.getNativeTransportPort()).build();
|
||||
|
||||
KeyspaceMetadata kmd = cluster.getMetadata().getKeyspace(keyspace);
|
||||
if (kmd == null) {
|
||||
session = cluster.connect();
|
||||
KeyspaceMetadata kmd = cluster.getMetadata().getKeyspace(keyspace);
|
||||
if (kmd == null) {
|
||||
session = cluster.connect();
|
||||
|
||||
String cql =
|
||||
"CREATE KEYSPACE "
|
||||
+ keyspace
|
||||
+ " WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1}"
|
||||
+ " AND DURABLE_WRITES = false;";
|
||||
System.out.println(cql + "\n");
|
||||
session.execute(cql);
|
||||
String cql = "CREATE KEYSPACE " + keyspace
|
||||
+ " WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1}"
|
||||
+ " AND DURABLE_WRITES = false;";
|
||||
System.out.println(cql + "\n");
|
||||
session.execute(cql);
|
||||
|
||||
cql = "USE " + keyspace + ";";
|
||||
System.out.println(cql + "\n");
|
||||
session.execute(cql);
|
||||
} else {
|
||||
session = cluster.connect(keyspace);
|
||||
}
|
||||
}
|
||||
cql = "USE " + keyspace + ";";
|
||||
System.out.println(cql + "\n");
|
||||
session.execute(cql);
|
||||
} else {
|
||||
session = cluster.connect(keyspace);
|
||||
}
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void after() {
|
||||
if (!keep && isConnected()) {
|
||||
session.close();
|
||||
session = null;
|
||||
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
|
||||
}
|
||||
}
|
||||
@AfterClass
|
||||
public static void after() {
|
||||
if (!keep && isConnected()) {
|
||||
session.close();
|
||||
session = null;
|
||||
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,18 +15,19 @@
|
|||
*/
|
||||
package net.helenus.test.integration.core;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ContextInitTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
HelenusSession session = Helenus.init(getSession()).get();
|
||||
HelenusSession session = Helenus.init(getSession()).get();
|
||||
|
||||
System.out.println("Works! " + session);
|
||||
}
|
||||
System.out.println("Works! " + session);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package net.helenus.test.integration.core;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusValidator;
|
||||
import net.helenus.mapping.HelenusEntity;
|
||||
|
@ -10,43 +13,40 @@ import net.helenus.mapping.annotation.Table;
|
|||
import net.helenus.support.HelenusException;
|
||||
import net.helenus.support.HelenusMappingException;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HelenusValidatorTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
@Table
|
||||
interface ModelForValidation {
|
||||
HelenusEntity entity;
|
||||
HelenusProperty prop;
|
||||
|
||||
@Constraints.Email
|
||||
@PartitionKey
|
||||
String id();
|
||||
}
|
||||
@Before
|
||||
public void begin() {
|
||||
Helenus.init(getSession()).singleton();
|
||||
|
||||
HelenusEntity entity;
|
||||
entity = Helenus.entity(ModelForValidation.class);
|
||||
|
||||
HelenusProperty prop;
|
||||
prop = entity.getProperty("id");
|
||||
}
|
||||
|
||||
@Before
|
||||
public void begin() {
|
||||
Helenus.init(getSession()).singleton();
|
||||
@Test(expected = HelenusMappingException.class)
|
||||
public void testWrongType() {
|
||||
HelenusValidator.INSTANCE.validate(prop, Integer.valueOf(123));
|
||||
}
|
||||
|
||||
entity = Helenus.entity(ModelForValidation.class);
|
||||
@Test(expected = HelenusException.class)
|
||||
public void testWrongValue() {
|
||||
HelenusValidator.INSTANCE.validate(prop, "123");
|
||||
}
|
||||
|
||||
prop = entity.getProperty("id");
|
||||
}
|
||||
public void testOk() {
|
||||
HelenusValidator.INSTANCE.validate(prop, "a@b.c");
|
||||
}
|
||||
|
||||
@Test(expected = HelenusMappingException.class)
|
||||
public void testWrongType() {
|
||||
HelenusValidator.INSTANCE.validate(prop, Integer.valueOf(123));
|
||||
}
|
||||
@Table
|
||||
interface ModelForValidation {
|
||||
|
||||
@Test(expected = HelenusException.class)
|
||||
public void testWrongValue() {
|
||||
HelenusValidator.INSTANCE.validate(prop, "123");
|
||||
}
|
||||
|
||||
public void testOk() {
|
||||
HelenusValidator.INSTANCE.validate(prop, "a@b.c");
|
||||
}
|
||||
@Constraints.Email
|
||||
@PartitionKey
|
||||
String id();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,407 +15,340 @@
|
|||
*/
|
||||
package net.helenus.test.integration.core.collection;
|
||||
|
||||
import static net.helenus.core.Query.eq;
|
||||
import static net.helenus.core.Query.get;
|
||||
import static net.helenus.core.Query.getIdx;
|
||||
import static net.helenus.core.Query.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
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;
|
||||
|
||||
public class CollectionTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static Customer customer;
|
||||
static Customer customer;
|
||||
|
||||
static HelenusSession session;
|
||||
static HelenusSession session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Customer.class).autoCreateDrop().get();
|
||||
customer = Helenus.dsl(Customer.class, session.getMetadata());
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Customer.class).autoCreateDrop().get();
|
||||
customer = Helenus.dsl(Customer.class, session.getMetadata());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(customer);
|
||||
}
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(customer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetCRUID() throws TimeoutException {
|
||||
@Test
|
||||
public void testSetCRUID() throws TimeoutException {
|
||||
|
||||
UUID id = UUID.randomUUID();
|
||||
UUID id = UUID.randomUUID();
|
||||
|
||||
Set<String> aliases = new HashSet<String>();
|
||||
aliases.add("Alex");
|
||||
aliases.add("Albert");
|
||||
Set<String> aliases = new HashSet<String>();
|
||||
aliases.add("Alex");
|
||||
aliases.add("Albert");
|
||||
|
||||
// CREATE
|
||||
// CREATE
|
||||
|
||||
session.insert().value(customer::id, id).value(customer::aliases, aliases).sync();
|
||||
session.insert().value(customer::id, id).value(customer::aliases, aliases).sync();
|
||||
|
||||
// READ
|
||||
// READ
|
||||
|
||||
// read full object
|
||||
// read full object
|
||||
|
||||
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());
|
||||
Assert.assertNull(actual.properties());
|
||||
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());
|
||||
Assert.assertNull(actual.properties());
|
||||
|
||||
// read full set
|
||||
// read full set
|
||||
|
||||
Set<String> actualSet =
|
||||
session.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(aliases, actualSet);
|
||||
Set<String> actualSet = session.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst()
|
||||
.get()._1;
|
||||
Assert.assertEquals(aliases, actualSet);
|
||||
|
||||
// UPDATE
|
||||
// UPDATE
|
||||
|
||||
Set<String> expected = new HashSet<String>();
|
||||
expected.add("unknown");
|
||||
Set<String> expected = new HashSet<String>();
|
||||
expected.add("unknown");
|
||||
|
||||
session.update().set(customer::aliases, expected).where(customer::id, eq(id)).sync();
|
||||
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());
|
||||
Assert.assertEquals(id, actual.id());
|
||||
Assert.assertEquals(expected, actual.aliases());
|
||||
|
||||
// INSERT
|
||||
// INSERT
|
||||
|
||||
// add operation
|
||||
// add operation
|
||||
|
||||
expected.add("add");
|
||||
session.update().add(customer::aliases, "add").where(customer::id, eq(id)).sync();
|
||||
expected.add("add");
|
||||
session.update().add(customer::aliases, "add").where(customer::id, eq(id)).sync();
|
||||
|
||||
actualSet =
|
||||
session.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualSet);
|
||||
actualSet = session.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualSet);
|
||||
|
||||
// addAll operation
|
||||
expected.addAll(aliases);
|
||||
session.update().addAll(customer::aliases, aliases).where(customer::id, eq(id)).sync();
|
||||
// addAll operation
|
||||
expected.addAll(aliases);
|
||||
session.update().addAll(customer::aliases, aliases).where(customer::id, eq(id)).sync();
|
||||
|
||||
actualSet =
|
||||
session.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualSet);
|
||||
actualSet = session.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualSet);
|
||||
|
||||
// DELETE
|
||||
// DELETE
|
||||
|
||||
// remove single value
|
||||
// remove single value
|
||||
|
||||
expected.remove("add");
|
||||
session.update().remove(customer::aliases, "add").where(customer::id, eq(id)).sync();
|
||||
expected.remove("add");
|
||||
session.update().remove(customer::aliases, "add").where(customer::id, eq(id)).sync();
|
||||
|
||||
actualSet =
|
||||
session.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualSet);
|
||||
actualSet = session.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualSet);
|
||||
|
||||
// remove values
|
||||
// remove values
|
||||
|
||||
expected.removeAll(aliases);
|
||||
session.update().removeAll(customer::aliases, aliases).where(customer::id, eq(id)).sync();
|
||||
expected.removeAll(aliases);
|
||||
session.update().removeAll(customer::aliases, aliases).where(customer::id, eq(id)).sync();
|
||||
|
||||
actualSet =
|
||||
session.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualSet);
|
||||
actualSet = session.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualSet);
|
||||
|
||||
// remove full list
|
||||
// remove full list
|
||||
|
||||
session.update().set(customer::aliases, null).where(customer::id, eq(id)).sync();
|
||||
session.update().set(customer::aliases, null).where(customer::id, eq(id)).sync();
|
||||
|
||||
actualSet =
|
||||
session.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualSet);
|
||||
actualSet = session.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualSet);
|
||||
|
||||
// remove object
|
||||
// remove object
|
||||
|
||||
session.delete().where(customer::id, eq(id)).sync();
|
||||
Long cnt = session.count().where(customer::id, eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
session.delete().where(customer::id, eq(id)).sync();
|
||||
Long cnt = session.count().where(customer::id, eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListCRUID() throws TimeoutException {
|
||||
@Test
|
||||
public void testListCRUID() throws TimeoutException {
|
||||
|
||||
UUID id = UUID.randomUUID();
|
||||
UUID id = UUID.randomUUID();
|
||||
|
||||
List<String> names = new ArrayList<String>();
|
||||
names.add("Alex");
|
||||
names.add("Albert");
|
||||
List<String> names = new ArrayList<String>();
|
||||
names.add("Alex");
|
||||
names.add("Albert");
|
||||
|
||||
// CREATE
|
||||
// CREATE
|
||||
|
||||
session.insert().value(customer::id, id).value(customer::names, names).sync();
|
||||
session.insert().value(customer::id, id).value(customer::names, names).sync();
|
||||
|
||||
// READ
|
||||
// READ
|
||||
|
||||
// read full object
|
||||
// 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());
|
||||
Assert.assertNull(actual.aliases());
|
||||
Assert.assertNull(actual.properties());
|
||||
Assert.assertEquals(id, actual.id());
|
||||
Assert.assertEquals(names, actual.names());
|
||||
Assert.assertNull(actual.aliases());
|
||||
Assert.assertNull(actual.properties());
|
||||
|
||||
// read full list
|
||||
// read full list
|
||||
|
||||
List<String> actualList =
|
||||
session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(names, actualList);
|
||||
List<String> actualList = session.select(customer::names).where(customer::id, eq(id)).sync().findFirst()
|
||||
.get()._1;
|
||||
Assert.assertEquals(names, actualList);
|
||||
|
||||
// read single value by index
|
||||
// read single value by index
|
||||
|
||||
String cql = session.select(getIdx(customer::names, 1)).where(customer::id, eq(id)).cql();
|
||||
String cql = session.select(getIdx(customer::names, 1)).where(customer::id, eq(id)).cql();
|
||||
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
|
||||
// UPDATE
|
||||
// UPDATE
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("unknown");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("unknown");
|
||||
|
||||
session.update().set(customer::names, expected).where(customer::id, eq(id)).sync();
|
||||
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());
|
||||
Assert.assertEquals(id, actual.id());
|
||||
Assert.assertEquals(expected, actual.names());
|
||||
|
||||
// INSERT
|
||||
// INSERT
|
||||
|
||||
// prepend operation
|
||||
// prepend operation
|
||||
|
||||
expected.add(0, "prepend");
|
||||
session.update().prepend(customer::names, "prepend").where(customer::id, eq(id)).sync();
|
||||
expected.add(0, "prepend");
|
||||
session.update().prepend(customer::names, "prepend").where(customer::id, eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualList);
|
||||
actualList = session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualList);
|
||||
|
||||
// append operation
|
||||
// append operation
|
||||
|
||||
expected.add("append");
|
||||
session.update().append(customer::names, "append").where(customer::id, eq(id)).sync();
|
||||
expected.add("append");
|
||||
session.update().append(customer::names, "append").where(customer::id, eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualList);
|
||||
actualList = session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualList);
|
||||
|
||||
// prependAll operation
|
||||
expected.addAll(0, names);
|
||||
session.update().prependAll(customer::names, names).where(customer::id, eq(id)).sync();
|
||||
// prependAll operation
|
||||
expected.addAll(0, names);
|
||||
session.update().prependAll(customer::names, names).where(customer::id, eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualList);
|
||||
actualList = session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualList);
|
||||
|
||||
// appendAll operation
|
||||
expected.addAll(names);
|
||||
session.update().appendAll(customer::names, names).where(customer::id, eq(id)).sync();
|
||||
// appendAll operation
|
||||
expected.addAll(names);
|
||||
session.update().appendAll(customer::names, names).where(customer::id, eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualList);
|
||||
actualList = session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualList);
|
||||
|
||||
// set by Index
|
||||
// set by Index
|
||||
|
||||
expected.set(5, "inserted");
|
||||
session.update().setIdx(customer::names, 5, "inserted").where(customer::id, eq(id)).sync();
|
||||
expected.set(5, "inserted");
|
||||
session.update().setIdx(customer::names, 5, "inserted").where(customer::id, eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualList);
|
||||
actualList = session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualList);
|
||||
|
||||
// DELETE
|
||||
// DELETE
|
||||
|
||||
// remove single value
|
||||
// remove single value
|
||||
|
||||
expected.remove("inserted");
|
||||
session.update().discard(customer::names, "inserted").where(customer::id, eq(id)).sync();
|
||||
expected.remove("inserted");
|
||||
session.update().discard(customer::names, "inserted").where(customer::id, eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualList);
|
||||
actualList = session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualList);
|
||||
|
||||
// remove values
|
||||
// remove values
|
||||
|
||||
expected.removeAll(names);
|
||||
session.update().discardAll(customer::names, names).where(customer::id, eq(id)).sync();
|
||||
expected.removeAll(names);
|
||||
session.update().discardAll(customer::names, names).where(customer::id, eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualList);
|
||||
actualList = session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualList);
|
||||
|
||||
// remove full list
|
||||
// remove full list
|
||||
|
||||
session.update().set(customer::names, null).where(customer::id, eq(id)).sync();
|
||||
session.update().set(customer::names, null).where(customer::id, eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualList);
|
||||
actualList = session.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualList);
|
||||
|
||||
// remove object
|
||||
// remove object
|
||||
|
||||
session.delete().where(customer::id, eq(id)).sync();
|
||||
Long cnt = session.count().where(customer::id, eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
session.delete().where(customer::id, eq(id)).sync();
|
||||
Long cnt = session.count().where(customer::id, eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapCRUID() throws TimeoutException {
|
||||
@Test
|
||||
public void testMapCRUID() throws TimeoutException {
|
||||
|
||||
UUID id = UUID.randomUUID();
|
||||
UUID id = UUID.randomUUID();
|
||||
|
||||
Map<String, String> props = new HashMap<String, String>();
|
||||
props.put("key1", "value1");
|
||||
props.put("key2", "value2");
|
||||
Map<String, String> props = new HashMap<String, String>();
|
||||
props.put("key1", "value1");
|
||||
props.put("key2", "value2");
|
||||
|
||||
// CREATE
|
||||
// CREATE
|
||||
|
||||
session.insert().value(customer::id, id).value(customer::properties, props).sync();
|
||||
session.insert().value(customer::id, id).value(customer::properties, props).sync();
|
||||
|
||||
// READ
|
||||
// READ
|
||||
|
||||
// read full object
|
||||
// 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());
|
||||
Assert.assertNull(actual.aliases());
|
||||
Assert.assertNull(actual.names());
|
||||
Assert.assertEquals(id, actual.id());
|
||||
Assert.assertEquals(props, actual.properties());
|
||||
Assert.assertNull(actual.aliases());
|
||||
Assert.assertNull(actual.names());
|
||||
|
||||
// read full map
|
||||
// read full map
|
||||
|
||||
Map<String, String> actualMap =
|
||||
session
|
||||
.select(customer::properties)
|
||||
.where(customer::id, eq(id))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
Assert.assertEquals(props, actualMap);
|
||||
Map<String, String> actualMap = session.select(customer::properties).where(customer::id, eq(id)).sync()
|
||||
.findFirst().get()._1;
|
||||
Assert.assertEquals(props, actualMap);
|
||||
|
||||
// read single key-value in map
|
||||
// read single key-value in map
|
||||
|
||||
String cql =
|
||||
session.select(get(customer::properties, "key1")).where(customer::id, eq(id)).cql();
|
||||
String cql = session.select(get(customer::properties, "key1")).where(customer::id, eq(id)).cql();
|
||||
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
|
||||
// UPDATE
|
||||
// UPDATE
|
||||
|
||||
Map<String, String> expected = new HashMap<String, String>();
|
||||
expected.put("k1", "v1");
|
||||
expected.put("k2", "v2");
|
||||
Map<String, String> expected = new HashMap<String, String>();
|
||||
expected.put("k1", "v1");
|
||||
expected.put("k2", "v2");
|
||||
|
||||
session.update().set(customer::properties, expected).where(customer::id, eq(id)).sync();
|
||||
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);
|
||||
Assert.assertEquals(id, actual.id());
|
||||
Assert.assertEquals(expected, actual.properties());
|
||||
actual = session.<Customer>select(customer).where(customer::id, eq(id)).single().sync().orElse(null);
|
||||
Assert.assertEquals(id, actual.id());
|
||||
Assert.assertEquals(expected, actual.properties());
|
||||
|
||||
// INSERT
|
||||
// INSERT
|
||||
|
||||
// put operation
|
||||
// put operation
|
||||
|
||||
expected.put("k3", "v3");
|
||||
session.update().put(customer::properties, "k3", "v3").where(customer::id, eq(id)).sync();
|
||||
expected.put("k3", "v3");
|
||||
session.update().put(customer::properties, "k3", "v3").where(customer::id, eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session
|
||||
.select(customer::properties)
|
||||
.where(customer::id, eq(id))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
Assert.assertEquals(expected, actualMap);
|
||||
actualMap = session.select(customer::properties).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualMap);
|
||||
|
||||
// putAll operation
|
||||
expected.putAll(props);
|
||||
session.update().putAll(customer::properties, props).where(customer::id, eq(id)).sync();
|
||||
// putAll operation
|
||||
expected.putAll(props);
|
||||
session.update().putAll(customer::properties, props).where(customer::id, eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session
|
||||
.select(customer::properties)
|
||||
.where(customer::id, eq(id))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
Assert.assertEquals(expected, actualMap);
|
||||
actualMap = session.select(customer::properties).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualMap);
|
||||
|
||||
// put existing
|
||||
// put existing
|
||||
|
||||
expected.put("k3", "v33");
|
||||
session.update().put(customer::properties, "k3", "v33").where(customer::id, eq(id)).sync();
|
||||
expected.put("k3", "v33");
|
||||
session.update().put(customer::properties, "k3", "v33").where(customer::id, eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session
|
||||
.select(customer::properties)
|
||||
.where(customer::id, eq(id))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
Assert.assertEquals(expected, actualMap);
|
||||
actualMap = session.select(customer::properties).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualMap);
|
||||
|
||||
// DELETE
|
||||
// DELETE
|
||||
|
||||
// remove single key
|
||||
// remove single key
|
||||
|
||||
expected.remove("k3");
|
||||
session.update().put(customer::properties, "k3", null).where(customer::id, eq(id)).sync();
|
||||
expected.remove("k3");
|
||||
session.update().put(customer::properties, "k3", null).where(customer::id, eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session
|
||||
.select(customer::properties)
|
||||
.where(customer::id, eq(id))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
Assert.assertEquals(expected, actualMap);
|
||||
|
||||
// remove full map
|
||||
|
||||
session.update().set(customer::properties, null).where(customer::id, eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session
|
||||
.select(customer::properties)
|
||||
.where(customer::id, eq(id))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
Assert.assertNull(actualMap);
|
||||
|
||||
// remove object
|
||||
|
||||
session.delete().where(customer::id, eq(id)).sync();
|
||||
Long cnt = session.count().where(customer::id, eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
actualMap = session.select(customer::properties).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(expected, actualMap);
|
||||
|
||||
// remove full map
|
||||
|
||||
session.update().set(customer::properties, null).where(customer::id, eq(id)).sync();
|
||||
|
||||
actualMap = session.select(customer::properties).where(customer::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualMap);
|
||||
|
||||
// remove object
|
||||
|
||||
session.delete().where(customer::id, eq(id)).sync();
|
||||
Long cnt = session.count().where(customer::id, eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,11 +15,13 @@
|
|||
*/
|
||||
package net.helenus.test.integration.core.collection;
|
||||
|
||||
import com.datastax.driver.core.DataType.Name;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.datastax.driver.core.DataType.Name;
|
||||
|
||||
import net.helenus.mapping.annotation.PartitionKey;
|
||||
import net.helenus.mapping.annotation.Table;
|
||||
import net.helenus.mapping.annotation.Types;
|
||||
|
@ -27,15 +29,15 @@ import net.helenus.mapping.annotation.Types;
|
|||
@Table
|
||||
public interface Customer {
|
||||
|
||||
@PartitionKey
|
||||
UUID id();
|
||||
@PartitionKey
|
||||
UUID id();
|
||||
|
||||
@Types.Set(Name.TEXT)
|
||||
Set<String> aliases();
|
||||
@Types.Set(Name.TEXT)
|
||||
Set<String> aliases();
|
||||
|
||||
@Types.List(Name.TEXT)
|
||||
List<String> names();
|
||||
@Types.List(Name.TEXT)
|
||||
List<String> names();
|
||||
|
||||
@Types.Map(key = Name.TEXT, value = Name.TEXT)
|
||||
Map<String, String> properties();
|
||||
@Types.Map(key = Name.TEXT, value = Name.TEXT)
|
||||
Map<String, String> properties();
|
||||
}
|
||||
|
|
|
@ -17,92 +17,89 @@ package net.helenus.test.integration.core.compound;
|
|||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.core.Operator;
|
||||
import net.helenus.core.Query;
|
||||
import net.helenus.support.Mutable;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CompondKeyTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
Timeline timeline;
|
||||
Timeline timeline;
|
||||
|
||||
HelenusSession session;
|
||||
HelenusSession session;
|
||||
|
||||
public static class TimelineImpl implements Timeline {
|
||||
@Before
|
||||
public void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Timeline.class).autoCreateDrop().get();
|
||||
timeline = Helenus.dsl(Timeline.class, session.getMetadata());
|
||||
}
|
||||
|
||||
UUID userId;
|
||||
Date timestamp;
|
||||
String text;
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
|
||||
@Override
|
||||
public UUID userId() {
|
||||
return userId;
|
||||
}
|
||||
UUID userId = UUID.randomUUID();
|
||||
long postTime = System.currentTimeMillis() - 100000L;
|
||||
|
||||
@Override
|
||||
public Date timestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
session.showCql(false);
|
||||
|
||||
@Override
|
||||
public String text() {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i != 100; ++i) {
|
||||
|
||||
@Before
|
||||
public void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Timeline.class).autoCreateDrop().get();
|
||||
timeline = Helenus.dsl(Timeline.class, session.getMetadata());
|
||||
}
|
||||
TimelineImpl post = new TimelineImpl();
|
||||
post.userId = userId;
|
||||
post.timestamp = new Date(postTime + 1000L * i);
|
||||
post.text = "hello";
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
session.upsert(post).sync();
|
||||
}
|
||||
|
||||
UUID userId = UUID.randomUUID();
|
||||
long postTime = System.currentTimeMillis() - 100000L;
|
||||
session.showCql(true);
|
||||
|
||||
session.showCql(false);
|
||||
final Mutable<Date> d = new Mutable<Date>(null);
|
||||
final Mutable<Integer> c = new Mutable<Integer>(0);
|
||||
|
||||
for (int i = 0; i != 100; ++i) {
|
||||
session.select(timeline::userId, timeline::timestamp, timeline::text)
|
||||
.where(timeline::userId, Operator.EQ, userId).orderBy(Query.desc(timeline::timestamp)).limit(5).sync()
|
||||
.forEach(t -> {
|
||||
|
||||
TimelineImpl post = new TimelineImpl();
|
||||
post.userId = userId;
|
||||
post.timestamp = new Date(postTime + 1000L * i);
|
||||
post.text = "hello";
|
||||
// System.out.println(t);
|
||||
c.set(c.get() + 1);
|
||||
|
||||
session.upsert(post).sync();
|
||||
}
|
||||
Date cd = d.get();
|
||||
if (cd != null) {
|
||||
Assert.assertTrue(cd.after(t._2));
|
||||
}
|
||||
d.set(t._2);
|
||||
});
|
||||
|
||||
session.showCql(true);
|
||||
Assert.assertEquals(Integer.valueOf(5), c.get());
|
||||
}
|
||||
|
||||
final Mutable<Date> d = new Mutable<Date>(null);
|
||||
final Mutable<Integer> c = new Mutable<Integer>(0);
|
||||
public static class TimelineImpl implements Timeline {
|
||||
|
||||
session
|
||||
.select(timeline::userId, timeline::timestamp, timeline::text)
|
||||
.where(timeline::userId, Operator.EQ, userId)
|
||||
.orderBy(Query.desc(timeline::timestamp))
|
||||
.limit(5)
|
||||
.sync()
|
||||
.forEach(
|
||||
t -> {
|
||||
UUID userId;
|
||||
Date timestamp;
|
||||
String text;
|
||||
|
||||
//System.out.println(t);
|
||||
c.set(c.get() + 1);
|
||||
@Override
|
||||
public UUID userId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
Date cd = d.get();
|
||||
if (cd != null) {
|
||||
Assert.assertTrue(cd.after(t._2));
|
||||
}
|
||||
d.set(t._2);
|
||||
});
|
||||
@Override
|
||||
public Date timestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
Assert.assertEquals(Integer.valueOf(5), c.get());
|
||||
}
|
||||
@Override
|
||||
public String text() {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,22 +17,19 @@ package net.helenus.test.integration.core.compound;
|
|||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
import net.helenus.mapping.annotation.ClusteringColumn;
|
||||
import net.helenus.mapping.annotation.Column;
|
||||
import net.helenus.mapping.annotation.PartitionKey;
|
||||
import net.helenus.mapping.annotation.Table;
|
||||
import net.helenus.mapping.annotation.Types;
|
||||
|
||||
import net.helenus.mapping.annotation.*;
|
||||
|
||||
@Table
|
||||
public interface Timeline {
|
||||
|
||||
@PartitionKey(ordinal = 0)
|
||||
UUID userId();
|
||||
@PartitionKey(ordinal = 0)
|
||||
UUID userId();
|
||||
|
||||
@ClusteringColumn(ordinal = 1)
|
||||
@Types.Timeuuid
|
||||
Date timestamp();
|
||||
@ClusteringColumn(ordinal = 1)
|
||||
@Types.Timeuuid
|
||||
Date timestamp();
|
||||
|
||||
@Column(ordinal = 2)
|
||||
String text();
|
||||
@Column(ordinal = 2)
|
||||
String text();
|
||||
}
|
||||
|
|
|
@ -18,46 +18,46 @@ package net.helenus.test.integration.core.counter;
|
|||
import static net.helenus.core.Query.eq;
|
||||
|
||||
import java.util.concurrent.TimeoutException;
|
||||
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;
|
||||
|
||||
public class CounterTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static Page page;
|
||||
static Page page;
|
||||
|
||||
static HelenusSession session;
|
||||
static HelenusSession session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Page.class).autoCreateDrop().get();
|
||||
page = Helenus.dsl(Page.class, session.getMetadata());
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Page.class).autoCreateDrop().get();
|
||||
page = Helenus.dsl(Page.class, session.getMetadata());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(page);
|
||||
}
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(page);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCounter() throws TimeoutException {
|
||||
@Test
|
||||
public void testCounter() throws TimeoutException {
|
||||
|
||||
boolean exists =
|
||||
session.select(page::hits).where(page::alias, eq("index")).sync().findFirst().isPresent();
|
||||
Assert.assertFalse(exists);
|
||||
boolean exists = session.select(page::hits).where(page::alias, eq("index")).sync().findFirst().isPresent();
|
||||
Assert.assertFalse(exists);
|
||||
|
||||
session.update().increment(page::hits, 10L).where(page::alias, eq("index")).sync();
|
||||
session.update().increment(page::hits, 10L).where(page::alias, eq("index")).sync();
|
||||
|
||||
long hits =
|
||||
session.select(page::hits).where(page::alias, eq("index")).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(10, hits);
|
||||
long hits = session.select(page::hits).where(page::alias, eq("index")).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(10, hits);
|
||||
|
||||
session.update().decrement(page::hits).where(page::alias, eq("index")).sync();
|
||||
session.update().decrement(page::hits).where(page::alias, eq("index")).sync();
|
||||
|
||||
hits = session.select(page::hits).where(page::alias, eq("index")).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(9, hits);
|
||||
}
|
||||
hits = session.select(page::hits).where(page::alias, eq("index")).sync().findFirst().get()._1;
|
||||
Assert.assertEquals(9, hits);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,9 +22,9 @@ import net.helenus.mapping.annotation.Types;
|
|||
@Table
|
||||
public interface Page {
|
||||
|
||||
@PartitionKey
|
||||
String alias();
|
||||
@PartitionKey
|
||||
String alias();
|
||||
|
||||
@Types.Counter
|
||||
long hits();
|
||||
@Types.Counter
|
||||
long hits();
|
||||
}
|
||||
|
|
|
@ -18,72 +18,62 @@ 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;
|
||||
|
||||
public class EntityDraftBuilderTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static Supply supply;
|
||||
static HelenusSession session;
|
||||
static Supply supply;
|
||||
static HelenusSession session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Supply.class).autoCreateDrop().get();
|
||||
supply = session.dsl(Supply.class);
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Supply.class).autoCreateDrop().get();
|
||||
supply = session.dsl(Supply.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFoo() throws Exception {
|
||||
Supply.Draft draft = null;
|
||||
@Test
|
||||
public void testFoo() throws Exception {
|
||||
Supply.Draft draft = null;
|
||||
|
||||
draft =
|
||||
Supply.draft("APAC")
|
||||
.code("WIDGET-002")
|
||||
.description("Our second Widget!")
|
||||
.demand(
|
||||
new HashMap<String, Long>() {
|
||||
{
|
||||
put("APAC", 100L);
|
||||
put("EMEA", 10000L);
|
||||
put("NORAM", 2000000L);
|
||||
}
|
||||
})
|
||||
.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>() {
|
||||
{
|
||||
add("Puddle, Inc.");
|
||||
add("Jimmy Town, LTD.");
|
||||
}
|
||||
});
|
||||
draft = Supply.draft("APAC").code("WIDGET-002").description("Our second Widget!")
|
||||
.demand(new HashMap<String, Long>() {
|
||||
{
|
||||
put("APAC", 100L);
|
||||
put("EMEA", 10000L);
|
||||
put("NORAM", 2000000L);
|
||||
}
|
||||
}).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>() {
|
||||
{
|
||||
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())
|
||||
.prepend(supply::suppliers, "Pignose Supply, LLC.")
|
||||
.sync();
|
||||
Assert.assertEquals(s2.suppliers().get(0), "Pignose Supply, LLC.");
|
||||
// List
|
||||
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();
|
||||
Assert.assertTrue(s3.shipments().contains(shipment));
|
||||
// Set
|
||||
String shipment = "Pignose, on the way! (1M units)";
|
||||
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);
|
||||
}
|
||||
// Map
|
||||
Supply s4 = session.<Supply>update(s3.update()).put(supply::demand, "NORAM", 10L).sync();
|
||||
Assert.assertEquals((long) s4.demand().get("NORAM"), 10L);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
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;
|
||||
|
@ -9,85 +10,85 @@ import net.helenus.mapping.annotation.*;
|
|||
@Table
|
||||
public interface Inventory {
|
||||
|
||||
static Inventory inventory = Helenus.dsl(Inventory.class);
|
||||
static Inventory inventory = Helenus.dsl(Inventory.class);
|
||||
|
||||
@PartitionKey
|
||||
UUID id();
|
||||
@Transient
|
||||
static Draft draft(UUID id) {
|
||||
return new Draft(id);
|
||||
}
|
||||
|
||||
@Column("emea")
|
||||
@Types.Counter
|
||||
long EMEA();
|
||||
@PartitionKey
|
||||
UUID id();
|
||||
|
||||
@Column("noram")
|
||||
@Types.Counter
|
||||
long NORAM();
|
||||
@Column("emea")
|
||||
@Types.Counter
|
||||
long EMEA();
|
||||
|
||||
@Column("apac")
|
||||
@Types.Counter
|
||||
long APAC();
|
||||
@Column("noram")
|
||||
@Types.Counter
|
||||
long NORAM();
|
||||
|
||||
@Transient
|
||||
static Draft draft(UUID id) {
|
||||
return new Draft(id);
|
||||
}
|
||||
@Column("apac")
|
||||
@Types.Counter
|
||||
long APAC();
|
||||
|
||||
@Transient
|
||||
default Draft update() {
|
||||
return new Draft(this);
|
||||
}
|
||||
@Transient
|
||||
default Draft update() {
|
||||
return new Draft(this);
|
||||
}
|
||||
|
||||
class Draft extends AbstractAuditedEntityDraft<Inventory> {
|
||||
class Draft extends AbstractAuditedEntityDraft<Inventory> {
|
||||
|
||||
// Entity/Draft pattern-enabling methods:
|
||||
Draft(UUID id) {
|
||||
super(null);
|
||||
// Entity/Draft pattern-enabling methods:
|
||||
Draft(UUID id) {
|
||||
super(null);
|
||||
|
||||
// Primary Key:
|
||||
set(inventory::id, id);
|
||||
}
|
||||
// Primary Key:
|
||||
set(inventory::id, id);
|
||||
}
|
||||
|
||||
Draft(Inventory inventory) {
|
||||
super((MapExportable) inventory);
|
||||
}
|
||||
Draft(Inventory 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() {
|
||||
return this.<UUID>get(inventory::id, UUID.class);
|
||||
}
|
||||
// Immutable properties:
|
||||
public UUID id() {
|
||||
return this.<UUID>get(inventory::id, UUID.class);
|
||||
}
|
||||
|
||||
public long EMEA() {
|
||||
return this.<Long>get(inventory::EMEA, long.class);
|
||||
}
|
||||
public long EMEA() {
|
||||
return this.<Long>get(inventory::EMEA, long.class);
|
||||
}
|
||||
|
||||
public Draft EMEA(long count) {
|
||||
mutate(inventory::EMEA, count);
|
||||
return this;
|
||||
}
|
||||
public Draft EMEA(long count) {
|
||||
mutate(inventory::EMEA, count);
|
||||
return this;
|
||||
}
|
||||
|
||||
public long APAC() {
|
||||
return this.<Long>get(inventory::APAC, long.class);
|
||||
}
|
||||
public long APAC() {
|
||||
return this.<Long>get(inventory::APAC, long.class);
|
||||
}
|
||||
|
||||
public Draft APAC(long count) {
|
||||
mutate(inventory::APAC, count);
|
||||
return this;
|
||||
}
|
||||
public Draft APAC(long count) {
|
||||
mutate(inventory::APAC, count);
|
||||
return this;
|
||||
}
|
||||
|
||||
public long NORAM() {
|
||||
return this.<Long>get(inventory::NORAM, long.class);
|
||||
}
|
||||
public long NORAM() {
|
||||
return this.<Long>get(inventory::NORAM, long.class);
|
||||
}
|
||||
|
||||
public Draft NORAM(long count) {
|
||||
mutate(inventory::NORAM, count);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
public Draft NORAM(long count) {
|
||||
mutate(inventory::NORAM, count);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package net.helenus.test.integration.core.draft;
|
||||
|
||||
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;
|
||||
|
@ -13,133 +15,133 @@ import net.helenus.mapping.annotation.*;
|
|||
@Table
|
||||
public interface Supply {
|
||||
|
||||
static Supply supply = Helenus.dsl(Supply.class);
|
||||
static Supply supply = Helenus.dsl(Supply.class);
|
||||
|
||||
@PartitionKey
|
||||
UUID id();
|
||||
@Transient
|
||||
static Draft draft(String region) {
|
||||
return new Draft(region);
|
||||
}
|
||||
|
||||
@ClusteringColumn(ordinal = 0)
|
||||
default String region() {
|
||||
return "NORAM";
|
||||
}
|
||||
@PartitionKey
|
||||
UUID id();
|
||||
|
||||
@Index(caseSensitive = false)
|
||||
String code();
|
||||
@ClusteringColumn(ordinal = 0)
|
||||
default String region() {
|
||||
return "NORAM";
|
||||
}
|
||||
|
||||
@Index
|
||||
String description(); // @IndexText == lucene index
|
||||
@Index(caseSensitive = false)
|
||||
String code();
|
||||
|
||||
@Index
|
||||
Map<String, Long> demand();
|
||||
@Index
|
||||
String description(); // @IndexText == lucene index
|
||||
|
||||
@Index
|
||||
List<String> suppliers();
|
||||
@Index
|
||||
Map<String, Long> demand();
|
||||
|
||||
@Index
|
||||
Set<String> shipments();
|
||||
@Index
|
||||
List<String> suppliers();
|
||||
|
||||
@Transient
|
||||
static Draft draft(String region) {
|
||||
return new Draft(region);
|
||||
}
|
||||
@Index
|
||||
Set<String> shipments();
|
||||
|
||||
@Transient
|
||||
default Draft update() {
|
||||
return new Draft(this);
|
||||
}
|
||||
@Transient
|
||||
default Draft update() {
|
||||
return new Draft(this);
|
||||
}
|
||||
|
||||
class Draft extends AbstractEntityDraft<Supply> {
|
||||
class Draft extends AbstractEntityDraft<Supply> {
|
||||
|
||||
// Entity/Draft pattern-enabling methods:
|
||||
Draft(String region) {
|
||||
super(null);
|
||||
// Entity/Draft pattern-enabling methods:
|
||||
Draft(String region) {
|
||||
super(null);
|
||||
|
||||
// Primary Key:
|
||||
set(supply::id, UUIDs.timeBased());
|
||||
set(supply::region, region);
|
||||
}
|
||||
// Primary Key:
|
||||
set(supply::id, UUIDs.timeBased());
|
||||
set(supply::region, region);
|
||||
}
|
||||
|
||||
Draft(Supply supply) {
|
||||
super((MapExportable) supply);
|
||||
}
|
||||
Draft(Supply supply) {
|
||||
super((MapExportable) supply);
|
||||
}
|
||||
|
||||
public Class<Supply> getEntityClass() {
|
||||
return Supply.class;
|
||||
}
|
||||
public Class<Supply> getEntityClass() {
|
||||
return Supply.class;
|
||||
}
|
||||
|
||||
// Immutable properties:
|
||||
public UUID id() {
|
||||
return this.<UUID>get(supply::id, UUID.class);
|
||||
}
|
||||
// Immutable properties:
|
||||
public UUID id() {
|
||||
return this.<UUID>get(supply::id, UUID.class);
|
||||
}
|
||||
|
||||
public String region() {
|
||||
return this.<String>get(supply::region, String.class);
|
||||
}
|
||||
public String region() {
|
||||
return this.<String>get(supply::region, String.class);
|
||||
}
|
||||
|
||||
// Mutable properties:
|
||||
public String code() {
|
||||
return this.<String>get(supply::code, String.class);
|
||||
}
|
||||
// Mutable properties:
|
||||
public String code() {
|
||||
return this.<String>get(supply::code, String.class);
|
||||
}
|
||||
|
||||
public Draft code(String code) {
|
||||
mutate(supply::code, code);
|
||||
return this;
|
||||
}
|
||||
public Draft code(String code) {
|
||||
mutate(supply::code, code);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Draft setCode(String code) {
|
||||
return code(code);
|
||||
}
|
||||
public Draft setCode(String code) {
|
||||
return code(code);
|
||||
}
|
||||
|
||||
public String description() {
|
||||
return this.<String>get(supply::description, String.class);
|
||||
}
|
||||
public String description() {
|
||||
return this.<String>get(supply::description, String.class);
|
||||
}
|
||||
|
||||
public Draft description(String description) {
|
||||
mutate(supply::description, description);
|
||||
return this;
|
||||
}
|
||||
public Draft description(String description) {
|
||||
mutate(supply::description, description);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Draft setDescription(String description) {
|
||||
return description(description);
|
||||
}
|
||||
public Draft setDescription(String description) {
|
||||
return description(description);
|
||||
}
|
||||
|
||||
public Map<String, Long> demand() {
|
||||
return this.<Map<String, Long>>get(supply::demand, Map.class);
|
||||
}
|
||||
public Map<String, Long> demand() {
|
||||
return this.<Map<String, Long>>get(supply::demand, Map.class);
|
||||
}
|
||||
|
||||
public Draft demand(Map<String, Long> demand) {
|
||||
mutate(supply::demand, demand);
|
||||
return this;
|
||||
}
|
||||
public Draft demand(Map<String, Long> demand) {
|
||||
mutate(supply::demand, demand);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Draft setDemand(Map<String, Long> demand) {
|
||||
return demand(demand);
|
||||
}
|
||||
public Draft setDemand(Map<String, Long> demand) {
|
||||
return demand(demand);
|
||||
}
|
||||
|
||||
public List<String> suppliers() {
|
||||
return this.<List<String>>get(supply::suppliers, List.class);
|
||||
}
|
||||
public List<String> suppliers() {
|
||||
return this.<List<String>>get(supply::suppliers, List.class);
|
||||
}
|
||||
|
||||
public Draft suppliers(List<String> suppliers) {
|
||||
mutate(supply::suppliers, suppliers);
|
||||
return this;
|
||||
}
|
||||
public Draft suppliers(List<String> suppliers) {
|
||||
mutate(supply::suppliers, suppliers);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Draft setSuppliers(List<String> suppliers) {
|
||||
return suppliers(suppliers);
|
||||
}
|
||||
public Draft setSuppliers(List<String> suppliers) {
|
||||
return suppliers(suppliers);
|
||||
}
|
||||
|
||||
public Set<String> shipments() {
|
||||
return this.<Set<String>>get(supply::shipments, Set.class);
|
||||
}
|
||||
public Set<String> shipments() {
|
||||
return this.<Set<String>>get(supply::shipments, Set.class);
|
||||
}
|
||||
|
||||
public Draft shipments(Set<String> shipments) {
|
||||
mutate(supply::shipments, shipments);
|
||||
return this;
|
||||
}
|
||||
public Draft shipments(Set<String> shipments) {
|
||||
mutate(supply::shipments, shipments);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Draft setshipments(Set<String> shipments) {
|
||||
return shipments(shipments);
|
||||
}
|
||||
}
|
||||
public Draft setshipments(Set<String> shipments) {
|
||||
return shipments(shipments);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,17 +23,17 @@ import net.helenus.mapping.annotation.Transient;
|
|||
@InheritedTable
|
||||
public interface Animal {
|
||||
|
||||
@PartitionKey(ordinal = 0)
|
||||
int id();
|
||||
@PartitionKey(ordinal = 0)
|
||||
int id();
|
||||
|
||||
@Column(ordinal = 1)
|
||||
boolean eatable();
|
||||
@Column(ordinal = 1)
|
||||
boolean eatable();
|
||||
|
||||
@Column
|
||||
boolean warmBlodded();
|
||||
@Column
|
||||
boolean warmBlodded();
|
||||
|
||||
@Transient
|
||||
default Animal me() {
|
||||
return this;
|
||||
}
|
||||
@Transient
|
||||
default Animal me() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ import net.helenus.mapping.annotation.Table;
|
|||
@Table("cats")
|
||||
public interface Cat extends Mammal {
|
||||
|
||||
@Column(ordinal = 0)
|
||||
@Index(caseSensitive = false)
|
||||
String nickname();
|
||||
@Column(ordinal = 0)
|
||||
@Index(caseSensitive = false)
|
||||
String nickname();
|
||||
}
|
||||
|
|
|
@ -5,73 +5,59 @@ import static net.helenus.core.Query.eq;
|
|||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
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;
|
||||
|
||||
public class HierarchyTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static Cat cat;
|
||||
static Cat cat;
|
||||
|
||||
static Pig pig;
|
||||
static Pig pig;
|
||||
|
||||
static HelenusSession session;
|
||||
static HelenusSession session;
|
||||
|
||||
static Random rnd = new Random();
|
||||
static Random rnd = new Random();
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session =
|
||||
Helenus.init(getSession()).showCql().add(Cat.class).add(Pig.class).autoCreateDrop().get();
|
||||
cat = Helenus.dsl(Cat.class);
|
||||
pig = Helenus.dsl(Pig.class);
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Cat.class).add(Pig.class).autoCreateDrop().get();
|
||||
cat = Helenus.dsl(Cat.class);
|
||||
pig = Helenus.dsl(Pig.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(cat);
|
||||
}
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(cat);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCounter() throws TimeoutException {
|
||||
@Test
|
||||
public void testCounter() throws TimeoutException {
|
||||
|
||||
session
|
||||
.insert()
|
||||
.value(cat::id, rnd.nextInt())
|
||||
.value(cat::nickname, "garfield")
|
||||
.value(cat::eatable, false)
|
||||
.sync();
|
||||
session
|
||||
.insert()
|
||||
.value(pig::id, rnd.nextInt())
|
||||
.value(pig::nickname, "porky")
|
||||
.value(pig::eatable, true)
|
||||
.sync();
|
||||
session.insert().value(cat::id, rnd.nextInt()).value(cat::nickname, "garfield").value(cat::eatable, false)
|
||||
.sync();
|
||||
session.insert().value(pig::id, rnd.nextInt()).value(pig::nickname, "porky").value(pig::eatable, true).sync();
|
||||
|
||||
Optional<Cat> animal =
|
||||
session.<Cat>select(Cat.class).where(cat::nickname, eq("garfield")).sync().findFirst();
|
||||
Assert.assertTrue(animal.isPresent());
|
||||
Assert.assertTrue(animal.get().warmBlodded());
|
||||
Assert.assertFalse(animal.get().eatable());
|
||||
}
|
||||
Optional<Cat> animal = session.<Cat>select(Cat.class).where(cat::nickname, eq("garfield")).sync().findFirst();
|
||||
Assert.assertTrue(animal.isPresent());
|
||||
Assert.assertTrue(animal.get().warmBlodded());
|
||||
Assert.assertFalse(animal.get().eatable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultMethod() throws TimeoutException {
|
||||
session
|
||||
.insert()
|
||||
.value(cat::id, rnd.nextInt())
|
||||
.value(cat::nickname, "garfield")
|
||||
.value(cat::eatable, false)
|
||||
.sync();
|
||||
Optional<Cat> animal =
|
||||
session.select(Cat.class).where(cat::nickname, eq("garfield")).single().sync();
|
||||
Assert.assertTrue(animal.isPresent());
|
||||
@Test
|
||||
public void testDefaultMethod() throws TimeoutException {
|
||||
session.insert().value(cat::id, rnd.nextInt()).value(cat::nickname, "garfield").value(cat::eatable, false)
|
||||
.sync();
|
||||
Optional<Cat> animal = session.select(Cat.class).where(cat::nickname, eq("garfield")).single().sync();
|
||||
Assert.assertTrue(animal.isPresent());
|
||||
|
||||
Cat cat = animal.get();
|
||||
Animal itsme = cat.me();
|
||||
Assert.assertEquals(cat, itsme);
|
||||
}
|
||||
Cat cat = animal.get();
|
||||
Animal itsme = cat.me();
|
||||
Assert.assertEquals(cat, itsme);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ import net.helenus.mapping.annotation.InheritedTable;
|
|||
@InheritedTable
|
||||
public interface Mammal extends Animal {
|
||||
|
||||
default boolean warmBlodded() {
|
||||
return true;
|
||||
}
|
||||
default boolean warmBlodded() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,6 @@ import net.helenus.mapping.annotation.Table;
|
|||
@Table("pigs")
|
||||
public interface Pig extends Mammal {
|
||||
|
||||
@Column(ordinal = 0)
|
||||
String nickname();
|
||||
@Column(ordinal = 0)
|
||||
String nickname();
|
||||
}
|
||||
|
|
|
@ -23,13 +23,13 @@ import net.helenus.mapping.annotation.Table;
|
|||
@Table("books")
|
||||
public interface Book {
|
||||
|
||||
@PartitionKey(ordinal = 0)
|
||||
long id();
|
||||
@PartitionKey(ordinal = 0)
|
||||
long id();
|
||||
|
||||
@Column(ordinal = 1)
|
||||
@Index
|
||||
String isbn();
|
||||
@Column(ordinal = 1)
|
||||
@Index
|
||||
String isbn();
|
||||
|
||||
@Column(ordinal = 2)
|
||||
String author();
|
||||
@Column(ordinal = 2)
|
||||
String author();
|
||||
}
|
||||
|
|
|
@ -16,39 +16,35 @@
|
|||
package net.helenus.test.integration.core.index;
|
||||
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.core.Query;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.core.Query;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
|
||||
public class SecondaryIndexTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
Book book;
|
||||
Book book;
|
||||
|
||||
HelenusSession session;
|
||||
HelenusSession session;
|
||||
|
||||
@Before
|
||||
public void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Book.class).autoCreateDrop().get();
|
||||
book = Helenus.dsl(Book.class, session.getMetadata());
|
||||
}
|
||||
@Before
|
||||
public void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Book.class).autoCreateDrop().get();
|
||||
book = Helenus.dsl(Book.class, session.getMetadata());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws TimeoutException {
|
||||
@Test
|
||||
public void test() throws TimeoutException {
|
||||
|
||||
session
|
||||
.insert()
|
||||
.value(book::id, 123L)
|
||||
.value(book::isbn, "ABC")
|
||||
.value(book::author, "Alex")
|
||||
.sync();
|
||||
session.insert().value(book::id, 123L).value(book::isbn, "ABC").value(book::author, "Alex").sync();
|
||||
|
||||
long actualId =
|
||||
session.select(book::id).where(book::isbn, Query.eq("ABC")).sync().findFirst().get()._1;
|
||||
long actualId = session.select(book::id).where(book::isbn, Query.eq("ABC")).sync().findFirst().get()._1;
|
||||
|
||||
Assert.assertEquals(123L, actualId);
|
||||
}
|
||||
Assert.assertEquals(123L, actualId);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
package net.helenus.test.integration.core.prepared;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import net.helenus.core.annotation.Cacheable;
|
||||
import net.helenus.mapping.annotation.PartitionKey;
|
||||
import net.helenus.mapping.annotation.Table;
|
||||
|
@ -24,13 +25,13 @@ import net.helenus.mapping.annotation.Table;
|
|||
@Cacheable
|
||||
public interface Car {
|
||||
|
||||
@PartitionKey(ordinal = 0)
|
||||
String make();
|
||||
@PartitionKey(ordinal = 0)
|
||||
String make();
|
||||
|
||||
@PartitionKey(ordinal = 1)
|
||||
String model();
|
||||
@PartitionKey(ordinal = 1)
|
||||
String model();
|
||||
|
||||
int year();
|
||||
int year();
|
||||
|
||||
BigDecimal price();
|
||||
BigDecimal price();
|
||||
}
|
||||
|
|
|
@ -15,8 +15,14 @@
|
|||
*/
|
||||
package net.helenus.test.integration.core.prepared;
|
||||
|
||||
import com.datastax.driver.core.ResultSet;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.datastax.driver.core.ResultSet;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.core.Query;
|
||||
|
@ -24,115 +30,86 @@ import net.helenus.core.operation.PreparedOperation;
|
|||
import net.helenus.core.operation.PreparedStreamOperation;
|
||||
import net.helenus.support.Fun;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PreparedStatementTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static Car car;
|
||||
static Car car;
|
||||
|
||||
static HelenusSession session;
|
||||
static HelenusSession session;
|
||||
|
||||
static PreparedOperation<ResultSet> insertOp;
|
||||
static PreparedOperation<ResultSet> insertOp;
|
||||
|
||||
static PreparedOperation<ResultSet> updateOp;
|
||||
static PreparedOperation<ResultSet> updateOp;
|
||||
|
||||
static PreparedStreamOperation<Car> selectOp;
|
||||
static PreparedStreamOperation<Car> selectOp;
|
||||
|
||||
static PreparedStreamOperation<Fun.Tuple1<BigDecimal>> selectPriceOp;
|
||||
static PreparedStreamOperation<Fun.Tuple1<BigDecimal>> selectPriceOp;
|
||||
|
||||
static PreparedOperation<ResultSet> deleteOp;
|
||||
static PreparedOperation<ResultSet> deleteOp;
|
||||
|
||||
static PreparedOperation<Long> countOp;
|
||||
static PreparedOperation<Long> countOp;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
|
||||
session = Helenus.init(getSession()).showCql().add(Car.class).autoCreateDrop().get();
|
||||
car = Helenus.dsl(Car.class, session.getMetadata());
|
||||
session = Helenus.init(getSession()).showCql().add(Car.class).autoCreateDrop().get();
|
||||
car = Helenus.dsl(Car.class, session.getMetadata());
|
||||
|
||||
insertOp =
|
||||
session
|
||||
.<ResultSet>insert()
|
||||
.value(car::make, Query.marker())
|
||||
.value(car::model, Query.marker())
|
||||
.value(car::year, 2004)
|
||||
.prepare();
|
||||
insertOp = session.<ResultSet>insert().value(car::make, Query.marker()).value(car::model, Query.marker())
|
||||
.value(car::year, 2004).prepare();
|
||||
|
||||
updateOp =
|
||||
session
|
||||
.update()
|
||||
.set(car::price, Query.marker())
|
||||
.where(car::make, Query.eq(Query.marker()))
|
||||
.and(car::model, Query.eq(Query.marker()))
|
||||
.prepare();
|
||||
updateOp = session.update().set(car::price, Query.marker()).where(car::make, Query.eq(Query.marker()))
|
||||
.and(car::model, Query.eq(Query.marker())).prepare();
|
||||
|
||||
selectOp =
|
||||
session
|
||||
.<Car>select(car)
|
||||
.where(car::make, Query.eq(Query.marker()))
|
||||
.and(car::model, Query.eq(Query.marker()))
|
||||
.prepare();
|
||||
selectOp = session.<Car>select(car).where(car::make, Query.eq(Query.marker()))
|
||||
.and(car::model, Query.eq(Query.marker())).prepare();
|
||||
|
||||
selectPriceOp =
|
||||
session
|
||||
.select(car::price)
|
||||
.where(car::make, Query.eq(Query.marker()))
|
||||
.and(car::model, Query.eq(Query.marker()))
|
||||
.prepare();
|
||||
selectPriceOp = session.select(car::price).where(car::make, Query.eq(Query.marker()))
|
||||
.and(car::model, Query.eq(Query.marker())).prepare();
|
||||
|
||||
deleteOp =
|
||||
session
|
||||
.delete()
|
||||
.where(car::make, Query.eq(Query.marker()))
|
||||
.and(car::model, Query.eq(Query.marker()))
|
||||
.prepare();
|
||||
deleteOp = session.delete().where(car::make, Query.eq(Query.marker())).and(car::model, Query.eq(Query.marker()))
|
||||
.prepare();
|
||||
|
||||
countOp =
|
||||
session
|
||||
.count()
|
||||
.where(car::make, Query.eq(Query.marker()))
|
||||
.and(car::model, Query.eq(Query.marker()))
|
||||
.prepare();
|
||||
}
|
||||
countOp = session.count().where(car::make, Query.eq(Query.marker())).and(car::model, Query.eq(Query.marker()))
|
||||
.prepare();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(car);
|
||||
}
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(car);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCRUID() throws Exception {
|
||||
@Test
|
||||
public void testCRUID() throws Exception {
|
||||
|
||||
// INSERT
|
||||
// INSERT
|
||||
|
||||
insertOp.bind("Nissan", "350Z").sync();
|
||||
insertOp.bind("Nissan", "350Z").sync();
|
||||
|
||||
// SELECT
|
||||
// SELECT
|
||||
|
||||
Car actual = selectOp.bind("Nissan", "350Z").sync().findFirst().get();
|
||||
Assert.assertEquals("Nissan", actual.make());
|
||||
Assert.assertEquals("350Z", actual.model());
|
||||
Assert.assertEquals(2004, actual.year());
|
||||
Assert.assertNull(actual.price());
|
||||
Car actual = selectOp.bind("Nissan", "350Z").sync().findFirst().get();
|
||||
Assert.assertEquals("Nissan", actual.make());
|
||||
Assert.assertEquals("350Z", actual.model());
|
||||
Assert.assertEquals(2004, actual.year());
|
||||
Assert.assertNull(actual.price());
|
||||
|
||||
// UPDATE
|
||||
// UPDATE
|
||||
|
||||
updateOp.bind(BigDecimal.valueOf(10000.0), "Nissan", "350Z").sync();
|
||||
updateOp.bind(BigDecimal.valueOf(10000.0), "Nissan", "350Z").sync();
|
||||
|
||||
BigDecimal price = selectPriceOp.bind("Nissan", "350Z").sync().findFirst().get()._1;
|
||||
BigDecimal price = selectPriceOp.bind("Nissan", "350Z").sync().findFirst().get()._1;
|
||||
|
||||
Assert.assertEquals(BigDecimal.valueOf(10000.0), price);
|
||||
Assert.assertEquals(BigDecimal.valueOf(10000.0), price);
|
||||
|
||||
// DELETE
|
||||
// DELETE
|
||||
|
||||
Long cnt = countOp.bind("Nissan", "350Z").sync();
|
||||
Assert.assertEquals(Long.valueOf(1), cnt);
|
||||
Long cnt = countOp.bind("Nissan", "350Z").sync();
|
||||
Assert.assertEquals(Long.valueOf(1), cnt);
|
||||
|
||||
deleteOp.bind("Nissan", "350Z").sync();
|
||||
deleteOp.bind("Nissan", "350Z").sync();
|
||||
|
||||
cnt = countOp.bind("Nissan", "350Z").sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
cnt = countOp.bind("Nissan", "350Z").sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,48 +18,49 @@ package net.helenus.test.integration.core.simple;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.core.operation.InsertOperation;
|
||||
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.core.operation.InsertOperation;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
|
||||
public class InsertPartialTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static HelenusSession session;
|
||||
static User user;
|
||||
static Random rnd = new Random();
|
||||
static HelenusSession session;
|
||||
static User user;
|
||||
static Random rnd = new Random();
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
session = Helenus.init(getSession()).showCql().add(User.class).autoCreateDrop().get();
|
||||
user = Helenus.dsl(User.class);
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
session = Helenus.init(getSession()).showCql().add(User.class).autoCreateDrop().get();
|
||||
user = Helenus.dsl(User.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPartialInsert() throws Exception {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
Long id = rnd.nextLong();
|
||||
map.put("id", id);
|
||||
map.put("age", 5);
|
||||
InsertOperation<User> insert = session.<User>insert(Helenus.map(User.class, map));
|
||||
String cql =
|
||||
"INSERT INTO simple_users (id,age) VALUES (" + id.toString() + ",5) IF NOT EXISTS;";
|
||||
Assert.assertEquals(cql, insert.cql());
|
||||
insert.sync();
|
||||
}
|
||||
@Test
|
||||
public void testPartialInsert() throws Exception {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
Long id = rnd.nextLong();
|
||||
map.put("id", id);
|
||||
map.put("age", 5);
|
||||
InsertOperation<User> insert = session.<User>insert(Helenus.map(User.class, map));
|
||||
String cql = "INSERT INTO simple_users (id,age) VALUES (" + id.toString() + ",5) IF NOT EXISTS;";
|
||||
Assert.assertEquals(cql, insert.cql());
|
||||
insert.sync();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPartialUpsert() throws Exception {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
Long id = rnd.nextLong();
|
||||
map.put("id", id);
|
||||
map.put("age", 5);
|
||||
InsertOperation upsert = session.upsert(Helenus.map(User.class, map));
|
||||
String cql = "INSERT INTO simple_users (id,age) VALUES (" + id.toString() + ",5);";
|
||||
Assert.assertEquals(cql, upsert.cql());
|
||||
upsert.sync();
|
||||
}
|
||||
@Test
|
||||
public void testPartialUpsert() throws Exception {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
Long id = rnd.nextLong();
|
||||
map.put("id", id);
|
||||
map.put("age", 5);
|
||||
InsertOperation upsert = session.upsert(Helenus.map(User.class, map));
|
||||
String cql = "INSERT INTO simple_users (id,age) VALUES (" + id.toString() + ",5);";
|
||||
Assert.assertEquals(cql, upsert.cql());
|
||||
upsert.sync();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,29 +16,25 @@
|
|||
package net.helenus.test.integration.core.simple;
|
||||
|
||||
import java.util.Date;
|
||||
import net.helenus.mapping.annotation.ClusteringColumn;
|
||||
import net.helenus.mapping.annotation.Column;
|
||||
import net.helenus.mapping.annotation.PartitionKey;
|
||||
import net.helenus.mapping.annotation.StaticColumn;
|
||||
import net.helenus.mapping.annotation.Table;
|
||||
import net.helenus.mapping.annotation.Types;
|
||||
|
||||
import net.helenus.mapping.annotation.*;
|
||||
|
||||
@Table
|
||||
public interface Message {
|
||||
|
||||
@PartitionKey
|
||||
int id();
|
||||
@PartitionKey
|
||||
int id();
|
||||
|
||||
@ClusteringColumn
|
||||
@Types.Timeuuid
|
||||
Date timestamp();
|
||||
@ClusteringColumn
|
||||
@Types.Timeuuid
|
||||
Date timestamp();
|
||||
|
||||
@StaticColumn(forceQuote = true)
|
||||
String from();
|
||||
@StaticColumn(forceQuote = true)
|
||||
String from();
|
||||
|
||||
@Column(forceQuote = true)
|
||||
String to();
|
||||
@Column(forceQuote = true)
|
||||
String to();
|
||||
|
||||
@Column
|
||||
String message();
|
||||
@Column
|
||||
String message();
|
||||
}
|
||||
|
|
|
@ -17,242 +17,173 @@ package net.helenus.test.integration.core.simple;
|
|||
|
||||
import static net.helenus.core.Query.eq;
|
||||
|
||||
import com.datastax.driver.core.ResultSet;
|
||||
import java.util.*;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.datastax.driver.core.ResultSet;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.core.Operator;
|
||||
import net.helenus.core.operation.UpdateOperation;
|
||||
import net.helenus.support.Fun;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static User user;
|
||||
static User user;
|
||||
|
||||
static HelenusSession session;
|
||||
static HelenusSession session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(User.class).autoCreateDrop().get();
|
||||
user = Helenus.dsl(User.class, session.getMetadata());
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(User.class).autoCreateDrop().get();
|
||||
user = Helenus.dsl(User.class, session.getMetadata());
|
||||
}
|
||||
|
||||
public static class UserImpl implements User {
|
||||
@Test
|
||||
public void testCruid() throws Exception {
|
||||
|
||||
Long id;
|
||||
String name;
|
||||
Integer age;
|
||||
UserType type;
|
||||
UserImpl newUser = new UserImpl();
|
||||
newUser.id = 100L;
|
||||
newUser.name = "alex";
|
||||
newUser.age = 34;
|
||||
newUser.type = UserType.USER;
|
||||
|
||||
@Override
|
||||
public Long id() {
|
||||
return id;
|
||||
}
|
||||
// CREATE
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
session.upsert(newUser).sync();
|
||||
|
||||
@Override
|
||||
public Integer age() {
|
||||
return age;
|
||||
}
|
||||
// READ
|
||||
|
||||
@Override
|
||||
public UserType type() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
// select row and map to entity
|
||||
|
||||
@Test
|
||||
public void testCruid() throws Exception {
|
||||
User actual = session.selectAll(User.class).mapTo(User.class).where(user::id, eq(100L)).sync().findFirst()
|
||||
.get();
|
||||
assertUsers(newUser, actual);
|
||||
|
||||
UserImpl newUser = new UserImpl();
|
||||
newUser.id = 100L;
|
||||
newUser.name = "alex";
|
||||
newUser.age = 34;
|
||||
newUser.type = UserType.USER;
|
||||
// select as object
|
||||
|
||||
// CREATE
|
||||
actual = session.<User>select(user).where(user::id, eq(100L)).single().sync().orElse(null);
|
||||
assertUsers(newUser, actual);
|
||||
|
||||
session.upsert(newUser).sync();
|
||||
// select by columns
|
||||
|
||||
// READ
|
||||
actual = session.select().column(user::id).column(user::name).column(user::age).column(user::type)
|
||||
.mapTo(User.class).where(user::id, eq(100L)).sync().findFirst().get();
|
||||
assertUsers(newUser, actual);
|
||||
|
||||
// select row and map to entity
|
||||
// select by columns
|
||||
|
||||
User actual =
|
||||
session
|
||||
.selectAll(User.class)
|
||||
.mapTo(User.class)
|
||||
.where(user::id, eq(100L))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get();
|
||||
assertUsers(newUser, actual);
|
||||
actual = session.select(User.class).mapTo(User.class).where(user::id, eq(100L)).sync().findFirst().get();
|
||||
assertUsers(newUser, actual);
|
||||
|
||||
// select as object
|
||||
// select as object and mapTo
|
||||
|
||||
actual = session.<User>select(user).where(user::id, eq(100L)).single().sync().orElse(null);
|
||||
assertUsers(newUser, actual);
|
||||
actual = session.select(user::id, user::name, user::age, user::type).mapTo(User.class).where(user::id, eq(100L))
|
||||
.sync().findFirst().get();
|
||||
assertUsers(newUser, actual);
|
||||
|
||||
// select by columns
|
||||
// select single column
|
||||
|
||||
actual =
|
||||
session
|
||||
.select()
|
||||
.column(user::id)
|
||||
.column(user::name)
|
||||
.column(user::age)
|
||||
.column(user::type)
|
||||
.mapTo(User.class)
|
||||
.where(user::id, eq(100L))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get();
|
||||
assertUsers(newUser, actual);
|
||||
String name = session.select(user::name).where(user::id, eq(100L)).sync().findFirst().get()._1;
|
||||
|
||||
// select by columns
|
||||
Assert.assertEquals(newUser.name(), name);
|
||||
|
||||
actual =
|
||||
session
|
||||
.select(User.class)
|
||||
.mapTo(User.class)
|
||||
.where(user::id, eq(100L))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get();
|
||||
assertUsers(newUser, actual);
|
||||
// select single column in array tuple
|
||||
|
||||
// select as object and mapTo
|
||||
name = (String) session.select().column(user::name).where(user::id, eq(100L)).sync().findFirst().get()._a[0];
|
||||
|
||||
actual =
|
||||
session
|
||||
.select(user::id, user::name, user::age, user::type)
|
||||
.mapTo(User.class)
|
||||
.where(user::id, eq(100L))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get();
|
||||
assertUsers(newUser, actual);
|
||||
Assert.assertEquals(newUser.name(), name);
|
||||
|
||||
// select single column
|
||||
// UPDATE
|
||||
|
||||
String name = session.select(user::name).where(user::id, eq(100L)).sync().findFirst().get()._1;
|
||||
session.update(user::name, "albert").set(user::age, 35).where(user::id, Operator.EQ, 100L).sync();
|
||||
|
||||
Assert.assertEquals(newUser.name(), name);
|
||||
long cnt = session.count(user).where(user::id, Operator.EQ, 100L).sync();
|
||||
Assert.assertEquals(1L, cnt);
|
||||
|
||||
// select single column in array tuple
|
||||
name = session.select(user::name).where(user::id, Operator.EQ, 100L).map(t -> "_" + t._1).sync().findFirst()
|
||||
.get();
|
||||
|
||||
name =
|
||||
(String)
|
||||
session
|
||||
.select()
|
||||
.column(user::name)
|
||||
.where(user::id, eq(100L))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._a[0];
|
||||
Assert.assertEquals("_albert", name);
|
||||
|
||||
Assert.assertEquals(newUser.name(), name);
|
||||
User u2 = session.<User>select(user).where(user::id, eq(100L)).single().sync().orElse(null);
|
||||
|
||||
// UPDATE
|
||||
Assert.assertEquals(Long.valueOf(100L), u2.id());
|
||||
Assert.assertEquals("albert", u2.name());
|
||||
Assert.assertEquals(Integer.valueOf(35), u2.age());
|
||||
|
||||
session
|
||||
.update(user::name, "albert")
|
||||
.set(user::age, 35)
|
||||
.where(user::id, Operator.EQ, 100L)
|
||||
.sync();
|
||||
//
|
||||
User greg = session.<User>insert(user).value(user::name, "greg").value(user::age, 44)
|
||||
.value(user::type, UserType.USER).value(user::id, 1234L).sync();
|
||||
|
||||
long cnt = session.count(user).where(user::id, Operator.EQ, 100L).sync();
|
||||
Assert.assertEquals(1L, cnt);
|
||||
Optional<User> maybeGreg = session.<User>select(user).where(user::id, eq(1234L)).single().sync();
|
||||
|
||||
name =
|
||||
session
|
||||
.select(user::name)
|
||||
.where(user::id, Operator.EQ, 100L)
|
||||
.map(t -> "_" + t._1)
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get();
|
||||
// INSERT
|
||||
|
||||
Assert.assertEquals("_albert", name);
|
||||
session.update().set(user::name, null).set(user::age, null).set(user::type, null).where(user::id, eq(100L))
|
||||
.zipkinContext(null).sync();
|
||||
|
||||
User u2 = session.<User>select(user).where(user::id, eq(100L)).single().sync().orElse(null);
|
||||
Fun.Tuple3<String, Integer, UserType> tuple = session.select(user::name, user::age, user::type)
|
||||
.where(user::id, eq(100L)).sync().findFirst().get();
|
||||
|
||||
Assert.assertEquals(Long.valueOf(100L), u2.id());
|
||||
Assert.assertEquals("albert", u2.name());
|
||||
Assert.assertEquals(Integer.valueOf(35), u2.age());
|
||||
Assert.assertNull(tuple._1);
|
||||
Assert.assertNull(tuple._2);
|
||||
Assert.assertNull(tuple._3);
|
||||
|
||||
//
|
||||
User greg =
|
||||
session
|
||||
.<User>insert(user)
|
||||
.value(user::name, "greg")
|
||||
.value(user::age, 44)
|
||||
.value(user::type, UserType.USER)
|
||||
.value(user::id, 1234L)
|
||||
.sync();
|
||||
// DELETE
|
||||
|
||||
Optional<User> maybeGreg =
|
||||
session.<User>select(user).where(user::id, eq(1234L)).single().sync();
|
||||
session.delete(user).where(user::id, eq(100L)).sync();
|
||||
|
||||
// INSERT
|
||||
cnt = session.select().count().where(user::id, eq(100L)).sync();
|
||||
Assert.assertEquals(0L, cnt);
|
||||
}
|
||||
|
||||
session
|
||||
.update()
|
||||
.set(user::name, null)
|
||||
.set(user::age, null)
|
||||
.set(user::type, null)
|
||||
.where(user::id, eq(100L))
|
||||
.zipkinContext(null)
|
||||
.sync();
|
||||
public void testZipkin() throws TimeoutException {
|
||||
session.update().set(user::name, null).set(user::age, null).set(user::type, null).where(user::id, eq(100L))
|
||||
.zipkinContext(null).sync();
|
||||
|
||||
Fun.Tuple3<String, Integer, UserType> tuple =
|
||||
session
|
||||
.select(user::name, user::age, user::type)
|
||||
.where(user::id, eq(100L))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get();
|
||||
UpdateOperation<ResultSet> update = session.update();
|
||||
update.set(user::name, null).zipkinContext(null).sync();
|
||||
}
|
||||
|
||||
Assert.assertNull(tuple._1);
|
||||
Assert.assertNull(tuple._2);
|
||||
Assert.assertNull(tuple._3);
|
||||
private void assertUsers(User expected, User actual) {
|
||||
Assert.assertEquals(expected.id(), actual.id());
|
||||
Assert.assertEquals(expected.name(), actual.name());
|
||||
Assert.assertEquals(expected.age(), actual.age());
|
||||
Assert.assertEquals(expected.type(), actual.type());
|
||||
}
|
||||
|
||||
// DELETE
|
||||
public static class UserImpl implements User {
|
||||
|
||||
session.delete(user).where(user::id, eq(100L)).sync();
|
||||
Long id;
|
||||
String name;
|
||||
Integer age;
|
||||
UserType type;
|
||||
|
||||
cnt = session.select().count().where(user::id, eq(100L)).sync();
|
||||
Assert.assertEquals(0L, cnt);
|
||||
}
|
||||
@Override
|
||||
public Long id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void testZipkin() throws TimeoutException {
|
||||
session
|
||||
.update()
|
||||
.set(user::name, null)
|
||||
.set(user::age, null)
|
||||
.set(user::type, null)
|
||||
.where(user::id, eq(100L))
|
||||
.zipkinContext(null)
|
||||
.sync();
|
||||
@Override
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
UpdateOperation<ResultSet> update = session.update();
|
||||
update.set(user::name, null).zipkinContext(null).sync();
|
||||
}
|
||||
@Override
|
||||
public Integer age() {
|
||||
return age;
|
||||
}
|
||||
|
||||
private void assertUsers(User expected, User actual) {
|
||||
Assert.assertEquals(expected.id(), actual.id());
|
||||
Assert.assertEquals(expected.name(), actual.name());
|
||||
Assert.assertEquals(expected.age(), actual.age());
|
||||
Assert.assertEquals(expected.type(), actual.type());
|
||||
}
|
||||
@Override
|
||||
public UserType type() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,152 +19,129 @@ import java.util.Date;
|
|||
import java.util.List;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.stream.Collectors;
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.core.Query;
|
||||
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.core.Query;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
|
||||
public class StaticColumnTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static HelenusSession session;
|
||||
static Message message;
|
||||
static HelenusSession session;
|
||||
static Message message;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session =
|
||||
Helenus.init(getSession())
|
||||
.showCql()
|
||||
.addPackage(Message.class.getPackage().getName())
|
||||
.autoCreateDrop()
|
||||
.get();
|
||||
message = Helenus.dsl(Message.class, session.getMetadata());
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().addPackage(Message.class.getPackage().getName()).autoCreateDrop()
|
||||
.get();
|
||||
message = Helenus.dsl(Message.class, session.getMetadata());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(message);
|
||||
}
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(message);
|
||||
}
|
||||
|
||||
private static class MessageImpl implements Message {
|
||||
@Test
|
||||
public void testCRUID() throws TimeoutException {
|
||||
|
||||
int id;
|
||||
Date timestamp;
|
||||
String from;
|
||||
String to;
|
||||
String msg;
|
||||
MessageImpl msg = new MessageImpl();
|
||||
msg.id = 123;
|
||||
msg.timestamp = new Date();
|
||||
msg.from = "Alex";
|
||||
msg.to = "Bob";
|
||||
msg.msg = "hi";
|
||||
|
||||
@Override
|
||||
public int id() {
|
||||
return id;
|
||||
}
|
||||
// CREATE
|
||||
|
||||
@Override
|
||||
public Date timestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
session.insert(msg).sync();
|
||||
|
||||
@Override
|
||||
public String from() {
|
||||
return from;
|
||||
}
|
||||
msg.id = 123;
|
||||
msg.to = "Craig";
|
||||
|
||||
@Override
|
||||
public String to() {
|
||||
return to;
|
||||
}
|
||||
session.insert(msg).sync();
|
||||
|
||||
@Override
|
||||
public String message() {
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
// READ
|
||||
|
||||
@Test
|
||||
public void testCRUID() throws TimeoutException {
|
||||
List<Message> actual = session.<Message>select(message).where(message::id, Query.eq(123)).sync()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
MessageImpl msg = new MessageImpl();
|
||||
msg.id = 123;
|
||||
msg.timestamp = new Date();
|
||||
msg.from = "Alex";
|
||||
msg.to = "Bob";
|
||||
msg.msg = "hi";
|
||||
Assert.assertEquals(2, actual.size());
|
||||
|
||||
// CREATE
|
||||
Message toCraig = actual.stream().filter(m -> m.to().equals("Craig")).findFirst().get();
|
||||
assertMessages(msg, toCraig);
|
||||
|
||||
session.insert(msg).sync();
|
||||
// UPDATE
|
||||
|
||||
msg.id = 123;
|
||||
msg.to = "Craig";
|
||||
session.update().set(message::from, "Albert").where(message::id, Query.eq(123))
|
||||
.onlyIf(message::from, Query.eq("Alex")).sync();
|
||||
|
||||
session.insert(msg).sync();
|
||||
long cnt = session.select(message::from).where(message::id, Query.eq(123)).sync()
|
||||
.filter(t -> t._1.equals("Albert")).count();
|
||||
|
||||
// READ
|
||||
Assert.assertEquals(2, cnt);
|
||||
|
||||
List<Message> actual =
|
||||
session
|
||||
.<Message>select(message)
|
||||
.where(message::id, Query.eq(123))
|
||||
.sync()
|
||||
.collect(Collectors.toList());
|
||||
// INSERT
|
||||
|
||||
Assert.assertEquals(2, actual.size());
|
||||
session.update().set(message::from, null).where(message::id, Query.eq(123)).sync();
|
||||
|
||||
Message toCraig = actual.stream().filter(m -> m.to().equals("Craig")).findFirst().get();
|
||||
assertMessages(msg, toCraig);
|
||||
session.select(message::from).where(message::id, Query.eq(123)).sync().map(t -> t._1)
|
||||
.forEach(Assert::assertNull);
|
||||
|
||||
// UPDATE
|
||||
session.update().set(message::from, "Alex").where(message::id, Query.eq(123))
|
||||
.onlyIf(message::from, Query.eq(null)).sync();
|
||||
|
||||
session
|
||||
.update()
|
||||
.set(message::from, "Albert")
|
||||
.where(message::id, Query.eq(123))
|
||||
.onlyIf(message::from, Query.eq("Alex"))
|
||||
.sync();
|
||||
// DELETE
|
||||
|
||||
long cnt =
|
||||
session
|
||||
.select(message::from)
|
||||
.where(message::id, Query.eq(123))
|
||||
.sync()
|
||||
.filter(t -> t._1.equals("Albert"))
|
||||
.count();
|
||||
session.delete().where(message::id, Query.eq(123)).sync();
|
||||
|
||||
Assert.assertEquals(2, cnt);
|
||||
cnt = session.count().where(message::id, Query.eq(123)).sync();
|
||||
Assert.assertEquals(0, cnt);
|
||||
}
|
||||
|
||||
// INSERT
|
||||
private void assertMessages(Message expected, Message actual) {
|
||||
Assert.assertEquals(expected.id(), actual.id());
|
||||
Assert.assertEquals(expected.from(), actual.from());
|
||||
Assert.assertEquals(expected.timestamp(), actual.timestamp());
|
||||
Assert.assertEquals(expected.to(), actual.to());
|
||||
Assert.assertEquals(expected.message(), actual.message());
|
||||
}
|
||||
|
||||
session.update().set(message::from, null).where(message::id, Query.eq(123)).sync();
|
||||
private static class MessageImpl implements Message {
|
||||
|
||||
session
|
||||
.select(message::from)
|
||||
.where(message::id, Query.eq(123))
|
||||
.sync()
|
||||
.map(t -> t._1)
|
||||
.forEach(Assert::assertNull);
|
||||
int id;
|
||||
Date timestamp;
|
||||
String from;
|
||||
String to;
|
||||
String msg;
|
||||
|
||||
session
|
||||
.update()
|
||||
.set(message::from, "Alex")
|
||||
.where(message::id, Query.eq(123))
|
||||
.onlyIf(message::from, Query.eq(null))
|
||||
.sync();
|
||||
@Override
|
||||
public int id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
// DELETE
|
||||
@Override
|
||||
public Date timestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
session.delete().where(message::id, Query.eq(123)).sync();
|
||||
@Override
|
||||
public String from() {
|
||||
return from;
|
||||
}
|
||||
|
||||
cnt = session.count().where(message::id, Query.eq(123)).sync();
|
||||
Assert.assertEquals(0, cnt);
|
||||
}
|
||||
@Override
|
||||
public String to() {
|
||||
return to;
|
||||
}
|
||||
|
||||
private void assertMessages(Message expected, Message actual) {
|
||||
Assert.assertEquals(expected.id(), actual.id());
|
||||
Assert.assertEquals(expected.from(), actual.from());
|
||||
Assert.assertEquals(expected.timestamp(), actual.timestamp());
|
||||
Assert.assertEquals(expected.to(), actual.to());
|
||||
Assert.assertEquals(expected.message(), actual.message());
|
||||
}
|
||||
@Override
|
||||
public String message() {
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
*/
|
||||
package net.helenus.test.integration.core.simple;
|
||||
|
||||
import net.helenus.core.annotation.Cacheable;
|
||||
import net.helenus.mapping.annotation.Column;
|
||||
import net.helenus.mapping.annotation.PartitionKey;
|
||||
import net.helenus.mapping.annotation.Table;
|
||||
|
@ -23,14 +22,14 @@ import net.helenus.mapping.annotation.Table;
|
|||
@Table("simple_users")
|
||||
public interface User {
|
||||
|
||||
@PartitionKey
|
||||
Long id();
|
||||
@PartitionKey
|
||||
Long id();
|
||||
|
||||
@Username
|
||||
@Column("override_name")
|
||||
String name();
|
||||
@Username
|
||||
@Column("override_name")
|
||||
String name();
|
||||
|
||||
Integer age();
|
||||
Integer age();
|
||||
|
||||
UserType type();
|
||||
UserType type();
|
||||
}
|
||||
|
|
|
@ -16,6 +16,5 @@
|
|||
package net.helenus.test.integration.core.simple;
|
||||
|
||||
public enum UserType {
|
||||
USER,
|
||||
ADMIN;
|
||||
USER, ADMIN;
|
||||
}
|
||||
|
|
|
@ -15,15 +15,13 @@
|
|||
*/
|
||||
package net.helenus.test.integration.core.simple;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.annotation.*;
|
||||
|
||||
import net.helenus.mapping.annotation.Constraints;
|
||||
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE})
|
||||
@Constraints.LowerCase
|
||||
public @interface Username {}
|
||||
public @interface Username {
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ package net.helenus.test.integration.core.tuple;
|
|||
|
||||
import com.datastax.driver.core.DataType;
|
||||
import com.datastax.driver.core.TupleValue;
|
||||
|
||||
import net.helenus.mapping.annotation.Column;
|
||||
import net.helenus.mapping.annotation.PartitionKey;
|
||||
import net.helenus.mapping.annotation.Table;
|
||||
|
@ -25,12 +26,12 @@ import net.helenus.mapping.annotation.Types;
|
|||
@Table
|
||||
public interface Album {
|
||||
|
||||
@PartitionKey(ordinal = 1)
|
||||
int id();
|
||||
@PartitionKey(ordinal = 1)
|
||||
int id();
|
||||
|
||||
AlbumInformation info();
|
||||
AlbumInformation info();
|
||||
|
||||
@Types.Tuple({DataType.Name.TEXT, DataType.Name.TEXT})
|
||||
@Column(ordinal = 1)
|
||||
TupleValue infoNoMapping();
|
||||
@Types.Tuple({DataType.Name.TEXT, DataType.Name.TEXT})
|
||||
@Column(ordinal = 1)
|
||||
TupleValue infoNoMapping();
|
||||
}
|
||||
|
|
|
@ -21,9 +21,9 @@ import net.helenus.mapping.annotation.Tuple;
|
|||
@Tuple
|
||||
public interface AlbumInformation {
|
||||
|
||||
@Column(ordinal = 0)
|
||||
String about();
|
||||
@Column(ordinal = 0)
|
||||
String about();
|
||||
|
||||
@Column(ordinal = 1)
|
||||
String place();
|
||||
@Column(ordinal = 1)
|
||||
String place();
|
||||
}
|
||||
|
|
|
@ -15,20 +15,21 @@
|
|||
*/
|
||||
package net.helenus.test.integration.core.tuple;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
|
||||
public class DslTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
@Test
|
||||
public void testDslBeforeSessionInit() {
|
||||
Assert.assertNotNull(Helenus.dsl(Album.class));
|
||||
}
|
||||
@Test
|
||||
public void testDslBeforeSessionInit() {
|
||||
Assert.assertNotNull(Helenus.dsl(Album.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSessionInitAddingDslProxy() {
|
||||
Assert.assertNotNull(Helenus.init(getSession()).showCql().add(Helenus.dsl(Album.class)));
|
||||
}
|
||||
@Test
|
||||
public void testSessionInitAddingDslProxy() {
|
||||
Assert.assertNotNull(Helenus.init(getSession()).showCql().add(Helenus.dsl(Album.class)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,129 +16,108 @@
|
|||
package net.helenus.test.integration.core.tuple;
|
||||
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.core.Query;
|
||||
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.core.Query;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
|
||||
public class InnerTupleTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static PhotoAlbum photoAlbum;
|
||||
static PhotoAlbum photoAlbum;
|
||||
|
||||
static HelenusSession session;
|
||||
static HelenusSession session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(PhotoAlbum.class).autoCreateDrop().get();
|
||||
photoAlbum = Helenus.dsl(PhotoAlbum.class, session.getMetadata());
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(PhotoAlbum.class).autoCreateDrop().get();
|
||||
photoAlbum = Helenus.dsl(PhotoAlbum.class, session.getMetadata());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(photoAlbum);
|
||||
}
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(photoAlbum);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCruid() throws TimeoutException {
|
||||
@Test
|
||||
public void testCruid() throws TimeoutException {
|
||||
|
||||
Photo photo =
|
||||
new Photo() {
|
||||
Photo photo = new Photo() {
|
||||
|
||||
@Override
|
||||
public byte[] blob() {
|
||||
return "jpeg".getBytes();
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public byte[] blob() {
|
||||
return "jpeg".getBytes();
|
||||
}
|
||||
};
|
||||
|
||||
PhotoFolder folder =
|
||||
new PhotoFolder() {
|
||||
PhotoFolder folder = new PhotoFolder() {
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "first";
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return "first";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Photo photo() {
|
||||
return photo;
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public Photo photo() {
|
||||
return photo;
|
||||
}
|
||||
};
|
||||
|
||||
// CREATE (C)
|
||||
// CREATE (C)
|
||||
|
||||
session.insert().value(photoAlbum::id, 123).value(photoAlbum::folder, folder).sync();
|
||||
session.insert().value(photoAlbum::id, 123).value(photoAlbum::folder, folder).sync();
|
||||
|
||||
// READ (R)
|
||||
// READ (R)
|
||||
|
||||
PhotoFolder actual =
|
||||
session
|
||||
.select(photoAlbum::folder)
|
||||
.where(photoAlbum::id, Query.eq(123))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
PhotoFolder actual = session.select(photoAlbum::folder).where(photoAlbum::id, Query.eq(123)).sync().findFirst()
|
||||
.get()._1;
|
||||
|
||||
Assert.assertEquals(folder.name(), actual.name());
|
||||
Assert.assertEquals(folder.name(), actual.name());
|
||||
|
||||
// UPDATE (U)
|
||||
// UPDATE (U)
|
||||
|
||||
// unfortunately this is not working right now in Cassandra, can not update a single column in tuple :(
|
||||
//session.update()
|
||||
// .set(photoAlbum.folder().photo()::blob, "Helenus".getBytes())
|
||||
// .where(photoAlbum::id, eq(123))
|
||||
// .sync();
|
||||
// unfortunately this is not working right now in Cassandra, can not update a
|
||||
// single column in tuple :(
|
||||
// session.update()
|
||||
// .set(photoAlbum.folder().photo()::blob, "Helenus".getBytes())
|
||||
// .where(photoAlbum::id, eq(123))
|
||||
// .sync();
|
||||
|
||||
PhotoFolder expected =
|
||||
new PhotoFolder() {
|
||||
PhotoFolder expected = new PhotoFolder() {
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "seconds";
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return "seconds";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Photo photo() {
|
||||
return photo;
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public Photo photo() {
|
||||
return photo;
|
||||
}
|
||||
};
|
||||
|
||||
session.update().set(photoAlbum::folder, expected).where(photoAlbum::id, Query.eq(123)).sync();
|
||||
session.update().set(photoAlbum::folder, expected).where(photoAlbum::id, Query.eq(123)).sync();
|
||||
|
||||
actual =
|
||||
session
|
||||
.select(photoAlbum::folder)
|
||||
.where(photoAlbum::id, Query.eq(123))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
actual = session.select(photoAlbum::folder).where(photoAlbum::id, Query.eq(123)).sync().findFirst().get()._1;
|
||||
|
||||
Assert.assertEquals(expected.name(), actual.name());
|
||||
Assert.assertEquals(expected.name(), actual.name());
|
||||
|
||||
// INSERT (I)
|
||||
// let's insert null ;)
|
||||
// INSERT (I)
|
||||
// let's insert null ;)
|
||||
|
||||
session.update().set(photoAlbum::folder, null).where(photoAlbum::id, Query.eq(123)).sync();
|
||||
session.update().set(photoAlbum::folder, null).where(photoAlbum::id, Query.eq(123)).sync();
|
||||
|
||||
actual =
|
||||
session
|
||||
.select(photoAlbum::folder)
|
||||
.where(photoAlbum::id, Query.eq(123))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
Assert.assertNull(actual);
|
||||
actual = session.select(photoAlbum::folder).where(photoAlbum::id, Query.eq(123)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actual);
|
||||
|
||||
// DELETE (D)
|
||||
session.delete().where(photoAlbum::id, Query.eq(123)).sync();
|
||||
// DELETE (D)
|
||||
session.delete().where(photoAlbum::id, Query.eq(123)).sync();
|
||||
|
||||
long cnt =
|
||||
session.select(photoAlbum::folder).where(photoAlbum::id, Query.eq(123)).sync().count();
|
||||
Assert.assertEquals(0, cnt);
|
||||
}
|
||||
long cnt = session.select(photoAlbum::folder).where(photoAlbum::id, Query.eq(123)).sync().count();
|
||||
Assert.assertEquals(0, cnt);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,5 +20,5 @@ import net.helenus.mapping.annotation.Tuple;
|
|||
@Tuple
|
||||
public interface Photo {
|
||||
|
||||
byte[] blob();
|
||||
byte[] blob();
|
||||
}
|
||||
|
|
|
@ -21,8 +21,8 @@ import net.helenus.mapping.annotation.Table;
|
|||
@Table
|
||||
public interface PhotoAlbum {
|
||||
|
||||
@PartitionKey
|
||||
int id();
|
||||
@PartitionKey
|
||||
int id();
|
||||
|
||||
PhotoFolder folder();
|
||||
PhotoFolder folder();
|
||||
}
|
||||
|
|
|
@ -21,9 +21,9 @@ import net.helenus.mapping.annotation.Tuple;
|
|||
@Tuple
|
||||
public interface PhotoFolder {
|
||||
|
||||
@Column(ordinal = 0)
|
||||
String name();
|
||||
@Column(ordinal = 0)
|
||||
String name();
|
||||
|
||||
@Column(ordinal = 1)
|
||||
Photo photo();
|
||||
@Column(ordinal = 1)
|
||||
Photo photo();
|
||||
}
|
||||
|
|
|
@ -17,157 +17,155 @@ package net.helenus.test.integration.core.tuple;
|
|||
|
||||
import static net.helenus.core.Query.eq;
|
||||
|
||||
import com.datastax.driver.core.DataType;
|
||||
import com.datastax.driver.core.TupleType;
|
||||
import com.datastax.driver.core.TupleValue;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
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 com.datastax.driver.core.DataType;
|
||||
import com.datastax.driver.core.TupleType;
|
||||
import com.datastax.driver.core.TupleValue;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
|
||||
public class TupleTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static Album album;
|
||||
static Album album;
|
||||
|
||||
static HelenusSession session;
|
||||
static HelenusSession session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
Helenus.clearDslCache();
|
||||
session = Helenus.init(getSession()).showCql().add(Album.class).autoCreateDrop().get();
|
||||
album = Helenus.dsl(Album.class, session.getMetadata());
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
Helenus.clearDslCache();
|
||||
session = Helenus.init(getSession()).showCql().add(Album.class).autoCreateDrop().get();
|
||||
album = Helenus.dsl(Album.class, session.getMetadata());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(album);
|
||||
}
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(album);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCruid() throws TimeoutException {
|
||||
@Test
|
||||
public void testCruid() throws TimeoutException {
|
||||
|
||||
AlbumInformation info =
|
||||
new AlbumInformation() {
|
||||
AlbumInformation info = new AlbumInformation() {
|
||||
|
||||
@Override
|
||||
public String about() {
|
||||
return "Cassandra";
|
||||
}
|
||||
@Override
|
||||
public String about() {
|
||||
return "Cassandra";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String place() {
|
||||
return "San Jose";
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public String place() {
|
||||
return "San Jose";
|
||||
}
|
||||
};
|
||||
|
||||
// CREATE (C)
|
||||
// CREATE (C)
|
||||
|
||||
session.insert().value(album::id, 123).value(album::info, info).sync();
|
||||
session.insert().value(album::id, 123).value(album::info, info).sync();
|
||||
|
||||
// READ (R)
|
||||
// READ (R)
|
||||
|
||||
AlbumInformation actual =
|
||||
session.select(album::info).where(album::id, eq(123)).sync().findFirst().get()._1;
|
||||
AlbumInformation actual = session.select(album::info).where(album::id, eq(123)).sync().findFirst().get()._1;
|
||||
|
||||
Assert.assertEquals(info.about(), actual.about());
|
||||
Assert.assertEquals(info.place(), actual.place());
|
||||
Assert.assertEquals(info.about(), actual.about());
|
||||
Assert.assertEquals(info.place(), actual.place());
|
||||
|
||||
// UPDATE (U)
|
||||
// UPDATE (U)
|
||||
|
||||
// unfortunately this is not working right now in Cassandra, can not update a single column in tuple :(
|
||||
//session.update()
|
||||
// .set(album.info()::about, "Helenus")
|
||||
// .where(album::id, eq(123))
|
||||
// .sync();
|
||||
// unfortunately this is not working right now in Cassandra, can not update a
|
||||
// single column in tuple :(
|
||||
// session.update()
|
||||
// .set(album.info()::about, "Helenus")
|
||||
// .where(album::id, eq(123))
|
||||
// .sync();
|
||||
|
||||
AlbumInformation expected =
|
||||
new AlbumInformation() {
|
||||
AlbumInformation expected = new AlbumInformation() {
|
||||
|
||||
@Override
|
||||
public String about() {
|
||||
return "Helenus";
|
||||
}
|
||||
@Override
|
||||
public String about() {
|
||||
return "Helenus";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String place() {
|
||||
return "Santa Cruz";
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public String place() {
|
||||
return "Santa Cruz";
|
||||
}
|
||||
};
|
||||
|
||||
session.update().set(album::info, expected).where(album::id, eq(123)).sync();
|
||||
session.update().set(album::info, expected).where(album::id, eq(123)).sync();
|
||||
|
||||
actual = session.select(album::info).where(album::id, eq(123)).sync().findFirst().get()._1;
|
||||
actual = session.select(album::info).where(album::id, eq(123)).sync().findFirst().get()._1;
|
||||
|
||||
Assert.assertEquals(expected.about(), actual.about());
|
||||
Assert.assertEquals(expected.place(), actual.place());
|
||||
Assert.assertEquals(expected.about(), actual.about());
|
||||
Assert.assertEquals(expected.place(), actual.place());
|
||||
|
||||
// INSERT (I)
|
||||
// let's insert null ;)
|
||||
// INSERT (I)
|
||||
// let's insert null ;)
|
||||
|
||||
session.update().set(album::info, null).where(album::id, eq(123)).sync();
|
||||
session.update().set(album::info, null).where(album::id, eq(123)).sync();
|
||||
|
||||
actual = session.select(album::info).where(album::id, eq(123)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actual);
|
||||
actual = session.select(album::info).where(album::id, eq(123)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actual);
|
||||
|
||||
// DELETE (D)
|
||||
session.delete().where(album::id, eq(123)).sync();
|
||||
// DELETE (D)
|
||||
session.delete().where(album::id, eq(123)).sync();
|
||||
|
||||
long cnt = session.select(album::info).where(album::id, eq(123)).sync().count();
|
||||
Assert.assertEquals(0, cnt);
|
||||
}
|
||||
long cnt = session.select(album::info).where(album::id, eq(123)).sync().count();
|
||||
Assert.assertEquals(0, cnt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoMapping() throws TimeoutException {
|
||||
@Test
|
||||
public void testNoMapping() throws TimeoutException {
|
||||
|
||||
TupleType tupleType = session.getMetadata().newTupleType(DataType.text(), DataType.text());
|
||||
TupleValue info = tupleType.newValue();
|
||||
TupleType tupleType = session.getMetadata().newTupleType(DataType.text(), DataType.text());
|
||||
TupleValue info = tupleType.newValue();
|
||||
|
||||
info.setString(0, "Cassandra");
|
||||
info.setString(1, "San Jose");
|
||||
info.setString(0, "Cassandra");
|
||||
info.setString(1, "San Jose");
|
||||
|
||||
// CREATE (C)
|
||||
// CREATE (C)
|
||||
|
||||
session.insert().value(album::id, 555).value(album::infoNoMapping, info).sync();
|
||||
session.insert().value(album::id, 555).value(album::infoNoMapping, info).sync();
|
||||
|
||||
// READ (R)
|
||||
// READ (R)
|
||||
|
||||
TupleValue actual =
|
||||
session.select(album::infoNoMapping).where(album::id, eq(555)).sync().findFirst().get()._1;
|
||||
TupleValue actual = session.select(album::infoNoMapping).where(album::id, eq(555)).sync().findFirst().get()._1;
|
||||
|
||||
Assert.assertEquals(info.getString(0), actual.getString(0));
|
||||
Assert.assertEquals(info.getString(1), actual.getString(1));
|
||||
Assert.assertEquals(info.getString(0), actual.getString(0));
|
||||
Assert.assertEquals(info.getString(1), actual.getString(1));
|
||||
|
||||
// UPDATE (U)
|
||||
// UPDATE (U)
|
||||
|
||||
TupleValue expected = tupleType.newValue();
|
||||
TupleValue expected = tupleType.newValue();
|
||||
|
||||
expected.setString(0, "Helenus");
|
||||
expected.setString(1, "Los Altos");
|
||||
expected.setString(0, "Helenus");
|
||||
expected.setString(1, "Los Altos");
|
||||
|
||||
session.update().set(album::infoNoMapping, expected).where(album::id, eq(555)).sync();
|
||||
session.update().set(album::infoNoMapping, expected).where(album::id, eq(555)).sync();
|
||||
|
||||
actual =
|
||||
session.select(album::infoNoMapping).where(album::id, eq(555)).sync().findFirst().get()._1;
|
||||
actual = session.select(album::infoNoMapping).where(album::id, eq(555)).sync().findFirst().get()._1;
|
||||
|
||||
Assert.assertEquals(expected.getString(0), actual.getString(0));
|
||||
Assert.assertEquals(expected.getString(1), actual.getString(1));
|
||||
Assert.assertEquals(expected.getString(0), actual.getString(0));
|
||||
Assert.assertEquals(expected.getString(1), actual.getString(1));
|
||||
|
||||
// INSERT (I)
|
||||
// let's insert null ;)
|
||||
// INSERT (I)
|
||||
// let's insert null ;)
|
||||
|
||||
session.update().set(album::infoNoMapping, null).where(album::id, eq(555)).sync();
|
||||
session.update().set(album::infoNoMapping, null).where(album::id, eq(555)).sync();
|
||||
|
||||
actual =
|
||||
session.select(album::infoNoMapping).where(album::id, eq(555)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actual);
|
||||
actual = session.select(album::infoNoMapping).where(album::id, eq(555)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actual);
|
||||
|
||||
// DELETE (D)
|
||||
session.delete().where(album::id, eq(555)).sync();
|
||||
// DELETE (D)
|
||||
session.delete().where(album::id, eq(555)).sync();
|
||||
|
||||
long cnt = session.select(album::infoNoMapping).where(album::id, eq(555)).sync().count();
|
||||
Assert.assertEquals(0, cnt);
|
||||
}
|
||||
long cnt = session.select(album::infoNoMapping).where(album::id, eq(555)).sync().count();
|
||||
Assert.assertEquals(0, cnt);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,9 +21,9 @@ import net.helenus.mapping.annotation.Tuple;
|
|||
@Tuple
|
||||
public interface Author {
|
||||
|
||||
@Column(ordinal = 0)
|
||||
String name();
|
||||
@Column(ordinal = 0)
|
||||
String name();
|
||||
|
||||
@Column(ordinal = 1)
|
||||
String city();
|
||||
@Column(ordinal = 1)
|
||||
String city();
|
||||
}
|
||||
|
|
|
@ -18,22 +18,23 @@ package net.helenus.test.integration.core.tuplecollection;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import net.helenus.mapping.annotation.PartitionKey;
|
||||
import net.helenus.mapping.annotation.Table;
|
||||
|
||||
@Table
|
||||
public interface Book {
|
||||
|
||||
@PartitionKey
|
||||
int id();
|
||||
@PartitionKey
|
||||
int id();
|
||||
|
||||
List<Author> authors();
|
||||
List<Author> authors();
|
||||
|
||||
Set<Author> reviewers();
|
||||
Set<Author> reviewers();
|
||||
|
||||
Map<Integer, Section> contents();
|
||||
Map<Integer, Section> contents();
|
||||
|
||||
Map<Section, String> notes();
|
||||
Map<Section, String> notes();
|
||||
|
||||
Map<Section, Author> writers();
|
||||
Map<Section, Author> writers();
|
||||
}
|
||||
|
|
|
@ -21,9 +21,9 @@ import net.helenus.mapping.annotation.Tuple;
|
|||
@Tuple
|
||||
public interface Section {
|
||||
|
||||
@Column(ordinal = 0)
|
||||
String title();
|
||||
@Column(ordinal = 0)
|
||||
String title();
|
||||
|
||||
@Column(ordinal = 1)
|
||||
int page();
|
||||
@Column(ordinal = 1)
|
||||
int page();
|
||||
}
|
||||
|
|
|
@ -15,124 +15,138 @@
|
|||
*/
|
||||
package net.helenus.test.integration.core.tuplecollection;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
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;
|
||||
|
||||
public abstract class TupleCollectionTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static Book book;
|
||||
static Book book;
|
||||
|
||||
static HelenusSession session;
|
||||
static HelenusSession session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Book.class).autoCreateDrop().get();
|
||||
book = Helenus.dsl(Book.class, session.getMetadata());
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Book.class).autoCreateDrop().get();
|
||||
book = Helenus.dsl(Book.class, session.getMetadata());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
System.out.println(book);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
System.out.println(book);
|
||||
}
|
||||
|
||||
public static final class AuthorImpl implements Author {
|
||||
public static final class AuthorImpl implements Author {
|
||||
|
||||
String name;
|
||||
String city;
|
||||
String name;
|
||||
String city;
|
||||
|
||||
AuthorImpl(String name, String city) {
|
||||
this.name = name;
|
||||
this.city = city;
|
||||
}
|
||||
AuthorImpl(String name, String city) {
|
||||
this.name = name;
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String city() {
|
||||
return city;
|
||||
}
|
||||
@Override
|
||||
public String city() {
|
||||
return city;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((city == null) ? 0 : city.hashCode());
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((city == null) ? 0 : city.hashCode());
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null) return false;
|
||||
if (getClass() != obj.getClass()) return false;
|
||||
AuthorImpl other = (AuthorImpl) obj;
|
||||
if (city == null) {
|
||||
if (other.city != null) return false;
|
||||
} else if (!city.equals(other.city)) return false;
|
||||
if (name == null) {
|
||||
if (other.name != null) return false;
|
||||
} else if (!name.equals(other.name)) return false;
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
AuthorImpl other = (AuthorImpl) obj;
|
||||
if (city == null) {
|
||||
if (other.city != null)
|
||||
return false;
|
||||
} else if (!city.equals(other.city))
|
||||
return false;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AuthorImpl [name=" + name + ", city=" + city + "]";
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AuthorImpl [name=" + name + ", city=" + city + "]";
|
||||
}
|
||||
}
|
||||
|
||||
public static final class SectionImpl implements Section {
|
||||
public static final class SectionImpl implements Section {
|
||||
|
||||
String title;
|
||||
int page;
|
||||
String title;
|
||||
int page;
|
||||
|
||||
SectionImpl(String title, int page) {
|
||||
this.title = title;
|
||||
this.page = page;
|
||||
}
|
||||
SectionImpl(String title, int page) {
|
||||
this.title = title;
|
||||
this.page = page;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String title() {
|
||||
return title;
|
||||
}
|
||||
@Override
|
||||
public String title() {
|
||||
return title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int page() {
|
||||
return page;
|
||||
}
|
||||
@Override
|
||||
public int page() {
|
||||
return page;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + page;
|
||||
result = prime * result + ((title == null) ? 0 : title.hashCode());
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + page;
|
||||
result = prime * result + ((title == null) ? 0 : title.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null) return false;
|
||||
if (getClass() != obj.getClass()) return false;
|
||||
SectionImpl other = (SectionImpl) obj;
|
||||
if (page != other.page) return false;
|
||||
if (title == null) {
|
||||
if (other.title != null) return false;
|
||||
} else if (!title.equals(other.title)) return false;
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
SectionImpl other = (SectionImpl) obj;
|
||||
if (page != other.page)
|
||||
return false;
|
||||
if (title == null) {
|
||||
if (other.title != null)
|
||||
return false;
|
||||
} else if (!title.equals(other.title))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SectionImpl [title=" + title + ", page=" + page + "]";
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SectionImpl [title=" + title + ", page=" + page + "]";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,130 +18,123 @@ package net.helenus.test.integration.core.tuplecollection;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import net.helenus.core.Query;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Query;
|
||||
|
||||
public class TupleKeyMapTest extends TupleCollectionTest {
|
||||
|
||||
@Test
|
||||
public void testKeyMapCRUID() throws TimeoutException {
|
||||
@Test
|
||||
public void testKeyMapCRUID() throws TimeoutException {
|
||||
|
||||
int id = 888;
|
||||
int id = 888;
|
||||
|
||||
Map<Section, String> notes = new HashMap<Section, String>();
|
||||
notes.put(new SectionImpl("first", 1), "value1");
|
||||
notes.put(new SectionImpl("second", 2), "value2");
|
||||
Map<Section, String> notes = new HashMap<Section, String>();
|
||||
notes.put(new SectionImpl("first", 1), "value1");
|
||||
notes.put(new SectionImpl("second", 2), "value2");
|
||||
|
||||
// CREATE
|
||||
// CREATE
|
||||
|
||||
session.insert().value(book::id, id).value(book::notes, notes).sync();
|
||||
session.insert().value(book::id, id).value(book::notes, notes).sync();
|
||||
|
||||
// READ
|
||||
// READ
|
||||
|
||||
// read full object
|
||||
// read full object
|
||||
|
||||
Book actual = session.<Book>select(book).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(notes, actual.notes());
|
||||
Assert.assertNull(actual.reviewers());
|
||||
Assert.assertNull(actual.writers());
|
||||
Assert.assertNull(actual.contents());
|
||||
Book actual = session.<Book>select(book).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(notes, actual.notes());
|
||||
Assert.assertNull(actual.reviewers());
|
||||
Assert.assertNull(actual.writers());
|
||||
Assert.assertNull(actual.contents());
|
||||
|
||||
// read full map
|
||||
// read full map
|
||||
|
||||
Map<Section, String> actualMap =
|
||||
session.select(book::notes).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(notes, actualMap);
|
||||
Map<Section, String> actualMap = session.select(book::notes).where(book::id, Query.eq(id)).sync().findFirst()
|
||||
.get()._1;
|
||||
assertEqualMaps(notes, actualMap);
|
||||
|
||||
// read single key-value in map
|
||||
// read single key-value in map
|
||||
|
||||
String cql =
|
||||
session
|
||||
.select(Query.get(book::notes, new SectionImpl("first", 1)))
|
||||
.where(book::id, Query.eq(id))
|
||||
.cql();
|
||||
String cql = session.select(Query.get(book::notes, new SectionImpl("first", 1))).where(book::id, Query.eq(id))
|
||||
.cql();
|
||||
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
|
||||
// UPDATE
|
||||
// UPDATE
|
||||
|
||||
Map<Section, String> expected = new HashMap<Section, String>();
|
||||
expected.put(new SectionImpl("f", 1), "v1");
|
||||
expected.put(new SectionImpl("s", 1), "v2");
|
||||
Map<Section, String> expected = new HashMap<Section, String>();
|
||||
expected.put(new SectionImpl("f", 1), "v1");
|
||||
expected.put(new SectionImpl("s", 1), "v2");
|
||||
|
||||
session.update().set(book::notes, expected).where(book::id, Query.eq(id)).sync();
|
||||
session.update().set(book::notes, expected).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actual = session.<Book>select(book).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(expected, actual.notes());
|
||||
actual = session.<Book>select(book).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(expected, actual.notes());
|
||||
|
||||
// INSERT
|
||||
// INSERT
|
||||
|
||||
// put operation
|
||||
// put operation
|
||||
|
||||
Section third = new SectionImpl("t", 3);
|
||||
Section third = new SectionImpl("t", 3);
|
||||
|
||||
expected.put(third, "v3");
|
||||
session.update().put(book::notes, third, "v3").where(book::id, Query.eq(id)).sync();
|
||||
expected.put(third, "v3");
|
||||
session.update().put(book::notes, third, "v3").where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::notes).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::notes).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// putAll operation
|
||||
expected.putAll(notes);
|
||||
session.update().putAll(book::notes, notes).where(book::id, Query.eq(id)).sync();
|
||||
// putAll operation
|
||||
expected.putAll(notes);
|
||||
session.update().putAll(book::notes, notes).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::notes).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::notes).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// put existing
|
||||
// put existing
|
||||
|
||||
expected.put(third, "v33");
|
||||
session.update().put(book::notes, third, "v33").where(book::id, Query.eq(id)).sync();
|
||||
expected.put(third, "v33");
|
||||
session.update().put(book::notes, third, "v33").where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::notes).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::notes).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// DELETE
|
||||
// DELETE
|
||||
|
||||
// remove single key
|
||||
// remove single key
|
||||
|
||||
expected.remove(third);
|
||||
session.update().put(book::notes, third, null).where(book::id, Query.eq(id)).sync();
|
||||
expected.remove(third);
|
||||
session.update().put(book::notes, third, null).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::notes).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::notes).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// remove full map
|
||||
// remove full map
|
||||
|
||||
session.update().set(book::notes, null).where(book::id, Query.eq(id)).sync();
|
||||
session.update().set(book::notes, null).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::notes).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualMap);
|
||||
actualMap = session.select(book::notes).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualMap);
|
||||
|
||||
// remove object
|
||||
// remove object
|
||||
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
|
||||
private void assertEqualMaps(Map<Section, String> expected, Map<Section, String> actual) {
|
||||
private void assertEqualMaps(Map<Section, String> expected, Map<Section, String> actual) {
|
||||
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
|
||||
for (Section e : expected.keySet()) {
|
||||
Section a =
|
||||
actual.keySet().stream().filter(p -> p.title().equals(e.title())).findFirst().get();
|
||||
Assert.assertEquals(e.title(), a.title());
|
||||
Assert.assertEquals(e.page(), a.page());
|
||||
Assert.assertEquals(expected.get(e), actual.get(a));
|
||||
}
|
||||
}
|
||||
for (Section e : expected.keySet()) {
|
||||
Section a = actual.keySet().stream().filter(p -> p.title().equals(e.title())).findFirst().get();
|
||||
Assert.assertEquals(e.title(), a.title());
|
||||
Assert.assertEquals(e.page(), a.page());
|
||||
Assert.assertEquals(expected.get(e), actual.get(a));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,157 +18,145 @@ package net.helenus.test.integration.core.tuplecollection;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import net.helenus.core.Query;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Query;
|
||||
|
||||
public class TupleListTest extends TupleCollectionTest {
|
||||
|
||||
@Test
|
||||
public void testListCRUID() throws TimeoutException {
|
||||
@Test
|
||||
public void testListCRUID() throws TimeoutException {
|
||||
|
||||
int id = 777;
|
||||
int id = 777;
|
||||
|
||||
List<Author> authors = new ArrayList<Author>();
|
||||
authors.add(new AuthorImpl("Alex", "San Jose"));
|
||||
authors.add(new AuthorImpl("Bob", "San Francisco"));
|
||||
List<Author> authors = new ArrayList<Author>();
|
||||
authors.add(new AuthorImpl("Alex", "San Jose"));
|
||||
authors.add(new AuthorImpl("Bob", "San Francisco"));
|
||||
|
||||
// CREATE
|
||||
// CREATE
|
||||
|
||||
session.insert().value(book::id, id).value(book::authors, authors).sync();
|
||||
session.insert().value(book::id, id).value(book::authors, authors).sync();
|
||||
|
||||
// READ
|
||||
// READ
|
||||
|
||||
// read full object
|
||||
// read full object
|
||||
|
||||
Book actual = session.select(Book.class).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualLists(authors, actual.authors());
|
||||
Assert.assertNull(actual.reviewers());
|
||||
Assert.assertNull(actual.contents());
|
||||
Book actual = session.select(Book.class).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualLists(authors, actual.authors());
|
||||
Assert.assertNull(actual.reviewers());
|
||||
Assert.assertNull(actual.contents());
|
||||
|
||||
// read full list
|
||||
// read full list
|
||||
|
||||
List<Author> actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(authors, actualList);
|
||||
List<Author> actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst()
|
||||
.get()._1;
|
||||
assertEqualLists(authors, actualList);
|
||||
|
||||
// read single value by index
|
||||
// read single value by index
|
||||
|
||||
String cql = session.select(Query.getIdx(book::authors, 1)).where(book::id, Query.eq(id)).cql();
|
||||
String cql = session.select(Query.getIdx(book::authors, 1)).where(book::id, Query.eq(id)).cql();
|
||||
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
|
||||
// UPDATE
|
||||
// UPDATE
|
||||
|
||||
List<Author> expected = new ArrayList<Author>();
|
||||
expected.add(new AuthorImpl("Unknown", "City 17"));
|
||||
List<Author> expected = new ArrayList<Author>();
|
||||
expected.add(new AuthorImpl("Unknown", "City 17"));
|
||||
|
||||
session.update().set(book::authors, expected).where(book::id, Query.eq(id)).sync();
|
||||
session.update().set(book::authors, expected).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actual = session.select(Book.class).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualLists(expected, actual.authors());
|
||||
actual = session.select(Book.class).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualLists(expected, actual.authors());
|
||||
|
||||
// INSERT
|
||||
// INSERT
|
||||
|
||||
// prepend operation
|
||||
// prepend operation
|
||||
|
||||
expected.add(0, new AuthorImpl("Prepend", "PrependCity"));
|
||||
session
|
||||
.update()
|
||||
.prepend(book::authors, new AuthorImpl("Prepend", "PrependCity"))
|
||||
.where(book::id, Query.eq(id))
|
||||
.sync();
|
||||
expected.add(0, new AuthorImpl("Prepend", "PrependCity"));
|
||||
session.update().prepend(book::authors, new AuthorImpl("Prepend", "PrependCity")).where(book::id, Query.eq(id))
|
||||
.sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
|
||||
// append operation
|
||||
// append operation
|
||||
|
||||
expected.add(new AuthorImpl("Append", "AppendCity"));
|
||||
session
|
||||
.update()
|
||||
.append(book::authors, new AuthorImpl("Append", "AppendCity"))
|
||||
.where(book::id, Query.eq(id))
|
||||
.sync();
|
||||
expected.add(new AuthorImpl("Append", "AppendCity"));
|
||||
session.update().append(book::authors, new AuthorImpl("Append", "AppendCity")).where(book::id, Query.eq(id))
|
||||
.sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
|
||||
// prependAll operation
|
||||
expected.addAll(0, authors);
|
||||
session.update().prependAll(book::authors, authors).where(book::id, Query.eq(id)).sync();
|
||||
// prependAll operation
|
||||
expected.addAll(0, authors);
|
||||
session.update().prependAll(book::authors, authors).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
|
||||
// appendAll operation
|
||||
expected.addAll(authors);
|
||||
session.update().appendAll(book::authors, authors).where(book::id, Query.eq(id)).sync();
|
||||
// appendAll operation
|
||||
expected.addAll(authors);
|
||||
session.update().appendAll(book::authors, authors).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
|
||||
// set by Index
|
||||
// set by Index
|
||||
|
||||
Author inserted = new AuthorImpl("Insert", "InsertCity");
|
||||
expected.set(5, inserted);
|
||||
session.update().setIdx(book::authors, 5, inserted).where(book::id, Query.eq(id)).sync();
|
||||
Author inserted = new AuthorImpl("Insert", "InsertCity");
|
||||
expected.set(5, inserted);
|
||||
session.update().setIdx(book::authors, 5, inserted).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
|
||||
// DELETE
|
||||
// DELETE
|
||||
|
||||
// remove single value
|
||||
// remove single value
|
||||
|
||||
expected.remove(inserted);
|
||||
session.update().discard(book::authors, inserted).where(book::id, Query.eq(id)).sync();
|
||||
expected.remove(inserted);
|
||||
session.update().discard(book::authors, inserted).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
|
||||
// remove values
|
||||
// remove values
|
||||
|
||||
expected.removeAll(authors);
|
||||
session.update().discardAll(book::authors, authors).where(book::id, Query.eq(id)).sync();
|
||||
expected.removeAll(authors);
|
||||
session.update().discardAll(book::authors, authors).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
|
||||
// remove full list
|
||||
// remove full list
|
||||
|
||||
session.update().set(book::authors, null).where(book::id, Query.eq(id)).sync();
|
||||
session.update().set(book::authors, null).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualList);
|
||||
|
||||
// remove object
|
||||
// remove object
|
||||
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
|
||||
private void assertEqualLists(List<Author> expected, List<Author> actual) {
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
private void assertEqualLists(List<Author> expected, List<Author> actual) {
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
|
||||
int size = expected.size();
|
||||
int size = expected.size();
|
||||
|
||||
for (int i = 0; i != size; ++i) {
|
||||
Author e = expected.get(i);
|
||||
Author a = actual.get(i);
|
||||
Assert.assertEquals(e.name(), a.name());
|
||||
Assert.assertEquals(e.city(), a.city());
|
||||
}
|
||||
}
|
||||
for (int i = 0; i != size; ++i) {
|
||||
Author e = expected.get(i);
|
||||
Author a = actual.get(i);
|
||||
Assert.assertEquals(e.name(), a.name());
|
||||
Assert.assertEquals(e.city(), a.city());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,140 +18,130 @@ package net.helenus.test.integration.core.tuplecollection;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import net.helenus.core.Query;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Query;
|
||||
|
||||
public class TupleMapTest extends TupleCollectionTest {
|
||||
|
||||
@Test
|
||||
public void testMapCRUID() throws TimeoutException {
|
||||
@Test
|
||||
public void testMapCRUID() throws TimeoutException {
|
||||
|
||||
int id = 333;
|
||||
int id = 333;
|
||||
|
||||
Map<Section, Author> writers = new HashMap<Section, Author>();
|
||||
writers.put(
|
||||
new SectionImpl("first", 1), new TupleCollectionTest.AuthorImpl("Alex", "San Jose"));
|
||||
writers.put(
|
||||
new SectionImpl("second", 2), new TupleCollectionTest.AuthorImpl("Bob", "San Francisco"));
|
||||
Map<Section, Author> writers = new HashMap<Section, Author>();
|
||||
writers.put(new SectionImpl("first", 1), new TupleCollectionTest.AuthorImpl("Alex", "San Jose"));
|
||||
writers.put(new SectionImpl("second", 2), new TupleCollectionTest.AuthorImpl("Bob", "San Francisco"));
|
||||
|
||||
// CREATE
|
||||
// CREATE
|
||||
|
||||
session.insert().value(book::id, id).value(book::writers, writers).sync();
|
||||
session.insert().value(book::id, id).value(book::writers, writers).sync();
|
||||
|
||||
// READ
|
||||
// READ
|
||||
|
||||
// read full object
|
||||
// read full object
|
||||
|
||||
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());
|
||||
Assert.assertNull(actual.notes());
|
||||
Assert.assertNull(actual.contents());
|
||||
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());
|
||||
Assert.assertNull(actual.notes());
|
||||
Assert.assertNull(actual.contents());
|
||||
|
||||
// read full map
|
||||
// read full map
|
||||
|
||||
Map<Section, Author> actualMap =
|
||||
session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(writers, actualMap);
|
||||
Map<Section, Author> actualMap = session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst()
|
||||
.get()._1;
|
||||
assertEqualMaps(writers, actualMap);
|
||||
|
||||
// read single key-value in map
|
||||
// read single key-value in map
|
||||
|
||||
String cql =
|
||||
session
|
||||
.select(Query.get(book::writers, new SectionImpl("first", 1)))
|
||||
.where(book::id, Query.eq(id))
|
||||
.cql();
|
||||
String cql = session.select(Query.get(book::writers, new SectionImpl("first", 1))).where(book::id, Query.eq(id))
|
||||
.cql();
|
||||
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
|
||||
// UPDATE
|
||||
// UPDATE
|
||||
|
||||
Map<Section, Author> expected = new HashMap<Section, Author>();
|
||||
expected.put(new SectionImpl("f", 1), new TupleCollectionTest.AuthorImpl("A", "SJ"));
|
||||
expected.put(new SectionImpl("s", 1), new TupleCollectionTest.AuthorImpl("B", "SF"));
|
||||
Map<Section, Author> expected = new HashMap<Section, Author>();
|
||||
expected.put(new SectionImpl("f", 1), new TupleCollectionTest.AuthorImpl("A", "SJ"));
|
||||
expected.put(new SectionImpl("s", 1), new TupleCollectionTest.AuthorImpl("B", "SF"));
|
||||
|
||||
session.update().set(book::writers, expected).where(book::id, Query.eq(id)).sync();
|
||||
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());
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(expected, actual.writers());
|
||||
|
||||
// INSERT
|
||||
// INSERT
|
||||
|
||||
// put operation
|
||||
// put operation
|
||||
|
||||
Section third = new SectionImpl("t", 3);
|
||||
Author unk = new TupleCollectionTest.AuthorImpl("Unk", "City 17");
|
||||
Section third = new SectionImpl("t", 3);
|
||||
Author unk = new TupleCollectionTest.AuthorImpl("Unk", "City 17");
|
||||
|
||||
expected.put(third, unk);
|
||||
session.update().put(book::writers, third, unk).where(book::id, Query.eq(id)).sync();
|
||||
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;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// putAll operation
|
||||
expected.putAll(writers);
|
||||
session.update().putAll(book::writers, writers).where(book::id, Query.eq(id)).sync();
|
||||
// putAll operation
|
||||
expected.putAll(writers);
|
||||
session.update().putAll(book::writers, writers).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// put existing
|
||||
// put existing
|
||||
|
||||
expected.put(third, unk);
|
||||
session.update().put(book::writers, third, unk).where(book::id, Query.eq(id)).sync();
|
||||
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;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// DELETE
|
||||
// DELETE
|
||||
|
||||
// remove single key
|
||||
// remove single key
|
||||
|
||||
expected.remove(third);
|
||||
session.update().put(book::writers, third, null).where(book::id, Query.eq(id)).sync();
|
||||
expected.remove(third);
|
||||
session.update().put(book::writers, third, null).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// remove full map
|
||||
// remove full map
|
||||
|
||||
session.update().set(book::writers, null).where(book::id, Query.eq(id)).sync();
|
||||
session.update().set(book::writers, null).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualMap);
|
||||
actualMap = session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualMap);
|
||||
|
||||
// remove object
|
||||
// remove object
|
||||
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
|
||||
private void assertEqualMaps(Map<Section, Author> expected, Map<Section, Author> actual) {
|
||||
private void assertEqualMaps(Map<Section, Author> expected, Map<Section, Author> actual) {
|
||||
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
|
||||
for (Section e : expected.keySet()) {
|
||||
Section a =
|
||||
actual.keySet().stream().filter(p -> p.title().equals(e.title())).findFirst().get();
|
||||
Assert.assertEquals(e.title(), a.title());
|
||||
Assert.assertEquals(e.page(), a.page());
|
||||
for (Section e : expected.keySet()) {
|
||||
Section a = actual.keySet().stream().filter(p -> p.title().equals(e.title())).findFirst().get();
|
||||
Assert.assertEquals(e.title(), a.title());
|
||||
Assert.assertEquals(e.page(), a.page());
|
||||
|
||||
Author ea = expected.get(e);
|
||||
Author aa = actual.get(a);
|
||||
Author ea = expected.get(e);
|
||||
Author aa = actual.get(a);
|
||||
|
||||
Assert.assertEquals(ea.name(), aa.name());
|
||||
Assert.assertEquals(ea.city(), aa.city());
|
||||
}
|
||||
}
|
||||
Assert.assertEquals(ea.name(), aa.name());
|
||||
Assert.assertEquals(ea.city(), aa.city());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,107 +18,100 @@ package net.helenus.test.integration.core.tuplecollection;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import net.helenus.core.Query;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Query;
|
||||
|
||||
public class TupleSetTest extends TupleCollectionTest {
|
||||
|
||||
@Test
|
||||
public void testSetCRUID() throws TimeoutException {
|
||||
@Test
|
||||
public void testSetCRUID() throws TimeoutException {
|
||||
|
||||
int id = 555;
|
||||
int id = 555;
|
||||
|
||||
// CREATE
|
||||
// CREATE
|
||||
|
||||
Set<Author> reviewers = new HashSet<Author>();
|
||||
reviewers.add(new AuthorImpl("Alex", "San Jose"));
|
||||
reviewers.add(new AuthorImpl("Bob", "San Francisco"));
|
||||
Set<Author> reviewers = new HashSet<Author>();
|
||||
reviewers.add(new AuthorImpl("Alex", "San Jose"));
|
||||
reviewers.add(new AuthorImpl("Bob", "San Francisco"));
|
||||
|
||||
session.insert().value(book::id, id).value(book::reviewers, reviewers).sync();
|
||||
session.insert().value(book::id, id).value(book::reviewers, reviewers).sync();
|
||||
|
||||
// READ
|
||||
// READ
|
||||
|
||||
Book actual = session.select(Book.class).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualSets(reviewers, actual.reviewers());
|
||||
Book actual = session.select(Book.class).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualSets(reviewers, actual.reviewers());
|
||||
|
||||
// UPDATE
|
||||
// UPDATE
|
||||
|
||||
Set<Author> expected = new HashSet<Author>();
|
||||
expected.add(new AuthorImpl("Craig", "Los Altos"));
|
||||
Set<Author> expected = new HashSet<Author>();
|
||||
expected.add(new AuthorImpl("Craig", "Los Altos"));
|
||||
|
||||
session.update().set(book::reviewers, expected).where(book::id, Query.eq(id)).sync();
|
||||
session.update().set(book::reviewers, expected).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
Set<Author> actualSet =
|
||||
session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
Set<Author> actualSet = session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst()
|
||||
.get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
|
||||
// add operation
|
||||
// add operation
|
||||
|
||||
expected.add(new AuthorImpl("Add", "AddCity"));
|
||||
session
|
||||
.update()
|
||||
.add(book::reviewers, new AuthorImpl("Add", "AddCity"))
|
||||
.where(book::id, Query.eq(id))
|
||||
.sync();
|
||||
expected.add(new AuthorImpl("Add", "AddCity"));
|
||||
session.update().add(book::reviewers, new AuthorImpl("Add", "AddCity")).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualSet =
|
||||
session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
actualSet = session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
|
||||
// addAll operation
|
||||
expected.addAll(reviewers);
|
||||
session.update().addAll(book::reviewers, reviewers).where(book::id, Query.eq(id)).sync();
|
||||
// addAll operation
|
||||
expected.addAll(reviewers);
|
||||
session.update().addAll(book::reviewers, reviewers).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualSet =
|
||||
session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
actualSet = session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
|
||||
// DELETE
|
||||
// DELETE
|
||||
|
||||
// remove single value
|
||||
// remove single value
|
||||
|
||||
Author a = expected.stream().filter(p -> p.name().equals("Add")).findFirst().get();
|
||||
expected.remove(a);
|
||||
Author a = expected.stream().filter(p -> p.name().equals("Add")).findFirst().get();
|
||||
expected.remove(a);
|
||||
|
||||
session.update().remove(book::reviewers, a).where(book::id, Query.eq(id)).sync();
|
||||
session.update().remove(book::reviewers, a).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualSet =
|
||||
session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
actualSet = session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
|
||||
// remove values
|
||||
// remove values
|
||||
|
||||
expected.remove(expected.stream().filter(p -> p.name().equals("Alex")).findFirst().get());
|
||||
expected.remove(expected.stream().filter(p -> p.name().equals("Bob")).findFirst().get());
|
||||
session.update().removeAll(book::reviewers, reviewers).where(book::id, Query.eq(id)).sync();
|
||||
expected.remove(expected.stream().filter(p -> p.name().equals("Alex")).findFirst().get());
|
||||
expected.remove(expected.stream().filter(p -> p.name().equals("Bob")).findFirst().get());
|
||||
session.update().removeAll(book::reviewers, reviewers).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualSet =
|
||||
session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
actualSet = session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
|
||||
// remove full list
|
||||
// remove full list
|
||||
|
||||
session.update().set(book::reviewers, null).where(book::id, Query.eq(id)).sync();
|
||||
session.update().set(book::reviewers, null).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualSet =
|
||||
session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualSet);
|
||||
actualSet = session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualSet);
|
||||
|
||||
// remove object
|
||||
// remove object
|
||||
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
|
||||
private void assertEqualSets(Set<Author> expected, Set<Author> actual) {
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
private void assertEqualSets(Set<Author> expected, Set<Author> actual) {
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
|
||||
for (Author e : expected) {
|
||||
Author a = actual.stream().filter(p -> p.name().equals(e.name())).findFirst().get();
|
||||
Assert.assertEquals(e.city(), a.city());
|
||||
}
|
||||
}
|
||||
for (Author e : expected) {
|
||||
Author a = actual.stream().filter(p -> p.name().equals(e.name())).findFirst().get();
|
||||
Assert.assertEquals(e.city(), a.city());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,126 +18,123 @@ package net.helenus.test.integration.core.tuplecollection;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import net.helenus.core.Query;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Query;
|
||||
|
||||
public class TupleValueMapTest extends TupleCollectionTest {
|
||||
|
||||
@Test
|
||||
public void testValueMapCRUID() throws TimeoutException {
|
||||
@Test
|
||||
public void testValueMapCRUID() throws TimeoutException {
|
||||
|
||||
int id = 999;
|
||||
int id = 999;
|
||||
|
||||
Map<Integer, Section> contents = new HashMap<Integer, Section>();
|
||||
contents.put(1, new SectionImpl("first", 1));
|
||||
contents.put(2, new SectionImpl("second", 2));
|
||||
Map<Integer, Section> contents = new HashMap<Integer, Section>();
|
||||
contents.put(1, new SectionImpl("first", 1));
|
||||
contents.put(2, new SectionImpl("second", 2));
|
||||
|
||||
// CREATE
|
||||
// CREATE
|
||||
|
||||
session.insert().value(book::id, id).value(book::contents, contents).sync();
|
||||
session.insert().value(book::id, id).value(book::contents, contents).sync();
|
||||
|
||||
// READ
|
||||
// READ
|
||||
|
||||
// read full object
|
||||
// read full object
|
||||
|
||||
Book actual = session.select(Book.class).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(contents, actual.contents());
|
||||
Assert.assertNull(actual.reviewers());
|
||||
Assert.assertNull(actual.writers());
|
||||
Assert.assertNull(actual.notes());
|
||||
Book actual = session.select(Book.class).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(contents, actual.contents());
|
||||
Assert.assertNull(actual.reviewers());
|
||||
Assert.assertNull(actual.writers());
|
||||
Assert.assertNull(actual.notes());
|
||||
|
||||
// read full map
|
||||
// read full map
|
||||
|
||||
Map<Integer, Section> actualMap =
|
||||
session.select(book::contents).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(contents, actualMap);
|
||||
Map<Integer, Section> actualMap = session.select(book::contents).where(book::id, Query.eq(id)).sync()
|
||||
.findFirst().get()._1;
|
||||
assertEqualMaps(contents, actualMap);
|
||||
|
||||
// read single key-value in map
|
||||
// read single key-value in map
|
||||
|
||||
String cql = session.select(Query.get(book::contents, 1)).where(book::id, Query.eq(id)).cql();
|
||||
String cql = session.select(Query.get(book::contents, 1)).where(book::id, Query.eq(id)).cql();
|
||||
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
|
||||
// UPDATE
|
||||
// UPDATE
|
||||
|
||||
Map<Integer, Section> expected = new HashMap<Integer, Section>();
|
||||
expected.put(4, new SectionImpl("4", 4));
|
||||
expected.put(5, new SectionImpl("5", 5));
|
||||
Map<Integer, Section> expected = new HashMap<Integer, Section>();
|
||||
expected.put(4, new SectionImpl("4", 4));
|
||||
expected.put(5, new SectionImpl("5", 5));
|
||||
|
||||
session.update().set(book::contents, expected).where(book::id, Query.eq(id)).sync();
|
||||
session.update().set(book::contents, expected).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actual = session.select(Book.class).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(expected, actual.contents());
|
||||
actual = session.select(Book.class).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(expected, actual.contents());
|
||||
|
||||
// INSERT
|
||||
// INSERT
|
||||
|
||||
// put operation
|
||||
// put operation
|
||||
|
||||
Section third = new SectionImpl("t", 3);
|
||||
Section third = new SectionImpl("t", 3);
|
||||
|
||||
expected.put(3, third);
|
||||
session.update().put(book::contents, 3, third).where(book::id, Query.eq(id)).sync();
|
||||
expected.put(3, third);
|
||||
session.update().put(book::contents, 3, third).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::contents).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::contents).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// putAll operation
|
||||
expected.putAll(contents);
|
||||
session.update().putAll(book::contents, contents).where(book::id, Query.eq(id)).sync();
|
||||
// putAll operation
|
||||
expected.putAll(contents);
|
||||
session.update().putAll(book::contents, contents).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::contents).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::contents).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// put existing
|
||||
// put existing
|
||||
|
||||
third = new SectionImpl("t-replace", 3);
|
||||
expected.put(3, third);
|
||||
session.update().put(book::contents, 3, third).where(book::id, Query.eq(id)).sync();
|
||||
third = new SectionImpl("t-replace", 3);
|
||||
expected.put(3, third);
|
||||
session.update().put(book::contents, 3, third).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::contents).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::contents).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// DELETE
|
||||
// DELETE
|
||||
|
||||
// remove single key
|
||||
// remove single key
|
||||
|
||||
expected.remove(3);
|
||||
session.update().put(book::contents, 3, null).where(book::id, Query.eq(id)).sync();
|
||||
expected.remove(3);
|
||||
session.update().put(book::contents, 3, null).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::contents).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::contents).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// remove full map
|
||||
// remove full map
|
||||
|
||||
session.update().set(book::contents, null).where(book::id, Query.eq(id)).sync();
|
||||
session.update().set(book::contents, null).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::contents).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualMap);
|
||||
actualMap = session.select(book::contents).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualMap);
|
||||
|
||||
// remove object
|
||||
// remove object
|
||||
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
|
||||
private void assertEqualMaps(Map<Integer, Section> expected, Map<Integer, Section> actual) {
|
||||
private void assertEqualMaps(Map<Integer, Section> expected, Map<Integer, Section> actual) {
|
||||
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
|
||||
for (Integer i : expected.keySet()) {
|
||||
Section e = expected.get(i);
|
||||
Section a = actual.get(i);
|
||||
Assert.assertEquals(e.title(), a.title());
|
||||
Assert.assertEquals(e.page(), a.page());
|
||||
}
|
||||
}
|
||||
for (Integer i : expected.keySet()) {
|
||||
Section e = expected.get(i);
|
||||
Section a = actual.get(i);
|
||||
Assert.assertEquals(e.title(), a.title());
|
||||
Assert.assertEquals(e.page(), a.page());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ import net.helenus.mapping.annotation.UDT;
|
|||
@UDT
|
||||
public interface Author {
|
||||
|
||||
String name();
|
||||
String name();
|
||||
|
||||
String city();
|
||||
String city();
|
||||
}
|
||||
|
|
|
@ -18,22 +18,23 @@ package net.helenus.test.integration.core.udtcollection;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import net.helenus.mapping.annotation.PartitionKey;
|
||||
import net.helenus.mapping.annotation.Table;
|
||||
|
||||
@Table
|
||||
public interface Book {
|
||||
|
||||
@PartitionKey
|
||||
int id();
|
||||
@PartitionKey
|
||||
int id();
|
||||
|
||||
List<Author> authors();
|
||||
List<Author> authors();
|
||||
|
||||
Set<Author> reviewers();
|
||||
Set<Author> reviewers();
|
||||
|
||||
Map<Integer, Section> contents();
|
||||
Map<Integer, Section> contents();
|
||||
|
||||
Map<Section, String> notes();
|
||||
Map<Section, String> notes();
|
||||
|
||||
Map<Section, Author> writers();
|
||||
Map<Section, Author> writers();
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ import net.helenus.mapping.annotation.UDT;
|
|||
@UDT
|
||||
public interface Section {
|
||||
|
||||
String title();
|
||||
String title();
|
||||
|
||||
int page();
|
||||
int page();
|
||||
}
|
||||
|
|
|
@ -15,125 +15,139 @@
|
|||
*/
|
||||
package net.helenus.test.integration.core.udtcollection;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
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;
|
||||
|
||||
public abstract class UDTCollectionTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static Book book;
|
||||
static Book book;
|
||||
|
||||
static HelenusSession session;
|
||||
static HelenusSession session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
Helenus.clearDslCache();
|
||||
session = Helenus.init(getSession()).showCql().add(Book.class).autoCreateDrop().get();
|
||||
book = Helenus.dsl(Book.class);
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
Helenus.clearDslCache();
|
||||
session = Helenus.init(getSession()).showCql().add(Book.class).autoCreateDrop().get();
|
||||
book = Helenus.dsl(Book.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
System.out.println(book);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
System.out.println(book);
|
||||
}
|
||||
|
||||
public static final class AuthorImpl implements Author {
|
||||
public static final class AuthorImpl implements Author {
|
||||
|
||||
String name;
|
||||
String city;
|
||||
String name;
|
||||
String city;
|
||||
|
||||
AuthorImpl(String name, String city) {
|
||||
this.name = name;
|
||||
this.city = city;
|
||||
}
|
||||
AuthorImpl(String name, String city) {
|
||||
this.name = name;
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String city() {
|
||||
return city;
|
||||
}
|
||||
@Override
|
||||
public String city() {
|
||||
return city;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((city == null) ? 0 : city.hashCode());
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((city == null) ? 0 : city.hashCode());
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null) return false;
|
||||
if (getClass() != obj.getClass()) return false;
|
||||
AuthorImpl other = (AuthorImpl) obj;
|
||||
if (city == null) {
|
||||
if (other.city != null) return false;
|
||||
} else if (!city.equals(other.city)) return false;
|
||||
if (name == null) {
|
||||
if (other.name != null) return false;
|
||||
} else if (!name.equals(other.name)) return false;
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
AuthorImpl other = (AuthorImpl) obj;
|
||||
if (city == null) {
|
||||
if (other.city != null)
|
||||
return false;
|
||||
} else if (!city.equals(other.city))
|
||||
return false;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AuthorImpl [name=" + name + ", city=" + city + "]";
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AuthorImpl [name=" + name + ", city=" + city + "]";
|
||||
}
|
||||
}
|
||||
|
||||
public static final class SectionImpl implements Section {
|
||||
public static final class SectionImpl implements Section {
|
||||
|
||||
String title;
|
||||
int page;
|
||||
String title;
|
||||
int page;
|
||||
|
||||
SectionImpl(String title, int page) {
|
||||
this.title = title;
|
||||
this.page = page;
|
||||
}
|
||||
SectionImpl(String title, int page) {
|
||||
this.title = title;
|
||||
this.page = page;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String title() {
|
||||
return title;
|
||||
}
|
||||
@Override
|
||||
public String title() {
|
||||
return title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int page() {
|
||||
return page;
|
||||
}
|
||||
@Override
|
||||
public int page() {
|
||||
return page;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + page;
|
||||
result = prime * result + ((title == null) ? 0 : title.hashCode());
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + page;
|
||||
result = prime * result + ((title == null) ? 0 : title.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null) return false;
|
||||
if (getClass() != obj.getClass()) return false;
|
||||
SectionImpl other = (SectionImpl) obj;
|
||||
if (page != other.page) return false;
|
||||
if (title == null) {
|
||||
if (other.title != null) return false;
|
||||
} else if (!title.equals(other.title)) return false;
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
SectionImpl other = (SectionImpl) obj;
|
||||
if (page != other.page)
|
||||
return false;
|
||||
if (title == null) {
|
||||
if (other.title != null)
|
||||
return false;
|
||||
} else if (!title.equals(other.title))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SectionImpl [title=" + title + ", page=" + page + "]";
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SectionImpl [title=" + title + ", page=" + page + "]";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,121 +21,120 @@ import static net.helenus.core.Query.get;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UDTKeyMapTest extends UDTCollectionTest {
|
||||
|
||||
@Test
|
||||
public void testKeyMapCRUID() throws TimeoutException {
|
||||
@Test
|
||||
public void testKeyMapCRUID() throws TimeoutException {
|
||||
|
||||
int id = 888;
|
||||
int id = 888;
|
||||
|
||||
Map<Section, String> notes = new HashMap<Section, String>();
|
||||
notes.put(new SectionImpl("first", 1), "value1");
|
||||
notes.put(new SectionImpl("second", 2), "value2");
|
||||
Map<Section, String> notes = new HashMap<Section, String>();
|
||||
notes.put(new SectionImpl("first", 1), "value1");
|
||||
notes.put(new SectionImpl("second", 2), "value2");
|
||||
|
||||
// CREATE
|
||||
// CREATE
|
||||
|
||||
session.insert().value(book::id, id).value(book::notes, notes).sync();
|
||||
session.insert().value(book::id, id).value(book::notes, notes).sync();
|
||||
|
||||
// READ
|
||||
// READ
|
||||
|
||||
// read full object
|
||||
// read full object
|
||||
|
||||
Book actual = session.<Book>select(book).where(book::id, eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(notes, actual.notes());
|
||||
Assert.assertNull(actual.reviewers());
|
||||
Assert.assertNull(actual.writers());
|
||||
Assert.assertNull(actual.contents());
|
||||
Book actual = session.<Book>select(book).where(book::id, eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(notes, actual.notes());
|
||||
Assert.assertNull(actual.reviewers());
|
||||
Assert.assertNull(actual.writers());
|
||||
Assert.assertNull(actual.contents());
|
||||
|
||||
// read full map
|
||||
// read full map
|
||||
|
||||
Map<Section, String> actualMap =
|
||||
session.select(book::notes).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(notes, actualMap);
|
||||
Map<Section, String> actualMap = session.select(book::notes).where(book::id, eq(id)).sync().findFirst()
|
||||
.get()._1;
|
||||
assertEqualMaps(notes, actualMap);
|
||||
|
||||
// read single key-value in map
|
||||
// read single key-value in map
|
||||
|
||||
String cql =
|
||||
session.select(get(book::notes, new SectionImpl("first", 1))).where(book::id, eq(id)).cql();
|
||||
String cql = session.select(get(book::notes, new SectionImpl("first", 1))).where(book::id, eq(id)).cql();
|
||||
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
|
||||
// UPDATE
|
||||
// UPDATE
|
||||
|
||||
Map<Section, String> expected = new HashMap<Section, String>();
|
||||
expected.put(new SectionImpl("f", 1), "v1");
|
||||
expected.put(new SectionImpl("s", 1), "v2");
|
||||
Map<Section, String> expected = new HashMap<Section, String>();
|
||||
expected.put(new SectionImpl("f", 1), "v1");
|
||||
expected.put(new SectionImpl("s", 1), "v2");
|
||||
|
||||
session.update().set(book::notes, expected).where(book::id, eq(id)).sync();
|
||||
session.update().set(book::notes, expected).where(book::id, eq(id)).sync();
|
||||
|
||||
actual = session.<Book>select(book).where(book::id, eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(expected, actual.notes());
|
||||
actual = session.<Book>select(book).where(book::id, eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(expected, actual.notes());
|
||||
|
||||
// INSERT
|
||||
// INSERT
|
||||
|
||||
// put operation
|
||||
// put operation
|
||||
|
||||
Section third = new SectionImpl("t", 3);
|
||||
Section third = new SectionImpl("t", 3);
|
||||
|
||||
expected.put(third, "v3");
|
||||
session.update().put(book::notes, third, "v3").where(book::id, eq(id)).sync();
|
||||
expected.put(third, "v3");
|
||||
session.update().put(book::notes, third, "v3").where(book::id, eq(id)).sync();
|
||||
|
||||
actualMap = session.select(book::notes).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::notes).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// putAll operation
|
||||
expected.putAll(notes);
|
||||
session.update().putAll(book::notes, notes).where(book::id, eq(id)).sync();
|
||||
// putAll operation
|
||||
expected.putAll(notes);
|
||||
session.update().putAll(book::notes, notes).where(book::id, eq(id)).sync();
|
||||
|
||||
actualMap = session.select(book::notes).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::notes).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// put existing
|
||||
// put existing
|
||||
|
||||
expected.put(third, "v33");
|
||||
session.update().put(book::notes, third, "v33").where(book::id, eq(id)).sync();
|
||||
expected.put(third, "v33");
|
||||
session.update().put(book::notes, third, "v33").where(book::id, eq(id)).sync();
|
||||
|
||||
actualMap = session.select(book::notes).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::notes).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// DELETE
|
||||
// DELETE
|
||||
|
||||
// remove single key
|
||||
// remove single key
|
||||
|
||||
expected.remove(third);
|
||||
session.update().put(book::notes, third, null).where(book::id, eq(id)).sync();
|
||||
expected.remove(third);
|
||||
session.update().put(book::notes, third, null).where(book::id, eq(id)).sync();
|
||||
|
||||
actualMap = session.select(book::notes).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::notes).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// remove full map
|
||||
// remove full map
|
||||
|
||||
session.update().set(book::notes, null).where(book::id, eq(id)).sync();
|
||||
session.update().set(book::notes, null).where(book::id, eq(id)).sync();
|
||||
|
||||
actualMap = session.select(book::notes).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualMap);
|
||||
actualMap = session.select(book::notes).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualMap);
|
||||
|
||||
// remove object
|
||||
// remove object
|
||||
|
||||
session.delete().where(book::id, eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
session.delete().where(book::id, eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
|
||||
private void assertEqualMaps(Map<Section, String> expected, Map<Section, String> actual) {
|
||||
private void assertEqualMaps(Map<Section, String> expected, Map<Section, String> actual) {
|
||||
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
|
||||
for (Section e : expected.keySet()) {
|
||||
Section a =
|
||||
actual.keySet().stream().filter(p -> p.title().equals(e.title())).findFirst().get();
|
||||
Assert.assertEquals(e.title(), a.title());
|
||||
Assert.assertEquals(e.page(), a.page());
|
||||
Assert.assertEquals(expected.get(e), actual.get(a));
|
||||
}
|
||||
}
|
||||
for (Section e : expected.keySet()) {
|
||||
Section a = actual.keySet().stream().filter(p -> p.title().equals(e.title())).findFirst().get();
|
||||
Assert.assertEquals(e.title(), a.title());
|
||||
Assert.assertEquals(e.page(), a.page());
|
||||
Assert.assertEquals(expected.get(e), actual.get(a));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,157 +18,145 @@ package net.helenus.test.integration.core.udtcollection;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import net.helenus.core.Query;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Query;
|
||||
|
||||
public class UDTListTest extends UDTCollectionTest {
|
||||
|
||||
@Test
|
||||
public void testListCRUID() throws TimeoutException {
|
||||
@Test
|
||||
public void testListCRUID() throws TimeoutException {
|
||||
|
||||
int id = 777;
|
||||
int id = 777;
|
||||
|
||||
List<Author> authors = new ArrayList<Author>();
|
||||
authors.add(new AuthorImpl("Alex", "San Jose"));
|
||||
authors.add(new AuthorImpl("Bob", "San Francisco"));
|
||||
List<Author> authors = new ArrayList<Author>();
|
||||
authors.add(new AuthorImpl("Alex", "San Jose"));
|
||||
authors.add(new AuthorImpl("Bob", "San Francisco"));
|
||||
|
||||
// CREATE
|
||||
// CREATE
|
||||
|
||||
session.insert().value(book::id, id).value(book::authors, authors).sync();
|
||||
session.insert().value(book::id, id).value(book::authors, authors).sync();
|
||||
|
||||
// READ
|
||||
// READ
|
||||
|
||||
// read full object
|
||||
// read full object
|
||||
|
||||
Book actual = session.<Book>select(book).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualLists(authors, actual.authors());
|
||||
Assert.assertNull(actual.reviewers());
|
||||
Assert.assertNull(actual.contents());
|
||||
Book actual = session.<Book>select(book).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualLists(authors, actual.authors());
|
||||
Assert.assertNull(actual.reviewers());
|
||||
Assert.assertNull(actual.contents());
|
||||
|
||||
// read full list
|
||||
// read full list
|
||||
|
||||
List<Author> actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(authors, actualList);
|
||||
List<Author> actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst()
|
||||
.get()._1;
|
||||
assertEqualLists(authors, actualList);
|
||||
|
||||
// read single value by index
|
||||
// read single value by index
|
||||
|
||||
String cql = session.select(Query.getIdx(book::authors, 1)).where(book::id, Query.eq(id)).cql();
|
||||
String cql = session.select(Query.getIdx(book::authors, 1)).where(book::id, Query.eq(id)).cql();
|
||||
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
|
||||
// UPDATE
|
||||
// UPDATE
|
||||
|
||||
List<Author> expected = new ArrayList<Author>();
|
||||
expected.add(new AuthorImpl("Unknown", "City 17"));
|
||||
List<Author> expected = new ArrayList<Author>();
|
||||
expected.add(new AuthorImpl("Unknown", "City 17"));
|
||||
|
||||
session.update().set(book::authors, expected).where(book::id, Query.eq(id)).sync();
|
||||
session.update().set(book::authors, expected).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actual = session.<Book>select(book).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualLists(expected, actual.authors());
|
||||
actual = session.<Book>select(book).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualLists(expected, actual.authors());
|
||||
|
||||
// INSERT
|
||||
// INSERT
|
||||
|
||||
// prepend operation
|
||||
// prepend operation
|
||||
|
||||
expected.add(0, new AuthorImpl("Prepend", "PrependCity"));
|
||||
session
|
||||
.update()
|
||||
.prepend(book::authors, new AuthorImpl("Prepend", "PrependCity"))
|
||||
.where(book::id, Query.eq(id))
|
||||
.sync();
|
||||
expected.add(0, new AuthorImpl("Prepend", "PrependCity"));
|
||||
session.update().prepend(book::authors, new AuthorImpl("Prepend", "PrependCity")).where(book::id, Query.eq(id))
|
||||
.sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
|
||||
// append operation
|
||||
// append operation
|
||||
|
||||
expected.add(new AuthorImpl("Append", "AppendCity"));
|
||||
session
|
||||
.update()
|
||||
.append(book::authors, new AuthorImpl("Append", "AppendCity"))
|
||||
.where(book::id, Query.eq(id))
|
||||
.sync();
|
||||
expected.add(new AuthorImpl("Append", "AppendCity"));
|
||||
session.update().append(book::authors, new AuthorImpl("Append", "AppendCity")).where(book::id, Query.eq(id))
|
||||
.sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
|
||||
// prependAll operation
|
||||
expected.addAll(0, authors);
|
||||
session.update().prependAll(book::authors, authors).where(book::id, Query.eq(id)).sync();
|
||||
// prependAll operation
|
||||
expected.addAll(0, authors);
|
||||
session.update().prependAll(book::authors, authors).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
|
||||
// appendAll operation
|
||||
expected.addAll(authors);
|
||||
session.update().appendAll(book::authors, authors).where(book::id, Query.eq(id)).sync();
|
||||
// appendAll operation
|
||||
expected.addAll(authors);
|
||||
session.update().appendAll(book::authors, authors).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
|
||||
// set by Index
|
||||
// set by Index
|
||||
|
||||
Author inserted = new AuthorImpl("Insert", "InsertCity");
|
||||
expected.set(5, inserted);
|
||||
session.update().setIdx(book::authors, 5, inserted).where(book::id, Query.eq(id)).sync();
|
||||
Author inserted = new AuthorImpl("Insert", "InsertCity");
|
||||
expected.set(5, inserted);
|
||||
session.update().setIdx(book::authors, 5, inserted).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
|
||||
// DELETE
|
||||
// DELETE
|
||||
|
||||
// remove single value
|
||||
// remove single value
|
||||
|
||||
expected.remove(inserted);
|
||||
session.update().discard(book::authors, inserted).where(book::id, Query.eq(id)).sync();
|
||||
expected.remove(inserted);
|
||||
session.update().discard(book::authors, inserted).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
|
||||
// remove values
|
||||
// remove values
|
||||
|
||||
expected.removeAll(authors);
|
||||
session.update().discardAll(book::authors, authors).where(book::id, Query.eq(id)).sync();
|
||||
expected.removeAll(authors);
|
||||
session.update().discardAll(book::authors, authors).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualLists(expected, actualList);
|
||||
|
||||
// remove full list
|
||||
// remove full list
|
||||
|
||||
session.update().set(book::authors, null).where(book::id, Query.eq(id)).sync();
|
||||
session.update().set(book::authors, null).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualList =
|
||||
session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualList);
|
||||
actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualList);
|
||||
|
||||
// remove object
|
||||
// remove object
|
||||
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
|
||||
private void assertEqualLists(List<Author> expected, List<Author> actual) {
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
private void assertEqualLists(List<Author> expected, List<Author> actual) {
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
|
||||
int size = expected.size();
|
||||
int size = expected.size();
|
||||
|
||||
for (int i = 0; i != size; ++i) {
|
||||
Author e = expected.get(i);
|
||||
Author a = actual.get(i);
|
||||
Assert.assertEquals(e.name(), a.name());
|
||||
Assert.assertEquals(e.city(), a.city());
|
||||
}
|
||||
}
|
||||
for (int i = 0; i != size; ++i) {
|
||||
Author e = expected.get(i);
|
||||
Author a = actual.get(i);
|
||||
Assert.assertEquals(e.name(), a.name());
|
||||
Assert.assertEquals(e.city(), a.city());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,136 +18,129 @@ package net.helenus.test.integration.core.udtcollection;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import net.helenus.core.Query;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Query;
|
||||
|
||||
public class UDTMapTest extends UDTCollectionTest {
|
||||
|
||||
@Test
|
||||
public void testMapCRUID() throws TimeoutException {
|
||||
@Test
|
||||
public void testMapCRUID() throws TimeoutException {
|
||||
|
||||
int id = 333;
|
||||
int id = 333;
|
||||
|
||||
Map<Section, Author> writers = new HashMap<Section, Author>();
|
||||
writers.put(new SectionImpl("first", 1), new AuthorImpl("Alex", "San Jose"));
|
||||
writers.put(new SectionImpl("second", 2), new AuthorImpl("Bob", "San Francisco"));
|
||||
Map<Section, Author> writers = new HashMap<Section, Author>();
|
||||
writers.put(new SectionImpl("first", 1), new AuthorImpl("Alex", "San Jose"));
|
||||
writers.put(new SectionImpl("second", 2), new AuthorImpl("Bob", "San Francisco"));
|
||||
|
||||
// CREATE
|
||||
// CREATE
|
||||
|
||||
session.insert().value(book::id, id).value(book::writers, writers).sync();
|
||||
session.insert().value(book::id, id).value(book::writers, writers).sync();
|
||||
|
||||
// READ
|
||||
// READ
|
||||
|
||||
// read full object
|
||||
// read full object
|
||||
|
||||
Book actual = session.<Book>select(book).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(writers, actual.writers());
|
||||
Assert.assertNull(actual.reviewers());
|
||||
Assert.assertNull(actual.notes());
|
||||
Assert.assertNull(actual.contents());
|
||||
Book actual = session.<Book>select(book).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(writers, actual.writers());
|
||||
Assert.assertNull(actual.reviewers());
|
||||
Assert.assertNull(actual.notes());
|
||||
Assert.assertNull(actual.contents());
|
||||
|
||||
// read full map
|
||||
// read full map
|
||||
|
||||
Map<Section, Author> actualMap =
|
||||
session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(writers, actualMap);
|
||||
Map<Section, Author> actualMap = session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst()
|
||||
.get()._1;
|
||||
assertEqualMaps(writers, actualMap);
|
||||
|
||||
// read single key-value in map
|
||||
// read single key-value in map
|
||||
|
||||
String cql =
|
||||
session
|
||||
.select(Query.get(book::writers, new SectionImpl("first", 1)))
|
||||
.where(book::id, Query.eq(id))
|
||||
.cql();
|
||||
String cql = session.select(Query.get(book::writers, new SectionImpl("first", 1))).where(book::id, Query.eq(id))
|
||||
.cql();
|
||||
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
|
||||
// UPDATE
|
||||
// UPDATE
|
||||
|
||||
Map<Section, Author> expected = new HashMap<Section, Author>();
|
||||
expected.put(new SectionImpl("f", 1), new AuthorImpl("A", "SJ"));
|
||||
expected.put(new SectionImpl("s", 1), new AuthorImpl("B", "SF"));
|
||||
Map<Section, Author> expected = new HashMap<Section, Author>();
|
||||
expected.put(new SectionImpl("f", 1), new AuthorImpl("A", "SJ"));
|
||||
expected.put(new SectionImpl("s", 1), new AuthorImpl("B", "SF"));
|
||||
|
||||
session.update().set(book::writers, expected).where(book::id, Query.eq(id)).sync();
|
||||
session.update().set(book::writers, expected).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actual = session.<Book>select(book).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(expected, actual.writers());
|
||||
actual = session.<Book>select(book).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(expected, actual.writers());
|
||||
|
||||
// INSERT
|
||||
// INSERT
|
||||
|
||||
// put operation
|
||||
// put operation
|
||||
|
||||
Section third = new SectionImpl("t", 3);
|
||||
Author unk = new AuthorImpl("Unk", "City 17");
|
||||
Section third = new SectionImpl("t", 3);
|
||||
Author unk = new AuthorImpl("Unk", "City 17");
|
||||
|
||||
expected.put(third, unk);
|
||||
session.update().put(book::writers, third, unk).where(book::id, Query.eq(id)).sync();
|
||||
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;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// putAll operation
|
||||
expected.putAll(writers);
|
||||
session.update().putAll(book::writers, writers).where(book::id, Query.eq(id)).sync();
|
||||
// putAll operation
|
||||
expected.putAll(writers);
|
||||
session.update().putAll(book::writers, writers).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// put existing
|
||||
// put existing
|
||||
|
||||
expected.put(third, unk);
|
||||
session.update().put(book::writers, third, unk).where(book::id, Query.eq(id)).sync();
|
||||
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;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// DELETE
|
||||
// DELETE
|
||||
|
||||
// remove single key
|
||||
// remove single key
|
||||
|
||||
expected.remove(third);
|
||||
session.update().put(book::writers, third, null).where(book::id, Query.eq(id)).sync();
|
||||
expected.remove(third);
|
||||
session.update().put(book::writers, third, null).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// remove full map
|
||||
// remove full map
|
||||
|
||||
session.update().set(book::writers, null).where(book::id, Query.eq(id)).sync();
|
||||
session.update().set(book::writers, null).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualMap =
|
||||
session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualMap);
|
||||
actualMap = session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualMap);
|
||||
|
||||
// remove object
|
||||
// remove object
|
||||
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
|
||||
private void assertEqualMaps(Map<Section, Author> expected, Map<Section, Author> actual) {
|
||||
private void assertEqualMaps(Map<Section, Author> expected, Map<Section, Author> actual) {
|
||||
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
|
||||
for (Section e : expected.keySet()) {
|
||||
Section a =
|
||||
actual.keySet().stream().filter(p -> p.title().equals(e.title())).findFirst().get();
|
||||
Assert.assertEquals(e.title(), a.title());
|
||||
Assert.assertEquals(e.page(), a.page());
|
||||
for (Section e : expected.keySet()) {
|
||||
Section a = actual.keySet().stream().filter(p -> p.title().equals(e.title())).findFirst().get();
|
||||
Assert.assertEquals(e.title(), a.title());
|
||||
Assert.assertEquals(e.page(), a.page());
|
||||
|
||||
Author ea = expected.get(e);
|
||||
Author aa = actual.get(a);
|
||||
Author ea = expected.get(e);
|
||||
Author aa = actual.get(a);
|
||||
|
||||
Assert.assertEquals(ea.name(), aa.name());
|
||||
Assert.assertEquals(ea.city(), aa.city());
|
||||
}
|
||||
}
|
||||
Assert.assertEquals(ea.name(), aa.name());
|
||||
Assert.assertEquals(ea.city(), aa.city());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,107 +18,100 @@ package net.helenus.test.integration.core.udtcollection;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import net.helenus.core.Query;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Query;
|
||||
|
||||
public class UDTSetTest extends UDTCollectionTest {
|
||||
|
||||
@Test
|
||||
public void testSetCRUID() throws TimeoutException {
|
||||
@Test
|
||||
public void testSetCRUID() throws TimeoutException {
|
||||
|
||||
int id = 555;
|
||||
int id = 555;
|
||||
|
||||
// CREATE
|
||||
// CREATE
|
||||
|
||||
Set<Author> reviewers = new HashSet<Author>();
|
||||
reviewers.add(new AuthorImpl("Alex", "San Jose"));
|
||||
reviewers.add(new AuthorImpl("Bob", "San Francisco"));
|
||||
Set<Author> reviewers = new HashSet<Author>();
|
||||
reviewers.add(new AuthorImpl("Alex", "San Jose"));
|
||||
reviewers.add(new AuthorImpl("Bob", "San Francisco"));
|
||||
|
||||
session.insert().value(book::id, id).value(book::reviewers, reviewers).sync();
|
||||
session.insert().value(book::id, id).value(book::reviewers, reviewers).sync();
|
||||
|
||||
// READ
|
||||
// READ
|
||||
|
||||
Book actual = session.select(Book.class).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualSets(reviewers, actual.reviewers());
|
||||
Book actual = session.select(Book.class).where(book::id, Query.eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualSets(reviewers, actual.reviewers());
|
||||
|
||||
// UPDATE
|
||||
// UPDATE
|
||||
|
||||
Set<Author> expected = new HashSet<Author>();
|
||||
expected.add(new AuthorImpl("Craig", "Los Altos"));
|
||||
Set<Author> expected = new HashSet<Author>();
|
||||
expected.add(new AuthorImpl("Craig", "Los Altos"));
|
||||
|
||||
session.update().set(book::reviewers, expected).where(book::id, Query.eq(id)).sync();
|
||||
session.update().set(book::reviewers, expected).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
Set<Author> actualSet =
|
||||
session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
Set<Author> actualSet = session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst()
|
||||
.get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
|
||||
// add operation
|
||||
// add operation
|
||||
|
||||
expected.add(new AuthorImpl("Add", "AddCity"));
|
||||
session
|
||||
.update()
|
||||
.add(book::reviewers, new AuthorImpl("Add", "AddCity"))
|
||||
.where(book::id, Query.eq(id))
|
||||
.sync();
|
||||
expected.add(new AuthorImpl("Add", "AddCity"));
|
||||
session.update().add(book::reviewers, new AuthorImpl("Add", "AddCity")).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualSet =
|
||||
session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
actualSet = session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
|
||||
// addAll operation
|
||||
expected.addAll(reviewers);
|
||||
session.update().addAll(book::reviewers, reviewers).where(book::id, Query.eq(id)).sync();
|
||||
// addAll operation
|
||||
expected.addAll(reviewers);
|
||||
session.update().addAll(book::reviewers, reviewers).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualSet =
|
||||
session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
actualSet = session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
|
||||
// DELETE
|
||||
// DELETE
|
||||
|
||||
// remove single value
|
||||
// remove single value
|
||||
|
||||
Author a = expected.stream().filter(p -> p.name().equals("Add")).findFirst().get();
|
||||
expected.remove(a);
|
||||
Author a = expected.stream().filter(p -> p.name().equals("Add")).findFirst().get();
|
||||
expected.remove(a);
|
||||
|
||||
session.update().remove(book::reviewers, a).where(book::id, Query.eq(id)).sync();
|
||||
session.update().remove(book::reviewers, a).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualSet =
|
||||
session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
actualSet = session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
|
||||
// remove values
|
||||
// remove values
|
||||
|
||||
expected.remove(expected.stream().filter(p -> p.name().equals("Alex")).findFirst().get());
|
||||
expected.remove(expected.stream().filter(p -> p.name().equals("Bob")).findFirst().get());
|
||||
session.update().removeAll(book::reviewers, reviewers).where(book::id, Query.eq(id)).sync();
|
||||
expected.remove(expected.stream().filter(p -> p.name().equals("Alex")).findFirst().get());
|
||||
expected.remove(expected.stream().filter(p -> p.name().equals("Bob")).findFirst().get());
|
||||
session.update().removeAll(book::reviewers, reviewers).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualSet =
|
||||
session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
actualSet = session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualSets(expected, actualSet);
|
||||
|
||||
// remove full list
|
||||
// remove full list
|
||||
|
||||
session.update().set(book::reviewers, null).where(book::id, Query.eq(id)).sync();
|
||||
session.update().set(book::reviewers, null).where(book::id, Query.eq(id)).sync();
|
||||
|
||||
actualSet =
|
||||
session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualSet);
|
||||
actualSet = session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualSet);
|
||||
|
||||
// remove object
|
||||
// remove object
|
||||
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
session.delete().where(book::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, Query.eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
|
||||
private void assertEqualSets(Set<Author> expected, Set<Author> actual) {
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
private void assertEqualSets(Set<Author> expected, Set<Author> actual) {
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
|
||||
for (Author e : expected) {
|
||||
Author a = actual.stream().filter(p -> p.name().equals(e.name())).findFirst().get();
|
||||
Assert.assertEquals(e.city(), a.city());
|
||||
}
|
||||
}
|
||||
for (Author e : expected) {
|
||||
Author a = actual.stream().filter(p -> p.name().equals(e.name())).findFirst().get();
|
||||
Assert.assertEquals(e.city(), a.city());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,120 +21,121 @@ import static net.helenus.core.Query.get;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UDTValueMapTest extends UDTCollectionTest {
|
||||
|
||||
@Test
|
||||
public void testValueMapCRUID() throws TimeoutException {
|
||||
@Test
|
||||
public void testValueMapCRUID() throws TimeoutException {
|
||||
|
||||
int id = 999;
|
||||
int id = 999;
|
||||
|
||||
Map<Integer, Section> contents = new HashMap<Integer, Section>();
|
||||
contents.put(1, new SectionImpl("first", 1));
|
||||
contents.put(2, new SectionImpl("second", 2));
|
||||
Map<Integer, Section> contents = new HashMap<Integer, Section>();
|
||||
contents.put(1, new SectionImpl("first", 1));
|
||||
contents.put(2, new SectionImpl("second", 2));
|
||||
|
||||
// CREATE
|
||||
// CREATE
|
||||
|
||||
session.insert().value(book::id, id).value(book::contents, contents).sync();
|
||||
session.insert().value(book::id, id).value(book::contents, contents).sync();
|
||||
|
||||
// READ
|
||||
// READ
|
||||
|
||||
// read full object
|
||||
// read full object
|
||||
|
||||
Book actual = session.select(Book.class).where(book::id, eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(contents, actual.contents());
|
||||
Assert.assertNull(actual.reviewers());
|
||||
Assert.assertNull(actual.writers());
|
||||
Assert.assertNull(actual.notes());
|
||||
Book actual = session.select(Book.class).where(book::id, eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(contents, actual.contents());
|
||||
Assert.assertNull(actual.reviewers());
|
||||
Assert.assertNull(actual.writers());
|
||||
Assert.assertNull(actual.notes());
|
||||
|
||||
// read full map
|
||||
// read full map
|
||||
|
||||
Map<Integer, Section> actualMap =
|
||||
session.select(book::contents).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(contents, actualMap);
|
||||
Map<Integer, Section> actualMap = session.select(book::contents).where(book::id, eq(id)).sync().findFirst()
|
||||
.get()._1;
|
||||
assertEqualMaps(contents, actualMap);
|
||||
|
||||
// read single key-value in map
|
||||
// read single key-value in map
|
||||
|
||||
String cql = session.select(get(book::contents, 1)).where(book::id, eq(id)).cql();
|
||||
String cql = session.select(get(book::contents, 1)).where(book::id, eq(id)).cql();
|
||||
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
System.out.println("Still not supporting cql = " + cql);
|
||||
|
||||
// UPDATE
|
||||
// UPDATE
|
||||
|
||||
Map<Integer, Section> expected = new HashMap<Integer, Section>();
|
||||
expected.put(4, new SectionImpl("4", 4));
|
||||
expected.put(5, new SectionImpl("5", 5));
|
||||
Map<Integer, Section> expected = new HashMap<Integer, Section>();
|
||||
expected.put(4, new SectionImpl("4", 4));
|
||||
expected.put(5, new SectionImpl("5", 5));
|
||||
|
||||
session.update().set(book::contents, expected).where(book::id, eq(id)).sync();
|
||||
session.update().set(book::contents, expected).where(book::id, eq(id)).sync();
|
||||
|
||||
actual = session.select(Book.class).where(book::id, eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(expected, actual.contents());
|
||||
actual = session.select(Book.class).where(book::id, eq(id)).sync().findFirst().get();
|
||||
Assert.assertEquals(id, actual.id());
|
||||
assertEqualMaps(expected, actual.contents());
|
||||
|
||||
// INSERT
|
||||
// INSERT
|
||||
|
||||
// put operation
|
||||
// put operation
|
||||
|
||||
Section third = new SectionImpl("t", 3);
|
||||
Section third = new SectionImpl("t", 3);
|
||||
|
||||
expected.put(3, third);
|
||||
session.update().put(book::contents, 3, third).where(book::id, eq(id)).sync();
|
||||
expected.put(3, third);
|
||||
session.update().put(book::contents, 3, third).where(book::id, eq(id)).sync();
|
||||
|
||||
actualMap = session.select(book::contents).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::contents).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// putAll operation
|
||||
expected.putAll(contents);
|
||||
session.update().putAll(book::contents, contents).where(book::id, eq(id)).sync();
|
||||
// putAll operation
|
||||
expected.putAll(contents);
|
||||
session.update().putAll(book::contents, contents).where(book::id, eq(id)).sync();
|
||||
|
||||
actualMap = session.select(book::contents).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::contents).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// put existing
|
||||
// put existing
|
||||
|
||||
third = new SectionImpl("t-replace", 3);
|
||||
expected.put(3, third);
|
||||
session.update().put(book::contents, 3, third).where(book::id, eq(id)).sync();
|
||||
third = new SectionImpl("t-replace", 3);
|
||||
expected.put(3, third);
|
||||
session.update().put(book::contents, 3, third).where(book::id, eq(id)).sync();
|
||||
|
||||
actualMap = session.select(book::contents).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::contents).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// DELETE
|
||||
// DELETE
|
||||
|
||||
// remove single key
|
||||
// remove single key
|
||||
|
||||
expected.remove(3);
|
||||
session.update().put(book::contents, 3, null).where(book::id, eq(id)).sync();
|
||||
expected.remove(3);
|
||||
session.update().put(book::contents, 3, null).where(book::id, eq(id)).sync();
|
||||
|
||||
actualMap = session.select(book::contents).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
actualMap = session.select(book::contents).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
assertEqualMaps(expected, actualMap);
|
||||
|
||||
// remove full map
|
||||
// remove full map
|
||||
|
||||
session.update().set(book::contents, null).where(book::id, eq(id)).sync();
|
||||
session.update().set(book::contents, null).where(book::id, eq(id)).sync();
|
||||
|
||||
actualMap = session.select(book::contents).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualMap);
|
||||
actualMap = session.select(book::contents).where(book::id, eq(id)).sync().findFirst().get()._1;
|
||||
Assert.assertNull(actualMap);
|
||||
|
||||
// remove object
|
||||
// remove object
|
||||
|
||||
session.delete().where(book::id, eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
session.delete().where(book::id, eq(id)).sync();
|
||||
Long cnt = session.count().where(book::id, eq(id)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
|
||||
private void assertEqualMaps(Map<Integer, Section> expected, Map<Integer, Section> actual) {
|
||||
private void assertEqualMaps(Map<Integer, Section> expected, Map<Integer, Section> actual) {
|
||||
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
Assert.assertEquals(expected.size(), actual.size());
|
||||
|
||||
for (Integer i : expected.keySet()) {
|
||||
Section e = expected.get(i);
|
||||
Section a = actual.get(i);
|
||||
Assert.assertEquals(e.title(), a.title());
|
||||
Assert.assertEquals(e.page(), a.page());
|
||||
}
|
||||
}
|
||||
for (Integer i : expected.keySet()) {
|
||||
Section e = expected.get(i);
|
||||
Section a = actual.get(i);
|
||||
Assert.assertEquals(e.title(), a.title());
|
||||
Assert.assertEquals(e.page(), a.page());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,133 +18,112 @@ 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;
|
||||
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.core.UnitOfWork;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
|
||||
public class AndThenOrderTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static HelenusSession session;
|
||||
static HelenusSession session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().autoCreateDrop().get();
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().autoCreateDrop().get();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAndThenOrdering() throws Exception {
|
||||
List<String> q = new ArrayList<String>(5);
|
||||
UnitOfWork uow1, uow2, uow3, uow4, uow5;
|
||||
@Test
|
||||
public void testAndThenOrdering() throws Exception {
|
||||
List<String> q = new ArrayList<String>(5);
|
||||
UnitOfWork uow1, uow2, uow3, uow4, uow5;
|
||||
|
||||
uow5 = session.begin();
|
||||
uow3 = session.begin(uow5);
|
||||
uow1 = session.begin(uow3);
|
||||
uow1.commit()
|
||||
.andThen(
|
||||
() -> {
|
||||
q.add("1");
|
||||
});
|
||||
uow2 = session.begin(uow3);
|
||||
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");
|
||||
});
|
||||
uow5 = session.begin();
|
||||
uow3 = session.begin(uow5);
|
||||
uow1 = session.begin(uow3);
|
||||
uow1.commit().andThen(() -> {
|
||||
q.add("1");
|
||||
});
|
||||
uow2 = session.begin(uow3);
|
||||
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");
|
||||
});
|
||||
|
||||
System.out.println(q);
|
||||
Assert.assertTrue(
|
||||
Arrays.equals(q.toArray(new String[5]), new String[] {"1", "2", "3", "4", "5"}));
|
||||
}
|
||||
System.out.println(q);
|
||||
Assert.assertTrue(Arrays.equals(q.toArray(new String[5]), new String[]{"1", "2", "3", "4", "5"}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExceptionWithinAndThen() throws Exception {
|
||||
List<String> q = new ArrayList<String>(5);
|
||||
UnitOfWork uow1, uow2, uow3, uow4, uow5;
|
||||
@Test
|
||||
public void testExceptionWithinAndThen() throws Exception {
|
||||
List<String> q = new ArrayList<String>(5);
|
||||
UnitOfWork uow1, uow2, uow3, uow4, uow5;
|
||||
|
||||
uow5 = session.begin();
|
||||
uow4 = session.begin(uow5);
|
||||
try {
|
||||
uow3 = session.begin(uow4);
|
||||
uow1 = session.begin(uow3);
|
||||
uow1.commit()
|
||||
.andThen(
|
||||
() -> {
|
||||
q.add("1");
|
||||
});
|
||||
uow2 = session.begin(uow3);
|
||||
uow2.commit()
|
||||
.andThen(
|
||||
() -> {
|
||||
q.add("2");
|
||||
});
|
||||
uow3.commit()
|
||||
.andThen(
|
||||
() -> {
|
||||
q.add("3");
|
||||
});
|
||||
uow4.commit()
|
||||
.andThen(
|
||||
() -> {
|
||||
q.add("4");
|
||||
});
|
||||
throw new Exception();
|
||||
} catch (Exception e) {
|
||||
uow4.abort();
|
||||
}
|
||||
uow5.commit()
|
||||
.andThen(
|
||||
() -> {
|
||||
q.add("5");
|
||||
});
|
||||
uow5 = session.begin();
|
||||
uow4 = session.begin(uow5);
|
||||
try {
|
||||
uow3 = session.begin(uow4);
|
||||
uow1 = session.begin(uow3);
|
||||
uow1.commit().andThen(() -> {
|
||||
q.add("1");
|
||||
});
|
||||
uow2 = session.begin(uow3);
|
||||
uow2.commit().andThen(() -> {
|
||||
q.add("2");
|
||||
});
|
||||
uow3.commit().andThen(() -> {
|
||||
q.add("3");
|
||||
});
|
||||
uow4.commit().andThen(() -> {
|
||||
q.add("4");
|
||||
});
|
||||
throw new Exception();
|
||||
} catch (Exception e) {
|
||||
uow4.abort();
|
||||
}
|
||||
uow5.commit().andThen(() -> {
|
||||
q.add("5");
|
||||
});
|
||||
|
||||
System.out.println(q);
|
||||
Assert.assertTrue(q.isEmpty() == true);
|
||||
}
|
||||
System.out.println(q);
|
||||
Assert.assertTrue(q.isEmpty() == true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClosableWillAbortWhenNotCommitted() throws Exception {
|
||||
UnitOfWork unitOfWork;
|
||||
try (UnitOfWork uow = session.begin()) {
|
||||
unitOfWork = uow;
|
||||
Assert.assertFalse(uow.hasAborted());
|
||||
}
|
||||
Assert.assertTrue(unitOfWork.hasAborted());
|
||||
}
|
||||
@Test
|
||||
public void testClosableWillAbortWhenNotCommitted() throws Exception {
|
||||
UnitOfWork unitOfWork;
|
||||
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()) {
|
||||
unitOfWork = uow;
|
||||
Assert.assertFalse(uow.hasAborted());
|
||||
uow.commit()
|
||||
.andThen(
|
||||
() -> {
|
||||
Assert.assertFalse(uow.hasAborted());
|
||||
Assert.assertTrue(uow.hasCommitted());
|
||||
});
|
||||
}
|
||||
Assert.assertFalse(unitOfWork.hasAborted());
|
||||
Assert.assertTrue(unitOfWork.hasCommitted());
|
||||
}
|
||||
@Test
|
||||
public void testClosable() throws Exception {
|
||||
UnitOfWork unitOfWork;
|
||||
try (UnitOfWork uow = session.begin()) {
|
||||
unitOfWork = uow;
|
||||
Assert.assertFalse(uow.hasAborted());
|
||||
uow.commit().andThen(() -> {
|
||||
Assert.assertFalse(uow.hasAborted());
|
||||
Assert.assertTrue(uow.hasCommitted());
|
||||
});
|
||||
}
|
||||
Assert.assertFalse(unitOfWork.hasAborted());
|
||||
Assert.assertTrue(unitOfWork.hasCommitted());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,13 +15,16 @@
|
|||
*/
|
||||
package net.helenus.test.integration.core.unitofwork;
|
||||
|
||||
import com.datastax.driver.core.DataType.Name;
|
||||
import java.util.Set;
|
||||
import net.helenus.mapping.annotation.*;
|
||||
|
||||
import com.datastax.driver.core.DataType.Name;
|
||||
|
||||
import net.helenus.mapping.annotation.Types;
|
||||
import net.helenus.mapping.annotation.UDT;
|
||||
|
||||
@UDT
|
||||
public interface Directory extends FilesystemNode {
|
||||
|
||||
@Types.Set(Name.TIMEUUID)
|
||||
Set<FilesystemNode> inodes();
|
||||
@Types.Set(Name.TIMEUUID)
|
||||
Set<FilesystemNode> inodes();
|
||||
}
|
||||
|
|
|
@ -15,11 +15,12 @@
|
|||
*/
|
||||
package net.helenus.test.integration.core.unitofwork;
|
||||
|
||||
import net.helenus.mapping.annotation.*;
|
||||
import net.helenus.mapping.annotation.Column;
|
||||
import net.helenus.mapping.annotation.UDT;
|
||||
|
||||
@UDT
|
||||
public interface File extends FilesystemNode {
|
||||
|
||||
@Column
|
||||
byte[] data();
|
||||
@Column
|
||||
byte[] data();
|
||||
}
|
||||
|
|
|
@ -20,5 +20,5 @@ import net.helenus.mapping.annotation.UDT;
|
|||
@UDT
|
||||
public interface FileAttributes {
|
||||
|
||||
String owner();
|
||||
String owner();
|
||||
}
|
||||
|
|
|
@ -16,17 +16,21 @@
|
|||
package net.helenus.test.integration.core.unitofwork;
|
||||
|
||||
import java.util.UUID;
|
||||
import net.helenus.mapping.annotation.*;
|
||||
|
||||
import net.helenus.mapping.annotation.ClusteringColumn;
|
||||
import net.helenus.mapping.annotation.Column;
|
||||
import net.helenus.mapping.annotation.PartitionKey;
|
||||
import net.helenus.mapping.annotation.Table;
|
||||
|
||||
@Table("fs")
|
||||
public interface FilesystemNode {
|
||||
|
||||
@PartitionKey
|
||||
UUID inode();
|
||||
@PartitionKey
|
||||
UUID inode();
|
||||
|
||||
@ClusteringColumn
|
||||
String name();
|
||||
@ClusteringColumn
|
||||
String name();
|
||||
|
||||
@Column
|
||||
FileAttributes attr();
|
||||
@Column
|
||||
FileAttributes attr();
|
||||
}
|
||||
|
|
|
@ -17,220 +17,191 @@ 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 org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.datastax.driver.core.utils.UUIDs;
|
||||
|
||||
import net.bytebuddy.utility.RandomString;
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.core.UnitOfWork;
|
||||
import net.helenus.core.annotation.Cacheable;
|
||||
import net.helenus.mapping.annotation.*;
|
||||
import net.helenus.mapping.annotation.Constraints;
|
||||
import net.helenus.mapping.annotation.Index;
|
||||
import net.helenus.mapping.annotation.PartitionKey;
|
||||
import net.helenus.mapping.annotation.Table;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
@Table
|
||||
@Cacheable
|
||||
interface Widget {
|
||||
@PartitionKey
|
||||
UUID id();
|
||||
@PartitionKey
|
||||
UUID id();
|
||||
|
||||
@Index
|
||||
@Constraints.Distinct()
|
||||
String name();
|
||||
@Index
|
||||
@Constraints.Distinct()
|
||||
String name();
|
||||
}
|
||||
|
||||
public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static Widget widget;
|
||||
static HelenusSession session;
|
||||
static Widget widget;
|
||||
static HelenusSession session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Widget.class).autoCreateDrop().get();
|
||||
widget = session.dsl(Widget.class);
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Widget.class).autoCreateDrop().get();
|
||||
widget = session.dsl(Widget.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectAfterSelect() throws Exception {
|
||||
Widget w1, w2, w3;
|
||||
UUID key = UUIDs.timeBased();
|
||||
@Test
|
||||
public void testSelectAfterSelect() throws Exception {
|
||||
Widget w1, w2, w3, w4;
|
||||
UUID key = UUIDs.timeBased();
|
||||
|
||||
// This should inserted Widget, but not cache it.
|
||||
session
|
||||
.<Widget>insert(widget)
|
||||
.value(widget::id, key)
|
||||
.value(widget::name, RandomString.make(20))
|
||||
.sync();
|
||||
// This should inserted Widget, but not cache it.
|
||||
w1 = session.<Widget>insert(widget).value(widget::id, key).value(widget::name, RandomString.make(20)).sync();
|
||||
|
||||
try (UnitOfWork uow = session.begin()) {
|
||||
try (UnitOfWork uow = session.begin()) {
|
||||
|
||||
uow.setPurpose("testSelectAfterSelect");
|
||||
uow.setPurpose("testSelectAfterSelect");
|
||||
|
||||
// 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);
|
||||
// This should read from the database and return a Widget.
|
||||
w2 = 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);
|
||||
// This should read from the cache and get the same instance of a Widget.
|
||||
w3 = session.<Widget>select(widget).where(widget::id, eq(key)).single().sync(uow).orElse(null);
|
||||
|
||||
uow.commit()
|
||||
.andThen(
|
||||
() -> {
|
||||
Assert.assertEquals(w1, w2);
|
||||
});
|
||||
}
|
||||
uow.commit().andThen(() -> {
|
||||
Assert.assertEquals(w2, w3);
|
||||
});
|
||||
}
|
||||
|
||||
w3 = session.<Widget>select(widget).where(widget::name, eq(w1.name())).single().sync().orElse(null);
|
||||
Assert.assertEquals(w1, w3);
|
||||
}
|
||||
w4 = session.<Widget>select(widget).where(widget::name, eq(w1.name())).single().sync().orElse(null);
|
||||
Assert.assertEquals(w4, w1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectAfterNestedSelect() throws Exception {
|
||||
Widget w1, w2, w3, w4;
|
||||
UUID key1 = UUIDs.timeBased();
|
||||
UUID key2 = UUIDs.timeBased();
|
||||
@Test
|
||||
public void testSelectAfterNestedSelect() throws Exception {
|
||||
Widget w1, w2, w3, w4;
|
||||
UUID key1 = UUIDs.timeBased();
|
||||
UUID key2 = UUIDs.timeBased();
|
||||
|
||||
// This should inserted Widget, and not cache it in uow1.
|
||||
try (UnitOfWork uow1 = session.begin()) {
|
||||
w1 =
|
||||
session
|
||||
.<Widget>insert(widget)
|
||||
.value(widget::id, key1)
|
||||
.value(widget::name, RandomString.make(20))
|
||||
.sync(uow1);
|
||||
// This should inserted Widget, and not cache it in uow1.
|
||||
try (UnitOfWork uow1 = session.begin()) {
|
||||
w1 = session.<Widget>insert(widget).value(widget::id, key1).value(widget::name, RandomString.make(20))
|
||||
.sync(uow1);
|
||||
|
||||
try (UnitOfWork uow2 = session.begin(uow1)) {
|
||||
try (UnitOfWork uow2 = session.begin(uow1)) {
|
||||
|
||||
// This should read from uow1's cache and return the same Widget.
|
||||
w2 =
|
||||
session
|
||||
.<Widget>select(widget)
|
||||
.where(widget::id, eq(key1))
|
||||
.single()
|
||||
.sync(uow2)
|
||||
.orElse(null);
|
||||
// This should read from uow1's cache and return the same Widget.
|
||||
w2 = session.<Widget>select(widget).where(widget::id, eq(key1)).single().sync(uow2).orElse(null);
|
||||
|
||||
Assert.assertEquals(w1, w2);
|
||||
Assert.assertEquals(w1, w2);
|
||||
|
||||
w3 =
|
||||
session
|
||||
.<Widget>insert(widget)
|
||||
.value(widget::id, key2)
|
||||
.value(widget::name, RandomString.make(20))
|
||||
.sync(uow2);
|
||||
w3 = session.<Widget>insert(widget).value(widget::id, key2).value(widget::name, RandomString.make(20))
|
||||
.sync(uow2);
|
||||
|
||||
uow2.commit()
|
||||
.andThen(
|
||||
() -> {
|
||||
Assert.assertEquals(w1, w2);
|
||||
});
|
||||
}
|
||||
uow2.commit().andThen(() -> {
|
||||
Assert.assertEquals(w1, w2);
|
||||
});
|
||||
}
|
||||
|
||||
// This should read from the cache and get the same instance of a Widget.
|
||||
w4 =
|
||||
session
|
||||
.<Widget>select(widget)
|
||||
.where(widget::id, eq(key2))
|
||||
.single()
|
||||
.sync(uow1)
|
||||
.orElse(null);
|
||||
// This should read from the cache and get the same instance of a Widget.
|
||||
w4 = session.<Widget>select(widget).where(widget::id, eq(key2)).single().sync(uow1).orElse(null);
|
||||
|
||||
uow1.commit()
|
||||
.andThen(
|
||||
() -> {
|
||||
Assert.assertEquals(w3, w4);
|
||||
});
|
||||
}
|
||||
}
|
||||
uow1.commit().andThen(() -> {
|
||||
Assert.assertEquals(w3, w4);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectViaIndexAfterSelect() throws Exception {
|
||||
Widget w1, w2;
|
||||
UUID key = UUIDs.timeBased();
|
||||
@Test
|
||||
public void testSelectViaIndexAfterSelect() throws Exception {
|
||||
Widget w1, w2;
|
||||
UUID key = UUIDs.timeBased();
|
||||
|
||||
try (UnitOfWork uow = session.begin()) {
|
||||
// This should insert and cache Widget in the uow.
|
||||
session
|
||||
.<Widget>insert(widget)
|
||||
.value(widget::id, key)
|
||||
.value(widget::name, RandomString.make(20))
|
||||
.sync(uow);
|
||||
try (UnitOfWork uow = session.begin()) {
|
||||
// This should insert and cache Widget in the uow.
|
||||
session.<Widget>insert(widget).value(widget::id, key).value(widget::name, RandomString.make(20)).sync(uow);
|
||||
|
||||
// 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);
|
||||
// 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);
|
||||
|
||||
// This should read from the cache and get the same instance of a Widget.
|
||||
w2 =
|
||||
session
|
||||
.<Widget>select(widget)
|
||||
.where(widget::name, eq(w1.name()))
|
||||
.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::name, eq(w1.name())).single().sync(uow).orElse(null);
|
||||
|
||||
uow.commit()
|
||||
.andThen(
|
||||
() -> {
|
||||
Assert.assertEquals(w1, w2);
|
||||
});
|
||||
}
|
||||
}
|
||||
uow.commit().andThen(() -> {
|
||||
Assert.assertEquals(w1, w2);
|
||||
});
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void testSelectAfterDeleted() throws Exception {
|
||||
Widget w1, w2, w3, w4;
|
||||
UUID key = UUIDs.timeBased();
|
||||
|
||||
/*
|
||||
@Test
|
||||
public void testSelectAfterInsertProperlyCachesEntity() throws Exception {
|
||||
Widget w1, w2, w3, w4;
|
||||
UUID key = UUIDs.timeBased();
|
||||
// This should inserted Widget, but not cache it.
|
||||
w1 = session.<Widget>insert(widget).value(widget::id, key).value(widget::name, RandomString.make(20)).sync();
|
||||
|
||||
try (UnitOfWork uow = session.begin()) {
|
||||
try (UnitOfWork uow = session.begin()) {
|
||||
|
||||
// This should cache the inserted Widget.
|
||||
w1 = session.<Widget>insert(widget)
|
||||
.value(widget::id, key)
|
||||
.value(widget::name, RandomString.make(20))
|
||||
.sync(uow);
|
||||
// This should read from the database and return a Widget.
|
||||
w2 = 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);
|
||||
// This should remove the object from the cache.
|
||||
session.delete(widget).where(widget::id, eq(key))
|
||||
.sync(uow);
|
||||
|
||||
uow.commit()
|
||||
.andThen(() -> {
|
||||
Assert.assertEquals(w1, w2);
|
||||
});
|
||||
}
|
||||
// This should fail to read from the cache.
|
||||
w3 = session.<Widget>select(widget).where(widget::id, eq(key)).single()
|
||||
.sync(uow).orElse(null);
|
||||
|
||||
// This should read the widget from the session cache and maintain object identity.
|
||||
w3 = session.<Widget>select(widget)
|
||||
.where(widget::id, eq(key))
|
||||
.single()
|
||||
.sync()
|
||||
.orElse(null);
|
||||
Assert.assertEquals(w3, null);
|
||||
|
||||
Assert.assertEquals(w1, w3);
|
||||
uow.commit().andThen(() -> {
|
||||
Assert.assertEquals(w1, w2);
|
||||
Assert.assertEquals(w3, null);
|
||||
});
|
||||
}
|
||||
|
||||
// This should read the widget from the database, no object identity but values should match.
|
||||
w4 = session.<Widget>select(widget)
|
||||
.where(widget::id, eq(key))
|
||||
.ignoreCache()
|
||||
.single()
|
||||
.sync()
|
||||
.orElse(null);
|
||||
w4 = session.<Widget>select(widget).where(widget::name, eq(w1.name())).single()
|
||||
.sync().orElse(null);
|
||||
|
||||
Assert.assertNotEquals(w1, w4);
|
||||
Assert.assertTrue(w1.equals(w4));
|
||||
}
|
||||
*/
|
||||
Assert.assertEquals(w4, null);
|
||||
}
|
||||
|
||||
/*
|
||||
* @Test public void testSelectAfterInsertProperlyCachesEntity() throws
|
||||
* Exception { Widget w1, w2, w3, w4; UUID key = UUIDs.timeBased();
|
||||
*
|
||||
* try (UnitOfWork uow = session.begin()) {
|
||||
*
|
||||
* // This should cache the inserted Widget. w1 = session.<Widget>insert(widget)
|
||||
* .value(widget::id, key) .value(widget::name, RandomString.make(20))
|
||||
* .sync(uow);
|
||||
*
|
||||
* // 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);
|
||||
*
|
||||
* uow.commit() .andThen(() -> { Assert.assertEquals(w1, w2); }); }
|
||||
*
|
||||
* // This should read the widget from the session cache and maintain object
|
||||
* identity. w3 = session.<Widget>select(widget) .where(widget::id, eq(key))
|
||||
* .single() .sync() .orElse(null);
|
||||
*
|
||||
* Assert.assertEquals(w1, w3);
|
||||
*
|
||||
* // This should read the widget from the database, no object identity but
|
||||
* values should match. w4 = session.<Widget>select(widget) .where(widget::id,
|
||||
* eq(key)) .ignoreCache() .single() .sync() .orElse(null);
|
||||
*
|
||||
* Assert.assertNotEquals(w1, w4); Assert.assertTrue(w1.equals(w4)); }
|
||||
*/
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
package net.helenus.test.integration.core.usertype;
|
||||
|
||||
import com.datastax.driver.core.UDTValue;
|
||||
|
||||
import net.helenus.mapping.annotation.Column;
|
||||
import net.helenus.mapping.annotation.PartitionKey;
|
||||
import net.helenus.mapping.annotation.Table;
|
||||
|
@ -24,13 +25,13 @@ import net.helenus.mapping.annotation.Types;
|
|||
@Table
|
||||
public interface Account {
|
||||
|
||||
@PartitionKey(ordinal = 0)
|
||||
long id();
|
||||
@PartitionKey(ordinal = 0)
|
||||
long id();
|
||||
|
||||
@Column
|
||||
Address address();
|
||||
@Column
|
||||
Address address();
|
||||
|
||||
@Types.UDT("address")
|
||||
@Column
|
||||
UDTValue addressNoMapping();
|
||||
@Types.UDT("address")
|
||||
@Column
|
||||
UDTValue addressNoMapping();
|
||||
}
|
||||
|
|
|
@ -15,8 +15,10 @@
|
|||
*/
|
||||
package net.helenus.test.integration.core.usertype;
|
||||
|
||||
import com.datastax.driver.core.DataType;
|
||||
import java.util.Set;
|
||||
|
||||
import com.datastax.driver.core.DataType;
|
||||
|
||||
import net.helenus.mapping.annotation.Column;
|
||||
import net.helenus.mapping.annotation.Types;
|
||||
import net.helenus.mapping.annotation.UDT;
|
||||
|
@ -24,19 +26,19 @@ import net.helenus.mapping.annotation.UDT;
|
|||
@UDT("address")
|
||||
public interface Address {
|
||||
|
||||
@Column(ordinal = 0, value = "line_1")
|
||||
String street();
|
||||
@Column(ordinal = 0, value = "line_1")
|
||||
String street();
|
||||
|
||||
@Column
|
||||
String city();
|
||||
@Column
|
||||
String city();
|
||||
|
||||
@Column
|
||||
int zip();
|
||||
@Column
|
||||
int zip();
|
||||
|
||||
@Column
|
||||
String country();
|
||||
@Column
|
||||
String country();
|
||||
|
||||
@Column
|
||||
@Types.Set(DataType.Name.TEXT)
|
||||
Set<String> phones();
|
||||
@Column
|
||||
@Types.Set(DataType.Name.TEXT)
|
||||
Set<String> phones();
|
||||
}
|
||||
|
|
|
@ -6,6 +6,6 @@ import net.helenus.mapping.annotation.UDT;
|
|||
@UDT
|
||||
public interface AddressInformation {
|
||||
|
||||
@Column
|
||||
Address address();
|
||||
@Column
|
||||
Address address();
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package net.helenus.test.integration.core.usertype;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import net.helenus.mapping.annotation.Column;
|
||||
import net.helenus.mapping.annotation.PartitionKey;
|
||||
import net.helenus.mapping.annotation.Table;
|
||||
|
@ -8,9 +9,9 @@ import net.helenus.mapping.annotation.Table;
|
|||
@Table
|
||||
public interface Customer {
|
||||
|
||||
@PartitionKey
|
||||
UUID id();
|
||||
@PartitionKey
|
||||
UUID id();
|
||||
|
||||
@Column
|
||||
AddressInformation addressInformation();
|
||||
@Column
|
||||
AddressInformation addressInformation();
|
||||
}
|
||||
|
|
|
@ -15,118 +15,111 @@
|
|||
*/
|
||||
package net.helenus.test.integration.core.usertype;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.core.Query;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.core.Query;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
|
||||
public class InnerUserDefinedTypeTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static Customer customer;
|
||||
static AddressInformation addressInformation;
|
||||
static Customer customer;
|
||||
static AddressInformation addressInformation;
|
||||
|
||||
static HelenusSession session;
|
||||
static HelenusSession session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
Helenus.clearDslCache();
|
||||
session = Helenus.init(getSession()).showCql().add(Customer.class).autoCreateDrop().get();
|
||||
customer = Helenus.dsl(Customer.class);
|
||||
addressInformation = Helenus.dsl(AddressInformation.class);
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
Helenus.clearDslCache();
|
||||
session = Helenus.init(getSession()).showCql().add(Customer.class).autoCreateDrop().get();
|
||||
customer = Helenus.dsl(Customer.class);
|
||||
addressInformation = Helenus.dsl(AddressInformation.class);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterTest() {
|
||||
session.getSession().execute("DROP TABLE IF EXISTS customer;");
|
||||
session.getSession().execute("DROP TYPE IF EXISTS address_information;");
|
||||
// SchemaUtil.dropUserType(session.getSessionRepository().findUserType("address_information")), true);
|
||||
}
|
||||
@AfterClass
|
||||
public static void afterTest() {
|
||||
session.getSession().execute("DROP TABLE IF EXISTS customer;");
|
||||
session.getSession().execute("DROP TYPE IF EXISTS address_information;");
|
||||
// SchemaUtil.dropUserType(session.getSessionRepository().findUserType("address_information")),
|
||||
// true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(addressInformation);
|
||||
System.out.println(customer);
|
||||
}
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(addressInformation);
|
||||
System.out.println(customer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCrud() throws TimeoutException {
|
||||
@Test
|
||||
public void testCrud() throws TimeoutException {
|
||||
|
||||
UUID id = UUID.randomUUID();
|
||||
UUID id = UUID.randomUUID();
|
||||
|
||||
Address a =
|
||||
new Address() {
|
||||
Address a = new Address() {
|
||||
|
||||
@Override
|
||||
public String street() {
|
||||
return "1 st";
|
||||
}
|
||||
@Override
|
||||
public String street() {
|
||||
return "1 st";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String city() {
|
||||
return "San Jose";
|
||||
}
|
||||
@Override
|
||||
public String city() {
|
||||
return "San Jose";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int zip() {
|
||||
return 95131;
|
||||
}
|
||||
@Override
|
||||
public int zip() {
|
||||
return 95131;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String country() {
|
||||
return "USA";
|
||||
}
|
||||
@Override
|
||||
public String country() {
|
||||
return "USA";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> phones() {
|
||||
return Sets.newHashSet("14080000000");
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public Set<String> phones() {
|
||||
return Sets.newHashSet("14080000000");
|
||||
}
|
||||
};
|
||||
|
||||
AddressInformation ai =
|
||||
new AddressInformation() {
|
||||
AddressInformation ai = new AddressInformation() {
|
||||
|
||||
@Override
|
||||
public Address address() {
|
||||
return a;
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public Address address() {
|
||||
return a;
|
||||
}
|
||||
};
|
||||
|
||||
session.insert().value(customer::id, id).value(customer::addressInformation, ai).sync();
|
||||
session.insert().value(customer::id, id).value(customer::addressInformation, ai).sync();
|
||||
|
||||
String cql =
|
||||
session
|
||||
.update()
|
||||
.set(customer.addressInformation().address()::street, "3 st")
|
||||
.where(customer::id, Query.eq(id))
|
||||
.cql();
|
||||
String cql = session.update().set(customer.addressInformation().address()::street, "3 st")
|
||||
.where(customer::id, Query.eq(id)).cql();
|
||||
|
||||
//TODO: System.out.println("At the time when this test was written Cassandra did not support queries like this: " + cql);
|
||||
// TODO: System.out.println("At the time when this test was written Cassandra
|
||||
// did not support queries like this: " + cql);
|
||||
|
||||
session.update().set(customer::addressInformation, ai).where(customer::id, Query.eq(id)).sync();
|
||||
session.update().set(customer::addressInformation, ai).where(customer::id, Query.eq(id)).sync();
|
||||
|
||||
String street =
|
||||
session
|
||||
.select(customer.addressInformation().address()::street)
|
||||
.where(customer::id, Query.eq(id))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
String street = session.select(customer.addressInformation().address()::street)
|
||||
.where(customer::id, Query.eq(id)).sync().findFirst().get()._1;
|
||||
|
||||
Assert.assertEquals("1 st", street);
|
||||
Assert.assertEquals("1 st", street);
|
||||
|
||||
session.delete().where(customer::id, Query.eq(id)).sync();
|
||||
session.delete().where(customer::id, Query.eq(id)).sync();
|
||||
|
||||
Long cnt = session.count().where(customer::id, Query.eq(id)).sync();
|
||||
Long cnt = session.count().where(customer::id, Query.eq(id)).sync();
|
||||
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,242 +15,204 @@
|
|||
*/
|
||||
package net.helenus.test.integration.core.usertype;
|
||||
|
||||
import com.datastax.driver.core.UDTValue;
|
||||
import com.datastax.driver.core.UserType;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.core.Query;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.datastax.driver.core.UDTValue;
|
||||
import com.datastax.driver.core.UserType;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.HelenusSession;
|
||||
import net.helenus.core.Query;
|
||||
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
|
||||
public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static Address address;
|
||||
static Account account;
|
||||
static Address address;
|
||||
static Account account;
|
||||
|
||||
static HelenusSession session;
|
||||
static HelenusSession session;
|
||||
|
||||
public static class AccountImpl implements Account {
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Account.class).autoCreateDrop().get();
|
||||
address = Helenus.dsl(Address.class);
|
||||
account = Helenus.dsl(Account.class);
|
||||
}
|
||||
|
||||
long id;
|
||||
Address address;
|
||||
UDTValue addressNoMapping;
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(address);
|
||||
System.out.println(account);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long id() {
|
||||
return id;
|
||||
}
|
||||
@Test
|
||||
public void testMappingCRUID() throws TimeoutException {
|
||||
|
||||
@Override
|
||||
public Address address() {
|
||||
return address;
|
||||
}
|
||||
AddressImpl addr = new AddressImpl();
|
||||
addr.street = "1 st";
|
||||
addr.city = "San Jose";
|
||||
|
||||
@Override
|
||||
public UDTValue addressNoMapping() {
|
||||
return addressNoMapping;
|
||||
}
|
||||
}
|
||||
AccountImpl acc = new AccountImpl();
|
||||
acc.id = 123L;
|
||||
acc.address = addr;
|
||||
|
||||
public static class AddressImpl implements Address {
|
||||
// CREATE
|
||||
|
||||
String street;
|
||||
String city;
|
||||
int zip;
|
||||
String country;
|
||||
Set<String> phones;
|
||||
session.upsert(acc).sync();
|
||||
|
||||
@Override
|
||||
public String street() {
|
||||
return street;
|
||||
}
|
||||
// READ
|
||||
|
||||
@Override
|
||||
public String city() {
|
||||
return city;
|
||||
}
|
||||
String streetName = session.select(account.address()::street).where(account::id, Query.eq(123L)).sync()
|
||||
.findFirst().get()._1;
|
||||
|
||||
@Override
|
||||
public int zip() {
|
||||
return zip;
|
||||
}
|
||||
Assert.assertEquals("1 st", streetName);
|
||||
|
||||
@Override
|
||||
public String country() {
|
||||
return country;
|
||||
}
|
||||
// UPDATE
|
||||
|
||||
@Override
|
||||
public Set<String> phones() {
|
||||
return phones;
|
||||
}
|
||||
}
|
||||
AddressImpl expected = new AddressImpl();
|
||||
expected.street = "2 st";
|
||||
expected.city = "San Francisco";
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Account.class).autoCreateDrop().get();
|
||||
address = Helenus.dsl(Address.class);
|
||||
account = Helenus.dsl(Account.class);
|
||||
}
|
||||
session.update().set(account::address, expected).where(account::id, Query.eq(123L)).sync();
|
||||
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(address);
|
||||
System.out.println(account);
|
||||
}
|
||||
Address actual = session.select(account::address).where(account::id, Query.eq(123L)).sync().findFirst()
|
||||
.get()._1;
|
||||
|
||||
@Test
|
||||
public void testMappingCRUID() throws TimeoutException {
|
||||
Assert.assertEquals(expected.street(), actual.street());
|
||||
Assert.assertEquals(expected.city(), actual.city());
|
||||
Assert.assertNull(actual.country());
|
||||
Assert.assertEquals(0, actual.zip());
|
||||
|
||||
AddressImpl addr = new AddressImpl();
|
||||
addr.street = "1 st";
|
||||
addr.city = "San Jose";
|
||||
// INSERT using UPDATE
|
||||
session.update().set(account::address, null).where(account::id, Query.eq(123L)).sync();
|
||||
|
||||
AccountImpl acc = new AccountImpl();
|
||||
acc.id = 123L;
|
||||
acc.address = addr;
|
||||
Address adrNull = session.select(account::address).where(account::id, Query.eq(123L)).sync().findFirst()
|
||||
.get()._1;
|
||||
Assert.assertNull(adrNull);
|
||||
|
||||
// CREATE
|
||||
// DELETE
|
||||
|
||||
session.upsert(acc).sync();
|
||||
session.delete().where(account::id, Query.eq(123L)).sync();
|
||||
|
||||
// READ
|
||||
Long cnt = session.count().where(account::id, Query.eq(123L)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
|
||||
String streetName =
|
||||
session
|
||||
.select(account.address()::street)
|
||||
.where(account::id, Query.eq(123L))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
@Test
|
||||
public void testNoMapping() throws TimeoutException {
|
||||
|
||||
Assert.assertEquals("1 st", streetName);
|
||||
String ks = getSession().getLoggedKeyspace();
|
||||
UserType addressType = getSession().getCluster().getMetadata().getKeyspace(ks).getUserType("address");
|
||||
|
||||
// UPDATE
|
||||
UDTValue addressNoMapping = addressType.newValue();
|
||||
addressNoMapping.setString("line_1", "1st street");
|
||||
addressNoMapping.setString("city", "San Jose");
|
||||
|
||||
AddressImpl expected = new AddressImpl();
|
||||
expected.street = "2 st";
|
||||
expected.city = "San Francisco";
|
||||
AccountImpl acc = new AccountImpl();
|
||||
acc.id = 777L;
|
||||
acc.addressNoMapping = addressNoMapping;
|
||||
|
||||
session.update().set(account::address, expected).where(account::id, Query.eq(123L)).sync();
|
||||
// CREATE
|
||||
|
||||
Address actual =
|
||||
session
|
||||
.select(account::address)
|
||||
.where(account::id, Query.eq(123L))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
session.upsert(acc).sync();
|
||||
|
||||
Assert.assertEquals(expected.street(), actual.street());
|
||||
Assert.assertEquals(expected.city(), actual.city());
|
||||
Assert.assertNull(actual.country());
|
||||
Assert.assertEquals(0, actual.zip());
|
||||
// READ
|
||||
|
||||
// INSERT using UPDATE
|
||||
session.update().set(account::address, null).where(account::id, Query.eq(123L)).sync();
|
||||
UDTValue found = session.select(account::addressNoMapping).where(account::id, Query.eq(777L)).sync().findFirst()
|
||||
.get()._1;
|
||||
|
||||
Address adrNull =
|
||||
session
|
||||
.select(account::address)
|
||||
.where(account::id, Query.eq(123L))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
Assert.assertNull(adrNull);
|
||||
Assert.assertEquals(addressNoMapping.getType(), found.getType());
|
||||
Assert.assertEquals(addressNoMapping.getString("line_1"), found.getString("line_1"));
|
||||
Assert.assertEquals(addressNoMapping.getString("city"), found.getString("city"));
|
||||
|
||||
// DELETE
|
||||
// UPDATE
|
||||
|
||||
session.delete().where(account::id, Query.eq(123L)).sync();
|
||||
addressNoMapping = addressType.newValue();
|
||||
addressNoMapping.setString("line_1", "Market street");
|
||||
addressNoMapping.setString("city", "San Francisco");
|
||||
|
||||
Long cnt = session.count().where(account::id, Query.eq(123L)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
session.update().set(account::addressNoMapping, addressNoMapping).where(account::id, Query.eq(777L)).sync();
|
||||
|
||||
@Test
|
||||
public void testNoMapping() throws TimeoutException {
|
||||
found = session.select(account::addressNoMapping).where(account::id, Query.eq(777L)).sync().findFirst()
|
||||
.get()._1;
|
||||
|
||||
String ks = getSession().getLoggedKeyspace();
|
||||
UserType addressType =
|
||||
getSession().getCluster().getMetadata().getKeyspace(ks).getUserType("address");
|
||||
Assert.assertEquals(addressNoMapping.getType(), found.getType());
|
||||
Assert.assertEquals(addressNoMapping.getString("line_1"), found.getString("line_1"));
|
||||
Assert.assertEquals(addressNoMapping.getString("city"), found.getString("city"));
|
||||
|
||||
UDTValue addressNoMapping = addressType.newValue();
|
||||
addressNoMapping.setString("line_1", "1st street");
|
||||
addressNoMapping.setString("city", "San Jose");
|
||||
// INSERT using UPDATE
|
||||
session.update().set(account::addressNoMapping, null).where(account::id, Query.eq(777L)).sync();
|
||||
|
||||
AccountImpl acc = new AccountImpl();
|
||||
acc.id = 777L;
|
||||
acc.addressNoMapping = addressNoMapping;
|
||||
found = session.select(account::addressNoMapping).where(account::id, Query.eq(777L)).sync().findFirst()
|
||||
.get()._1;
|
||||
Assert.assertNull(found);
|
||||
|
||||
// CREATE
|
||||
// DELETE
|
||||
|
||||
session.upsert(acc).sync();
|
||||
session.delete().where(account::id, Query.eq(777L)).sync();
|
||||
|
||||
// READ
|
||||
Long cnt = session.count().where(account::id, Query.eq(777L)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
|
||||
UDTValue found =
|
||||
session
|
||||
.select(account::addressNoMapping)
|
||||
.where(account::id, Query.eq(777L))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
public static class AccountImpl implements Account {
|
||||
|
||||
Assert.assertEquals(addressNoMapping.getType(), found.getType());
|
||||
Assert.assertEquals(addressNoMapping.getString("line_1"), found.getString("line_1"));
|
||||
Assert.assertEquals(addressNoMapping.getString("city"), found.getString("city"));
|
||||
long id;
|
||||
Address address;
|
||||
UDTValue addressNoMapping;
|
||||
|
||||
// UPDATE
|
||||
@Override
|
||||
public long id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
addressNoMapping = addressType.newValue();
|
||||
addressNoMapping.setString("line_1", "Market street");
|
||||
addressNoMapping.setString("city", "San Francisco");
|
||||
@Override
|
||||
public Address address() {
|
||||
return address;
|
||||
}
|
||||
|
||||
session
|
||||
.update()
|
||||
.set(account::addressNoMapping, addressNoMapping)
|
||||
.where(account::id, Query.eq(777L))
|
||||
.sync();
|
||||
@Override
|
||||
public UDTValue addressNoMapping() {
|
||||
return addressNoMapping;
|
||||
}
|
||||
}
|
||||
|
||||
found =
|
||||
session
|
||||
.select(account::addressNoMapping)
|
||||
.where(account::id, Query.eq(777L))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
public static class AddressImpl implements Address {
|
||||
|
||||
Assert.assertEquals(addressNoMapping.getType(), found.getType());
|
||||
Assert.assertEquals(addressNoMapping.getString("line_1"), found.getString("line_1"));
|
||||
Assert.assertEquals(addressNoMapping.getString("city"), found.getString("city"));
|
||||
String street;
|
||||
String city;
|
||||
int zip;
|
||||
String country;
|
||||
Set<String> phones;
|
||||
|
||||
// INSERT using UPDATE
|
||||
session.update().set(account::addressNoMapping, null).where(account::id, Query.eq(777L)).sync();
|
||||
@Override
|
||||
public String street() {
|
||||
return street;
|
||||
}
|
||||
|
||||
found =
|
||||
session
|
||||
.select(account::addressNoMapping)
|
||||
.where(account::id, Query.eq(777L))
|
||||
.sync()
|
||||
.findFirst()
|
||||
.get()
|
||||
._1;
|
||||
Assert.assertNull(found);
|
||||
@Override
|
||||
public String city() {
|
||||
return city;
|
||||
}
|
||||
|
||||
// DELETE
|
||||
@Override
|
||||
public int zip() {
|
||||
return zip;
|
||||
}
|
||||
|
||||
session.delete().where(account::id, Query.eq(777L)).sync();
|
||||
@Override
|
||||
public String country() {
|
||||
return country;
|
||||
}
|
||||
|
||||
Long cnt = session.count().where(account::id, Query.eq(777L)).sync();
|
||||
Assert.assertEquals(Long.valueOf(0), cnt);
|
||||
}
|
||||
@Override
|
||||
public Set<String> phones() {
|
||||
return phones;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,28 +2,25 @@ package net.helenus.test.integration.core.views;
|
|||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
import net.helenus.mapping.annotation.ClusteringColumn;
|
||||
import net.helenus.mapping.annotation.CoveringIndex;
|
||||
import net.helenus.mapping.annotation.PartitionKey;
|
||||
import net.helenus.mapping.annotation.Table;
|
||||
|
||||
@Table
|
||||
@CoveringIndex(
|
||||
name = "cyclist_mv",
|
||||
covering = {"age", "birthday", "country"},
|
||||
partitionKeys = {"age", "cid"},
|
||||
clusteringColumns = {}
|
||||
)
|
||||
@CoveringIndex(name = "cyclist_mv", covering = {"age", "birthday", "country"}, partitionKeys = {"age",
|
||||
"cid"}, clusteringColumns = {})
|
||||
public interface Cyclist {
|
||||
@ClusteringColumn
|
||||
UUID cid();
|
||||
@ClusteringColumn
|
||||
UUID cid();
|
||||
|
||||
String name();
|
||||
String name();
|
||||
|
||||
@PartitionKey
|
||||
int age();
|
||||
@PartitionKey
|
||||
int age();
|
||||
|
||||
Date birthday();
|
||||
Date birthday();
|
||||
|
||||
String country();
|
||||
String country();
|
||||
}
|
||||
|
|
|
@ -2,19 +2,23 @@ package net.helenus.test.integration.core.views;
|
|||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
import net.helenus.mapping.OrderingDirection;
|
||||
import net.helenus.mapping.annotation.*;
|
||||
import net.helenus.mapping.annotation.ClusteringColumn;
|
||||
import net.helenus.mapping.annotation.Index;
|
||||
import net.helenus.mapping.annotation.MaterializedView;
|
||||
import net.helenus.mapping.annotation.PartitionKey;
|
||||
|
||||
@MaterializedView
|
||||
public interface CyclistsByAge extends Cyclist {
|
||||
@PartitionKey
|
||||
UUID cid();
|
||||
@PartitionKey
|
||||
UUID cid();
|
||||
|
||||
@ClusteringColumn(ordering = OrderingDirection.ASC)
|
||||
int age();
|
||||
@ClusteringColumn(ordering = OrderingDirection.ASC)
|
||||
int age();
|
||||
|
||||
Date birthday();
|
||||
Date birthday();
|
||||
|
||||
@Index
|
||||
String country();
|
||||
@Index
|
||||
String country();
|
||||
}
|
||||
|
|
|
@ -19,13 +19,16 @@ import static net.helenus.core.Query.eq;
|
|||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
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 org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
// See: https://docs.datastax.com/en/cql/3.3/cql/cql_using/useCreateMV.html
|
||||
// https://docs.datastax.com/en/cql/3.3/cql/cql_reference/cqlCreateMaterializedView.html
|
||||
|
@ -33,45 +36,35 @@ import org.junit.Test;
|
|||
// https://cassandra-zone.com/materialized-views/
|
||||
public class MaterializedViewTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
static Cyclist cyclist;
|
||||
static HelenusSession session;
|
||||
static Cyclist cyclist;
|
||||
static HelenusSession session;
|
||||
|
||||
static Date dateFromString(String dateInString) {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
|
||||
try {
|
||||
return formatter.parse(dateInString);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
static Date dateFromString(String dateInString) {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
|
||||
try {
|
||||
return formatter.parse(dateInString);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session =
|
||||
Helenus.init(getSession())
|
||||
.showCql()
|
||||
.add(Cyclist.class)
|
||||
.add(CyclistsByAge.class)
|
||||
.autoCreateDrop()
|
||||
.get();
|
||||
cyclist = session.dsl(Cyclist.class);
|
||||
@BeforeClass
|
||||
public static void beforeTest() {
|
||||
session = Helenus.init(getSession()).showCql().add(Cyclist.class).add(CyclistsByAge.class).autoCreateDrop()
|
||||
.get();
|
||||
cyclist = session.dsl(Cyclist.class);
|
||||
|
||||
//try {
|
||||
session
|
||||
.insert(cyclist)
|
||||
.value(cyclist::cid, UUID.randomUUID())
|
||||
.value(cyclist::age, 18)
|
||||
.value(cyclist::birthday, dateFromString("1997-02-08"))
|
||||
.value(cyclist::country, "Netherlands")
|
||||
.value(cyclist::name, "Pascal EENKHOORN")
|
||||
.sync();
|
||||
//} catch (TimeoutException e) {
|
||||
//}
|
||||
}
|
||||
// try {
|
||||
session.insert(cyclist).value(cyclist::cid, UUID.randomUUID()).value(cyclist::age, 18)
|
||||
.value(cyclist::birthday, dateFromString("1997-02-08")).value(cyclist::country, "Netherlands")
|
||||
.value(cyclist::name, "Pascal EENKHOORN").sync();
|
||||
// } catch (TimeoutException e) {
|
||||
// }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMv() throws TimeoutException {
|
||||
session.select(Cyclist.class).from(CyclistsByAge.class).where(cyclist::age, eq(18)).sync();
|
||||
}
|
||||
@Test
|
||||
public void testMv() throws TimeoutException {
|
||||
session.select(Cyclist.class).from(CyclistsByAge.class).where(cyclist::age, eq(18)).sync();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,74 +16,75 @@
|
|||
package net.helenus.test.performance.core.dsl;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import net.helenus.core.reflect.MapExportable;
|
||||
|
||||
public final class CachedElevatorImpl implements Elevator, MapExportable {
|
||||
|
||||
private final Map<String, Object> backingMap;
|
||||
private final Map<String, Object> backingMap;
|
||||
|
||||
private int mask = 0;
|
||||
private int mask = 0;
|
||||
|
||||
private int height;
|
||||
private Double price;
|
||||
private String name;
|
||||
private int height;
|
||||
private Double price;
|
||||
private String name;
|
||||
|
||||
public CachedElevatorImpl(Map<String, Object> backingMap) {
|
||||
this.backingMap = backingMap;
|
||||
}
|
||||
public CachedElevatorImpl(Map<String, Object> backingMap) {
|
||||
this.backingMap = backingMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int height() {
|
||||
@Override
|
||||
public int height() {
|
||||
|
||||
if ((mask & 1) == 0) {
|
||||
if ((mask & 1) == 0) {
|
||||
|
||||
Object obj = backingMap.get("height");
|
||||
if (obj != null) {
|
||||
height = ((Integer) obj).intValue();
|
||||
}
|
||||
Object obj = backingMap.get("height");
|
||||
if (obj != null) {
|
||||
height = ((Integer) obj).intValue();
|
||||
}
|
||||
|
||||
mask &= 1;
|
||||
}
|
||||
return height;
|
||||
}
|
||||
mask &= 1;
|
||||
}
|
||||
return height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double price() {
|
||||
@Override
|
||||
public Double price() {
|
||||
|
||||
if ((mask & 2) == 0) {
|
||||
if ((mask & 2) == 0) {
|
||||
|
||||
Object obj = backingMap.get("price");
|
||||
if (obj != null) {
|
||||
price = (Double) obj;
|
||||
}
|
||||
Object obj = backingMap.get("price");
|
||||
if (obj != null) {
|
||||
price = (Double) obj;
|
||||
}
|
||||
|
||||
mask &= 2;
|
||||
}
|
||||
return price;
|
||||
}
|
||||
mask &= 2;
|
||||
}
|
||||
return price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
@Override
|
||||
public String name() {
|
||||
|
||||
if ((mask & 4) == 0) {
|
||||
if ((mask & 4) == 0) {
|
||||
|
||||
Object obj = backingMap.get("name");
|
||||
if (obj != null) {
|
||||
name = (String) obj;
|
||||
}
|
||||
Object obj = backingMap.get("name");
|
||||
if (obj != null) {
|
||||
name = (String) obj;
|
||||
}
|
||||
|
||||
mask &= 4;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
mask &= 4;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return backingMap;
|
||||
}
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return backingMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return backingMap.toString();
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return backingMap.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,9 +17,9 @@ package net.helenus.test.performance.core.dsl;
|
|||
|
||||
public interface Elevator {
|
||||
|
||||
int height();
|
||||
int height();
|
||||
|
||||
Double price();
|
||||
Double price();
|
||||
|
||||
String name();
|
||||
String name();
|
||||
}
|
||||
|
|
|
@ -16,50 +16,51 @@
|
|||
package net.helenus.test.performance.core.dsl;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import net.helenus.core.reflect.MapExportable;
|
||||
|
||||
public final class ElevatorImpl implements Elevator, MapExportable {
|
||||
|
||||
private final Map<String, Object> backingMap;
|
||||
private final Map<String, Object> backingMap;
|
||||
|
||||
public ElevatorImpl(Map<String, Object> backingMap) {
|
||||
this.backingMap = backingMap;
|
||||
}
|
||||
public ElevatorImpl(Map<String, Object> backingMap) {
|
||||
this.backingMap = backingMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int height() {
|
||||
Object obj = backingMap.get("height");
|
||||
if (obj != null) {
|
||||
return ((Integer) obj).intValue();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@Override
|
||||
public int height() {
|
||||
Object obj = backingMap.get("height");
|
||||
if (obj != null) {
|
||||
return ((Integer) obj).intValue();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double price() {
|
||||
Object obj = backingMap.get("price");
|
||||
if (obj != null) {
|
||||
return (Double) obj;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Double price() {
|
||||
Object obj = backingMap.get("price");
|
||||
if (obj != null) {
|
||||
return (Double) obj;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
Object obj = backingMap.get("name");
|
||||
if (obj != null) {
|
||||
return (String) obj;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public String name() {
|
||||
Object obj = backingMap.get("name");
|
||||
if (obj != null) {
|
||||
return (String) obj;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return backingMap;
|
||||
}
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return backingMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return backingMap.toString();
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return backingMap.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,99 +17,101 @@ package net.helenus.test.performance.core.dsl;
|
|||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import net.helenus.core.Helenus;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
|
||||
public class MappingTest {
|
||||
|
||||
static Map<String, Object> fixture;
|
||||
static Map<String, Object> fixture;
|
||||
|
||||
static {
|
||||
fixture = new HashMap<String, Object>();
|
||||
fixture.put("height", Integer.valueOf(55));
|
||||
fixture.put("price", Double.valueOf(44.99));
|
||||
fixture.put("name", "first");
|
||||
}
|
||||
static {
|
||||
fixture = new HashMap<String, Object>();
|
||||
fixture.put("height", Integer.valueOf(55));
|
||||
fixture.put("price", Double.valueOf(44.99));
|
||||
fixture.put("name", "first");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReflectionConstructor() {
|
||||
@Test
|
||||
public void testReflectionConstructor() {
|
||||
|
||||
long t0 = System.currentTimeMillis();
|
||||
long t0 = System.currentTimeMillis();
|
||||
|
||||
for (int i = 0; i != 100000; ++i) {
|
||||
Helenus.map(Elevator.class, fixture);
|
||||
}
|
||||
for (int i = 0; i != 100000; ++i) {
|
||||
Helenus.map(Elevator.class, fixture);
|
||||
}
|
||||
|
||||
long t1 = System.currentTimeMillis() - t0;
|
||||
long t1 = System.currentTimeMillis() - t0;
|
||||
|
||||
System.out.println("ReflectionConstructor = " + t1);
|
||||
}
|
||||
System.out.println("ReflectionConstructor = " + t1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReflectionAccess() {
|
||||
@Test
|
||||
public void testReflectionAccess() {
|
||||
|
||||
long t0 = System.currentTimeMillis();
|
||||
long t0 = System.currentTimeMillis();
|
||||
|
||||
Elevator elevator = Helenus.map(Elevator.class, fixture);
|
||||
Elevator elevator = Helenus.map(Elevator.class, fixture);
|
||||
|
||||
for (int i = 0; i != 100000; ++i) {
|
||||
elevator.height();
|
||||
elevator.price();
|
||||
elevator.name();
|
||||
}
|
||||
for (int i = 0; i != 100000; ++i) {
|
||||
elevator.height();
|
||||
elevator.price();
|
||||
elevator.name();
|
||||
}
|
||||
|
||||
long t1 = System.currentTimeMillis() - t0;
|
||||
long t1 = System.currentTimeMillis() - t0;
|
||||
|
||||
System.out.println("ReflectionAccess = " + t1);
|
||||
}
|
||||
System.out.println("ReflectionAccess = " + t1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavaAccess() {
|
||||
@Test
|
||||
public void testJavaAccess() {
|
||||
|
||||
long t0 = System.currentTimeMillis();
|
||||
long t0 = System.currentTimeMillis();
|
||||
|
||||
Elevator elevator = new ElevatorImpl(fixture);
|
||||
Elevator elevator = new ElevatorImpl(fixture);
|
||||
|
||||
for (int i = 0; i != 100000; ++i) {
|
||||
elevator.height();
|
||||
elevator.price();
|
||||
elevator.name();
|
||||
}
|
||||
for (int i = 0; i != 100000; ++i) {
|
||||
elevator.height();
|
||||
elevator.price();
|
||||
elevator.name();
|
||||
}
|
||||
|
||||
long t1 = System.currentTimeMillis() - t0;
|
||||
long t1 = System.currentTimeMillis() - t0;
|
||||
|
||||
System.out.println("JavaAccess = " + t1);
|
||||
}
|
||||
System.out.println("JavaAccess = " + t1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavaCachedAccess() {
|
||||
@Test
|
||||
public void testJavaCachedAccess() {
|
||||
|
||||
long t0 = System.currentTimeMillis();
|
||||
long t0 = System.currentTimeMillis();
|
||||
|
||||
Elevator elevator = new CachedElevatorImpl(fixture);
|
||||
Elevator elevator = new CachedElevatorImpl(fixture);
|
||||
|
||||
for (int i = 0; i != 100000; ++i) {
|
||||
elevator.height();
|
||||
elevator.price();
|
||||
elevator.name();
|
||||
}
|
||||
for (int i = 0; i != 100000; ++i) {
|
||||
elevator.height();
|
||||
elevator.price();
|
||||
elevator.name();
|
||||
}
|
||||
|
||||
long t1 = System.currentTimeMillis() - t0;
|
||||
long t1 = System.currentTimeMillis() - t0;
|
||||
|
||||
System.out.println("JavaCachedAccess = " + t1);
|
||||
}
|
||||
System.out.println("JavaCachedAccess = " + t1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavaConstructor() {
|
||||
@Test
|
||||
public void testJavaConstructor() {
|
||||
|
||||
long t0 = System.currentTimeMillis();
|
||||
long t0 = System.currentTimeMillis();
|
||||
|
||||
for (int i = 0; i != 100000; ++i) {
|
||||
new ElevatorImpl(fixture);
|
||||
}
|
||||
for (int i = 0; i != 100000; ++i) {
|
||||
new ElevatorImpl(fixture);
|
||||
}
|
||||
|
||||
long t1 = System.currentTimeMillis() - t0;
|
||||
long t1 = System.currentTimeMillis() - t0;
|
||||
|
||||
System.out.println("JavaConstructor = " + t1);
|
||||
}
|
||||
System.out.println("JavaConstructor = " + t1);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,42 +18,43 @@ package net.helenus.test.unit.core.dsl;
|
|||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import net.helenus.core.reflect.Drafted;
|
||||
import net.helenus.mapping.annotation.*;
|
||||
|
||||
@Table
|
||||
public interface Account {
|
||||
|
||||
@PartitionKey
|
||||
Long id();
|
||||
@PartitionKey
|
||||
Long id();
|
||||
|
||||
@ClusteringColumn
|
||||
Date time();
|
||||
@ClusteringColumn
|
||||
Date time();
|
||||
|
||||
@Index
|
||||
@Column("is_active")
|
||||
boolean active();
|
||||
@Index
|
||||
@Column("is_active")
|
||||
boolean active();
|
||||
|
||||
@Transient
|
||||
default Draft draft() {
|
||||
return new Draft();
|
||||
}
|
||||
@Transient
|
||||
default Draft draft() {
|
||||
return new Draft();
|
||||
}
|
||||
|
||||
class Draft implements Drafted { //TODO
|
||||
class Draft implements Drafted { // TODO
|
||||
|
||||
@Override
|
||||
public Set<String> mutated() {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Set<String> mutated() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object build() {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Object build() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,10 +15,12 @@
|
|||
*/
|
||||
package net.helenus.test.unit.core.dsl;
|
||||
|
||||
import com.datastax.driver.core.DataType.Name;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.datastax.driver.core.DataType.Name;
|
||||
|
||||
import net.helenus.mapping.annotation.PartitionKey;
|
||||
import net.helenus.mapping.annotation.Table;
|
||||
import net.helenus.mapping.annotation.Types;
|
||||
|
@ -26,15 +28,15 @@ import net.helenus.mapping.annotation.Types;
|
|||
@Table
|
||||
public interface AccountWithCollections {
|
||||
|
||||
@PartitionKey
|
||||
long id();
|
||||
@PartitionKey
|
||||
long id();
|
||||
|
||||
@Types.Set(Name.TEXT)
|
||||
Set<String> aliases();
|
||||
@Types.Set(Name.TEXT)
|
||||
Set<String> aliases();
|
||||
|
||||
@Types.List(Name.TEXT)
|
||||
List<String> name();
|
||||
@Types.List(Name.TEXT)
|
||||
List<String> name();
|
||||
|
||||
@Types.Map(key = Name.TEXT, value = Name.TEXT)
|
||||
Map<String, String> properties();
|
||||
@Types.Map(key = Name.TEXT, value = Name.TEXT)
|
||||
Map<String, String> properties();
|
||||
}
|
||||
|
|
|
@ -15,63 +15,64 @@
|
|||
*/
|
||||
package net.helenus.test.unit.core.dsl;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Getter;
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.Query;
|
||||
import net.helenus.core.reflect.HelenusPropertyNode;
|
||||
import net.helenus.support.DslPropertyException;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CollectionsDlsTest {
|
||||
|
||||
static AccountWithCollections account;
|
||||
static AccountWithCollections account;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
account = Helenus.dsl(AccountWithCollections.class);
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
account = Helenus.dsl(AccountWithCollections.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(account);
|
||||
}
|
||||
@Test
|
||||
public void testPrint() {
|
||||
System.out.println(account);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapGet() {
|
||||
@Test
|
||||
public void testMapGet() {
|
||||
|
||||
String columnName = null;
|
||||
String columnName = null;
|
||||
|
||||
Getter<String> getter = Query.get(account::properties, "key1");
|
||||
Getter<String> getter = Query.get(account::properties, "key1");
|
||||
|
||||
try {
|
||||
getter.get();
|
||||
} catch (DslPropertyException e) {
|
||||
try {
|
||||
getter.get();
|
||||
} catch (DslPropertyException e) {
|
||||
|
||||
HelenusPropertyNode node = e.getPropertyNode();
|
||||
columnName = node.getColumnName();
|
||||
}
|
||||
HelenusPropertyNode node = e.getPropertyNode();
|
||||
columnName = node.getColumnName();
|
||||
}
|
||||
|
||||
Assert.assertEquals("\"properties\"[\"key1\"]", columnName);
|
||||
}
|
||||
Assert.assertEquals("\"properties\"[\"key1\"]", columnName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListGet() {
|
||||
@Test
|
||||
public void testListGet() {
|
||||
|
||||
String columnName = null;
|
||||
String columnName = null;
|
||||
|
||||
Getter<String> getter = Query.getIdx(account::name, 2);
|
||||
Getter<String> getter = Query.getIdx(account::name, 2);
|
||||
|
||||
try {
|
||||
getter.get();
|
||||
} catch (DslPropertyException e) {
|
||||
try {
|
||||
getter.get();
|
||||
} catch (DslPropertyException e) {
|
||||
|
||||
HelenusPropertyNode node = e.getPropertyNode();
|
||||
HelenusPropertyNode node = e.getPropertyNode();
|
||||
|
||||
columnName = node.getColumnName();
|
||||
}
|
||||
columnName = node.getColumnName();
|
||||
}
|
||||
|
||||
Assert.assertEquals("\"name\"[\"2\"]", columnName);
|
||||
}
|
||||
Assert.assertEquals("\"name\"[\"2\"]", columnName);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,27 +15,28 @@
|
|||
*/
|
||||
package net.helenus.test.unit.core.dsl;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.support.DslPropertyException;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.support.DslPropertyException;
|
||||
|
||||
public class DslTest {
|
||||
|
||||
static Account account;
|
||||
static Account account;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
account = Helenus.dsl(Account.class);
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
account = Helenus.dsl(Account.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception {
|
||||
System.out.println(account);
|
||||
}
|
||||
@Test
|
||||
public void testToString() throws Exception {
|
||||
System.out.println(account);
|
||||
}
|
||||
|
||||
@Test(expected = DslPropertyException.class)
|
||||
public void test() throws Exception {
|
||||
account.id();
|
||||
}
|
||||
@Test(expected = DslPropertyException.class)
|
||||
public void test() throws Exception {
|
||||
account.id();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import net.helenus.mapping.annotation.UDT;
|
|||
@UDT
|
||||
public interface Rocket {
|
||||
|
||||
int length();
|
||||
int length();
|
||||
|
||||
double price();
|
||||
double price();
|
||||
}
|
||||
|
|
|
@ -17,40 +17,42 @@ package net.helenus.test.unit.core.dsl;
|
|||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import net.helenus.core.Helenus;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
|
||||
public class UDTCollectionsDlsTest {
|
||||
|
||||
@Test
|
||||
public void testMap() {
|
||||
@Test
|
||||
public void testMap() {
|
||||
|
||||
Map<Rocket, String> comments = new HashMap<Rocket, String>();
|
||||
Map<Rocket, String> comments = new HashMap<Rocket, String>();
|
||||
|
||||
Map<String, Object> firstMap = new HashMap<String, Object>();
|
||||
firstMap.put("length", 100);
|
||||
firstMap.put("price", 100.0);
|
||||
Map<String, Object> firstMap = new HashMap<String, Object>();
|
||||
firstMap.put("length", 100);
|
||||
firstMap.put("price", 100.0);
|
||||
|
||||
Rocket first = Helenus.map(Rocket.class, firstMap);
|
||||
Rocket first = Helenus.map(Rocket.class, firstMap);
|
||||
|
||||
Map<String, Object> secondMap = new HashMap<String, Object>();
|
||||
secondMap.put("length", 50);
|
||||
secondMap.put("price", 70.0);
|
||||
Map<String, Object> secondMap = new HashMap<String, Object>();
|
||||
secondMap.put("length", 50);
|
||||
secondMap.put("price", 70.0);
|
||||
|
||||
Rocket second = Helenus.map(Rocket.class, secondMap);
|
||||
Rocket second = Helenus.map(Rocket.class, secondMap);
|
||||
|
||||
Assert.assertEquals(first.hashCode(), first.hashCode());
|
||||
Assert.assertEquals(second.hashCode(), second.hashCode());
|
||||
Assert.assertEquals(first.hashCode(), first.hashCode());
|
||||
Assert.assertEquals(second.hashCode(), second.hashCode());
|
||||
|
||||
Assert.assertFalse(first.equals(second));
|
||||
Assert.assertTrue(first.equals(first));
|
||||
Assert.assertTrue(second.equals(second));
|
||||
Assert.assertFalse(first.equals(second));
|
||||
Assert.assertTrue(first.equals(first));
|
||||
Assert.assertTrue(second.equals(second));
|
||||
|
||||
comments.put(first, "fast");
|
||||
comments.put(second, "nice");
|
||||
comments.put(first, "fast");
|
||||
comments.put(second, "nice");
|
||||
|
||||
Assert.assertEquals("fast", comments.get(first));
|
||||
Assert.assertEquals("nice", comments.get(second));
|
||||
}
|
||||
Assert.assertEquals("fast", comments.get(first));
|
||||
Assert.assertEquals("nice", comments.get(second));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,51 +17,57 @@ package net.helenus.test.unit.core.dsl;
|
|||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.support.HelenusException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.support.HelenusException;
|
||||
|
||||
public class WrapperTest {
|
||||
|
||||
@Test
|
||||
public void testWrap() throws Exception {
|
||||
@Test
|
||||
public void testWrap() throws Exception {
|
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
map.put("id", 123L);
|
||||
map.put("active", Boolean.TRUE);
|
||||
map.put("unknownField", "he-he");
|
||||
map.put("id", 123L);
|
||||
map.put("active", Boolean.TRUE);
|
||||
map.put("unknownField", "he-he");
|
||||
|
||||
Account account = Helenus.map(Account.class, map);
|
||||
Account account = Helenus.map(Account.class, map);
|
||||
|
||||
Assert.assertEquals(Long.valueOf(123L), account.id());
|
||||
Assert.assertTrue(account.active());
|
||||
}
|
||||
Assert.assertEquals(Long.valueOf(123L), account.id());
|
||||
Assert.assertTrue(account.active());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitive() throws Exception {
|
||||
@Test
|
||||
public void testPrimitive() throws Exception {
|
||||
|
||||
// NOTE: noramlly a ValueProviderMap for the entity would include all keys for an entity
|
||||
// at creation time. This test need to validate that MapperInvocationHander will return
|
||||
// the correct default value for an entity, the twist is that if the key doesn't exist
|
||||
// in the map then it returns null (so as to support the partial update feature). Here we
|
||||
// need to setup the test with a null value for the key we'd like to test.
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
// NOTE: noramlly a ValueProviderMap for the entity would include all keys for
|
||||
// an entity
|
||||
// at creation time. This test need to validate that MapperInvocationHander will
|
||||
// return
|
||||
// the correct default value for an entity, the twist is that if the key doesn't
|
||||
// exist
|
||||
// in the map then it returns null (so as to support the partial update
|
||||
// feature). Here we
|
||||
// need to setup the test with a null value for the key we'd like to test.
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
map.put("id", 123L);
|
||||
map.put("active", null);
|
||||
map.put("id", 123L);
|
||||
map.put("active", null);
|
||||
|
||||
Account account = Helenus.map(Account.class, map);
|
||||
Account account = Helenus.map(Account.class, map);
|
||||
|
||||
Assert.assertFalse(account.active());
|
||||
}
|
||||
Assert.assertFalse(account.active());
|
||||
}
|
||||
|
||||
@Test(expected = HelenusException.class)
|
||||
public void testWrongMethods() throws Exception {
|
||||
@Test(expected = HelenusException.class)
|
||||
public void testWrongMethods() throws Exception {
|
||||
|
||||
WrongAccount wrongAccount = Helenus.map(WrongAccount.class, new HashMap<String, Object>());
|
||||
WrongAccount wrongAccount = Helenus.map(WrongAccount.class, new HashMap<String, Object>());
|
||||
|
||||
wrongAccount.id();
|
||||
}
|
||||
wrongAccount.id();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,5 +17,5 @@ package net.helenus.test.unit.core.dsl;
|
|||
|
||||
public interface WrongAccount {
|
||||
|
||||
void id();
|
||||
void id();
|
||||
}
|
||||
|
|
|
@ -18,86 +18,88 @@ package net.helenus.test.unit.support;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import net.helenus.support.Immutables;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.support.Immutables;
|
||||
|
||||
public class ImmutablesTest {
|
||||
|
||||
@Test
|
||||
public void testSet() {
|
||||
@Test
|
||||
public void testSet() {
|
||||
|
||||
Set<Integer> set = Immutables.setOf(123);
|
||||
Set<Integer> set = Immutables.setOf(123);
|
||||
|
||||
Assert.assertEquals(1, set.size());
|
||||
Assert.assertFalse(set.isEmpty());
|
||||
Assert.assertEquals(1, set.size());
|
||||
Assert.assertFalse(set.isEmpty());
|
||||
|
||||
Assert.assertTrue(set.contains(123));
|
||||
Assert.assertFalse(set.contains(125));
|
||||
Assert.assertTrue(set.contains(123));
|
||||
Assert.assertFalse(set.contains(125));
|
||||
|
||||
int c = 0;
|
||||
for (Integer v : set) {
|
||||
Assert.assertEquals(Integer.valueOf(123), v);
|
||||
c++;
|
||||
}
|
||||
int c = 0;
|
||||
for (Integer v : set) {
|
||||
Assert.assertEquals(Integer.valueOf(123), v);
|
||||
c++;
|
||||
}
|
||||
|
||||
Assert.assertEquals(1, c);
|
||||
}
|
||||
Assert.assertEquals(1, c);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testList() {
|
||||
@Test
|
||||
public void testList() {
|
||||
|
||||
List<Integer> list = Immutables.listOf(123);
|
||||
List<Integer> list = Immutables.listOf(123);
|
||||
|
||||
Assert.assertEquals(1, list.size());
|
||||
Assert.assertFalse(list.isEmpty());
|
||||
Assert.assertEquals(1, list.size());
|
||||
Assert.assertFalse(list.isEmpty());
|
||||
|
||||
Assert.assertTrue(list.contains(123));
|
||||
Assert.assertFalse(list.contains(125));
|
||||
Assert.assertTrue(list.contains(123));
|
||||
Assert.assertFalse(list.contains(125));
|
||||
|
||||
int c = 0;
|
||||
for (Integer v : list) {
|
||||
Assert.assertEquals(Integer.valueOf(123), v);
|
||||
c++;
|
||||
}
|
||||
int c = 0;
|
||||
for (Integer v : list) {
|
||||
Assert.assertEquals(Integer.valueOf(123), v);
|
||||
c++;
|
||||
}
|
||||
|
||||
Assert.assertEquals(1, c);
|
||||
}
|
||||
Assert.assertEquals(1, c);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMap() {
|
||||
@Test
|
||||
public void testMap() {
|
||||
|
||||
Map<Integer, Integer> map = Immutables.mapOf(123, 555);
|
||||
Map<Integer, Integer> map = Immutables.mapOf(123, 555);
|
||||
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertFalse(map.isEmpty());
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertFalse(map.isEmpty());
|
||||
|
||||
Assert.assertTrue(map.containsKey(123));
|
||||
Assert.assertFalse(map.containsKey(125));
|
||||
Assert.assertTrue(map.containsKey(123));
|
||||
Assert.assertFalse(map.containsKey(125));
|
||||
|
||||
int c = 0;
|
||||
for (Integer v : map.keySet()) {
|
||||
Assert.assertEquals(Integer.valueOf(123), v);
|
||||
c++;
|
||||
}
|
||||
int c = 0;
|
||||
for (Integer v : map.keySet()) {
|
||||
Assert.assertEquals(Integer.valueOf(123), v);
|
||||
c++;
|
||||
}
|
||||
|
||||
Assert.assertEquals(1, c);
|
||||
Assert.assertEquals(1, c);
|
||||
|
||||
c = 0;
|
||||
for (Integer v : map.values()) {
|
||||
Assert.assertEquals(Integer.valueOf(555), v);
|
||||
c++;
|
||||
}
|
||||
c = 0;
|
||||
for (Integer v : map.values()) {
|
||||
Assert.assertEquals(Integer.valueOf(555), v);
|
||||
c++;
|
||||
}
|
||||
|
||||
Assert.assertEquals(1, c);
|
||||
Assert.assertEquals(1, c);
|
||||
|
||||
c = 0;
|
||||
for (Map.Entry<Integer, Integer> e : map.entrySet()) {
|
||||
Assert.assertEquals(Integer.valueOf(123), e.getKey());
|
||||
Assert.assertEquals(Integer.valueOf(555), e.getValue());
|
||||
c++;
|
||||
}
|
||||
c = 0;
|
||||
for (Map.Entry<Integer, Integer> e : map.entrySet()) {
|
||||
Assert.assertEquals(Integer.valueOf(123), e.getKey());
|
||||
Assert.assertEquals(Integer.valueOf(555), e.getValue());
|
||||
c++;
|
||||
}
|
||||
|
||||
Assert.assertEquals(1, c);
|
||||
}
|
||||
Assert.assertEquals(1, c);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,66 +19,68 @@ import java.util.ArrayList;
|
|||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import net.helenus.support.Transformers;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.helenus.support.Transformers;
|
||||
|
||||
public class TransformersTest {
|
||||
|
||||
@Test
|
||||
public void testList() {
|
||||
@Test
|
||||
public void testList() {
|
||||
|
||||
List<Integer> source = new ArrayList<Integer>();
|
||||
source.add(555);
|
||||
source.add(777);
|
||||
source.add(999);
|
||||
List<Integer> source = new ArrayList<Integer>();
|
||||
source.add(555);
|
||||
source.add(777);
|
||||
source.add(999);
|
||||
|
||||
List<String> out = Transformers.transformList(source, x -> "_" + x);
|
||||
List<String> out = Transformers.transformList(source, x -> "_" + x);
|
||||
|
||||
Assert.assertEquals(3, out.size());
|
||||
Assert.assertEquals(false, out.isEmpty());
|
||||
Assert.assertEquals("_555", out.get(0));
|
||||
Assert.assertEquals("_777", out.get(1));
|
||||
Assert.assertEquals("_999", out.get(2));
|
||||
Assert.assertEquals(3, out.size());
|
||||
Assert.assertEquals(false, out.isEmpty());
|
||||
Assert.assertEquals("_555", out.get(0));
|
||||
Assert.assertEquals("_777", out.get(1));
|
||||
Assert.assertEquals("_999", out.get(2));
|
||||
|
||||
Iterator<String> i = out.iterator();
|
||||
Assert.assertTrue(i.hasNext());
|
||||
Assert.assertEquals("_555", i.next());
|
||||
Assert.assertTrue(i.hasNext());
|
||||
Assert.assertEquals("_777", i.next());
|
||||
Assert.assertTrue(i.hasNext());
|
||||
Assert.assertEquals("_999", i.next());
|
||||
Assert.assertFalse(i.hasNext());
|
||||
Iterator<String> i = out.iterator();
|
||||
Assert.assertTrue(i.hasNext());
|
||||
Assert.assertEquals("_555", i.next());
|
||||
Assert.assertTrue(i.hasNext());
|
||||
Assert.assertEquals("_777", i.next());
|
||||
Assert.assertTrue(i.hasNext());
|
||||
Assert.assertEquals("_999", i.next());
|
||||
Assert.assertFalse(i.hasNext());
|
||||
|
||||
ListIterator<String> li = out.listIterator();
|
||||
Assert.assertTrue(li.hasNext());
|
||||
Assert.assertEquals(0, li.nextIndex());
|
||||
Assert.assertEquals(-1, li.previousIndex());
|
||||
Assert.assertEquals("_555", li.next());
|
||||
Assert.assertTrue(li.hasNext());
|
||||
Assert.assertEquals(1, li.nextIndex());
|
||||
Assert.assertEquals(0, li.previousIndex());
|
||||
Assert.assertEquals("_777", li.next());
|
||||
Assert.assertTrue(li.hasNext());
|
||||
Assert.assertEquals(2, li.nextIndex());
|
||||
Assert.assertEquals(1, li.previousIndex());
|
||||
Assert.assertEquals("_999", li.next());
|
||||
Assert.assertFalse(li.hasNext());
|
||||
Assert.assertEquals(3, li.nextIndex());
|
||||
Assert.assertEquals(2, li.previousIndex());
|
||||
}
|
||||
ListIterator<String> li = out.listIterator();
|
||||
Assert.assertTrue(li.hasNext());
|
||||
Assert.assertEquals(0, li.nextIndex());
|
||||
Assert.assertEquals(-1, li.previousIndex());
|
||||
Assert.assertEquals("_555", li.next());
|
||||
Assert.assertTrue(li.hasNext());
|
||||
Assert.assertEquals(1, li.nextIndex());
|
||||
Assert.assertEquals(0, li.previousIndex());
|
||||
Assert.assertEquals("_777", li.next());
|
||||
Assert.assertTrue(li.hasNext());
|
||||
Assert.assertEquals(2, li.nextIndex());
|
||||
Assert.assertEquals(1, li.previousIndex());
|
||||
Assert.assertEquals("_999", li.next());
|
||||
Assert.assertFalse(li.hasNext());
|
||||
Assert.assertEquals(3, li.nextIndex());
|
||||
Assert.assertEquals(2, li.previousIndex());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullsInList() {
|
||||
@Test
|
||||
public void testNullsInList() {
|
||||
|
||||
List<Integer> source = new ArrayList<Integer>();
|
||||
source.add(555);
|
||||
source.add(null);
|
||||
source.add(999);
|
||||
List<Integer> source = new ArrayList<Integer>();
|
||||
source.add(555);
|
||||
source.add(null);
|
||||
source.add(999);
|
||||
|
||||
List<String> out = Transformers.transformList(source, x -> "_" + x);
|
||||
List<String> out = Transformers.transformList(source, x -> "_" + x);
|
||||
|
||||
Assert.assertEquals(3, out.size());
|
||||
Assert.assertEquals("_null", out.get(1));
|
||||
}
|
||||
Assert.assertEquals(3, out.size());
|
||||
Assert.assertEquals("_null", out.get(1));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,18 +13,14 @@
|
|||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# for production, you should probably set the root to INFO
|
||||
# and the pattern to %c instead of %l. (%l is slower.)
|
||||
|
||||
# output messages into a rolling log file as well as stdout
|
||||
log4j.rootLogger=ERROR,stdout
|
||||
|
||||
# stdout
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c{3} - %m%n
|
||||
log4j.appender.stdout.follow=true
|
||||
|
||||
log4j.logger.org.apache=WARN
|
||||
log4j.logger.org.cassandraunit=ERROR
|
||||
log4j.logger.org.cassandraunit=ERROR
|
||||
|
|
|
@ -1 +1 @@
|
|||
handlers = org.slf4j.bridge.SLF4JBridgeHandler
|
||||
handlers=org.slf4j.bridge.SLF4JBridgeHandler
|
||||
|
|
Loading…
Reference in a new issue