diff --git a/.idea/eclipseCodeFormatter.xml b/.idea/eclipseCodeFormatter.xml index 4ae0669..5896462 100644 --- a/.idea/eclipseCodeFormatter.xml +++ b/.idea/eclipseCodeFormatter.xml @@ -3,7 +3,6 @@ diff --git a/NOTES b/NOTES index bbacfc9..65017da 100644 --- a/NOTES +++ b/NOTES @@ -1,172 +1,27 @@ +Operation/ +|-- AbstractStatementOperation +| |-- AbstractOperation +| | |-- AbstractFilterOperation +| | | |-- CountOperation +| | | |-- DeleteOperation +| | | `-- UpdateOperation +| | |-- BoundOperation +| | `-- InsertOperation +| |-- AbstractOptionalOperation +| | |-- AbstractFilterOptionalOperation +| | | |-- SelectFirstOperation +| | | `-- SelectFirstTransformingOperation +| | `-- BoundOptionalOperation +| `-- AbstractStreamOperation +| |-- AbstractFilterStreamOperation +| | |-- SelectOperation +| | `-- SelectTransformingOperation +| `-- BoundStreamOperation +|-- PreparedOperation +|-- PreparedOptionalOperation +`-- PreparedStreamOperation - - - ---- Cache - // `E` is the type of the Entity class or one of: - // - ResultSet - // - ArrayTuple{N} - // - Count - // `F` is the type argument passed to us from HelenusSession DSL and carried on via one of the - // Operation classes, it is going to be one of: - // - ResultSet - // - ArrayTuple{N} - // - or a type previously registered as a HelenusEntity. - // In the form of a: - // - Stream or an - // - Optional - // - // Operation/ - // |-- AbstractStatementOperation - // | |-- AbstractOperation - // | | |-- AbstractFilterOperation - // | | | |-- CountOperation - // | | | |-- DeleteOperation - // | | | `-- UpdateOperation - // | | |-- BoundOperation - // | | `-- InsertOperation - // | |-- AbstractOptionalOperation - // | | |-- AbstractFilterOptionalOperation - // | | | |-- SelectFirstOperation - // | | | `-- SelectFirstTransformingOperation - // | | `-- BoundOptionalOperation - // | `-- AbstractStreamOperation - // | |-- AbstractFilterStreamOperation - // | | |-- SelectOperation - // | | `-- SelectTransformingOperation - // | `-- BoundStreamOperation - // |-- PreparedOperation - // |-- PreparedOptionalOperation - // `-- PreparedStreamOperation - // - // These all boil down to: Select, Update, Insert, Delete and Count - // - // -- Select: - // 1) Select statements that contain all primary key information will be "distinct" and - // result in a single value or no match. - // If present, return cached entity otherwise execute query and cache result. - // - // 2) Otherwise the result is a set, possibly empty, of values that match. - // When within a UOW: - // If present, return the cached value(s) from the statement cache matching the query string. - // Otherwise, execute query and cache the result in the statement cache and update/merge the - // entites into the entity cache. - // NOTE: When we read data from the database we augment the select clause with TTL and write time - // stamps for all columns that record such information so as to be able to properlty expire - // and merge values in the cache. - // - // -- Update: - // Execute the database statement and then iff successs upsert the entity being updated into the - // entity cache. - // - // -- Insert/Upsert: - // Same as Update. - // - // -- Delete: - // Same as update, only remove the cached value from all caches on success. - // - // -- Count: - // If operating within a UOW lookup count in statement cache, if not present execute query and cache result. - // - - - if (delegate instanceof SelectOperation) { - SelectOperation op = (SelectOperation) delegate; - - // Determine if we are caching and if so where. - AbstractCache> cache = delegate.getCache(); - boolean prepareStatementForCaching = cache != null; - if (uow != null) { - prepareStatementForCaching = true; - cache = uow.>getCacheEnclosing(cache); - } - - // The delegate will provide the cache key becuase it will either be: - // a) when distinct: the combination of the partition/cluster key columns - // b) otherwise: the table name followed by the portion of the SQL statement that would form the WHERE clause - CacheKey key = (cache == null) ? null : delegate.getCacheKey(); - if (key != null && cache != null) { - Set value = cache.get(key); - if (value != null) { - // Select will always return a Stream - // TODO(gburd): SelectTransforming... apply fn here? - result = (E) value.stream(); - if (cacheHitCounter != null) { - cacheHitCounter.inc(); - } - if (log != null) { - log.info("cache hit"); - } - return result; - } else { - if (cacheMissCounter != null) { - cacheMissCounter.inc(); - } - if (log != null) { - log.info("cache miss"); - } - } - } - } - - - - if (cache != null) { - Object obj = delegate.unwrap(result); - if (obj != null) { - cache.put(key, obj); - } - - delegate.extract(result, key, cache); - } - } - - } - - - -// TODO: first, ask the delegate for the cacheKey -// if this is a SELECT query: -// if not in cache build the statement, execute the future, cache the result, transform the result then cache the transformations -// if INSERT/UPSERT/UPDATE -// if DELETE -// if COUNT ----------------------------- - - @Override - public CacheKey getCacheKey() { - - Listkeys = new ArrayList<>(filters.size()); - HelenusEntity entity = props.get(0).getEntity(); - - for (HelenusPropertyNode prop : props) { - switch(prop.getProperty().getColumnType()) { - case PARTITION_KEY: - case CLUSTERING_COLUMN: - - Filter filter = filters.get(prop.getProperty()); - if (filter != null) { - keys.add(filter.toString()); - } else { - // we're missing a part of the primary key, so we can't create a proper cache key - return null; - } - break; - default: - // We've past the primary key components in this ordered list, so we're done building - // the cache key. - if (keys.size() > 0) { - return new CacheKey(entity, Joiner.on(",").join(keys)); - } - return null; - } - } - return null; - } - ---------------------------- - // TODO(gburd): create a statement that matches one that wasn't prepared //String key = // "use " + preparedStatement.getQueryKeyspace() + "; " + preparedStatement.getQueryString(); @@ -175,64 +30,6 @@ //} ------------------------- -package net.helenus.core.operation; - -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.ResultSetFuture; -import com.datastax.driver.core.Statement; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.RemovalListener; -import com.google.common.cache.RemovalNotification; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; - -public abstract class AbstractCache { - final Logger logger = LoggerFactory.getLogger(getClass()); - public Cache cache; - - public AbstractCache() { - RemovalListener listener = - new RemovalListener() { - @Override - public void onRemoval(RemovalNotification n) { - if (n.wasEvicted()) { - String cause = n.getCause().name(); - logger.info(cause); - } - } - }; - - cache = CacheBuilder.newBuilder() - .maximumSize(10_000) - .expireAfterAccess(20, TimeUnit.MINUTES) - .weakKeys() - .softValues() - .removalListener(listener) - .build(); - } - - V get(K key) { - return cache.getIfPresent(key); - } - - void put(K key, V value) { - cache.put(key, value); - } -} - ------------------------------------------------------------------------------------------------- - -cache entites (2 methods) marked @Cacheable -cache entites in txn context -cache results when .cache() chained before .{a}sync() call, return a EvictableCacheItem that has an .evict() method -fix txn .andThen() chains - - primitive types have default values, (e.g. boolean, int, ...) but primative wrapper classes do not and can be null (e.g. Boolean, Integer, ...) @@ -372,3 +169,17 @@ begin: cache.put } */ +------------------ + +InsertOperation + + + Class iface = entity.getMappingInterface(); + boolean includesNonIdentityValues = values.stream().map(t -> { + ColumnType type = t._1.getProperty().getColumnType(); + return !((type == ColumnType.PARTITION_KEY) || (type == ColumnType.CLUSTERING_COLUMN)); + }) + .reduce(false, (acc, t) -> acc || t); + if (resultType == iface) { + if (values.size() > 0 && includesNonIdentityValues) { + boolean immutable = iface.isAssignableFrom(Drafted.class); diff --git a/src/main/java/com/datastax/driver/core/DefaultMetadata.java b/src/main/java/com/datastax/driver/core/DefaultMetadata.java index 4ca45d0..57566ab 100644 --- a/src/main/java/com/datastax/driver/core/DefaultMetadata.java +++ b/src/main/java/com/datastax/driver/core/DefaultMetadata.java @@ -5,19 +5,19 @@ import java.util.List; public class DefaultMetadata extends Metadata { - public DefaultMetadata() { - super(null); - } + public DefaultMetadata() { + super(null); + } - private DefaultMetadata(Cluster.Manager cluster) { - super(cluster); - } + private DefaultMetadata(Cluster.Manager cluster) { + super(cluster); + } - public TupleType newTupleType(DataType... types) { - return newTupleType(Arrays.asList(types)); - } + public TupleType newTupleType(DataType... types) { + return newTupleType(Arrays.asList(types)); + } - public TupleType newTupleType(List types) { - return new TupleType(types, ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE); - } + public TupleType newTupleType(List types) { + return new TupleType(types, ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE); + } } diff --git a/src/main/java/com/datastax/driver/core/querybuilder/IsNotNullClause.java b/src/main/java/com/datastax/driver/core/querybuilder/IsNotNullClause.java index 991de7c..9ff2596 100644 --- a/src/main/java/com/datastax/driver/core/querybuilder/IsNotNullClause.java +++ b/src/main/java/com/datastax/driver/core/querybuilder/IsNotNullClause.java @@ -15,35 +15,34 @@ */ package com.datastax.driver.core.querybuilder; -import java.util.List; - import com.datastax.driver.core.CodecRegistry; +import java.util.List; public class IsNotNullClause extends Clause { - final String name; + final String name; - public IsNotNullClause(String name) { - this.name = name; - } + public IsNotNullClause(String name) { + this.name = name; + } - @Override - String name() { - return name; - } + @Override + String name() { + return name; + } - @Override - Object firstValue() { - return null; - } + @Override + Object firstValue() { + return null; + } - @Override - void appendTo(StringBuilder sb, List variables, CodecRegistry codecRegistry) { - Utils.appendName(name, sb).append(" IS NOT NULL"); - } + @Override + void appendTo(StringBuilder sb, List variables, CodecRegistry codecRegistry) { + Utils.appendName(name, sb).append(" IS NOT NULL"); + } - @Override - boolean containsBindMarker() { - return false; - } + @Override + boolean containsBindMarker() { + return false; + } } diff --git a/src/main/java/com/datastax/driver/core/schemabuilder/CreateCustomIndex.java b/src/main/java/com/datastax/driver/core/schemabuilder/CreateCustomIndex.java index 227c622..ea95028 100644 --- a/src/main/java/com/datastax/driver/core/schemabuilder/CreateCustomIndex.java +++ b/src/main/java/com/datastax/driver/core/schemabuilder/CreateCustomIndex.java @@ -6,143 +6,150 @@ import com.google.common.base.Optional; public class CreateCustomIndex extends CreateIndex { - private String indexName; - private boolean ifNotExists = false; - private Optional keyspaceName = Optional.absent(); - private String tableName; - private String columnName; - private boolean keys; + private String indexName; + private boolean ifNotExists = false; + private Optional keyspaceName = Optional.absent(); + private String tableName; + private String columnName; + private boolean keys; - CreateCustomIndex(String indexName) { - super(indexName); - validateNotEmpty(indexName, "Index name"); - validateNotKeyWord(indexName, - String.format("The index name '%s' is not allowed because it is a reserved keyword", indexName)); - this.indexName = indexName; - } + CreateCustomIndex(String indexName) { + super(indexName); + validateNotEmpty(indexName, "Index name"); + validateNotKeyWord( + indexName, + String.format( + "The index name '%s' is not allowed because it is a reserved keyword", indexName)); + this.indexName = indexName; + } - /** - * Add the 'IF NOT EXISTS' condition to this CREATE INDEX statement. - * - * @return this CREATE INDEX statement. - */ - public CreateIndex ifNotExists() { - this.ifNotExists = true; - return this; - } + /** + * Add the 'IF NOT EXISTS' condition to this CREATE INDEX statement. + * + * @return this CREATE INDEX statement. + */ + public CreateIndex ifNotExists() { + this.ifNotExists = true; + return this; + } - /** - * Specify the keyspace and table to create the index on. - * - * @param keyspaceName - * the keyspace name. - * @param tableName - * the table name. - * @return a {@link CreateIndex.CreateIndexOn} that will allow the specification - * of the column. - */ - public CreateIndex.CreateIndexOn onTable(String keyspaceName, String tableName) { - validateNotEmpty(keyspaceName, "Keyspace name"); - validateNotEmpty(tableName, "Table name"); - validateNotKeyWord(keyspaceName, - String.format("The keyspace name '%s' is not allowed because it is a reserved keyword", keyspaceName)); - validateNotKeyWord(tableName, - String.format("The table name '%s' is not allowed because it is a reserved keyword", tableName)); - this.keyspaceName = Optional.fromNullable(keyspaceName); - this.tableName = tableName; - return new CreateCustomIndex.CreateIndexOn(); - } + /** + * Specify the keyspace and table to create the index on. + * + * @param keyspaceName the keyspace name. + * @param tableName the table name. + * @return a {@link CreateIndex.CreateIndexOn} that will allow the specification of the column. + */ + public CreateIndex.CreateIndexOn onTable(String keyspaceName, String tableName) { + validateNotEmpty(keyspaceName, "Keyspace name"); + validateNotEmpty(tableName, "Table name"); + validateNotKeyWord( + keyspaceName, + String.format( + "The keyspace name '%s' is not allowed because it is a reserved keyword", + keyspaceName)); + validateNotKeyWord( + tableName, + String.format( + "The table name '%s' is not allowed because it is a reserved keyword", tableName)); + this.keyspaceName = Optional.fromNullable(keyspaceName); + this.tableName = tableName; + return new CreateCustomIndex.CreateIndexOn(); + } - /** - * Specify the table to create the index on. - * - * @param tableName - * the table name. - * @return a {@link CreateIndex.CreateIndexOn} that will allow the specification - * of the column. - */ - public CreateIndex.CreateIndexOn onTable(String tableName) { - validateNotEmpty(tableName, "Table name"); - validateNotKeyWord(tableName, - String.format("The table name '%s' is not allowed because it is a reserved keyword", tableName)); - this.tableName = tableName; - return new CreateCustomIndex.CreateIndexOn(); - } + /** + * Specify the table to create the index on. + * + * @param tableName the table name. + * @return a {@link CreateIndex.CreateIndexOn} that will allow the specification of the column. + */ + public CreateIndex.CreateIndexOn onTable(String tableName) { + validateNotEmpty(tableName, "Table name"); + validateNotKeyWord( + tableName, + String.format( + "The table name '%s' is not allowed because it is a reserved keyword", tableName)); + this.tableName = tableName; + return new CreateCustomIndex.CreateIndexOn(); + } - String getCustomClassName() { - return ""; - } + String getCustomClassName() { + return ""; + } - String getOptions() { - return ""; - } + String getOptions() { + return ""; + } - @Override - public String buildInternal() { - StringBuilder createStatement = new StringBuilder(STATEMENT_START).append("CREATE CUSTOM INDEX "); + @Override + public String buildInternal() { + StringBuilder createStatement = + new StringBuilder(STATEMENT_START).append("CREATE CUSTOM INDEX "); - if (ifNotExists) { - createStatement.append("IF NOT EXISTS "); - } + if (ifNotExists) { + createStatement.append("IF NOT EXISTS "); + } - createStatement.append(indexName).append(" ON "); + createStatement.append(indexName).append(" ON "); - if (keyspaceName.isPresent()) { - createStatement.append(keyspaceName.get()).append("."); - } - createStatement.append(tableName); + if (keyspaceName.isPresent()) { + createStatement.append(keyspaceName.get()).append("."); + } + createStatement.append(tableName); - createStatement.append("("); - if (keys) { - createStatement.append("KEYS("); - } + createStatement.append("("); + if (keys) { + createStatement.append("KEYS("); + } - createStatement.append(columnName); + createStatement.append(columnName); - if (keys) { - createStatement.append(")"); - } - createStatement.append(")"); + if (keys) { + createStatement.append(")"); + } + createStatement.append(")"); - createStatement.append(" USING '"); - createStatement.append(getCustomClassName()); - createStatement.append("' WITH OPTIONS = {"); - createStatement.append(getOptions()); - createStatement.append(" }"); + createStatement.append(" USING '"); + createStatement.append(getCustomClassName()); + createStatement.append("' WITH OPTIONS = {"); + createStatement.append(getOptions()); + createStatement.append(" }"); - return createStatement.toString(); - } + return createStatement.toString(); + } - public class CreateIndexOn extends CreateIndex.CreateIndexOn { - /** - * Specify the column to create the index on. - * - * @param columnName - * the column name. - * @return the final CREATE INDEX statement. - */ - public SchemaStatement andColumn(String columnName) { - validateNotEmpty(columnName, "Column name"); - validateNotKeyWord(columnName, - String.format("The column name '%s' is not allowed because it is a reserved keyword", columnName)); - CreateCustomIndex.this.columnName = columnName; - return SchemaStatement.fromQueryString(buildInternal()); - } + public class CreateIndexOn extends CreateIndex.CreateIndexOn { + /** + * Specify the column to create the index on. + * + * @param columnName the column name. + * @return the final CREATE INDEX statement. + */ + public SchemaStatement andColumn(String columnName) { + validateNotEmpty(columnName, "Column name"); + validateNotKeyWord( + columnName, + String.format( + "The column name '%s' is not allowed because it is a reserved keyword", columnName)); + CreateCustomIndex.this.columnName = columnName; + return SchemaStatement.fromQueryString(buildInternal()); + } - /** - * Create an index on the keys of the given map column. - * - * @param columnName - * the column name. - * @return the final CREATE INDEX statement. - */ - public SchemaStatement andKeysOfColumn(String columnName) { - validateNotEmpty(columnName, "Column name"); - validateNotKeyWord(columnName, - String.format("The column name '%s' is not allowed because it is a reserved keyword", columnName)); - CreateCustomIndex.this.columnName = columnName; - CreateCustomIndex.this.keys = true; - return SchemaStatement.fromQueryString(buildInternal()); - } - } + /** + * Create an index on the keys of the given map column. + * + * @param columnName the column name. + * @return the final CREATE INDEX statement. + */ + public SchemaStatement andKeysOfColumn(String columnName) { + validateNotEmpty(columnName, "Column name"); + validateNotKeyWord( + columnName, + String.format( + "The column name '%s' is not allowed because it is a reserved keyword", columnName)); + CreateCustomIndex.this.columnName = columnName; + CreateCustomIndex.this.keys = true; + return SchemaStatement.fromQueryString(buildInternal()); + } + } } diff --git a/src/main/java/com/datastax/driver/core/schemabuilder/CreateMaterializedView.java b/src/main/java/com/datastax/driver/core/schemabuilder/CreateMaterializedView.java index 867cbc6..d6ba093 100644 --- a/src/main/java/com/datastax/driver/core/schemabuilder/CreateMaterializedView.java +++ b/src/main/java/com/datastax/driver/core/schemabuilder/CreateMaterializedView.java @@ -5,48 +5,53 @@ import com.datastax.driver.core.querybuilder.Select; public class CreateMaterializedView extends Create { - private String viewName; - private Select.Where selection; - private String primaryKey; - private String clustering; + private String viewName; + private Select.Where selection; + private String primaryKey; + private String clustering; - public CreateMaterializedView(String keyspaceName, String viewName, Select.Where selection, String primaryKey, - String clustering) { - super(keyspaceName, viewName); - this.viewName = viewName; - this.selection = selection; - this.primaryKey = primaryKey; - this.clustering = clustering; - } + public CreateMaterializedView( + String keyspaceName, + String viewName, + Select.Where selection, + String primaryKey, + String clustering) { + super(keyspaceName, viewName); + this.viewName = viewName; + this.selection = selection; + this.primaryKey = primaryKey; + this.clustering = clustering; + } - public String getQueryString(CodecRegistry codecRegistry) { - return buildInternal(); - } + public String getQueryString(CodecRegistry codecRegistry) { + return buildInternal(); + } - public String buildInternal() { - StringBuilder createStatement = new StringBuilder(STATEMENT_START).append("CREATE MATERIALIZED VIEW"); - if (ifNotExists) { - createStatement.append(" IF NOT EXISTS"); - } - createStatement.append(" "); - if (keyspaceName.isPresent()) { - createStatement.append(keyspaceName.get()).append("."); - } - createStatement.append(viewName); - createStatement.append(" AS "); - createStatement.append(selection.getQueryString()); - createStatement.setLength(createStatement.length() - 1); - createStatement.append(" "); - createStatement.append(primaryKey); - if (clustering != null) { - createStatement.append(" ").append(clustering); - } - createStatement.append(";"); + public String buildInternal() { + StringBuilder createStatement = + new StringBuilder(STATEMENT_START).append("CREATE MATERIALIZED VIEW"); + if (ifNotExists) { + createStatement.append(" IF NOT EXISTS"); + } + createStatement.append(" "); + if (keyspaceName.isPresent()) { + createStatement.append(keyspaceName.get()).append("."); + } + createStatement.append(viewName); + createStatement.append(" AS "); + createStatement.append(selection.getQueryString()); + createStatement.setLength(createStatement.length() - 1); + createStatement.append(" "); + createStatement.append(primaryKey); + if (clustering != null) { + createStatement.append(" ").append(clustering); + } + createStatement.append(";"); - return createStatement.toString(); - } + return createStatement.toString(); + } - public String toString() { - return buildInternal(); - } + public String toString() { + return buildInternal(); + } } diff --git a/src/main/java/com/datastax/driver/core/schemabuilder/CreateSasiIndex.java b/src/main/java/com/datastax/driver/core/schemabuilder/CreateSasiIndex.java index 6487b32..7a82590 100644 --- a/src/main/java/com/datastax/driver/core/schemabuilder/CreateSasiIndex.java +++ b/src/main/java/com/datastax/driver/core/schemabuilder/CreateSasiIndex.java @@ -2,16 +2,17 @@ package com.datastax.driver.core.schemabuilder; public class CreateSasiIndex extends CreateCustomIndex { - public CreateSasiIndex(String indexName) { - super(indexName); - } + public CreateSasiIndex(String indexName) { + super(indexName); + } - String getCustomClassName() { - return "org.apache.cassandra.index.sasi.SASIIndex"; - } + String getCustomClassName() { + return "org.apache.cassandra.index.sasi.SASIIndex"; + } - String getOptions() { - return "'analyzer_class': " + "'org.apache.cassandra.index.sasi.analyzer.NonTokenizingAnalyzer', " - + "'case_sensitive': 'false'"; - } + String getOptions() { + return "'analyzer_class': " + + "'org.apache.cassandra.index.sasi.analyzer.NonTokenizingAnalyzer', " + + "'case_sensitive': 'false'"; + } } diff --git a/src/main/java/com/datastax/driver/core/schemabuilder/CreateTable.java b/src/main/java/com/datastax/driver/core/schemabuilder/CreateTable.java index cab8549..7efce6f 100644 --- a/src/main/java/com/datastax/driver/core/schemabuilder/CreateTable.java +++ b/src/main/java/com/datastax/driver/core/schemabuilder/CreateTable.java @@ -20,19 +20,19 @@ import com.datastax.driver.core.CodecRegistry; /** A built CREATE TABLE statement. */ public class CreateTable extends Create { - public CreateTable(String keyspaceName, String tableName) { - super(keyspaceName, tableName); - } + public CreateTable(String keyspaceName, String tableName) { + super(keyspaceName, tableName); + } - public CreateTable(String tableName) { - super(tableName); - } + public CreateTable(String tableName) { + super(tableName); + } - public String getQueryString(CodecRegistry codecRegistry) { - return buildInternal(); - } + public String getQueryString(CodecRegistry codecRegistry) { + return buildInternal(); + } - public String toString() { - return buildInternal(); - } + public String toString() { + return buildInternal(); + } } diff --git a/src/main/java/com/datastax/driver/core/schemabuilder/DropMaterializedView.java b/src/main/java/com/datastax/driver/core/schemabuilder/DropMaterializedView.java index a7279f2..4e24381 100644 --- a/src/main/java/com/datastax/driver/core/schemabuilder/DropMaterializedView.java +++ b/src/main/java/com/datastax/driver/core/schemabuilder/DropMaterializedView.java @@ -4,46 +4,49 @@ import com.google.common.base.Optional; public class DropMaterializedView extends Drop { - private final String itemType = "MATERIALIZED VIEW"; - private Optional keyspaceName = Optional.absent(); - private String itemName; - private boolean ifExists = true; - public DropMaterializedView(String keyspaceName, String viewName) { - this(keyspaceName, viewName, DroppedItem.MATERIALIZED_VIEW); - } + private Optional keyspaceName = Optional.absent(); + private String itemName; + private boolean ifExists = true; - private DropMaterializedView(String keyspaceName, String viewName, DroppedItem itemType) { - super(keyspaceName, viewName, Drop.DroppedItem.TABLE); - validateNotEmpty(keyspaceName, "Keyspace name"); - this.keyspaceName = Optional.fromNullable(keyspaceName); - this.itemName = viewName; - } + public DropMaterializedView(String keyspaceName, String viewName) { + this(keyspaceName, viewName, DroppedItem.MATERIALIZED_VIEW); + } - /** - * Add the 'IF EXISTS' condition to this DROP statement. - * - * @return this statement. - */ - public Drop ifExists() { - this.ifExists = true; - return this; - } + private DropMaterializedView(String keyspaceName, String viewName, DroppedItem itemType) { + super(keyspaceName, viewName, Drop.DroppedItem.TABLE); + validateNotEmpty(keyspaceName, "Keyspace name"); + this.keyspaceName = Optional.fromNullable(keyspaceName); + this.itemName = viewName; + } - @Override - public String buildInternal() { - StringBuilder dropStatement = new StringBuilder("DROP " + itemType + " "); - if (ifExists) { - dropStatement.append("IF EXISTS "); - } - if (keyspaceName.isPresent()) { - dropStatement.append(keyspaceName.get()).append("."); - } + /** + * Add the 'IF EXISTS' condition to this DROP statement. + * + * @return this statement. + */ + public Drop ifExists() { + this.ifExists = true; + return this; + } - dropStatement.append(itemName); - return dropStatement.toString(); - } + @Override + public String buildInternal() { + StringBuilder dropStatement = new StringBuilder("DROP MATERIALIZED VIEW "); + if (ifExists) { + dropStatement.append("IF EXISTS "); + } + if (keyspaceName.isPresent()) { + dropStatement.append(keyspaceName.get()).append("."); + } - enum DroppedItem { - TABLE, TYPE, INDEX, MATERIALIZED_VIEW - } + dropStatement.append(itemName); + return dropStatement.toString(); + } + + enum DroppedItem { + TABLE, + TYPE, + INDEX, + MATERIALIZED_VIEW + } } diff --git a/src/main/java/net/helenus/config/DefaultHelenusSettings.java b/src/main/java/net/helenus/config/DefaultHelenusSettings.java index a8daa1c..500fed5 100644 --- a/src/main/java/net/helenus/config/DefaultHelenusSettings.java +++ b/src/main/java/net/helenus/config/DefaultHelenusSettings.java @@ -17,7 +17,6 @@ package net.helenus.config; import java.lang.reflect.Method; import java.util.function.Function; - import net.helenus.core.DslInstantiator; import net.helenus.core.MapperInstantiator; import net.helenus.core.reflect.ReflectionDslInstantiator; @@ -26,23 +25,23 @@ import net.helenus.mapping.convert.CamelCaseToUnderscoreConverter; public class DefaultHelenusSettings implements HelenusSettings { - @Override - public Function getPropertyToColumnConverter() { - return CamelCaseToUnderscoreConverter.INSTANCE; - } + @Override + public Function getPropertyToColumnConverter() { + return CamelCaseToUnderscoreConverter.INSTANCE; + } - @Override - public Function getGetterMethodDetector() { - return GetterMethodDetector.INSTANCE; - } + @Override + public Function getGetterMethodDetector() { + return GetterMethodDetector.INSTANCE; + } - @Override - public DslInstantiator getDslInstantiator() { - return ReflectionDslInstantiator.INSTANCE; - } + @Override + public DslInstantiator getDslInstantiator() { + return ReflectionDslInstantiator.INSTANCE; + } - @Override - public MapperInstantiator getMapperInstantiator() { - return ReflectionMapperInstantiator.INSTANCE; - } + @Override + public MapperInstantiator getMapperInstantiator() { + return ReflectionMapperInstantiator.INSTANCE; + } } diff --git a/src/main/java/net/helenus/config/GetterMethodDetector.java b/src/main/java/net/helenus/config/GetterMethodDetector.java index a4535f5..60a9ec0 100644 --- a/src/main/java/net/helenus/config/GetterMethodDetector.java +++ b/src/main/java/net/helenus/config/GetterMethodDetector.java @@ -18,32 +18,31 @@ package net.helenus.config; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.function.Function; - import net.helenus.mapping.annotation.Transient; public enum GetterMethodDetector implements Function { - INSTANCE; + INSTANCE; - @Override - public Boolean apply(Method method) { + @Override + public Boolean apply(Method method) { - if (method == null) { - throw new IllegalArgumentException("empty parameter"); - } + if (method == null) { + throw new IllegalArgumentException("empty parameter"); + } - if (method.getParameterCount() != 0 || method.getReturnType() == void.class) { - return false; - } + if (method.getParameterCount() != 0 || method.getReturnType() == void.class) { + return false; + } - if (Modifier.isStatic(method.getModifiers())) { - return false; - } + if (Modifier.isStatic(method.getModifiers())) { + return false; + } - // Methods marked "Transient" are not mapped, skip them. - if (method.getDeclaredAnnotation(Transient.class) != null) { - return false; - } + // Methods marked "Transient" are not mapped, skip them. + if (method.getDeclaredAnnotation(Transient.class) != null) { + return false; + } - return true; - } + return true; + } } diff --git a/src/main/java/net/helenus/config/HelenusSettings.java b/src/main/java/net/helenus/config/HelenusSettings.java index 27d1431..c8ffee2 100644 --- a/src/main/java/net/helenus/config/HelenusSettings.java +++ b/src/main/java/net/helenus/config/HelenusSettings.java @@ -17,17 +17,16 @@ package net.helenus.config; import java.lang.reflect.Method; import java.util.function.Function; - import net.helenus.core.DslInstantiator; import net.helenus.core.MapperInstantiator; public interface HelenusSettings { - Function getPropertyToColumnConverter(); + Function getPropertyToColumnConverter(); - Function getGetterMethodDetector(); + Function getGetterMethodDetector(); - DslInstantiator getDslInstantiator(); + DslInstantiator getDslInstantiator(); - MapperInstantiator getMapperInstantiator(); + MapperInstantiator getMapperInstantiator(); } diff --git a/src/main/java/net/helenus/core/AbstractAuditedEntityDraft.java b/src/main/java/net/helenus/core/AbstractAuditedEntityDraft.java index f2b16ff..a9a09e2 100644 --- a/src/main/java/net/helenus/core/AbstractAuditedEntityDraft.java +++ b/src/main/java/net/helenus/core/AbstractAuditedEntityDraft.java @@ -3,37 +3,36 @@ package net.helenus.core; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; - import net.helenus.core.reflect.MapExportable; public abstract class AbstractAuditedEntityDraft extends AbstractEntityDraft { - public AbstractAuditedEntityDraft(MapExportable entity) { - super(entity); + public AbstractAuditedEntityDraft(MapExportable entity) { + super(entity); - Date in = new Date(); - LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault()); - Date now = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant()); + Date in = new Date(); + LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault()); + Date now = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant()); - String who = getCurrentAuditor(); + String who = getCurrentAuditor(); - if (entity == null) { - if (who != null) { - set("createdBy", who); - } - set("createdAt", now); - } - if (who != null) { - set("modifiedBy", who); - } - set("modifiedAt", now); - } + if (entity == null) { + if (who != null) { + set("createdBy", who); + } + set("createdAt", now); + } + if (who != null) { + set("modifiedBy", who); + } + set("modifiedAt", now); + } - protected String getCurrentAuditor() { - return null; - } + protected String getCurrentAuditor() { + return null; + } - public Date createdAt() { - return (Date) get("createdAt", Date.class); - } + public Date createdAt() { + return (Date) get("createdAt", Date.class); + } } diff --git a/src/main/java/net/helenus/core/AbstractEntityDraft.java b/src/main/java/net/helenus/core/AbstractEntityDraft.java index 94005ff..0101351 100644 --- a/src/main/java/net/helenus/core/AbstractEntityDraft.java +++ b/src/main/java/net/helenus/core/AbstractEntityDraft.java @@ -1,174 +1,171 @@ package net.helenus.core; +import com.google.common.primitives.Primitives; import java.io.Serializable; import java.util.*; - -import org.apache.commons.lang3.SerializationUtils; - -import com.google.common.primitives.Primitives; - import net.helenus.core.reflect.DefaultPrimitiveTypes; import net.helenus.core.reflect.Drafted; import net.helenus.core.reflect.MapExportable; import net.helenus.mapping.MappingUtil; +import org.apache.commons.lang3.SerializationUtils; public abstract class AbstractEntityDraft implements Drafted { - private final Map backingMap = new HashMap(); - private final MapExportable entity; - private final Map entityMap; + private final Map backingMap = new HashMap(); + private final MapExportable entity; + private final Map entityMap; - public AbstractEntityDraft(MapExportable entity) { - this.entity = entity; - this.entityMap = entity != null ? entity.toMap() : new HashMap(); - } + public AbstractEntityDraft(MapExportable entity) { + this.entity = entity; + this.entityMap = entity != null ? entity.toMap() : new HashMap(); + } - public abstract Class getEntityClass(); + public abstract Class getEntityClass(); - public E build() { - return Helenus.map(getEntityClass(), toMap()); - } + public E build() { + return Helenus.map(getEntityClass(), toMap()); + } - @SuppressWarnings("unchecked") - public T get(Getter getter, Class returnType) { - return (T) get(this.methodNameFor(getter), returnType); - } + @SuppressWarnings("unchecked") + public T get(Getter getter, Class returnType) { + return (T) get(this.methodNameFor(getter), returnType); + } - @SuppressWarnings("unchecked") - public T get(String key, Class returnType) { - T value = (T) backingMap.get(key); + @SuppressWarnings("unchecked") + public T get(String key, Class returnType) { + T value = (T) backingMap.get(key); - if (value == null) { - value = (T) entityMap.get(key); - if (value == null) { + if (value == null) { + value = (T) entityMap.get(key); + if (value == null) { - if (Primitives.allPrimitiveTypes().contains(returnType)) { + if (Primitives.allPrimitiveTypes().contains(returnType)) { - DefaultPrimitiveTypes type = DefaultPrimitiveTypes.lookup(returnType); - if (type == null) { - throw new RuntimeException("unknown primitive type " + returnType); - } + DefaultPrimitiveTypes type = DefaultPrimitiveTypes.lookup(returnType); + if (type == null) { + throw new RuntimeException("unknown primitive type " + returnType); + } - return (T) type.getDefaultValue(); - } - } else { - // Collections fetched from the entityMap - if (value instanceof Collection) { - try { - value = MappingUtil.clone(value); - } catch (CloneNotSupportedException e) { - // TODO(gburd): deep?shallow? copy of List, Map, Set to a mutable collection. - value = (T) SerializationUtils.clone((Serializable) value); - } - } - } - } + return (T) type.getDefaultValue(); + } + } else { + // Collections fetched from the entityMap + if (value instanceof Collection) { + try { + value = MappingUtil.clone(value); + } catch (CloneNotSupportedException e) { + // TODO(gburd): deep?shallow? copy of List, Map, Set to a mutable collection. + value = (T) SerializationUtils.clone((Serializable) value); + } + } + } + } - return value; - } + return value; + } - public Object set(Getter getter, Object value) { - return set(this.methodNameFor(getter), value); - } + public Object set(Getter getter, Object value) { + return set(this.methodNameFor(getter), value); + } - public Object set(String key, Object value) { - if (key == null || value == null) { - return null; - } + public Object set(String key, Object value) { + if (key == null || value == null) { + return null; + } - backingMap.put(key, value); - return value; - } + backingMap.put(key, value); + return value; + } - @SuppressWarnings("unchecked") - public T mutate(Getter getter, T value) { - return (T) mutate(this.methodNameFor(getter), value); - } + @SuppressWarnings("unchecked") + public T mutate(Getter getter, T value) { + return (T) mutate(this.methodNameFor(getter), value); + } - public Object mutate(String key, Object value) { - Objects.requireNonNull(key); + public Object mutate(String key, Object value) { + Objects.requireNonNull(key); - if (value == null) { - return null; - } + if (value == null) { + return null; + } - if (entity != null) { - Map map = entity.toMap(); + if (entity != null) { + Map map = entity.toMap(); - if (map.containsKey(key) && !value.equals(map.get(key))) { - backingMap.put(key, value); - return value; - } + if (map.containsKey(key) && !value.equals(map.get(key))) { + backingMap.put(key, value); + return value; + } - return map.get(key); - } else { - backingMap.put(key, value); + return map.get(key); + } else { + backingMap.put(key, value); - return null; - } - } + return null; + } + } - private String methodNameFor(Getter getter) { - return MappingUtil.resolveMappingProperty(getter).getProperty().getPropertyName(); - } + private String methodNameFor(Getter getter) { + return MappingUtil.resolveMappingProperty(getter).getProperty().getPropertyName(); + } - public Object unset(Getter getter) { - return unset(methodNameFor(getter)); - } + public Object unset(Getter getter) { + return unset(methodNameFor(getter)); + } - public Object unset(String key) { - if (key != null) { - Object value = backingMap.get(key); - backingMap.put(key, null); - return value; - } - return null; - } + public Object unset(String key) { + if (key != null) { + Object value = backingMap.get(key); + backingMap.put(key, null); + return value; + } + return null; + } - public boolean reset(Getter getter, T desiredValue) { - return this.reset(this.methodNameFor(getter), desiredValue); - } + public boolean reset(Getter getter, T desiredValue) { + return this.reset(this.methodNameFor(getter), desiredValue); + } - public boolean reset(String key, T desiredValue) { - if (key != null && desiredValue != null) { - @SuppressWarnings("unchecked") - T currentValue = (T) backingMap.get(key); - if (currentValue == null || !currentValue.equals(desiredValue)) { - set(key, desiredValue); - return true; - } - } - return false; - } + public boolean reset(String key, T desiredValue) { + if (key != null && desiredValue != null) { + @SuppressWarnings("unchecked") + T currentValue = (T) backingMap.get(key); + if (currentValue == null || !currentValue.equals(desiredValue)) { + set(key, desiredValue); + return true; + } + } + return false; + } - @Override - public Map toMap() { - return toMap(entityMap); - } + @Override + public Map toMap() { + return toMap(entityMap); + } - public Map toMap(Map entityMap) { - Map combined; - if (entityMap != null && entityMap.size() > 0) { - combined = new HashMap(entityMap.size()); - for (String key : entityMap.keySet()) { - combined.put(key, entityMap.get(key)); - } - } else { - combined = new HashMap(backingMap.size()); - } - for (String key : mutated()) { - combined.put(key, backingMap.get(key)); - } - return combined; - } + public Map toMap(Map entityMap) { + Map combined; + if (entityMap != null && entityMap.size() > 0) { + combined = new HashMap(entityMap.size()); + for (Map.Entry e : entityMap.entrySet()) { + combined.put(e.getKey(), e.getValue()); + } + } else { + combined = new HashMap(backingMap.size()); + } + for (String key : mutated()) { + combined.put(key, backingMap.get(key)); + } + return combined; + } - @Override - public Set mutated() { - return backingMap.keySet(); - } + @Override + public Set mutated() { + return backingMap.keySet(); + } - @Override - public String toString() { - return backingMap.toString(); - } + @Override + public String toString() { + return backingMap.toString(); + } } diff --git a/src/main/java/net/helenus/core/AbstractSessionOperations.java b/src/main/java/net/helenus/core/AbstractSessionOperations.java index 3f8eba2..ba92e44 100644 --- a/src/main/java/net/helenus/core/AbstractSessionOperations.java +++ b/src/main/java/net/helenus/core/AbstractSessionOperations.java @@ -15,143 +15,139 @@ */ package net.helenus.core; -import java.io.PrintStream; -import java.util.List; -import java.util.concurrent.Executor; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - +import brave.Tracer; import com.codahale.metrics.MetricRegistry; import com.datastax.driver.core.*; import com.google.common.base.Stopwatch; import com.google.common.collect.Table; import com.google.common.util.concurrent.ListenableFuture; - -import brave.Tracer; +import java.io.PrintStream; +import java.util.List; +import java.util.concurrent.Executor; 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.Either; import net.helenus.support.HelenusException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public abstract class AbstractSessionOperations { - private static final Logger LOG = LoggerFactory.getLogger(AbstractSessionOperations.class); + private static final Logger LOG = LoggerFactory.getLogger(AbstractSessionOperations.class); - public abstract Session currentSession(); + public abstract Session currentSession(); - public abstract String usingKeyspace(); + public abstract String usingKeyspace(); - public abstract boolean isShowCql(); + public abstract boolean isShowCql(); - public abstract PrintStream getPrintStream(); + public abstract PrintStream getPrintStream(); - public abstract Executor getExecutor(); + public abstract Executor getExecutor(); - public abstract SessionRepository getSessionRepository(); + public abstract SessionRepository getSessionRepository(); - public abstract ColumnValueProvider getValueProvider(); + public abstract ColumnValueProvider getValueProvider(); - public abstract ColumnValuePreparer getValuePreparer(); + public abstract ColumnValuePreparer getValuePreparer(); - public abstract ConsistencyLevel getDefaultConsistencyLevel(); + public abstract ConsistencyLevel getDefaultConsistencyLevel(); - public abstract boolean getDefaultQueryIdempotency(); + public abstract boolean getDefaultQueryIdempotency(); - public PreparedStatement prepare(RegularStatement statement) { - try { - logStatement(statement, false); - return currentSession().prepare(statement); - } catch (RuntimeException e) { - throw translateException(e); - } - } + public PreparedStatement prepare(RegularStatement statement) { + try { + logStatement(statement, false); + return currentSession().prepare(statement); + } catch (RuntimeException e) { + throw translateException(e); + } + } - public ListenableFuture prepareAsync(RegularStatement statement) { - try { - logStatement(statement, false); - return currentSession().prepareAsync(statement); - } catch (RuntimeException e) { - throw translateException(e); - } - } + public ListenableFuture prepareAsync(RegularStatement statement) { + try { + logStatement(statement, false); + return currentSession().prepareAsync(statement); + } catch (RuntimeException e) { + throw translateException(e); + } + } - public ResultSet execute(Statement statement, boolean showValues) { - return execute(statement, null, null, showValues); - } + public ResultSet execute(Statement statement, boolean showValues) { + return execute(statement, null, null, showValues); + } - public ResultSet execute(Statement statement, Stopwatch timer, boolean showValues) { - return execute(statement, null, timer, showValues); - } + public ResultSet execute(Statement statement, Stopwatch timer, boolean showValues) { + return execute(statement, null, timer, showValues); + } - public ResultSet execute(Statement statement, UnitOfWork uow, boolean showValues) { - return execute(statement, uow, null, showValues); - } + public ResultSet execute(Statement statement, UnitOfWork uow, boolean showValues) { + return execute(statement, uow, null, showValues); + } - public ResultSet execute(Statement statement, UnitOfWork uow, Stopwatch timer, boolean showValues) { - return executeAsync(statement, uow, timer, showValues).getUninterruptibly(); - } + public ResultSet execute( + Statement statement, UnitOfWork uow, Stopwatch timer, boolean showValues) { + return executeAsync(statement, uow, timer, showValues).getUninterruptibly(); + } - public ResultSetFuture executeAsync(Statement statement, boolean showValues) { - return executeAsync(statement, null, null, showValues); - } + public ResultSetFuture executeAsync(Statement statement, boolean showValues) { + return executeAsync(statement, null, null, showValues); + } - public ResultSetFuture executeAsync(Statement statement, Stopwatch timer, boolean showValues) { - return executeAsync(statement, null, timer, showValues); - } + public ResultSetFuture executeAsync(Statement statement, Stopwatch timer, boolean showValues) { + return executeAsync(statement, null, timer, showValues); + } - public ResultSetFuture executeAsync(Statement statement, UnitOfWork uow, boolean showValues) { - return executeAsync(statement, uow, null, showValues); - } + public ResultSetFuture executeAsync(Statement statement, UnitOfWork uow, boolean showValues) { + return executeAsync(statement, uow, null, showValues); + } - public ResultSetFuture executeAsync(Statement statement, UnitOfWork uow, Stopwatch timer, boolean showValues) { - try { - logStatement(statement, showValues); - return currentSession().executeAsync(statement); - } catch (RuntimeException e) { - throw translateException(e); - } - } + public ResultSetFuture executeAsync( + Statement statement, UnitOfWork uow, Stopwatch timer, boolean showValues) { + try { + logStatement(statement, showValues); + return currentSession().executeAsync(statement); + } catch (RuntimeException e) { + throw translateException(e); + } + } - 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)); - } - } + private void logStatement(Statement statement, boolean showValues) { + if (isShowCql()) { + printCql(Operation.queryString(statement, showValues)); + } else if (LOG.isDebugEnabled()) { + LOG.info("CQL> " + Operation.queryString(statement, showValues)); + } + } - public Tracer getZipkinTracer() { - return null; - } + public Tracer getZipkinTracer() { + return null; + } - public MetricRegistry getMetricRegistry() { - return null; - } + public MetricRegistry getMetricRegistry() { + return null; + } - public void mergeCache(Table>> uowCache) { - } + public void mergeCache(Table>> uowCache) {} - RuntimeException translateException(RuntimeException e) { - if (e instanceof HelenusException) { - return e; - } - throw new HelenusException(e); - } + RuntimeException translateException(RuntimeException e) { + if (e instanceof HelenusException) { + return e; + } + throw new HelenusException(e); + } - public Object checkCache(String tableName, List facets) { - return null; - } + public Object checkCache(String tableName, List facets) { + return null; + } - public void updateCache(Object pojo, List facets) { - } + public void updateCache(Object pojo, List facets) {} - void printCql(String cql) { - getPrintStream().println(cql); - } + void printCql(String cql) { + getPrintStream().println(cql); + } - public void cacheEvict(List facets) { - } + public void cacheEvict(List facets) {} } diff --git a/src/main/java/net/helenus/core/AbstractUnitOfWork.java b/src/main/java/net/helenus/core/AbstractUnitOfWork.java index 1b4454d..f658956 100644 --- a/src/main/java/net/helenus/core/AbstractUnitOfWork.java +++ b/src/main/java/net/helenus/core/AbstractUnitOfWork.java @@ -17,349 +17,391 @@ package net.helenus.core; import static net.helenus.core.HelenusSession.deleted; -import java.util.*; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.diffplug.common.base.Errors; import com.google.common.base.Stopwatch; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; import com.google.common.collect.TreeTraverser; - +import java.util.*; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import net.helenus.core.cache.CacheUtil; import net.helenus.core.cache.Facet; import net.helenus.support.Either; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** Encapsulates the concept of a "transaction" as a unit-of-work. */ -public abstract class AbstractUnitOfWork implements UnitOfWork, AutoCloseable { +public abstract class AbstractUnitOfWork + implements UnitOfWork, AutoCloseable { - private static final Logger LOG = LoggerFactory.getLogger(AbstractUnitOfWork.class); + private static final Logger LOG = LoggerFactory.getLogger(AbstractUnitOfWork.class); - private final List> nested = new ArrayList<>(); - private final HelenusSession session; - private final AbstractUnitOfWork parent; - private final Table>> cache = HashBasedTable.create(); - protected String purpose; - protected List nestedPurposes = new ArrayList(); - protected int cacheHits = 0; - protected int cacheMisses = 0; - protected int databaseLookups = 0; - protected Stopwatch elapsedTime; - protected Map databaseTime = new HashMap<>(); - protected double cacheLookupTime = 0.0; - private List postCommit = new ArrayList(); - private boolean aborted = false; - private boolean committed = false; + private final List> nested = new ArrayList<>(); + private final HelenusSession session; + private final AbstractUnitOfWork parent; + private final Table>> cache = HashBasedTable.create(); + protected String purpose; + protected List nestedPurposes = new ArrayList(); + protected String info; + protected int cacheHits = 0; + protected int cacheMisses = 0; + protected int databaseLookups = 0; + protected Stopwatch elapsedTime; + protected Map databaseTime = new HashMap<>(); + protected double cacheLookupTime = 0.0; + private List postCommit = new ArrayList(); + private boolean aborted = false; + private boolean committed = false; - protected AbstractUnitOfWork(HelenusSession session, AbstractUnitOfWork parent) { - Objects.requireNonNull(session, "containing session cannot be null"); + protected AbstractUnitOfWork(HelenusSession session, AbstractUnitOfWork parent) { + Objects.requireNonNull(session, "containing session cannot be null"); - this.session = session; - this.parent = parent; - } + this.session = session; + this.parent = parent; + } - @Override - public void addDatabaseTime(String name, Stopwatch amount) { - Double time = databaseTime.get(name); - if (time == null) { - databaseTime.put(name, (double) amount.elapsed(TimeUnit.MICROSECONDS)); - } else { - databaseTime.put(name, time + amount.elapsed(TimeUnit.MICROSECONDS)); - } - } + @Override + public void addDatabaseTime(String name, Stopwatch amount) { + Double time = databaseTime.get(name); + if (time == null) { + databaseTime.put(name, (double) amount.elapsed(TimeUnit.MICROSECONDS)); + } else { + databaseTime.put(name, time + amount.elapsed(TimeUnit.MICROSECONDS)); + } + } - @Override - public void addCacheLookupTime(Stopwatch amount) { - cacheLookupTime += amount.elapsed(TimeUnit.MICROSECONDS); - } + @Override + public void addCacheLookupTime(Stopwatch amount) { + cacheLookupTime += amount.elapsed(TimeUnit.MICROSECONDS); + } - @Override - public void addNestedUnitOfWork(UnitOfWork uow) { - synchronized (nested) { - nested.add((AbstractUnitOfWork) uow); - } - } + @Override + public void addNestedUnitOfWork(UnitOfWork uow) { + synchronized (nested) { + nested.add((AbstractUnitOfWork) uow); + } + } - @Override - public synchronized UnitOfWork begin() { - if (LOG.isInfoEnabled()) { - elapsedTime = Stopwatch.createStarted(); - } - // log.record(txn::start) - return this; - } + @Override + public synchronized UnitOfWork begin() { + if (LOG.isInfoEnabled()) { + elapsedTime = Stopwatch.createStarted(); + } + // log.record(txn::start) + return this; + } - @Override - public String getPurpose() { - return purpose; - } + @Override + public String getPurpose() { + return purpose; + } - @Override - public UnitOfWork setPurpose(String purpose) { - this.purpose = purpose; - return this; - } + @Override + public UnitOfWork setPurpose(String purpose) { + this.purpose = purpose; + return this; + } - @Override - public void recordCacheAndDatabaseOperationCount(int cache, int ops) { - if (cache > 0) { - cacheHits += cache; - } else { - cacheMisses += Math.abs(cache); - } - if (ops > 0) { - databaseLookups += ops; - } - } + @Override + public void setInfo(String info) { + this.info = info; + } - public String logTimers(String what) { - double e = (double) elapsedTime.elapsed(TimeUnit.MICROSECONDS) / 1000.0; - double d = 0.0; - double c = cacheLookupTime / 1000.0; - double fc = (c / e) * 100.0; - String database = ""; - if (databaseTime.size() > 0) { - List 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 x = nestedPurposes.stream().distinct().collect(Collectors.joining(", ")); - 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%s", hashCode(), - (nested.size() > 0 ? ", [" + n + "]" : ""), what, e, cache, database, da, - (purpose == null ? "" : " " + purpose), (nestedPurposes.isEmpty()) ? "" : ", " + x); - return s; - } + @Override + public void recordCacheAndDatabaseOperationCount(int cache, int ops) { + if (cache > 0) { + cacheHits += cache; + } else { + cacheMisses += Math.abs(cache); + } + if (ops > 0) { + databaseLookups += ops; + } + } - private void applyPostCommitFunctions() { - if (!postCommit.isEmpty()) { - for (CommitThunk f : postCommit) { - f.apply(); - } - } - if (LOG.isInfoEnabled()) { - LOG.info(logTimers("committed")); - } - } + public String logTimers(String what) { + double e = (double) elapsedTime.elapsed(TimeUnit.MICROSECONDS) / 1000.0; + double d = 0.0; + double c = cacheLookupTime / 1000.0; + double fc = (c / e) * 100.0; + String database = ""; + if (databaseTime.size() > 0) { + List dbt = new ArrayList<>(databaseTime.size()); + for (Map.Entry dt : databaseTime.entrySet()) { + double t = dt.getValue() / 1000.0; + d += t; + dbt.add(String.format("%s took %,.3fms %,2.2f%%", dt.getKey(), 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 x = nestedPurposes.stream().distinct().collect(Collectors.joining(", ")); + 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%s%s", + hashCode(), + (nested.size() > 0 ? ", [" + n + "]" : ""), + what, + e, + cache, + database, + da, + (purpose == null ? "" : " " + purpose), + (nestedPurposes.isEmpty()) ? "" : ", " + x, + (info == null) ? "" : " " + info); + return s; + } - @Override - public Optional cacheLookup(List facets) { - String tableName = CacheUtil.schemaName(facets); - Optional result = Optional.empty(); - for (Facet facet : facets) { - if (!facet.fixed()) { - String columnName = facet.name() + "==" + facet.value(); - Either> eitherValue = cache.get(tableName, columnName); - if (eitherValue != null) { - Object value = deleted; - if (eitherValue.isLeft()) { - value = eitherValue.getLeft(); - } - result = Optional.of(value); - break; - } - } - } - if (!result.isPresent()) { - // Be sure to check all enclosing UnitOfWork caches as well, we may be nested. - if (parent != null) { - return parent.cacheLookup(facets); - } - } - return result; - } + private void applyPostCommitFunctions() { + if (!postCommit.isEmpty()) { + for (CommitThunk f : postCommit) { + f.apply(); + } + } + if (LOG.isInfoEnabled()) { + LOG.info(logTimers("committed")); + } + } - @Override - public List cacheEvict(List facets) { - Either> deletedObjectFacets = Either.right(facets); - String tableName = CacheUtil.schemaName(facets); - Optional optionalValue = cacheLookup(facets); - if (optionalValue.isPresent()) { - Object value = optionalValue.get(); + @Override + public Optional cacheLookup(List facets) { + String tableName = CacheUtil.schemaName(facets); + Optional result = Optional.empty(); + for (Facet facet : facets) { + if (!facet.fixed()) { + String columnName = facet.name() + "==" + facet.value(); + Either> eitherValue = cache.get(tableName, columnName); + if (eitherValue != null) { + Object value = deleted; + if (eitherValue.isLeft()) { + value = eitherValue.getLeft(); + } + result = Optional.of(value); + break; + } + } + } + if (!result.isPresent()) { + // Be sure to check all enclosing UnitOfWork caches as well, we may be nested. + if (parent != null) { + return parent.cacheLookup(facets); + } + } + return result; + } - 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> 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(parts[0], parts[1])); - } - } - }); - } - return facets; - } + @Override + public List cacheEvict(List facets) { + Either> deletedObjectFacets = Either.right(facets); + String tableName = CacheUtil.schemaName(facets); + Optional optionalValue = cacheLookup(facets); + if (optionalValue.isPresent()) { + Object value = optionalValue.get(); - @Override - public void cacheUpdate(Object value, List facets) { - String tableName = CacheUtil.schemaName(facets); - for (Facet facet : facets) { - if (!facet.fixed()) { - String columnName = facet.name() + "==" + facet.value(); - cache.put(tableName, columnName, Either.left(value)); - } - } - } + 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> 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(parts[0], parts[1])); + } + } + }); + } + return facets; + } - private Iterator> getChildNodes() { - return nested.iterator(); - } + @Override + public void cacheUpdate(Object value, List facets) { + String tableName = CacheUtil.schemaName(facets); + for (Facet facet : facets) { + if (!facet.fixed()) { + String columnName = facet.name() + "==" + facet.value(); + cache.put(tableName, columnName, Either.left(value)); + } + } + } - /** - * Checks to see if the work performed between calling begin and now can be - * committed or not. - * - * @return a function from which to chain work that only happens when commit is - * successful - * @throws E - * when the work overlaps with other concurrent writers. - */ - public PostCommitFunction commit() throws E { - // All nested UnitOfWork should be committed (not aborted) before calls to - // commit, check. - boolean canCommit = true; - TreeTraverser> traverser = TreeTraverser.using(node -> node::getChildNodes); - for (AbstractUnitOfWork uow : traverser.postOrderTraversal(this)) { - if (this != uow) { - canCommit &= (!uow.aborted && uow.committed); - } - } + private Iterator> getChildNodes() { + return nested.iterator(); + } - // log.record(txn::provisionalCommit) - // examine log for conflicts in read-set and write-set between begin and - // provisional commit - // if (conflict) { throw new ConflictingUnitOfWorkException(this) } - // else return function so as to enable commit.andThen(() -> { do something iff - // commit was successful; }) + /** + * Checks to see if the work performed between calling begin and now can be committed or not. + * + * @return a function from which to chain work that only happens when commit is successful + * @throws E when the work overlaps with other concurrent writers. + */ + public PostCommitFunction commit() throws E { + // All nested UnitOfWork should be committed (not aborted) before calls to + // commit, check. + boolean canCommit = true; + TreeTraverser> traverser = + TreeTraverser.using(node -> node::getChildNodes); + for (AbstractUnitOfWork uow : traverser.postOrderTraversal(this)) { + if (this != uow) { + canCommit &= (!uow.aborted && uow.committed); + } + } - if (canCommit) { - committed = true; - aborted = false; + // log.record(txn::provisionalCommit) + // examine log for conflicts in read-set and write-set between begin and + // provisional commit + // if (conflict) { throw new ConflictingUnitOfWorkException(this) } + // else return function so as to enable commit.andThen(() -> { do something iff + // commit was successful; }) - nested.forEach((uow) -> Errors.rethrow().wrap(uow::commit)); - elapsedTime.stop(); + if (canCommit) { + committed = true; + aborted = false; - if (parent == null) { - // Apply all post-commit functions, this is the outter-most UnitOfWork. - traverser.postOrderTraversal(this).forEach(uow -> { - uow.applyPostCommitFunctions(); - }); + nested.forEach((uow) -> Errors.rethrow().wrap(uow::commit)); + elapsedTime.stop(); - // Merge our cache into the session cache. - session.mergeCache(cache); + 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); - } else { + // Merge our cache into the session cache. + session.mergeCache(cache); - // Merge cache and statistics into parent if there is one. - parent.mergeCache(cache); - if (purpose != null) { - parent.nestedPurposes.add(purpose); - } - 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 ctor = clazz.getConstructor(conflictExceptionClass); - // T object = ctor.newInstance(new Object[] { String message }); - // } - return new PostCommitFunction(this, postCommit); - } + return new PostCommitFunction(this, null); + } else { - /* Explicitly discard the work and mark it as as such in the log. */ - public synchronized void abort() { - TreeTraverser> traverser = TreeTraverser.using(node -> node::getChildNodes); - traverser.postOrderTraversal(this).forEach(uow -> { - uow.committed = false; - uow.aborted = true; - }); - // log.record(txn::abort) - // cache.invalidateSince(txn::start time) - if (LOG.isInfoEnabled()) { - if (elapsedTime.isRunning()) { - elapsedTime.stop(); - } - LOG.info(logTimers("aborted")); - } - } + // Merge cache and statistics into parent if there is one. + parent.mergeCache(cache); + if (purpose != null) { + parent.nestedPurposes.add(purpose); + } + parent.cacheHits += cacheHits; + parent.cacheMisses += cacheMisses; + parent.databaseLookups += databaseLookups; + parent.cacheLookupTime += cacheLookupTime; + for (Map.Entry dt : databaseTime.entrySet()) { + String name = dt.getKey(); + if (parent.databaseTime.containsKey(name)) { + double t = parent.databaseTime.get(name); + parent.databaseTime.put(name, t + dt.getValue()); + } else { + parent.databaseTime.put(name, dt.getValue()); + } + } + } + } + // else { + // Constructor ctor = clazz.getConstructor(conflictExceptionClass); + // T object = ctor.newInstance(new Object[] { String message }); + // } + return new PostCommitFunction(this, postCommit); + } - private void mergeCache(Table>> from) { - Table>> to = this.cache; - from.rowMap().forEach((rowKey, columnMap) -> { - columnMap.forEach((columnKey, value) -> { - if (to.contains(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)); - } - }); - }); - } + /* Explicitly discard the work and mark it as as such in the log. */ + public synchronized void abort() { + TreeTraverser> traverser = + TreeTraverser.using(node -> node::getChildNodes); + traverser + .postOrderTraversal(this) + .forEach( + uow -> { + uow.committed = false; + uow.aborted = true; + }); + // log.record(txn::abort) + // cache.invalidateSince(txn::start time) + if (LOG.isInfoEnabled()) { + if (elapsedTime.isRunning()) { + elapsedTime.stop(); + } + LOG.info(logTimers("aborted")); + } + } - public String describeConflicts() { - return "it's complex..."; - } + private void mergeCache(Table>> from) { + Table>> to = this.cache; + from.rowMap() + .forEach( + (rowKey, columnMap) -> { + columnMap.forEach( + (columnKey, value) -> { + if (to.contains(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)); + } + }); + }); + } - @Override - public void close() throws E { - // Closing a AbstractUnitOfWork will abort iff we've not already aborted or - // committed this unit of work. - if (aborted == false && committed == false) { - abort(); - } - } + public String describeConflicts() { + return "it's complex..."; + } - public boolean hasAborted() { - return aborted; - } + @Override + public void close() throws E { + // Closing a AbstractUnitOfWork will abort iff we've not already aborted or + // committed this unit of work. + if (aborted == false && committed == false) { + abort(); + } + } - public boolean hasCommitted() { - return committed; - } + public boolean hasAborted() { + return aborted; + } + + public boolean hasCommitted() { + return committed; + } } diff --git a/src/main/java/net/helenus/core/AutoDdl.java b/src/main/java/net/helenus/core/AutoDdl.java index d578cfd..c1e2626 100644 --- a/src/main/java/net/helenus/core/AutoDdl.java +++ b/src/main/java/net/helenus/core/AutoDdl.java @@ -16,5 +16,8 @@ package net.helenus.core; public enum AutoDdl { - VALIDATE, UPDATE, CREATE, CREATE_DROP; + VALIDATE, + UPDATE, + CREATE, + CREATE_DROP; } diff --git a/src/main/java/net/helenus/core/CommitThunk.java b/src/main/java/net/helenus/core/CommitThunk.java index 11b1e30..1ad9e05 100644 --- a/src/main/java/net/helenus/core/CommitThunk.java +++ b/src/main/java/net/helenus/core/CommitThunk.java @@ -2,5 +2,5 @@ package net.helenus.core; @FunctionalInterface public interface CommitThunk { - void apply(); + void apply(); } diff --git a/src/main/java/net/helenus/core/ConflictingUnitOfWorkException.java b/src/main/java/net/helenus/core/ConflictingUnitOfWorkException.java index e8ab229..ef2957a 100644 --- a/src/main/java/net/helenus/core/ConflictingUnitOfWorkException.java +++ b/src/main/java/net/helenus/core/ConflictingUnitOfWorkException.java @@ -18,9 +18,9 @@ package net.helenus.core; public class ConflictingUnitOfWorkException extends Exception { - final UnitOfWork uow; + final UnitOfWork uow; - ConflictingUnitOfWorkException(UnitOfWork uow) { - this.uow = uow; - } + ConflictingUnitOfWorkException(UnitOfWork uow) { + this.uow = uow; + } } diff --git a/src/main/java/net/helenus/core/DslInstantiator.java b/src/main/java/net/helenus/core/DslInstantiator.java index 29b96de..d05914c 100644 --- a/src/main/java/net/helenus/core/DslInstantiator.java +++ b/src/main/java/net/helenus/core/DslInstantiator.java @@ -15,13 +15,15 @@ */ package net.helenus.core; -import java.util.Optional; - import com.datastax.driver.core.Metadata; - +import java.util.Optional; import net.helenus.core.reflect.HelenusPropertyNode; public interface DslInstantiator { - E instantiate(Class iface, ClassLoader classLoader, Optional parent, Metadata metadata); + E instantiate( + Class iface, + ClassLoader classLoader, + Optional parent, + Metadata metadata); } diff --git a/src/main/java/net/helenus/core/Filter.java b/src/main/java/net/helenus/core/Filter.java index 9eeb803..5edd5bb 100644 --- a/src/main/java/net/helenus/core/Filter.java +++ b/src/main/java/net/helenus/core/Filter.java @@ -15,102 +15,101 @@ */ package net.helenus.core; -import java.util.Objects; - import com.datastax.driver.core.querybuilder.Clause; - +import java.util.Objects; import net.helenus.core.reflect.HelenusPropertyNode; import net.helenus.mapping.MappingUtil; import net.helenus.mapping.value.ColumnValuePreparer; public final class Filter { - private final HelenusPropertyNode node; - private final Postulate postulate; + private final HelenusPropertyNode node; + private final Postulate postulate; - private Filter(HelenusPropertyNode node, Postulate postulate) { - this.node = node; - this.postulate = postulate; - } + private Filter(HelenusPropertyNode node, Postulate postulate) { + this.node = node; + this.postulate = postulate; + } - public static Filter equal(Getter getter, V val) { - return create(getter, Operator.EQ, val); - } + public static Filter equal(Getter getter, V val) { + return create(getter, Operator.EQ, val); + } - public static Filter in(Getter getter, V... vals) { - Objects.requireNonNull(getter, "empty getter"); - Objects.requireNonNull(vals, "empty values"); + public static Filter in(Getter getter, V... vals) { + Objects.requireNonNull(getter, "empty getter"); + Objects.requireNonNull(vals, "empty values"); - if (vals.length == 0) { - throw new IllegalArgumentException("values array is empty"); - } + if (vals.length == 0) { + throw new IllegalArgumentException("values array is empty"); + } - for (int i = 0; i != vals.length; ++i) { - Objects.requireNonNull(vals[i], "value[" + i + "] is empty"); - } + for (int i = 0; i != vals.length; ++i) { + Objects.requireNonNull(vals[i], "value[" + i + "] is empty"); + } - HelenusPropertyNode node = MappingUtil.resolveMappingProperty(getter); + HelenusPropertyNode node = MappingUtil.resolveMappingProperty(getter); - Postulate postulate = Postulate.of(Operator.IN, vals); + Postulate postulate = Postulate.of(Operator.IN, vals); - return new Filter(node, postulate); - } + return new Filter(node, postulate); + } - public static Filter greaterThan(Getter getter, V val) { - return create(getter, Operator.GT, val); - } + public static Filter greaterThan(Getter getter, V val) { + return create(getter, Operator.GT, val); + } - public static Filter lessThan(Getter getter, V val) { - return create(getter, Operator.LT, val); - } + public static Filter lessThan(Getter getter, V val) { + return create(getter, Operator.LT, val); + } - public static Filter greaterThanOrEqual(Getter getter, V val) { - return create(getter, Operator.GTE, val); - } + public static Filter greaterThanOrEqual(Getter getter, V val) { + return create(getter, Operator.GTE, val); + } - public static Filter lessThanOrEqual(Getter getter, V val) { - return create(getter, Operator.LTE, val); - } + public static Filter lessThanOrEqual(Getter getter, V val) { + return create(getter, Operator.LTE, val); + } - public static Filter create(Getter getter, Postulate postulate) { - Objects.requireNonNull(getter, "empty getter"); - Objects.requireNonNull(postulate, "empty operator"); + public static Filter create(Getter getter, Postulate postulate) { + Objects.requireNonNull(getter, "empty getter"); + Objects.requireNonNull(postulate, "empty operator"); - HelenusPropertyNode node = MappingUtil.resolveMappingProperty(getter); + HelenusPropertyNode node = MappingUtil.resolveMappingProperty(getter); - return new Filter(node, postulate); - } + return new Filter(node, postulate); + } - public static Filter create(Getter getter, Operator op, V val) { - Objects.requireNonNull(getter, "empty getter"); - Objects.requireNonNull(op, "empty op"); - Objects.requireNonNull(val, "empty value"); + public static Filter create(Getter getter, Operator op, V val) { + Objects.requireNonNull(getter, "empty getter"); + Objects.requireNonNull(op, "empty op"); + Objects.requireNonNull(val, "empty value"); - if (op == Operator.IN) { - throw new IllegalArgumentException("invalid usage of the 'in' operator, use Filter.in() static method"); - } + if (op == Operator.IN) { + throw new IllegalArgumentException( + "invalid usage of the 'in' operator, use Filter.in() static method"); + } - HelenusPropertyNode node = MappingUtil.resolveMappingProperty(getter); + HelenusPropertyNode node = MappingUtil.resolveMappingProperty(getter); - Postulate postulate = Postulate.of(op, val); + Postulate postulate = Postulate.of(op, val); - return new Filter(node, postulate); - } + return new Filter(node, postulate); + } - public HelenusPropertyNode getNode() { - return node; - } + public HelenusPropertyNode getNode() { + return node; + } - public Clause getClause(ColumnValuePreparer valuePreparer) { - return postulate.getClause(node, valuePreparer); - } + public Clause getClause(ColumnValuePreparer valuePreparer) { + return postulate.getClause(node, valuePreparer); + } - public V[] postulateValues() { - return postulate.values(); - } + public V[] postulateValues() { + return postulate.values(); + } - @Override - public String toString() { - return node.getColumnName() + postulate.toString(); - } + @Override + public String toString() { + return node.getColumnName() + postulate.toString(); + } } diff --git a/src/main/java/net/helenus/core/Getter.java b/src/main/java/net/helenus/core/Getter.java index 2cf9c39..7b7d5dd 100644 --- a/src/main/java/net/helenus/core/Getter.java +++ b/src/main/java/net/helenus/core/Getter.java @@ -17,5 +17,5 @@ package net.helenus.core; public interface Getter { - V get(); + V get(); } diff --git a/src/main/java/net/helenus/core/Helenus.java b/src/main/java/net/helenus/core/Helenus.java index eb6b493..edb15e2 100644 --- a/src/main/java/net/helenus/core/Helenus.java +++ b/src/main/java/net/helenus/core/Helenus.java @@ -15,17 +15,15 @@ */ package net.helenus.core; +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Metadata; +import com.datastax.driver.core.Session; import java.util.HashSet; import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; - -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.Metadata; -import com.datastax.driver.core.Session; - import net.helenus.config.DefaultHelenusSettings; import net.helenus.config.HelenusSettings; import net.helenus.core.reflect.DslExportable; @@ -35,161 +33,166 @@ import net.helenus.support.HelenusMappingException; public final class Helenus { - private static final ConcurrentMap, Object> dslCache = new ConcurrentHashMap, Object>(); - private static final ConcurrentMap, Metadata> metadataForEntity = new ConcurrentHashMap, Metadata>(); - private static final Set sessions = new HashSet(); - private static volatile HelenusSettings settings = new DefaultHelenusSettings(); - private static volatile HelenusSession singleton; + private static final ConcurrentMap, Object> dslCache = + new ConcurrentHashMap, Object>(); + private static final ConcurrentMap, Metadata> metadataForEntity = + new ConcurrentHashMap, Metadata>(); + private static final Set sessions = new HashSet(); + private static volatile HelenusSettings settings = new DefaultHelenusSettings(); + private static volatile HelenusSession singleton; - private Helenus() { - } + private Helenus() {} - protected static void setSession(HelenusSession session) { - sessions.add(session); - singleton = session; - } + protected static void setSession(HelenusSession session) { + sessions.add(session); + singleton = session; + } - public static HelenusSession session() { - return singleton; - } + public static HelenusSession session() { + return singleton; + } - public static void shutdown() { - sessions.forEach((session) -> { - session.close(); - sessions.remove(session); - }); - dslCache.clear(); - } + public static void shutdown() { + sessions.forEach( + (session) -> { + session.close(); + sessions.remove(session); + }); + dslCache.clear(); + } - public static HelenusSettings settings() { - return settings; - } + public static HelenusSettings settings() { + return settings; + } - public static HelenusSettings settings(HelenusSettings overrideSettings) { - HelenusSettings old = settings; - settings = overrideSettings; - return old; - } + public static HelenusSettings settings(HelenusSettings overrideSettings) { + HelenusSettings old = settings; + settings = overrideSettings; + return old; + } - public static SessionInitializer connect(Cluster cluster) { - Session session = cluster.connect(); - return new SessionInitializer(session); - } + public static SessionInitializer connect(Cluster cluster) { + Session session = cluster.connect(); + return new SessionInitializer(session); + } - public static SessionInitializer connect(Cluster cluster, String keyspace) { - Session session = cluster.connect(keyspace); - return new SessionInitializer(session); - } + public static SessionInitializer connect(Cluster cluster, String keyspace) { + Session session = cluster.connect(keyspace); + return new SessionInitializer(session); + } - public static SessionInitializer init(Session session) { + public static SessionInitializer init(Session session) { - if (session == null) { - throw new IllegalArgumentException("empty session"); - } + if (session == null) { + throw new IllegalArgumentException("empty session"); + } - return new SessionInitializer(session); - } + return new SessionInitializer(session); + } - public static void clearDslCache() { - dslCache.clear(); - } + public static void clearDslCache() { + dslCache.clear(); + } - public static E dsl(Class iface) { - return dsl(iface, null); - } + public static E dsl(Class iface) { + return dsl(iface, null); + } - public static E dsl(Class iface, Metadata metadata) { - return dsl(iface, iface.getClassLoader(), Optional.empty(), metadata); - } + public static E dsl(Class iface, Metadata metadata) { + return dsl(iface, iface.getClassLoader(), Optional.empty(), metadata); + } - public static E dsl(Class iface, ClassLoader classLoader, Metadata metadata) { - return dsl(iface, classLoader, Optional.empty(), metadata); - } + public static E dsl(Class iface, ClassLoader classLoader, Metadata metadata) { + return dsl(iface, classLoader, Optional.empty(), metadata); + } - public static E dsl(Class iface, ClassLoader classLoader, Optional parent, - Metadata metadata) { + public static E dsl( + Class iface, + ClassLoader classLoader, + Optional parent, + Metadata metadata) { - Object instance = null; + Object instance = null; - if (!parent.isPresent()) { - instance = dslCache.get(iface); - } + if (!parent.isPresent()) { + instance = dslCache.get(iface); + } - if (instance == null) { + if (instance == null) { - instance = settings.getDslInstantiator().instantiate(iface, classLoader, parent, metadata); + instance = settings.getDslInstantiator().instantiate(iface, classLoader, parent, metadata); - if (!parent.isPresent()) { + if (!parent.isPresent()) { - Object c = dslCache.putIfAbsent(iface, instance); - if (c != null) { - instance = c; - } - } - } + Object c = dslCache.putIfAbsent(iface, instance); + if (c != null) { + instance = c; + } + } + } - return (E) instance; - } + return (E) instance; + } - public static E map(Class iface, Map src) { - return map(iface, src, iface.getClassLoader()); - } + public static E map(Class iface, Map src) { + return map(iface, src, iface.getClassLoader()); + } - public static E map(Class iface, Map src, ClassLoader classLoader) { - return settings.getMapperInstantiator().instantiate(iface, src, classLoader); - } + public static E map(Class iface, Map src, ClassLoader classLoader) { + return settings.getMapperInstantiator().instantiate(iface, src, classLoader); + } - public static HelenusEntity entity(Class iface) { - Metadata metadata = metadataForEntity.get(iface); - if (metadata == null) { - HelenusSession session = session(); - if (session != null) { - metadata = session.getMetadata(); - } - } - return entity(iface, metadata); - } + public static HelenusEntity entity(Class iface) { + Metadata metadata = metadataForEntity.get(iface); + if (metadata == null) { + HelenusSession session = session(); + if (session != null) { + metadata = session.getMetadata(); + } + } + return entity(iface, metadata); + } - public static HelenusEntity entity(Class iface, Metadata metadata) { + public static HelenusEntity entity(Class iface, Metadata metadata) { - Object dsl = dsl(iface, metadata); + Object dsl = dsl(iface, metadata); - DslExportable e = (DslExportable) dsl; + DslExportable e = (DslExportable) dsl; - return e.getHelenusMappingEntity(); - } + return e.getHelenusMappingEntity(); + } - public static HelenusEntity resolve(Object ifaceOrDsl) { - return resolve(ifaceOrDsl, metadataForEntity.get(ifaceOrDsl)); - } + public static HelenusEntity resolve(Object ifaceOrDsl) { + return resolve(ifaceOrDsl, metadataForEntity.get(ifaceOrDsl)); + } - public static HelenusEntity resolve(Object ifaceOrDsl, Metadata metadata) { + public static HelenusEntity resolve(Object ifaceOrDsl, Metadata metadata) { - if (ifaceOrDsl == null) { - throw new HelenusMappingException("ifaceOrDsl is null"); - } + if (ifaceOrDsl == null) { + throw new HelenusMappingException("ifaceOrDsl is null"); + } - if (ifaceOrDsl instanceof DslExportable) { + if (ifaceOrDsl instanceof DslExportable) { - DslExportable e = (DslExportable) ifaceOrDsl; + DslExportable e = (DslExportable) ifaceOrDsl; - return e.getHelenusMappingEntity(); - } + return e.getHelenusMappingEntity(); + } - if (ifaceOrDsl instanceof Class) { + if (ifaceOrDsl instanceof Class) { - Class iface = (Class) ifaceOrDsl; + Class iface = (Class) ifaceOrDsl; - if (!iface.isInterface()) { - throw new HelenusMappingException("class is not an interface " + iface); - } + if (!iface.isInterface()) { + throw new HelenusMappingException("class is not an interface " + iface); + } - if (metadata != null) { - metadataForEntity.putIfAbsent(iface, metadata); - } - return entity(iface, metadata); - } + if (metadata != null) { + metadataForEntity.putIfAbsent(iface, metadata); + } + return entity(iface, metadata); + } - throw new HelenusMappingException("unknown dsl object or mapping interface " + ifaceOrDsl); - } + throw new HelenusMappingException("unknown dsl object or mapping interface " + ifaceOrDsl); + } } diff --git a/src/main/java/net/helenus/core/HelenusSession.java b/src/main/java/net/helenus/core/HelenusSession.java index 91986e4..7661f54 100644 --- a/src/main/java/net/helenus/core/HelenusSession.java +++ b/src/main/java/net/helenus/core/HelenusSession.java @@ -17,6 +17,10 @@ package net.helenus.core; import static net.helenus.core.Query.eq; +import brave.Tracer; +import com.codahale.metrics.MetricRegistry; +import com.datastax.driver.core.*; +import com.google.common.collect.Table; import java.io.Closeable; import java.io.PrintStream; import java.lang.reflect.Constructor; @@ -24,16 +28,9 @@ import java.lang.reflect.InvocationTargetException; import java.util.*; import java.util.concurrent.Executor; import java.util.function.Function; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.codahale.metrics.MetricRegistry; -import com.datastax.driver.core.*; -import com.google.common.collect.Table; - -import brave.Tracer; import net.helenus.core.cache.CacheUtil; import net.helenus.core.cache.Facet; import net.helenus.core.cache.SessionCache; @@ -50,654 +47,771 @@ import net.helenus.support.*; import net.helenus.support.Fun.Tuple1; import net.helenus.support.Fun.Tuple2; import net.helenus.support.Fun.Tuple6; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HelenusSession extends AbstractSessionOperations implements Closeable { + + public static final Object deleted = new Object(); + private static final Logger LOG = LoggerFactory.getLogger(HelenusSession.class); + private static final Pattern classNameRegex = + Pattern.compile("^(?:\\w+\\.)+(?:(\\w+)|(\\w+)\\$.*)$"); + + private final Session session; + private final CodecRegistry registry; + private final ConsistencyLevel defaultConsistencyLevel; + private final boolean defaultQueryIdempotency; + private final MetricRegistry metricRegistry; + private final Tracer zipkinTracer; + private final PrintStream printStream; + private final Class unitOfWorkClass; + private final SessionRepository sessionRepository; + private final Executor executor; + private final boolean dropSchemaOnClose; + private final SessionCache sessionCache; + private final RowColumnValueProvider valueProvider; + private final StatementColumnValuePreparer valuePreparer; + private final Metadata metadata; + private volatile String usingKeyspace; + private volatile boolean showCql; + + HelenusSession( + Session session, + String usingKeyspace, + CodecRegistry registry, + boolean showCql, + PrintStream printStream, + SessionRepositoryBuilder sessionRepositoryBuilder, + Executor executor, + boolean dropSchemaOnClose, + ConsistencyLevel consistencyLevel, + boolean defaultQueryIdempotency, + Class unitOfWorkClass, + SessionCache sessionCache, + MetricRegistry metricRegistry, + Tracer tracer) { + this.session = session; + this.registry = registry == null ? CodecRegistry.DEFAULT_INSTANCE : registry; + this.usingKeyspace = + Objects.requireNonNull( + usingKeyspace, "keyspace needs to be selected before creating session"); + this.showCql = showCql; + this.printStream = printStream; + this.sessionRepository = sessionRepositoryBuilder.build(); + this.executor = executor; + this.dropSchemaOnClose = dropSchemaOnClose; + this.defaultConsistencyLevel = consistencyLevel; + this.defaultQueryIdempotency = defaultQueryIdempotency; + this.unitOfWorkClass = unitOfWorkClass; + this.metricRegistry = metricRegistry; + this.zipkinTracer = tracer; + + if (sessionCache == null) { + this.sessionCache = SessionCache.defaultCache(); + } else { + this.sessionCache = sessionCache; + } + + this.valueProvider = new RowColumnValueProvider(this.sessionRepository); + this.valuePreparer = new StatementColumnValuePreparer(this.sessionRepository); + this.metadata = session.getCluster().getMetadata(); + } + + @Override + public Session currentSession() { + return session; + } + + @Override + public String usingKeyspace() { + return usingKeyspace; + } + + public HelenusSession useKeyspace(String keyspace) { + session.execute(SchemaUtil.use(keyspace, false)); + this.usingKeyspace = keyspace; + return this; + } + + @Override + public boolean isShowCql() { + return showCql; + } + + @Override + public PrintStream getPrintStream() { + return printStream; + } + + public HelenusSession showCql() { + this.showCql = true; + return this; + } + + public HelenusSession showCql(boolean showCql) { + this.showCql = showCql; + return this; + } + + @Override + public Executor getExecutor() { + return executor; + } + + @Override + public SessionRepository getSessionRepository() { + return sessionRepository; + } + + @Override + public ColumnValueProvider getValueProvider() { + return valueProvider; + } + + @Override + public ColumnValuePreparer getValuePreparer() { + return valuePreparer; + } + + @Override + public Tracer getZipkinTracer() { + return zipkinTracer; + } + + @Override + public MetricRegistry getMetricRegistry() { + return metricRegistry; + } + + @Override + public ConsistencyLevel getDefaultConsistencyLevel() { + return defaultConsistencyLevel; + } + + @Override + public boolean getDefaultQueryIdempotency() { + return defaultQueryIdempotency; + } + + @Override + public Object checkCache(String tableName, List facets) { + List facetCombinations = CacheUtil.flattenFacets(facets); + Object result = null; + for (String[] combination : facetCombinations) { + String cacheKey = tableName + "." + Arrays.toString(combination); + result = sessionCache.get(cacheKey); + if (result != null) { + return result; + } + } + return null; + } + + @Override + public void cacheEvict(List facets) { + String tableName = CacheUtil.schemaName(facets); + List facetCombinations = CacheUtil.flattenFacets(facets); + for (String[] combination : facetCombinations) { + String cacheKey = tableName + "." + Arrays.toString(combination); + sessionCache.invalidate(cacheKey); + } + } + + @Override + public void updateCache(Object pojo, List facets) { + Map valueMap = + pojo instanceof MapExportable ? ((MapExportable) pojo).toMap() : null; + List boundFacets = new ArrayList<>(); + for (Facet facet : facets) { + if (facet instanceof UnboundFacet) { + UnboundFacet unboundFacet = (UnboundFacet) facet; + UnboundFacet.Binder binder = unboundFacet.binder(); + for (HelenusProperty prop : unboundFacet.getProperties()) { + Object value; + if (valueMap == null) { + value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop, false); + if (value != null) { + binder.setValueForProperty(prop, value.toString()); + } + } else { + value = valueMap.get(prop.getPropertyName()); + binder.setValueForProperty(prop, value.toString()); + } + } + if (binder.isBound()) { + boundFacets.add(binder.bind()); + } + } else { + boundFacets.add(facet); + } + } + String tableName = CacheUtil.schemaName(facets); + List facetCombinations = CacheUtil.flattenFacets(boundFacets); + replaceCachedFacetValues(pojo, tableName, facetCombinations); + } + + @Override + public void mergeCache(Table>> uowCache) { + List items = + uowCache + .values() + .stream() + .filter(Either::isLeft) + .map(Either::getLeft) + .distinct() + .collect(Collectors.toList()); + for (Object pojo : items) { + HelenusEntity entity = Helenus.resolve(MappingUtil.getMappingInterface(pojo)); + Map valueMap = + pojo instanceof MapExportable ? ((MapExportable) pojo).toMap() : null; + if (entity.isCacheable()) { + List 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); + 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); + } + } + List facetCombinations = CacheUtil.flattenFacets(boundFacets); + String tableName = CacheUtil.schemaName(boundFacets); + replaceCachedFacetValues(pojo, tableName, facetCombinations); + } + } + + List> deletedFacetSets = + uowCache + .values() + .stream() + .filter(Either::isRight) + .map(Either::getRight) + .collect(Collectors.toList()); + for (List facets : deletedFacetSets) { + String tableName = CacheUtil.schemaName(facets); + List combinations = CacheUtil.flattenFacets(facets); + for (String[] combination : combinations) { + String cacheKey = tableName + "." + Arrays.toString(combination); + sessionCache.invalidate(cacheKey); + } + } + } + + private void replaceCachedFacetValues( + Object pojo, String tableName, List facetCombinations) { + for (String[] combination : facetCombinations) { + String cacheKey = tableName + "." + Arrays.toString(combination); + sessionCache.invalidate(cacheKey); + sessionCache.put(cacheKey, pojo); + } + } + + public Metadata getMetadata() { + return metadata; + } + + public UnitOfWork begin() { + return this.begin(null); + } + + private String extractClassNameFromStackFrame(String classNameOnStack) { + String name = null; + Matcher m = classNameRegex.matcher(classNameOnStack); + if (m.find()) { + name = (m.group(1) != null) ? m.group(1) : ((m.group(2) != null) ? m.group(2) : name); + } else { + name = classNameOnStack; + } + return name; + } + + public synchronized UnitOfWork begin(UnitOfWork parent) { + try { + Class clazz = unitOfWorkClass; + Constructor ctor = + clazz.getConstructor(HelenusSession.class, UnitOfWork.class); + UnitOfWork uow = ctor.newInstance(this, parent); + if (LOG.isInfoEnabled() && uow.getPurpose() == null) { + StringBuilder purpose = null; + int frame = 0; + StackTraceElement[] trace = Thread.currentThread().getStackTrace(); + String targetClassName = HelenusSession.class.getSimpleName(); + String stackClassName = null; + do { + frame++; + stackClassName = extractClassNameFromStackFrame(trace[frame].getClassName()); + } while (!stackClassName.equals(targetClassName) && frame < trace.length); + do { + frame++; + stackClassName = extractClassNameFromStackFrame(trace[frame].getClassName()); + } while (stackClassName.equals(targetClassName) && frame < trace.length); + if (frame < trace.length) { + purpose = + new StringBuilder() + .append(trace[frame].getClassName()) + .append(".") + .append(trace[frame].getMethodName()) + .append("(") + .append(trace[frame].getFileName()) + .append(":") + .append(trace[frame].getLineNumber()) + .append(")"); + uow.setPurpose(purpose.toString()); + } + } + if (parent != null) { + parent.addNestedUnitOfWork(uow); + } + return uow.begin(); + } catch (NoSuchMethodException + | InvocationTargetException + | InstantiationException + | IllegalAccessException e) { + throw new HelenusException( + String.format( + "Unable to instantiate %s as a UnitOfWork.", unitOfWorkClass.getSimpleName()), + e); + } + } + + public SelectOperation select(E pojo) { + Objects.requireNonNull( + pojo, "supplied object must be a dsl for a registered entity but cannot be null"); + ColumnValueProvider valueProvider = getValueProvider(); + HelenusEntity entity = Helenus.resolve(pojo); + Class entityClass = entity.getMappingInterface(); + + return new SelectOperation( + this, + entity, + (r) -> { + Map map = new ValueProviderMap(r, valueProvider, entity); + return (E) Helenus.map(entityClass, map); + }); + } + + public SelectOperation select(Class entityClass) { + Objects.requireNonNull(entityClass, "entityClass is empty"); + ColumnValueProvider valueProvider = getValueProvider(); + HelenusEntity entity = Helenus.entity(entityClass); + + return new SelectOperation( + this, + entity, + (r) -> { + Map map = new ValueProviderMap(r, valueProvider, entity); + return (E) Helenus.map(entityClass, map); + }); + } + + public SelectOperation select() { + return new SelectOperation(this); + } + + public SelectOperation selectAll(Class entityClass) { + Objects.requireNonNull(entityClass, "entityClass is empty"); + return new SelectOperation(this, Helenus.entity(entityClass)); + } + + public SelectOperation selectAll(E pojo) { + Objects.requireNonNull( + pojo, "supplied object must be a dsl for a registered entity but cannot be null"); + HelenusEntity entity = Helenus.resolve(pojo); + return new SelectOperation(this, entity); + } + + public SelectOperation selectAll(Class entityClass, Function rowMapper) { + Objects.requireNonNull(entityClass, "entityClass is empty"); + Objects.requireNonNull(rowMapper, "rowMapper is empty"); + return new SelectOperation(this, Helenus.entity(entityClass), rowMapper); + } + + public SelectOperation> select(Getter getter1) { + Objects.requireNonNull(getter1, "field 1 is empty"); + + HelenusPropertyNode p1 = MappingUtil.resolveMappingProperty(getter1); + return new SelectOperation>( + this, new Mappers.Mapper1(getValueProvider(), p1), p1); + } + + public SelectOperation> select(Getter getter1, Getter getter2) { + Objects.requireNonNull(getter1, "field 1 is empty"); + Objects.requireNonNull(getter2, "field 2 is empty"); + + HelenusPropertyNode p1 = MappingUtil.resolveMappingProperty(getter1); + HelenusPropertyNode p2 = MappingUtil.resolveMappingProperty(getter2); + return new SelectOperation>( + this, new Mappers.Mapper2(getValueProvider(), p1, p2), p1, p2); + } + + public SelectOperation> select( + Getter getter1, Getter getter2, Getter getter3) { + Objects.requireNonNull(getter1, "field 1 is empty"); + Objects.requireNonNull(getter2, "field 2 is empty"); + Objects.requireNonNull(getter3, "field 3 is empty"); + + HelenusPropertyNode p1 = MappingUtil.resolveMappingProperty(getter1); + HelenusPropertyNode p2 = MappingUtil.resolveMappingProperty(getter2); + HelenusPropertyNode p3 = MappingUtil.resolveMappingProperty(getter3); + return new SelectOperation>( + this, new Mappers.Mapper3(getValueProvider(), p1, p2, p3), p1, p2, p3); + } + + public SelectOperation> select( + Getter getter1, Getter getter2, Getter getter3, Getter getter4) { + Objects.requireNonNull(getter1, "field 1 is empty"); + Objects.requireNonNull(getter2, "field 2 is empty"); + Objects.requireNonNull(getter3, "field 3 is empty"); + Objects.requireNonNull(getter4, "field 4 is empty"); + + HelenusPropertyNode p1 = MappingUtil.resolveMappingProperty(getter1); + HelenusPropertyNode p2 = MappingUtil.resolveMappingProperty(getter2); + HelenusPropertyNode p3 = MappingUtil.resolveMappingProperty(getter3); + HelenusPropertyNode p4 = MappingUtil.resolveMappingProperty(getter4); + return new SelectOperation>( + this, + new Mappers.Mapper4(getValueProvider(), p1, p2, p3, p4), + p1, + p2, + p3, + p4); + } + + public SelectOperation> select( + Getter getter1, + Getter getter2, + Getter getter3, + Getter getter4, + Getter getter5) { + Objects.requireNonNull(getter1, "field 1 is empty"); + Objects.requireNonNull(getter2, "field 2 is empty"); + Objects.requireNonNull(getter3, "field 3 is empty"); + Objects.requireNonNull(getter4, "field 4 is empty"); + Objects.requireNonNull(getter5, "field 5 is empty"); + + HelenusPropertyNode p1 = MappingUtil.resolveMappingProperty(getter1); + HelenusPropertyNode p2 = MappingUtil.resolveMappingProperty(getter2); + HelenusPropertyNode p3 = MappingUtil.resolveMappingProperty(getter3); + HelenusPropertyNode p4 = MappingUtil.resolveMappingProperty(getter4); + HelenusPropertyNode p5 = MappingUtil.resolveMappingProperty(getter5); + return new SelectOperation>( + this, + new Mappers.Mapper5(getValueProvider(), p1, p2, p3, p4, p5), + p1, + p2, + p3, + p4, + p5); + } + + public SelectOperation> select( + Getter getter1, + Getter getter2, + Getter getter3, + Getter getter4, + Getter getter5, + Getter getter6) { + Objects.requireNonNull(getter1, "field 1 is empty"); + Objects.requireNonNull(getter2, "field 2 is empty"); + Objects.requireNonNull(getter3, "field 3 is empty"); + Objects.requireNonNull(getter4, "field 4 is empty"); + Objects.requireNonNull(getter5, "field 5 is empty"); + Objects.requireNonNull(getter6, "field 6 is empty"); + + HelenusPropertyNode p1 = MappingUtil.resolveMappingProperty(getter1); + HelenusPropertyNode p2 = MappingUtil.resolveMappingProperty(getter2); + HelenusPropertyNode p3 = MappingUtil.resolveMappingProperty(getter3); + HelenusPropertyNode p4 = MappingUtil.resolveMappingProperty(getter4); + HelenusPropertyNode p5 = MappingUtil.resolveMappingProperty(getter5); + HelenusPropertyNode p6 = MappingUtil.resolveMappingProperty(getter6); + return new SelectOperation>( + this, + new Mappers.Mapper6(getValueProvider(), p1, p2, p3, p4, p5, p6), + p1, + p2, + p3, + p4, + p5, + p6); + } + + public + SelectOperation> select( + Getter getter1, + Getter getter2, + Getter getter3, + Getter getter4, + Getter getter5, + Getter getter6, + Getter getter7) { + Objects.requireNonNull(getter1, "field 1 is empty"); + Objects.requireNonNull(getter2, "field 2 is empty"); + Objects.requireNonNull(getter3, "field 3 is empty"); + Objects.requireNonNull(getter4, "field 4 is empty"); + Objects.requireNonNull(getter5, "field 5 is empty"); + Objects.requireNonNull(getter6, "field 6 is empty"); + Objects.requireNonNull(getter7, "field 7 is empty"); + + HelenusPropertyNode p1 = MappingUtil.resolveMappingProperty(getter1); + HelenusPropertyNode p2 = MappingUtil.resolveMappingProperty(getter2); + HelenusPropertyNode p3 = MappingUtil.resolveMappingProperty(getter3); + HelenusPropertyNode p4 = MappingUtil.resolveMappingProperty(getter4); + HelenusPropertyNode p5 = MappingUtil.resolveMappingProperty(getter5); + HelenusPropertyNode p6 = MappingUtil.resolveMappingProperty(getter6); + HelenusPropertyNode p7 = MappingUtil.resolveMappingProperty(getter7); + return new SelectOperation>( + this, + new Mappers.Mapper7( + getValueProvider(), p1, p2, p3, p4, p5, p6, p7), + p1, + p2, + p3, + p4, + p5, + p6, + p7); + } + + public CountOperation count() { + return new CountOperation(this); + } + + public CountOperation count(Object dsl) { + Objects.requireNonNull(dsl, "dsl is empty"); + return new CountOperation(this, Helenus.resolve(dsl)); + } + + public UpdateOperation update() { + return new UpdateOperation(this); + } + + public UpdateOperation update(Object pojo) { + if (pojo instanceof MapExportable == false) { + throw new HelenusMappingException( + "update of objects that don't implement MapExportable is not yet supported"); + } + return new UpdateOperation(this, pojo); + } + + public UpdateOperation update(Drafted drafted) { + if (drafted instanceof AbstractEntityDraft == false) { + throw new HelenusMappingException( + "update of draft objects that don't inherit from AbstractEntityDraft is not yet supported"); + } + AbstractEntityDraft draft = (AbstractEntityDraft) drafted; + UpdateOperation update = new UpdateOperation(this, draft); + Map map = draft.toMap(); + Set mutatedProperties = draft.mutated(); + HelenusEntity entity = Helenus.entity(draft.getEntityClass()); + + // Add all the mutated values contained in the draft. + entity + .getOrderedProperties() + .forEach( + property -> { + switch (property.getColumnType()) { + case PARTITION_KEY: + case CLUSTERING_COLUMN: + break; + default: + String propertyName = property.getPropertyName(); + if (mutatedProperties.contains(propertyName)) { + Object value = map.get(propertyName); + Getter getter = + new Getter() { + @Override + public Object get() { + throw new DslPropertyException( + new HelenusPropertyNode(property, Optional.empty())); + } + }; + update.set(getter, value); + } + } + }); + + // Add the partition and clustering keys if they were in the draft (normally the + // case). + entity + .getOrderedProperties() + .forEach( + property -> { + switch (property.getColumnType()) { + case PARTITION_KEY: + case CLUSTERING_COLUMN: + String propertyName = property.getPropertyName(); + Object value = map.get(propertyName); + Getter getter = + new Getter() { + @Override + public Object get() { + throw new DslPropertyException( + new HelenusPropertyNode(property, Optional.empty())); + } + }; + update.where(getter, eq(value)); + } + }); + + return update; + } + + public UpdateOperation update(Getter getter, V v) { + Objects.requireNonNull(getter, "field is empty"); + Objects.requireNonNull(v, "value is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(getter); + + return new UpdateOperation(this, p, v); + } + + public InsertOperation insert() { + return new InsertOperation(this, true); + } + + public InsertOperation insert(Class resultType) { + return new InsertOperation(this, resultType, true); + } + + public InsertOperation insert(T pojo) { + Objects.requireNonNull( + pojo, + "supplied object must be either an instance of the entity class or a dsl for it, but cannot be null"); + HelenusEntity entity = null; + try { + entity = Helenus.resolve(pojo); + } catch (HelenusMappingException e) { + } + if (entity != null) { + return new InsertOperation(this, entity.getMappingInterface(), true); + } else { + return this.insert(pojo, null); + } + } + + public InsertOperation insert(Drafted draft) { + return insert(draft.build(), draft.mutated()); + } + + private InsertOperation insert(T pojo, Set mutations) { + Objects.requireNonNull(pojo, "pojo is empty"); + + Class iface = MappingUtil.getMappingInterface(pojo); + HelenusEntity entity = Helenus.entity(iface); + + return new InsertOperation(this, entity, pojo, mutations, true); + } + + public InsertOperation upsert() { + return new InsertOperation(this, false); + } + + public InsertOperation upsert(Class resultType) { + return new InsertOperation(this, resultType, false); + } + + public InsertOperation upsert(Drafted draft) { + return this.upsert((T) draft.build(), draft.mutated()); + } + + public InsertOperation upsert(T pojo) { + Objects.requireNonNull( + pojo, + "supplied object must be either an instance of the entity class or a dsl for it, but cannot be null"); + HelenusEntity entity = null; + try { + entity = Helenus.resolve(pojo); + } catch (HelenusMappingException e) { + } + if (entity != null) { + return new InsertOperation(this, entity.getMappingInterface(), false); + } else { + return this.upsert(pojo, null); + } + } + + private InsertOperation upsert(T pojo, Set mutations) { + Objects.requireNonNull(pojo, "pojo is empty"); + + Class iface = MappingUtil.getMappingInterface(pojo); + HelenusEntity entity = Helenus.entity(iface); + + return new InsertOperation(this, entity, pojo, mutations, false); + } + + public DeleteOperation delete() { + return new DeleteOperation(this); + } + + public DeleteOperation delete(Object dsl) { + Objects.requireNonNull(dsl, "dsl is empty"); + return new DeleteOperation(this, Helenus.resolve(dsl)); + } + + public Session getSession() { + return session; + } + + public E dsl(Class iface) { + return Helenus.dsl(iface, getMetadata()); + } + + public void close() { + + if (session.isClosed()) { + return; + } + + if (dropSchemaOnClose) { + dropSchema(); + } + + session.close(); + } + + public CloseFuture closeAsync() { + + if (!session.isClosed() && dropSchemaOnClose) { + dropSchema(); + } + + return session.closeAsync(); + } + + private void dropSchema() { + + sessionRepository.entities().forEach(e -> dropEntity(e)); + } + + private void dropEntity(HelenusEntity entity) { -public final class HelenusSession extends AbstractSessionOperations implements Closeable { - - public static final Object deleted = new Object(); - private static final Logger LOG = LoggerFactory.getLogger(HelenusSession.class); - - private final Session session; - private final CodecRegistry registry; - private final ConsistencyLevel defaultConsistencyLevel; - private final boolean defaultQueryIdempotency; - private final MetricRegistry metricRegistry; - private final Tracer zipkinTracer; - private final PrintStream printStream; - private final Class unitOfWorkClass; - private final SessionRepository sessionRepository; - private final Executor executor; - private final boolean dropSchemaOnClose; - private final SessionCache sessionCache; - private final RowColumnValueProvider valueProvider; - private final StatementColumnValuePreparer valuePreparer; - private final Metadata metadata; - private volatile String usingKeyspace; - private volatile boolean showCql; - - HelenusSession(Session session, String usingKeyspace, CodecRegistry registry, boolean showCql, - PrintStream printStream, SessionRepositoryBuilder sessionRepositoryBuilder, Executor executor, - boolean dropSchemaOnClose, ConsistencyLevel consistencyLevel, boolean defaultQueryIdempotency, - Class unitOfWorkClass, SessionCache sessionCache, MetricRegistry metricRegistry, - Tracer tracer) { - this.session = session; - this.registry = registry == null ? CodecRegistry.DEFAULT_INSTANCE : registry; - this.usingKeyspace = Objects.requireNonNull(usingKeyspace, - "keyspace needs to be selected before creating session"); - this.showCql = showCql; - this.printStream = printStream; - this.sessionRepository = sessionRepositoryBuilder.build(); - this.executor = executor; - this.dropSchemaOnClose = dropSchemaOnClose; - this.defaultConsistencyLevel = consistencyLevel; - this.defaultQueryIdempotency = defaultQueryIdempotency; - this.unitOfWorkClass = unitOfWorkClass; - this.metricRegistry = metricRegistry; - this.zipkinTracer = tracer; - - if (sessionCache == null) { - this.sessionCache = SessionCache.defaultCache(); - } else { - this.sessionCache = sessionCache; - } - - this.valueProvider = new RowColumnValueProvider(this.sessionRepository); - this.valuePreparer = new StatementColumnValuePreparer(this.sessionRepository); - this.metadata = session.getCluster().getMetadata(); - } - - @Override - public Session currentSession() { - return session; - } - - @Override - public String usingKeyspace() { - return usingKeyspace; - } - - public HelenusSession useKeyspace(String keyspace) { - session.execute(SchemaUtil.use(keyspace, false)); - this.usingKeyspace = keyspace; - return this; - } - - @Override - public boolean isShowCql() { - return showCql; - } - - @Override - public PrintStream getPrintStream() { - return printStream; - } - - public HelenusSession showCql() { - this.showCql = true; - return this; - } - - public HelenusSession showCql(boolean showCql) { - this.showCql = showCql; - return this; - } - - @Override - public Executor getExecutor() { - return executor; - } - - @Override - public SessionRepository getSessionRepository() { - return sessionRepository; - } - - @Override - public ColumnValueProvider getValueProvider() { - return valueProvider; - } - - @Override - public ColumnValuePreparer getValuePreparer() { - return valuePreparer; - } - - @Override - public Tracer getZipkinTracer() { - return zipkinTracer; - } - - @Override - public MetricRegistry getMetricRegistry() { - return metricRegistry; - } - - @Override - public ConsistencyLevel getDefaultConsistencyLevel() { - return defaultConsistencyLevel; - } - - @Override - public boolean getDefaultQueryIdempotency() { - return defaultQueryIdempotency; - } - - @Override - public Object checkCache(String tableName, List facets) { - List facetCombinations = CacheUtil.flattenFacets(facets); - Object result = null; - for (String[] combination : facetCombinations) { - String cacheKey = tableName + "." + Arrays.toString(combination); - result = sessionCache.get(cacheKey); - if (result != null) { - return result; - } - } - return null; - } - - @Override - public void cacheEvict(List facets) { - String tableName = CacheUtil.schemaName(facets); - List facetCombinations = CacheUtil.flattenFacets(facets); - for (String[] combination : facetCombinations) { - String cacheKey = tableName + "." + Arrays.toString(combination); - sessionCache.invalidate(cacheKey); - } - } - - @Override - public void updateCache(Object pojo, List facets) { - Map valueMap = pojo instanceof MapExportable ? ((MapExportable) pojo).toMap() : null; - List boundFacets = new ArrayList<>(); - for (Facet facet : facets) { - if (facet instanceof UnboundFacet) { - UnboundFacet unboundFacet = (UnboundFacet) facet; - UnboundFacet.Binder binder = unboundFacet.binder(); - for (HelenusProperty prop : unboundFacet.getProperties()) { - Object value; - if (valueMap == null) { - value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop, false); - if (value != null) { - binder.setValueForProperty(prop, value.toString()); - } - } else { - value = valueMap.get(prop.getPropertyName()); - binder.setValueForProperty(prop, value.toString()); - } - } - if (binder.isBound()) { - boundFacets.add(binder.bind()); - } - } else { - boundFacets.add(facet); - } - } - String tableName = CacheUtil.schemaName(facets); - List facetCombinations = CacheUtil.flattenFacets(boundFacets); - mergeAndUpdateCacheValues(pojo, tableName, facetCombinations); - } - - @Override - public void mergeCache(Table>> uowCache) { - List items = uowCache.values().stream().filter(Either::isLeft).map(Either::getLeft).distinct() - .collect(Collectors.toList()); - for (Object pojo : items) { - HelenusEntity entity = Helenus.resolve(MappingUtil.getMappingInterface(pojo)); - Map valueMap = pojo instanceof MapExportable ? ((MapExportable) pojo).toMap() : null; - if (entity.isCacheable()) { - List 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); - 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 facetCombinations = CacheUtil.flattenFacets(boundFacets); - String tableName = CacheUtil.schemaName(boundFacets); - mergeAndUpdateCacheValues(pojo, tableName, facetCombinations); - } - } - - List> deletedFacetSets = uowCache.values().stream().filter(Either::isRight).map(Either::getRight) - .collect(Collectors.toList()); - for (List facets : deletedFacetSets) { - String tableName = CacheUtil.schemaName(facets); - List combinations = CacheUtil.flattenFacets(facets); - for (String[] combination : combinations) { - String cacheKey = tableName + "." + Arrays.toString(combination); - sessionCache.invalidate(cacheKey); - } - } - } - - private void mergeAndUpdateCacheValues(Object pojo, String tableName, List facetCombinations) { - Object merged = null; - for (String[] combination : facetCombinations) { - String cacheKey = tableName + "." + Arrays.toString(combination); - Object value = sessionCache.get(cacheKey); - if (value == null) { - sessionCache.put(cacheKey, pojo); - } else { - if (merged == null) { - merged = pojo; - } else { - merged = CacheUtil.merge(value, pojo); - } - sessionCache.put(cacheKey, merged); - } - } - } - - public Metadata getMetadata() { - return metadata; - } - - public UnitOfWork begin() { - return this.begin(null); - } - - public synchronized UnitOfWork begin(UnitOfWork parent) { - try { - Class clazz = unitOfWorkClass; - Constructor ctor = clazz.getConstructor(HelenusSession.class, UnitOfWork.class); - UnitOfWork uow = ctor.newInstance(this, parent); - if (LOG.isInfoEnabled() && uow.getPurpose() == null) { - StringBuilder purpose = null; - StackTraceElement[] trace = Thread.currentThread().getStackTrace(); - int frame = 2; - if (trace[2].getMethodName().equals("begin")) { - frame = 3; - } else if (trace[2].getClassName().equals(unitOfWorkClass.getName())) { - frame = 3; - } - purpose = new StringBuilder().append(trace[frame].getClassName()).append(".") - .append(trace[frame].getMethodName()).append("(").append(trace[frame].getFileName()).append(":") - .append(trace[frame].getLineNumber()).append(")"); - uow.setPurpose(purpose.toString()); - } - if (parent != null) { - parent.addNestedUnitOfWork(uow); - } - return uow.begin(); - } catch (NoSuchMethodException | InvocationTargetException | InstantiationException - | IllegalAccessException e) { - throw new HelenusException( - String.format("Unable to instantiate {} as a UnitOfWork.", unitOfWorkClass.getSimpleName()), e); - } - } - - public SelectOperation select(E pojo) { - Objects.requireNonNull(pojo, "supplied object must be a dsl for a registered entity but cannot be null"); - ColumnValueProvider valueProvider = getValueProvider(); - HelenusEntity entity = Helenus.resolve(pojo); - Class entityClass = entity.getMappingInterface(); - - return new SelectOperation(this, entity, (r) -> { - Map map = new ValueProviderMap(r, valueProvider, entity); - return (E) Helenus.map(entityClass, map); - }); - } - - public SelectOperation select(Class entityClass) { - Objects.requireNonNull(entityClass, "entityClass is empty"); - ColumnValueProvider valueProvider = getValueProvider(); - HelenusEntity entity = Helenus.entity(entityClass); - - return new SelectOperation(this, entity, (r) -> { - Map map = new ValueProviderMap(r, valueProvider, entity); - return (E) Helenus.map(entityClass, map); - }); - } - - public SelectOperation select() { - return new SelectOperation(this); - } - - public SelectOperation selectAll(Class entityClass) { - Objects.requireNonNull(entityClass, "entityClass is empty"); - return new SelectOperation(this, Helenus.entity(entityClass)); - } - - public SelectOperation selectAll(E pojo) { - Objects.requireNonNull(pojo, "supplied object must be a dsl for a registered entity but cannot be null"); - HelenusEntity entity = Helenus.resolve(pojo); - return new SelectOperation(this, entity); - } - - public SelectOperation selectAll(Class entityClass, Function rowMapper) { - Objects.requireNonNull(entityClass, "entityClass is empty"); - Objects.requireNonNull(rowMapper, "rowMapper is empty"); - return new SelectOperation(this, Helenus.entity(entityClass), rowMapper); - } - - public SelectOperation> select(Getter getter1) { - Objects.requireNonNull(getter1, "field 1 is empty"); - - HelenusPropertyNode p1 = MappingUtil.resolveMappingProperty(getter1); - return new SelectOperation>(this, new Mappers.Mapper1(getValueProvider(), p1), p1); - } - - public SelectOperation> select(Getter getter1, Getter getter2) { - Objects.requireNonNull(getter1, "field 1 is empty"); - Objects.requireNonNull(getter2, "field 2 is empty"); - - HelenusPropertyNode p1 = MappingUtil.resolveMappingProperty(getter1); - HelenusPropertyNode p2 = MappingUtil.resolveMappingProperty(getter2); - return new SelectOperation>(this, new Mappers.Mapper2(getValueProvider(), p1, p2), - p1, p2); - } - - public SelectOperation> select(Getter getter1, Getter getter2, - Getter getter3) { - Objects.requireNonNull(getter1, "field 1 is empty"); - Objects.requireNonNull(getter2, "field 2 is empty"); - Objects.requireNonNull(getter3, "field 3 is empty"); - - HelenusPropertyNode p1 = MappingUtil.resolveMappingProperty(getter1); - HelenusPropertyNode p2 = MappingUtil.resolveMappingProperty(getter2); - HelenusPropertyNode p3 = MappingUtil.resolveMappingProperty(getter3); - return new SelectOperation>(this, - new Mappers.Mapper3(getValueProvider(), p1, p2, p3), p1, p2, p3); - } - - public SelectOperation> select(Getter getter1, Getter getter2, - Getter getter3, Getter getter4) { - Objects.requireNonNull(getter1, "field 1 is empty"); - Objects.requireNonNull(getter2, "field 2 is empty"); - Objects.requireNonNull(getter3, "field 3 is empty"); - Objects.requireNonNull(getter4, "field 4 is empty"); - - HelenusPropertyNode p1 = MappingUtil.resolveMappingProperty(getter1); - HelenusPropertyNode p2 = MappingUtil.resolveMappingProperty(getter2); - HelenusPropertyNode p3 = MappingUtil.resolveMappingProperty(getter3); - HelenusPropertyNode p4 = MappingUtil.resolveMappingProperty(getter4); - return new SelectOperation>(this, - new Mappers.Mapper4(getValueProvider(), p1, p2, p3, p4), p1, p2, p3, p4); - } - - public SelectOperation> select(Getter getter1, - Getter getter2, Getter getter3, Getter getter4, Getter getter5) { - Objects.requireNonNull(getter1, "field 1 is empty"); - Objects.requireNonNull(getter2, "field 2 is empty"); - Objects.requireNonNull(getter3, "field 3 is empty"); - Objects.requireNonNull(getter4, "field 4 is empty"); - Objects.requireNonNull(getter5, "field 5 is empty"); - - HelenusPropertyNode p1 = MappingUtil.resolveMappingProperty(getter1); - HelenusPropertyNode p2 = MappingUtil.resolveMappingProperty(getter2); - HelenusPropertyNode p3 = MappingUtil.resolveMappingProperty(getter3); - HelenusPropertyNode p4 = MappingUtil.resolveMappingProperty(getter4); - HelenusPropertyNode p5 = MappingUtil.resolveMappingProperty(getter5); - return new SelectOperation>(this, - new Mappers.Mapper5(getValueProvider(), p1, p2, p3, p4, p5), p1, p2, p3, p4, p5); - } - - public SelectOperation> select(Getter getter1, - Getter getter2, Getter getter3, Getter getter4, Getter getter5, Getter getter6) { - Objects.requireNonNull(getter1, "field 1 is empty"); - Objects.requireNonNull(getter2, "field 2 is empty"); - Objects.requireNonNull(getter3, "field 3 is empty"); - Objects.requireNonNull(getter4, "field 4 is empty"); - Objects.requireNonNull(getter5, "field 5 is empty"); - Objects.requireNonNull(getter6, "field 6 is empty"); - - HelenusPropertyNode p1 = MappingUtil.resolveMappingProperty(getter1); - HelenusPropertyNode p2 = MappingUtil.resolveMappingProperty(getter2); - HelenusPropertyNode p3 = MappingUtil.resolveMappingProperty(getter3); - HelenusPropertyNode p4 = MappingUtil.resolveMappingProperty(getter4); - HelenusPropertyNode p5 = MappingUtil.resolveMappingProperty(getter5); - HelenusPropertyNode p6 = MappingUtil.resolveMappingProperty(getter6); - return new SelectOperation>(this, - new Mappers.Mapper6(getValueProvider(), p1, p2, p3, p4, p5, p6), p1, p2, p3, p4, - p5, p6); - } - - public SelectOperation> select( - Getter getter1, Getter getter2, Getter getter3, Getter getter4, Getter getter5, - Getter getter6, Getter getter7) { - Objects.requireNonNull(getter1, "field 1 is empty"); - Objects.requireNonNull(getter2, "field 2 is empty"); - Objects.requireNonNull(getter3, "field 3 is empty"); - Objects.requireNonNull(getter4, "field 4 is empty"); - Objects.requireNonNull(getter5, "field 5 is empty"); - Objects.requireNonNull(getter6, "field 6 is empty"); - Objects.requireNonNull(getter7, "field 7 is empty"); - - HelenusPropertyNode p1 = MappingUtil.resolveMappingProperty(getter1); - HelenusPropertyNode p2 = MappingUtil.resolveMappingProperty(getter2); - HelenusPropertyNode p3 = MappingUtil.resolveMappingProperty(getter3); - HelenusPropertyNode p4 = MappingUtil.resolveMappingProperty(getter4); - HelenusPropertyNode p5 = MappingUtil.resolveMappingProperty(getter5); - HelenusPropertyNode p6 = MappingUtil.resolveMappingProperty(getter6); - HelenusPropertyNode p7 = MappingUtil.resolveMappingProperty(getter7); - return new SelectOperation>(this, - new Mappers.Mapper7(getValueProvider(), p1, p2, p3, p4, p5, p6, p7), p1, p2, - p3, p4, p5, p6, p7); - } - - public CountOperation count() { - return new CountOperation(this); - } - - public CountOperation count(Object dsl) { - Objects.requireNonNull(dsl, "dsl is empty"); - return new CountOperation(this, Helenus.resolve(dsl)); - } - - public UpdateOperation update() { - return new UpdateOperation(this); - } - - public UpdateOperation update(Object pojo) { - if (pojo instanceof MapExportable == false) { - throw new HelenusMappingException( - "update of objects that don't implement MapExportable is not yet supported"); - } - return new UpdateOperation(this, pojo); - } - - public UpdateOperation update(Drafted drafted) { - if (drafted instanceof AbstractEntityDraft == false) { - throw new HelenusMappingException( - "update of draft objects that don't inherit from AbstractEntityDraft is not yet supported"); - } - AbstractEntityDraft draft = (AbstractEntityDraft) drafted; - UpdateOperation update = new UpdateOperation(this, draft); - Map map = draft.toMap(); - Set mutatedProperties = draft.mutated(); - HelenusEntity entity = Helenus.entity(draft.getEntityClass()); - - // Add all the mutated values contained in the draft. - entity.getOrderedProperties().forEach(property -> { - switch (property.getColumnType()) { - case PARTITION_KEY : - case CLUSTERING_COLUMN : - break; - default : - String propertyName = property.getPropertyName(); - if (mutatedProperties.contains(propertyName)) { - Object value = map.get(propertyName); - Getter getter = new Getter() { - @Override - public Object get() { - throw new DslPropertyException(new HelenusPropertyNode(property, Optional.empty())); - } - }; - update.set(getter, value); - } - } - }); - - // Add the partition and clustering keys if they were in the draft (normally the - // case). - entity.getOrderedProperties().forEach(property -> { - switch (property.getColumnType()) { - case PARTITION_KEY : - case CLUSTERING_COLUMN : - String propertyName = property.getPropertyName(); - Object value = map.get(propertyName); - Getter getter = new Getter() { - @Override - public Object get() { - throw new DslPropertyException(new HelenusPropertyNode(property, Optional.empty())); - } - }; - update.where(getter, eq(value)); - } - }); - - return update; - } - - public UpdateOperation update(Getter getter, V v) { - Objects.requireNonNull(getter, "field is empty"); - Objects.requireNonNull(v, "value is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(getter); - - return new UpdateOperation(this, p, v); - } - - public InsertOperation insert() { - return new InsertOperation(this, true); - } - - public InsertOperation insert(Class resultType) { - return new InsertOperation(this, resultType, true); - } - - public InsertOperation insert(T pojo) { - Objects.requireNonNull(pojo, - "supplied object must be either an instance of the entity class or a dsl for it, but cannot be null"); - HelenusEntity entity = null; - try { - entity = Helenus.resolve(pojo); - } catch (HelenusMappingException e) { - } - if (entity != null) { - return new InsertOperation(this, entity.getMappingInterface(), true); - } else { - return this.insert(pojo, null); - } - } - - public InsertOperation insert(Drafted draft) { - return insert(draft.build(), draft.mutated()); - } - - private InsertOperation insert(T pojo, Set mutations) { - Objects.requireNonNull(pojo, "pojo is empty"); - - Class iface = MappingUtil.getMappingInterface(pojo); - HelenusEntity entity = Helenus.entity(iface); - - return new InsertOperation(this, entity, pojo, mutations, true); - } - - public InsertOperation upsert() { - return new InsertOperation(this, false); - } - - public InsertOperation upsert(Class resultType) { - return new InsertOperation(this, resultType, false); - } - - public InsertOperation upsert(Drafted draft) { - return this.upsert((T) draft.build(), draft.mutated()); - } - - public InsertOperation upsert(T pojo) { - Objects.requireNonNull(pojo, - "supplied object must be either an instance of the entity class or a dsl for it, but cannot be null"); - HelenusEntity entity = null; - try { - entity = Helenus.resolve(pojo); - } catch (HelenusMappingException e) { - } - if (entity != null) { - return new InsertOperation(this, entity.getMappingInterface(), false); - } else { - return this.upsert(pojo, null); - } - } - - private InsertOperation upsert(T pojo, Set mutations) { - Objects.requireNonNull(pojo, "pojo is empty"); - - Class iface = MappingUtil.getMappingInterface(pojo); - HelenusEntity entity = Helenus.entity(iface); - - return new InsertOperation(this, entity, pojo, mutations, false); - } - - public DeleteOperation delete() { - return new DeleteOperation(this); - } - - public DeleteOperation delete(Object dsl) { - Objects.requireNonNull(dsl, "dsl is empty"); - return new DeleteOperation(this, Helenus.resolve(dsl)); - } - - public Session getSession() { - return session; - } - - public E dsl(Class iface) { - return Helenus.dsl(iface, getMetadata()); - } - - public void close() { - - if (session.isClosed()) { - return; - } - - if (dropSchemaOnClose) { - dropSchema(); - } - - session.close(); - } - - public CloseFuture closeAsync() { - - if (!session.isClosed() && dropSchemaOnClose) { - dropSchema(); - } - - return session.closeAsync(); - } - - private void dropSchema() { - - sessionRepository.entities().forEach(e -> dropEntity(e)); - } - - private void dropEntity(HelenusEntity entity) { + switch (entity.getType()) { + case TABLE: + execute(SchemaUtil.dropTable(entity), true); + break; - switch (entity.getType()) { - case TABLE : - execute(SchemaUtil.dropTable(entity), true); - break; + case UDT: + execute(SchemaUtil.dropUserType(entity), true); + break; - case UDT : - execute(SchemaUtil.dropUserType(entity), true); - break; - } - } + default: + throw new HelenusException("Unknown entity type."); + } + } } diff --git a/src/main/java/net/helenus/core/HelenusValidator.java b/src/main/java/net/helenus/core/HelenusValidator.java index 1e75544..b0087f4 100644 --- a/src/main/java/net/helenus/core/HelenusValidator.java +++ b/src/main/java/net/helenus/core/HelenusValidator.java @@ -16,33 +16,32 @@ package net.helenus.core; import java.lang.annotation.Annotation; - import javax.validation.ConstraintValidator; - import net.helenus.mapping.HelenusProperty; import net.helenus.support.HelenusException; import net.helenus.support.HelenusMappingException; public enum HelenusValidator implements PropertyValueValidator { - INSTANCE; + INSTANCE; - public void validate(HelenusProperty prop, Object value) { + public void validate(HelenusProperty prop, Object value) { - for (ConstraintValidator validator : prop.getValidators()) { + for (ConstraintValidator validator : prop.getValidators()) { - ConstraintValidator typeless = (ConstraintValidator) validator; + ConstraintValidator typeless = (ConstraintValidator) validator; - boolean valid = false; + boolean valid = false; - try { - valid = typeless.isValid(value, null); - } catch (ClassCastException e) { - throw new HelenusMappingException("validator was used for wrong type '" + value + "' in " + prop, e); - } + try { + valid = typeless.isValid(value, null); + } catch (ClassCastException e) { + throw new HelenusMappingException( + "validator was used for wrong type '" + value + "' in " + prop, e); + } - if (!valid) { - throw new HelenusException("wrong value '" + value + "' for " + prop); - } - } - } + if (!valid) { + throw new HelenusException("wrong value '" + value + "' for " + prop); + } + } + } } diff --git a/src/main/java/net/helenus/core/MapperInstantiator.java b/src/main/java/net/helenus/core/MapperInstantiator.java index c16b4a1..70d4082 100644 --- a/src/main/java/net/helenus/core/MapperInstantiator.java +++ b/src/main/java/net/helenus/core/MapperInstantiator.java @@ -19,5 +19,5 @@ import java.util.Map; public interface MapperInstantiator { - E instantiate(Class iface, Map src, ClassLoader classLoader); + E instantiate(Class iface, Map src, ClassLoader classLoader); } diff --git a/src/main/java/net/helenus/core/Mappers.java b/src/main/java/net/helenus/core/Mappers.java index d774d7f..4dc26b1 100644 --- a/src/main/java/net/helenus/core/Mappers.java +++ b/src/main/java/net/helenus/core/Mappers.java @@ -15,10 +15,8 @@ */ package net.helenus.core; -import java.util.function.Function; - import com.datastax.driver.core.Row; - +import java.util.function.Function; import net.helenus.core.reflect.HelenusPropertyNode; import net.helenus.mapping.HelenusProperty; import net.helenus.mapping.value.ColumnValueProvider; @@ -26,161 +24,203 @@ import net.helenus.support.Fun; public final class Mappers { - private Mappers() { - } + private Mappers() {} - public static final class Mapper1 implements Function> { + public static final class Mapper1 implements Function> { - private final ColumnValueProvider provider; - private final HelenusProperty p1; + private final ColumnValueProvider provider; + private final HelenusProperty p1; - public Mapper1(ColumnValueProvider provider, HelenusPropertyNode p1) { - this.provider = provider; - this.p1 = p1.getProperty(); - } + public Mapper1(ColumnValueProvider provider, HelenusPropertyNode p1) { + this.provider = provider; + this.p1 = p1.getProperty(); + } - @Override - public Fun.Tuple1 apply(Row row) { - return new Fun.Tuple1(provider.getColumnValue(row, 0, p1)); - } - } + @Override + public Fun.Tuple1 apply(Row row) { + return new Fun.Tuple1(provider.getColumnValue(row, 0, p1)); + } + } - public static final class Mapper2 implements Function> { + public static final class Mapper2 implements Function> { - private final ColumnValueProvider provider; - private final HelenusProperty p1; - private final HelenusProperty p2; + private final ColumnValueProvider provider; + private final HelenusProperty p1; + private final HelenusProperty p2; - public Mapper2(ColumnValueProvider provider, HelenusPropertyNode p1, HelenusPropertyNode p2) { - this.provider = provider; - this.p1 = p1.getProperty(); - this.p2 = p2.getProperty(); - } + public Mapper2(ColumnValueProvider provider, HelenusPropertyNode p1, HelenusPropertyNode p2) { + this.provider = provider; + this.p1 = p1.getProperty(); + this.p2 = p2.getProperty(); + } - @Override - public Fun.Tuple2 apply(Row row) { - return new Fun.Tuple2(provider.getColumnValue(row, 0, p1), provider.getColumnValue(row, 1, p2)); - } - } + @Override + public Fun.Tuple2 apply(Row row) { + return new Fun.Tuple2( + provider.getColumnValue(row, 0, p1), provider.getColumnValue(row, 1, p2)); + } + } - public static final class Mapper3 implements Function> { + public static final class Mapper3 implements Function> { - private final ColumnValueProvider provider; - private final HelenusProperty p1; - private final HelenusProperty p2; - private final HelenusProperty p3; + private final ColumnValueProvider provider; + private final HelenusProperty p1; + private final HelenusProperty p2; + private final HelenusProperty p3; - public Mapper3(ColumnValueProvider provider, HelenusPropertyNode p1, HelenusPropertyNode p2, - HelenusPropertyNode p3) { - this.provider = provider; - this.p1 = p1.getProperty(); - this.p2 = p2.getProperty(); - this.p3 = p3.getProperty(); - } + public Mapper3( + ColumnValueProvider provider, + HelenusPropertyNode p1, + HelenusPropertyNode p2, + HelenusPropertyNode p3) { + this.provider = provider; + this.p1 = p1.getProperty(); + this.p2 = p2.getProperty(); + this.p3 = p3.getProperty(); + } - @Override - public Fun.Tuple3 apply(Row row) { - return new Fun.Tuple3(provider.getColumnValue(row, 0, p1), provider.getColumnValue(row, 1, p2), - provider.getColumnValue(row, 2, p3)); - } - } + @Override + public Fun.Tuple3 apply(Row row) { + return new Fun.Tuple3( + provider.getColumnValue(row, 0, p1), + provider.getColumnValue(row, 1, p2), + provider.getColumnValue(row, 2, p3)); + } + } - public static final class Mapper4 implements Function> { + public static final class Mapper4 implements Function> { - private final ColumnValueProvider provider; - private final HelenusProperty p1; - private final HelenusProperty p2; - private final HelenusProperty p3; - private final HelenusProperty p4; + private final ColumnValueProvider provider; + private final HelenusProperty p1; + private final HelenusProperty p2; + private final HelenusProperty p3; + private final HelenusProperty p4; - public Mapper4(ColumnValueProvider provider, HelenusPropertyNode p1, HelenusPropertyNode p2, - HelenusPropertyNode p3, HelenusPropertyNode p4) { - this.provider = provider; - this.p1 = p1.getProperty(); - this.p2 = p2.getProperty(); - this.p3 = p3.getProperty(); - this.p4 = p4.getProperty(); - } + public Mapper4( + ColumnValueProvider provider, + HelenusPropertyNode p1, + HelenusPropertyNode p2, + HelenusPropertyNode p3, + HelenusPropertyNode p4) { + this.provider = provider; + this.p1 = p1.getProperty(); + this.p2 = p2.getProperty(); + this.p3 = p3.getProperty(); + this.p4 = p4.getProperty(); + } - @Override - public Fun.Tuple4 apply(Row row) { - return new Fun.Tuple4(provider.getColumnValue(row, 0, p1), provider.getColumnValue(row, 1, p2), - provider.getColumnValue(row, 2, p3), provider.getColumnValue(row, 3, p4)); - } - } + @Override + public Fun.Tuple4 apply(Row row) { + return new Fun.Tuple4( + provider.getColumnValue(row, 0, p1), + provider.getColumnValue(row, 1, p2), + provider.getColumnValue(row, 2, p3), + provider.getColumnValue(row, 3, p4)); + } + } - public static final class Mapper5 implements Function> { + public static final class Mapper5 + implements Function> { - private final ColumnValueProvider provider; - private final HelenusProperty p1, p2, p3, p4, p5; + private final ColumnValueProvider provider; + private final HelenusProperty p1, p2, p3, p4, p5; - public Mapper5(ColumnValueProvider provider, HelenusPropertyNode p1, HelenusPropertyNode p2, - HelenusPropertyNode p3, HelenusPropertyNode p4, HelenusPropertyNode p5) { - this.provider = provider; - this.p1 = p1.getProperty(); - this.p2 = p2.getProperty(); - this.p3 = p3.getProperty(); - this.p4 = p4.getProperty(); - this.p5 = p5.getProperty(); - } + public Mapper5( + ColumnValueProvider provider, + HelenusPropertyNode p1, + HelenusPropertyNode p2, + HelenusPropertyNode p3, + HelenusPropertyNode p4, + HelenusPropertyNode p5) { + this.provider = provider; + this.p1 = p1.getProperty(); + this.p2 = p2.getProperty(); + this.p3 = p3.getProperty(); + this.p4 = p4.getProperty(); + this.p5 = p5.getProperty(); + } - @Override - public Fun.Tuple5 apply(Row row) { - return new Fun.Tuple5(provider.getColumnValue(row, 0, p1), - provider.getColumnValue(row, 1, p2), provider.getColumnValue(row, 2, p3), - provider.getColumnValue(row, 3, p4), provider.getColumnValue(row, 4, p5)); - } - } + @Override + public Fun.Tuple5 apply(Row row) { + return new Fun.Tuple5( + provider.getColumnValue(row, 0, p1), + provider.getColumnValue(row, 1, p2), + provider.getColumnValue(row, 2, p3), + provider.getColumnValue(row, 3, p4), + provider.getColumnValue(row, 4, p5)); + } + } - public static final class Mapper6 implements Function> { + public static final class Mapper6 + implements Function> { - private final ColumnValueProvider provider; - private final HelenusProperty p1, p2, p3, p4, p5, p6; + private final ColumnValueProvider provider; + private final HelenusProperty p1, p2, p3, p4, p5, p6; - public Mapper6(ColumnValueProvider provider, HelenusPropertyNode p1, HelenusPropertyNode p2, - HelenusPropertyNode p3, HelenusPropertyNode p4, HelenusPropertyNode p5, HelenusPropertyNode p6) { - this.provider = provider; - this.p1 = p1.getProperty(); - this.p2 = p2.getProperty(); - this.p3 = p3.getProperty(); - this.p4 = p4.getProperty(); - this.p5 = p5.getProperty(); - this.p6 = p6.getProperty(); - } + public Mapper6( + ColumnValueProvider provider, + HelenusPropertyNode p1, + HelenusPropertyNode p2, + HelenusPropertyNode p3, + HelenusPropertyNode p4, + HelenusPropertyNode p5, + HelenusPropertyNode p6) { + this.provider = provider; + this.p1 = p1.getProperty(); + this.p2 = p2.getProperty(); + this.p3 = p3.getProperty(); + this.p4 = p4.getProperty(); + this.p5 = p5.getProperty(); + this.p6 = p6.getProperty(); + } - @Override - public Fun.Tuple6 apply(Row row) { - return new Fun.Tuple6(provider.getColumnValue(row, 0, p1), - provider.getColumnValue(row, 1, p2), provider.getColumnValue(row, 2, p3), - provider.getColumnValue(row, 3, p4), provider.getColumnValue(row, 4, p5), - provider.getColumnValue(row, 5, p6)); - } - } + @Override + public Fun.Tuple6 apply(Row row) { + return new Fun.Tuple6( + provider.getColumnValue(row, 0, p1), + provider.getColumnValue(row, 1, p2), + provider.getColumnValue(row, 2, p3), + provider.getColumnValue(row, 3, p4), + provider.getColumnValue(row, 4, p5), + provider.getColumnValue(row, 5, p6)); + } + } - public static final class Mapper7 implements Function> { + public static final class Mapper7 + implements Function> { - private final ColumnValueProvider provider; - private final HelenusProperty p1, p2, p3, p4, p5, p6, p7; + private final ColumnValueProvider provider; + private final HelenusProperty p1, p2, p3, p4, p5, p6, p7; - public Mapper7(ColumnValueProvider provider, HelenusPropertyNode p1, HelenusPropertyNode p2, - HelenusPropertyNode p3, HelenusPropertyNode p4, HelenusPropertyNode p5, HelenusPropertyNode p6, - HelenusPropertyNode p7) { - this.provider = provider; - this.p1 = p1.getProperty(); - this.p2 = p2.getProperty(); - this.p3 = p3.getProperty(); - this.p4 = p4.getProperty(); - this.p5 = p5.getProperty(); - this.p6 = p6.getProperty(); - this.p7 = p7.getProperty(); - } + public Mapper7( + ColumnValueProvider provider, + HelenusPropertyNode p1, + HelenusPropertyNode p2, + HelenusPropertyNode p3, + HelenusPropertyNode p4, + HelenusPropertyNode p5, + HelenusPropertyNode p6, + HelenusPropertyNode p7) { + this.provider = provider; + this.p1 = p1.getProperty(); + this.p2 = p2.getProperty(); + this.p3 = p3.getProperty(); + this.p4 = p4.getProperty(); + this.p5 = p5.getProperty(); + this.p6 = p6.getProperty(); + this.p7 = p7.getProperty(); + } - @Override - public Fun.Tuple7 apply(Row row) { - return new Fun.Tuple7(provider.getColumnValue(row, 0, p1), - provider.getColumnValue(row, 1, p2), provider.getColumnValue(row, 2, p3), - provider.getColumnValue(row, 3, p4), provider.getColumnValue(row, 4, p5), - provider.getColumnValue(row, 5, p6), provider.getColumnValue(row, 6, p7)); - } - } + @Override + public Fun.Tuple7 apply(Row row) { + return new Fun.Tuple7( + provider.getColumnValue(row, 0, p1), + provider.getColumnValue(row, 1, p2), + provider.getColumnValue(row, 2, p3), + provider.getColumnValue(row, 3, p4), + provider.getColumnValue(row, 4, p5), + provider.getColumnValue(row, 5, p6), + provider.getColumnValue(row, 6, p7)); + } + } } diff --git a/src/main/java/net/helenus/core/Operator.java b/src/main/java/net/helenus/core/Operator.java index 3d249ad..29feb47 100644 --- a/src/main/java/net/helenus/core/Operator.java +++ b/src/main/java/net/helenus/core/Operator.java @@ -19,37 +19,37 @@ import java.util.HashMap; import java.util.Map; public enum Operator { - EQ("=="), + EQ("=="), - IN("in"), + IN("in"), - GT(">"), + GT(">"), - LT("<"), + LT("<"), - GTE(">="), + GTE(">="), - LTE("<="); + LTE("<="); - private static final Map indexByName = new HashMap(); + private static final Map indexByName = new HashMap(); - static { - for (Operator fo : Operator.values()) { - indexByName.put(fo.getName(), fo); - } - } + static { + for (Operator fo : Operator.values()) { + indexByName.put(fo.getName(), fo); + } + } - private final String name; + private final String name; - private Operator(String name) { - this.name = name; - } + private Operator(String name) { + this.name = name; + } - public static Operator findByOperator(String name) { - return indexByName.get(name); - } + public static Operator findByOperator(String name) { + return indexByName.get(name); + } - public String getName() { - return name; - } + public String getName() { + return name; + } } diff --git a/src/main/java/net/helenus/core/Ordered.java b/src/main/java/net/helenus/core/Ordered.java index e86b293..ac426aa 100644 --- a/src/main/java/net/helenus/core/Ordered.java +++ b/src/main/java/net/helenus/core/Ordered.java @@ -1,10 +1,8 @@ package net.helenus.core; -import java.util.Objects; - import com.datastax.driver.core.querybuilder.Ordering; import com.datastax.driver.core.querybuilder.QueryBuilder; - +import java.util.Objects; import net.helenus.core.reflect.HelenusPropertyNode; import net.helenus.mapping.ColumnType; import net.helenus.mapping.MappingUtil; @@ -13,34 +11,34 @@ import net.helenus.support.HelenusMappingException; public final class Ordered { - private final Getter getter; - private final OrderingDirection direction; + private final Getter getter; + private final OrderingDirection direction; - public Ordered(Getter getter, OrderingDirection direction) { - this.getter = getter; - this.direction = direction; - } + public Ordered(Getter getter, OrderingDirection direction) { + this.getter = getter; + this.direction = direction; + } - public Ordering getOrdering() { + public Ordering getOrdering() { - Objects.requireNonNull(getter, "property is null"); - Objects.requireNonNull(direction, "direction is null"); + Objects.requireNonNull(getter, "property is null"); + Objects.requireNonNull(direction, "direction is null"); - HelenusPropertyNode propNode = MappingUtil.resolveMappingProperty(getter); + HelenusPropertyNode propNode = MappingUtil.resolveMappingProperty(getter); - if (propNode.getProperty().getColumnType() != ColumnType.CLUSTERING_COLUMN) { - throw new HelenusMappingException( - "property must be a clustering column " + propNode.getProperty().getPropertyName()); - } + if (propNode.getProperty().getColumnType() != ColumnType.CLUSTERING_COLUMN) { + throw new HelenusMappingException( + "property must be a clustering column " + propNode.getProperty().getPropertyName()); + } - switch (direction) { - case ASC : - return QueryBuilder.asc(propNode.getColumnName()); + switch (direction) { + case ASC: + return QueryBuilder.asc(propNode.getColumnName()); - case DESC : - return QueryBuilder.desc(propNode.getColumnName()); - } + case DESC: + return QueryBuilder.desc(propNode.getColumnName()); + } - throw new HelenusMappingException("invalid direction " + direction); - } + throw new HelenusMappingException("invalid direction " + direction); + } } diff --git a/src/main/java/net/helenus/core/PostCommitFunction.java b/src/main/java/net/helenus/core/PostCommitFunction.java index a859608..5521304 100644 --- a/src/main/java/net/helenus/core/PostCommitFunction.java +++ b/src/main/java/net/helenus/core/PostCommitFunction.java @@ -5,25 +5,25 @@ import java.util.Objects; public class PostCommitFunction implements java.util.function.Function { - private final UnitOfWork uow; - private final List postCommit; + private final UnitOfWork uow; + private final List postCommit; - PostCommitFunction(UnitOfWork uow, List postCommit) { - this.uow = uow; - this.postCommit = postCommit; - } + PostCommitFunction(UnitOfWork uow, List postCommit) { + this.uow = uow; + this.postCommit = postCommit; + } - public void andThen(CommitThunk after) { - Objects.requireNonNull(after); - if (postCommit == null) { - after.apply(); - } else { - postCommit.add(after); - } - } + public void andThen(CommitThunk after) { + Objects.requireNonNull(after); + if (postCommit == null) { + after.apply(); + } else { + postCommit.add(after); + } + } - @Override - public R apply(T t) { - return null; - } + @Override + public R apply(T t) { + return null; + } } diff --git a/src/main/java/net/helenus/core/Postulate.java b/src/main/java/net/helenus/core/Postulate.java index 05e9f57..fb7e319 100644 --- a/src/main/java/net/helenus/core/Postulate.java +++ b/src/main/java/net/helenus/core/Postulate.java @@ -17,85 +17,84 @@ package net.helenus.core; import com.datastax.driver.core.querybuilder.Clause; import com.datastax.driver.core.querybuilder.QueryBuilder; - import net.helenus.core.reflect.HelenusPropertyNode; import net.helenus.mapping.value.ColumnValuePreparer; import net.helenus.support.HelenusMappingException; public final class Postulate { - private final Operator operator; - private final V[] values; + private final Operator operator; + private final V[] values; - protected Postulate(Operator op, V[] values) { - this.operator = op; - this.values = values; - } + protected Postulate(Operator op, V[] values) { + this.operator = op; + this.values = values; + } - public static Postulate of(Operator op, V... values) { - return new Postulate(op, values); - } + public static Postulate of(Operator op, V... values) { + return new Postulate(op, values); + } - public Clause getClause(HelenusPropertyNode node, ColumnValuePreparer valuePreparer) { + public Clause getClause(HelenusPropertyNode node, ColumnValuePreparer valuePreparer) { - switch (operator) { - case EQ : - return QueryBuilder.eq(node.getColumnName(), - valuePreparer.prepareColumnValue(values[0], node.getProperty())); + switch (operator) { + case EQ: + return QueryBuilder.eq( + node.getColumnName(), valuePreparer.prepareColumnValue(values[0], node.getProperty())); - case IN : - Object[] preparedValues = new Object[values.length]; - for (int i = 0; i != values.length; ++i) { - preparedValues[i] = valuePreparer.prepareColumnValue(values[i], node.getProperty()); - } - return QueryBuilder.in(node.getColumnName(), preparedValues); + case IN: + Object[] preparedValues = new Object[values.length]; + for (int i = 0; i != values.length; ++i) { + preparedValues[i] = valuePreparer.prepareColumnValue(values[i], node.getProperty()); + } + return QueryBuilder.in(node.getColumnName(), preparedValues); - case LT : - return QueryBuilder.lt(node.getColumnName(), - valuePreparer.prepareColumnValue(values[0], node.getProperty())); + case LT: + return QueryBuilder.lt( + node.getColumnName(), valuePreparer.prepareColumnValue(values[0], node.getProperty())); - case LTE : - return QueryBuilder.lte(node.getColumnName(), - valuePreparer.prepareColumnValue(values[0], node.getProperty())); + case LTE: + return QueryBuilder.lte( + node.getColumnName(), valuePreparer.prepareColumnValue(values[0], node.getProperty())); - case GT : - return QueryBuilder.gt(node.getColumnName(), - valuePreparer.prepareColumnValue(values[0], node.getProperty())); + case GT: + return QueryBuilder.gt( + node.getColumnName(), valuePreparer.prepareColumnValue(values[0], node.getProperty())); - case GTE : - return QueryBuilder.gte(node.getColumnName(), - valuePreparer.prepareColumnValue(values[0], node.getProperty())); + case GTE: + return QueryBuilder.gte( + node.getColumnName(), valuePreparer.prepareColumnValue(values[0], node.getProperty())); - default : - throw new HelenusMappingException("unknown filter operation " + operator); - } - } + default: + throw new HelenusMappingException("unknown filter operation " + operator); + } + } - public V[] values() { - return values; - } + public V[] values() { + return values; + } - @Override - public String toString() { + @Override + public String toString() { - if (operator == Operator.IN) { + if (operator == Operator.IN) { - if (values == null) { - return "in()"; - } + if (values == null) { + return "in()"; + } - int len = values.length; - StringBuilder b = new StringBuilder(); - b.append("in("); - for (int i = 0; i != len; i++) { - if (b.length() > 3) { - b.append(", "); - } - b.append(String.valueOf(values[i])); - } - return b.append(')').toString(); - } + int len = values.length; + StringBuilder b = new StringBuilder(); + b.append("in("); + for (int i = 0; i != len; i++) { + if (b.length() > 3) { + b.append(", "); + } + b.append(String.valueOf(values[i])); + } + return b.append(')').toString(); + } - return operator.getName() + values[0]; - } + return operator.getName() + values[0]; + } } diff --git a/src/main/java/net/helenus/core/PropertyValueValidator.java b/src/main/java/net/helenus/core/PropertyValueValidator.java index ca4aafb..276418b 100644 --- a/src/main/java/net/helenus/core/PropertyValueValidator.java +++ b/src/main/java/net/helenus/core/PropertyValueValidator.java @@ -19,5 +19,5 @@ import net.helenus.mapping.HelenusProperty; public interface PropertyValueValidator { - void validate(HelenusProperty prop, Object value); + void validate(HelenusProperty prop, Object value); } diff --git a/src/main/java/net/helenus/core/Query.java b/src/main/java/net/helenus/core/Query.java index 7418876..b18fccb 100644 --- a/src/main/java/net/helenus/core/Query.java +++ b/src/main/java/net/helenus/core/Query.java @@ -15,83 +15,80 @@ */ package net.helenus.core; +import com.datastax.driver.core.querybuilder.BindMarker; +import com.datastax.driver.core.querybuilder.QueryBuilder; import java.util.List; import java.util.Map; import java.util.Objects; - -import com.datastax.driver.core.querybuilder.BindMarker; -import com.datastax.driver.core.querybuilder.QueryBuilder; - import net.helenus.mapping.OrderingDirection; /** Sugar methods for the queries */ public final class Query { - private Query() { - } + private Query() {} - public static BindMarker marker() { - return QueryBuilder.bindMarker(); - } + public static BindMarker marker() { + return QueryBuilder.bindMarker(); + } - public static BindMarker marker(String name) { - return QueryBuilder.bindMarker(name); - } + public static BindMarker marker(String name) { + return QueryBuilder.bindMarker(name); + } - public static Ordered asc(Getter getter) { - return new Ordered(getter, OrderingDirection.ASC); - } + public static Ordered asc(Getter getter) { + return new Ordered(getter, OrderingDirection.ASC); + } - public static Ordered desc(Getter getter) { - return new Ordered(getter, OrderingDirection.DESC); - } + public static Ordered desc(Getter getter) { + return new Ordered(getter, OrderingDirection.DESC); + } - public static Postulate eq(V val) { - return Postulate.of(Operator.EQ, val); - } + public static Postulate eq(V val) { + return Postulate.of(Operator.EQ, val); + } - public static Postulate lt(V val) { - return Postulate.of(Operator.LT, val); - } + public static Postulate lt(V val) { + return Postulate.of(Operator.LT, val); + } - public static Postulate lte(V val) { - return Postulate.of(Operator.LTE, val); - } + public static Postulate lte(V val) { + return Postulate.of(Operator.LTE, val); + } - public static Postulate gt(V val) { - return Postulate.of(Operator.GT, val); - } + public static Postulate gt(V val) { + return Postulate.of(Operator.GT, val); + } - public static Postulate gte(V val) { - return Postulate.of(Operator.GTE, val); - } + public static Postulate gte(V val) { + return Postulate.of(Operator.GTE, val); + } - public static Postulate in(V[] vals) { - return new Postulate(Operator.IN, vals); - } + public static Postulate in(V[] vals) { + return new Postulate(Operator.IN, vals); + } - public static Getter getIdx(Getter> listGetter, int index) { - Objects.requireNonNull(listGetter, "listGetter is null"); + public static Getter getIdx(Getter> listGetter, int index) { + Objects.requireNonNull(listGetter, "listGetter is null"); - return new Getter() { + return new Getter() { - @Override - public V get() { - return listGetter.get().get(index); - } - }; - } + @Override + public V get() { + return listGetter.get().get(index); + } + }; + } - public static Getter get(Getter> mapGetter, K k) { - Objects.requireNonNull(mapGetter, "mapGetter is null"); - Objects.requireNonNull(k, "key is null"); + public static Getter get(Getter> mapGetter, K k) { + Objects.requireNonNull(mapGetter, "mapGetter is null"); + Objects.requireNonNull(k, "key is null"); - return new Getter() { + return new Getter() { - @Override - public V get() { - return mapGetter.get().get(k); - } - }; - } + @Override + public V get() { + return mapGetter.get().get(k); + } + }; + } } diff --git a/src/main/java/net/helenus/core/SchemaUtil.java b/src/main/java/net/helenus/core/SchemaUtil.java index 44ccae8..721e512 100644 --- a/src/main/java/net/helenus/core/SchemaUtil.java +++ b/src/main/java/net/helenus/core/SchemaUtil.java @@ -15,16 +15,14 @@ */ package net.helenus.core; -import java.util.*; -import java.util.stream.Collectors; - import com.datastax.driver.core.*; import com.datastax.driver.core.querybuilder.IsNotNullClause; import com.datastax.driver.core.querybuilder.QueryBuilder; import com.datastax.driver.core.querybuilder.Select; import com.datastax.driver.core.schemabuilder.*; import com.datastax.driver.core.schemabuilder.Create.Options; - +import java.util.*; +import java.util.stream.Collectors; import net.helenus.core.reflect.HelenusPropertyNode; import net.helenus.mapping.*; import net.helenus.mapping.ColumnType; @@ -35,392 +33,428 @@ import net.helenus.support.HelenusMappingException; public final class SchemaUtil { - private SchemaUtil() { - } + private SchemaUtil() {} - public static RegularStatement use(String keyspace, boolean forceQuote) { - if (forceQuote) { - return new SimpleStatement("USE" + CqlUtil.forceQuote(keyspace)); - } else { - return new SimpleStatement("USE " + keyspace); - } - } + public static RegularStatement use(String keyspace, boolean forceQuote) { + if (forceQuote) { + return new SimpleStatement("USE" + CqlUtil.forceQuote(keyspace)); + } else { + return new SimpleStatement("USE " + keyspace); + } + } - public static SchemaStatement createUserType(HelenusEntity entity) { + public static SchemaStatement createUserType(HelenusEntity entity) { - if (entity.getType() != HelenusEntityType.UDT) { - throw new HelenusMappingException("expected UDT entity " + entity); - } + if (entity.getType() != HelenusEntityType.UDT) { + throw new HelenusMappingException("expected UDT entity " + entity); + } - CreateType create = SchemaBuilder.createType(entity.getName().toCql()); + CreateType create = SchemaBuilder.createType(entity.getName().toCql()); - for (HelenusProperty prop : entity.getOrderedProperties()) { + for (HelenusProperty prop : entity.getOrderedProperties()) { - ColumnType columnType = prop.getColumnType(); + ColumnType columnType = prop.getColumnType(); - if (columnType == ColumnType.PARTITION_KEY || columnType == ColumnType.CLUSTERING_COLUMN) { - throw new HelenusMappingException("primary key columns are not supported in UserDefinedType for " - + prop.getPropertyName() + " in entity " + entity); - } + if (columnType == ColumnType.PARTITION_KEY || columnType == ColumnType.CLUSTERING_COLUMN) { + throw new HelenusMappingException( + "primary key columns are not supported in UserDefinedType for " + + prop.getPropertyName() + + " in entity " + + entity); + } + + try { + prop.getDataType().addColumn(create, prop.getColumnName()); + } catch (IllegalArgumentException e) { + throw new HelenusMappingException( + "invalid column name '" + + prop.getColumnName() + + "' in entity '" + + entity.getName().getName() + + "'", + e); + } + } + + return create; + } + + public static List alterUserType( + UserType userType, HelenusEntity entity, boolean dropUnusedColumns) { + + if (entity.getType() != HelenusEntityType.UDT) { + throw new HelenusMappingException("expected UDT entity " + entity); + } + + List result = new ArrayList(); + + /** + * TODO: In future replace SchemaBuilder.alterTable by SchemaBuilder.alterType when it will + * exist + */ + Alter alter = SchemaBuilder.alterTable(entity.getName().toCql()); + + final Set visitedColumns = + dropUnusedColumns ? new HashSet() : Collections.emptySet(); + + for (HelenusProperty prop : entity.getOrderedProperties()) { + + String columnName = prop.getColumnName().getName(); + + if (dropUnusedColumns) { + visitedColumns.add(columnName); + } + + ColumnType columnType = prop.getColumnType(); + + if (columnType == ColumnType.PARTITION_KEY || columnType == ColumnType.CLUSTERING_COLUMN) { + continue; + } + + DataType dataType = userType.getFieldType(columnName); + SchemaStatement stmt = + prop.getDataType() + .alterColumn(alter, prop.getColumnName(), optional(columnName, dataType)); + + if (stmt != null) { + result.add(stmt); + } + } + + if (dropUnusedColumns) { + for (String field : userType.getFieldNames()) { + if (!visitedColumns.contains(field)) { + + result.add(alter.dropColumn(field)); + } + } + } + + return result; + } + + public static SchemaStatement dropUserType(HelenusEntity entity) { + + if (entity.getType() != HelenusEntityType.UDT) { + throw new HelenusMappingException("expected UDT entity " + entity); + } + + return SchemaBuilder.dropType(entity.getName().toCql()).ifExists(); + } + + public static SchemaStatement dropUserType(UserType type) { + + return SchemaBuilder.dropType(type.getTypeName()).ifExists(); + } + + public static String createPrimaryKeyPhrase(Collection properties) { + List p = new ArrayList(properties.size()); + List c = new ArrayList(properties.size()); + + for (HelenusProperty prop : properties) { + String columnName = prop.getColumnName().toCql(); + switch (prop.getColumnType()) { + case PARTITION_KEY: + p.add(columnName); + break; + case CLUSTERING_COLUMN: + c.add(columnName); + break; + default: + break; + } + } + + return "(" + + ((p.size() > 1) ? "(" + String.join(", ", p) + ")" : p.get(0)) + + ((c.size() > 0) + ? ", " + ((c.size() > 1) ? "(" + String.join(", ", c) + ")" : c.get(0)) + : "") + + ")"; + } + + public static SchemaStatement createMaterializedView( + String keyspace, String viewName, HelenusEntity entity) { + if (entity.getType() != HelenusEntityType.VIEW) { + throw new HelenusMappingException("expected view entity " + entity); + } + + List props = new ArrayList(); + entity + .getOrderedProperties() + .stream() + .map(p -> new HelenusPropertyNode(p, Optional.empty())) + .forEach(p -> props.add(p)); + + Select.Selection selection = QueryBuilder.select(); + + for (HelenusPropertyNode prop : props) { + String columnName = prop.getColumnName(); + selection = selection.column(columnName); + } + Class iface = entity.getMappingInterface(); + String tableName = Helenus.entity(iface.getInterfaces()[0]).getName().toCql(); + Select.Where where = selection.from(tableName).where(); + List o = new ArrayList(props.size()); - try { - prop.getDataType().addColumn(create, prop.getColumnName()); - } catch (IllegalArgumentException e) { - throw new HelenusMappingException("invalid column name '" + prop.getColumnName() + "' in entity '" - + entity.getName().getName() + "'", e); - } - } - - return create; - } - - public static List alterUserType(UserType userType, HelenusEntity entity, - boolean dropUnusedColumns) { - - if (entity.getType() != HelenusEntityType.UDT) { - throw new HelenusMappingException("expected UDT entity " + entity); - } - - List result = new ArrayList(); - - /** - * TODO: In future replace SchemaBuilder.alterTable by SchemaBuilder.alterType - * when it will exist - */ - Alter alter = SchemaBuilder.alterTable(entity.getName().toCql()); - - final Set visitedColumns = dropUnusedColumns ? new HashSet() : Collections.emptySet(); - - for (HelenusProperty prop : entity.getOrderedProperties()) { - - String columnName = prop.getColumnName().getName(); - - if (dropUnusedColumns) { - visitedColumns.add(columnName); - } - - ColumnType columnType = prop.getColumnType(); - - if (columnType == ColumnType.PARTITION_KEY || columnType == ColumnType.CLUSTERING_COLUMN) { - continue; - } - - DataType dataType = userType.getFieldType(columnName); - SchemaStatement stmt = prop.getDataType().alterColumn(alter, prop.getColumnName(), - optional(columnName, dataType)); - - if (stmt != null) { - result.add(stmt); - } - } - - if (dropUnusedColumns) { - for (String field : userType.getFieldNames()) { - if (!visitedColumns.contains(field)) { - - result.add(alter.dropColumn(field)); - } - } - } - - return result; - } - - public static SchemaStatement dropUserType(HelenusEntity entity) { - - if (entity.getType() != HelenusEntityType.UDT) { - throw new HelenusMappingException("expected UDT entity " + entity); - } - - return SchemaBuilder.dropType(entity.getName().toCql()).ifExists(); - } - - public static SchemaStatement dropUserType(UserType type) { - - return SchemaBuilder.dropType(type.getTypeName()).ifExists(); - } - - public static String createPrimaryKeyPhrase(Collection properties) { - List p = new ArrayList(properties.size()); - List c = new ArrayList(properties.size()); - - for (HelenusProperty prop : properties) { - String columnName = prop.getColumnName().toCql(); - switch (prop.getColumnType()) { - case PARTITION_KEY : - p.add(columnName); - break; - case CLUSTERING_COLUMN : - c.add(columnName); - break; - default : - break; - } - } - - return "(" + ((p.size() > 1) ? "(" + String.join(", ", p) + ")" : p.get(0)) - + ((c.size() > 0) ? ", " + ((c.size() > 1) ? "(" + String.join(", ", c) + ")" : c.get(0)) : "") + ")"; - } - - public static SchemaStatement createMaterializedView(String keyspace, String viewName, HelenusEntity entity) { - if (entity.getType() != HelenusEntityType.VIEW) { - throw new HelenusMappingException("expected view entity " + entity); - } - - if (entity == null) { - throw new HelenusMappingException("no entity or table to select data"); - } - - List props = new ArrayList(); - entity.getOrderedProperties().stream().map(p -> new HelenusPropertyNode(p, Optional.empty())) - .forEach(p -> props.add(p)); - - Select.Selection selection = QueryBuilder.select(); - - for (HelenusPropertyNode prop : props) { - String columnName = prop.getColumnName(); - selection = selection.column(columnName); - } - Class iface = entity.getMappingInterface(); - String tableName = Helenus.entity(iface.getInterfaces()[0]).getName().toCql(); - Select.Where where = selection.from(tableName).where(); - List o = new ArrayList(props.size()); - - for (HelenusPropertyNode prop : props) { - String columnName = prop.getColumnName(); - switch (prop.getProperty().getColumnType()) { - case PARTITION_KEY : - where = where.and(new IsNotNullClause(columnName)); - break; - - case CLUSTERING_COLUMN : - where = where.and(new IsNotNullClause(columnName)); - - ClusteringColumn clusteringColumn = prop.getProperty().getGetterMethod() - .getAnnotation(ClusteringColumn.class); - if (clusteringColumn != null && clusteringColumn.ordering() != null) { - o.add(columnName + " " + clusteringColumn.ordering().cql()); - } - break; - default : - break; - } - } - - String primaryKey = "PRIMARY KEY " + createPrimaryKeyPhrase(entity.getOrderedProperties()); - - String clustering = ""; - if (o.size() > 0) { - clustering = "WITH CLUSTERING ORDER BY (" + String.join(", ", o) + ")"; - } - return new CreateMaterializedView(keyspace, viewName, where, primaryKey, clustering).ifNotExists(); - } - - public static SchemaStatement dropMaterializedView(String keyspace, String viewName, HelenusEntity entity) { - return new DropMaterializedView(keyspace, viewName); - } - - public static SchemaStatement createTable(HelenusEntity entity) { - - if (entity.getType() != HelenusEntityType.TABLE) { - throw new HelenusMappingException("expected table entity " + entity); - } - - // NOTE: There is a bug in the normal path of createTable where the - // "cache" is set too early and never unset preventing more than - // one column on a table. - // SchemaBuilder.createTable(entity.getName().toCql()); - CreateTable create = new CreateTable(entity.getName().toCql()); - - create.ifNotExists(); - - List clusteringColumns = new ArrayList(); - - for (HelenusProperty prop : entity.getOrderedProperties()) { - - ColumnType columnType = prop.getColumnType(); - - if (columnType == ColumnType.CLUSTERING_COLUMN) { - clusteringColumns.add(prop); - } - - prop.getDataType().addColumn(create, prop.getColumnName()); - } - - if (!clusteringColumns.isEmpty()) { - Options options = create.withOptions(); - clusteringColumns - .forEach(p -> options.clusteringOrder(p.getColumnName().toCql(), mapDirection(p.getOrdering()))); - } - - return create; - } - - public static List alterTable(TableMetadata tmd, HelenusEntity entity, boolean dropUnusedColumns) { - - if (entity.getType() != HelenusEntityType.TABLE) { - throw new HelenusMappingException("expected table entity " + entity); - } - - List result = new ArrayList(); - - Alter alter = SchemaBuilder.alterTable(entity.getName().toCql()); - - final Set visitedColumns = dropUnusedColumns ? new HashSet() : Collections.emptySet(); - - for (HelenusProperty prop : entity.getOrderedProperties()) { - - String columnName = prop.getColumnName().getName(); - - if (dropUnusedColumns) { - visitedColumns.add(columnName); - } - - ColumnType columnType = prop.getColumnType(); - - if (columnType == ColumnType.PARTITION_KEY || columnType == ColumnType.CLUSTERING_COLUMN) { - continue; - } - - ColumnMetadata columnMetadata = tmd.getColumn(columnName); - SchemaStatement stmt = prop.getDataType().alterColumn(alter, prop.getColumnName(), - optional(columnMetadata)); - - if (stmt != null) { - result.add(stmt); - } - } - - if (dropUnusedColumns) { - for (ColumnMetadata cm : tmd.getColumns()) { - if (!visitedColumns.contains(cm.getName())) { - - result.add(alter.dropColumn(cm.getName())); - } - } - } - - return result; - } - - public static SchemaStatement dropTable(HelenusEntity entity) { - - if (entity.getType() != HelenusEntityType.TABLE) { - throw new HelenusMappingException("expected table entity " + entity); - } - - return SchemaBuilder.dropTable(entity.getName().toCql()).ifExists(); - } - - public static SchemaStatement createIndex(HelenusProperty prop) { - if (prop.caseSensitiveIndex()) { - return SchemaBuilder.createIndex(prop.getIndexName().get().toCql()).ifNotExists() - .onTable(prop.getEntity().getName().toCql()).andColumn(prop.getColumnName().toCql()); - } else { - return new CreateSasiIndex(prop.getIndexName().get().toCql()).ifNotExists() - .onTable(prop.getEntity().getName().toCql()).andColumn(prop.getColumnName().toCql()); - } - } - - public static List createIndexes(HelenusEntity entity) { - - return entity.getOrderedProperties().stream().filter(p -> p.getIndexName().isPresent()) - .map(p -> SchemaUtil.createIndex(p)).collect(Collectors.toList()); - } - - public static List alterIndexes(TableMetadata tmd, HelenusEntity entity, - boolean dropUnusedIndexes) { - - List list = new ArrayList(); - - final Set visitedColumns = dropUnusedIndexes ? new HashSet() : Collections.emptySet(); - - entity.getOrderedProperties().stream().filter(p -> p.getIndexName().isPresent()).forEach(p -> { - String columnName = p.getColumnName().getName(); - - if (dropUnusedIndexes) { - visitedColumns.add(columnName); - } - - ColumnMetadata cm = tmd.getColumn(columnName); - - if (cm != null) { - IndexMetadata im = tmd.getIndex(columnName); - if (im == null) { - list.add(createIndex(p)); - } - } else { - list.add(createIndex(p)); - } - }); - - if (dropUnusedIndexes) { - - tmd.getColumns().stream() - .filter(c -> tmd.getIndex(c.getName()) != null && !visitedColumns.contains(c.getName())) - .forEach(c -> { - list.add(SchemaBuilder.dropIndex(tmd.getIndex(c.getName()).getName()).ifExists()); - }); - } - - return list; - } - - public static SchemaStatement dropIndex(HelenusProperty prop) { - return SchemaBuilder.dropIndex(prop.getIndexName().get().toCql()).ifExists(); - } - - private static SchemaBuilder.Direction mapDirection(OrderingDirection o) { - switch (o) { - case ASC : - return SchemaBuilder.Direction.ASC; - case DESC : - return SchemaBuilder.Direction.DESC; - } - throw new HelenusMappingException("unknown ordering " + o); - } - - public static void throwNoMapping(HelenusProperty prop) { - - throw new HelenusMappingException( - "only primitive types and Set,List,Map collections and UserDefinedTypes are allowed, unknown type for property '" - + prop.getPropertyName() + "' type is '" + prop.getJavaType() + "' in the entity " - + prop.getEntity()); - } - - private static OptionalColumnMetadata optional(final ColumnMetadata columnMetadata) { - if (columnMetadata != null) { - return new OptionalColumnMetadata() { - - @Override - public String getName() { - return columnMetadata.getName(); - } - - @Override - public DataType getType() { - return columnMetadata.getType(); - } - }; - } - return null; - } - - private static OptionalColumnMetadata optional(final String name, final DataType dataType) { - if (dataType != null) { - return new OptionalColumnMetadata() { - - @Override - public String getName() { - return name; - } - - @Override - public DataType getType() { - return dataType; - } - }; - } - return null; - } + for (HelenusPropertyNode prop : props) { + String columnName = prop.getColumnName(); + switch (prop.getProperty().getColumnType()) { + case PARTITION_KEY: + where = where.and(new IsNotNullClause(columnName)); + break; + + case CLUSTERING_COLUMN: + where = where.and(new IsNotNullClause(columnName)); + + ClusteringColumn clusteringColumn = + prop.getProperty().getGetterMethod().getAnnotation(ClusteringColumn.class); + if (clusteringColumn != null && clusteringColumn.ordering() != null) { + o.add(columnName + " " + clusteringColumn.ordering().cql()); + } + break; + default: + break; + } + } + + String primaryKey = "PRIMARY KEY " + createPrimaryKeyPhrase(entity.getOrderedProperties()); + + String clustering = ""; + if (o.size() > 0) { + clustering = "WITH CLUSTERING ORDER BY (" + String.join(", ", o) + ")"; + } + return new CreateMaterializedView(keyspace, viewName, where, primaryKey, clustering) + .ifNotExists(); + } + + public static SchemaStatement dropMaterializedView( + String keyspace, String viewName, HelenusEntity entity) { + return new DropMaterializedView(keyspace, viewName); + } + + public static SchemaStatement createTable(HelenusEntity entity) { + + if (entity.getType() != HelenusEntityType.TABLE) { + throw new HelenusMappingException("expected table entity " + entity); + } + + // NOTE: There is a bug in the normal path of createTable where the + // "cache" is set too early and never unset preventing more than + // one column on a table. + // SchemaBuilder.createTable(entity.getName().toCql()); + CreateTable create = new CreateTable(entity.getName().toCql()); + + create.ifNotExists(); + + List clusteringColumns = new ArrayList(); + + for (HelenusProperty prop : entity.getOrderedProperties()) { + + ColumnType columnType = prop.getColumnType(); + + if (columnType == ColumnType.CLUSTERING_COLUMN) { + clusteringColumns.add(prop); + } + + prop.getDataType().addColumn(create, prop.getColumnName()); + } + + if (!clusteringColumns.isEmpty()) { + Options options = create.withOptions(); + clusteringColumns.forEach( + p -> options.clusteringOrder(p.getColumnName().toCql(), mapDirection(p.getOrdering()))); + } + + return create; + } + + public static List alterTable( + TableMetadata tmd, HelenusEntity entity, boolean dropUnusedColumns) { + + if (entity.getType() != HelenusEntityType.TABLE) { + throw new HelenusMappingException("expected table entity " + entity); + } + + List result = new ArrayList(); + + Alter alter = SchemaBuilder.alterTable(entity.getName().toCql()); + + final Set visitedColumns = + dropUnusedColumns ? new HashSet() : Collections.emptySet(); + + for (HelenusProperty prop : entity.getOrderedProperties()) { + + String columnName = prop.getColumnName().getName(); + + if (dropUnusedColumns) { + visitedColumns.add(columnName); + } + + ColumnType columnType = prop.getColumnType(); + + if (columnType == ColumnType.PARTITION_KEY || columnType == ColumnType.CLUSTERING_COLUMN) { + continue; + } + + ColumnMetadata columnMetadata = tmd.getColumn(columnName); + SchemaStatement stmt = + prop.getDataType().alterColumn(alter, prop.getColumnName(), optional(columnMetadata)); + + if (stmt != null) { + result.add(stmt); + } + } + + if (dropUnusedColumns) { + for (ColumnMetadata cm : tmd.getColumns()) { + if (!visitedColumns.contains(cm.getName())) { + + result.add(alter.dropColumn(cm.getName())); + } + } + } + + return result; + } + + public static SchemaStatement dropTable(HelenusEntity entity) { + + if (entity.getType() != HelenusEntityType.TABLE) { + throw new HelenusMappingException("expected table entity " + entity); + } + + return SchemaBuilder.dropTable(entity.getName().toCql()).ifExists(); + } + + public static SchemaStatement createIndex(HelenusProperty prop) { + if (prop.caseSensitiveIndex()) { + return SchemaBuilder.createIndex(prop.getIndexName().get().toCql()) + .ifNotExists() + .onTable(prop.getEntity().getName().toCql()) + .andColumn(prop.getColumnName().toCql()); + } else { + return new CreateSasiIndex(prop.getIndexName().get().toCql()) + .ifNotExists() + .onTable(prop.getEntity().getName().toCql()) + .andColumn(prop.getColumnName().toCql()); + } + } + + public static List createIndexes(HelenusEntity entity) { + + return entity + .getOrderedProperties() + .stream() + .filter(p -> p.getIndexName().isPresent()) + .map(p -> SchemaUtil.createIndex(p)) + .collect(Collectors.toList()); + } + + public static List alterIndexes( + TableMetadata tmd, HelenusEntity entity, boolean dropUnusedIndexes) { + + List list = new ArrayList(); + + final Set visitedColumns = + dropUnusedIndexes ? new HashSet() : Collections.emptySet(); + + entity + .getOrderedProperties() + .stream() + .filter(p -> p.getIndexName().isPresent()) + .forEach( + p -> { + String columnName = p.getColumnName().getName(); + + if (dropUnusedIndexes) { + visitedColumns.add(columnName); + } + + ColumnMetadata cm = tmd.getColumn(columnName); + + if (cm != null) { + IndexMetadata im = tmd.getIndex(columnName); + if (im == null) { + list.add(createIndex(p)); + } + } else { + list.add(createIndex(p)); + } + }); + + if (dropUnusedIndexes) { + + tmd.getColumns() + .stream() + .filter(c -> tmd.getIndex(c.getName()) != null && !visitedColumns.contains(c.getName())) + .forEach( + c -> { + list.add(SchemaBuilder.dropIndex(tmd.getIndex(c.getName()).getName()).ifExists()); + }); + } + + return list; + } + + public static SchemaStatement dropIndex(HelenusProperty prop) { + return SchemaBuilder.dropIndex(prop.getIndexName().get().toCql()).ifExists(); + } + + private static SchemaBuilder.Direction mapDirection(OrderingDirection o) { + switch (o) { + case ASC: + return SchemaBuilder.Direction.ASC; + case DESC: + return SchemaBuilder.Direction.DESC; + } + throw new HelenusMappingException("unknown ordering " + o); + } + + public static void throwNoMapping(HelenusProperty prop) { + + throw new HelenusMappingException( + "only primitive types and Set,List,Map collections and UserDefinedTypes are allowed, unknown type for property '" + + prop.getPropertyName() + + "' type is '" + + prop.getJavaType() + + "' in the entity " + + prop.getEntity()); + } + + private static OptionalColumnMetadata optional(final ColumnMetadata columnMetadata) { + if (columnMetadata != null) { + return new OptionalColumnMetadata() { + + @Override + public String getName() { + return columnMetadata.getName(); + } + + @Override + public DataType getType() { + return columnMetadata.getType(); + } + }; + } + return null; + } + + private static OptionalColumnMetadata optional(final String name, final DataType dataType) { + if (dataType != null) { + return new OptionalColumnMetadata() { + + @Override + public String getName() { + return name; + } + + @Override + public DataType getType() { + return dataType; + } + }; + } + return null; + } } diff --git a/src/main/java/net/helenus/core/SessionInitializer.java b/src/main/java/net/helenus/core/SessionInitializer.java index 2559276..c77379d 100644 --- a/src/main/java/net/helenus/core/SessionInitializer.java +++ b/src/main/java/net/helenus/core/SessionInitializer.java @@ -15,18 +15,16 @@ */ package net.helenus.core; +import brave.Tracer; +import com.codahale.metrics.MetricRegistry; +import com.datastax.driver.core.*; +import com.google.common.util.concurrent.MoreExecutors; import java.io.IOException; import java.io.PrintStream; import java.util.*; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.function.Consumer; - -import com.codahale.metrics.MetricRegistry; -import com.datastax.driver.core.*; -import com.google.common.util.concurrent.MoreExecutors; - -import brave.Tracer; import net.helenus.core.cache.SessionCache; import net.helenus.core.reflect.DslExportable; import net.helenus.mapping.HelenusEntity; @@ -40,350 +38,405 @@ import net.helenus.support.PackageUtil; public final class SessionInitializer extends AbstractSessionOperations { - private final Session session; - private final List>> initList = new ArrayList>>(); - private CodecRegistry registry; - private String usingKeyspace; - private boolean showCql = false; - private ConsistencyLevel consistencyLevel; - private boolean idempotent = true; - private MetricRegistry metricRegistry = new MetricRegistry(); - private Tracer zipkinTracer; - private PrintStream printStream = System.out; - private Executor executor = MoreExecutors.directExecutor(); - private Class unitOfWorkClass = UnitOfWorkImpl.class; - private SessionRepositoryBuilder sessionRepository; - private boolean dropUnusedColumns = false; - private boolean dropUnusedIndexes = false; - private KeyspaceMetadata keyspaceMetadata; - private AutoDdl autoDdl = AutoDdl.UPDATE; - private SessionCache sessionCache = null; - - SessionInitializer(Session session) { - this.session = Objects.requireNonNull(session, "empty session"); - this.usingKeyspace = session.getLoggedKeyspace(); // can be null - this.sessionRepository = new SessionRepositoryBuilder(session); - } - - @Override - public Session currentSession() { - return session; - } - - @Override - public String usingKeyspace() { - return usingKeyspace; - } - - @Override - public Executor getExecutor() { - return executor; - } - - @Override - public SessionRepository getSessionRepository() { - throw new HelenusException("not expected to call"); - } - - @Override - public ColumnValueProvider getValueProvider() { - throw new HelenusException("not expected to call"); - } - - @Override - public ColumnValuePreparer getValuePreparer() { - throw new HelenusException("not expected to call"); - } - - public SessionInitializer showCql() { - this.showCql = true; - return this; - } - - public SessionInitializer showCql(boolean enabled) { - this.showCql = enabled; - return this; - } - - public SessionInitializer metricRegistry(MetricRegistry metricRegistry) { - this.metricRegistry = metricRegistry; - return this; - } - - public SessionInitializer zipkinTracer(Tracer tracer) { - this.zipkinTracer = tracer; - return this; - } - - public SessionInitializer setUnitOfWorkClass(Class e) { - this.unitOfWorkClass = e; - return this; - } - - public SessionInitializer consistencyLevel(ConsistencyLevel consistencyLevel) { - this.consistencyLevel = consistencyLevel; - return this; - } - - public SessionInitializer setSessionCache(SessionCache sessionCache) { - this.sessionCache = sessionCache; - return this; - } - - public ConsistencyLevel getDefaultConsistencyLevel() { - return consistencyLevel; - } - - public SessionInitializer idempotentQueryExecution(boolean idempotent) { - this.idempotent = idempotent; - return this; - } - - public boolean getDefaultQueryIdempotency() { - return idempotent; - } - - @Override - public PrintStream getPrintStream() { - return printStream; - } - - public SessionInitializer printTo(PrintStream out) { - this.printStream = out; - return this; - } - - public SessionInitializer withExecutor(Executor executor) { - Objects.requireNonNull(executor, "empty executor"); - this.executor = executor; - return this; - } - - public SessionInitializer withCachingExecutor() { - this.executor = Executors.newCachedThreadPool(); - return this; - } - - public SessionInitializer dropUnusedColumns(boolean enabled) { - this.dropUnusedColumns = enabled; - return this; - } - - public SessionInitializer dropUnusedIndexes(boolean enabled) { - this.dropUnusedIndexes = enabled; - return this; - } - - public SessionInitializer withCodecRegistry(CodecRegistry registry) { - this.registry = registry; - return this; - } - - @Override - public boolean isShowCql() { - return showCql; - } - - public SessionInitializer addPackage(String packageName) { - try { - PackageUtil.getClasses(packageName).stream().filter(c -> c.isInterface() && !c.isAnnotation()) - .forEach(clazz -> { - initList.add(Either.right(clazz)); - }); - } catch (IOException | ClassNotFoundException e) { - throw new HelenusException("fail to add package " + packageName, e); - } - return this; - } - - public SessionInitializer add(Object... dsls) { - Objects.requireNonNull(dsls, "dsls is empty"); - int len = dsls.length; - for (int i = 0; i != len; ++i) { - Object obj = Objects.requireNonNull(dsls[i], "element " + i + " is empty"); - initList.add(Either.left(obj)); - } - return this; - } - - public SessionInitializer autoValidate() { - this.autoDdl = AutoDdl.VALIDATE; - return this; - } - - public SessionInitializer autoUpdate() { - this.autoDdl = AutoDdl.UPDATE; - return this; - } - - public SessionInitializer autoCreate() { - this.autoDdl = AutoDdl.CREATE; - return this; - } - - public SessionInitializer autoCreateDrop() { - this.autoDdl = AutoDdl.CREATE_DROP; - return this; - } - - public SessionInitializer auto(AutoDdl autoDdl) { - this.autoDdl = autoDdl; - return this; - } - - public SessionInitializer use(String keyspace) { - session.execute(SchemaUtil.use(keyspace, false)); - this.usingKeyspace = keyspace; - return this; - } - - public SessionInitializer use(String keyspace, boolean forceQuote) { - session.execute(SchemaUtil.use(keyspace, forceQuote)); - this.usingKeyspace = keyspace; - return this; - } - - public void singleton() { - Helenus.setSession(get()); - } - - public synchronized HelenusSession get() { - initialize(); - return new HelenusSession(session, usingKeyspace, registry, showCql, printStream, sessionRepository, executor, - autoDdl == AutoDdl.CREATE_DROP, consistencyLevel, idempotent, unitOfWorkClass, sessionCache, - metricRegistry, zipkinTracer); - } - - private void initialize() { - - Objects.requireNonNull(usingKeyspace, "please define keyspace by 'use' operator"); - - initList.forEach((either) -> { - Class iface = null; - if (either.isLeft()) { - iface = MappingUtil.getMappingInterface(either.getLeft()); - } else { - iface = either.getRight(); - } - - DslExportable dsl = (DslExportable) Helenus.dsl(iface); - dsl.setCassandraMetadataForHelenusSession(session.getCluster().getMetadata()); - sessionRepository.add(dsl); - }); - - TableOperations tableOps = new TableOperations(this, dropUnusedColumns, dropUnusedIndexes); - UserTypeOperations userTypeOps = new UserTypeOperations(this, dropUnusedColumns); - - switch (autoDdl) { - case CREATE_DROP : - - // Drop view first, otherwise a `DROP TABLE ...` will fail as the type is still - // referenced - // by a view. - sessionRepository.entities().stream().filter(e -> e.getType() == HelenusEntityType.VIEW) - .forEach(e -> tableOps.dropView(e)); - - // Drop tables second, before DROP TYPE otherwise a `DROP TYPE ...` will fail as - // the type is - // still referenced by a table. - sessionRepository.entities().stream().filter(e -> e.getType() == HelenusEntityType.TABLE) - .forEach(e -> tableOps.dropTable(e)); - - eachUserTypeInReverseOrder(userTypeOps, e -> userTypeOps.dropUserType(e)); - - // FALLTHRU to CREATE case (read: the absence of a `break;` statement here is - // intentional!) - case CREATE : - eachUserTypeInOrder(userTypeOps, e -> userTypeOps.createUserType(e)); - - sessionRepository.entities().stream().filter(e -> e.getType() == HelenusEntityType.TABLE) - .forEach(e -> tableOps.createTable(e)); - - sessionRepository.entities().stream().filter(e -> e.getType() == HelenusEntityType.VIEW) - .forEach(e -> tableOps.createView(e)); - - break; - - case VALIDATE : - eachUserTypeInOrder(userTypeOps, e -> userTypeOps.validateUserType(getUserType(e), e)); - - sessionRepository.entities().stream().filter(e -> e.getType() == HelenusEntityType.TABLE) - .forEach(e -> tableOps.validateTable(getTableMetadata(e), e)); - - break; - - case UPDATE : - eachUserTypeInOrder(userTypeOps, e -> userTypeOps.updateUserType(getUserType(e), e)); - - sessionRepository.entities().stream().filter(e -> e.getType() == HelenusEntityType.VIEW) - .forEach(e -> tableOps.dropView(e)); - - sessionRepository.entities().stream().filter(e -> e.getType() == HelenusEntityType.TABLE) - .forEach(e -> tableOps.updateTable(getTableMetadata(e), e)); - - sessionRepository.entities().stream().filter(e -> e.getType() == HelenusEntityType.VIEW) - .forEach(e -> tableOps.createView(e)); - break; - } - - KeyspaceMetadata km = getKeyspaceMetadata(); - - for (UserType userType : km.getUserTypes()) { - sessionRepository.addUserType(userType.getTypeName(), userType); - } - } - - private void eachUserTypeInOrder(UserTypeOperations userTypeOps, Consumer action) { - - Set processedSet = new HashSet(); - Set stack = new HashSet(); - - sessionRepository.entities().stream().filter(e -> e.getType() == HelenusEntityType.UDT).forEach(e -> { - stack.clear(); - eachUserTypeInRecursion(e, processedSet, stack, userTypeOps, action); - }); - } - - private void eachUserTypeInReverseOrder(UserTypeOperations userTypeOps, Consumer action) { - ArrayDeque deque = new ArrayDeque<>(); - eachUserTypeInOrder(userTypeOps, e -> deque.addFirst(e)); - deque.stream().forEach(e -> { - action.accept(e); - }); - } - - private void eachUserTypeInRecursion(HelenusEntity e, Set processedSet, Set stack, - UserTypeOperations userTypeOps, Consumer action) { - - stack.add(e); - - Collection createBefore = sessionRepository.getUserTypeUses(e); - - for (HelenusEntity be : createBefore) { - if (!processedSet.contains(be) && !stack.contains(be)) { - eachUserTypeInRecursion(be, processedSet, stack, userTypeOps, action); - processedSet.add(be); - } - } - - if (!processedSet.contains(e)) { - action.accept(e); - processedSet.add(e); - } - } - - private KeyspaceMetadata getKeyspaceMetadata() { - if (keyspaceMetadata == null) { - keyspaceMetadata = session.getCluster().getMetadata().getKeyspace(usingKeyspace.toLowerCase()); - } - return keyspaceMetadata; - } - - private TableMetadata getTableMetadata(HelenusEntity entity) { - return getKeyspaceMetadata().getTable(entity.getName().getName()); - } - - private UserType getUserType(HelenusEntity entity) { - return getKeyspaceMetadata().getUserType(entity.getName().getName()); - } + private final Session session; + private final List>> initList = new ArrayList>>(); + private CodecRegistry registry; + private String usingKeyspace; + private boolean showCql = false; + private ConsistencyLevel consistencyLevel; + private boolean idempotent = true; + private MetricRegistry metricRegistry = new MetricRegistry(); + private Tracer zipkinTracer; + private PrintStream printStream = System.out; + private Executor executor = MoreExecutors.directExecutor(); + private Class unitOfWorkClass = UnitOfWorkImpl.class; + private SessionRepositoryBuilder sessionRepository; + private boolean dropUnusedColumns = false; + private boolean dropUnusedIndexes = false; + private KeyspaceMetadata keyspaceMetadata; + private AutoDdl autoDdl = AutoDdl.UPDATE; + private SessionCache sessionCache = null; + + SessionInitializer(Session session) { + this.session = Objects.requireNonNull(session, "empty session"); + this.usingKeyspace = session.getLoggedKeyspace(); // can be null + this.sessionRepository = new SessionRepositoryBuilder(session); + } + + @Override + public Session currentSession() { + return session; + } + + @Override + public String usingKeyspace() { + return usingKeyspace; + } + + @Override + public Executor getExecutor() { + return executor; + } + + @Override + public SessionRepository getSessionRepository() { + throw new HelenusException("not expected to call"); + } + + @Override + public ColumnValueProvider getValueProvider() { + throw new HelenusException("not expected to call"); + } + + @Override + public ColumnValuePreparer getValuePreparer() { + throw new HelenusException("not expected to call"); + } + + public SessionInitializer showCql() { + this.showCql = true; + return this; + } + + public SessionInitializer showCql(boolean enabled) { + this.showCql = enabled; + return this; + } + + public SessionInitializer metricRegistry(MetricRegistry metricRegistry) { + this.metricRegistry = metricRegistry; + return this; + } + + public SessionInitializer zipkinTracer(Tracer tracer) { + this.zipkinTracer = tracer; + return this; + } + + public SessionInitializer setUnitOfWorkClass(Class e) { + this.unitOfWorkClass = e; + return this; + } + + public SessionInitializer consistencyLevel(ConsistencyLevel consistencyLevel) { + this.consistencyLevel = consistencyLevel; + return this; + } + + public SessionInitializer setSessionCache(SessionCache sessionCache) { + this.sessionCache = sessionCache; + return this; + } + + public ConsistencyLevel getDefaultConsistencyLevel() { + return consistencyLevel; + } + + public SessionInitializer idempotentQueryExecution(boolean idempotent) { + this.idempotent = idempotent; + return this; + } + + public boolean getDefaultQueryIdempotency() { + return idempotent; + } + + @Override + public PrintStream getPrintStream() { + return printStream; + } + + public SessionInitializer printTo(PrintStream out) { + this.printStream = out; + return this; + } + + public SessionInitializer withExecutor(Executor executor) { + Objects.requireNonNull(executor, "empty executor"); + this.executor = executor; + return this; + } + + public SessionInitializer withCachingExecutor() { + this.executor = Executors.newCachedThreadPool(); + return this; + } + + public SessionInitializer dropUnusedColumns(boolean enabled) { + this.dropUnusedColumns = enabled; + return this; + } + + public SessionInitializer dropUnusedIndexes(boolean enabled) { + this.dropUnusedIndexes = enabled; + return this; + } + + public SessionInitializer withCodecRegistry(CodecRegistry registry) { + this.registry = registry; + return this; + } + + @Override + public boolean isShowCql() { + return showCql; + } + + public SessionInitializer addPackage(String packageName) { + try { + PackageUtil.getClasses(packageName) + .stream() + .filter(c -> c.isInterface() && !c.isAnnotation()) + .forEach( + clazz -> { + initList.add(Either.right(clazz)); + }); + } catch (IOException | ClassNotFoundException e) { + throw new HelenusException("fail to add package " + packageName, e); + } + return this; + } + + public SessionInitializer add(Object... dsls) { + Objects.requireNonNull(dsls, "dsls is empty"); + int len = dsls.length; + for (int i = 0; i != len; ++i) { + Object obj = Objects.requireNonNull(dsls[i], "element " + i + " is empty"); + initList.add(Either.left(obj)); + } + return this; + } + + public SessionInitializer autoValidate() { + this.autoDdl = AutoDdl.VALIDATE; + return this; + } + + public SessionInitializer autoUpdate() { + this.autoDdl = AutoDdl.UPDATE; + return this; + } + + public SessionInitializer autoCreate() { + this.autoDdl = AutoDdl.CREATE; + return this; + } + + public SessionInitializer autoCreateDrop() { + this.autoDdl = AutoDdl.CREATE_DROP; + return this; + } + + public SessionInitializer auto(AutoDdl autoDdl) { + this.autoDdl = autoDdl; + return this; + } + + public SessionInitializer use(String keyspace) { + session.execute(SchemaUtil.use(keyspace, false)); + this.usingKeyspace = keyspace; + return this; + } + + public SessionInitializer use(String keyspace, boolean forceQuote) { + session.execute(SchemaUtil.use(keyspace, forceQuote)); + this.usingKeyspace = keyspace; + return this; + } + + public void singleton() { + Helenus.setSession(get()); + } + + public synchronized HelenusSession get() { + initialize(); + return new HelenusSession( + session, + usingKeyspace, + registry, + showCql, + printStream, + sessionRepository, + executor, + autoDdl == AutoDdl.CREATE_DROP, + consistencyLevel, + idempotent, + unitOfWorkClass, + sessionCache, + metricRegistry, + zipkinTracer); + } + + private void initialize() { + + Objects.requireNonNull(usingKeyspace, "please define keyspace by 'use' operator"); + + initList.forEach( + (either) -> { + Class iface = null; + if (either.isLeft()) { + iface = MappingUtil.getMappingInterface(either.getLeft()); + } else { + iface = either.getRight(); + } + + DslExportable dsl = (DslExportable) Helenus.dsl(iface); + dsl.setCassandraMetadataForHelenusSession(session.getCluster().getMetadata()); + sessionRepository.add(dsl); + }); + + TableOperations tableOps = new TableOperations(this, dropUnusedColumns, dropUnusedIndexes); + UserTypeOperations userTypeOps = new UserTypeOperations(this, dropUnusedColumns); + + switch (autoDdl) { + case CREATE_DROP: + + // Drop view first, otherwise a `DROP TABLE ...` will fail as the type is still + // referenced + // by a view. + sessionRepository + .entities() + .stream() + .filter(e -> e.getType() == HelenusEntityType.VIEW) + .forEach(e -> tableOps.dropView(e)); + + // Drop tables second, before DROP TYPE otherwise a `DROP TYPE ...` will fail as + // the type is + // still referenced by a table. + sessionRepository + .entities() + .stream() + .filter(e -> e.getType() == HelenusEntityType.TABLE) + .forEach(e -> tableOps.dropTable(e)); + + eachUserTypeInReverseOrder(userTypeOps, e -> userTypeOps.dropUserType(e)); + + // FALLTHRU to CREATE case (read: the absence of a `break;` statement here is + // intentional!) + case CREATE: + eachUserTypeInOrder(userTypeOps, e -> userTypeOps.createUserType(e)); + + sessionRepository + .entities() + .stream() + .filter(e -> e.getType() == HelenusEntityType.TABLE) + .forEach(e -> tableOps.createTable(e)); + + sessionRepository + .entities() + .stream() + .filter(e -> e.getType() == HelenusEntityType.VIEW) + .forEach(e -> tableOps.createView(e)); + + break; + + case VALIDATE: + eachUserTypeInOrder(userTypeOps, e -> userTypeOps.validateUserType(getUserType(e), e)); + + sessionRepository + .entities() + .stream() + .filter(e -> e.getType() == HelenusEntityType.TABLE) + .forEach(e -> tableOps.validateTable(getTableMetadata(e), e)); + + break; + + case UPDATE: + eachUserTypeInOrder(userTypeOps, e -> userTypeOps.updateUserType(getUserType(e), e)); + + sessionRepository + .entities() + .stream() + .filter(e -> e.getType() == HelenusEntityType.VIEW) + .forEach(e -> tableOps.dropView(e)); + + sessionRepository + .entities() + .stream() + .filter(e -> e.getType() == HelenusEntityType.TABLE) + .forEach(e -> tableOps.updateTable(getTableMetadata(e), e)); + + sessionRepository + .entities() + .stream() + .filter(e -> e.getType() == HelenusEntityType.VIEW) + .forEach(e -> tableOps.createView(e)); + break; + } + + KeyspaceMetadata km = getKeyspaceMetadata(); + + for (UserType userType : km.getUserTypes()) { + sessionRepository.addUserType(userType.getTypeName(), userType); + } + } + + private void eachUserTypeInOrder( + UserTypeOperations userTypeOps, Consumer action) { + + Set processedSet = new HashSet(); + Set stack = new HashSet(); + + sessionRepository + .entities() + .stream() + .filter(e -> e.getType() == HelenusEntityType.UDT) + .forEach( + e -> { + stack.clear(); + eachUserTypeInRecursion(e, processedSet, stack, userTypeOps, action); + }); + } + + private void eachUserTypeInReverseOrder( + UserTypeOperations userTypeOps, Consumer action) { + ArrayDeque deque = new ArrayDeque<>(); + eachUserTypeInOrder(userTypeOps, e -> deque.addFirst(e)); + deque + .stream() + .forEach( + e -> { + action.accept(e); + }); + } + + private void eachUserTypeInRecursion( + HelenusEntity e, + Set processedSet, + Set stack, + UserTypeOperations userTypeOps, + Consumer action) { + + stack.add(e); + + Collection createBefore = sessionRepository.getUserTypeUses(e); + + for (HelenusEntity be : createBefore) { + if (!processedSet.contains(be) && !stack.contains(be)) { + eachUserTypeInRecursion(be, processedSet, stack, userTypeOps, action); + processedSet.add(be); + } + } + + if (!processedSet.contains(e)) { + action.accept(e); + processedSet.add(e); + } + } + + private KeyspaceMetadata getKeyspaceMetadata() { + if (keyspaceMetadata == null) { + keyspaceMetadata = + session.getCluster().getMetadata().getKeyspace(usingKeyspace.toLowerCase()); + } + return keyspaceMetadata; + } + + private TableMetadata getTableMetadata(HelenusEntity entity) { + return getKeyspaceMetadata().getTable(entity.getName().getName()); + } + + private UserType getUserType(HelenusEntity entity) { + return getKeyspaceMetadata().getUserType(entity.getName().getName()); + } } diff --git a/src/main/java/net/helenus/core/SessionRepository.java b/src/main/java/net/helenus/core/SessionRepository.java index faf7774..05d6f64 100644 --- a/src/main/java/net/helenus/core/SessionRepository.java +++ b/src/main/java/net/helenus/core/SessionRepository.java @@ -15,31 +15,30 @@ */ package net.helenus.core; -import java.util.Collection; - import com.datastax.driver.core.UserType; import com.google.common.collect.ImmutableMap; - +import java.util.Collection; import net.helenus.mapping.HelenusEntity; public final class SessionRepository { - private final ImmutableMap userTypeMap; + private final ImmutableMap userTypeMap; - private final ImmutableMap, HelenusEntity> entityMap; + private final ImmutableMap, HelenusEntity> entityMap; - public SessionRepository(SessionRepositoryBuilder builder) { + public SessionRepository(SessionRepositoryBuilder builder) { - userTypeMap = ImmutableMap.builder().putAll(builder.getUserTypeMap()).build(); + userTypeMap = ImmutableMap.builder().putAll(builder.getUserTypeMap()).build(); - entityMap = ImmutableMap., HelenusEntity>builder().putAll(builder.getEntityMap()).build(); - } + entityMap = + ImmutableMap., HelenusEntity>builder().putAll(builder.getEntityMap()).build(); + } - public UserType findUserType(String name) { - return userTypeMap.get(name.toLowerCase()); - } + public UserType findUserType(String name) { + return userTypeMap.get(name.toLowerCase()); + } - public Collection entities() { - return entityMap.values(); - } + public Collection entities() { + return entityMap.values(); + } } diff --git a/src/main/java/net/helenus/core/SessionRepositoryBuilder.java b/src/main/java/net/helenus/core/SessionRepositoryBuilder.java index 8ef0734..413ec48 100644 --- a/src/main/java/net/helenus/core/SessionRepositoryBuilder.java +++ b/src/main/java/net/helenus/core/SessionRepositoryBuilder.java @@ -15,17 +15,15 @@ */ package net.helenus.core; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - import com.datastax.driver.core.Session; import com.datastax.driver.core.UDTValue; import com.datastax.driver.core.UserType; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; - +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; import net.helenus.mapping.HelenusEntity; import net.helenus.mapping.HelenusEntityType; import net.helenus.mapping.HelenusProperty; @@ -35,110 +33,112 @@ import net.helenus.support.HelenusMappingException; public final class SessionRepositoryBuilder { - private static final Optional OPTIONAL_UDT = Optional.of(HelenusEntityType.UDT); + private static final Optional OPTIONAL_UDT = + Optional.of(HelenusEntityType.UDT); - private final Map, HelenusEntity> entityMap = new HashMap, HelenusEntity>(); + private final Map, HelenusEntity> entityMap = new HashMap, HelenusEntity>(); - private final Map userTypeMap = new HashMap(); + private final Map userTypeMap = new HashMap(); - private final Multimap userTypeUsesMap = HashMultimap.create(); + private final Multimap userTypeUsesMap = HashMultimap.create(); - private final Session session; + private final Session session; - SessionRepositoryBuilder(Session session) { - this.session = session; - } + SessionRepositoryBuilder(Session session) { + this.session = session; + } - public SessionRepository build() { - return new SessionRepository(this); - } + public SessionRepository build() { + return new SessionRepository(this); + } - public Collection getUserTypeUses(HelenusEntity udtName) { - return userTypeUsesMap.get(udtName); - } + public Collection getUserTypeUses(HelenusEntity udtName) { + return userTypeUsesMap.get(udtName); + } - public Collection entities() { - return entityMap.values(); - } + public Collection entities() { + return entityMap.values(); + } - protected Map, HelenusEntity> getEntityMap() { - return entityMap; - } + protected Map, HelenusEntity> getEntityMap() { + return entityMap; + } - protected Map getUserTypeMap() { - return userTypeMap; - } + protected Map getUserTypeMap() { + return userTypeMap; + } - public void addUserType(String name, UserType userType) { - userTypeMap.putIfAbsent(name.toLowerCase(), userType); - } + public void addUserType(String name, UserType userType) { + userTypeMap.putIfAbsent(name.toLowerCase(), userType); + } - public HelenusEntity add(Object dsl) { - return add(dsl, Optional.empty()); - } + public HelenusEntity add(Object dsl) { + return add(dsl, Optional.empty()); + } - public void addEntity(HelenusEntity entity) { + public void addEntity(HelenusEntity entity) { - HelenusEntity concurrentEntity = entityMap.putIfAbsent(entity.getMappingInterface(), entity); + HelenusEntity concurrentEntity = entityMap.putIfAbsent(entity.getMappingInterface(), entity); - if (concurrentEntity == null) { - addUserDefinedTypes(entity.getOrderedProperties()); - } - } + if (concurrentEntity == null) { + addUserDefinedTypes(entity.getOrderedProperties()); + } + } - public HelenusEntity add(Object dsl, Optional type) { + public HelenusEntity add(Object dsl, Optional type) { - HelenusEntity helenusEntity = Helenus.resolve(dsl, session.getCluster().getMetadata()); + HelenusEntity helenusEntity = Helenus.resolve(dsl, session.getCluster().getMetadata()); - Class iface = helenusEntity.getMappingInterface(); + Class iface = helenusEntity.getMappingInterface(); - HelenusEntity entity = entityMap.get(iface); + HelenusEntity entity = entityMap.get(iface); - if (entity == null) { + if (entity == null) { - entity = helenusEntity; + entity = helenusEntity; - if (type.isPresent() && entity.getType() != type.get()) { - throw new HelenusMappingException("unexpected entity type " + entity.getType() + " for " + entity); - } + if (type.isPresent() && entity.getType() != type.get()) { + throw new HelenusMappingException( + "unexpected entity type " + entity.getType() + " for " + entity); + } - HelenusEntity concurrentEntity = entityMap.putIfAbsent(iface, entity); + HelenusEntity concurrentEntity = entityMap.putIfAbsent(iface, entity); - if (concurrentEntity == null) { - addUserDefinedTypes(entity.getOrderedProperties()); - } else { - entity = concurrentEntity; - } - } + if (concurrentEntity == null) { + addUserDefinedTypes(entity.getOrderedProperties()); + } else { + entity = concurrentEntity; + } + } - return entity; - } + return entity; + } - private void addUserDefinedTypes(Collection props) { + private void addUserDefinedTypes(Collection props) { - for (HelenusProperty prop : props) { + for (HelenusProperty prop : props) { - AbstractDataType type = prop.getDataType(); + AbstractDataType type = prop.getDataType(); - if (type instanceof DTDataType) { - continue; - } + if (type instanceof DTDataType) { + continue; + } - if (!UDTValue.class.isAssignableFrom(prop.getJavaType())) { + if (!UDTValue.class.isAssignableFrom(prop.getJavaType())) { - for (Class udtClass : type.getTypeArguments()) { + for (Class udtClass : type.getTypeArguments()) { - if (UDTValue.class.isAssignableFrom(udtClass)) { - continue; - } + if (UDTValue.class.isAssignableFrom(udtClass)) { + continue; + } - HelenusEntity addedUserType = add(udtClass, OPTIONAL_UDT); + HelenusEntity addedUserType = add(udtClass, OPTIONAL_UDT); - if (HelenusEntityType.UDT == prop.getEntity().getType()) { - userTypeUsesMap.put(prop.getEntity(), addedUserType); - } - } - } - } - } + if (HelenusEntityType.UDT == prop.getEntity().getType()) { + userTypeUsesMap.put(prop.getEntity(), addedUserType); + } + } + } + } + } } diff --git a/src/main/java/net/helenus/core/TableOperations.java b/src/main/java/net/helenus/core/TableOperations.java index 354851a..50108a6 100644 --- a/src/main/java/net/helenus/core/TableOperations.java +++ b/src/main/java/net/helenus/core/TableOperations.java @@ -15,88 +15,98 @@ */ package net.helenus.core; -import java.util.List; - import com.datastax.driver.core.TableMetadata; import com.datastax.driver.core.schemabuilder.SchemaStatement; - +import java.util.List; import net.helenus.mapping.HelenusEntity; import net.helenus.support.HelenusException; public final class TableOperations { - private final AbstractSessionOperations sessionOps; - private final boolean dropUnusedColumns; - private final boolean dropUnusedIndexes; + private final AbstractSessionOperations sessionOps; + private final boolean dropUnusedColumns; + private final boolean dropUnusedIndexes; - public TableOperations(AbstractSessionOperations sessionOps, boolean dropUnusedColumns, boolean dropUnusedIndexes) { - this.sessionOps = sessionOps; - this.dropUnusedColumns = dropUnusedColumns; - this.dropUnusedIndexes = dropUnusedIndexes; - } + public TableOperations( + AbstractSessionOperations sessionOps, boolean dropUnusedColumns, boolean dropUnusedIndexes) { + this.sessionOps = sessionOps; + this.dropUnusedColumns = dropUnusedColumns; + this.dropUnusedIndexes = dropUnusedIndexes; + } - public void createTable(HelenusEntity entity) { - sessionOps.execute(SchemaUtil.createTable(entity), true); - executeBatch(SchemaUtil.createIndexes(entity)); - } + public void createTable(HelenusEntity entity) { + sessionOps.execute(SchemaUtil.createTable(entity), true); + executeBatch(SchemaUtil.createIndexes(entity)); + } - public void dropTable(HelenusEntity entity) { - sessionOps.execute(SchemaUtil.dropTable(entity), true); - } + public void dropTable(HelenusEntity entity) { + sessionOps.execute(SchemaUtil.dropTable(entity), true); + } - public void validateTable(TableMetadata tmd, HelenusEntity entity) { + public void validateTable(TableMetadata tmd, HelenusEntity entity) { - if (tmd == null) { - throw new HelenusException( - "table does not exists " + entity.getName() + "for entity " + entity.getMappingInterface()); - } + if (tmd == null) { + throw new HelenusException( + "table does not exists " + + entity.getName() + + "for entity " + + entity.getMappingInterface()); + } - List list = SchemaUtil.alterTable(tmd, entity, dropUnusedColumns); + List list = SchemaUtil.alterTable(tmd, entity, dropUnusedColumns); - list.addAll(SchemaUtil.alterIndexes(tmd, entity, dropUnusedIndexes)); + list.addAll(SchemaUtil.alterIndexes(tmd, entity, dropUnusedIndexes)); - if (!list.isEmpty()) { - throw new HelenusException( - "schema changed for entity " + entity.getMappingInterface() + ", apply this command: " + list); - } - } + if (!list.isEmpty()) { + throw new HelenusException( + "schema changed for entity " + + entity.getMappingInterface() + + ", apply this command: " + + list); + } + } - public void updateTable(TableMetadata tmd, HelenusEntity entity) { - if (tmd == null) { - createTable(entity); - return; - } + public void updateTable(TableMetadata tmd, HelenusEntity entity) { + if (tmd == null) { + createTable(entity); + return; + } - executeBatch(SchemaUtil.alterTable(tmd, entity, dropUnusedColumns)); - executeBatch(SchemaUtil.alterIndexes(tmd, entity, dropUnusedIndexes)); - } + executeBatch(SchemaUtil.alterTable(tmd, entity, dropUnusedColumns)); + executeBatch(SchemaUtil.alterIndexes(tmd, entity, dropUnusedIndexes)); + } - public void createView(HelenusEntity entity) { - sessionOps.execute( - SchemaUtil.createMaterializedView(sessionOps.usingKeyspace(), entity.getName().toCql(), entity), true); - // executeBatch(SchemaUtil.createIndexes(entity)); NOTE: Unfortunately C* 3.10 - // does not yet support 2i on materialized views. - } + public void createView(HelenusEntity entity) { + sessionOps.execute( + SchemaUtil.createMaterializedView( + sessionOps.usingKeyspace(), entity.getName().toCql(), entity), + true); + // executeBatch(SchemaUtil.createIndexes(entity)); NOTE: Unfortunately C* 3.10 + // does not yet support 2i on materialized views. + } - public void dropView(HelenusEntity entity) { - sessionOps.execute( - SchemaUtil.dropMaterializedView(sessionOps.usingKeyspace(), entity.getName().toCql(), entity), true); - } + public void dropView(HelenusEntity entity) { + sessionOps.execute( + SchemaUtil.dropMaterializedView( + sessionOps.usingKeyspace(), entity.getName().toCql(), entity), + true); + } - public void updateView(TableMetadata tmd, HelenusEntity entity) { - if (tmd == null) { - createTable(entity); - return; - } + public void updateView(TableMetadata tmd, HelenusEntity entity) { + if (tmd == null) { + createTable(entity); + return; + } - executeBatch(SchemaUtil.alterTable(tmd, entity, dropUnusedColumns)); - executeBatch(SchemaUtil.alterIndexes(tmd, entity, dropUnusedIndexes)); - } + executeBatch(SchemaUtil.alterTable(tmd, entity, dropUnusedColumns)); + executeBatch(SchemaUtil.alterIndexes(tmd, entity, dropUnusedIndexes)); + } - private void executeBatch(List list) { + private void executeBatch(List list) { - list.forEach(s -> { - sessionOps.execute(s, true); - }); - } + list.forEach( + s -> { + sessionOps.execute(s, true); + }); + } } diff --git a/src/main/java/net/helenus/core/UnitOfWork.java b/src/main/java/net/helenus/core/UnitOfWork.java index 5d55853..76bbc43 100644 --- a/src/main/java/net/helenus/core/UnitOfWork.java +++ b/src/main/java/net/helenus/core/UnitOfWork.java @@ -15,59 +15,57 @@ */ package net.helenus.core; +import com.google.common.base.Stopwatch; import java.util.List; import java.util.Optional; - -import com.google.common.base.Stopwatch; - import net.helenus.core.cache.Facet; public interface UnitOfWork extends AutoCloseable { - /** - * Marks the beginning of a transactional section of work. Will write a - * recordCacheAndDatabaseOperationCount to the shared write-ahead log. - * - * @return the handle used to commit or abort the work. - */ - UnitOfWork begin(); + /** + * Marks the beginning of a transactional section of work. Will write a + * recordCacheAndDatabaseOperationCount to the shared write-ahead log. + * + * @return the handle used to commit or abort the work. + */ + UnitOfWork begin(); - void addNestedUnitOfWork(UnitOfWork uow); + void addNestedUnitOfWork(UnitOfWork uow); - /** - * Checks to see if the work performed between calling begin and now can be - * committed or not. - * - * @return a function from which to chain work that only happens when commit is - * successful - * @throws X - * when the work overlaps with other concurrent writers. - */ - PostCommitFunction commit() throws X; + /** + * Checks to see if the work performed between calling begin and now can be committed or not. + * + * @return a function from which to chain work that only happens when commit is successful + * @throws X when the work overlaps with other concurrent writers. + */ + PostCommitFunction commit() throws X; - /** - * Explicitly abort the work within this unit of work. Any nested aborted unit - * of work will trigger the entire unit of work to commit. - */ - void abort(); + /** + * Explicitly abort the work within this unit of work. Any nested aborted unit of work will + * trigger the entire unit of work to commit. + */ + void abort(); - boolean hasAborted(); + boolean hasAborted(); - boolean hasCommitted(); + boolean hasCommitted(); - Optional cacheLookup(List facets); + Optional cacheLookup(List facets); - void cacheUpdate(Object pojo, List facets); + void cacheUpdate(Object pojo, List facets); - List cacheEvict(List facets); + List cacheEvict(List facets); - String getPurpose(); - UnitOfWork setPurpose(String purpose); + String getPurpose(); - void addDatabaseTime(String name, Stopwatch amount); - void addCacheLookupTime(Stopwatch amount); + UnitOfWork setPurpose(String purpose); - // Cache > 0 means "cache hit", < 0 means cache miss. - void recordCacheAndDatabaseOperationCount(int cache, int database); + void setInfo(String info); + void addDatabaseTime(String name, Stopwatch amount); + + void addCacheLookupTime(Stopwatch amount); + + // Cache > 0 means "cache hit", < 0 means cache miss. + void recordCacheAndDatabaseOperationCount(int cache, int database); } diff --git a/src/main/java/net/helenus/core/UnitOfWorkImpl.java b/src/main/java/net/helenus/core/UnitOfWorkImpl.java index ce57ff9..52cae59 100644 --- a/src/main/java/net/helenus/core/UnitOfWorkImpl.java +++ b/src/main/java/net/helenus/core/UnitOfWorkImpl.java @@ -19,8 +19,8 @@ import net.helenus.support.HelenusException; class UnitOfWorkImpl extends AbstractUnitOfWork { - @SuppressWarnings("unchecked") - public UnitOfWorkImpl(HelenusSession session, UnitOfWork parent) { - super(session, (AbstractUnitOfWork) parent); - } + @SuppressWarnings("unchecked") + public UnitOfWorkImpl(HelenusSession session, UnitOfWork parent) { + super(session, (AbstractUnitOfWork) parent); + } } diff --git a/src/main/java/net/helenus/core/UserTypeOperations.java b/src/main/java/net/helenus/core/UserTypeOperations.java index 2b1ed32..2c18339 100644 --- a/src/main/java/net/helenus/core/UserTypeOperations.java +++ b/src/main/java/net/helenus/core/UserTypeOperations.java @@ -15,63 +15,65 @@ */ package net.helenus.core; -import java.util.List; - import com.datastax.driver.core.UserType; import com.datastax.driver.core.schemabuilder.SchemaStatement; - +import java.util.List; import net.helenus.mapping.HelenusEntity; import net.helenus.support.HelenusException; public final class UserTypeOperations { - private final AbstractSessionOperations sessionOps; - private final boolean dropUnusedColumns; + private final AbstractSessionOperations sessionOps; + private final boolean dropUnusedColumns; - public UserTypeOperations(AbstractSessionOperations sessionOps, boolean dropUnusedColumns) { - this.sessionOps = sessionOps; - this.dropUnusedColumns = dropUnusedColumns; - } + public UserTypeOperations(AbstractSessionOperations sessionOps, boolean dropUnusedColumns) { + this.sessionOps = sessionOps; + this.dropUnusedColumns = dropUnusedColumns; + } - public void createUserType(HelenusEntity entity) { + public void createUserType(HelenusEntity entity) { - sessionOps.execute(SchemaUtil.createUserType(entity), true); - } + sessionOps.execute(SchemaUtil.createUserType(entity), true); + } - public void dropUserType(HelenusEntity entity) { + public void dropUserType(HelenusEntity entity) { - sessionOps.execute(SchemaUtil.dropUserType(entity), true); - } + sessionOps.execute(SchemaUtil.dropUserType(entity), true); + } - public void validateUserType(UserType userType, HelenusEntity entity) { + public void validateUserType(UserType userType, HelenusEntity entity) { - if (userType == null) { - throw new HelenusException( - "userType not exists " + entity.getName() + "for entity " + entity.getMappingInterface()); - } + if (userType == null) { + throw new HelenusException( + "userType not exists " + entity.getName() + "for entity " + entity.getMappingInterface()); + } - List list = SchemaUtil.alterUserType(userType, entity, dropUnusedColumns); + List list = SchemaUtil.alterUserType(userType, entity, dropUnusedColumns); - if (!list.isEmpty()) { - throw new HelenusException( - "schema changed for entity " + entity.getMappingInterface() + ", apply this command: " + list); - } - } + if (!list.isEmpty()) { + throw new HelenusException( + "schema changed for entity " + + entity.getMappingInterface() + + ", apply this command: " + + list); + } + } - public void updateUserType(UserType userType, HelenusEntity entity) { + public void updateUserType(UserType userType, HelenusEntity entity) { - if (userType == null) { - createUserType(entity); - return; - } + if (userType == null) { + createUserType(entity); + return; + } - executeBatch(SchemaUtil.alterUserType(userType, entity, dropUnusedColumns)); - } + executeBatch(SchemaUtil.alterUserType(userType, entity, dropUnusedColumns)); + } - private void executeBatch(List list) { + private void executeBatch(List list) { - list.forEach(s -> { - sessionOps.execute(s, true); - }); - } + list.forEach( + s -> { + sessionOps.execute(s, true); + }); + } } diff --git a/src/main/java/net/helenus/core/annotation/Cacheable.java b/src/main/java/net/helenus/core/annotation/Cacheable.java index 8202bdd..935a214 100644 --- a/src/main/java/net/helenus/core/annotation/Cacheable.java +++ b/src/main/java/net/helenus/core/annotation/Cacheable.java @@ -22,5 +22,4 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) -public @interface Cacheable { -} +public @interface Cacheable {} diff --git a/src/main/java/net/helenus/core/annotation/Retry.java b/src/main/java/net/helenus/core/annotation/Retry.java index 6ca6308..b3b2c25 100644 --- a/src/main/java/net/helenus/core/annotation/Retry.java +++ b/src/main/java/net/helenus/core/annotation/Retry.java @@ -21,14 +21,15 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.concurrent.TimeoutException; - import net.helenus.core.ConflictingUnitOfWorkException; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Retry { - Class[] on() default {ConflictingUnitOfWorkException.class, TimeoutException.class}; + Class[] on() default { + ConflictingUnitOfWorkException.class, TimeoutException.class + }; - int times() default 3; + int times() default 3; } diff --git a/src/main/java/net/helenus/core/aspect/RetryAspect.java b/src/main/java/net/helenus/core/aspect/RetryAspect.java index 58d1f1d..9237e4b 100644 --- a/src/main/java/net/helenus/core/aspect/RetryAspect.java +++ b/src/main/java/net/helenus/core/aspect/RetryAspect.java @@ -18,7 +18,7 @@ package net.helenus.core.aspect; import java.lang.reflect.Method; import java.util.Arrays; - +import net.helenus.core.annotation.Retry; import org.apache.commons.lang3.exception.ExceptionUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; @@ -29,71 +29,69 @@ import org.slf4j.LoggerFactory; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.util.Assert; -import net.helenus.core.annotation.Retry; - @Aspect public class RetryAspect { - private static final Logger log = LoggerFactory.getLogger(RetryAspect.class); + private static final Logger log = LoggerFactory.getLogger(RetryAspect.class); - @Around("@annotation(net.helenus.core.annotations.Retry)") - public Object retry(ProceedingJoinPoint pjp) throws Throwable { - Retry retryAnnotation = getRetryAnnotation(pjp); - return (retryAnnotation != null) ? proceed(pjp, retryAnnotation) : proceed(pjp); - } + @Around("@annotation(net.helenus.core.annotations.Retry)") + public Object retry(ProceedingJoinPoint pjp) throws Throwable { + Retry retryAnnotation = getRetryAnnotation(pjp); + return (retryAnnotation != null) ? proceed(pjp, retryAnnotation) : proceed(pjp); + } - private Object proceed(ProceedingJoinPoint pjp) throws Throwable { - return pjp.proceed(); - } + private Object proceed(ProceedingJoinPoint pjp) throws Throwable { + return pjp.proceed(); + } - private Object proceed(ProceedingJoinPoint pjp, Retry retryAnnotation) throws Throwable { - int times = retryAnnotation.times(); - Class[] retryOn = retryAnnotation.on(); - Assert.isTrue(times > 0, "@Retry{times} should be greater than 0!"); - Assert.isTrue(retryOn.length > 0, "@Retry{on} should have at least one Throwable!"); - log.info("Proceed with {} retries on {}", times, Arrays.toString(retryOn)); - return tryProceeding(pjp, times, retryOn); - } + private Object proceed(ProceedingJoinPoint pjp, Retry retryAnnotation) throws Throwable { + int times = retryAnnotation.times(); + Class[] retryOn = retryAnnotation.on(); + Assert.isTrue(times > 0, "@Retry{times} should be greater than 0!"); + Assert.isTrue(retryOn.length > 0, "@Retry{on} should have at least one Throwable!"); + log.info("Proceed with {} retries on {}", times, Arrays.toString(retryOn)); + return tryProceeding(pjp, times, retryOn); + } - private Object tryProceeding(ProceedingJoinPoint pjp, int times, Class[] retryOn) - throws Throwable { - try { - return proceed(pjp); - } catch (Throwable throwable) { - if (isRetryThrowable(throwable, retryOn) && times-- > 0) { - log.info("Conflict detected, {} remaining retries on {}", times, Arrays.toString(retryOn)); - return tryProceeding(pjp, times, retryOn); - } - throw throwable; - } - } + private Object tryProceeding( + ProceedingJoinPoint pjp, int times, Class[] retryOn) throws Throwable { + try { + return proceed(pjp); + } catch (Throwable throwable) { + if (isRetryThrowable(throwable, retryOn) && times-- > 0) { + log.info("Conflict detected, {} remaining retries on {}", times, Arrays.toString(retryOn)); + return tryProceeding(pjp, times, retryOn); + } + throw throwable; + } + } - private boolean isRetryThrowable(Throwable throwable, Class[] retryOn) { - Throwable[] causes = ExceptionUtils.getThrowables(throwable); - for (Throwable cause : causes) { - for (Class retryThrowable : retryOn) { - if (retryThrowable.isAssignableFrom(cause.getClass())) { - return true; - } - } - } - return false; - } + private boolean isRetryThrowable(Throwable throwable, Class[] retryOn) { + Throwable[] causes = ExceptionUtils.getThrowables(throwable); + for (Throwable cause : causes) { + for (Class retryThrowable : retryOn) { + if (retryThrowable.isAssignableFrom(cause.getClass())) { + return true; + } + } + } + return false; + } - private Retry getRetryAnnotation(ProceedingJoinPoint pjp) throws NoSuchMethodException { - MethodSignature signature = (MethodSignature) pjp.getSignature(); - Method method = signature.getMethod(); - Retry retryAnnotation = AnnotationUtils.findAnnotation(method, Retry.class); + private Retry getRetryAnnotation(ProceedingJoinPoint pjp) throws NoSuchMethodException { + MethodSignature signature = (MethodSignature) pjp.getSignature(); + Method method = signature.getMethod(); + Retry retryAnnotation = AnnotationUtils.findAnnotation(method, Retry.class); - if (retryAnnotation != null) { - return retryAnnotation; - } + if (retryAnnotation != null) { + return retryAnnotation; + } - Class[] argClasses = new Class[pjp.getArgs().length]; - for (int i = 0; i < pjp.getArgs().length; i++) { - argClasses[i] = pjp.getArgs()[i].getClass(); - } - method = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(), argClasses); - return AnnotationUtils.findAnnotation(method, Retry.class); - } + Class[] argClasses = new Class[pjp.getArgs().length]; + for (int i = 0; i < pjp.getArgs().length; i++) { + argClasses[i] = pjp.getArgs()[i].getClass(); + } + method = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(), argClasses); + return AnnotationUtils.findAnnotation(method, Retry.class); + } } diff --git a/src/main/java/net/helenus/core/aspect/RetryConcurrentUnitOfWorkAspect.java b/src/main/java/net/helenus/core/aspect/RetryConcurrentUnitOfWorkAspect.java index d2ad9a4..7d52f16 100644 --- a/src/main/java/net/helenus/core/aspect/RetryConcurrentUnitOfWorkAspect.java +++ b/src/main/java/net/helenus/core/aspect/RetryConcurrentUnitOfWorkAspect.java @@ -2,7 +2,7 @@ package net.helenus.core.aspect; import java.lang.reflect.Method; import java.util.Arrays; - +import net.helenus.core.annotation.Retry; import org.apache.commons.lang3.exception.ExceptionUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; @@ -13,71 +13,69 @@ import org.slf4j.LoggerFactory; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.util.Assert; -import net.helenus.core.annotation.Retry; - @Aspect public class RetryConcurrentUnitOfWorkAspect { - private static final Logger log = LoggerFactory.getLogger(RetryConcurrentUnitOfWorkAspect.class); + private static final Logger log = LoggerFactory.getLogger(RetryConcurrentUnitOfWorkAspect.class); - @Around("@annotation(net.helenus.core.annotations.Retry)") - public Object retry(ProceedingJoinPoint pjp) throws Throwable { - Retry retryAnnotation = getRetryAnnotation(pjp); - return (retryAnnotation != null) ? proceed(pjp, retryAnnotation) : proceed(pjp); - } + @Around("@annotation(net.helenus.core.annotations.Retry)") + public Object retry(ProceedingJoinPoint pjp) throws Throwable { + Retry retryAnnotation = getRetryAnnotation(pjp); + return (retryAnnotation != null) ? proceed(pjp, retryAnnotation) : proceed(pjp); + } - private Object proceed(ProceedingJoinPoint pjp) throws Throwable { - return pjp.proceed(); - } + private Object proceed(ProceedingJoinPoint pjp) throws Throwable { + return pjp.proceed(); + } - private Object proceed(ProceedingJoinPoint pjp, Retry retryAnnotation) throws Throwable { - int times = retryAnnotation.times(); - Class[] retryOn = retryAnnotation.on(); - Assert.isTrue(times > 0, "@Retry{times} should be greater than 0!"); - Assert.isTrue(retryOn.length > 0, "@Retry{on} should have at least one Throwable!"); - log.info("Proceed with {} retries on {}", times, Arrays.toString(retryOn)); - return tryProceeding(pjp, times, retryOn); - } + private Object proceed(ProceedingJoinPoint pjp, Retry retryAnnotation) throws Throwable { + int times = retryAnnotation.times(); + Class[] retryOn = retryAnnotation.on(); + Assert.isTrue(times > 0, "@Retry{times} should be greater than 0!"); + Assert.isTrue(retryOn.length > 0, "@Retry{on} should have at least one Throwable!"); + log.info("Proceed with {} retries on {}", times, Arrays.toString(retryOn)); + return tryProceeding(pjp, times, retryOn); + } - private Object tryProceeding(ProceedingJoinPoint pjp, int times, Class[] retryOn) - throws Throwable { - try { - return proceed(pjp); - } catch (Throwable throwable) { - if (isRetryThrowable(throwable, retryOn) && times-- > 0) { - log.info("Conflict detected, {} remaining retries on {}", times, Arrays.toString(retryOn)); - return tryProceeding(pjp, times, retryOn); - } - throw throwable; - } - } + private Object tryProceeding( + ProceedingJoinPoint pjp, int times, Class[] retryOn) throws Throwable { + try { + return proceed(pjp); + } catch (Throwable throwable) { + if (isRetryThrowable(throwable, retryOn) && times-- > 0) { + log.info("Conflict detected, {} remaining retries on {}", times, Arrays.toString(retryOn)); + return tryProceeding(pjp, times, retryOn); + } + throw throwable; + } + } - private boolean isRetryThrowable(Throwable throwable, Class[] retryOn) { - Throwable[] causes = ExceptionUtils.getThrowables(throwable); - for (Throwable cause : causes) { - for (Class retryThrowable : retryOn) { - if (retryThrowable.isAssignableFrom(cause.getClass())) { - return true; - } - } - } - return false; - } + private boolean isRetryThrowable(Throwable throwable, Class[] retryOn) { + Throwable[] causes = ExceptionUtils.getThrowables(throwable); + for (Throwable cause : causes) { + for (Class retryThrowable : retryOn) { + if (retryThrowable.isAssignableFrom(cause.getClass())) { + return true; + } + } + } + return false; + } - private Retry getRetryAnnotation(ProceedingJoinPoint pjp) throws NoSuchMethodException { - MethodSignature signature = (MethodSignature) pjp.getSignature(); - Method method = signature.getMethod(); - Retry retryAnnotation = AnnotationUtils.findAnnotation(method, Retry.class); + private Retry getRetryAnnotation(ProceedingJoinPoint pjp) throws NoSuchMethodException { + MethodSignature signature = (MethodSignature) pjp.getSignature(); + Method method = signature.getMethod(); + Retry retryAnnotation = AnnotationUtils.findAnnotation(method, Retry.class); - if (retryAnnotation != null) { - return retryAnnotation; - } + if (retryAnnotation != null) { + return retryAnnotation; + } - Class[] argClasses = new Class[pjp.getArgs().length]; - for (int i = 0; i < pjp.getArgs().length; i++) { - argClasses[i] = pjp.getArgs()[i].getClass(); - } - method = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(), argClasses); - return AnnotationUtils.findAnnotation(method, Retry.class); - } + Class[] argClasses = new Class[pjp.getArgs().length]; + for (int i = 0; i < pjp.getArgs().length; i++) { + argClasses[i] = pjp.getArgs()[i].getClass(); + } + method = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(), argClasses); + return AnnotationUtils.findAnnotation(method, Retry.class); + } } diff --git a/src/main/java/net/helenus/core/cache/BoundFacet.java b/src/main/java/net/helenus/core/cache/BoundFacet.java index 0eea23c..9ecbd83 100644 --- a/src/main/java/net/helenus/core/cache/BoundFacet.java +++ b/src/main/java/net/helenus/core/cache/BoundFacet.java @@ -18,28 +18,37 @@ package net.helenus.core.cache; import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; - import net.helenus.mapping.HelenusProperty; public class BoundFacet extends Facet { - private final Map properties; + private final Map properties; - public BoundFacet(HelenusProperty property, Object value) { - super(property.getPropertyName(), value == null ? null : value.toString()); - this.properties = new HashMap(1); - this.properties.put(property, value); - } - - public BoundFacet(String name, Map properties) { - super(name, - (properties.keySet().size() > 1) - ? "[" + String.join(", ", - properties.keySet().stream().map(key -> properties.get(key).toString()) - .collect(Collectors.toSet())) - + "]" - : String.join("", properties.keySet().stream().map(key -> properties.get(key).toString()) - .collect(Collectors.toSet()))); - this.properties = properties; - } + public BoundFacet(HelenusProperty property, Object value) { + super(property.getPropertyName(), value == null ? null : value.toString()); + this.properties = new HashMap(1); + this.properties.put(property, value); + } + public BoundFacet(String name, Map properties) { + super( + name, + (properties.keySet().size() > 1) + ? "[" + + String.join( + ", ", + properties + .keySet() + .stream() + .map(key -> properties.get(key).toString()) + .collect(Collectors.toSet())) + + "]" + : String.join( + "", + properties + .keySet() + .stream() + .map(key -> properties.get(key).toString()) + .collect(Collectors.toSet()))); + this.properties = properties; + } } diff --git a/src/main/java/net/helenus/core/cache/CacheUtil.java b/src/main/java/net/helenus/core/cache/CacheUtil.java index 2607d45..a2c2e9f 100644 --- a/src/main/java/net/helenus/core/cache/CacheUtil.java +++ b/src/main/java/net/helenus/core/cache/CacheUtil.java @@ -6,59 +6,68 @@ import java.util.stream.Collectors; public class CacheUtil { - public static List combinations(List items) { - int n = items.size(); - if (n > 20 || n < 0) - throw new IllegalArgumentException(n + " is out of range"); - long e = Math.round(Math.pow(2, n)); - List out = new ArrayList((int) e - 1); - for (int k = 1; k <= items.size(); k++) { - kCombinations(items, 0, k, new String[k], out); - } - return out; - } + public static List combinations(List items) { + int n = items.size(); + if (n > 20) throw new IllegalArgumentException(n + " is out of range"); + long e = Math.round(Math.pow(2, n)); + List out = new ArrayList((int) e - 1); + for (int k = 1; k <= items.size(); k++) { + kCombinations(items, 0, k, new String[k], out); + } + return out; + } - private static void kCombinations(List items, int n, int k, String[] arr, List out) { - if (k == 0) { - out.add(arr.clone()); - } else { - for (int i = n; i <= items.size() - k; i++) { - arr[arr.length - k] = items.get(i); - kCombinations(items, i + 1, k - 1, arr, out); - } - } - } + private static void kCombinations( + List items, int n, int k, String[] arr, List out) { + if (k == 0) { + out.add(arr.clone()); + } else { + for (int i = n; i <= items.size() - k; i++) { + arr[arr.length - k] = items.get(i); + kCombinations(items, i + 1, k - 1, arr, out); + } + } + } - public static List flattenFacets(List facets) { - List combinations = CacheUtil.combinations( - facets.stream().filter(facet -> !facet.fixed()).filter(facet -> facet.value() != null).map(facet -> { - return facet.name() + "==" + facet.value(); - }).collect(Collectors.toList())); - return combinations; - } + public static List flattenFacets(List facets) { + List combinations = + CacheUtil.combinations( + facets + .stream() + .filter(facet -> !facet.fixed()) + .filter(facet -> facet.value() != null) + .map( + facet -> { + return facet.name() + "==" + facet.value(); + }) + .collect(Collectors.toList())); + return combinations; + } - public static Object merge(Object to, Object from) { - if (to == from) { - return to; - } else { - return from; - } - /* - * // TODO(gburd): take ttl and writeTime into account when merging. Map toValueMap = to instanceof MapExportable ? ((MapExportable) - * to).toMap() : null; Map fromValueMap = to instanceof - * MapExportable ? ((MapExportable) from).toMap() : null; - * - * if (toValueMap != null && fromValueMap != null) { for (String key : - * fromValueMap.keySet()) { if (toValueMap.containsKey(key) && - * toValueMap.get(key) != fromValueMap.get(key)) { toValueMap.put(key, - * fromValueMap.get(key)); } } } return to; - */ - } - - public static String schemaName(List facets) { - return facets.stream().filter(Facet::fixed).map(facet -> facet.value().toString()) - .collect(Collectors.joining(".")); - } + public static Object merge(Object to, Object from) { + if (to == from) { + return to; + } else { + return from; + } + /* + * // TODO(gburd): take ttl and writeTime into account when merging. Map toValueMap = to instanceof MapExportable ? ((MapExportable) + * to).toMap() : null; Map fromValueMap = to instanceof + * MapExportable ? ((MapExportable) from).toMap() : null; + * + * if (toValueMap != null && fromValueMap != null) { for (String key : + * fromValueMap.keySet()) { if (toValueMap.containsKey(key) && + * toValueMap.get(key) != fromValueMap.get(key)) { toValueMap.put(key, + * fromValueMap.get(key)); } } } return to; + */ + } + public static String schemaName(List facets) { + return facets + .stream() + .filter(Facet::fixed) + .map(facet -> facet.value().toString()) + .collect(Collectors.joining(".")); + } } diff --git a/src/main/java/net/helenus/core/cache/Facet.java b/src/main/java/net/helenus/core/cache/Facet.java index 4f74ab5..27eb52b 100644 --- a/src/main/java/net/helenus/core/cache/Facet.java +++ b/src/main/java/net/helenus/core/cache/Facet.java @@ -16,38 +16,35 @@ package net.helenus.core.cache; -/** - * An Entity is identifiable via one or more Facets - */ +/** An Entity is identifiable via one or more Facets */ public class Facet { - private final String name; - private T value; - private boolean fixed = false; + private final String name; + private T value; + private boolean fixed = false; - public Facet(String name) { - this.name = name; - } + public Facet(String name) { + this.name = name; + } - public Facet(String name, T value) { - this.name = name; - this.value = value; - } + public Facet(String name, T value) { + this.name = name; + this.value = value; + } - public String name() { - return name; - } + public String name() { + return name; + } - public T value() { - return value; - } + public T value() { + return value; + } - public Facet setFixed() { - fixed = true; - return this; - } - - public boolean fixed() { - return fixed; - } + public Facet setFixed() { + fixed = true; + return this; + } + public boolean fixed() { + return fixed; + } } diff --git a/src/main/java/net/helenus/core/cache/GuavaCache.java b/src/main/java/net/helenus/core/cache/GuavaCache.java index 4411e7c..5418c3a 100644 --- a/src/main/java/net/helenus/core/cache/GuavaCache.java +++ b/src/main/java/net/helenus/core/cache/GuavaCache.java @@ -20,25 +20,24 @@ import com.google.common.cache.Cache; public class GuavaCache implements SessionCache { - final Cache cache; + final Cache cache; - GuavaCache(Cache cache) { - this.cache = cache; - } + GuavaCache(Cache cache) { + this.cache = cache; + } - @Override - public void invalidate(K key) { - cache.invalidate(key); - } + @Override + public void invalidate(K key) { + cache.invalidate(key); + } - @Override - public V get(K key) { - return cache.getIfPresent(key); - } - - @Override - public void put(K key, V value) { - cache.put(key, value); - } + @Override + public V get(K key) { + return cache.getIfPresent(key); + } + @Override + public void put(K key, V value) { + cache.put(key, value); + } } diff --git a/src/main/java/net/helenus/core/cache/SessionCache.java b/src/main/java/net/helenus/core/cache/SessionCache.java index 44717e0..4b6d8d0 100644 --- a/src/main/java/net/helenus/core/cache/SessionCache.java +++ b/src/main/java/net/helenus/core/cache/SessionCache.java @@ -16,21 +16,45 @@ package net.helenus.core.cache; -import java.util.concurrent.TimeUnit; - import com.google.common.cache.CacheBuilder; +import com.google.common.cache.RemovalListener; +import com.google.common.cache.RemovalNotification; +import java.util.concurrent.TimeUnit; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public interface SessionCache { - static SessionCache defaultCache() { - int MAX_CACHE_SIZE = 10000; - int MAX_CACHE_EXPIRE_SECONDS = 600; - return new GuavaCache(CacheBuilder.newBuilder().maximumSize(MAX_CACHE_SIZE) - .expireAfterAccess(MAX_CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS) - .expireAfterWrite(MAX_CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS).recordStats().build()); - } + static final Logger LOG = LoggerFactory.getLogger(SessionCache.class); - void invalidate(K key); - V get(K key); - void put(K key, V value); + static SessionCache defaultCache() { + GuavaCache cache; + RemovalListener listener = + new RemovalListener() { + @Override + public void onRemoval(RemovalNotification n) { + if (n.wasEvicted()) { + String cause = n.getCause().name(); + LOG.info(cause); + } + } + }; + + cache = + new GuavaCache( + CacheBuilder.newBuilder() + .maximumSize(25_000) + .expireAfterAccess(5, TimeUnit.MINUTES) + .softValues() + .removalListener(listener) + .build()); + + return cache; + } + + void invalidate(K key); + + V get(K key); + + void put(K key, V value); } diff --git a/src/main/java/net/helenus/core/cache/UnboundFacet.java b/src/main/java/net/helenus/core/cache/UnboundFacet.java index e2b618b..60c0d36 100644 --- a/src/main/java/net/helenus/core/cache/UnboundFacet.java +++ b/src/main/java/net/helenus/core/cache/UnboundFacet.java @@ -19,56 +19,55 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; - import net.helenus.core.SchemaUtil; import net.helenus.mapping.HelenusProperty; public class UnboundFacet extends Facet { - private final List properties; + private final List properties; - public UnboundFacet(List properties) { - super(SchemaUtil.createPrimaryKeyPhrase(properties)); - this.properties = properties; - } + public UnboundFacet(List properties) { + super(SchemaUtil.createPrimaryKeyPhrase(properties)); + this.properties = properties; + } - public UnboundFacet(HelenusProperty property) { - super(property.getPropertyName()); - properties = new ArrayList(); - properties.add(property); - } + public UnboundFacet(HelenusProperty property) { + super(property.getPropertyName()); + properties = new ArrayList(); + properties.add(property); + } - public List getProperties() { - return properties; - } + public List getProperties() { + return properties; + } - public Binder binder() { - return new Binder(name(), properties); - } + public Binder binder() { + return new Binder(name(), properties); + } - public static class Binder { + public static class Binder { - private final String name; - private final List properties = new ArrayList(); - private Map boundProperties = new HashMap(); + private final String name; + private final List properties = new ArrayList(); + private Map boundProperties = new HashMap(); - Binder(String name, List properties) { - this.name = name; - this.properties.addAll(properties); - } + Binder(String name, List properties) { + this.name = name; + this.properties.addAll(properties); + } - public Binder setValueForProperty(HelenusProperty prop, Object value) { - properties.remove(prop); - boundProperties.put(prop, value); - return this; - } + public Binder setValueForProperty(HelenusProperty prop, Object value) { + properties.remove(prop); + boundProperties.put(prop, value); + return this; + } - public boolean isBound() { - return properties.isEmpty(); - } + public boolean isBound() { + return properties.isEmpty(); + } - public BoundFacet bind() { - return new BoundFacet(name, boundProperties); - } - } + public BoundFacet bind() { + return new BoundFacet(name, boundProperties); + } + } } diff --git a/src/main/java/net/helenus/core/operation/AbstractFilterOperation.java b/src/main/java/net/helenus/core/operation/AbstractFilterOperation.java index eb30d37..93e164e 100644 --- a/src/main/java/net/helenus/core/operation/AbstractFilterOperation.java +++ b/src/main/java/net/helenus/core/operation/AbstractFilterOperation.java @@ -16,132 +16,129 @@ package net.helenus.core.operation; import java.util.*; - import net.helenus.core.*; import net.helenus.core.cache.Facet; import net.helenus.core.cache.UnboundFacet; import net.helenus.mapping.HelenusProperty; public abstract class AbstractFilterOperation> - extends - AbstractOperation { + extends AbstractOperation { - protected List> filters = null; - protected List> ifFilters = null; + protected List> filters = null; + protected List> ifFilters = null; - public AbstractFilterOperation(AbstractSessionOperations sessionOperations) { - super(sessionOperations); - } + public AbstractFilterOperation(AbstractSessionOperations sessionOperations) { + super(sessionOperations); + } - public O where(Getter getter, Postulate postulate) { + public O where(Getter getter, Postulate postulate) { - addFilter(Filter.create(getter, postulate)); + addFilter(Filter.create(getter, postulate)); - return (O) this; - } + return (O) this; + } - public O where(Getter getter, Operator operator, V val) { + public O where(Getter getter, Operator operator, V val) { - addFilter(Filter.create(getter, operator, val)); + addFilter(Filter.create(getter, operator, val)); - return (O) this; - } + return (O) this; + } - public O where(Filter filter) { + public O where(Filter filter) { - addFilter(filter); + addFilter(filter); - return (O) this; - } + return (O) this; + } - public O and(Getter getter, Postulate postulate) { + public O and(Getter getter, Postulate postulate) { - addFilter(Filter.create(getter, postulate)); + addFilter(Filter.create(getter, postulate)); - return (O) this; - } + return (O) this; + } - public O and(Getter getter, Operator operator, V val) { + public O and(Getter getter, Operator operator, V val) { - addFilter(Filter.create(getter, operator, val)); + addFilter(Filter.create(getter, operator, val)); - return (O) this; - } + return (O) this; + } - public O and(Filter filter) { + public O and(Filter filter) { - addFilter(filter); + addFilter(filter); - return (O) this; - } + return (O) this; + } - public O onlyIf(Getter getter, Postulate postulate) { + public O onlyIf(Getter getter, Postulate postulate) { - addIfFilter(Filter.create(getter, postulate)); + addIfFilter(Filter.create(getter, postulate)); - return (O) this; - } + return (O) this; + } - public O onlyIf(Getter getter, Operator operator, V val) { + public O onlyIf(Getter getter, Operator operator, V val) { - addIfFilter(Filter.create(getter, operator, val)); + addIfFilter(Filter.create(getter, operator, val)); - return (O) this; - } + return (O) this; + } - public O onlyIf(Filter filter) { + public O onlyIf(Filter filter) { - addIfFilter(filter); + addIfFilter(filter); - return (O) this; - } + return (O) this; + } - private void addFilter(Filter filter) { - if (filters == null) { - filters = new LinkedList>(); - } - filters.add(filter); - } + private void addFilter(Filter filter) { + if (filters == null) { + filters = new LinkedList>(); + } + filters.add(filter); + } - private void addIfFilter(Filter filter) { - if (ifFilters == null) { - ifFilters = new LinkedList>(); - } - ifFilters.add(filter); - } + private void addIfFilter(Filter filter) { + if (ifFilters == null) { + ifFilters = new LinkedList>(); + } + ifFilters.add(filter); + } - protected List bindFacetValues(List facets) { - if (facets == null) { - return new ArrayList(); - } - List boundFacets = new ArrayList<>(); - Map filterMap = new HashMap<>(filters.size()); - filters.forEach(f -> filterMap.put(f.getNode().getProperty(), f)); + protected List bindFacetValues(List facets) { + if (facets == null) { + return new ArrayList(); + } + List boundFacets = new ArrayList<>(); + Map 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()) { + 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; - } + 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; + } } diff --git a/src/main/java/net/helenus/core/operation/AbstractFilterOptionalOperation.java b/src/main/java/net/helenus/core/operation/AbstractFilterOptionalOperation.java index 83374aa..6abfd81 100644 --- a/src/main/java/net/helenus/core/operation/AbstractFilterOptionalOperation.java +++ b/src/main/java/net/helenus/core/operation/AbstractFilterOptionalOperation.java @@ -19,95 +19,94 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; - import net.helenus.core.*; import net.helenus.mapping.HelenusProperty; -public abstract class AbstractFilterOptionalOperation> - extends - AbstractOptionalOperation { +public abstract class AbstractFilterOptionalOperation< + E, O extends AbstractFilterOptionalOperation> + extends AbstractOptionalOperation { - protected Map> filters = null; - protected List> ifFilters = null; + protected Map> filters = null; + protected List> ifFilters = null; - public AbstractFilterOptionalOperation(AbstractSessionOperations sessionOperations) { - super(sessionOperations); - } + public AbstractFilterOptionalOperation(AbstractSessionOperations sessionOperations) { + super(sessionOperations); + } - public O where(Getter getter, Postulate postulate) { + public O where(Getter getter, Postulate postulate) { - addFilter(Filter.create(getter, postulate)); + addFilter(Filter.create(getter, postulate)); - return (O) this; - } + return (O) this; + } - public O where(Getter getter, Operator operator, V val) { + public O where(Getter getter, Operator operator, V val) { - addFilter(Filter.create(getter, operator, val)); + addFilter(Filter.create(getter, operator, val)); - return (O) this; - } + return (O) this; + } - public O where(Filter filter) { + public O where(Filter filter) { - addFilter(filter); + addFilter(filter); - return (O) this; - } + return (O) this; + } - public O and(Getter getter, Postulate postulate) { + public O and(Getter getter, Postulate postulate) { - addFilter(Filter.create(getter, postulate)); + addFilter(Filter.create(getter, postulate)); - return (O) this; - } + return (O) this; + } - public O and(Getter getter, Operator operator, V val) { + public O and(Getter getter, Operator operator, V val) { - addFilter(Filter.create(getter, operator, val)); + addFilter(Filter.create(getter, operator, val)); - return (O) this; - } + return (O) this; + } - public O and(Filter filter) { + public O and(Filter filter) { - addFilter(filter); + addFilter(filter); - return (O) this; - } + return (O) this; + } - public O onlyIf(Getter getter, Postulate postulate) { + public O onlyIf(Getter getter, Postulate postulate) { - addIfFilter(Filter.create(getter, postulate)); + addIfFilter(Filter.create(getter, postulate)); - return (O) this; - } + return (O) this; + } - public O onlyIf(Getter getter, Operator operator, V val) { + public O onlyIf(Getter getter, Operator operator, V val) { - addIfFilter(Filter.create(getter, operator, val)); + addIfFilter(Filter.create(getter, operator, val)); - return (O) this; - } + return (O) this; + } - public O onlyIf(Filter filter) { + public O onlyIf(Filter filter) { - addIfFilter(filter); + addIfFilter(filter); - return (O) this; - } + return (O) this; + } - private void addFilter(Filter filter) { - if (filters == null) { - filters = new LinkedHashMap>(); - } - filters.put(filter.getNode().getProperty(), filter); - } + private void addFilter(Filter filter) { + if (filters == null) { + filters = new LinkedHashMap>(); + } + filters.put(filter.getNode().getProperty(), filter); + } - private void addIfFilter(Filter filter) { - if (ifFilters == null) { - ifFilters = new LinkedList>(); - } - ifFilters.add(filter); - } + private void addIfFilter(Filter filter) { + if (ifFilters == null) { + ifFilters = new LinkedList>(); + } + ifFilters.add(filter); + } } diff --git a/src/main/java/net/helenus/core/operation/AbstractFilterStreamOperation.java b/src/main/java/net/helenus/core/operation/AbstractFilterStreamOperation.java index 519f421..b78daf1 100644 --- a/src/main/java/net/helenus/core/operation/AbstractFilterStreamOperation.java +++ b/src/main/java/net/helenus/core/operation/AbstractFilterStreamOperation.java @@ -19,95 +19,94 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; - import net.helenus.core.*; import net.helenus.mapping.HelenusProperty; -public abstract class AbstractFilterStreamOperation> - extends - AbstractStreamOperation { +public abstract class AbstractFilterStreamOperation< + E, O extends AbstractFilterStreamOperation> + extends AbstractStreamOperation { - protected Map> filters = null; - protected List> ifFilters = null; + protected Map> filters = null; + protected List> ifFilters = null; - public AbstractFilterStreamOperation(AbstractSessionOperations sessionOperations) { - super(sessionOperations); - } + public AbstractFilterStreamOperation(AbstractSessionOperations sessionOperations) { + super(sessionOperations); + } - public O where(Getter getter, Postulate postulate) { + public O where(Getter getter, Postulate postulate) { - addFilter(Filter.create(getter, postulate)); + addFilter(Filter.create(getter, postulate)); - return (O) this; - } + return (O) this; + } - public O where(Getter getter, Operator operator, V val) { + public O where(Getter getter, Operator operator, V val) { - addFilter(Filter.create(getter, operator, val)); + addFilter(Filter.create(getter, operator, val)); - return (O) this; - } + return (O) this; + } - public O where(Filter filter) { + public O where(Filter filter) { - addFilter(filter); + addFilter(filter); - return (O) this; - } + return (O) this; + } - public O and(Getter getter, Postulate postulate) { + public O and(Getter getter, Postulate postulate) { - addFilter(Filter.create(getter, postulate)); + addFilter(Filter.create(getter, postulate)); - return (O) this; - } + return (O) this; + } - public O and(Getter getter, Operator operator, V val) { + public O and(Getter getter, Operator operator, V val) { - addFilter(Filter.create(getter, operator, val)); + addFilter(Filter.create(getter, operator, val)); - return (O) this; - } + return (O) this; + } - public O and(Filter filter) { + public O and(Filter filter) { - addFilter(filter); + addFilter(filter); - return (O) this; - } + return (O) this; + } - public O onlyIf(Getter getter, Postulate postulate) { + public O onlyIf(Getter getter, Postulate postulate) { - addIfFilter(Filter.create(getter, postulate)); + addIfFilter(Filter.create(getter, postulate)); - return (O) this; - } + return (O) this; + } - public O onlyIf(Getter getter, Operator operator, V val) { + public O onlyIf(Getter getter, Operator operator, V val) { - addIfFilter(Filter.create(getter, operator, val)); + addIfFilter(Filter.create(getter, operator, val)); - return (O) this; - } + return (O) this; + } - public O onlyIf(Filter filter) { + public O onlyIf(Filter filter) { - addIfFilter(filter); + addIfFilter(filter); - return (O) this; - } + return (O) this; + } - private void addFilter(Filter filter) { - if (filters == null) { - filters = new LinkedHashMap>(); - } - filters.put(filter.getNode().getProperty(), filter); - } + private void addFilter(Filter filter) { + if (filters == null) { + filters = new LinkedHashMap>(); + } + filters.put(filter.getNode().getProperty(), filter); + } - private void addIfFilter(Filter filter) { - if (ifFilters == null) { - ifFilters = new LinkedList>(); - } - ifFilters.add(filter); - } + private void addIfFilter(Filter filter) { + if (ifFilters == null) { + ifFilters = new LinkedList>(); + } + ifFilters.add(filter); + } } diff --git a/src/main/java/net/helenus/core/operation/AbstractOperation.java b/src/main/java/net/helenus/core/operation/AbstractOperation.java index 27d7eaf..9d12a52 100644 --- a/src/main/java/net/helenus/core/operation/AbstractOperation.java +++ b/src/main/java/net/helenus/core/operation/AbstractOperation.java @@ -15,73 +15,86 @@ */ package net.helenus.core.operation; +import com.codahale.metrics.Timer; +import com.datastax.driver.core.ResultSet; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.TimeoutException; - -import com.codahale.metrics.Timer; -import com.datastax.driver.core.ResultSet; - import net.helenus.core.AbstractSessionOperations; import net.helenus.core.UnitOfWork; -public abstract class AbstractOperation> extends AbstractStatementOperation { +public abstract class AbstractOperation> + extends AbstractStatementOperation { - public AbstractOperation(AbstractSessionOperations sessionOperations) { - super(sessionOperations); - } + public AbstractOperation(AbstractSessionOperations sessionOperations) { + super(sessionOperations); + } - public abstract E transform(ResultSet resultSet); + public abstract E transform(ResultSet resultSet); - public PreparedOperation prepare() { - return new PreparedOperation(prepareStatement(), this); - } + public PreparedOperation prepare() { + return new PreparedOperation(prepareStatement(), this); + } - public E sync() throws TimeoutException { - final Timer.Context context = requestLatency.time(); - try { - ResultSet resultSet = this.execute(sessionOps, null, traceContext, queryExecutionTimeout, queryTimeoutUnits, - showValues, false); - return transform(resultSet); - } finally { - context.stop(); - } - } + public E sync() throws TimeoutException { + final Timer.Context context = requestLatency.time(); + try { + ResultSet resultSet = + this.execute( + sessionOps, + null, + traceContext, + queryExecutionTimeout, + queryTimeoutUnits, + showValues, + false); + return transform(resultSet); + } finally { + context.stop(); + } + } - public E sync(UnitOfWork uow) throws TimeoutException { - if (uow == null) - return sync(); + public E sync(UnitOfWork uow) throws TimeoutException { + if (uow == null) return sync(); - final Timer.Context context = requestLatency.time(); - try { - ResultSet resultSet = execute(sessionOps, uow, traceContext, queryExecutionTimeout, queryTimeoutUnits, - showValues, true); - E result = transform(resultSet); - return result; - } finally { - context.stop(); - } - } + final Timer.Context context = requestLatency.time(); + try { + ResultSet resultSet = + execute( + sessionOps, + uow, + traceContext, + queryExecutionTimeout, + queryTimeoutUnits, + showValues, + true); + E result = transform(resultSet); + return result; + } finally { + context.stop(); + } + } - public CompletableFuture async() { - return CompletableFuture.supplyAsync(() -> { - try { - return sync(); - } catch (TimeoutException ex) { - throw new CompletionException(ex); - } - }); - } + public CompletableFuture async() { + return CompletableFuture.supplyAsync( + () -> { + try { + return sync(); + } catch (TimeoutException ex) { + throw new CompletionException(ex); + } + }); + } - public CompletableFuture async(UnitOfWork uow) { - if (uow == null) - return async(); - return CompletableFuture.supplyAsync(() -> { - try { - return sync(); - } catch (TimeoutException ex) { - throw new CompletionException(ex); - } - }); - } + public CompletableFuture async(UnitOfWork uow) { + if (uow == null) return async(); + return CompletableFuture.supplyAsync( + () -> { + try { + return sync(); + } catch (TimeoutException ex) { + throw new CompletionException(ex); + } + }); + } } diff --git a/src/main/java/net/helenus/core/operation/AbstractOptionalOperation.java b/src/main/java/net/helenus/core/operation/AbstractOptionalOperation.java index 87a1bad..8fe5c11 100644 --- a/src/main/java/net/helenus/core/operation/AbstractOptionalOperation.java +++ b/src/main/java/net/helenus/core/operation/AbstractOptionalOperation.java @@ -17,12 +17,6 @@ package net.helenus.core.operation; import static net.helenus.core.HelenusSession.deleted; -import java.util.List; -import java.util.Optional; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; -import java.util.concurrent.TimeoutException; - import com.codahale.metrics.Timer; import com.datastax.driver.core.PreparedStatement; import com.datastax.driver.core.ResultSet; @@ -30,180 +24,198 @@ import com.google.common.base.Function; import com.google.common.base.Stopwatch; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; - +import java.util.List; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.TimeoutException; import net.helenus.core.AbstractSessionOperations; import net.helenus.core.UnitOfWork; import net.helenus.core.cache.CacheUtil; import net.helenus.core.cache.Facet; public abstract class AbstractOptionalOperation> - extends - AbstractStatementOperation { + extends AbstractStatementOperation { - public AbstractOptionalOperation(AbstractSessionOperations sessionOperations) { - super(sessionOperations); - } + public AbstractOptionalOperation(AbstractSessionOperations sessionOperations) { + super(sessionOperations); + } - public abstract Optional transform(ResultSet resultSet); + public abstract Optional transform(ResultSet resultSet); - public PreparedOptionalOperation prepare() { - return new PreparedOptionalOperation(prepareStatement(), this); - } + public PreparedOptionalOperation prepare() { + return new PreparedOptionalOperation(prepareStatement(), this); + } - public ListenableFuture> prepareAsync() { - final O _this = (O) this; - return Futures.transform(prepareStatementAsync(), - new Function>() { - @Override - public PreparedOptionalOperation apply(PreparedStatement preparedStatement) { - return new PreparedOptionalOperation(preparedStatement, _this); - } - }); - } + public ListenableFuture> prepareAsync() { + final O _this = (O) this; + return Futures.transform( + prepareStatementAsync(), + new Function>() { + @Override + public PreparedOptionalOperation apply(PreparedStatement preparedStatement) { + return new PreparedOptionalOperation(preparedStatement, _this); + } + }); + } - public Optional sync() throws TimeoutException { - final Timer.Context context = requestLatency.time(); - try { - Optional result = Optional.empty(); - E cacheResult = null; - boolean updateCache = isSessionCacheable() && checkCache; + public Optional sync() throws TimeoutException { + final Timer.Context context = requestLatency.time(); + try { + Optional result = Optional.empty(); + E cacheResult = null; + boolean updateCache = isSessionCacheable() && checkCache; - if (checkCache && isSessionCacheable()) { - List facets = bindFacetValues(); - String tableName = CacheUtil.schemaName(facets); - cacheResult = (E) sessionOps.checkCache(tableName, facets); - if (cacheResult != null) { - result = Optional.of(cacheResult); - updateCache = false; - sessionCacheHits.mark(); - cacheHits.mark(); - } else { - sessionCacheMiss.mark(); - cacheMiss.mark(); - } - } + if (checkCache && isSessionCacheable()) { + List facets = bindFacetValues(); + String tableName = CacheUtil.schemaName(facets); + cacheResult = (E) sessionOps.checkCache(tableName, facets); + if (cacheResult != null) { + result = Optional.of(cacheResult); + updateCache = false; + sessionCacheHits.mark(); + cacheHits.mark(); + } else { + sessionCacheMiss.mark(); + cacheMiss.mark(); + } + } - if (!result.isPresent()) { - // Formulate the query and execute it against the Cassandra cluster. - ResultSet resultSet = this.execute(sessionOps, null, traceContext, queryExecutionTimeout, - queryTimeoutUnits, showValues, false); + if (!result.isPresent()) { + // Formulate the query and execute it against the Cassandra cluster. + ResultSet resultSet = + this.execute( + sessionOps, + null, + traceContext, + queryExecutionTimeout, + queryTimeoutUnits, + showValues, + false); - // Transform the query result set into the desired shape. - result = transform(resultSet); - } + // Transform the query result set into the desired shape. + result = transform(resultSet); + } - if (updateCache && result.isPresent()) { - List facets = getFacets(); - if (facets != null && facets.size() > 1) { - sessionOps.updateCache(result.get(), facets); - } - } - return result; - } finally { - context.stop(); - } - } + if (updateCache && result.isPresent()) { + List facets = getFacets(); + if (facets != null && facets.size() > 1) { + sessionOps.updateCache(result.get(), facets); + } + } + return result; + } finally { + context.stop(); + } + } - public Optional sync(UnitOfWork uow) throws TimeoutException { - if (uow == null) - return sync(); + public Optional sync(UnitOfWork uow) throws TimeoutException { + if (uow == null) return sync(); - final Timer.Context context = requestLatency.time(); - try { + final Timer.Context context = requestLatency.time(); + try { - Optional result = Optional.empty(); - E cachedResult = null; - final boolean updateCache; + Optional result = Optional.empty(); + E cachedResult = null; + final boolean updateCache; - if (checkCache) { - Stopwatch timer = Stopwatch.createStarted(); - try { - List facets = bindFacetValues(); - 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 (checkCache) { + Stopwatch timer = Stopwatch.createStarted(); + try { + List facets = bindFacetValues(); + 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; + } - // Check to see if we fetched the object from the cache - if (result.isPresent()) { - // If we fetched the `deleted` object then the result is null (really - // Optional.empty()). - if (result.get() == deleted) { - result = Optional.empty(); - } - } else { + // Check to see if we fetched the object from the cache + if (result.isPresent()) { + // If we fetched the `deleted` object then the result is null (really + // Optional.empty()). + if (result.get() == deleted) { + result = Optional.empty(); + } + } else { - // Formulate the query and execute it against the Cassandra cluster. - ResultSet resultSet = execute(sessionOps, uow, traceContext, queryExecutionTimeout, queryTimeoutUnits, - showValues, true); + // Formulate the query and execute it against the Cassandra cluster. + ResultSet resultSet = + execute( + sessionOps, + uow, + traceContext, + queryExecutionTimeout, + queryTimeoutUnits, + showValues, + true); - // Transform the query result set into the desired shape. - result = transform(resultSet); - } + // Transform the query result set into the desired shape. + 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() && result.get() != deleted) { - cacheUpdate(uow, result.get(), getFacets()); - } + // 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() && result.get() != deleted) { + cacheUpdate(uow, result.get(), getFacets()); + } - return result; - } finally { - context.stop(); - } - } + return result; + } finally { + context.stop(); + } + } - public CompletableFuture> async() { - return CompletableFuture.>supplyAsync(() -> { - try { - return sync(); - } catch (TimeoutException ex) { - throw new CompletionException(ex); - } - }); - } + public CompletableFuture> async() { + return CompletableFuture.>supplyAsync( + () -> { + try { + return sync(); + } catch (TimeoutException ex) { + throw new CompletionException(ex); + } + }); + } - public CompletableFuture> async(UnitOfWork uow) { - if (uow == null) - return async(); - return CompletableFuture.>supplyAsync(() -> { - try { - return sync(); - } catch (TimeoutException ex) { - throw new CompletionException(ex); - } - }); - } + public CompletableFuture> async(UnitOfWork uow) { + if (uow == null) return async(); + return CompletableFuture.>supplyAsync( + () -> { + try { + return sync(); + } catch (TimeoutException ex) { + throw new CompletionException(ex); + } + }); + } } diff --git a/src/main/java/net/helenus/core/operation/AbstractStatementOperation.java b/src/main/java/net/helenus/core/operation/AbstractStatementOperation.java index 7f9cff3..791f2ca 100644 --- a/src/main/java/net/helenus/core/operation/AbstractStatementOperation.java +++ b/src/main/java/net/helenus/core/operation/AbstractStatementOperation.java @@ -15,12 +15,8 @@ */ package net.helenus.core.operation; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.TimeUnit; - +import brave.Tracer; +import brave.propagation.TraceContext; import com.datastax.driver.core.ConsistencyLevel; import com.datastax.driver.core.PreparedStatement; import com.datastax.driver.core.RegularStatement; @@ -31,9 +27,11 @@ import com.datastax.driver.core.policies.FallthroughRetryPolicy; import com.datastax.driver.core.policies.RetryPolicy; import com.datastax.driver.core.querybuilder.BuiltStatement; import com.google.common.util.concurrent.ListenableFuture; - -import brave.Tracer; -import brave.propagation.TraceContext; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.TimeUnit; import net.helenus.core.AbstractSessionOperations; import net.helenus.core.UnitOfWork; import net.helenus.core.cache.Facet; @@ -43,323 +41,324 @@ import net.helenus.mapping.HelenusProperty; import net.helenus.mapping.value.BeanColumnValueProvider; import net.helenus.support.HelenusException; -public abstract class AbstractStatementOperation> extends Operation { - - protected boolean checkCache = true; - protected boolean showValues = true; - protected TraceContext traceContext; - long queryExecutionTimeout = 10; - TimeUnit queryTimeoutUnits = TimeUnit.SECONDS; - private ConsistencyLevel consistencyLevel; - private ConsistencyLevel serialConsistencyLevel; - private RetryPolicy retryPolicy; - private boolean idempotent = false; - private boolean enableTracing = false; - private long[] defaultTimestamp = null; - private int[] fetchSize = null; - - public AbstractStatementOperation(AbstractSessionOperations sessionOperations) { - super(sessionOperations); - this.consistencyLevel = sessionOperations.getDefaultConsistencyLevel(); - this.idempotent = sessionOperations.getDefaultQueryIdempotency(); - } - - public abstract Statement buildStatement(boolean cached); - - public O uncached(boolean enabled) { - checkCache = enabled; - return (O) this; - } - - public O uncached() { - checkCache = false; - return (O) this; - } - - public O showValues(boolean enabled) { - this.showValues = enabled; - return (O) this; - } - - public O defaultTimestamp(long timestamp) { - this.defaultTimestamp = new long[1]; - this.defaultTimestamp[0] = timestamp; - return (O) this; - } - - public O retryPolicy(RetryPolicy retryPolicy) { - this.retryPolicy = retryPolicy; - return (O) this; - } - - public O defaultRetryPolicy() { - this.retryPolicy = DefaultRetryPolicy.INSTANCE; - return (O) this; - } - - public O idempotent() { - this.idempotent = true; - return (O) this; - } - - public O isIdempotent(boolean idempotent) { - this.idempotent = idempotent; - return (O) this; - } - - public O downgradingConsistencyRetryPolicy() { - this.retryPolicy = DowngradingConsistencyRetryPolicy.INSTANCE; - return (O) this; - } - - public O fallthroughRetryPolicy() { - this.retryPolicy = FallthroughRetryPolicy.INSTANCE; - return (O) this; - } - - public O consistency(ConsistencyLevel level) { - this.consistencyLevel = level; - return (O) this; - } - - public O consistencyAny() { - this.consistencyLevel = ConsistencyLevel.ANY; - return (O) this; - } - - public O consistencyOne() { - this.consistencyLevel = ConsistencyLevel.ONE; - return (O) this; - } - - public O consistencyQuorum() { - this.consistencyLevel = ConsistencyLevel.QUORUM; - return (O) this; - } - - public O consistencyAll() { - this.consistencyLevel = ConsistencyLevel.ALL; - return (O) this; - } - - public O consistencyLocalOne() { - this.consistencyLevel = ConsistencyLevel.LOCAL_ONE; - return (O) this; - } - - public O consistencyLocalQuorum() { - this.consistencyLevel = ConsistencyLevel.LOCAL_QUORUM; - return (O) this; - } - - public O consistencyEachQuorum() { - this.consistencyLevel = ConsistencyLevel.EACH_QUORUM; - return (O) this; - } - - public O serialConsistency(ConsistencyLevel level) { - this.serialConsistencyLevel = level; - return (O) this; - } - - public O serialConsistencyAny() { - this.serialConsistencyLevel = ConsistencyLevel.ANY; - return (O) this; - } - - public O serialConsistencyOne() { - this.serialConsistencyLevel = ConsistencyLevel.ONE; - return (O) this; - } - - public O serialConsistencyQuorum() { - this.serialConsistencyLevel = ConsistencyLevel.QUORUM; - return (O) this; - } - - public O serialConsistencyAll() { - this.serialConsistencyLevel = ConsistencyLevel.ALL; - return (O) this; - } - - public O serialConsistencyLocal() { - this.serialConsistencyLevel = ConsistencyLevel.LOCAL_SERIAL; - return (O) this; - } - - public O serialConsistencyLocalQuorum() { - this.serialConsistencyLevel = ConsistencyLevel.LOCAL_QUORUM; - return (O) this; - } - - public O disableTracing() { - this.enableTracing = false; - return (O) this; - } - - public O enableTracing() { - this.enableTracing = true; - return (O) this; - } - - public O tracing(boolean enable) { - this.enableTracing = enable; - return (O) this; - } - - public O fetchSize(int fetchSize) { - this.fetchSize = new int[1]; - this.fetchSize[0] = fetchSize; - return (O) this; - } - - public O queryTimeoutMs(long ms) { - this.queryExecutionTimeout = ms; - this.queryTimeoutUnits = TimeUnit.MILLISECONDS; - return (O) this; - } - - public O queryTimeout(long timeout, TimeUnit units) { - this.queryExecutionTimeout = timeout; - this.queryTimeoutUnits = units; - return (O) this; - } - - public Statement options(Statement statement) { - - if (defaultTimestamp != null) { - statement.setDefaultTimestamp(defaultTimestamp[0]); - } - - if (consistencyLevel != null) { - statement.setConsistencyLevel(consistencyLevel); - } - - if (serialConsistencyLevel != null) { - statement.setSerialConsistencyLevel(serialConsistencyLevel); - } - - if (retryPolicy != null) { - statement.setRetryPolicy(retryPolicy); - } - - if (enableTracing) { - statement.enableTracing(); - } else { - statement.disableTracing(); - } - - if (fetchSize != null) { - statement.setFetchSize(fetchSize[0]); - } - - if (idempotent) { - statement.setIdempotent(true); - } - - return statement; - } - - public O zipkinContext(TraceContext traceContext) { - if (traceContext != null) { - Tracer tracer = this.sessionOps.getZipkinTracer(); - if (tracer != null) { - this.traceContext = traceContext; - } - } - - return (O) this; - } - - public Statement statement() { - return buildStatement(false); - } - - public String cql() { - Statement statement = buildStatement(false); - if (statement == null) - return ""; - if (statement instanceof BuiltStatement) { - BuiltStatement buildStatement = (BuiltStatement) statement; - return buildStatement.setForceNoValues(true).getQueryString(); - } else { - return statement.toString(); - } - } - - public PreparedStatement prepareStatement() { - - Statement statement = buildStatement(true); - - if (statement instanceof RegularStatement) { - - RegularStatement regularStatement = (RegularStatement) statement; - - return sessionOps.prepare(regularStatement); - } - - throw new HelenusException("only RegularStatements can be prepared"); - } - - public ListenableFuture prepareStatementAsync() { - - Statement statement = buildStatement(true); - - if (statement instanceof RegularStatement) { - - RegularStatement regularStatement = (RegularStatement) statement; - - return sessionOps.prepareAsync(regularStatement); - } - - throw new HelenusException("only RegularStatements can be prepared"); - } - - protected E checkCache(UnitOfWork uow, List facets) { - E result = null; - Optional optionalCachedResult = Optional.empty(); - - if (!facets.isEmpty()) { - optionalCachedResult = uow.cacheLookup(facets); - if (optionalCachedResult.isPresent()) { - result = (E) optionalCachedResult.get(); - } - } - - return result; - } - - protected void cacheUpdate(UnitOfWork uow, E pojo, List identifyingFacets) { - List facets = new ArrayList<>(); - Map valueMap = pojo instanceof MapExportable ? ((MapExportable) pojo).toMap() : null; - - for (Facet facet : identifyingFacets) { - if (facet instanceof UnboundFacet) { - UnboundFacet unboundFacet = (UnboundFacet) facet; - UnboundFacet.Binder binder = unboundFacet.binder(); - for (HelenusProperty prop : unboundFacet.getProperties()) { - Object value; - if (valueMap == null) { - value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop, false); - if (value != null) { - binder.setValueForProperty(prop, value.toString()); - } - } else { - value = valueMap.get(prop.getPropertyName()); - if (value != null) { - binder.setValueForProperty(prop, value.toString()); - } - } - } - if (binder.isBound()) { - facets.add(binder.bind()); - } - } else { - facets.add(facet); - } - } - - // Cache the value (pojo), the statement key, and the fully bound facets. - uow.cacheUpdate(pojo, facets); - } +public abstract class AbstractStatementOperation> + extends Operation { + + protected boolean checkCache = true; + protected boolean showValues = true; + protected TraceContext traceContext; + long queryExecutionTimeout = 10; + TimeUnit queryTimeoutUnits = TimeUnit.SECONDS; + private ConsistencyLevel consistencyLevel; + private ConsistencyLevel serialConsistencyLevel; + private RetryPolicy retryPolicy; + private boolean idempotent = false; + private boolean enableTracing = false; + private long[] defaultTimestamp = null; + private int[] fetchSize = null; + + public AbstractStatementOperation(AbstractSessionOperations sessionOperations) { + super(sessionOperations); + this.consistencyLevel = sessionOperations.getDefaultConsistencyLevel(); + this.idempotent = sessionOperations.getDefaultQueryIdempotency(); + } + + public abstract Statement buildStatement(boolean cached); + + public O uncached(boolean enabled) { + checkCache = enabled; + return (O) this; + } + + public O uncached() { + checkCache = false; + return (O) this; + } + + public O showValues(boolean enabled) { + this.showValues = enabled; + return (O) this; + } + + public O defaultTimestamp(long timestamp) { + this.defaultTimestamp = new long[1]; + this.defaultTimestamp[0] = timestamp; + return (O) this; + } + + public O retryPolicy(RetryPolicy retryPolicy) { + this.retryPolicy = retryPolicy; + return (O) this; + } + + public O defaultRetryPolicy() { + this.retryPolicy = DefaultRetryPolicy.INSTANCE; + return (O) this; + } + + public O idempotent() { + this.idempotent = true; + return (O) this; + } + + public O isIdempotent(boolean idempotent) { + this.idempotent = idempotent; + return (O) this; + } + + public O downgradingConsistencyRetryPolicy() { + this.retryPolicy = DowngradingConsistencyRetryPolicy.INSTANCE; + return (O) this; + } + + public O fallthroughRetryPolicy() { + this.retryPolicy = FallthroughRetryPolicy.INSTANCE; + return (O) this; + } + + public O consistency(ConsistencyLevel level) { + this.consistencyLevel = level; + return (O) this; + } + + public O consistencyAny() { + this.consistencyLevel = ConsistencyLevel.ANY; + return (O) this; + } + + public O consistencyOne() { + this.consistencyLevel = ConsistencyLevel.ONE; + return (O) this; + } + + public O consistencyQuorum() { + this.consistencyLevel = ConsistencyLevel.QUORUM; + return (O) this; + } + + public O consistencyAll() { + this.consistencyLevel = ConsistencyLevel.ALL; + return (O) this; + } + + public O consistencyLocalOne() { + this.consistencyLevel = ConsistencyLevel.LOCAL_ONE; + return (O) this; + } + + public O consistencyLocalQuorum() { + this.consistencyLevel = ConsistencyLevel.LOCAL_QUORUM; + return (O) this; + } + + public O consistencyEachQuorum() { + this.consistencyLevel = ConsistencyLevel.EACH_QUORUM; + return (O) this; + } + + public O serialConsistency(ConsistencyLevel level) { + this.serialConsistencyLevel = level; + return (O) this; + } + + public O serialConsistencyAny() { + this.serialConsistencyLevel = ConsistencyLevel.ANY; + return (O) this; + } + + public O serialConsistencyOne() { + this.serialConsistencyLevel = ConsistencyLevel.ONE; + return (O) this; + } + + public O serialConsistencyQuorum() { + this.serialConsistencyLevel = ConsistencyLevel.QUORUM; + return (O) this; + } + + public O serialConsistencyAll() { + this.serialConsistencyLevel = ConsistencyLevel.ALL; + return (O) this; + } + + public O serialConsistencyLocal() { + this.serialConsistencyLevel = ConsistencyLevel.LOCAL_SERIAL; + return (O) this; + } + + public O serialConsistencyLocalQuorum() { + this.serialConsistencyLevel = ConsistencyLevel.LOCAL_QUORUM; + return (O) this; + } + + public O disableTracing() { + this.enableTracing = false; + return (O) this; + } + + public O enableTracing() { + this.enableTracing = true; + return (O) this; + } + + public O tracing(boolean enable) { + this.enableTracing = enable; + return (O) this; + } + + public O fetchSize(int fetchSize) { + this.fetchSize = new int[1]; + this.fetchSize[0] = fetchSize; + return (O) this; + } + + public O queryTimeoutMs(long ms) { + this.queryExecutionTimeout = ms; + this.queryTimeoutUnits = TimeUnit.MILLISECONDS; + return (O) this; + } + + public O queryTimeout(long timeout, TimeUnit units) { + this.queryExecutionTimeout = timeout; + this.queryTimeoutUnits = units; + return (O) this; + } + + public Statement options(Statement statement) { + + if (defaultTimestamp != null) { + statement.setDefaultTimestamp(defaultTimestamp[0]); + } + + if (consistencyLevel != null) { + statement.setConsistencyLevel(consistencyLevel); + } + + if (serialConsistencyLevel != null) { + statement.setSerialConsistencyLevel(serialConsistencyLevel); + } + + if (retryPolicy != null) { + statement.setRetryPolicy(retryPolicy); + } + + if (enableTracing) { + statement.enableTracing(); + } else { + statement.disableTracing(); + } + + if (fetchSize != null) { + statement.setFetchSize(fetchSize[0]); + } + + if (idempotent) { + statement.setIdempotent(true); + } + + return statement; + } + + public O zipkinContext(TraceContext traceContext) { + if (traceContext != null) { + Tracer tracer = this.sessionOps.getZipkinTracer(); + if (tracer != null) { + this.traceContext = traceContext; + } + } + + return (O) this; + } + + public Statement statement() { + return buildStatement(false); + } + + public String cql() { + Statement statement = buildStatement(false); + if (statement == null) return ""; + if (statement instanceof BuiltStatement) { + BuiltStatement buildStatement = (BuiltStatement) statement; + return buildStatement.setForceNoValues(true).getQueryString(); + } else { + return statement.toString(); + } + } + + public PreparedStatement prepareStatement() { + + Statement statement = buildStatement(true); + + if (statement instanceof RegularStatement) { + + RegularStatement regularStatement = (RegularStatement) statement; + + return sessionOps.prepare(regularStatement); + } + + throw new HelenusException("only RegularStatements can be prepared"); + } + + public ListenableFuture prepareStatementAsync() { + + Statement statement = buildStatement(true); + + if (statement instanceof RegularStatement) { + + RegularStatement regularStatement = (RegularStatement) statement; + + return sessionOps.prepareAsync(regularStatement); + } + + throw new HelenusException("only RegularStatements can be prepared"); + } + + protected E checkCache(UnitOfWork uow, List facets) { + E result = null; + Optional optionalCachedResult = Optional.empty(); + + if (!facets.isEmpty()) { + optionalCachedResult = uow.cacheLookup(facets); + if (optionalCachedResult.isPresent()) { + result = (E) optionalCachedResult.get(); + } + } + + return result; + } + + protected void cacheUpdate(UnitOfWork uow, E pojo, List identifyingFacets) { + List facets = new ArrayList<>(); + Map valueMap = + pojo instanceof MapExportable ? ((MapExportable) pojo).toMap() : null; + + for (Facet facet : identifyingFacets) { + if (facet instanceof UnboundFacet) { + UnboundFacet unboundFacet = (UnboundFacet) facet; + UnboundFacet.Binder binder = unboundFacet.binder(); + for (HelenusProperty prop : unboundFacet.getProperties()) { + Object value; + if (valueMap == null) { + value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop, false); + if (value != null) { + binder.setValueForProperty(prop, value.toString()); + } + } else { + value = valueMap.get(prop.getPropertyName()); + if (value != null) { + binder.setValueForProperty(prop, value.toString()); + } + } + } + if (binder.isBound()) { + facets.add(binder.bind()); + } + } else { + facets.add(facet); + } + } + + // Cache the value (pojo), the statement key, and the fully bound facets. + uow.cacheUpdate(pojo, facets); + } } diff --git a/src/main/java/net/helenus/core/operation/AbstractStreamOperation.java b/src/main/java/net/helenus/core/operation/AbstractStreamOperation.java index f7dd6aa..fe0978e 100644 --- a/src/main/java/net/helenus/core/operation/AbstractStreamOperation.java +++ b/src/main/java/net/helenus/core/operation/AbstractStreamOperation.java @@ -17,13 +17,6 @@ package net.helenus.core.operation; import static net.helenus.core.HelenusSession.deleted; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; -import java.util.concurrent.TimeoutException; -import java.util.stream.Stream; - import com.codahale.metrics.Timer; import com.datastax.driver.core.PreparedStatement; import com.datastax.driver.core.ResultSet; @@ -31,185 +24,206 @@ import com.google.common.base.Function; import com.google.common.base.Stopwatch; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; - +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.TimeoutException; +import java.util.stream.Stream; import net.helenus.core.AbstractSessionOperations; import net.helenus.core.UnitOfWork; import net.helenus.core.cache.CacheUtil; import net.helenus.core.cache.Facet; public abstract class AbstractStreamOperation> - extends - AbstractStatementOperation { + extends AbstractStatementOperation { - public AbstractStreamOperation(AbstractSessionOperations sessionOperations) { - super(sessionOperations); - } + public AbstractStreamOperation(AbstractSessionOperations sessionOperations) { + super(sessionOperations); + } - public abstract Stream transform(ResultSet resultSet); + public abstract Stream transform(ResultSet resultSet); - public PreparedStreamOperation prepare() { - return new PreparedStreamOperation(prepareStatement(), this); - } + public PreparedStreamOperation prepare() { + return new PreparedStreamOperation(prepareStatement(), this); + } - public ListenableFuture> prepareAsync() { - final O _this = (O) this; - return Futures.transform(prepareStatementAsync(), - new Function>() { - @Override - public PreparedStreamOperation apply(PreparedStatement preparedStatement) { - return new PreparedStreamOperation(preparedStatement, _this); - } - }); - } + public ListenableFuture> prepareAsync() { + final O _this = (O) this; + return Futures.transform( + prepareStatementAsync(), + new Function>() { + @Override + public PreparedStreamOperation apply(PreparedStatement preparedStatement) { + return new PreparedStreamOperation(preparedStatement, _this); + } + }); + } - public Stream sync() throws TimeoutException { - final Timer.Context context = requestLatency.time(); - try { - Stream resultStream = null; - E cacheResult = null; - boolean updateCache = isSessionCacheable(); + public Stream sync() throws TimeoutException { + final Timer.Context context = requestLatency.time(); + try { + Stream resultStream = null; + E cacheResult = null; + boolean updateCache = isSessionCacheable(); - if (checkCache && isSessionCacheable()) { - List facets = bindFacetValues(); - String tableName = CacheUtil.schemaName(facets); - cacheResult = (E) sessionOps.checkCache(tableName, facets); - if (cacheResult != null) { - resultStream = Stream.of(cacheResult); - updateCache = false; - sessionCacheHits.mark(); - cacheHits.mark(); - } else { - sessionCacheMiss.mark(); - cacheMiss.mark(); - } - } + if (checkCache && isSessionCacheable()) { + List facets = bindFacetValues(); + String tableName = CacheUtil.schemaName(facets); + cacheResult = (E) sessionOps.checkCache(tableName, facets); + if (cacheResult != null) { + resultStream = Stream.of(cacheResult); + updateCache = false; + sessionCacheHits.mark(); + cacheHits.mark(); + } else { + sessionCacheMiss.mark(); + cacheMiss.mark(); + } + } - if (resultStream == null) { - // Formulate the query and execute it against the Cassandra cluster. - ResultSet resultSet = this.execute(sessionOps, null, traceContext, queryExecutionTimeout, - queryTimeoutUnits, showValues, false); + if (resultStream == null) { + // Formulate the query and execute it against the Cassandra cluster. + ResultSet resultSet = + this.execute( + sessionOps, + null, + traceContext, + queryExecutionTimeout, + queryTimeoutUnits, + showValues, + false); - // Transform the query result set into the desired shape. - resultStream = transform(resultSet); - } + // Transform the query result set into the desired shape. + resultStream = transform(resultSet); + } - if (updateCache && resultStream != null) { - List facets = getFacets(); - if (facets != null && facets.size() > 1) { - List again = new ArrayList<>(); - resultStream.forEach(result -> { - sessionOps.updateCache(result, facets); - again.add(result); - }); - resultStream = again.stream(); - } - } - return resultStream; + if (updateCache && resultStream != null) { + List facets = getFacets(); + if (facets != null && facets.size() > 1) { + List again = new ArrayList<>(); + resultStream.forEach( + result -> { + sessionOps.updateCache(result, facets); + again.add(result); + }); + resultStream = again.stream(); + } + } + return resultStream; - } finally { - context.stop(); - } - } + } finally { + context.stop(); + } + } - public Stream sync(UnitOfWork uow) throws TimeoutException { - if (uow == null) - return sync(); + public Stream sync(UnitOfWork uow) throws TimeoutException { + if (uow == null) return sync(); - final Timer.Context context = requestLatency.time(); - try { - Stream resultStream = null; - E cachedResult = null; - final boolean updateCache; + final Timer.Context context = requestLatency.time(); + try { + Stream resultStream = null; + E cachedResult = null; + final boolean updateCache; - if (checkCache) { - Stopwatch timer = Stopwatch.createStarted(); - try { - List facets = bindFacetValues(); - 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 (checkCache) { + Stopwatch timer = Stopwatch.createStarted(); + try { + List facets = bindFacetValues(); + 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; + } - // Check to see if we fetched the object from the cache - if (resultStream == null) { - ResultSet resultSet = execute(sessionOps, uow, traceContext, queryExecutionTimeout, queryTimeoutUnits, - showValues, true); - resultStream = transform(resultSet); - } + // Check to see if we fetched the object from the cache + if (resultStream == null) { + ResultSet resultSet = + execute( + sessionOps, + uow, + traceContext, + queryExecutionTimeout, + queryTimeoutUnits, + showValues, + true); + resultStream = transform(resultSet); + } - // If we have a result and we're caching then we need to put it into the cache - // for future requests to find. - if (resultStream != null) { - List again = new ArrayList<>(); - List facets = getFacets(); - resultStream.forEach(result -> { - if (result != deleted) { - if (updateCache) { - cacheUpdate(uow, result, facets); - } - again.add(result); - } - }); - resultStream = again.stream(); - } + // If we have a result and we're caching then we need to put it into the cache + // for future requests to find. + if (resultStream != null) { + List again = new ArrayList<>(); + List facets = getFacets(); + resultStream.forEach( + result -> { + if (result != deleted) { + if (updateCache) { + cacheUpdate(uow, result, facets); + } + again.add(result); + } + }); + resultStream = again.stream(); + } - return resultStream; - } finally { - context.stop(); - } - } + return resultStream; + } finally { + context.stop(); + } + } - public CompletableFuture> async() { - return CompletableFuture.>supplyAsync(() -> { - try { - return sync(); - } catch (TimeoutException ex) { - throw new CompletionException(ex); - } - }); - } + public CompletableFuture> async() { + return CompletableFuture.>supplyAsync( + () -> { + try { + return sync(); + } catch (TimeoutException ex) { + throw new CompletionException(ex); + } + }); + } - public CompletableFuture> async(UnitOfWork uow) { - if (uow == null) - return async(); - return CompletableFuture.>supplyAsync(() -> { - try { - return sync(); - } catch (TimeoutException ex) { - throw new CompletionException(ex); - } - }); - } + public CompletableFuture> async(UnitOfWork uow) { + if (uow == null) return async(); + return CompletableFuture.>supplyAsync( + () -> { + try { + return sync(); + } catch (TimeoutException ex) { + throw new CompletionException(ex); + } + }); + } } diff --git a/src/main/java/net/helenus/core/operation/BoundOperation.java b/src/main/java/net/helenus/core/operation/BoundOperation.java index 002dbd6..887e582 100644 --- a/src/main/java/net/helenus/core/operation/BoundOperation.java +++ b/src/main/java/net/helenus/core/operation/BoundOperation.java @@ -21,27 +21,27 @@ import com.datastax.driver.core.Statement; public final class BoundOperation extends AbstractOperation> { - private final BoundStatement boundStatement; - private final AbstractOperation delegate; + private final BoundStatement boundStatement; + private final AbstractOperation delegate; - public BoundOperation(BoundStatement boundStatement, AbstractOperation operation) { - super(operation.sessionOps); - this.boundStatement = boundStatement; - this.delegate = operation; - } + public BoundOperation(BoundStatement boundStatement, AbstractOperation operation) { + super(operation.sessionOps); + this.boundStatement = boundStatement; + this.delegate = operation; + } - @Override - public E transform(ResultSet resultSet) { - return delegate.transform(resultSet); - } + @Override + public E transform(ResultSet resultSet) { + return delegate.transform(resultSet); + } - @Override - public Statement buildStatement(boolean cached) { - return boundStatement; - } + @Override + public Statement buildStatement(boolean cached) { + return boundStatement; + } - @Override - public boolean isSessionCacheable() { - return delegate.isSessionCacheable(); - } + @Override + public boolean isSessionCacheable() { + return delegate.isSessionCacheable(); + } } diff --git a/src/main/java/net/helenus/core/operation/BoundOptionalOperation.java b/src/main/java/net/helenus/core/operation/BoundOptionalOperation.java index 40eb2a0..896c850 100644 --- a/src/main/java/net/helenus/core/operation/BoundOptionalOperation.java +++ b/src/main/java/net/helenus/core/operation/BoundOptionalOperation.java @@ -15,35 +15,36 @@ */ package net.helenus.core.operation; -import java.util.Optional; - import com.datastax.driver.core.BoundStatement; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Statement; +import java.util.Optional; -public final class BoundOptionalOperation extends AbstractOptionalOperation> { +public final class BoundOptionalOperation + extends AbstractOptionalOperation> { - private final BoundStatement boundStatement; - private final AbstractOptionalOperation delegate; + private final BoundStatement boundStatement; + private final AbstractOptionalOperation delegate; - public BoundOptionalOperation(BoundStatement boundStatement, AbstractOptionalOperation operation) { - super(operation.sessionOps); - this.boundStatement = boundStatement; - this.delegate = operation; - } + public BoundOptionalOperation( + BoundStatement boundStatement, AbstractOptionalOperation operation) { + super(operation.sessionOps); + this.boundStatement = boundStatement; + this.delegate = operation; + } - @Override - public Optional transform(ResultSet resultSet) { - return delegate.transform(resultSet); - } + @Override + public Optional transform(ResultSet resultSet) { + return delegate.transform(resultSet); + } - @Override - public Statement buildStatement(boolean cached) { - return boundStatement; - } + @Override + public Statement buildStatement(boolean cached) { + return boundStatement; + } - @Override - public boolean isSessionCacheable() { - return delegate.isSessionCacheable(); - } + @Override + public boolean isSessionCacheable() { + return delegate.isSessionCacheable(); + } } diff --git a/src/main/java/net/helenus/core/operation/BoundStreamOperation.java b/src/main/java/net/helenus/core/operation/BoundStreamOperation.java index 7b7c087..7e0edde 100644 --- a/src/main/java/net/helenus/core/operation/BoundStreamOperation.java +++ b/src/main/java/net/helenus/core/operation/BoundStreamOperation.java @@ -15,43 +15,43 @@ */ package net.helenus.core.operation; -import java.util.List; -import java.util.stream.Stream; - import com.datastax.driver.core.BoundStatement; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Statement; - +import java.util.List; +import java.util.stream.Stream; import net.helenus.core.cache.Facet; -public final class BoundStreamOperation extends AbstractStreamOperation> { +public final class BoundStreamOperation + extends AbstractStreamOperation> { - private final BoundStatement boundStatement; - private final AbstractStreamOperation delegate; + private final BoundStatement boundStatement; + private final AbstractStreamOperation delegate; - public BoundStreamOperation(BoundStatement boundStatement, AbstractStreamOperation operation) { - super(operation.sessionOps); - this.boundStatement = boundStatement; - this.delegate = operation; - } + public BoundStreamOperation( + BoundStatement boundStatement, AbstractStreamOperation operation) { + super(operation.sessionOps); + this.boundStatement = boundStatement; + this.delegate = operation; + } - @Override - public List bindFacetValues() { - return delegate.bindFacetValues(); - } + @Override + public List bindFacetValues() { + return delegate.bindFacetValues(); + } - @Override - public Stream transform(ResultSet resultSet) { - return delegate.transform(resultSet); - } + @Override + public Stream transform(ResultSet resultSet) { + return delegate.transform(resultSet); + } - @Override - public Statement buildStatement(boolean cached) { - return boundStatement; - } + @Override + public Statement buildStatement(boolean cached) { + return boundStatement; + } - @Override - public boolean isSessionCacheable() { - return delegate.isSessionCacheable(); - } + @Override + public boolean isSessionCacheable() { + return delegate.isSessionCacheable(); + } } diff --git a/src/main/java/net/helenus/core/operation/CountOperation.java b/src/main/java/net/helenus/core/operation/CountOperation.java index 3631b93..b751cfb 100644 --- a/src/main/java/net/helenus/core/operation/CountOperation.java +++ b/src/main/java/net/helenus/core/operation/CountOperation.java @@ -20,7 +20,6 @@ import com.datastax.driver.core.querybuilder.BuiltStatement; import com.datastax.driver.core.querybuilder.QueryBuilder; import com.datastax.driver.core.querybuilder.Select; import com.datastax.driver.core.querybuilder.Select.Where; - import net.helenus.core.AbstractSessionOperations; import net.helenus.core.Filter; import net.helenus.core.reflect.HelenusPropertyNode; @@ -29,53 +28,56 @@ import net.helenus.support.HelenusMappingException; public final class CountOperation extends AbstractFilterOperation { - private HelenusEntity entity; + private HelenusEntity entity; - public CountOperation(AbstractSessionOperations sessionOperations) { - super(sessionOperations); - } + public CountOperation(AbstractSessionOperations sessionOperations) { + super(sessionOperations); + } - public CountOperation(AbstractSessionOperations sessionOperations, HelenusEntity entity) { - super(sessionOperations); - this.entity = entity; - } + public CountOperation(AbstractSessionOperations sessionOperations, HelenusEntity entity) { + super(sessionOperations); + this.entity = entity; + } - @Override - public BuiltStatement buildStatement(boolean cached) { + @Override + public BuiltStatement buildStatement(boolean cached) { - if (filters != null && !filters.isEmpty()) { - filters.forEach(f -> addPropertyNode(f.getNode())); - } + if (filters != null && !filters.isEmpty()) { + filters.forEach(f -> addPropertyNode(f.getNode())); + } - if (entity == null) { - throw new HelenusMappingException("unknown entity"); - } + if (entity == null) { + throw new HelenusMappingException("unknown entity"); + } - Select select = QueryBuilder.select().countAll().from(entity.getName().toCql()); + Select select = QueryBuilder.select().countAll().from(entity.getName().toCql()); - if (filters != null && !filters.isEmpty()) { + if (filters != null && !filters.isEmpty()) { - Where where = select.where(); + Where where = select.where(); - for (Filter filter : filters) { - where.and(filter.getClause(sessionOps.getValuePreparer())); - } - } + for (Filter filter : filters) { + where.and(filter.getClause(sessionOps.getValuePreparer())); + } + } - return select; - } + return select; + } - @Override - public Long transform(ResultSet resultSet) { - return resultSet.one().getLong(0); - } + @Override + public Long transform(ResultSet resultSet) { + return resultSet.one().getLong(0); + } - private void addPropertyNode(HelenusPropertyNode p) { - if (entity == null) { - entity = p.getEntity(); - } else if (entity != p.getEntity()) { - throw new HelenusMappingException("you can count columns only in single entity " - + entity.getMappingInterface() + " or " + p.getEntity().getMappingInterface()); - } - } + private void addPropertyNode(HelenusPropertyNode p) { + if (entity == null) { + entity = p.getEntity(); + } else if (entity != p.getEntity()) { + throw new HelenusMappingException( + "you can count columns only in single entity " + + entity.getMappingInterface() + + " or " + + p.getEntity().getMappingInterface()); + } + } } diff --git a/src/main/java/net/helenus/core/operation/DeleteOperation.java b/src/main/java/net/helenus/core/operation/DeleteOperation.java index 1280f45..8ce7d59 100644 --- a/src/main/java/net/helenus/core/operation/DeleteOperation.java +++ b/src/main/java/net/helenus/core/operation/DeleteOperation.java @@ -15,15 +15,13 @@ */ package net.helenus.core.operation; -import java.util.List; -import java.util.concurrent.TimeoutException; - import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.querybuilder.BuiltStatement; import com.datastax.driver.core.querybuilder.Delete; import com.datastax.driver.core.querybuilder.Delete.Where; import com.datastax.driver.core.querybuilder.QueryBuilder; - +import java.util.List; +import java.util.concurrent.TimeoutException; import net.helenus.core.AbstractSessionOperations; import net.helenus.core.Filter; import net.helenus.core.UnitOfWork; @@ -34,126 +32,128 @@ import net.helenus.support.HelenusMappingException; public final class DeleteOperation extends AbstractFilterOperation { - private HelenusEntity entity; + private HelenusEntity entity; - private boolean ifExists = false; + private boolean ifExists = false; - private int[] ttl; - private long[] timestamp; + private int[] ttl; + private long[] timestamp; - public DeleteOperation(AbstractSessionOperations sessionOperations) { - super(sessionOperations); - } + public DeleteOperation(AbstractSessionOperations sessionOperations) { + super(sessionOperations); + } - public DeleteOperation(AbstractSessionOperations sessionOperations, HelenusEntity entity) { - super(sessionOperations); + public DeleteOperation(AbstractSessionOperations sessionOperations, HelenusEntity entity) { + super(sessionOperations); - this.entity = entity; - } + this.entity = entity; + } - @Override - public BuiltStatement buildStatement(boolean cached) { + @Override + public BuiltStatement buildStatement(boolean cached) { - if (filters != null && !filters.isEmpty()) { - filters.forEach(f -> addPropertyNode(f.getNode())); - } + if (filters != null && !filters.isEmpty()) { + filters.forEach(f -> addPropertyNode(f.getNode())); + } - if (entity == null) { - throw new HelenusMappingException("unknown entity"); - } + if (entity == null) { + throw new HelenusMappingException("unknown entity"); + } - if (filters != null && !filters.isEmpty()) { + if (filters != null && !filters.isEmpty()) { - Delete delete = QueryBuilder.delete().from(entity.getName().toCql()); + Delete delete = QueryBuilder.delete().from(entity.getName().toCql()); - if (this.ifExists) { - delete.ifExists(); - } + if (this.ifExists) { + delete.ifExists(); + } - Where where = delete.where(); + Where where = delete.where(); - for (Filter filter : filters) { - where.and(filter.getClause(sessionOps.getValuePreparer())); - } + for (Filter filter : filters) { + where.and(filter.getClause(sessionOps.getValuePreparer())); + } - if (ifFilters != null && !ifFilters.isEmpty()) { + if (ifFilters != null && !ifFilters.isEmpty()) { - for (Filter filter : ifFilters) { - delete.onlyIf(filter.getClause(sessionOps.getValuePreparer())); - } - } + for (Filter filter : ifFilters) { + delete.onlyIf(filter.getClause(sessionOps.getValuePreparer())); + } + } - if (this.ttl != null) { - delete.using(QueryBuilder.ttl(this.ttl[0])); - } - if (this.timestamp != null) { - delete.using(QueryBuilder.timestamp(this.timestamp[0])); - } + if (this.ttl != null) { + delete.using(QueryBuilder.ttl(this.ttl[0])); + } + if (this.timestamp != null) { + delete.using(QueryBuilder.timestamp(this.timestamp[0])); + } - return delete; + return delete; - } else { - return QueryBuilder.truncate(entity.getName().toCql()); - } - } + } else { + return QueryBuilder.truncate(entity.getName().toCql()); + } + } - @Override - public ResultSet transform(ResultSet resultSet) { - return resultSet; - } + @Override + public ResultSet transform(ResultSet resultSet) { + return resultSet; + } - public DeleteOperation ifExists() { - this.ifExists = true; - return this; - } + public DeleteOperation ifExists() { + this.ifExists = true; + return this; + } - public DeleteOperation usingTtl(int ttl) { - this.ttl = new int[1]; - this.ttl[0] = ttl; - return this; - } + public DeleteOperation usingTtl(int ttl) { + this.ttl = new int[1]; + this.ttl[0] = ttl; + return this; + } - public DeleteOperation usingTimestamp(long timestamp) { - this.timestamp = new long[1]; - this.timestamp[0] = timestamp; - return this; - } + public DeleteOperation usingTimestamp(long timestamp) { + this.timestamp = new long[1]; + this.timestamp[0] = timestamp; + return this; + } - private void addPropertyNode(HelenusPropertyNode p) { - if (entity == null) { - entity = p.getEntity(); - } else if (entity != p.getEntity()) { - throw new HelenusMappingException("you can delete rows only in single entity " - + entity.getMappingInterface() + " or " + p.getEntity().getMappingInterface()); - } - } + private void addPropertyNode(HelenusPropertyNode p) { + if (entity == null) { + entity = p.getEntity(); + } else if (entity != p.getEntity()) { + throw new HelenusMappingException( + "you can delete rows only in single entity " + + entity.getMappingInterface() + + " or " + + p.getEntity().getMappingInterface()); + } + } - public List bindFacetValues() { - return bindFacetValues(getFacets()); - } + public List bindFacetValues() { + return bindFacetValues(getFacets()); + } - @Override - public ResultSet sync() throws TimeoutException { - ResultSet result = super.sync(); - if (entity.isCacheable()) { - sessionOps.cacheEvict(bindFacetValues()); - } - return result; - } + @Override + public ResultSet sync() throws TimeoutException { + ResultSet result = super.sync(); + if (entity.isCacheable()) { + sessionOps.cacheEvict(bindFacetValues()); + } + return result; + } - @Override - public ResultSet sync(UnitOfWork uow) throws TimeoutException { - if (uow == null) { - return sync(); - } - ResultSet result = super.sync(uow); - uow.cacheEvict(bindFacetValues()); - return result; - } - - @Override - public List getFacets() { - return entity.getFacets(); - } + @Override + public ResultSet sync(UnitOfWork uow) throws TimeoutException { + if (uow == null) { + return sync(); + } + ResultSet result = super.sync(uow); + uow.cacheEvict(bindFacetValues()); + return result; + } + @Override + public List getFacets() { + return entity.getFacets(); + } } diff --git a/src/main/java/net/helenus/core/operation/InsertOperation.java b/src/main/java/net/helenus/core/operation/InsertOperation.java index 91959ba..3d88eb7 100644 --- a/src/main/java/net/helenus/core/operation/InsertOperation.java +++ b/src/main/java/net/helenus/core/operation/InsertOperation.java @@ -15,20 +15,19 @@ */ package net.helenus.core.operation; -import java.util.*; -import java.util.concurrent.TimeoutException; -import java.util.function.Function; - import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.querybuilder.BuiltStatement; import com.datastax.driver.core.querybuilder.Insert; import com.datastax.driver.core.querybuilder.QueryBuilder; - +import java.util.*; +import java.util.concurrent.TimeoutException; +import java.util.function.Function; import net.helenus.core.AbstractSessionOperations; import net.helenus.core.Getter; import net.helenus.core.Helenus; import net.helenus.core.UnitOfWork; import net.helenus.core.cache.Facet; +import net.helenus.core.cache.UnboundFacet; import net.helenus.core.reflect.DefaultPrimitiveTypes; import net.helenus.core.reflect.Drafted; import net.helenus.core.reflect.HelenusPropertyNode; @@ -42,233 +41,282 @@ import net.helenus.support.HelenusMappingException; public final class InsertOperation extends AbstractOperation> { - private final List> values = new ArrayList>(); - private final T pojo; - private final Class resultType; - private HelenusEntity entity; - private boolean ifNotExists; + private final List> values = + new ArrayList>(); + private final T pojo; + private final Class resultType; + private HelenusEntity entity; + private boolean ifNotExists; - private int[] ttl; - private long[] timestamp; + private int[] ttl; + private long[] timestamp; - public InsertOperation(AbstractSessionOperations sessionOperations, boolean ifNotExists) { - super(sessionOperations); + public InsertOperation(AbstractSessionOperations sessionOperations, boolean ifNotExists) { + super(sessionOperations); - this.ifNotExists = ifNotExists; - this.pojo = null; - this.resultType = ResultSet.class; - } + this.ifNotExists = ifNotExists; + this.pojo = null; + this.resultType = ResultSet.class; + } - public InsertOperation(AbstractSessionOperations sessionOperations, Class resultType, boolean ifNotExists) { - super(sessionOperations); + public InsertOperation( + AbstractSessionOperations sessionOperations, Class resultType, boolean ifNotExists) { + super(sessionOperations); - this.ifNotExists = ifNotExists; - this.pojo = null; - this.resultType = resultType; - } + this.ifNotExists = ifNotExists; + this.pojo = null; + this.resultType = resultType; + } - public InsertOperation(AbstractSessionOperations sessionOperations, HelenusEntity entity, T pojo, - Set mutations, boolean ifNotExists) { - super(sessionOperations); + public InsertOperation( + AbstractSessionOperations sessionOperations, + HelenusEntity entity, + T pojo, + Set mutations, + boolean ifNotExists) { + super(sessionOperations); - this.entity = entity; - this.pojo = pojo; - this.ifNotExists = ifNotExists; - this.resultType = entity.getMappingInterface(); + this.entity = entity; + this.pojo = pojo; + this.ifNotExists = ifNotExists; + this.resultType = entity.getMappingInterface(); - Collection properties = entity.getOrderedProperties(); - Set keys = (mutations == null) ? null : mutations; + Collection properties = entity.getOrderedProperties(); + Set keys = (mutations == null) ? null : mutations; - for (HelenusProperty prop : properties) { - boolean addProp = false; + for (HelenusProperty prop : properties) { + boolean addProp = false; - switch (prop.getColumnType()) { - case PARTITION_KEY : - case CLUSTERING_COLUMN : - addProp = true; - break; - default : - addProp = (keys == null || keys.contains(prop.getPropertyName())); - } + switch (prop.getColumnType()) { + case PARTITION_KEY: + case CLUSTERING_COLUMN: + addProp = true; + break; + default: + addProp = (keys == null || keys.contains(prop.getPropertyName())); + } - if (addProp) { - Object value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop); - value = sessionOps.getValuePreparer().prepareColumnValue(value, prop); + if (addProp) { + Object value = BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop); + value = sessionOps.getValuePreparer().prepareColumnValue(value, prop); - if (value != null) { - HelenusPropertyNode node = new HelenusPropertyNode(prop, Optional.empty()); - values.add(Fun.Tuple2.of(node, value)); - } - } - } - } + if (value != null) { + HelenusPropertyNode node = new HelenusPropertyNode(prop, Optional.empty()); + values.add(Fun.Tuple2.of(node, value)); + } + } + } + } - public InsertOperation ifNotExists() { - this.ifNotExists = true; - return this; - } + public InsertOperation ifNotExists() { + this.ifNotExists = true; + return this; + } - public InsertOperation ifNotExists(boolean enable) { - this.ifNotExists = enable; - return this; - } + public InsertOperation ifNotExists(boolean enable) { + this.ifNotExists = enable; + return this; + } - public InsertOperation value(Getter getter, V val) { + public InsertOperation value(Getter getter, V val) { - Objects.requireNonNull(getter, "getter is empty"); + Objects.requireNonNull(getter, "getter is empty"); - if (val != null) { - HelenusPropertyNode node = MappingUtil.resolveMappingProperty(getter); - Object value = sessionOps.getValuePreparer().prepareColumnValue(val, node.getProperty()); + if (val != null) { + HelenusPropertyNode node = MappingUtil.resolveMappingProperty(getter); + Object value = sessionOps.getValuePreparer().prepareColumnValue(val, node.getProperty()); - if (value != null) { - values.add(Fun.Tuple2.of(node, value)); - } - } + if (value != null) { + values.add(Fun.Tuple2.of(node, value)); + } + } - return this; - } + return this; + } - @Override - public BuiltStatement buildStatement(boolean cached) { + @Override + public BuiltStatement buildStatement(boolean cached) { - values.forEach(t -> addPropertyNode(t._1)); + values.forEach(t -> addPropertyNode(t._1)); - if (values.isEmpty()) - return null; + if (values.isEmpty()) return null; - if (entity == null) { - throw new HelenusMappingException("unknown entity"); - } + if (entity == null) { + throw new HelenusMappingException("unknown entity"); + } - Insert insert = QueryBuilder.insertInto(entity.getName().toCql()); + Insert insert = QueryBuilder.insertInto(entity.getName().toCql()); - if (ifNotExists) { - insert.ifNotExists(); - } + if (ifNotExists) { + insert.ifNotExists(); + } - values.forEach(t -> { - insert.value(t._1.getColumnName(), t._2); - }); + values.forEach( + t -> { + insert.value(t._1.getColumnName(), t._2); + }); - if (this.ttl != null) { - insert.using(QueryBuilder.ttl(this.ttl[0])); - } - if (this.timestamp != null) { - insert.using(QueryBuilder.timestamp(this.timestamp[0])); - } + if (this.ttl != null) { + insert.using(QueryBuilder.ttl(this.ttl[0])); + } + if (this.timestamp != null) { + insert.using(QueryBuilder.timestamp(this.timestamp[0])); + } - return insert; - } + return insert; + } - @Override - public T transform(ResultSet resultSet) { - Class iface = entity.getMappingInterface(); - if (resultType == iface) { - if (values.size() > 0) { - boolean immutable = iface.isAssignableFrom(Drafted.class); - Collection properties = entity.getOrderedProperties(); - Map backingMap = new HashMap(properties.size()); + @Override + public T transform(ResultSet resultSet) { + if ((ifNotExists == true) && (resultSet.wasApplied() == false)) { + throw new HelenusException("Statement was not applied due to consistency constraints"); + } - // First, add all the inserted values into our new map. - values.forEach(t -> backingMap.put(t._1.getProperty().getPropertyName(), t._2)); + Class iface = entity.getMappingInterface(); + if (resultType == iface) { + if (values.size() > 0) { + boolean immutable = iface.isAssignableFrom(Drafted.class); + Collection properties = entity.getOrderedProperties(); + Map backingMap = new HashMap(properties.size()); - // Then, fill in all the rest of the properties. - for (HelenusProperty prop : properties) { - String key = prop.getPropertyName(); - if (backingMap.containsKey(key)) { - // Some values man need to be converted (e.g. from String to Enum). This is done - // within the BeanColumnValueProvider below. - Optional> converter = prop - .getReadConverter(sessionOps.getSessionRepository()); - if (converter.isPresent()) { - backingMap.put(key, converter.get().apply(backingMap.get(key))); - } - } else { - // If we started this operation with an instance of this type, use values from - // that. - if (pojo != null) { - backingMap.put(key, - BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop, immutable)); - } else { - // Otherwise we'll use default values for the property type if available. - Class propType = prop.getJavaType(); - if (propType.isPrimitive()) { - DefaultPrimitiveTypes type = DefaultPrimitiveTypes.lookup(propType); - if (type == null) { - throw new HelenusException("unknown primitive type " + propType); - } - backingMap.put(key, type.getDefaultValue()); - } - } - } - } + // First, add all the inserted values into our new map. + values.forEach(t -> backingMap.put(t._1.getProperty().getPropertyName(), t._2)); - // Lastly, create a new proxy object for the entity and return the new instance. - return (T) Helenus.map(iface, backingMap); - } - // Oddly, this insert didn't change any value so simply return the pojo. - // TODO(gburd): this pojo is the result of a Draft.build() call which will not - // preserve object identity (o1 == o2), ... fix me. - return (T) pojo; - } - return (T) resultSet; - } + // Then, fill in all the rest of the properties. + for (HelenusProperty prop : properties) { + String key = prop.getPropertyName(); + if (backingMap.containsKey(key)) { + // Some values man need to be converted (e.g. from String to Enum). This is done + // within the BeanColumnValueProvider below. + Optional> converter = + prop.getReadConverter(sessionOps.getSessionRepository()); + if (converter.isPresent()) { + backingMap.put(key, converter.get().apply(backingMap.get(key))); + } + } else { + // If we started this operation with an instance of this type, use values from + // that. + if (pojo != null) { + backingMap.put( + key, BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop, immutable)); + } else { + // Otherwise we'll use default values for the property type if available. + Class propType = prop.getJavaType(); + if (propType.isPrimitive()) { + DefaultPrimitiveTypes type = DefaultPrimitiveTypes.lookup(propType); + if (type == null) { + throw new HelenusException("unknown primitive type " + propType); + } + backingMap.put(key, type.getDefaultValue()); + } + } + } + } - public InsertOperation usingTtl(int ttl) { - this.ttl = new int[1]; - this.ttl[0] = ttl; - return this; - } + // Lastly, create a new proxy object for the entity and return the new instance. + return (T) Helenus.map(iface, backingMap); + } + // Oddly, this insert didn't change anything so simply return the pojo. + return (T) pojo; + } + return (T) resultSet; + } - public InsertOperation usingTimestamp(long timestamp) { - this.timestamp = new long[1]; - this.timestamp[0] = timestamp; - return this; - } + public InsertOperation usingTtl(int ttl) { + this.ttl = new int[1]; + this.ttl[0] = ttl; + return this; + } - private void addPropertyNode(HelenusPropertyNode p) { - if (entity == null) { - entity = p.getEntity(); - } else if (entity != p.getEntity()) { - throw new HelenusMappingException("you can insert only single entity " + entity.getMappingInterface() - + " or " + p.getEntity().getMappingInterface()); - } - } + public InsertOperation usingTimestamp(long timestamp) { + this.timestamp = new long[1]; + this.timestamp[0] = timestamp; + return this; + } - @Override - public T sync() throws TimeoutException { - T result = super.sync(); - if (entity.isCacheable() && result != null) { - sessionOps.updateCache(result, entity.getFacets()); - } - return result; - } + private void addPropertyNode(HelenusPropertyNode p) { + if (entity == null) { + entity = p.getEntity(); + } else if (entity != p.getEntity()) { + throw new HelenusMappingException( + "you can insert only single entity " + + entity.getMappingInterface() + + " or " + + p.getEntity().getMappingInterface()); + } + } - @Override - public T sync(UnitOfWork uow) throws TimeoutException { - if (uow == null) { - return sync(); - } - T result = super.sync(uow); - Class iface = entity.getMappingInterface(); - if (resultType == iface) { - cacheUpdate(uow, result, entity.getFacets()); - } else { - if (entity.isCacheable()) { - sessionOps.cacheEvict(bindFacetValues()); - } - } - return result; - } + @Override + public T sync() throws TimeoutException { + T result = super.sync(); + if (entity.isCacheable() && result != null) { + sessionOps.updateCache(result, entity.getFacets()); + } + return result; + } - @Override - public List getFacets() { - if (entity != null) { - return entity.getFacets(); - } else { - return new ArrayList(); - } - } + @Override + public T sync(UnitOfWork uow) throws TimeoutException { + if (uow == null) { + return sync(); + } + T result = super.sync(uow); + if (result != null && pojo != null && !(pojo == result) && pojo.equals(result)) { + // To preserve object identity we need to find this object in cache + // because it was unchanged by the INSERT but pojo in this case was + // the result of a draft.build(). + T cachedValue = (T) uow.cacheLookup(bindFacetValues()); + if (cachedValue != null) { + result = cachedValue; + } + } + Class iface = entity.getMappingInterface(); + if (resultType == iface) { + cacheUpdate(uow, result, entity.getFacets()); + } else { + if (entity.isCacheable()) { + sessionOps.cacheEvict(bindFacetValues()); + } + } + return result; + } + @Override + public List bindFacetValues() { + List facets = getFacets(); + if (facets == null || facets.size() == 0) { + return new ArrayList(); + } + List boundFacets = new ArrayList<>(); + Map valuesMap = new HashMap<>(values.size()); + values.forEach(t -> valuesMap.put(t._1.getProperty(), t._2)); + + for (Facet facet : facets) { + if (facet instanceof UnboundFacet) { + UnboundFacet unboundFacet = (UnboundFacet) facet; + UnboundFacet.Binder binder = unboundFacet.binder(); + for (HelenusProperty prop : unboundFacet.getProperties()) { + Object value = valuesMap.get(prop); + if (value != null) { + binder.setValueForProperty(prop, value.toString()); + } + } + if (binder.isBound()) { + boundFacets.add(binder.bind()); + } + } else { + boundFacets.add(facet); + } + } + return boundFacets; + } + + @Override + public List getFacets() { + if (entity != null) { + return entity.getFacets(); + } else { + return new ArrayList(); + } + } } diff --git a/src/main/java/net/helenus/core/operation/Operation.java b/src/main/java/net/helenus/core/operation/Operation.java index 00262bb..8618293 100644 --- a/src/main/java/net/helenus/core/operation/Operation.java +++ b/src/main/java/net/helenus/core/operation/Operation.java @@ -15,153 +15,190 @@ */ package net.helenus.core.operation; +import brave.Span; +import brave.Tracer; +import brave.propagation.TraceContext; +import com.codahale.metrics.Meter; +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Timer; +import com.datastax.driver.core.*; +import com.datastax.driver.core.querybuilder.BuiltStatement; +import com.google.common.base.Stopwatch; +import java.net.InetAddress; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.codahale.metrics.Meter; -import com.codahale.metrics.MetricRegistry; -import com.codahale.metrics.Timer; -import com.datastax.driver.core.RegularStatement; -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.ResultSetFuture; -import com.datastax.driver.core.Statement; -import com.datastax.driver.core.querybuilder.BuiltStatement; -import com.google.common.base.Stopwatch; - -import brave.Span; -import brave.Tracer; -import brave.propagation.TraceContext; +import java.util.stream.Collectors; import net.helenus.core.AbstractSessionOperations; import net.helenus.core.UnitOfWork; import net.helenus.core.cache.Facet; +import net.helenus.support.HelenusException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public abstract class Operation { - private static final Logger LOG = LoggerFactory.getLogger(Operation.class); + private static final Logger LOG = LoggerFactory.getLogger(Operation.class); - protected final AbstractSessionOperations sessionOps; - protected final Meter uowCacheHits; - protected final Meter uowCacheMiss; - protected final Meter sessionCacheHits; - protected final Meter sessionCacheMiss; - protected final Meter cacheHits; - protected final Meter cacheMiss; - protected final Timer requestLatency; + protected final AbstractSessionOperations sessionOps; + protected final Meter uowCacheHits; + protected final Meter uowCacheMiss; + protected final Meter sessionCacheHits; + protected final Meter sessionCacheMiss; + protected final Meter cacheHits; + protected final Meter cacheMiss; + protected final Timer requestLatency; - Operation(AbstractSessionOperations sessionOperations) { - 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"); - } + Operation(AbstractSessionOperations sessionOperations) { + 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 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(); + 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; + } - } - return query; - } + public ResultSet execute( + AbstractSessionOperations session, + UnitOfWork uow, + TraceContext traceContext, + long timeout, + TimeUnit units, + boolean showValues, + boolean cached) + throws TimeoutException { - public ResultSet execute(AbstractSessionOperations session, UnitOfWork uow, TraceContext traceContext, long timeout, - TimeUnit units, boolean showValues, boolean cached) throws TimeoutException { + // Start recording in a Zipkin sub-span our execution time to perform this + // operation. + Tracer tracer = session.getZipkinTracer(); + Span span = null; + if (tracer != null && traceContext != null) { + span = tracer.newChild(traceContext); + } - // Start recording in a Zipkin sub-span our execution time to perform this - // operation. - Tracer tracer = session.getZipkinTracer(); - Span span = null; - if (tracer != null && traceContext != null) { - span = tracer.newChild(traceContext); - } + try { - try { + if (span != null) { + span.name("cassandra"); + span.start(); + } - if (span != null) { - span.name("cassandra"); - span.start(); - } + Statement statement = options(buildStatement(cached)); + Stopwatch timer = Stopwatch.createStarted(); + try { + ResultSetFuture futureResultSet = session.executeAsync(statement, uow, timer, showValues); + if (uow != null) uow.recordCacheAndDatabaseOperationCount(0, 1); + ResultSet resultSet = futureResultSet.getUninterruptibly(timeout, units); + ColumnDefinitions columnDefinitions = resultSet.getColumnDefinitions(); + if (LOG.isDebugEnabled()) { + ExecutionInfo ei = resultSet.getExecutionInfo(); + Host qh = ei.getQueriedHost(); + String oh = + ei.getTriedHosts() + .stream() + .map(Host::getAddress) + .map(InetAddress::toString) + .collect(Collectors.joining(", ")); + ConsistencyLevel cl = ei.getAchievedConsistencyLevel(); + int se = ei.getSpeculativeExecutions(); + String warn = ei.getWarnings().stream().collect(Collectors.joining(", ")); + String ri = + String.format( + "%s %s %s %s %s %s%sspec-retries: %d", + "server v" + qh.getCassandraVersion(), + qh.getAddress().toString(), + (oh != null && !oh.equals("")) ? " [tried: " + oh + "]" : "", + qh.getDatacenter(), + qh.getRack(), + (cl != null) + ? (" consistency: " + + cl.name() + + (cl.isDCLocal() ? " DC " : "") + + (cl.isSerial() ? " SC " : "")) + : "", + (warn != null && !warn.equals("")) ? ": " + warn : "", + se); + if (uow != null) uow.setInfo(ri); + else LOG.debug(ri); + } + if (!resultSet.wasApplied() + && !(columnDefinitions.size() > 1 || !columnDefinitions.contains("[applied]"))) { + throw new HelenusException("Operation Failed"); + } + return resultSet; - Statement statement = options(buildStatement(cached)); - Stopwatch timer = Stopwatch.createStarted(); - try { - ResultSetFuture futureResultSet = session.executeAsync(statement, uow, timer, showValues); - if (uow != null) - uow.recordCacheAndDatabaseOperationCount(0, 1); - ResultSet resultSet = futureResultSet.getUninterruptibly(timeout, units); - return resultSet; + } finally { + timer.stop(); + if (uow != null) uow.addDatabaseTime("Cassandra", timer); + log(statement, uow, timer, showValues); + } - } finally { - timer.stop(); - if (uow != null) - uow.addDatabaseTime("Cassandra", timer); - log(statement, uow, timer, showValues); - } + } finally { - } finally { + if (span != null) { + span.finish(); + } + } + } - if (span != null) { - span.finish(); - } - } - } + 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))); + } + } - 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; + } - public Statement options(Statement statement) { - return statement; - } + public Statement buildStatement(boolean cached) { + return null; + } - public Statement buildStatement(boolean cached) { - return null; - } + public List getFacets() { + return new ArrayList(); + } - public List getFacets() { - return new ArrayList(); - } - - public List bindFacetValues() { - return null; - } - - public boolean isSessionCacheable() { - return false; - } + public List bindFacetValues() { + return null; + } + public boolean isSessionCacheable() { + return false; + } } diff --git a/src/main/java/net/helenus/core/operation/PreparedOperation.java b/src/main/java/net/helenus/core/operation/PreparedOperation.java index 43a56c2..baba8df 100644 --- a/src/main/java/net/helenus/core/operation/PreparedOperation.java +++ b/src/main/java/net/helenus/core/operation/PreparedOperation.java @@ -20,28 +20,27 @@ import com.datastax.driver.core.PreparedStatement; public final class PreparedOperation { - private final PreparedStatement preparedStatement; - private final AbstractOperation operation; + private final PreparedStatement preparedStatement; + private final AbstractOperation operation; - public PreparedOperation(PreparedStatement statement, AbstractOperation operation) { - this.preparedStatement = statement; - this.operation = operation; - } + public PreparedOperation(PreparedStatement statement, AbstractOperation operation) { + this.preparedStatement = statement; + this.operation = operation; + } - public PreparedStatement getPreparedStatement() { - return preparedStatement; - } + public PreparedStatement getPreparedStatement() { + return preparedStatement; + } - public BoundOperation bind(Object... params) { + public BoundOperation bind(Object... params) { - BoundStatement boundStatement = preparedStatement.bind(params); + BoundStatement boundStatement = preparedStatement.bind(params); - return new BoundOperation(boundStatement, operation); - } - - @Override - public String toString() { - return preparedStatement.getQueryString(); - } + return new BoundOperation(boundStatement, operation); + } + @Override + public String toString() { + return preparedStatement.getQueryString(); + } } diff --git a/src/main/java/net/helenus/core/operation/PreparedOptionalOperation.java b/src/main/java/net/helenus/core/operation/PreparedOptionalOperation.java index 31feb2b..2ee7e29 100644 --- a/src/main/java/net/helenus/core/operation/PreparedOptionalOperation.java +++ b/src/main/java/net/helenus/core/operation/PreparedOptionalOperation.java @@ -20,27 +20,28 @@ import com.datastax.driver.core.PreparedStatement; public final class PreparedOptionalOperation { - private final PreparedStatement preparedStatement; - private final AbstractOptionalOperation operation; + private final PreparedStatement preparedStatement; + private final AbstractOptionalOperation operation; - public PreparedOptionalOperation(PreparedStatement statement, AbstractOptionalOperation operation) { - this.preparedStatement = statement; - this.operation = operation; - } + public PreparedOptionalOperation( + PreparedStatement statement, AbstractOptionalOperation operation) { + this.preparedStatement = statement; + this.operation = operation; + } - public PreparedStatement getPreparedStatement() { - return preparedStatement; - } + public PreparedStatement getPreparedStatement() { + return preparedStatement; + } - public BoundOptionalOperation bind(Object... params) { + public BoundOptionalOperation bind(Object... params) { - BoundStatement boundStatement = preparedStatement.bind(params); + BoundStatement boundStatement = preparedStatement.bind(params); - return new BoundOptionalOperation(boundStatement, operation); - } + return new BoundOptionalOperation(boundStatement, operation); + } - @Override - public String toString() { - return preparedStatement.getQueryString(); - } + @Override + public String toString() { + return preparedStatement.getQueryString(); + } } diff --git a/src/main/java/net/helenus/core/operation/PreparedStreamOperation.java b/src/main/java/net/helenus/core/operation/PreparedStreamOperation.java index 132e39c..cd0f6be 100644 --- a/src/main/java/net/helenus/core/operation/PreparedStreamOperation.java +++ b/src/main/java/net/helenus/core/operation/PreparedStreamOperation.java @@ -20,25 +20,26 @@ import com.datastax.driver.core.PreparedStatement; public final class PreparedStreamOperation { - private final PreparedStatement preparedStatement; - private final AbstractStreamOperation operation; + private final PreparedStatement preparedStatement; + private final AbstractStreamOperation operation; - public PreparedStreamOperation(PreparedStatement statement, AbstractStreamOperation operation) { - this.preparedStatement = statement; - this.operation = operation; - } + public PreparedStreamOperation( + PreparedStatement statement, AbstractStreamOperation operation) { + this.preparedStatement = statement; + this.operation = operation; + } - public PreparedStatement getPreparedStatement() { - return preparedStatement; - } + public PreparedStatement getPreparedStatement() { + return preparedStatement; + } - public BoundStreamOperation bind(Object... params) { - BoundStatement boundStatement = preparedStatement.bind(params); - return new BoundStreamOperation(boundStatement, operation); - } + public BoundStreamOperation bind(Object... params) { + BoundStatement boundStatement = preparedStatement.bind(params); + return new BoundStreamOperation(boundStatement, operation); + } - @Override - public String toString() { - return preparedStatement.getQueryString(); - } + @Override + public String toString() { + return preparedStatement.getQueryString(); + } } diff --git a/src/main/java/net/helenus/core/operation/SelectFirstOperation.java b/src/main/java/net/helenus/core/operation/SelectFirstOperation.java index 15a25a6..f5d28eb 100644 --- a/src/main/java/net/helenus/core/operation/SelectFirstOperation.java +++ b/src/main/java/net/helenus/core/operation/SelectFirstOperation.java @@ -15,53 +15,52 @@ */ package net.helenus.core.operation; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.querybuilder.BuiltStatement; import java.util.List; import java.util.Optional; import java.util.function.Function; - -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.querybuilder.BuiltStatement; - import net.helenus.core.cache.Facet; -public final class SelectFirstOperation extends AbstractFilterOptionalOperation> { +public final class SelectFirstOperation + extends AbstractFilterOptionalOperation> { - private final SelectOperation delegate; + private final SelectOperation delegate; - public SelectFirstOperation(SelectOperation delegate) { - super(delegate.sessionOps); + public SelectFirstOperation(SelectOperation delegate) { + super(delegate.sessionOps); - this.delegate = delegate; - this.filters = delegate.filters; - this.ifFilters = delegate.ifFilters; - } + this.delegate = delegate; + this.filters = delegate.filters; + this.ifFilters = delegate.ifFilters; + } - public SelectFirstTransformingOperation map(Function fn) { - return new SelectFirstTransformingOperation(delegate, fn); - } + public SelectFirstTransformingOperation map(Function fn) { + return new SelectFirstTransformingOperation(delegate, fn); + } - @Override - public BuiltStatement buildStatement(boolean cached) { - return delegate.buildStatement(cached); - } + @Override + public BuiltStatement buildStatement(boolean cached) { + return delegate.buildStatement(cached); + } - @Override - public List getFacets() { - return delegate.getFacets(); - } + @Override + public List getFacets() { + return delegate.getFacets(); + } - @Override - public List bindFacetValues() { - return delegate.bindFacetValues(); - } + @Override + public List bindFacetValues() { + return delegate.bindFacetValues(); + } - @Override - public Optional transform(ResultSet resultSet) { - return delegate.transform(resultSet).findFirst(); - } + @Override + public Optional transform(ResultSet resultSet) { + return delegate.transform(resultSet).findFirst(); + } - @Override - public boolean isSessionCacheable() { - return delegate.isSessionCacheable(); - } + @Override + public boolean isSessionCacheable() { + return delegate.isSessionCacheable(); + } } diff --git a/src/main/java/net/helenus/core/operation/SelectFirstTransformingOperation.java b/src/main/java/net/helenus/core/operation/SelectFirstTransformingOperation.java index 727d663..07325ba 100644 --- a/src/main/java/net/helenus/core/operation/SelectFirstTransformingOperation.java +++ b/src/main/java/net/helenus/core/operation/SelectFirstTransformingOperation.java @@ -15,48 +15,45 @@ */ package net.helenus.core.operation; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.querybuilder.BuiltStatement; import java.util.List; import java.util.Optional; import java.util.function.Function; - -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.querybuilder.BuiltStatement; - import net.helenus.core.cache.Facet; public final class SelectFirstTransformingOperation - extends - AbstractFilterOptionalOperation> { + extends AbstractFilterOptionalOperation> { - private final SelectOperation delegate; - private final Function fn; + private final SelectOperation delegate; + private final Function fn; - public SelectFirstTransformingOperation(SelectOperation delegate, Function fn) { - super(delegate.sessionOps); + public SelectFirstTransformingOperation(SelectOperation delegate, Function fn) { + super(delegate.sessionOps); - this.delegate = delegate; - this.fn = fn; - this.filters = delegate.filters; - this.ifFilters = delegate.ifFilters; - } + this.delegate = delegate; + this.fn = fn; + this.filters = delegate.filters; + this.ifFilters = delegate.ifFilters; + } - @Override - public List bindFacetValues() { - return delegate.bindFacetValues(); - } + @Override + public List bindFacetValues() { + return delegate.bindFacetValues(); + } - @Override - public BuiltStatement buildStatement(boolean cached) { - return delegate.buildStatement(cached); - } + @Override + public BuiltStatement buildStatement(boolean cached) { + return delegate.buildStatement(cached); + } - @Override - public Optional transform(ResultSet resultSet) { - return delegate.transform(resultSet).findFirst().map(fn); - } + @Override + public Optional transform(ResultSet resultSet) { + return delegate.transform(resultSet).findFirst().map(fn); + } - @Override - public boolean isSessionCacheable() { - return delegate.isSessionCacheable(); - } + @Override + public boolean isSessionCacheable() { + return delegate.isSessionCacheable(); + } } diff --git a/src/main/java/net/helenus/core/operation/SelectOperation.java b/src/main/java/net/helenus/core/operation/SelectOperation.java index 497868b..b70c595 100644 --- a/src/main/java/net/helenus/core/operation/SelectOperation.java +++ b/src/main/java/net/helenus/core/operation/SelectOperation.java @@ -15,14 +15,6 @@ */ package net.helenus.core.operation; -import java.util.*; -import java.util.function.Function; -import java.util.stream.Stream; -import java.util.stream.StreamSupport; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; import com.datastax.driver.core.querybuilder.BuiltStatement; @@ -32,7 +24,10 @@ import com.datastax.driver.core.querybuilder.Select; import com.datastax.driver.core.querybuilder.Select.Selection; import com.datastax.driver.core.querybuilder.Select.Where; import com.google.common.collect.Iterables; - +import java.util.*; +import java.util.function.Function; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; import net.helenus.core.*; import net.helenus.core.cache.Facet; import net.helenus.core.cache.UnboundFacet; @@ -45,285 +40,326 @@ import net.helenus.mapping.value.ColumnValueProvider; import net.helenus.mapping.value.ValueProviderMap; import net.helenus.support.Fun; import net.helenus.support.HelenusMappingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public final class SelectOperation extends AbstractFilterStreamOperation> { - private static final Logger LOG = LoggerFactory.getLogger(SelectOperation.class); + private static final Logger LOG = LoggerFactory.getLogger(SelectOperation.class); - protected final List props = new ArrayList(); - protected Function rowMapper = null; - protected List ordering = null; - protected Integer limit = null; - protected boolean allowFiltering = false; - protected String alternateTableName = null; - protected boolean isCacheable = false; + protected final List props = new ArrayList(); + protected Function rowMapper = null; + protected List ordering = null; + protected Integer limit = null; + protected boolean allowFiltering = false; + protected String alternateTableName = null; + protected boolean isCacheable = false; - @SuppressWarnings("unchecked") - public SelectOperation(AbstractSessionOperations sessionOperations) { - super(sessionOperations); + @SuppressWarnings("unchecked") + public SelectOperation(AbstractSessionOperations sessionOperations) { + super(sessionOperations); - this.rowMapper = new Function() { + this.rowMapper = + new Function() { - @Override - public E apply(Row source) { + @Override + public E apply(Row source) { - ColumnValueProvider valueProvider = sessionOps.getValueProvider(); - Object[] arr = new Object[props.size()]; + ColumnValueProvider valueProvider = sessionOps.getValueProvider(); + Object[] arr = new Object[props.size()]; - int i = 0; - for (HelenusPropertyNode p : props) { - Object value = valueProvider.getColumnValue(source, -1, p.getProperty()); - arr[i++] = value; - } + int i = 0; + for (HelenusPropertyNode p : props) { + Object value = valueProvider.getColumnValue(source, -1, p.getProperty()); + arr[i++] = value; + } - return (E) Fun.ArrayTuple.of(arr); - } - }; - } + return (E) Fun.ArrayTuple.of(arr); + } + }; + } - public SelectOperation(AbstractSessionOperations sessionOperations, HelenusEntity entity) { + public SelectOperation(AbstractSessionOperations sessionOperations, HelenusEntity entity) { - super(sessionOperations); + super(sessionOperations); - entity.getOrderedProperties().stream().map(p -> new HelenusPropertyNode(p, Optional.empty())) - .forEach(p -> this.props.add(p)); + entity + .getOrderedProperties() + .stream() + .map(p -> new HelenusPropertyNode(p, Optional.empty())) + .forEach(p -> this.props.add(p)); - isCacheable = entity.isCacheable(); - } + isCacheable = entity.isCacheable(); + } - public SelectOperation(AbstractSessionOperations sessionOperations, HelenusEntity entity, - Function rowMapper) { + public SelectOperation( + AbstractSessionOperations sessionOperations, + HelenusEntity entity, + Function rowMapper) { - super(sessionOperations); - this.rowMapper = rowMapper; + super(sessionOperations); + this.rowMapper = rowMapper; - entity.getOrderedProperties().stream().map(p -> new HelenusPropertyNode(p, Optional.empty())) - .forEach(p -> this.props.add(p)); + entity + .getOrderedProperties() + .stream() + .map(p -> new HelenusPropertyNode(p, Optional.empty())) + .forEach(p -> this.props.add(p)); - isCacheable = entity.isCacheable(); - } + isCacheable = entity.isCacheable(); + } - public SelectOperation(AbstractSessionOperations sessionOperations, Function rowMapper, - HelenusPropertyNode... props) { + public SelectOperation( + AbstractSessionOperations sessionOperations, + Function rowMapper, + HelenusPropertyNode... props) { - super(sessionOperations); + super(sessionOperations); - this.rowMapper = rowMapper; - Collections.addAll(this.props, props); - } + this.rowMapper = rowMapper; + Collections.addAll(this.props, props); + } - public CountOperation count() { + public CountOperation count() { - HelenusEntity entity = null; - for (HelenusPropertyNode prop : props) { + HelenusEntity entity = null; + for (HelenusPropertyNode prop : props) { - if (entity == null) { - entity = prop.getEntity(); - } else if (entity != prop.getEntity()) { - throw new HelenusMappingException("you can count records only from a single entity " - + entity.getMappingInterface() + " or " + prop.getEntity().getMappingInterface()); - } - } + if (entity == null) { + entity = prop.getEntity(); + } else if (entity != prop.getEntity()) { + throw new HelenusMappingException( + "you can count records only from a single entity " + + entity.getMappingInterface() + + " or " + + prop.getEntity().getMappingInterface()); + } + } - return new CountOperation(sessionOps, entity); - } + return new CountOperation(sessionOps, entity); + } - public SelectOperation from(Class materializedViewClass) { - Objects.requireNonNull(materializedViewClass); - HelenusEntity entity = Helenus.entity(materializedViewClass); - this.alternateTableName = entity.getName().toCql(); - this.props.clear(); - entity.getOrderedProperties().stream().map(p -> new HelenusPropertyNode(p, Optional.empty())) - .forEach(p -> this.props.add(p)); - return this; - } + public SelectOperation from(Class materializedViewClass) { + Objects.requireNonNull(materializedViewClass); + HelenusEntity entity = Helenus.entity(materializedViewClass); + this.alternateTableName = entity.getName().toCql(); + this.props.clear(); + entity + .getOrderedProperties() + .stream() + .map(p -> new HelenusPropertyNode(p, Optional.empty())) + .forEach(p -> this.props.add(p)); + return this; + } - public SelectFirstOperation single() { - limit(1); - return new SelectFirstOperation(this); - } + public SelectFirstOperation single() { + limit(1); + return new SelectFirstOperation(this); + } - public SelectTransformingOperation mapTo(Class entityClass) { + public SelectTransformingOperation mapTo(Class entityClass) { - Objects.requireNonNull(entityClass, "entityClass is null"); + Objects.requireNonNull(entityClass, "entityClass is null"); - HelenusEntity entity = Helenus.entity(entityClass); + HelenusEntity entity = Helenus.entity(entityClass); - this.rowMapper = null; + this.rowMapper = null; - return new SelectTransformingOperation(this, (r) -> { - Map map = new ValueProviderMap(r, sessionOps.getValueProvider(), entity); - return (R) Helenus.map(entityClass, map); - }); - } + return new SelectTransformingOperation( + this, + (r) -> { + Map map = new ValueProviderMap(r, sessionOps.getValueProvider(), entity); + return (R) Helenus.map(entityClass, map); + }); + } - public SelectTransformingOperation map(Function fn) { - return new SelectTransformingOperation(this, fn); - } + public SelectTransformingOperation map(Function fn) { + return new SelectTransformingOperation(this, fn); + } - public SelectOperation column(Getter getter) { - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(getter); - this.props.add(p); - return this; - } + public SelectOperation column(Getter getter) { + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(getter); + this.props.add(p); + return this; + } - public SelectOperation orderBy(Getter getter, OrderingDirection direction) { - getOrCreateOrdering().add(new Ordered(getter, direction).getOrdering()); - return this; - } + public SelectOperation orderBy(Getter getter, OrderingDirection direction) { + getOrCreateOrdering().add(new Ordered(getter, direction).getOrdering()); + return this; + } - public SelectOperation orderBy(Ordered ordered) { - getOrCreateOrdering().add(ordered.getOrdering()); - return this; - } + public SelectOperation orderBy(Ordered ordered) { + getOrCreateOrdering().add(ordered.getOrdering()); + return this; + } - public SelectOperation limit(Integer limit) { - this.limit = limit; - return this; - } + public SelectOperation limit(Integer limit) { + this.limit = limit; + return this; + } - public SelectOperation allowFiltering() { - this.allowFiltering = true; - return this; - } + public SelectOperation allowFiltering() { + this.allowFiltering = true; + return this; + } - @Override - public boolean isSessionCacheable() { - return isCacheable; - } + @Override + public boolean isSessionCacheable() { + return isCacheable; + } - @Override - public List getFacets() { - HelenusEntity entity = props.get(0).getEntity(); - return entity.getFacets(); - } + @Override + public List getFacets() { + HelenusEntity entity = props.get(0).getEntity(); + return entity.getFacets(); + } - @Override - public List bindFacetValues() { - HelenusEntity entity = props.get(0).getEntity(); - List boundFacets = new ArrayList<>(); + @Override + public List bindFacetValues() { + HelenusEntity entity = props.get(0).getEntity(); + List boundFacets = new ArrayList<>(); - for (Facet facet : entity.getFacets()) { - if (facet instanceof UnboundFacet) { - UnboundFacet unboundFacet = (UnboundFacet) facet; - UnboundFacet.Binder binder = unboundFacet.binder(); - for (HelenusProperty prop : unboundFacet.getProperties()) { - if (filters != null) { - Filter filter = filters.get(prop); - if (filter != null) { - Object[] postulates = filter.postulateValues(); - for (Object p : postulates) { - binder.setValueForProperty(prop, p.toString()); - } - } - } + for (Facet facet : entity.getFacets()) { + if (facet instanceof UnboundFacet) { + UnboundFacet unboundFacet = (UnboundFacet) facet; + UnboundFacet.Binder binder = unboundFacet.binder(); + for (HelenusProperty prop : unboundFacet.getProperties()) { + if (filters != null) { + Filter filter = filters.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; + } - } - if (binder.isBound()) { - boundFacets.add(binder.bind()); - } - } else { - boundFacets.add(facet); - } - } - return boundFacets; - } + @Override + public BuiltStatement buildStatement(boolean cached) { - @Override - public BuiltStatement buildStatement(boolean cached) { + HelenusEntity entity = null; + Selection selection = QueryBuilder.select(); - HelenusEntity entity = null; - Selection selection = QueryBuilder.select(); + for (HelenusPropertyNode prop : props) { + String columnName = prop.getColumnName(); + selection = selection.column(columnName); - for (HelenusPropertyNode prop : props) { - String columnName = prop.getColumnName(); - selection = selection.column(columnName); + if (entity == null) { + entity = prop.getEntity(); + } else if (entity != prop.getEntity()) { + throw new HelenusMappingException( + "you can select columns only from a single entity " + + entity.getMappingInterface() + + " or " + + prop.getEntity().getMappingInterface()); + } - if (prop.getProperty().caseSensitiveIndex()) { - allowFiltering = true; - } + // 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: + break; + default: + if (entity.equals(prop.getEntity())) { + if (prop.getNext().isPresent()) { + columnName = Iterables.getLast(prop).getColumnName().toCql(true); + } + if (!prop.getProperty().getDataType().isCollectionType()) { + selection.writeTime(columnName).as(columnName + "_writeTime"); + selection.ttl(columnName).as(columnName + "_ttl"); + } + } + break; + } + } + } - if (entity == null) { - entity = prop.getEntity(); - } else if (entity != prop.getEntity()) { - throw new HelenusMappingException("you can select columns only from a single entity " - + entity.getMappingInterface() + " or " + prop.getEntity().getMappingInterface()); - } + if (entity == null) { + throw new HelenusMappingException("no entity or table to select data"); + } - // 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 : - break; - default : - if (entity.equals(prop.getEntity())) { - if (prop.getNext().isPresent()) { - columnName = Iterables.getLast(prop).getColumnName().toCql(true); - } - if (!prop.getProperty().getDataType().isCollectionType()) { - selection.writeTime(columnName).as(columnName + "_writeTime"); - selection.ttl(columnName).as(columnName + "_ttl"); - } - } - break; - } - } - } + String tableName = alternateTableName == null ? entity.getName().toCql() : alternateTableName; + Select select = selection.from(tableName); - if (entity == null) { - throw new HelenusMappingException("no entity or table to select data"); - } + if (ordering != null && !ordering.isEmpty()) { + select.orderBy(ordering.toArray(new Ordering[ordering.size()])); + } - String tableName = alternateTableName == null ? entity.getName().toCql() : alternateTableName; - Select select = selection.from(tableName); + if (limit != null) { + select.limit(limit); + } - if (ordering != null && !ordering.isEmpty()) { - select.orderBy(ordering.toArray(new Ordering[ordering.size()])); - } + if (filters != null && !filters.isEmpty()) { - if (limit != null) { - select.limit(limit); - } + Where where = select.where(); - if (filters != null && !filters.isEmpty()) { + boolean isFirstIndex = true; + for (Filter filter : filters.values()) { + where.and(filter.getClause(sessionOps.getValuePreparer())); + HelenusProperty prop = filter.getNode().getProperty(); + if (allowFiltering == false) { + switch (prop.getColumnType()) { + case PARTITION_KEY: + case CLUSTERING_COLUMN: + break; + default: + // When using non-Cassandra-standard 2i types or when using more than one + // indexed column or non-indexed columns the query must include ALLOW FILTERING. + if (prop.caseSensitiveIndex()) { + allowFiltering = true; + } else if (prop.getIndexName() != null) { + allowFiltering |= !isFirstIndex; + isFirstIndex = false; + } else { + allowFiltering = true; + } + } + } + } + } - Where where = select.where(); + if (ifFilters != null && !ifFilters.isEmpty()) { + LOG.error("onlyIf conditions " + ifFilters + " would be ignored in the statement " + select); + } - for (Filter filter : filters.values()) { - where.and(filter.getClause(sessionOps.getValuePreparer())); - } - } + if (allowFiltering) { + select.allowFiltering(); + } - if (ifFilters != null && !ifFilters.isEmpty()) { - LOG.error("onlyIf conditions " + ifFilters + " would be ignored in the statement " + select); - } + return select; + } - if (allowFiltering) { - select.allowFiltering(); - } + @SuppressWarnings("unchecked") + @Override + public Stream transform(ResultSet resultSet) { + if (rowMapper != null) { + return StreamSupport.stream( + Spliterators.spliteratorUnknownSize(resultSet.iterator(), Spliterator.ORDERED), false) + .map(rowMapper); + } else { + return (Stream) + StreamSupport.stream( + Spliterators.spliteratorUnknownSize(resultSet.iterator(), Spliterator.ORDERED), + false); + } + } - return select; - } - - @SuppressWarnings("unchecked") - @Override - public Stream transform(ResultSet resultSet) { - if (rowMapper != null) { - return StreamSupport - .stream(Spliterators.spliteratorUnknownSize(resultSet.iterator(), Spliterator.ORDERED), false) - .map(rowMapper); - } else { - return (Stream) StreamSupport - .stream(Spliterators.spliteratorUnknownSize(resultSet.iterator(), Spliterator.ORDERED), false); - } - } - - private List getOrCreateOrdering() { - if (ordering == null) { - ordering = new ArrayList(); - } - return ordering; - } + private List getOrCreateOrdering() { + if (ordering == null) { + ordering = new ArrayList(); + } + return ordering; + } } diff --git a/src/main/java/net/helenus/core/operation/SelectTransformingOperation.java b/src/main/java/net/helenus/core/operation/SelectTransformingOperation.java index 4a7720e..a7ed832 100644 --- a/src/main/java/net/helenus/core/operation/SelectTransformingOperation.java +++ b/src/main/java/net/helenus/core/operation/SelectTransformingOperation.java @@ -15,48 +15,45 @@ */ package net.helenus.core.operation; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.querybuilder.BuiltStatement; import java.util.List; import java.util.function.Function; import java.util.stream.Stream; - -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.querybuilder.BuiltStatement; - import net.helenus.core.cache.Facet; public final class SelectTransformingOperation - extends - AbstractFilterStreamOperation> { + extends AbstractFilterStreamOperation> { - private final SelectOperation delegate; - private final Function fn; + private final SelectOperation delegate; + private final Function fn; - public SelectTransformingOperation(SelectOperation delegate, Function fn) { - super(delegate.sessionOps); + public SelectTransformingOperation(SelectOperation delegate, Function fn) { + super(delegate.sessionOps); - this.delegate = delegate; - this.fn = fn; - this.filters = delegate.filters; - this.ifFilters = delegate.ifFilters; - } + this.delegate = delegate; + this.fn = fn; + this.filters = delegate.filters; + this.ifFilters = delegate.ifFilters; + } - @Override - public List bindFacetValues() { - return delegate.bindFacetValues(); - } + @Override + public List bindFacetValues() { + return delegate.bindFacetValues(); + } - @Override - public List getFacets() { - return delegate.getFacets(); - } + @Override + public List getFacets() { + return delegate.getFacets(); + } - @Override - public BuiltStatement buildStatement(boolean cached) { - return delegate.buildStatement(cached); - } + @Override + public BuiltStatement buildStatement(boolean cached) { + return delegate.buildStatement(cached); + } - @Override - public Stream transform(ResultSet resultSet) { - return delegate.transform(resultSet).map(fn); - } + @Override + public Stream transform(ResultSet resultSet) { + return delegate.transform(resultSet).map(fn); + } } diff --git a/src/main/java/net/helenus/core/operation/UpdateOperation.java b/src/main/java/net/helenus/core/operation/UpdateOperation.java index aa65bae..bbc4935 100644 --- a/src/main/java/net/helenus/core/operation/UpdateOperation.java +++ b/src/main/java/net/helenus/core/operation/UpdateOperation.java @@ -15,17 +15,15 @@ */ package net.helenus.core.operation; -import java.util.*; -import java.util.concurrent.TimeoutException; -import java.util.function.Function; -import java.util.stream.Collectors; - import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.querybuilder.Assignment; import com.datastax.driver.core.querybuilder.BuiltStatement; import com.datastax.driver.core.querybuilder.QueryBuilder; import com.datastax.driver.core.querybuilder.Update; - +import java.util.*; +import java.util.concurrent.TimeoutException; +import java.util.function.Function; +import java.util.stream.Collectors; import net.helenus.core.*; import net.helenus.core.cache.BoundFacet; import net.helenus.core.cache.Facet; @@ -36,697 +34,739 @@ import net.helenus.mapping.HelenusProperty; import net.helenus.mapping.MappingUtil; import net.helenus.mapping.value.BeanColumnValueProvider; import net.helenus.mapping.value.ValueProviderMap; +import net.helenus.support.HelenusException; import net.helenus.support.HelenusMappingException; import net.helenus.support.Immutables; public final class UpdateOperation extends AbstractFilterOperation> { - private final Map assignments = new HashMap<>(); - private final AbstractEntityDraft draft; - private final Map draftMap; - private HelenusEntity entity = null; - private Object pojo; - private int[] ttl; - private long[] timestamp; - - public UpdateOperation(AbstractSessionOperations sessionOperations) { - super(sessionOperations); - this.draft = null; - this.draftMap = null; - } - - public UpdateOperation(AbstractSessionOperations sessionOperations, AbstractEntityDraft draft) { - super(sessionOperations); - this.draft = draft; - this.draftMap = draft.toMap(); - } - - public UpdateOperation(AbstractSessionOperations sessionOperations, Object pojo) { - super(sessionOperations); - this.draft = null; - this.draftMap = null; - this.pojo = pojo; - this.entity = Helenus.resolve(MappingUtil.getMappingInterface(pojo)); - } - - public UpdateOperation(AbstractSessionOperations sessionOperations, HelenusPropertyNode p, Object v) { - super(sessionOperations); - this.draft = null; - this.draftMap = null; - - Object value = sessionOps.getValuePreparer().prepareColumnValue(v, p.getProperty()); - assignments.put(QueryBuilder.set(p.getColumnName(), value), new BoundFacet(p.getProperty(), v)); - - addPropertyNode(p); - } - - public UpdateOperation set(Getter getter, V v) { - Objects.requireNonNull(getter, "getter is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(getter); - HelenusProperty prop = p.getProperty(); - - Object value = sessionOps.getValuePreparer().prepareColumnValue(v, prop); - assignments.put(QueryBuilder.set(p.getColumnName(), value), new BoundFacet(prop, value)); - - if (draft != null) { - String key = prop.getPropertyName(); - if (draft.get(key, value.getClass()) != value) { - draft.set(key, value); - } - } - - if (entity != null) { - if (entity.isCacheable() && pojo != null && pojo instanceof MapExportable) { - String key = prop.getPropertyName(); - Map map = ((MapExportable) pojo).toMap(); - if (!(map instanceof ValueProviderMap)) { - if (map.get(key) != value) { - map.put(key, value); - } - } - } - } - - addPropertyNode(p); - - return this; - } - - /* - * - * - * COUNTER - * - * - */ - - public UpdateOperation increment(Getter counterGetter) { - return increment(counterGetter, 1L); - } - - public UpdateOperation increment(Getter counterGetter, long delta) { - - Objects.requireNonNull(counterGetter, "counterGetter is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(counterGetter); - - BoundFacet facet = null; - if (pojo != null) { - HelenusProperty prop = p.getProperty(); - Long value = (Long) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop); - facet = new BoundFacet(prop, value + delta); - } else if (draft != null) { - String key = p.getProperty().getPropertyName(); - draftMap.put(key, (Long) draftMap.get(key) + delta); - } - - assignments.put(QueryBuilder.incr(p.getColumnName(), delta), facet); - - addPropertyNode(p); - - return this; - } - - public UpdateOperation decrement(Getter counterGetter) { - return decrement(counterGetter, 1L); - } - - public UpdateOperation decrement(Getter counterGetter, long delta) { - - Objects.requireNonNull(counterGetter, "counterGetter is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(counterGetter); - - BoundFacet facet = null; - if (pojo != null) { - HelenusProperty prop = p.getProperty(); - Long value = (Long) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop); - facet = new BoundFacet(prop, value - delta); - } else if (draft != null) { - String key = p.getProperty().getPropertyName(); - draftMap.put(key, (Long) draftMap.get(key) - delta); - } - - assignments.put(QueryBuilder.decr(p.getColumnName(), delta), facet); - - addPropertyNode(p); - - return this; - } - - /* - * - * - * LIST - * - */ - - public UpdateOperation prepend(Getter> listGetter, V value) { - - Objects.requireNonNull(listGetter, "listGetter is empty"); - Objects.requireNonNull(value, "value is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(listGetter); - Object valueObj = prepareSingleListValue(p, value); - - BoundFacet facet = null; - if (pojo != null) { - HelenusProperty prop = p.getProperty(); - List list = new ArrayList((List) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); - list.add(0, value); - facet = new BoundFacet(prop, list); - } else if (draft != null) { - String key = p.getProperty().getPropertyName(); - List list = (List) draftMap.get(key); - list.add(0, value); - } - - assignments.put(QueryBuilder.prepend(p.getColumnName(), valueObj), facet); - - addPropertyNode(p); - - return this; - } - - public UpdateOperation prependAll(Getter> listGetter, List value) { - - Objects.requireNonNull(listGetter, "listGetter is empty"); - Objects.requireNonNull(value, "value is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(listGetter); - List valueObj = prepareListValue(p, value); - - BoundFacet facet = null; - if (pojo != null) { - HelenusProperty prop = p.getProperty(); - List list = new ArrayList((List) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); - list.addAll(0, value); - facet = new BoundFacet(prop, list); - } else if (draft != null && value.size() > 0) { - String key = p.getProperty().getPropertyName(); - List list = (List) draftMap.get(key); - list.addAll(0, value); - } - - assignments.put(QueryBuilder.prependAll(p.getColumnName(), valueObj), facet); - - addPropertyNode(p); - - return this; - } - - public UpdateOperation setIdx(Getter> listGetter, int idx, V value) { - - Objects.requireNonNull(listGetter, "listGetter is empty"); - Objects.requireNonNull(value, "value is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(listGetter); - Object valueObj = prepareSingleListValue(p, value); - - BoundFacet facet = null; - if (pojo != null || draft != null) { - List list; - HelenusProperty prop = p.getProperty(); - if (pojo != null) { - list = new ArrayList((List) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); - } else { - String key = p.getProperty().getPropertyName(); - list = (List) draftMap.get(key); - } - if (idx < 0) { - list.add(0, value); - } else if (idx > list.size()) { - list.add(list.size(), value); - } else { - list.add(idx, value); - } - list.add(0, value); - facet = new BoundFacet(prop, list); - } - - assignments.put(QueryBuilder.setIdx(p.getColumnName(), idx, valueObj), facet); - - addPropertyNode(p); - - return this; - } - - public UpdateOperation append(Getter> listGetter, V value) { - - Objects.requireNonNull(listGetter, "listGetter is empty"); - Objects.requireNonNull(value, "value is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(listGetter); - Object valueObj = prepareSingleListValue(p, value); - - BoundFacet facet = null; - if (pojo != null) { - HelenusProperty prop = p.getProperty(); - List list = new ArrayList((List) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); - list.add(value); - facet = new BoundFacet(prop, list); - } else if (draft != null) { - String key = p.getProperty().getPropertyName(); - List list = (List) draftMap.get(key); - list.add(value); - } - assignments.put(QueryBuilder.append(p.getColumnName(), valueObj), facet); - - addPropertyNode(p); - - return this; - } - - public UpdateOperation appendAll(Getter> listGetter, List value) { - - Objects.requireNonNull(listGetter, "listGetter is empty"); - Objects.requireNonNull(value, "value is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(listGetter); - List valueObj = prepareListValue(p, value); - - BoundFacet facet = null; - if (pojo != null) { - HelenusProperty prop = p.getProperty(); - List list = new ArrayList((List) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); - list.addAll(value); - facet = new BoundFacet(prop, list); - } else if (draft != null && value.size() > 0) { - String key = p.getProperty().getPropertyName(); - List list = (List) draftMap.get(key); - list.addAll(value); - } - assignments.put(QueryBuilder.appendAll(p.getColumnName(), valueObj), facet); - - addPropertyNode(p); - - return this; - } - - public UpdateOperation discard(Getter> listGetter, V value) { - - Objects.requireNonNull(listGetter, "listGetter is empty"); - Objects.requireNonNull(value, "value is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(listGetter); - Object valueObj = prepareSingleListValue(p, value); - - BoundFacet facet = null; - if (pojo != null) { - HelenusProperty prop = p.getProperty(); - List list = new ArrayList((List) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); - list.remove(value); - facet = new BoundFacet(prop, list); - } else if (draft != null) { - String key = p.getProperty().getPropertyName(); - List list = (List) draftMap.get(key); - list.remove(value); - } - assignments.put(QueryBuilder.discard(p.getColumnName(), valueObj), facet); - - addPropertyNode(p); - - return this; - } - - public UpdateOperation discardAll(Getter> listGetter, List value) { - - Objects.requireNonNull(listGetter, "listGetter is empty"); - Objects.requireNonNull(value, "value is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(listGetter); - List valueObj = prepareListValue(p, value); - - BoundFacet facet = null; - if (pojo != null) { - HelenusProperty prop = p.getProperty(); - List list = new ArrayList((List) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); - list.removeAll(value); - facet = new BoundFacet(prop, list); - } else if (draft != null) { - String key = p.getProperty().getPropertyName(); - List list = (List) draftMap.get(key); - list.removeAll(value); - } - assignments.put(QueryBuilder.discardAll(p.getColumnName(), valueObj), facet); - - addPropertyNode(p); - - return this; - } - - private Object prepareSingleListValue(HelenusPropertyNode p, Object value) { - HelenusProperty prop = p.getProperty(); - - Object valueObj = value; - - Optional> converter = prop.getWriteConverter(sessionOps.getSessionRepository()); - if (converter.isPresent()) { - List convertedList = (List) converter.get().apply(Immutables.listOf(value)); - valueObj = convertedList.get(0); - } - - return valueObj; - } - - private List prepareListValue(HelenusPropertyNode p, List value) { - - HelenusProperty prop = p.getProperty(); - - List valueObj = value; - - Optional> converter = prop.getWriteConverter(sessionOps.getSessionRepository()); - if (converter.isPresent()) { - valueObj = (List) converter.get().apply(value); - } - - return valueObj; - } - - /* - * - * - * SET - * - * - */ - - public UpdateOperation add(Getter> setGetter, V value) { - - Objects.requireNonNull(setGetter, "setGetter is empty"); - Objects.requireNonNull(value, "value is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(setGetter); - Object valueObj = prepareSingleSetValue(p, value); - - BoundFacet facet = null; - if (pojo != null) { - HelenusProperty prop = p.getProperty(); - Set set = new HashSet((Set) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); - set.add(value); - facet = new BoundFacet(prop, set); - } else if (draft != null) { - String key = p.getProperty().getPropertyName(); - Set set = (Set) draftMap.get(key); - set.add(value); - } - assignments.put(QueryBuilder.add(p.getColumnName(), valueObj), facet); - - addPropertyNode(p); - - return this; - } - - public UpdateOperation addAll(Getter> setGetter, Set value) { - - Objects.requireNonNull(setGetter, "setGetter is empty"); - Objects.requireNonNull(value, "value is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(setGetter); - Set valueObj = prepareSetValue(p, value); - - BoundFacet facet = null; - if (pojo != null) { - HelenusProperty prop = p.getProperty(); - Set set = new HashSet((Set) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); - set.addAll(value); - facet = new BoundFacet(prop, set); - } else if (draft != null) { - String key = p.getProperty().getPropertyName(); - Set set = (Set) draftMap.get(key); - set.addAll(value); - } - assignments.put(QueryBuilder.addAll(p.getColumnName(), valueObj), facet); - - addPropertyNode(p); - - return this; - } - - public UpdateOperation remove(Getter> setGetter, V value) { - - Objects.requireNonNull(setGetter, "setGetter is empty"); - Objects.requireNonNull(value, "value is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(setGetter); - Object valueObj = prepareSingleSetValue(p, value); - - BoundFacet facet = null; - if (pojo != null) { - HelenusProperty prop = p.getProperty(); - Set set = new HashSet((Set) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); - set.remove(value); - facet = new BoundFacet(prop, set); - } else if (draft != null) { - String key = p.getProperty().getPropertyName(); - Set set = (Set) draftMap.get(key); - set.remove(value); - } - assignments.put(QueryBuilder.remove(p.getColumnName(), valueObj), facet); - - addPropertyNode(p); - - return this; - } - - public UpdateOperation removeAll(Getter> setGetter, Set value) { - - Objects.requireNonNull(setGetter, "setGetter is empty"); - Objects.requireNonNull(value, "value is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(setGetter); - Set valueObj = prepareSetValue(p, value); - - BoundFacet facet = null; - if (pojo != null) { - HelenusProperty prop = p.getProperty(); - Set set = new HashSet((Set) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); - set.removeAll(value); - facet = new BoundFacet(prop, set); - } else if (draft != null) { - String key = p.getProperty().getPropertyName(); - Set set = (Set) draftMap.get(key); - set.removeAll(value); - } - assignments.put(QueryBuilder.removeAll(p.getColumnName(), valueObj), facet); - - addPropertyNode(p); - - return this; - } - - private Object prepareSingleSetValue(HelenusPropertyNode p, Object value) { - - HelenusProperty prop = p.getProperty(); - Object valueObj = value; - - Optional> converter = prop.getWriteConverter(sessionOps.getSessionRepository()); - if (converter.isPresent()) { - Set convertedSet = (Set) converter.get().apply(Immutables.setOf(value)); - valueObj = convertedSet.iterator().next(); - } - - return valueObj; - } - - private Set prepareSetValue(HelenusPropertyNode p, Set value) { - - HelenusProperty prop = p.getProperty(); - Set valueObj = value; - - Optional> converter = prop.getWriteConverter(sessionOps.getSessionRepository()); - if (converter.isPresent()) { - valueObj = (Set) converter.get().apply(value); - } - - return valueObj; - } - - /* - * - * - * MAP - * - * - */ - - public UpdateOperation put(Getter> mapGetter, K key, V value) { - - Objects.requireNonNull(mapGetter, "mapGetter is empty"); - Objects.requireNonNull(key, "key is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(mapGetter); - HelenusProperty prop = p.getProperty(); - - BoundFacet facet = null; - if (pojo != null) { - Map map = new HashMap( - (Map) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); - map.put(key, value); - facet = new BoundFacet(prop, map); - } else if (draft != null) { - ((Map) draftMap.get(prop.getPropertyName())).put(key, value); - } - - Optional> converter = prop.getWriteConverter(sessionOps.getSessionRepository()); - if (converter.isPresent()) { - Map convertedMap = (Map) converter.get() - .apply(Immutables.mapOf(key, value)); - for (Map.Entry e : convertedMap.entrySet()) { - assignments.put(QueryBuilder.put(p.getColumnName(), e.getKey(), e.getValue()), facet); - } - } else { - assignments.put(QueryBuilder.put(p.getColumnName(), key, value), facet); - } - - addPropertyNode(p); - - return this; - } - - public UpdateOperation putAll(Getter> mapGetter, Map map) { - - Objects.requireNonNull(mapGetter, "mapGetter is empty"); - Objects.requireNonNull(map, "map is empty"); - - HelenusPropertyNode p = MappingUtil.resolveMappingProperty(mapGetter); - HelenusProperty prop = p.getProperty(); - - BoundFacet facet = null; - if (pojo != null) { - Map newMap = new HashMap( - (Map) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); - newMap.putAll(map); - facet = new BoundFacet(prop, newMap); - } else if (draft != null) { - ((Map) draftMap.get(prop.getPropertyName())).putAll(map); - } - - Optional> converter = prop.getWriteConverter(sessionOps.getSessionRepository()); - if (converter.isPresent()) { - Map convertedMap = (Map) converter.get().apply(map); - assignments.put(QueryBuilder.putAll(p.getColumnName(), convertedMap), facet); - } else { - assignments.put(QueryBuilder.putAll(p.getColumnName(), map), facet); - } - - addPropertyNode(p); - - return this; - } - - @Override - public BuiltStatement buildStatement(boolean cached) { - - if (entity == null) { - throw new HelenusMappingException("empty update operation"); - } - - Update update = QueryBuilder.update(entity.getName().toCql()); - - for (Assignment assignment : assignments.keySet()) { - update.with(assignment); - } - - if (filters != null && !filters.isEmpty()) { - - for (Filter filter : filters) { - update.where(filter.getClause(sessionOps.getValuePreparer())); - } - } - - if (ifFilters != null && !ifFilters.isEmpty()) { - - for (Filter filter : ifFilters) { - update.onlyIf(filter.getClause(sessionOps.getValuePreparer())); - } - } - - if (this.ttl != null) { - update.using(QueryBuilder.ttl(this.ttl[0])); - } - - if (this.timestamp != null) { - update.using(QueryBuilder.timestamp(this.timestamp[0])); - } - - return update; - } - - @Override - public E transform(ResultSet resultSet) { - if (draft != null) { - return Helenus.map(draft.getEntityClass(), draft.toMap(draftMap)); - } else { - return (E) resultSet; - } - } - - public UpdateOperation usingTtl(int ttl) { - this.ttl = new int[1]; - this.ttl[0] = ttl; - return this; - } - - public UpdateOperation usingTimestamp(long timestamp) { - this.timestamp = new long[1]; - this.timestamp[0] = timestamp; - return this; - } - - private void addPropertyNode(HelenusPropertyNode p) { - if (entity == null) { - entity = p.getEntity(); - } else if (entity != p.getEntity()) { - throw new HelenusMappingException("you can update columns only in single entity " - + entity.getMappingInterface() + " or " + p.getEntity().getMappingInterface()); - } - } - - @Override - public E sync() throws TimeoutException { - E result = super.sync(); - if (entity.isCacheable()) { - if (draft != null) { - sessionOps.updateCache(draft, bindFacetValues()); - } else if (pojo != null) { - sessionOps.updateCache(pojo, bindFacetValues()); - } else { - sessionOps.cacheEvict(bindFacetValues()); - } - } - return result; - } - - @Override - public E sync(UnitOfWork uow) throws TimeoutException { - if (uow == null) { - return sync(); - } - E result = super.sync(uow); - if (draft != null) { - cacheUpdate(uow, result, bindFacetValues()); - } else if (pojo != null) { - cacheUpdate(uow, (E) pojo, bindFacetValues()); - } - return result; - } - - @Override - public List bindFacetValues() { - List facets = bindFacetValues(entity.getFacets()); - facets.addAll(assignments.values().stream().distinct().filter(o -> o != null).collect(Collectors.toList())); - return facets; - } - - @Override - public List getFacets() { - if (entity != null) { - return entity.getFacets(); - } else { - return new ArrayList(); - } - } - + private final Map assignments = new HashMap<>(); + private final AbstractEntityDraft draft; + private final Map draftMap; + private HelenusEntity entity = null; + private Object pojo; + private int[] ttl; + private long[] timestamp; + + public UpdateOperation(AbstractSessionOperations sessionOperations) { + super(sessionOperations); + this.draft = null; + this.draftMap = null; + } + + public UpdateOperation( + AbstractSessionOperations sessionOperations, AbstractEntityDraft draft) { + super(sessionOperations); + this.draft = draft; + this.draftMap = draft.toMap(); + } + + public UpdateOperation(AbstractSessionOperations sessionOperations, Object pojo) { + super(sessionOperations); + this.draft = null; + this.draftMap = null; + this.pojo = pojo; + this.entity = Helenus.resolve(MappingUtil.getMappingInterface(pojo)); + } + + public UpdateOperation( + AbstractSessionOperations sessionOperations, HelenusPropertyNode p, Object v) { + super(sessionOperations); + this.draft = null; + this.draftMap = null; + + Object value = sessionOps.getValuePreparer().prepareColumnValue(v, p.getProperty()); + assignments.put(QueryBuilder.set(p.getColumnName(), value), new BoundFacet(p.getProperty(), v)); + + addPropertyNode(p); + } + + public UpdateOperation set(Getter getter, V v) { + Objects.requireNonNull(getter, "getter is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(getter); + HelenusProperty prop = p.getProperty(); + + Object value = sessionOps.getValuePreparer().prepareColumnValue(v, prop); + assignments.put(QueryBuilder.set(p.getColumnName(), value), new BoundFacet(prop, value)); + + if (draft != null) { + String key = prop.getPropertyName(); + if (draft.get(key, value.getClass()) != v) { + draft.set(key, v); + } + } + + if (entity != null) { + if (entity.isCacheable() && pojo != null && pojo instanceof MapExportable) { + String key = prop.getPropertyName(); + Map map = ((MapExportable) pojo).toMap(); + if (!(map instanceof ValueProviderMap)) { + if (map.get(key) != v) { + map.put(key, v); + } + } + } + } + + addPropertyNode(p); + + return this; + } + + /* + * + * + * COUNTER + * + * + */ + + public UpdateOperation increment(Getter counterGetter) { + return increment(counterGetter, 1L); + } + + public UpdateOperation increment(Getter counterGetter, long delta) { + + Objects.requireNonNull(counterGetter, "counterGetter is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(counterGetter); + + BoundFacet facet = null; + if (pojo != null) { + HelenusProperty prop = p.getProperty(); + Long value = (Long) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop); + facet = new BoundFacet(prop, value + delta); + } else if (draft != null) { + String key = p.getProperty().getPropertyName(); + draftMap.put(key, (Long) draftMap.get(key) + delta); + } + + assignments.put(QueryBuilder.incr(p.getColumnName(), delta), facet); + + addPropertyNode(p); + + return this; + } + + public UpdateOperation decrement(Getter counterGetter) { + return decrement(counterGetter, 1L); + } + + public UpdateOperation decrement(Getter counterGetter, long delta) { + + Objects.requireNonNull(counterGetter, "counterGetter is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(counterGetter); + + BoundFacet facet = null; + if (pojo != null) { + HelenusProperty prop = p.getProperty(); + Long value = (Long) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop); + facet = new BoundFacet(prop, value - delta); + } else if (draft != null) { + String key = p.getProperty().getPropertyName(); + draftMap.put(key, (Long) draftMap.get(key) - delta); + } + + assignments.put(QueryBuilder.decr(p.getColumnName(), delta), facet); + + addPropertyNode(p); + + return this; + } + + /* + * + * + * LIST + * + */ + + public UpdateOperation prepend(Getter> listGetter, V value) { + + Objects.requireNonNull(listGetter, "listGetter is empty"); + Objects.requireNonNull(value, "value is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(listGetter); + Object valueObj = prepareSingleListValue(p, value); + + BoundFacet facet = null; + if (pojo != null) { + HelenusProperty prop = p.getProperty(); + List list = + new ArrayList( + (List) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); + list.add(0, value); + facet = new BoundFacet(prop, list); + } else if (draft != null) { + String key = p.getProperty().getPropertyName(); + List list = (List) draftMap.get(key); + list.add(0, value); + } + + assignments.put(QueryBuilder.prepend(p.getColumnName(), valueObj), facet); + + addPropertyNode(p); + + return this; + } + + public UpdateOperation prependAll(Getter> listGetter, List value) { + + Objects.requireNonNull(listGetter, "listGetter is empty"); + Objects.requireNonNull(value, "value is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(listGetter); + List valueObj = prepareListValue(p, value); + + BoundFacet facet = null; + if (pojo != null) { + HelenusProperty prop = p.getProperty(); + List list = + new ArrayList( + (List) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); + list.addAll(0, value); + facet = new BoundFacet(prop, list); + } else if (draft != null && value.size() > 0) { + String key = p.getProperty().getPropertyName(); + List list = (List) draftMap.get(key); + list.addAll(0, value); + } + + assignments.put(QueryBuilder.prependAll(p.getColumnName(), valueObj), facet); + + addPropertyNode(p); + + return this; + } + + public UpdateOperation setIdx(Getter> listGetter, int idx, V value) { + + Objects.requireNonNull(listGetter, "listGetter is empty"); + Objects.requireNonNull(value, "value is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(listGetter); + Object valueObj = prepareSingleListValue(p, value); + + BoundFacet facet = null; + if (pojo != null || draft != null) { + List list; + HelenusProperty prop = p.getProperty(); + if (pojo != null) { + list = + new ArrayList( + (List) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); + } else { + String key = p.getProperty().getPropertyName(); + list = (List) draftMap.get(key); + } + if (idx < 0) { + list.add(0, value); + } else if (idx > list.size()) { + list.add(list.size(), value); + } else { + list.add(idx, value); + } + list.add(0, value); + facet = new BoundFacet(prop, list); + } + + assignments.put(QueryBuilder.setIdx(p.getColumnName(), idx, valueObj), facet); + + addPropertyNode(p); + + return this; + } + + public UpdateOperation append(Getter> listGetter, V value) { + + Objects.requireNonNull(listGetter, "listGetter is empty"); + Objects.requireNonNull(value, "value is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(listGetter); + Object valueObj = prepareSingleListValue(p, value); + + BoundFacet facet = null; + if (pojo != null) { + HelenusProperty prop = p.getProperty(); + List list = + new ArrayList( + (List) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); + list.add(value); + facet = new BoundFacet(prop, list); + } else if (draft != null) { + String key = p.getProperty().getPropertyName(); + List list = (List) draftMap.get(key); + list.add(value); + } + assignments.put(QueryBuilder.append(p.getColumnName(), valueObj), facet); + + addPropertyNode(p); + + return this; + } + + public UpdateOperation appendAll(Getter> listGetter, List value) { + + Objects.requireNonNull(listGetter, "listGetter is empty"); + Objects.requireNonNull(value, "value is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(listGetter); + List valueObj = prepareListValue(p, value); + + BoundFacet facet = null; + if (pojo != null) { + HelenusProperty prop = p.getProperty(); + List list = + new ArrayList( + (List) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); + list.addAll(value); + facet = new BoundFacet(prop, list); + } else if (draft != null && value.size() > 0) { + String key = p.getProperty().getPropertyName(); + List list = (List) draftMap.get(key); + list.addAll(value); + } + assignments.put(QueryBuilder.appendAll(p.getColumnName(), valueObj), facet); + + addPropertyNode(p); + + return this; + } + + public UpdateOperation discard(Getter> listGetter, V value) { + + Objects.requireNonNull(listGetter, "listGetter is empty"); + Objects.requireNonNull(value, "value is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(listGetter); + Object valueObj = prepareSingleListValue(p, value); + + BoundFacet facet = null; + if (pojo != null) { + HelenusProperty prop = p.getProperty(); + List list = + new ArrayList( + (List) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); + list.remove(value); + facet = new BoundFacet(prop, list); + } else if (draft != null) { + String key = p.getProperty().getPropertyName(); + List list = (List) draftMap.get(key); + list.remove(value); + } + assignments.put(QueryBuilder.discard(p.getColumnName(), valueObj), facet); + + addPropertyNode(p); + + return this; + } + + public UpdateOperation discardAll(Getter> listGetter, List value) { + + Objects.requireNonNull(listGetter, "listGetter is empty"); + Objects.requireNonNull(value, "value is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(listGetter); + List valueObj = prepareListValue(p, value); + + BoundFacet facet = null; + if (pojo != null) { + HelenusProperty prop = p.getProperty(); + List list = + new ArrayList( + (List) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); + list.removeAll(value); + facet = new BoundFacet(prop, list); + } else if (draft != null) { + String key = p.getProperty().getPropertyName(); + List list = (List) draftMap.get(key); + list.removeAll(value); + } + assignments.put(QueryBuilder.discardAll(p.getColumnName(), valueObj), facet); + + addPropertyNode(p); + + return this; + } + + private Object prepareSingleListValue(HelenusPropertyNode p, Object value) { + HelenusProperty prop = p.getProperty(); + + Object valueObj = value; + + Optional> converter = + prop.getWriteConverter(sessionOps.getSessionRepository()); + if (converter.isPresent()) { + List convertedList = (List) converter.get().apply(Immutables.listOf(value)); + valueObj = convertedList.get(0); + } + + return valueObj; + } + + private List prepareListValue(HelenusPropertyNode p, List value) { + + HelenusProperty prop = p.getProperty(); + + List valueObj = value; + + Optional> converter = + prop.getWriteConverter(sessionOps.getSessionRepository()); + if (converter.isPresent()) { + valueObj = (List) converter.get().apply(value); + } + + return valueObj; + } + + /* + * + * + * SET + * + * + */ + + public UpdateOperation add(Getter> setGetter, V value) { + + Objects.requireNonNull(setGetter, "setGetter is empty"); + Objects.requireNonNull(value, "value is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(setGetter); + Object valueObj = prepareSingleSetValue(p, value); + + BoundFacet facet = null; + if (pojo != null) { + HelenusProperty prop = p.getProperty(); + Set set = + new HashSet((Set) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); + set.add(value); + facet = new BoundFacet(prop, set); + } else if (draft != null) { + String key = p.getProperty().getPropertyName(); + Set set = (Set) draftMap.get(key); + set.add(value); + } + assignments.put(QueryBuilder.add(p.getColumnName(), valueObj), facet); + + addPropertyNode(p); + + return this; + } + + public UpdateOperation addAll(Getter> setGetter, Set value) { + + Objects.requireNonNull(setGetter, "setGetter is empty"); + Objects.requireNonNull(value, "value is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(setGetter); + Set valueObj = prepareSetValue(p, value); + + BoundFacet facet = null; + if (pojo != null) { + HelenusProperty prop = p.getProperty(); + Set set = + new HashSet((Set) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); + set.addAll(value); + facet = new BoundFacet(prop, set); + } else if (draft != null) { + String key = p.getProperty().getPropertyName(); + Set set = (Set) draftMap.get(key); + set.addAll(value); + } + assignments.put(QueryBuilder.addAll(p.getColumnName(), valueObj), facet); + + addPropertyNode(p); + + return this; + } + + public UpdateOperation remove(Getter> setGetter, V value) { + + Objects.requireNonNull(setGetter, "setGetter is empty"); + Objects.requireNonNull(value, "value is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(setGetter); + Object valueObj = prepareSingleSetValue(p, value); + + BoundFacet facet = null; + if (pojo != null) { + HelenusProperty prop = p.getProperty(); + Set set = + new HashSet((Set) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); + set.remove(value); + facet = new BoundFacet(prop, set); + } else if (draft != null) { + String key = p.getProperty().getPropertyName(); + Set set = (Set) draftMap.get(key); + set.remove(value); + } + assignments.put(QueryBuilder.remove(p.getColumnName(), valueObj), facet); + + addPropertyNode(p); + + return this; + } + + public UpdateOperation removeAll(Getter> setGetter, Set value) { + + Objects.requireNonNull(setGetter, "setGetter is empty"); + Objects.requireNonNull(value, "value is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(setGetter); + Set valueObj = prepareSetValue(p, value); + + BoundFacet facet = null; + if (pojo != null) { + HelenusProperty prop = p.getProperty(); + Set set = + new HashSet((Set) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); + set.removeAll(value); + facet = new BoundFacet(prop, set); + } else if (draft != null) { + String key = p.getProperty().getPropertyName(); + Set set = (Set) draftMap.get(key); + set.removeAll(value); + } + assignments.put(QueryBuilder.removeAll(p.getColumnName(), valueObj), facet); + + addPropertyNode(p); + + return this; + } + + private Object prepareSingleSetValue(HelenusPropertyNode p, Object value) { + + HelenusProperty prop = p.getProperty(); + Object valueObj = value; + + Optional> converter = + prop.getWriteConverter(sessionOps.getSessionRepository()); + if (converter.isPresent()) { + Set convertedSet = (Set) converter.get().apply(Immutables.setOf(value)); + valueObj = convertedSet.iterator().next(); + } + + return valueObj; + } + + private Set prepareSetValue(HelenusPropertyNode p, Set value) { + + HelenusProperty prop = p.getProperty(); + Set valueObj = value; + + Optional> converter = + prop.getWriteConverter(sessionOps.getSessionRepository()); + if (converter.isPresent()) { + valueObj = (Set) converter.get().apply(value); + } + + return valueObj; + } + + /* + * + * + * MAP + * + * + */ + + public UpdateOperation put(Getter> mapGetter, K key, V value) { + + Objects.requireNonNull(mapGetter, "mapGetter is empty"); + Objects.requireNonNull(key, "key is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(mapGetter); + HelenusProperty prop = p.getProperty(); + + BoundFacet facet = null; + if (pojo != null) { + Map map = + new HashMap( + (Map) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); + map.put(key, value); + facet = new BoundFacet(prop, map); + } else if (draft != null) { + ((Map) draftMap.get(prop.getPropertyName())).put(key, value); + } + + Optional> converter = + prop.getWriteConverter(sessionOps.getSessionRepository()); + if (converter.isPresent()) { + Map convertedMap = + (Map) converter.get().apply(Immutables.mapOf(key, value)); + for (Map.Entry e : convertedMap.entrySet()) { + assignments.put(QueryBuilder.put(p.getColumnName(), e.getKey(), e.getValue()), facet); + } + } else { + assignments.put(QueryBuilder.put(p.getColumnName(), key, value), facet); + } + + addPropertyNode(p); + + return this; + } + + public UpdateOperation putAll(Getter> mapGetter, Map map) { + + Objects.requireNonNull(mapGetter, "mapGetter is empty"); + Objects.requireNonNull(map, "map is empty"); + + HelenusPropertyNode p = MappingUtil.resolveMappingProperty(mapGetter); + HelenusProperty prop = p.getProperty(); + + BoundFacet facet = null; + if (pojo != null) { + Map newMap = + new HashMap( + (Map) BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop)); + newMap.putAll(map); + facet = new BoundFacet(prop, newMap); + } else if (draft != null) { + ((Map) draftMap.get(prop.getPropertyName())).putAll(map); + } + + Optional> converter = + prop.getWriteConverter(sessionOps.getSessionRepository()); + if (converter.isPresent()) { + Map convertedMap = (Map) converter.get().apply(map); + assignments.put(QueryBuilder.putAll(p.getColumnName(), convertedMap), facet); + } else { + assignments.put(QueryBuilder.putAll(p.getColumnName(), map), facet); + } + + addPropertyNode(p); + + return this; + } + + @Override + public BuiltStatement buildStatement(boolean cached) { + + if (entity == null) { + throw new HelenusMappingException("empty update operation"); + } + + Update update = QueryBuilder.update(entity.getName().toCql()); + + for (Assignment assignment : assignments.keySet()) { + update.with(assignment); + } + + if (filters != null && !filters.isEmpty()) { + + for (Filter filter : filters) { + update.where(filter.getClause(sessionOps.getValuePreparer())); + } + } + + if (ifFilters != null && !ifFilters.isEmpty()) { + + for (Filter filter : ifFilters) { + update.onlyIf(filter.getClause(sessionOps.getValuePreparer())); + } + } + + if (this.ttl != null) { + update.using(QueryBuilder.ttl(this.ttl[0])); + } + + if (this.timestamp != null) { + update.using(QueryBuilder.timestamp(this.timestamp[0])); + } + + return update; + } + + @Override + public E transform(ResultSet resultSet) { + if ((ifFilters != null && !ifFilters.isEmpty()) && (resultSet.wasApplied() == false)) { + throw new HelenusException("Statement was not applied due to consistency constraints"); + } + + if (draft != null) { + return Helenus.map(draft.getEntityClass(), draft.toMap(draftMap)); + } else { + return (E) resultSet; + } + } + + public UpdateOperation usingTtl(int ttl) { + this.ttl = new int[1]; + this.ttl[0] = ttl; + return this; + } + + public UpdateOperation usingTimestamp(long timestamp) { + this.timestamp = new long[1]; + this.timestamp[0] = timestamp; + return this; + } + + private void addPropertyNode(HelenusPropertyNode p) { + if (entity == null) { + entity = p.getEntity(); + } else if (entity != p.getEntity()) { + throw new HelenusMappingException( + "you can update columns only in single entity " + + entity.getMappingInterface() + + " or " + + p.getEntity().getMappingInterface()); + } + } + + @Override + public E sync() throws TimeoutException { + E result = super.sync(); + if (entity.isCacheable()) { + if (draft != null) { + sessionOps.updateCache(draft, bindFacetValues()); + } else if (pojo != null) { + sessionOps.updateCache(pojo, bindFacetValues()); + } else { + sessionOps.cacheEvict(bindFacetValues()); + } + } + return result; + } + + @Override + public E sync(UnitOfWork uow) throws TimeoutException { + if (uow == null) { + return sync(); + } + E result = super.sync(uow); + if (draft != null) { + cacheUpdate(uow, result, bindFacetValues()); + } else if (pojo != null) { + cacheUpdate(uow, (E) pojo, bindFacetValues()); + return (E) pojo; + } + return result; + } + + @Override + public List bindFacetValues() { + List facets = bindFacetValues(entity.getFacets()); + facets.addAll( + assignments + .values() + .stream() + .distinct() + .filter(o -> o != null) + .collect(Collectors.toList())); + return facets; + } + + @Override + public List getFacets() { + if (entity != null) { + return entity.getFacets(); + } else { + return new ArrayList(); + } + } } diff --git a/src/main/java/net/helenus/core/reflect/DefaultPrimitiveTypes.java b/src/main/java/net/helenus/core/reflect/DefaultPrimitiveTypes.java index e2a05ad..dd778bd 100644 --- a/src/main/java/net/helenus/core/reflect/DefaultPrimitiveTypes.java +++ b/src/main/java/net/helenus/core/reflect/DefaultPrimitiveTypes.java @@ -19,34 +19,41 @@ import java.util.HashMap; import java.util.Map; public enum DefaultPrimitiveTypes { - BOOLEAN(boolean.class, false), BYTE(byte.class, (byte) 0x0), CHAR(char.class, (char) 0x0), SHORT(short.class, - (short) 0), INT(int.class, 0), LONG(long.class, 0L), FLOAT(float.class, 0.0f), DOUBLE(double.class, 0.0); + BOOLEAN(boolean.class, false), + BYTE(byte.class, (byte) 0x0), + CHAR(char.class, (char) 0x0), + SHORT(short.class, (short) 0), + INT(int.class, 0), + LONG(long.class, 0L), + FLOAT(float.class, 0.0f), + DOUBLE(double.class, 0.0); - private static final Map, DefaultPrimitiveTypes> map = new HashMap, DefaultPrimitiveTypes>(); + private static final Map, DefaultPrimitiveTypes> map = + new HashMap, DefaultPrimitiveTypes>(); - static { - for (DefaultPrimitiveTypes type : DefaultPrimitiveTypes.values()) { - map.put(type.getPrimitiveClass(), type); - } - } + static { + for (DefaultPrimitiveTypes type : DefaultPrimitiveTypes.values()) { + map.put(type.getPrimitiveClass(), type); + } + } - private final Class primitiveClass; - private final Object defaultValue; + private final Class primitiveClass; + private final Object defaultValue; - private DefaultPrimitiveTypes(Class primitiveClass, Object defaultValue) { - this.primitiveClass = primitiveClass; - this.defaultValue = defaultValue; - } + private DefaultPrimitiveTypes(Class primitiveClass, Object defaultValue) { + this.primitiveClass = primitiveClass; + this.defaultValue = defaultValue; + } - public static DefaultPrimitiveTypes lookup(Class primitiveClass) { - return map.get(primitiveClass); - } + public static DefaultPrimitiveTypes lookup(Class primitiveClass) { + return map.get(primitiveClass); + } - public Class getPrimitiveClass() { - return primitiveClass; - } + public Class getPrimitiveClass() { + return primitiveClass; + } - public Object getDefaultValue() { - return defaultValue; - } + public Object getDefaultValue() { + return defaultValue; + } } diff --git a/src/main/java/net/helenus/core/reflect/Drafted.java b/src/main/java/net/helenus/core/reflect/Drafted.java index 52810b6..17196b8 100644 --- a/src/main/java/net/helenus/core/reflect/Drafted.java +++ b/src/main/java/net/helenus/core/reflect/Drafted.java @@ -19,7 +19,7 @@ import java.util.Set; public interface Drafted extends MapExportable { - Set mutated(); + Set mutated(); - T build(); + T build(); } diff --git a/src/main/java/net/helenus/core/reflect/DslExportable.java b/src/main/java/net/helenus/core/reflect/DslExportable.java index fbe7cbc..df9597b 100644 --- a/src/main/java/net/helenus/core/reflect/DslExportable.java +++ b/src/main/java/net/helenus/core/reflect/DslExportable.java @@ -16,18 +16,17 @@ package net.helenus.core.reflect; import com.datastax.driver.core.Metadata; - import net.helenus.mapping.HelenusEntity; public interface DslExportable { - String GET_ENTITY_METHOD = "getHelenusMappingEntity"; - String GET_PARENT_METHOD = "getParentDslHelenusPropertyNode"; - String SET_METADATA_METHOD = "setCassandraMetadataForHelenusSession"; + String GET_ENTITY_METHOD = "getHelenusMappingEntity"; + String GET_PARENT_METHOD = "getParentDslHelenusPropertyNode"; + String SET_METADATA_METHOD = "setCassandraMetadataForHelenusSession"; - HelenusEntity getHelenusMappingEntity(); + HelenusEntity getHelenusMappingEntity(); - HelenusPropertyNode getParentDslHelenusPropertyNode(); + HelenusPropertyNode getParentDslHelenusPropertyNode(); - void setCassandraMetadataForHelenusSession(Metadata metadata); + void setCassandraMetadataForHelenusSession(Metadata metadata); } diff --git a/src/main/java/net/helenus/core/reflect/DslInvocationHandler.java b/src/main/java/net/helenus/core/reflect/DslInvocationHandler.java index f1eeb03..641edf2 100644 --- a/src/main/java/net/helenus/core/reflect/DslInvocationHandler.java +++ b/src/main/java/net/helenus/core/reflect/DslInvocationHandler.java @@ -15,6 +15,7 @@ */ package net.helenus.core.reflect; +import com.datastax.driver.core.*; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; @@ -22,9 +23,6 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Optional; - -import com.datastax.driver.core.*; - import net.helenus.core.Helenus; import net.helenus.mapping.HelenusEntity; import net.helenus.mapping.HelenusMappingEntity; @@ -37,164 +35,178 @@ import net.helenus.support.HelenusException; public class DslInvocationHandler implements InvocationHandler { - private final Class iface; - private final ClassLoader classLoader; - private final Optional parent; - private final Map map = new HashMap(); - private final Map udtMap = new HashMap(); - private final Map tupleMap = new HashMap(); - private HelenusEntity entity = null; - private Metadata metadata = null; + private final Class iface; + private final ClassLoader classLoader; + private final Optional parent; + private final Map map = new HashMap(); + private final Map udtMap = new HashMap(); + private final Map tupleMap = new HashMap(); + private HelenusEntity entity = null; + private Metadata metadata = null; - public DslInvocationHandler(Class iface, ClassLoader classLoader, Optional parent, - Metadata metadata) { + public DslInvocationHandler( + Class iface, + ClassLoader classLoader, + Optional parent, + Metadata metadata) { - this.metadata = metadata; - this.parent = parent; - this.iface = iface; - this.classLoader = classLoader; - } + this.metadata = metadata; + this.parent = parent; + this.iface = iface; + this.classLoader = classLoader; + } - public void setCassandraMetadataForHelenusSession(Metadata metadata) { - if (metadata != null) { - this.metadata = metadata; - entity = init(metadata); - } - } + public void setCassandraMetadataForHelenusSession(Metadata metadata) { + if (metadata != null) { + this.metadata = metadata; + entity = init(metadata); + } + } - private HelenusEntity init(Metadata metadata) { - HelenusEntity entity = new HelenusMappingEntity(iface, metadata); - Collection properties = entity.getOrderedProperties(); - if (properties != null) { - for (HelenusProperty prop : properties) { + private HelenusEntity init(Metadata metadata) { + HelenusEntity entity = new HelenusMappingEntity(iface, metadata); + Collection properties = entity.getOrderedProperties(); + if (properties != null) { + for (HelenusProperty prop : properties) { - 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; - } + return entity; + } - @Override - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - HelenusEntity entity = this.entity; - String methodName = method.getName(); + HelenusEntity entity = this.entity; + String methodName = method.getName(); - if ("equals".equals(methodName) && method.getParameterCount() == 1) { - Object otherObj = args[0]; - if (otherObj == null) { - return false; - } - if (Proxy.isProxyClass(otherObj.getClass())) { - return this == Proxy.getInvocationHandler(otherObj); - } - return false; - } + if ("equals".equals(methodName) && method.getParameterCount() == 1) { + Object otherObj = args[0]; + if (otherObj == null) { + return false; + } + if (Proxy.isProxyClass(otherObj.getClass())) { + return this == Proxy.getInvocationHandler(otherObj); + } + return false; + } - if (DslExportable.SET_METADATA_METHOD.equals(methodName) && args.length == 1 && args[0] instanceof Metadata) { - if (metadata == null) { - this.setCassandraMetadataForHelenusSession((Metadata) args[0]); - } - return null; - } + if (DslExportable.SET_METADATA_METHOD.equals(methodName) + && args.length == 1 + && args[0] instanceof Metadata) { + if (metadata == null) { + this.setCassandraMetadataForHelenusSession((Metadata) args[0]); + } + return null; + } - if (method.getParameterCount() != 0 || method.getReturnType() == void.class) { - throw new HelenusException("invalid getter method " + method); - } + if (method.getParameterCount() != 0 || method.getReturnType() == void.class) { + throw new HelenusException("invalid getter method " + method); + } - if ("hashCode".equals(methodName)) { - return hashCode(); - } + if ("hashCode".equals(methodName)) { + return hashCode(); + } - if (DslExportable.GET_PARENT_METHOD.equals(methodName)) { - return parent.get(); - } + if (DslExportable.GET_PARENT_METHOD.equals(methodName)) { + return parent.get(); + } - if (entity == null) { - entity = init(metadata); - } + if (entity == null) { + entity = init(metadata); + } - if ("toString".equals(methodName)) { - return entity.toString(); - } + if ("toString".equals(methodName)) { + return entity.toString(); + } - if (DslExportable.GET_ENTITY_METHOD.equals(methodName)) { - return entity; - } + if (DslExportable.GET_ENTITY_METHOD.equals(methodName)) { + return entity; + } - HelenusProperty prop = map.get(method); - if (prop == null) { - prop = entity.getProperty(methodName); - } + HelenusProperty prop = map.get(method); + if (prop == null) { + prop = entity.getProperty(methodName); + } - if (prop != null) { + if (prop != null) { - AbstractDataType type = prop.getDataType(); + AbstractDataType type = prop.getDataType(); - if (type instanceof UDTDataType) { + if (type instanceof UDTDataType) { - Object childDsl = udtMap.get(method); + Object childDsl = udtMap.get(method); - if (childDsl != null) { - return childDsl; - } - } + if (childDsl != null) { + return childDsl; + } + } - if (type instanceof DTDataType) { - DTDataType dataType = (DTDataType) type; - DataType dt = dataType.getDataType(); + if (type instanceof DTDataType) { + DTDataType dataType = (DTDataType) type; + DataType dt = dataType.getDataType(); - switch (dt.getName()) { - case TUPLE : - Object childDsl = tupleMap.get(method); + switch (dt.getName()) { + case TUPLE: + Object childDsl = tupleMap.get(method); - if (childDsl != null) { - return childDsl; - } + if (childDsl != null) { + return childDsl; + } - break; + break; - case SET : - return new SetDsl(new HelenusPropertyNode(prop, parent)); + case SET: + return new SetDsl(new HelenusPropertyNode(prop, parent)); - case LIST : - return new ListDsl(new HelenusPropertyNode(prop, parent)); + case LIST: + return new ListDsl(new HelenusPropertyNode(prop, parent)); - case MAP : - return new MapDsl(new HelenusPropertyNode(prop, parent)); + case MAP: + return new MapDsl(new HelenusPropertyNode(prop, parent)); - default : - break; - } - } + default: + break; + } + } - throw new DslPropertyException(new HelenusPropertyNode(prop, parent)); - } + throw new DslPropertyException(new HelenusPropertyNode(prop, parent)); + } - throw new HelenusException("invalid method call " + method); - } + throw new HelenusException("invalid method call " + method); + } } diff --git a/src/main/java/net/helenus/core/reflect/HelenusNamedProperty.java b/src/main/java/net/helenus/core/reflect/HelenusNamedProperty.java index 93bfdf2..9c4e448 100644 --- a/src/main/java/net/helenus/core/reflect/HelenusNamedProperty.java +++ b/src/main/java/net/helenus/core/reflect/HelenusNamedProperty.java @@ -19,9 +19,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.Optional; import java.util.function.Function; - import javax.validation.ConstraintValidator; - import net.helenus.core.SessionRepository; import net.helenus.mapping.*; import net.helenus.mapping.type.AbstractDataType; @@ -29,79 +27,79 @@ import net.helenus.support.HelenusMappingException; public final class HelenusNamedProperty implements HelenusProperty { - private final String name; + private final String name; - public HelenusNamedProperty(String name) { - this.name = name; - } + public HelenusNamedProperty(String name) { + this.name = name; + } - @Override - public HelenusEntity getEntity() { - throw new HelenusMappingException("will never called"); - } + @Override + public HelenusEntity getEntity() { + throw new HelenusMappingException("will never called"); + } - @Override - public String getPropertyName() { - return name; - } + @Override + public String getPropertyName() { + return name; + } - @Override - public Method getGetterMethod() { - throw new HelenusMappingException("will never called"); - } + @Override + public Method getGetterMethod() { + throw new HelenusMappingException("will never called"); + } - @Override - public IdentityName getColumnName() { - return IdentityName.of(name, false); - } + @Override + public IdentityName getColumnName() { + return IdentityName.of(name, false); + } - @Override - public Optional getIndexName() { - return Optional.empty(); - } + @Override + public Optional getIndexName() { + return Optional.empty(); + } - @Override - public boolean caseSensitiveIndex() { - return false; - } + @Override + public boolean caseSensitiveIndex() { + return false; + } - @Override - public Class getJavaType() { - throw new HelenusMappingException("will never called"); - } + @Override + public Class getJavaType() { + throw new HelenusMappingException("will never called"); + } - @Override - public AbstractDataType getDataType() { - throw new HelenusMappingException("will never called"); - } + @Override + public AbstractDataType getDataType() { + throw new HelenusMappingException("will never called"); + } - @Override - public ColumnType getColumnType() { - return ColumnType.COLUMN; - } + @Override + public ColumnType getColumnType() { + return ColumnType.COLUMN; + } - @Override - public int getOrdinal() { - return 0; - } + @Override + public int getOrdinal() { + return 0; + } - @Override - public OrderingDirection getOrdering() { - return OrderingDirection.ASC; - } + @Override + public OrderingDirection getOrdering() { + return OrderingDirection.ASC; + } - @Override - public Optional> getReadConverter(SessionRepository repository) { - return Optional.empty(); - } + @Override + public Optional> getReadConverter(SessionRepository repository) { + return Optional.empty(); + } - @Override - public Optional> getWriteConverter(SessionRepository repository) { - return Optional.empty(); - } + @Override + public Optional> getWriteConverter(SessionRepository repository) { + return Optional.empty(); + } - @Override - public ConstraintValidator[] getValidators() { - return MappingUtil.EMPTY_VALIDATORS; - } + @Override + public ConstraintValidator[] getValidators() { + return MappingUtil.EMPTY_VALIDATORS; + } } diff --git a/src/main/java/net/helenus/core/reflect/HelenusPropertyNode.java b/src/main/java/net/helenus/core/reflect/HelenusPropertyNode.java index 0c28931..b148433 100644 --- a/src/main/java/net/helenus/core/reflect/HelenusPropertyNode.java +++ b/src/main/java/net/helenus/core/reflect/HelenusPropertyNode.java @@ -17,90 +17,89 @@ package net.helenus.core.reflect; import java.util.*; import java.util.stream.Collectors; - import net.helenus.mapping.HelenusEntity; import net.helenus.mapping.HelenusProperty; public final class HelenusPropertyNode implements Iterable { - private final HelenusProperty prop; - private final Optional next; + private final HelenusProperty prop; + private final Optional next; - public HelenusPropertyNode(HelenusProperty prop, Optional next) { - this.prop = prop; - this.next = next; - } + public HelenusPropertyNode(HelenusProperty prop, Optional next) { + this.prop = prop; + this.next = next; + } - public String getColumnName() { - if (next.isPresent()) { + public String getColumnName() { + if (next.isPresent()) { - List columnNames = new ArrayList(); - for (HelenusProperty p : this) { - columnNames.add(p.getColumnName().toCql(true)); - } - Collections.reverse(columnNames); + List columnNames = new ArrayList(); + for (HelenusProperty p : this) { + columnNames.add(p.getColumnName().toCql(true)); + } + Collections.reverse(columnNames); - if (prop instanceof HelenusNamedProperty) { - int size = columnNames.size(); - StringBuilder str = new StringBuilder(); - for (int i = 0; i != size - 1; ++i) { - if (str.length() != 0) { - str.append("."); - } - str.append(columnNames.get(i)); - } - str.append("[").append(columnNames.get(size - 1)).append("]"); - return str.toString(); - } else { - return columnNames.stream().collect(Collectors.joining(".")); - } - } else { - return prop.getColumnName().toCql(); - } - } + if (prop instanceof HelenusNamedProperty) { + int size = columnNames.size(); + StringBuilder str = new StringBuilder(); + for (int i = 0; i != size - 1; ++i) { + if (str.length() != 0) { + str.append("."); + } + str.append(columnNames.get(i)); + } + str.append("[").append(columnNames.get(size - 1)).append("]"); + return str.toString(); + } else { + return columnNames.stream().collect(Collectors.joining(".")); + } + } else { + return prop.getColumnName().toCql(); + } + } - public HelenusEntity getEntity() { - if (next.isPresent()) { - HelenusProperty last = prop; - for (HelenusProperty p : this) { - last = p; - } - return last.getEntity(); - } else { - return prop.getEntity(); - } - } + public HelenusEntity getEntity() { + if (next.isPresent()) { + HelenusProperty last = prop; + for (HelenusProperty p : this) { + last = p; + } + return last.getEntity(); + } else { + return prop.getEntity(); + } + } - public HelenusProperty getProperty() { - return prop; - } + public HelenusProperty getProperty() { + return prop; + } - public Optional getNext() { - return next; - } + public Optional getNext() { + return next; + } - public Iterator iterator() { - return new PropertyNodeIterator(Optional.of(this)); - } + public Iterator iterator() { + return new PropertyNodeIterator(Optional.of(this)); + } - private static class PropertyNodeIterator implements Iterator { + private static class PropertyNodeIterator implements Iterator { - private Optional next; + private Optional next; - public PropertyNodeIterator(Optional next) { - this.next = next; - } + public PropertyNodeIterator(Optional next) { + this.next = next; + } - @Override - public boolean hasNext() { - return next.isPresent(); - } + @Override + public boolean hasNext() { + return next.isPresent(); + } - @Override - public HelenusProperty next() { - HelenusPropertyNode node = next.get(); - next = node.next; - return node.prop; - } - } + @Override + public HelenusProperty next() { + HelenusPropertyNode node = next.get(); + next = node.next; + return node.prop; + } + } } diff --git a/src/main/java/net/helenus/core/reflect/ListDsl.java b/src/main/java/net/helenus/core/reflect/ListDsl.java index 3a8755f..b834098 100644 --- a/src/main/java/net/helenus/core/reflect/ListDsl.java +++ b/src/main/java/net/helenus/core/reflect/ListDsl.java @@ -16,165 +16,164 @@ package net.helenus.core.reflect; import java.util.*; - import net.helenus.mapping.HelenusProperty; import net.helenus.support.DslPropertyException; import net.helenus.support.HelenusMappingException; public final class ListDsl implements List { - private final HelenusPropertyNode parent; + private final HelenusPropertyNode parent; - public ListDsl(HelenusPropertyNode parent) { - this.parent = parent; - } + public ListDsl(HelenusPropertyNode parent) { + this.parent = parent; + } - public HelenusPropertyNode getParent() { - return parent; - } + public HelenusPropertyNode getParent() { + return parent; + } - @Override - public V get(int index) { - HelenusProperty prop = new HelenusNamedProperty(Integer.toString(index)); - throw new DslPropertyException(new HelenusPropertyNode(prop, Optional.of(parent))); - } + @Override + public V get(int index) { + HelenusProperty prop = new HelenusNamedProperty(Integer.toString(index)); + throw new DslPropertyException(new HelenusPropertyNode(prop, Optional.of(parent))); + } - @Override - public int size() { - throwShouldNeverCall(); - return 0; - } + @Override + public int size() { + throwShouldNeverCall(); + return 0; + } - @Override - public boolean isEmpty() { - throwShouldNeverCall(); - return false; - } + @Override + public boolean isEmpty() { + throwShouldNeverCall(); + return false; + } - @Override - public boolean contains(Object o) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean contains(Object o) { + throwShouldNeverCall(); + return false; + } - @Override - public Iterator iterator() { - throwShouldNeverCall(); - return null; - } + @Override + public Iterator iterator() { + throwShouldNeverCall(); + return null; + } - @Override - public Object[] toArray() { - throwShouldNeverCall(); - return null; - } + @Override + public Object[] toArray() { + throwShouldNeverCall(); + return null; + } - @Override - public T[] toArray(T[] a) { - throwShouldNeverCall(); - return null; - } + @Override + public T[] toArray(T[] a) { + throwShouldNeverCall(); + return null; + } - @Override - public boolean add(V e) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean add(V e) { + throwShouldNeverCall(); + return false; + } - @Override - public boolean remove(Object o) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean remove(Object o) { + throwShouldNeverCall(); + return false; + } - @Override - public boolean containsAll(Collection c) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean containsAll(Collection c) { + throwShouldNeverCall(); + return false; + } - @Override - public boolean addAll(Collection c) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean addAll(Collection c) { + throwShouldNeverCall(); + return false; + } - @Override - public boolean addAll(int index, Collection c) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean addAll(int index, Collection c) { + throwShouldNeverCall(); + return false; + } - @Override - public boolean removeAll(Collection c) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean removeAll(Collection c) { + throwShouldNeverCall(); + return false; + } - @Override - public boolean retainAll(Collection c) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean retainAll(Collection c) { + throwShouldNeverCall(); + return false; + } - @Override - public void clear() { - throwShouldNeverCall(); - } + @Override + public void clear() { + throwShouldNeverCall(); + } - @Override - public V set(int index, V element) { - throwShouldNeverCall(); - return null; - } + @Override + public V set(int index, V element) { + throwShouldNeverCall(); + return null; + } - @Override - public void add(int index, V element) { - throwShouldNeverCall(); - } + @Override + public void add(int index, V element) { + throwShouldNeverCall(); + } - @Override - public V remove(int index) { - throwShouldNeverCall(); - return null; - } + @Override + public V remove(int index) { + throwShouldNeverCall(); + return null; + } - @Override - public int indexOf(Object o) { - throwShouldNeverCall(); - return 0; - } + @Override + public int indexOf(Object o) { + throwShouldNeverCall(); + return 0; + } - @Override - public int lastIndexOf(Object o) { - throwShouldNeverCall(); - return 0; - } + @Override + public int lastIndexOf(Object o) { + throwShouldNeverCall(); + return 0; + } - @Override - public ListIterator listIterator() { - throwShouldNeverCall(); - return null; - } + @Override + public ListIterator listIterator() { + throwShouldNeverCall(); + return null; + } - @Override - public ListIterator listIterator(int index) { - throwShouldNeverCall(); - return null; - } + @Override + public ListIterator listIterator(int index) { + throwShouldNeverCall(); + return null; + } - @Override - public List subList(int fromIndex, int toIndex) { - throwShouldNeverCall(); - return null; - } + @Override + public List subList(int fromIndex, int toIndex) { + throwShouldNeverCall(); + return null; + } - private void throwShouldNeverCall() { - throw new HelenusMappingException("should be never called"); - } + private void throwShouldNeverCall() { + throw new HelenusMappingException("should be never called"); + } - @Override - public String toString() { - return "ListDsl"; - } + @Override + public String toString() { + return "ListDsl"; + } } diff --git a/src/main/java/net/helenus/core/reflect/MapDsl.java b/src/main/java/net/helenus/core/reflect/MapDsl.java index c6d4cc6..7c1828d 100644 --- a/src/main/java/net/helenus/core/reflect/MapDsl.java +++ b/src/main/java/net/helenus/core/reflect/MapDsl.java @@ -19,99 +19,98 @@ import java.util.Collection; import java.util.Map; import java.util.Optional; import java.util.Set; - import net.helenus.mapping.HelenusProperty; import net.helenus.support.DslPropertyException; import net.helenus.support.HelenusMappingException; public final class MapDsl implements Map { - private final HelenusPropertyNode parent; + private final HelenusPropertyNode parent; - public MapDsl(HelenusPropertyNode parent) { - this.parent = parent; - } + public MapDsl(HelenusPropertyNode parent) { + this.parent = parent; + } - public HelenusPropertyNode getParent() { - return parent; - } + public HelenusPropertyNode getParent() { + return parent; + } - @Override - public V get(Object key) { - HelenusProperty prop = new HelenusNamedProperty(key.toString()); - throw new DslPropertyException(new HelenusPropertyNode(prop, Optional.of(parent))); - } + @Override + public V get(Object key) { + HelenusProperty prop = new HelenusNamedProperty(key.toString()); + throw new DslPropertyException(new HelenusPropertyNode(prop, Optional.of(parent))); + } - @Override - public int size() { - throwShouldNeverCall(); - return 0; - } + @Override + public int size() { + throwShouldNeverCall(); + return 0; + } - @Override - public boolean isEmpty() { - throwShouldNeverCall(); - return false; - } + @Override + public boolean isEmpty() { + throwShouldNeverCall(); + return false; + } - @Override - public boolean containsKey(Object key) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean containsKey(Object key) { + throwShouldNeverCall(); + return false; + } - @Override - public boolean containsValue(Object value) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean containsValue(Object value) { + throwShouldNeverCall(); + return false; + } - @Override - public V put(K key, V value) { - throwShouldNeverCall(); - return null; - } + @Override + public V put(K key, V value) { + throwShouldNeverCall(); + return null; + } - @Override - public V remove(Object key) { - throwShouldNeverCall(); - return null; - } + @Override + public V remove(Object key) { + throwShouldNeverCall(); + return null; + } - @Override - public void putAll(Map m) { - throwShouldNeverCall(); - } + @Override + public void putAll(Map m) { + throwShouldNeverCall(); + } - @Override - public void clear() { - throwShouldNeverCall(); - } + @Override + public void clear() { + throwShouldNeverCall(); + } - @Override - public Set keySet() { - throwShouldNeverCall(); - return null; - } + @Override + public Set keySet() { + throwShouldNeverCall(); + return null; + } - @Override - public Collection values() { - throwShouldNeverCall(); - return null; - } + @Override + public Collection values() { + throwShouldNeverCall(); + return null; + } - @Override - public Set> entrySet() { - throwShouldNeverCall(); - return null; - } + @Override + public Set> entrySet() { + throwShouldNeverCall(); + return null; + } - private void throwShouldNeverCall() { - throw new HelenusMappingException("should be never called"); - } + private void throwShouldNeverCall() { + throw new HelenusMappingException("should be never called"); + } - @Override - public String toString() { - return "MapDsl"; - } + @Override + public String toString() { + return "MapDsl"; + } } diff --git a/src/main/java/net/helenus/core/reflect/MapExportable.java b/src/main/java/net/helenus/core/reflect/MapExportable.java index 2e71bdf..9160cc8 100644 --- a/src/main/java/net/helenus/core/reflect/MapExportable.java +++ b/src/main/java/net/helenus/core/reflect/MapExportable.java @@ -19,7 +19,7 @@ import java.util.Map; public interface MapExportable { - public static final String TO_MAP_METHOD = "toMap"; + public static final String TO_MAP_METHOD = "toMap"; - Map toMap(); + Map toMap(); } diff --git a/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java b/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java index cbff40b..e33291a 100644 --- a/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java +++ b/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java @@ -27,155 +27,161 @@ import java.lang.reflect.Proxy; import java.util.HashMap; import java.util.Map; import java.util.Set; - import net.helenus.core.Helenus; import net.helenus.mapping.annotation.Transient; import net.helenus.mapping.value.ValueProviderMap; import net.helenus.support.HelenusException; public class MapperInvocationHandler implements InvocationHandler, Serializable { - private static final long serialVersionUID = -7044209982830584984L; + private static final long serialVersionUID = -7044209982830584984L; - private final Map src; - private final Class iface; + private final Map src; + private final Class iface; - public MapperInvocationHandler(Class iface, Map src) { - this.src = src; - this.iface = iface; - } + public MapperInvocationHandler(Class iface, Map src) { + this.src = src; + this.iface = iface; + } - private Object invokeDefault(Object proxy, Method method, Object[] args) throws Throwable { - // NOTE: This is reflection magic to invoke (non-recursively) a default method - // implemented on an interface - // that we've proxied (in ReflectionDslInstantiator). I found the answer in this - // article. - // https://zeroturnaround.com/rebellabs/recognize-and-conquer-java-proxies-default-methods-and-method-handles/ + private Object invokeDefault(Object proxy, Method method, Object[] args) throws Throwable { + // NOTE: This is reflection magic to invoke (non-recursively) a default method + // implemented on an interface + // that we've proxied (in ReflectionDslInstantiator). I found the answer in this + // article. + // https://zeroturnaround.com/rebellabs/recognize-and-conquer-java-proxies-default-methods-and-method-handles/ - // First, we need an instance of a private inner-class found in MethodHandles. - Constructor constructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, - int.class); - constructor.setAccessible(true); + // First, we need an instance of a private inner-class found in MethodHandles. + Constructor constructor = + MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, int.class); + constructor.setAccessible(true); - // Now we need to lookup and invoke special the default method on the interface - // class. - final Class declaringClass = method.getDeclaringClass(); - Object result = constructor.newInstance(declaringClass, MethodHandles.Lookup.PRIVATE) - .unreflectSpecial(method, declaringClass).bindTo(proxy).invokeWithArguments(args); - return result; - } + // Now we need to lookup and invoke special the default method on the interface + // class. + final Class declaringClass = method.getDeclaringClass(); + Object result = + constructor + .newInstance(declaringClass, MethodHandles.Lookup.PRIVATE) + .unreflectSpecial(method, declaringClass) + .bindTo(proxy) + .invokeWithArguments(args); + return result; + } - private Object writeReplace() { - return new SerializationProxy(this); - } - private void readObject(ObjectInputStream stream) throws InvalidObjectException { - throw new InvalidObjectException("Proxy required."); - } + private Object writeReplace() { + return new SerializationProxy(this); + } - @Override - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + private void readObject(ObjectInputStream stream) throws InvalidObjectException { + throw new InvalidObjectException("Proxy required."); + } - // Transient, default methods should simply be invoked as-is. - if (method.isDefault() && method.getDeclaredAnnotation(Transient.class) != null) { - return invokeDefault(proxy, method, args); - } + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - String methodName = method.getName(); + // Transient, default methods should simply be invoked as-is. + if (method.isDefault() && method.getDeclaredAnnotation(Transient.class) != null) { + return invokeDefault(proxy, method, args); + } - if ("equals".equals(methodName) && method.getParameterCount() == 1) { - Object otherObj = args[0]; - if (otherObj == null) { - return false; - } - if (Proxy.isProxyClass(otherObj.getClass())) { - if (this == Proxy.getInvocationHandler(otherObj)) { - return true; - } - } - if (otherObj instanceof MapExportable && src.equals(((MapExportable) otherObj).toMap())) { - return true; - } - return false; - } + String methodName = method.getName(); - if (method.getParameterCount() != 0 || method.getReturnType() == void.class) { - throw new HelenusException("invalid getter method " + method); - } + if ("equals".equals(methodName) && method.getParameterCount() == 1) { + Object otherObj = args[0]; + if (otherObj == null) { + return false; + } + if (Proxy.isProxyClass(otherObj.getClass())) { + if (this == Proxy.getInvocationHandler(otherObj)) { + return true; + } + } + if (otherObj instanceof MapExportable && src.equals(((MapExportable) otherObj).toMap())) { + return true; + } + if (src instanceof MapExportable && otherObj.equals(((MapExportable) src).toMap())) { + return true; + } + return false; + } - if ("hashCode".equals(methodName)) { - return hashCode(); - } + if (method.getParameterCount() != 0 || method.getReturnType() == void.class) { + throw new HelenusException("invalid getter method " + method); + } - if ("toString".equals(methodName)) { - return iface.getSimpleName() + ": " + src.toString(); - } + if ("hashCode".equals(methodName)) { + return hashCode(); + } - if ("writeReplace".equals(methodName)) { - return new SerializationProxy(this); - } + if ("toString".equals(methodName)) { + return iface.getSimpleName() + ": " + src.toString(); + } - if ("readObject".equals(methodName)) { - throw new InvalidObjectException("Proxy required."); - } + if ("writeReplace".equals(methodName)) { + return new SerializationProxy(this); + } - if ("dsl".equals(methodName)) { - return Helenus.dsl(iface); - } + if ("readObject".equals(methodName)) { + throw new InvalidObjectException("Proxy required."); + } - if (MapExportable.TO_MAP_METHOD.equals(methodName)) { - return src; // return Collections.unmodifiableMap(src); - } + if ("dsl".equals(methodName)) { + return Helenus.dsl(iface); + } - Object value = src.get(methodName); + if (MapExportable.TO_MAP_METHOD.equals(methodName)) { + return src; // Collections.unmodifiableMap(src); + } - Class returnType = method.getReturnType(); + Object value = src.get(methodName); - if (value == null) { + Class returnType = method.getReturnType(); - // Default implementations of non-Transient methods in entities are the default - // value when the - // map contains 'null'. - if (method.isDefault()) { - return invokeDefault(proxy, method, args); - } + if (value == null) { - // Otherwise, if the return type of the method is a primitive Java type then - // we'll return the standard - // default values to avoid a NPE in user code. - if (returnType.isPrimitive()) { - DefaultPrimitiveTypes type = DefaultPrimitiveTypes.lookup(returnType); - if (type == null) { - throw new HelenusException("unknown primitive type " + returnType); - } - return type.getDefaultValue(); - } - } + // Default implementations of non-Transient methods in entities are the default + // value when the + // map contains 'null'. + if (method.isDefault()) { + return invokeDefault(proxy, method, args); + } - return value; - } + // Otherwise, if the return type of the method is a primitive Java type then + // we'll return the standard + // default values to avoid a NPE in user code. + if (returnType.isPrimitive()) { + DefaultPrimitiveTypes type = DefaultPrimitiveTypes.lookup(returnType); + if (type == null) { + throw new HelenusException("unknown primitive type " + returnType); + } + return type.getDefaultValue(); + } + } - static class SerializationProxy implements Serializable { + return value; + } - private static final long serialVersionUID = -5617583940055969353L; + static class SerializationProxy implements Serializable { - private final Class iface; - private final Map src; + private static final long serialVersionUID = -5617583940055969353L; - public SerializationProxy(MapperInvocationHandler mapper) { - this.iface = mapper.iface; - if (mapper.src instanceof ValueProviderMap) { - this.src = new HashMap(mapper.src.size()); - Set keys = mapper.src.keySet(); - for (String key : keys) { - this.src.put(key, mapper.src.get(key)); - } - } else { - this.src = mapper.src; - } - } + private final Class iface; + private final Map src; - Object readResolve() throws ObjectStreamException { - return new MapperInvocationHandler(iface, src); - } + public SerializationProxy(MapperInvocationHandler mapper) { + this.iface = mapper.iface; + if (mapper.src instanceof ValueProviderMap) { + this.src = new HashMap(mapper.src.size()); + Set keys = mapper.src.keySet(); + for (String key : keys) { + this.src.put(key, mapper.src.get(key)); + } + } else { + this.src = mapper.src; + } + } - } + Object readResolve() throws ObjectStreamException { + return new MapperInvocationHandler(iface, src); + } + } } diff --git a/src/main/java/net/helenus/core/reflect/ReflectionDslInstantiator.java b/src/main/java/net/helenus/core/reflect/ReflectionDslInstantiator.java index 7e30435..7f1b218 100644 --- a/src/main/java/net/helenus/core/reflect/ReflectionDslInstantiator.java +++ b/src/main/java/net/helenus/core/reflect/ReflectionDslInstantiator.java @@ -15,22 +15,25 @@ */ package net.helenus.core.reflect; +import com.datastax.driver.core.Metadata; import java.lang.reflect.Proxy; import java.util.Optional; - -import com.datastax.driver.core.Metadata; - import net.helenus.core.DslInstantiator; public enum ReflectionDslInstantiator implements DslInstantiator { - INSTANCE; + INSTANCE; - @Override - @SuppressWarnings("unchecked") - public E instantiate(Class iface, ClassLoader classLoader, Optional parent, - Metadata metadata) { - DslInvocationHandler handler = new DslInvocationHandler(iface, classLoader, parent, metadata); - E proxy = (E) Proxy.newProxyInstance(classLoader, new Class[]{iface, DslExportable.class}, handler); - return proxy; - } + @Override + @SuppressWarnings("unchecked") + public E instantiate( + Class iface, + ClassLoader classLoader, + Optional parent, + Metadata metadata) { + DslInvocationHandler handler = + new DslInvocationHandler(iface, classLoader, parent, metadata); + E proxy = + (E) Proxy.newProxyInstance(classLoader, new Class[] {iface, DslExportable.class}, handler); + return proxy; + } } diff --git a/src/main/java/net/helenus/core/reflect/ReflectionInstantiator.java b/src/main/java/net/helenus/core/reflect/ReflectionInstantiator.java index 748d88a..c7e40f8 100644 --- a/src/main/java/net/helenus/core/reflect/ReflectionInstantiator.java +++ b/src/main/java/net/helenus/core/reflect/ReflectionInstantiator.java @@ -19,15 +19,14 @@ import net.helenus.support.HelenusMappingException; public final class ReflectionInstantiator { - private ReflectionInstantiator() { - } + private ReflectionInstantiator() {} - public static T instantiateClass(Class clazz) { + public static T instantiateClass(Class clazz) { - try { - return clazz.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { - throw new HelenusMappingException("invalid class " + clazz, e); - } - } + try { + return clazz.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + throw new HelenusMappingException("invalid class " + clazz, e); + } + } } diff --git a/src/main/java/net/helenus/core/reflect/ReflectionMapperInstantiator.java b/src/main/java/net/helenus/core/reflect/ReflectionMapperInstantiator.java index 4ce43f8..5efc78d 100644 --- a/src/main/java/net/helenus/core/reflect/ReflectionMapperInstantiator.java +++ b/src/main/java/net/helenus/core/reflect/ReflectionMapperInstantiator.java @@ -18,19 +18,20 @@ package net.helenus.core.reflect; import java.io.Serializable; import java.lang.reflect.Proxy; import java.util.Map; - import net.helenus.core.MapperInstantiator; public enum ReflectionMapperInstantiator implements MapperInstantiator { - INSTANCE; + INSTANCE; - @Override - @SuppressWarnings("unchecked") - public E instantiate(Class iface, Map src, ClassLoader classLoader) { + @Override + @SuppressWarnings("unchecked") + public E instantiate(Class iface, Map src, ClassLoader classLoader) { - MapperInvocationHandler handler = new MapperInvocationHandler(iface, src); - E proxy = (E) Proxy.newProxyInstance(classLoader, new Class[]{iface, MapExportable.class, Serializable.class}, - handler); - return proxy; - } + MapperInvocationHandler handler = new MapperInvocationHandler(iface, src); + E proxy = + (E) + Proxy.newProxyInstance( + classLoader, new Class[] {iface, MapExportable.class, Serializable.class}, handler); + return proxy; + } } diff --git a/src/main/java/net/helenus/core/reflect/SetDsl.java b/src/main/java/net/helenus/core/reflect/SetDsl.java index 663b945..6a55118 100644 --- a/src/main/java/net/helenus/core/reflect/SetDsl.java +++ b/src/main/java/net/helenus/core/reflect/SetDsl.java @@ -18,104 +18,103 @@ package net.helenus.core.reflect; import java.util.Collection; import java.util.Iterator; import java.util.Set; - import net.helenus.support.HelenusMappingException; public final class SetDsl implements Set { - private final HelenusPropertyNode parent; + private final HelenusPropertyNode parent; - public SetDsl(HelenusPropertyNode parent) { - this.parent = parent; - } + public SetDsl(HelenusPropertyNode parent) { + this.parent = parent; + } - public HelenusPropertyNode getParent() { - return parent; - } + public HelenusPropertyNode getParent() { + return parent; + } - @Override - public int size() { - throwShouldNeverCall(); - return 0; - } + @Override + public int size() { + throwShouldNeverCall(); + return 0; + } - @Override - public boolean isEmpty() { - throwShouldNeverCall(); - return false; - } + @Override + public boolean isEmpty() { + throwShouldNeverCall(); + return false; + } - @Override - public boolean contains(Object o) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean contains(Object o) { + throwShouldNeverCall(); + return false; + } - @Override - public Iterator iterator() { - throwShouldNeverCall(); - return null; - } + @Override + public Iterator iterator() { + throwShouldNeverCall(); + return null; + } - @Override - public Object[] toArray() { - throwShouldNeverCall(); - return null; - } + @Override + public Object[] toArray() { + throwShouldNeverCall(); + return null; + } - @Override - public T[] toArray(T[] a) { - throwShouldNeverCall(); - return null; - } + @Override + public T[] toArray(T[] a) { + throwShouldNeverCall(); + return null; + } - @Override - public boolean add(V e) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean add(V e) { + throwShouldNeverCall(); + return false; + } - @Override - public boolean remove(Object o) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean remove(Object o) { + throwShouldNeverCall(); + return false; + } - @Override - public boolean containsAll(Collection c) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean containsAll(Collection c) { + throwShouldNeverCall(); + return false; + } - @Override - public boolean addAll(Collection c) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean addAll(Collection c) { + throwShouldNeverCall(); + return false; + } - @Override - public boolean retainAll(Collection c) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean retainAll(Collection c) { + throwShouldNeverCall(); + return false; + } - @Override - public boolean removeAll(Collection c) { - throwShouldNeverCall(); - return false; - } + @Override + public boolean removeAll(Collection c) { + throwShouldNeverCall(); + return false; + } - @Override - public void clear() { - throwShouldNeverCall(); - } + @Override + public void clear() { + throwShouldNeverCall(); + } - private void throwShouldNeverCall() { - throw new HelenusMappingException("should be never called"); - } + private void throwShouldNeverCall() { + throw new HelenusMappingException("should be never called"); + } - @Override - public String toString() { - return "SetDsl"; - } + @Override + public String toString() { + return "SetDsl"; + } } diff --git a/src/main/java/net/helenus/mapping/ColumnInformation.java b/src/main/java/net/helenus/mapping/ColumnInformation.java index 5ba9ddd..fa6d25f 100644 --- a/src/main/java/net/helenus/mapping/ColumnInformation.java +++ b/src/main/java/net/helenus/mapping/ColumnInformation.java @@ -16,7 +16,6 @@ package net.helenus.mapping; import java.lang.reflect.Method; - import net.helenus.mapping.annotation.ClusteringColumn; import net.helenus.mapping.annotation.Column; import net.helenus.mapping.annotation.PartitionKey; @@ -25,91 +24,99 @@ import net.helenus.support.HelenusMappingException; public final class ColumnInformation { - private final IdentityName columnName; - private final ColumnType columnType; - private final int ordinal; - private final OrderingDirection ordering; + private final IdentityName columnName; + private final ColumnType columnType; + private final int ordinal; + private final OrderingDirection ordering; - public ColumnInformation(Method getter) { + public ColumnInformation(Method getter) { - String columnName = null; - boolean forceQuote = false; - ColumnType columnTypeLocal = ColumnType.COLUMN; - int ordinalLocal = 0; - OrderingDirection orderingLocal = OrderingDirection.ASC; + String columnName = null; + boolean forceQuote = false; + ColumnType columnTypeLocal = ColumnType.COLUMN; + int ordinalLocal = 0; + OrderingDirection orderingLocal = OrderingDirection.ASC; - PartitionKey partitionKey = getter.getDeclaredAnnotation(PartitionKey.class); - if (partitionKey != null) { - columnName = partitionKey.value(); - forceQuote = partitionKey.forceQuote(); - columnTypeLocal = ColumnType.PARTITION_KEY; - ordinalLocal = partitionKey.ordinal(); - } + PartitionKey partitionKey = getter.getDeclaredAnnotation(PartitionKey.class); + if (partitionKey != null) { + columnName = partitionKey.value(); + forceQuote = partitionKey.forceQuote(); + columnTypeLocal = ColumnType.PARTITION_KEY; + ordinalLocal = partitionKey.ordinal(); + } - ClusteringColumn clusteringColumn = getter.getDeclaredAnnotation(ClusteringColumn.class); - if (clusteringColumn != null) { - ensureSingleColumnType(columnTypeLocal, getter); - columnName = clusteringColumn.value(); - forceQuote = clusteringColumn.forceQuote(); - columnTypeLocal = ColumnType.CLUSTERING_COLUMN; - ordinalLocal = clusteringColumn.ordinal(); - orderingLocal = clusteringColumn.ordering(); - } + ClusteringColumn clusteringColumn = getter.getDeclaredAnnotation(ClusteringColumn.class); + if (clusteringColumn != null) { + ensureSingleColumnType(columnTypeLocal, getter); + columnName = clusteringColumn.value(); + forceQuote = clusteringColumn.forceQuote(); + columnTypeLocal = ColumnType.CLUSTERING_COLUMN; + ordinalLocal = clusteringColumn.ordinal(); + orderingLocal = clusteringColumn.ordering(); + } - StaticColumn staticColumn = getter.getDeclaredAnnotation(StaticColumn.class); - if (staticColumn != null) { - ensureSingleColumnType(columnTypeLocal, getter); - columnName = staticColumn.value(); - forceQuote = staticColumn.forceQuote(); - columnTypeLocal = ColumnType.STATIC_COLUMN; - ordinalLocal = staticColumn.ordinal(); - } + StaticColumn staticColumn = getter.getDeclaredAnnotation(StaticColumn.class); + if (staticColumn != null) { + ensureSingleColumnType(columnTypeLocal, getter); + columnName = staticColumn.value(); + forceQuote = staticColumn.forceQuote(); + columnTypeLocal = ColumnType.STATIC_COLUMN; + ordinalLocal = staticColumn.ordinal(); + } - Column column = getter.getDeclaredAnnotation(Column.class); - if (column != null) { - ensureSingleColumnType(columnTypeLocal, getter); - columnName = column.value(); - forceQuote = column.forceQuote(); - columnTypeLocal = ColumnType.COLUMN; - ordinalLocal = column.ordinal(); - } + Column column = getter.getDeclaredAnnotation(Column.class); + if (column != null) { + ensureSingleColumnType(columnTypeLocal, getter); + columnName = column.value(); + forceQuote = column.forceQuote(); + columnTypeLocal = ColumnType.COLUMN; + ordinalLocal = column.ordinal(); + } - if (columnName == null || columnName.isEmpty()) { - columnName = MappingUtil.getDefaultColumnName(getter); - } + if (columnName == null || columnName.isEmpty()) { + columnName = MappingUtil.getDefaultColumnName(getter); + } - this.columnName = new IdentityName(columnName, forceQuote); - this.columnType = columnTypeLocal; - this.ordinal = ordinalLocal; - this.ordering = orderingLocal; - } + this.columnName = new IdentityName(columnName, forceQuote); + this.columnType = columnTypeLocal; + this.ordinal = ordinalLocal; + this.ordering = orderingLocal; + } - public IdentityName getColumnName() { - return columnName; - } + public IdentityName getColumnName() { + return columnName; + } - public ColumnType getColumnType() { - return columnType; - } + public ColumnType getColumnType() { + return columnType; + } - public int getOrdinal() { - return ordinal; - } + public int getOrdinal() { + return ordinal; + } - public OrderingDirection getOrdering() { - return ordering; - } + public OrderingDirection getOrdering() { + return ordering; + } - private void ensureSingleColumnType(ColumnType columnTypeLocal, Method getter) { + private void ensureSingleColumnType(ColumnType columnTypeLocal, Method getter) { - if (columnTypeLocal != ColumnType.COLUMN) { - throw new HelenusMappingException("property can be annotated only by a single column type " + getter); - } - } + if (columnTypeLocal != ColumnType.COLUMN) { + throw new HelenusMappingException( + "property can be annotated only by a single column type " + getter); + } + } - @Override - public String toString() { - return "ColumnInformation [columnName=" + columnName + ", columnType=" + columnType + ", ordinal=" + ordinal - + ", ordering=" + ordering + "]"; - } + @Override + public String toString() { + return "ColumnInformation [columnName=" + + columnName + + ", columnType=" + + columnType + + ", ordinal=" + + ordinal + + ", ordering=" + + ordering + + "]"; + } } diff --git a/src/main/java/net/helenus/mapping/ColumnType.java b/src/main/java/net/helenus/mapping/ColumnType.java index e56b079..eb8407d 100644 --- a/src/main/java/net/helenus/mapping/ColumnType.java +++ b/src/main/java/net/helenus/mapping/ColumnType.java @@ -16,5 +16,8 @@ package net.helenus.mapping; public enum ColumnType { - PARTITION_KEY, CLUSTERING_COLUMN, STATIC_COLUMN, COLUMN; + PARTITION_KEY, + CLUSTERING_COLUMN, + STATIC_COLUMN, + COLUMN; } diff --git a/src/main/java/net/helenus/mapping/HelenusEntity.java b/src/main/java/net/helenus/mapping/HelenusEntity.java index 3b99de5..b19fee6 100644 --- a/src/main/java/net/helenus/mapping/HelenusEntity.java +++ b/src/main/java/net/helenus/mapping/HelenusEntity.java @@ -17,22 +17,21 @@ package net.helenus.mapping; import java.util.Collection; import java.util.List; - import net.helenus.core.cache.Facet; public interface HelenusEntity { - HelenusEntityType getType(); + HelenusEntityType getType(); - boolean isCacheable(); + boolean isCacheable(); - Class getMappingInterface(); + Class getMappingInterface(); - IdentityName getName(); + IdentityName getName(); - Collection getOrderedProperties(); + Collection getOrderedProperties(); - HelenusProperty getProperty(String name); + HelenusProperty getProperty(String name); - List getFacets(); + List getFacets(); } diff --git a/src/main/java/net/helenus/mapping/HelenusEntityType.java b/src/main/java/net/helenus/mapping/HelenusEntityType.java index 0924ec7..2ef8d63 100644 --- a/src/main/java/net/helenus/mapping/HelenusEntityType.java +++ b/src/main/java/net/helenus/mapping/HelenusEntityType.java @@ -16,5 +16,8 @@ package net.helenus.mapping; public enum HelenusEntityType { - TABLE, VIEW, TUPLE, UDT; + TABLE, + VIEW, + TUPLE, + UDT; } diff --git a/src/main/java/net/helenus/mapping/HelenusMappingEntity.java b/src/main/java/net/helenus/mapping/HelenusMappingEntity.java index 5872598..cf343bb 100644 --- a/src/main/java/net/helenus/mapping/HelenusMappingEntity.java +++ b/src/main/java/net/helenus/mapping/HelenusMappingEntity.java @@ -15,18 +15,13 @@ */ package net.helenus.mapping; -import java.lang.reflect.Method; -import java.util.*; - -import javax.validation.ConstraintValidator; - -import org.apache.commons.lang3.ClassUtils; - import com.datastax.driver.core.DefaultMetadata; import com.datastax.driver.core.Metadata; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; - +import java.lang.reflect.Method; +import java.util.*; +import javax.validation.ConstraintValidator; import net.helenus.config.HelenusSettings; import net.helenus.core.Helenus; import net.helenus.core.annotation.Cacheable; @@ -35,278 +30,301 @@ import net.helenus.core.cache.UnboundFacet; import net.helenus.mapping.annotation.*; import net.helenus.mapping.validator.DistinctValidator; import net.helenus.support.HelenusMappingException; +import org.apache.commons.lang3.ClassUtils; public final class HelenusMappingEntity implements HelenusEntity { - private final Class iface; - private final HelenusEntityType type; - private final IdentityName name; - private final boolean cacheable; - private final ImmutableMap methods; - private final ImmutableMap props; - private final ImmutableList orderedProps; - private final List facets; + private final Class iface; + private final HelenusEntityType type; + private final IdentityName name; + private final boolean cacheable; + private final ImmutableMap methods; + private final ImmutableMap props; + private final ImmutableList orderedProps; + private final List facets; - public HelenusMappingEntity(Class iface, Metadata metadata) { - this(iface, autoDetectType(iface), metadata); - } + public HelenusMappingEntity(Class iface, Metadata metadata) { + this(iface, autoDetectType(iface), metadata); + } - public HelenusMappingEntity(Class iface, HelenusEntityType type, Metadata metadata) { + public HelenusMappingEntity(Class iface, HelenusEntityType type, Metadata metadata) { - if (iface == null || !iface.isInterface()) { - throw new IllegalArgumentException("invalid parameter " + iface); - } + if (iface == null || !iface.isInterface()) { + throw new IllegalArgumentException("invalid parameter " + iface); + } - this.iface = iface; - this.type = Objects.requireNonNull(type, "type is empty"); - this.name = resolveName(iface, type); + this.iface = iface; + this.type = Objects.requireNonNull(type, "type is empty"); + this.name = resolveName(iface, type); - HelenusSettings settings = Helenus.settings(); + HelenusSettings settings = Helenus.settings(); - Map methods = new HashMap(); - for (Method m : iface.getDeclaredMethods()) { - methods.put(m.getName(), m); - } + Map methods = new HashMap(); + for (Method m : iface.getDeclaredMethods()) { + methods.put(m.getName(), m); + } - for (Class c : ClassUtils.getAllInterfaces(iface)) { - if (c.getDeclaredAnnotation(Table.class) != null || c.getDeclaredAnnotation(InheritedTable.class) != null) { - for (Method m : c.getDeclaredMethods()) { - Method o = methods.get(m.getName()); - if (o != null) { - // Prefer overridden method implementation. - if (o.getDeclaringClass().isAssignableFrom(m.getDeclaringClass())) { - methods.put(m.getName(), m); - } - } else { - methods.put(m.getName(), m); - } - } - } - } + for (Class c : ClassUtils.getAllInterfaces(iface)) { + if (c.getDeclaredAnnotation(Table.class) != null + || c.getDeclaredAnnotation(InheritedTable.class) != null) { + for (Method m : c.getDeclaredMethods()) { + Method o = methods.get(m.getName()); + if (o != null) { + // Prefer overridden method implementation. + if (o.getDeclaringClass().isAssignableFrom(m.getDeclaringClass())) { + methods.put(m.getName(), m); + } + } else { + methods.put(m.getName(), m); + } + } + } + } - List propsLocal = new ArrayList(); - ImmutableMap.Builder propsBuilder = ImmutableMap.builder(); - ImmutableMap.Builder methodsBuilder = ImmutableMap.builder(); + List propsLocal = new ArrayList(); + ImmutableMap.Builder propsBuilder = ImmutableMap.builder(); + ImmutableMap.Builder methodsBuilder = ImmutableMap.builder(); - for (Method method : methods.values()) { + for (Method method : methods.values()) { - if (settings.getGetterMethodDetector().apply(method)) { + if (settings.getGetterMethodDetector().apply(method)) { - methodsBuilder.put(method.getName(), method); + methodsBuilder.put(method.getName(), method); - if (metadata != null) { - HelenusProperty prop = new HelenusMappingProperty(this, method, metadata); + if (metadata != null) { + HelenusProperty prop = new HelenusMappingProperty(this, method, metadata); - propsBuilder.put(prop.getPropertyName(), prop); - propsLocal.add(prop); - } - } - } + propsBuilder.put(prop.getPropertyName(), prop); + propsLocal.add(prop); + } + } + } - this.methods = methodsBuilder.build(); - this.props = propsBuilder.build(); + this.methods = methodsBuilder.build(); + this.props = propsBuilder.build(); - Collections.sort(propsLocal, TypeAndOrdinalColumnComparator.INSTANCE); - this.orderedProps = ImmutableList.copyOf(propsLocal); + Collections.sort(propsLocal, TypeAndOrdinalColumnComparator.INSTANCE); + this.orderedProps = ImmutableList.copyOf(propsLocal); - validateOrdinals(); + validateOrdinals(); - // Caching - cacheable = (null != iface.getDeclaredAnnotation(Cacheable.class)); + // Caching + cacheable = (null != iface.getDeclaredAnnotation(Cacheable.class)); - List primaryKeyProperties = new ArrayList<>(); - ImmutableList.Builder facetsBuilder = ImmutableList.builder(); - facetsBuilder.add(new Facet("table", name.toCql()).setFixed()); - for (HelenusProperty prop : orderedProps) { - switch (prop.getColumnType()) { - case PARTITION_KEY : - case CLUSTERING_COLUMN : - primaryKeyProperties.add(prop); - break; - default : - if (primaryKeyProperties != null && primaryKeyProperties.size() > 0) { - facetsBuilder.add(new UnboundFacet(primaryKeyProperties)); - primaryKeyProperties = null; - } - for (ConstraintValidator constraint : MappingUtil.getValidators(prop.getGetterMethod())) { - if (constraint.getClass().isAssignableFrom(DistinctValidator.class)) { - UnboundFacet facet = new UnboundFacet(prop); - facetsBuilder.add(facet); - break; - } - } - } - } - if (primaryKeyProperties != null && primaryKeyProperties.size() > 0) { - facetsBuilder.add(new UnboundFacet(primaryKeyProperties)); - } - this.facets = facetsBuilder.build(); - } + List primaryKeyProperties = new ArrayList<>(); + ImmutableList.Builder facetsBuilder = ImmutableList.builder(); + facetsBuilder.add(new Facet("table", name.toCql()).setFixed()); + for (HelenusProperty prop : orderedProps) { + switch (prop.getColumnType()) { + case PARTITION_KEY: + case CLUSTERING_COLUMN: + primaryKeyProperties.add(prop); + break; + default: + if (primaryKeyProperties != null && primaryKeyProperties.size() > 0) { + facetsBuilder.add(new UnboundFacet(primaryKeyProperties)); + primaryKeyProperties = null; + } + for (ConstraintValidator constraint : + MappingUtil.getValidators(prop.getGetterMethod())) { + if (constraint.getClass().isAssignableFrom(DistinctValidator.class)) { + UnboundFacet facet = new UnboundFacet(prop); + facetsBuilder.add(facet); + break; + } + } + } + } + if (primaryKeyProperties != null && primaryKeyProperties.size() > 0) { + facetsBuilder.add(new UnboundFacet(primaryKeyProperties)); + } + this.facets = facetsBuilder.build(); + } - private static IdentityName resolveName(Class iface, HelenusEntityType type) { + private static IdentityName resolveName(Class iface, HelenusEntityType type) { - switch (type) { - case TABLE : - return MappingUtil.getTableName(iface, true); + switch (type) { + case TABLE: + return MappingUtil.getTableName(iface, true); - case VIEW : - return MappingUtil.getViewName(iface, true); + case VIEW: + return MappingUtil.getViewName(iface, true); - case TUPLE : - return IdentityName.of(MappingUtil.getDefaultEntityName(iface), false); + case TUPLE: + return IdentityName.of(MappingUtil.getDefaultEntityName(iface), false); - case UDT : - return MappingUtil.getUserDefinedTypeName(iface, true); - } + case UDT: + return MappingUtil.getUserDefinedTypeName(iface, true); + } - throw new HelenusMappingException("invalid entity type " + type + " in " + type); - } + throw new HelenusMappingException("invalid entity type " + type + " in " + type); + } - private static HelenusEntityType autoDetectType(Class iface) { + private static HelenusEntityType autoDetectType(Class iface) { - Objects.requireNonNull(iface, "empty iface"); + Objects.requireNonNull(iface, "empty iface"); - if (null != iface.getDeclaredAnnotation(Table.class)) { - return HelenusEntityType.TABLE; - } else if (null != iface.getDeclaredAnnotation(MaterializedView.class)) { - return HelenusEntityType.VIEW; - } else if (null != iface.getDeclaredAnnotation(Tuple.class)) { - return HelenusEntityType.TUPLE; - } else if (null != iface.getDeclaredAnnotation(UDT.class)) { - return HelenusEntityType.UDT; - } + if (null != iface.getDeclaredAnnotation(Table.class)) { + return HelenusEntityType.TABLE; + } else if (null != iface.getDeclaredAnnotation(MaterializedView.class)) { + return HelenusEntityType.VIEW; + } else if (null != iface.getDeclaredAnnotation(Tuple.class)) { + return HelenusEntityType.TUPLE; + } else if (null != iface.getDeclaredAnnotation(UDT.class)) { + return HelenusEntityType.UDT; + } - throw new HelenusMappingException("entity must be annotated by @Table or @Tuple or @UserDefinedType " + iface); - } + throw new HelenusMappingException( + "entity must be annotated by @Table or @Tuple or @UserDefinedType " + iface); + } - @Override - public HelenusEntityType getType() { - return type; - } + @Override + public HelenusEntityType getType() { + return type; + } - @Override - public boolean isCacheable() { - return cacheable; - } + @Override + public boolean isCacheable() { + return cacheable; + } - @Override - public Class getMappingInterface() { - return iface; - } + @Override + public Class getMappingInterface() { + return iface; + } - @Override - public Collection getOrderedProperties() { - return orderedProps; - } + @Override + public Collection getOrderedProperties() { + return orderedProps; + } - @Override - public HelenusProperty getProperty(String name) { - HelenusProperty property = props.get(name); - if (property == null && methods.containsKey(name)) { - property = new HelenusMappingProperty(this, methods.get(name), new DefaultMetadata()); - return property; // TODO(gburd): review adding these into the props map... - } - return props.get(name); - } + @Override + public HelenusProperty getProperty(String name) { + HelenusProperty property = props.get(name); + if (property == null && methods.containsKey(name)) { + property = new HelenusMappingProperty(this, methods.get(name), new DefaultMetadata()); + return property; // TODO(gburd): review adding these into the props map... + } + return props.get(name); + } - @Override - public List getFacets() { - return facets; - } + @Override + public List getFacets() { + return facets; + } - @Override - public IdentityName getName() { - return name; - } + @Override + public IdentityName getName() { + return name; + } - private void validateOrdinals() { + private void validateOrdinals() { - switch (getType()) { - case TABLE : - validateOrdinalsForTable(); - break; + switch (getType()) { + case TABLE: + validateOrdinalsForTable(); + break; - case TUPLE : - validateOrdinalsInTuple(); - break; + case TUPLE: + validateOrdinalsInTuple(); + break; - default : - break; - } - } + default: + break; + } + } - private void validateOrdinalsForTable() { + private void validateOrdinalsForTable() { - BitSet partitionKeys = new BitSet(); - BitSet clusteringColumns = new BitSet(); + BitSet partitionKeys = new BitSet(); + BitSet clusteringColumns = new BitSet(); - for (HelenusProperty prop : getOrderedProperties()) { + for (HelenusProperty prop : getOrderedProperties()) { - ColumnType type = prop.getColumnType(); + ColumnType type = prop.getColumnType(); - int ordinal = prop.getOrdinal(); + int ordinal = prop.getOrdinal(); - switch (type) { - case PARTITION_KEY : - if (partitionKeys.get(ordinal)) { - throw new HelenusMappingException( - "detected two or more partition key columns with the same ordinal " + ordinal + " in " - + prop.getEntity()); - } - partitionKeys.set(ordinal); - break; + switch (type) { + case PARTITION_KEY: + if (partitionKeys.get(ordinal)) { + throw new HelenusMappingException( + "detected two or more partition key columns with the same ordinal " + + ordinal + + " in " + + prop.getEntity()); + } + partitionKeys.set(ordinal); + break; - case CLUSTERING_COLUMN : - if (clusteringColumns.get(ordinal)) { - throw new HelenusMappingException("detected two or clustering columns with the same ordinal " - + ordinal + " in " + prop.getEntity()); - } - clusteringColumns.set(ordinal); - break; + case CLUSTERING_COLUMN: + if (clusteringColumns.get(ordinal)) { + throw new HelenusMappingException( + "detected two or clustering columns with the same ordinal " + + ordinal + + " in " + + prop.getEntity()); + } + clusteringColumns.set(ordinal); + break; - default : - break; - } - } - } + default: + break; + } + } + } - private void validateOrdinalsInTuple() { - boolean[] ordinals = new boolean[props.size()]; + private void validateOrdinalsInTuple() { + boolean[] ordinals = new boolean[props.size()]; - getOrderedProperties().forEach(p -> { - int ordinal = p.getOrdinal(); + getOrderedProperties() + .forEach( + p -> { + int ordinal = p.getOrdinal(); - if (ordinal < 0 || ordinal >= ordinals.length) { - throw new HelenusMappingException("invalid ordinal " + ordinal + " found for property " - + p.getPropertyName() + " in " + p.getEntity()); - } + if (ordinal < 0 || ordinal >= ordinals.length) { + throw new HelenusMappingException( + "invalid ordinal " + + ordinal + + " found for property " + + p.getPropertyName() + + " in " + + p.getEntity()); + } - if (ordinals[ordinal]) { - throw new HelenusMappingException( - "detected two or more properties with the same ordinal " + ordinal + " in " + p.getEntity()); - } + if (ordinals[ordinal]) { + throw new HelenusMappingException( + "detected two or more properties with the same ordinal " + + ordinal + + " in " + + p.getEntity()); + } - ordinals[ordinal] = true; - }); + ordinals[ordinal] = true; + }); - for (int i = 0; i != ordinals.length; ++i) { - if (!ordinals[i]) { - throw new HelenusMappingException("detected absent ordinal " + i + " in " + this); - } - } - } + for (int i = 0; i != ordinals.length; ++i) { + if (!ordinals[i]) { + throw new HelenusMappingException("detected absent ordinal " + i + " in " + this); + } + } + } - @Override - public String toString() { + @Override + public String toString() { - StringBuilder str = new StringBuilder(); - str.append(iface.getSimpleName()).append("(").append(name.getName()).append(") ") - .append(type.name().toLowerCase()).append(":\n"); + StringBuilder str = new StringBuilder(); + str.append(iface.getSimpleName()) + .append("(") + .append(name.getName()) + .append(") ") + .append(type.name().toLowerCase()) + .append(":\n"); - for (HelenusProperty prop : getOrderedProperties()) { - str.append(prop.toString()); - str.append("\n"); - } - return str.toString(); - } + for (HelenusProperty prop : getOrderedProperties()) { + str.append(prop.toString()); + str.append("\n"); + } + return str.toString(); + } } diff --git a/src/main/java/net/helenus/mapping/HelenusMappingProperty.java b/src/main/java/net/helenus/mapping/HelenusMappingProperty.java index 1382829..fbea1dd 100644 --- a/src/main/java/net/helenus/mapping/HelenusMappingProperty.java +++ b/src/main/java/net/helenus/mapping/HelenusMappingProperty.java @@ -15,16 +15,13 @@ */ package net.helenus.mapping; +import com.datastax.driver.core.Metadata; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.Optional; import java.util.function.Function; - import javax.validation.ConstraintValidator; - -import com.datastax.driver.core.Metadata; - import net.helenus.core.SessionRepository; import net.helenus.mapping.javatype.AbstractJavaType; import net.helenus.mapping.javatype.MappingJavaTypes; @@ -32,171 +29,172 @@ import net.helenus.mapping.type.AbstractDataType; public final class HelenusMappingProperty implements HelenusProperty { - private final HelenusEntity entity; - private final Method getter; + private final HelenusEntity entity; + private final Method getter; - private final String propertyName; - private final Optional indexName; - private final boolean caseSensitiveIndex; + private final String propertyName; + private final Optional indexName; + private final boolean caseSensitiveIndex; - private final ColumnInformation columnInfo; + private final ColumnInformation columnInfo; - private final Type genericJavaType; - private final Class javaType; - private final AbstractJavaType abstractJavaType; - private final AbstractDataType dataType; - private final ConstraintValidator[] validators; - private volatile Optional> readConverter = null; - private volatile Optional> writeConverter = null; + private final Type genericJavaType; + private final Class javaType; + private final AbstractJavaType abstractJavaType; + private final AbstractDataType dataType; + private final ConstraintValidator[] validators; + private volatile Optional> readConverter = null; + private volatile Optional> writeConverter = null; - public HelenusMappingProperty(HelenusMappingEntity entity, Method getter, Metadata metadata) { - this.entity = entity; - this.getter = getter; + public HelenusMappingProperty(HelenusMappingEntity entity, Method getter, Metadata metadata) { + this.entity = entity; + this.getter = getter; - this.propertyName = MappingUtil.getPropertyName(getter); - this.indexName = MappingUtil.getIndexName(getter); - this.caseSensitiveIndex = MappingUtil.caseSensitiveIndex(getter); + this.propertyName = MappingUtil.getPropertyName(getter); + this.indexName = MappingUtil.getIndexName(getter); + this.caseSensitiveIndex = MappingUtil.caseSensitiveIndex(getter); - this.columnInfo = new ColumnInformation(getter); + this.columnInfo = new ColumnInformation(getter); - this.genericJavaType = getter.getGenericReturnType(); - this.javaType = getter.getReturnType(); - this.abstractJavaType = MappingJavaTypes.resolveJavaType(this.javaType); + this.genericJavaType = getter.getGenericReturnType(); + this.javaType = getter.getReturnType(); + this.abstractJavaType = MappingJavaTypes.resolveJavaType(this.javaType); - this.dataType = abstractJavaType.resolveDataType(this.getter, this.genericJavaType, - this.columnInfo.getColumnType(), metadata); + this.dataType = + abstractJavaType.resolveDataType( + this.getter, this.genericJavaType, this.columnInfo.getColumnType(), metadata); - this.validators = MappingUtil.getValidators(getter); - } + this.validators = MappingUtil.getValidators(getter); + } - @Override - public HelenusEntity getEntity() { - return entity; - } + @Override + public HelenusEntity getEntity() { + return entity; + } - @Override - public Class getJavaType() { - return (Class) javaType; - } + @Override + public Class getJavaType() { + return (Class) javaType; + } - @Override - public AbstractDataType getDataType() { - return dataType; - } + @Override + public AbstractDataType getDataType() { + return dataType; + } - @Override - public ColumnType getColumnType() { - return columnInfo.getColumnType(); - } + @Override + public ColumnType getColumnType() { + return columnInfo.getColumnType(); + } - @Override - public int getOrdinal() { - return columnInfo.getOrdinal(); - } + @Override + public int getOrdinal() { + return columnInfo.getOrdinal(); + } - @Override - public OrderingDirection getOrdering() { - return columnInfo.getOrdering(); - } + @Override + public OrderingDirection getOrdering() { + return columnInfo.getOrdering(); + } - @Override - public IdentityName getColumnName() { - return columnInfo.getColumnName(); - } + @Override + public IdentityName getColumnName() { + return columnInfo.getColumnName(); + } - @Override - public Optional getIndexName() { - return indexName; - } + @Override + public Optional getIndexName() { + return indexName; + } - @Override - public boolean caseSensitiveIndex() { - return caseSensitiveIndex; - } + @Override + public boolean caseSensitiveIndex() { + return caseSensitiveIndex; + } - @Override - public String getPropertyName() { - return propertyName; - } + @Override + public String getPropertyName() { + return propertyName; + } - @Override - public Method getGetterMethod() { - return getter; - } + @Override + public Method getGetterMethod() { + return getter; + } - @Override - public Optional> getReadConverter(SessionRepository repository) { + @Override + public Optional> getReadConverter(SessionRepository repository) { - if (readConverter == null) { - readConverter = abstractJavaType.resolveReadConverter(this.dataType, repository); - } + if (readConverter == null) { + readConverter = abstractJavaType.resolveReadConverter(this.dataType, repository); + } - return readConverter; - } + return readConverter; + } - @Override - public Optional> getWriteConverter(SessionRepository repository) { + @Override + public Optional> getWriteConverter(SessionRepository repository) { - if (writeConverter == null) { - writeConverter = abstractJavaType.resolveWriteConverter(this.dataType, repository); - } + if (writeConverter == null) { + writeConverter = abstractJavaType.resolveWriteConverter(this.dataType, repository); + } - return writeConverter; - } + return writeConverter; + } - @Override - public ConstraintValidator[] getValidators() { - return validators; - } + @Override + public ConstraintValidator[] getValidators() { + return validators; + } - @Override - public String toString() { + @Override + public String toString() { - StringBuilder str = new StringBuilder(); + StringBuilder str = new StringBuilder(); - String columnName = this.getColumnName().getName(); - str.append(" "); - str.append(this.getDataType()); - str.append(" "); - str.append(this.getPropertyName()); - str.append("("); - if (!columnName.equals(this.getPropertyName())) { - str.append(columnName); - } - str.append(") "); + String columnName = this.getColumnName().getName(); + str.append(" "); + str.append(this.getDataType()); + str.append(" "); + str.append(this.getPropertyName()); + str.append("("); + if (!columnName.equals(this.getPropertyName())) { + str.append(columnName); + } + str.append(") "); - ColumnType type = this.getColumnType(); + ColumnType type = this.getColumnType(); - switch (type) { - case PARTITION_KEY : - str.append("partition_key["); - str.append(this.getOrdinal()); - str.append("] "); - break; + switch (type) { + case PARTITION_KEY: + str.append("partition_key["); + str.append(this.getOrdinal()); + str.append("] "); + break; - case CLUSTERING_COLUMN : - str.append("clustering_column["); - str.append(this.getOrdinal()); - str.append("] "); - OrderingDirection od = this.getOrdering(); - if (od != null) { - str.append(od.name().toLowerCase()).append(" "); - } - break; + case CLUSTERING_COLUMN: + str.append("clustering_column["); + str.append(this.getOrdinal()); + str.append("] "); + OrderingDirection od = this.getOrdering(); + if (od != null) { + str.append(od.name().toLowerCase()).append(" "); + } + break; - case STATIC_COLUMN : - str.append("static "); - break; + case STATIC_COLUMN: + str.append("static "); + break; - case COLUMN : - break; - } + case COLUMN: + break; + } - Optional idx = this.getIndexName(); - if (idx.isPresent()) { - str.append("index(").append(idx.get().getName()).append(") "); - } + Optional idx = this.getIndexName(); + if (idx.isPresent()) { + str.append("index(").append(idx.get().getName()).append(") "); + } - return str.toString(); - } + return str.toString(); + } } diff --git a/src/main/java/net/helenus/mapping/HelenusProperty.java b/src/main/java/net/helenus/mapping/HelenusProperty.java index fecc550..08b428e 100644 --- a/src/main/java/net/helenus/mapping/HelenusProperty.java +++ b/src/main/java/net/helenus/mapping/HelenusProperty.java @@ -19,39 +19,37 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.Optional; import java.util.function.Function; - import javax.validation.ConstraintValidator; - import net.helenus.core.SessionRepository; import net.helenus.mapping.type.AbstractDataType; public interface HelenusProperty { - HelenusEntity getEntity(); + HelenusEntity getEntity(); - String getPropertyName(); + String getPropertyName(); - Method getGetterMethod(); + Method getGetterMethod(); - IdentityName getColumnName(); + IdentityName getColumnName(); - Optional getIndexName(); + Optional getIndexName(); - boolean caseSensitiveIndex(); + boolean caseSensitiveIndex(); - Class getJavaType(); + Class getJavaType(); - AbstractDataType getDataType(); + AbstractDataType getDataType(); - ColumnType getColumnType(); + ColumnType getColumnType(); - int getOrdinal(); + int getOrdinal(); - OrderingDirection getOrdering(); + OrderingDirection getOrdering(); - Optional> getReadConverter(SessionRepository repository); + Optional> getReadConverter(SessionRepository repository); - Optional> getWriteConverter(SessionRepository repository); + Optional> getWriteConverter(SessionRepository repository); - ConstraintValidator[] getValidators(); + ConstraintValidator[] getValidators(); } diff --git a/src/main/java/net/helenus/mapping/IdentityName.java b/src/main/java/net/helenus/mapping/IdentityName.java index cf61cbe..02f0858 100644 --- a/src/main/java/net/helenus/mapping/IdentityName.java +++ b/src/main/java/net/helenus/mapping/IdentityName.java @@ -19,41 +19,41 @@ import net.helenus.support.CqlUtil; public final class IdentityName { - private final String name; + private final String name; - private final boolean forceQuote; + private final boolean forceQuote; - public IdentityName(String name, boolean forceQuote) { - this.name = name.toLowerCase(); - this.forceQuote = forceQuote; - } + public IdentityName(String name, boolean forceQuote) { + this.name = name.toLowerCase(); + this.forceQuote = forceQuote; + } - public static IdentityName of(String name, boolean forceQuote) { - return new IdentityName(name, forceQuote); - } + public static IdentityName of(String name, boolean forceQuote) { + return new IdentityName(name, forceQuote); + } - public String getName() { - return name; - } + public String getName() { + return name; + } - public boolean isForceQuote() { - return forceQuote; - } + public boolean isForceQuote() { + return forceQuote; + } - public String toCql(boolean overrideForceQuote) { - if (overrideForceQuote) { - return CqlUtil.forceQuote(name); - } else { - return name; - } - } + public String toCql(boolean overrideForceQuote) { + if (overrideForceQuote) { + return CqlUtil.forceQuote(name); + } else { + return name; + } + } - public String toCql() { - return toCql(forceQuote); - } + public String toCql() { + return toCql(forceQuote); + } - @Override - public String toString() { - return toCql(); - } + @Override + public String toString() { + return toCql(); + } } diff --git a/src/main/java/net/helenus/mapping/MappingUtil.java b/src/main/java/net/helenus/mapping/MappingUtil.java index f421c40..4bff364 100644 --- a/src/main/java/net/helenus/mapping/MappingUtil.java +++ b/src/main/java/net/helenus/mapping/MappingUtil.java @@ -21,10 +21,8 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.Optional; - import javax.validation.Constraint; import javax.validation.ConstraintValidator; - import net.helenus.core.Getter; import net.helenus.core.Helenus; import net.helenus.core.reflect.*; @@ -34,290 +32,292 @@ import net.helenus.support.HelenusMappingException; public final class MappingUtil { - @SuppressWarnings("unchecked") - public static final ConstraintValidator[] EMPTY_VALIDATORS = new ConstraintValidator[0]; + @SuppressWarnings("unchecked") + public static final ConstraintValidator[] EMPTY_VALIDATORS = + new ConstraintValidator[0]; - private MappingUtil() { - } + private MappingUtil() {} - public static ConstraintValidator[] getValidators(Method getterMethod) { + public static ConstraintValidator[] getValidators(Method getterMethod) { - List> list = null; + List> list = null; - for (Annotation constraintAnnotation : getterMethod.getDeclaredAnnotations()) { + for (Annotation constraintAnnotation : getterMethod.getDeclaredAnnotations()) { - list = addValidators(constraintAnnotation, list); + list = addValidators(constraintAnnotation, list); - Class annotationType = constraintAnnotation.annotationType(); + Class annotationType = constraintAnnotation.annotationType(); - for (Annotation possibleConstraint : annotationType.getDeclaredAnnotations()) { + for (Annotation possibleConstraint : annotationType.getDeclaredAnnotations()) { - list = addValidators(possibleConstraint, list); - } - } + list = addValidators(possibleConstraint, list); + } + } - if (list == null) { - return EMPTY_VALIDATORS; - } else { - return list.toArray(EMPTY_VALIDATORS); - } - } + if (list == null) { + return EMPTY_VALIDATORS; + } else { + return list.toArray(EMPTY_VALIDATORS); + } + } - private static List> addValidators(Annotation constraintAnnotation, - List> list) { + private static List> addValidators( + Annotation constraintAnnotation, List> list) { - Class annotationType = constraintAnnotation.annotationType(); + Class annotationType = constraintAnnotation.annotationType(); - for (Annotation possibleConstraint : annotationType.getDeclaredAnnotations()) { + for (Annotation possibleConstraint : annotationType.getDeclaredAnnotations()) { - if (possibleConstraint instanceof Constraint) { + if (possibleConstraint instanceof Constraint) { - Constraint constraint = (Constraint) possibleConstraint; + Constraint constraint = (Constraint) possibleConstraint; - for (Class> clazz : constraint.validatedBy()) { + for (Class> clazz : constraint.validatedBy()) { - ConstraintValidator validator = ReflectionInstantiator - .instantiateClass(clazz); + ConstraintValidator validator = + ReflectionInstantiator.instantiateClass(clazz); - ((ConstraintValidator) validator).initialize(constraintAnnotation); + ((ConstraintValidator) validator).initialize(constraintAnnotation); - if (list == null) { - list = new ArrayList>(); - } + if (list == null) { + list = new ArrayList>(); + } - list.add(validator); - } - } - } + list.add(validator); + } + } + } - return list; - } + return list; + } - public static Optional getIndexName(Method getterMethod) { + public static Optional getIndexName(Method getterMethod) { - String indexName = null; - boolean forceQuote = false; + String indexName = null; + boolean forceQuote = false; - Index index = getterMethod.getDeclaredAnnotation(Index.class); + Index index = getterMethod.getDeclaredAnnotation(Index.class); - if (index != null) { - indexName = index.value(); - forceQuote = index.forceQuote(); + if (index != null) { + indexName = index.value(); + forceQuote = index.forceQuote(); - if (indexName == null || indexName.isEmpty()) { - indexName = getDefaultColumnName(getterMethod); - } - } + if (indexName == null || indexName.isEmpty()) { + indexName = getDefaultColumnName(getterMethod); + } + } - return indexName != null ? Optional.of(new IdentityName(indexName, forceQuote)) : Optional.empty(); - } + return indexName != null + ? Optional.of(new IdentityName(indexName, forceQuote)) + : Optional.empty(); + } - public static boolean caseSensitiveIndex(Method getterMethod) { - Index index = getterMethod.getDeclaredAnnotation(Index.class); + public static boolean caseSensitiveIndex(Method getterMethod) { + Index index = getterMethod.getDeclaredAnnotation(Index.class); - if (index != null) { - return index.caseSensitive(); - } + if (index != null) { + return index.caseSensitive(); + } - return false; - } + return false; + } - public static String getPropertyName(Method getter) { - return getter.getName(); - } + public static String getPropertyName(Method getter) { + return getter.getName(); + } - public static String getDefaultColumnName(Method getter) { - return Helenus.settings().getPropertyToColumnConverter().apply(getPropertyName(getter)); - } + public static String getDefaultColumnName(Method getter) { + return Helenus.settings().getPropertyToColumnConverter().apply(getPropertyName(getter)); + } - public static IdentityName getUserDefinedTypeName(Class iface, boolean required) { + public static IdentityName getUserDefinedTypeName(Class iface, boolean required) { - String userTypeName = null; - boolean forceQuote = false; + String userTypeName = null; + boolean forceQuote = false; - UDT userDefinedType = iface.getDeclaredAnnotation(UDT.class); + UDT userDefinedType = iface.getDeclaredAnnotation(UDT.class); - if (userDefinedType != null) { + if (userDefinedType != null) { - userTypeName = userDefinedType.value(); - forceQuote = userDefinedType.forceQuote(); + userTypeName = userDefinedType.value(); + forceQuote = userDefinedType.forceQuote(); - if (userTypeName == null || userTypeName.isEmpty()) { - userTypeName = getDefaultEntityName(iface); - } + if (userTypeName == null || userTypeName.isEmpty()) { + userTypeName = getDefaultEntityName(iface); + } - return new IdentityName(userTypeName, forceQuote); - } + return new IdentityName(userTypeName, forceQuote); + } - if (required) { - throw new HelenusMappingException("entity must have annotation @UserDefinedType " + iface); - } + if (required) { + throw new HelenusMappingException("entity must have annotation @UserDefinedType " + iface); + } - return null; - } + return null; + } - public static boolean isTuple(Class iface) { + public static boolean isTuple(Class iface) { - Tuple tuple = iface.getDeclaredAnnotation(Tuple.class); + Tuple tuple = iface.getDeclaredAnnotation(Tuple.class); - return tuple != null; - } + return tuple != null; + } - public static boolean isUDT(Class iface) { + public static boolean isUDT(Class iface) { - UDT udt = iface.getDeclaredAnnotation(UDT.class); + UDT udt = iface.getDeclaredAnnotation(UDT.class); - return udt != null; - } + return udt != null; + } - public static IdentityName getViewName(Class iface, boolean required) { + public static IdentityName getViewName(Class iface, boolean required) { - String viewName = null; - boolean forceQuote = false; + String viewName = null; + boolean forceQuote = false; - MaterializedView view = iface.getDeclaredAnnotation(MaterializedView.class); + MaterializedView view = iface.getDeclaredAnnotation(MaterializedView.class); - if (view != null) { - viewName = view.value(); - forceQuote = view.forceQuote(); + if (view != null) { + viewName = view.value(); + forceQuote = view.forceQuote(); - } else if (required) { - throw new HelenusMappingException("entity must have annotation @Table " + iface); - } + } else if (required) { + throw new HelenusMappingException("entity must have annotation @Table " + iface); + } - if (viewName == null || viewName.isEmpty()) { - viewName = getDefaultEntityName(iface); - } + if (viewName == null || viewName.isEmpty()) { + viewName = getDefaultEntityName(iface); + } - return new IdentityName(viewName, forceQuote); - } + return new IdentityName(viewName, forceQuote); + } - public static IdentityName getTableName(Class iface, boolean required) { + public static IdentityName getTableName(Class iface, boolean required) { - String tableName = null; - boolean forceQuote = false; + String tableName = null; + boolean forceQuote = false; - Table table = iface.getDeclaredAnnotation(Table.class); + Table table = iface.getDeclaredAnnotation(Table.class); - if (table != null) { - tableName = table.value(); - forceQuote = table.forceQuote(); + if (table != null) { + tableName = table.value(); + forceQuote = table.forceQuote(); - } else if (required) { - throw new HelenusMappingException("entity must have annotation @Table " + iface); - } + } else if (required) { + throw new HelenusMappingException("entity must have annotation @Table " + iface); + } - if (tableName == null || tableName.isEmpty()) { - tableName = getDefaultEntityName(iface); - } + if (tableName == null || tableName.isEmpty()) { + tableName = getDefaultEntityName(iface); + } - return new IdentityName(tableName, forceQuote); - } + return new IdentityName(tableName, forceQuote); + } - public static String getDefaultEntityName(Class iface) { - return Helenus.settings().getPropertyToColumnConverter().apply(iface.getSimpleName()); - } + public static String getDefaultEntityName(Class iface) { + return Helenus.settings().getPropertyToColumnConverter().apply(iface.getSimpleName()); + } - public static Class getMappingInterface(Object pojo) { + public static Class getMappingInterface(Object pojo) { - Class iface = null; + Class iface = null; - if (pojo instanceof Class) { - iface = (Class) pojo; + if (pojo instanceof Class) { + iface = (Class) pojo; - if (!iface.isInterface()) { - throw new HelenusMappingException("expected interface " + iface); - } + if (!iface.isInterface()) { + throw new HelenusMappingException("expected interface " + iface); + } - } else { - Class[] ifaces = pojo.getClass().getInterfaces(); + } else { + Class[] ifaces = pojo.getClass().getInterfaces(); - int len = ifaces.length; - for (int i = 0; i != len; ++i) { + int len = ifaces.length; + for (int i = 0; i != len; ++i) { - iface = ifaces[0]; + iface = ifaces[0]; - if (MapExportable.class.isAssignableFrom(iface)) { - continue; - } + if (MapExportable.class.isAssignableFrom(iface)) { + continue; + } - if (iface.getDeclaredAnnotation(Table.class) != null - || iface.getDeclaredAnnotation(MaterializedView.class) != null - || iface.getDeclaredAnnotation(UDT.class) != null - || iface.getDeclaredAnnotation(Tuple.class) != null) { + if (iface.getDeclaredAnnotation(Table.class) != null + || iface.getDeclaredAnnotation(MaterializedView.class) != null + || iface.getDeclaredAnnotation(UDT.class) != null + || iface.getDeclaredAnnotation(Tuple.class) != null) { - break; - } - } - } + break; + } + } + } - if (iface == null) { - throw new HelenusMappingException("dsl interface not found for " + pojo); - } + if (iface == null) { + throw new HelenusMappingException("dsl interface not found for " + pojo); + } - return iface; - } + return iface; + } - public static HelenusPropertyNode resolveMappingProperty(Getter getter) { - - try { - Object childDsl = getter.get(); - - if (childDsl instanceof DslExportable) { - DslExportable e = (DslExportable) childDsl; - return e.getParentDslHelenusPropertyNode(); - } else if (childDsl instanceof MapDsl) { - MapDsl mapDsl = (MapDsl) childDsl; - return mapDsl.getParent(); - } else if (childDsl instanceof ListDsl) { - ListDsl listDsl = (ListDsl) childDsl; - return listDsl.getParent(); - } else if (childDsl instanceof SetDsl) { - SetDsl setDsl = (SetDsl) childDsl; - return setDsl.getParent(); - } - - throw new HelenusMappingException("getter must reference to the dsl object " + getter); - - } catch (DslPropertyException e) { - return e.getPropertyNode(); - } - } - - // https://stackoverflow.com/a/4882306/366692 - public static T clone(T object) throws CloneNotSupportedException { - Object clone = null; - - // Use reflection, because there is no other way - try { - Method method = object.getClass().getMethod("clone"); - clone = method.invoke(object); - } catch (InvocationTargetException e) { - rethrow(e.getCause()); - } catch (Exception cause) { - rethrow(cause); - } - if (object.getClass().isInstance(clone)) { - @SuppressWarnings("unchecked") // clone class <= object class <= T - T t = (T) clone; - return t; - } else { - throw new ClassCastException(clone.getClass().getName()); - } - } - - private static void rethrow(Throwable cause) throws CloneNotSupportedException { - if (cause instanceof RuntimeException) { - throw (RuntimeException) cause; - } - if (cause instanceof Error) { - throw (Error) cause; - } - if (cause instanceof CloneNotSupportedException) { - throw (CloneNotSupportedException) cause; - } - CloneNotSupportedException e = new CloneNotSupportedException(); - e.initCause(cause); - throw e; - } + public static HelenusPropertyNode resolveMappingProperty(Getter getter) { + + try { + Object childDsl = getter.get(); + + if (childDsl instanceof DslExportable) { + DslExportable e = (DslExportable) childDsl; + return e.getParentDslHelenusPropertyNode(); + } else if (childDsl instanceof MapDsl) { + MapDsl mapDsl = (MapDsl) childDsl; + return mapDsl.getParent(); + } else if (childDsl instanceof ListDsl) { + ListDsl listDsl = (ListDsl) childDsl; + return listDsl.getParent(); + } else if (childDsl instanceof SetDsl) { + SetDsl setDsl = (SetDsl) childDsl; + return setDsl.getParent(); + } + + throw new HelenusMappingException("getter must reference to the dsl object " + getter); + + } catch (DslPropertyException e) { + return e.getPropertyNode(); + } + } + + // https://stackoverflow.com/a/4882306/366692 + public static T clone(T object) throws CloneNotSupportedException { + Object clone = null; + + // Use reflection, because there is no other way + try { + Method method = object.getClass().getMethod("clone"); + clone = method.invoke(object); + } catch (InvocationTargetException e) { + rethrow(e.getCause()); + } catch (Exception cause) { + rethrow(cause); + } + if (object.getClass().isInstance(clone)) { + @SuppressWarnings("unchecked") // clone class <= object class <= T + T t = (T) clone; + return t; + } else { + throw new ClassCastException(clone.getClass().getName()); + } + } + + private static void rethrow(Throwable cause) throws CloneNotSupportedException { + if (cause instanceof RuntimeException) { + throw (RuntimeException) cause; + } + if (cause instanceof Error) { + throw (Error) cause; + } + if (cause instanceof CloneNotSupportedException) { + throw (CloneNotSupportedException) cause; + } + CloneNotSupportedException e = new CloneNotSupportedException(); + e.initCause(cause); + throw e; + } } diff --git a/src/main/java/net/helenus/mapping/OrderingDirection.java b/src/main/java/net/helenus/mapping/OrderingDirection.java index 2f41023..41f2670 100644 --- a/src/main/java/net/helenus/mapping/OrderingDirection.java +++ b/src/main/java/net/helenus/mapping/OrderingDirection.java @@ -18,28 +18,28 @@ package net.helenus.mapping; import net.helenus.support.HelenusMappingException; public enum OrderingDirection { - ASC("ASC"), + ASC("ASC"), - DESC("DESC"); + DESC("DESC"); - private final String cql; + private final String cql; - private OrderingDirection(String cql) { - this.cql = cql; - } + private OrderingDirection(String cql) { + this.cql = cql; + } - public static OrderingDirection parseString(String name) { + public static OrderingDirection parseString(String name) { - if (ASC.cql.equalsIgnoreCase(name)) { - return ASC; - } else if (DESC.cql.equalsIgnoreCase(name)) { - return DESC; - } + if (ASC.cql.equalsIgnoreCase(name)) { + return ASC; + } else if (DESC.cql.equalsIgnoreCase(name)) { + return DESC; + } - throw new HelenusMappingException("invalid ordering direction name " + name); - } + throw new HelenusMappingException("invalid ordering direction name " + name); + } - public String cql() { - return cql; - } + public String cql() { + return cql; + } } diff --git a/src/main/java/net/helenus/mapping/TypeAndOrdinalColumnComparator.java b/src/main/java/net/helenus/mapping/TypeAndOrdinalColumnComparator.java index 825e0dc..ef925ea 100644 --- a/src/main/java/net/helenus/mapping/TypeAndOrdinalColumnComparator.java +++ b/src/main/java/net/helenus/mapping/TypeAndOrdinalColumnComparator.java @@ -18,16 +18,17 @@ package net.helenus.mapping; import java.util.Comparator; public enum TypeAndOrdinalColumnComparator implements Comparator { - INSTANCE; + INSTANCE; - public int compare(HelenusProperty thisVal, HelenusProperty anotherVal) { + public int compare(HelenusProperty thisVal, HelenusProperty anotherVal) { - int c = Integer.compare(thisVal.getColumnType().ordinal(), anotherVal.getColumnType().ordinal()); + int c = + Integer.compare(thisVal.getColumnType().ordinal(), anotherVal.getColumnType().ordinal()); - if (c == 0) { - c = Integer.compare(thisVal.getOrdinal(), anotherVal.getOrdinal()); - } + if (c == 0) { + c = Integer.compare(thisVal.getOrdinal(), anotherVal.getOrdinal()); + } - return c; - } + return c; + } } diff --git a/src/main/java/net/helenus/mapping/annotation/ClusteringColumn.java b/src/main/java/net/helenus/mapping/annotation/ClusteringColumn.java index c1f72de..4a6228b 100644 --- a/src/main/java/net/helenus/mapping/annotation/ClusteringColumn.java +++ b/src/main/java/net/helenus/mapping/annotation/ClusteringColumn.java @@ -19,93 +19,78 @@ import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; - import net.helenus.mapping.OrderingDirection; /** * ClusteringColumn is the family column in legacy Cassandra API * - *

- * The purpose of this column is have additional dimension in the table. - * Both @PartitionKey and @ClusteringColumn together are parts of the primary - * key of the table. The primary difference between them is that the first one - * is using for routing purposes in order to locate a data node in the cluster, - * otherwise the second one is using inside the node to locate peace of data in + *

The purpose of this column is have additional dimension in the table. Both @PartitionKey + * and @ClusteringColumn together are parts of the primary key of the table. The primary difference + * between them is that the first one is using for routing purposes in order to locate a data node + * in the cluster, otherwise the second one is using inside the node to locate peace of data in * concrete machine. * - *

- * ClusteringColumn can be represented as a Key in SortedMap that fully stored - * in a single node. All developers must be careful for selecting fields for - * clustering columns, because all data inside this SortedMap must fit in to one - * node. + *

ClusteringColumn can be represented as a Key in SortedMap that fully stored in a single node. + * All developers must be careful for selecting fields for clustering columns, because all data + * inside this SortedMap must fit in to one node. * - *

- * ClusteringColumn can have more than one part and the order of parts is - * important. This order defines the way how Cassandra joins the parts and - * influence of data retrieval operations. Each part can have ordering property - * that defines default ascending or descending order of data. In case of two - * and more parts in select queries developer needs to have consisdent order of - * all parts as they defined in table. + *

ClusteringColumn can have more than one part and the order of parts is important. This order + * defines the way how Cassandra joins the parts and influence of data retrieval operations. Each + * part can have ordering property that defines default ascending or descending order of data. In + * case of two and more parts in select queries developer needs to have consisdent order of all + * parts as they defined in table. * - *

- * For example, first part is ASC ordering, second is also ASC, so Cassandra - * will sort entries like this: a-a a-b b-a b-b In this case we are able run - * queries: ORDER BY first ASC, second ASC ORDER BY first DESC, second DESC - * WHERE first=? ORDER BY second ASC WHERE first=? ORDER BY second DESC WHERE - * first=? AND second=? + *

For example, first part is ASC ordering, second is also ASC, so Cassandra will sort entries + * like this: a-a a-b b-a b-b In this case we are able run queries: ORDER BY first ASC, second ASC + * ORDER BY first DESC, second DESC WHERE first=? ORDER BY second ASC WHERE first=? ORDER BY second + * DESC WHERE first=? AND second=? * - *

- * But, we can not run queries: ORDER BY first DESC, second ASC ORDER BY first - * ASC, second DESC WHERE second=? ORDER BY first (ASC,DESC) + *

But, we can not run queries: ORDER BY first DESC, second ASC ORDER BY first ASC, second DESC + * WHERE second=? ORDER BY first (ASC,DESC) */ @Retention(value = RetentionPolicy.RUNTIME) @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) public @interface ClusteringColumn { - /** - * Default value is the name of the method normalized to underscore - * - * @return name of the column - */ - String value() default ""; + /** + * Default value is the name of the method normalized to underscore + * + * @return name of the column + */ + String value() default ""; - /** - * ClusteringColumn parts must be ordered in the @Table. It is the requirement - * of Cassandra. Cassandra joins all parts to the final clustering key that is - * stored in column family name. Additionally all parts can have some ordering - * (ASC, DESC) that with sequence of parts determines key comparison function, - * so Cassandra storing column family names always in sorted order. - * - *

- * Be default ordinal has 0 value, that's because in most cases @Table have - * single column for ClusteringColumn If you have 2 and more parts of the - * ClusteringColumn, then you need to use ordinal() to define the sequence of - * the parts - * - * @return number that used to sort clustering columns - */ - int ordinal() default 0; + /** + * ClusteringColumn parts must be ordered in the @Table. It is the requirement of Cassandra. + * Cassandra joins all parts to the final clustering key that is stored in column family name. + * Additionally all parts can have some ordering (ASC, DESC) that with sequence of parts + * determines key comparison function, so Cassandra storing column family names always in sorted + * order. + * + *

Be default ordinal has 0 value, that's because in most cases @Table have single column for + * ClusteringColumn If you have 2 and more parts of the ClusteringColumn, then you need to use + * ordinal() to define the sequence of the parts + * + * @return number that used to sort clustering columns + */ + int ordinal() default 0; - /** - * Default order of values in the ClusteringColumn This ordering is using for - * comparison of the clustering column values when Cassandra stores it in the - * sorted order. - * - *

- * Default value is the ascending order - * - * @return ascending order or descending order of clustering column values - */ - OrderingDirection ordering() default OrderingDirection.ASC; + /** + * Default order of values in the ClusteringColumn This ordering is using for comparison of the + * clustering column values when Cassandra stores it in the sorted order. + * + *

Default value is the ascending order + * + * @return ascending order or descending order of clustering column values + */ + OrderingDirection ordering() default OrderingDirection.ASC; - /** - * For reserved words in Cassandra we need quotation in CQL queries. This - * property marks that the name of the UDT type needs to be quoted. - * - *

- * Default value is false, we are quoting only selected names. - * - * @return true if name have to be quoted - */ - boolean forceQuote() default false; + /** + * For reserved words in Cassandra we need quotation in CQL queries. This property marks that the + * name of the UDT type needs to be quoted. + * + *

Default value is false, we are quoting only selected names. + * + * @return true if name have to be quoted + */ + boolean forceQuote() default false; } diff --git a/src/main/java/net/helenus/mapping/annotation/Column.java b/src/main/java/net/helenus/mapping/annotation/Column.java index 466f26c..ab4b2d4 100644 --- a/src/main/java/net/helenus/mapping/annotation/Column.java +++ b/src/main/java/net/helenus/mapping/annotation/Column.java @@ -18,51 +18,45 @@ package net.helenus.mapping.annotation; import java.lang.annotation.*; /** - * Column annotation is used to define additional properties of the column in - * entity mapping interfaces: @Table, @UDT, @Tuple + * Column annotation is used to define additional properties of the column in entity mapping + * interfaces: @Table, @UDT, @Tuple * - *

- * Column annotation can be used to override default name of the column or to - * setup order of the columns in the mapping + *

Column annotation can be used to override default name of the column or to setup order of the + * columns in the mapping * - *

- * Usually for @Table and @UDT types it is not important to define order of the - * columns, but in @Tuple mapping it is required, because tuple itself - * represents the sequence of the types with particular order in the table's - * column + *

Usually for @Table and @UDT types it is not important to define order of the columns, but + * in @Tuple mapping it is required, because tuple itself represents the sequence of the types with + * particular order in the table's column */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) public @interface Column { - /** - * Default value is the name of the method normalized to underscore - * - * @return name of the column - */ - String value() default ""; + /** + * Default value is the name of the method normalized to underscore + * + * @return name of the column + */ + String value() default ""; - /** - * Ordinal will be used for ascending sorting of columns - * - *

- * Default value is 0, because not all mapping entities require all fields to - * have unique ordinals, only @Tuple mapping entity requires all of them to be - * unique. - * - * @return number that used to sort columns, usually for @Tuple only - */ - int ordinal() default 0; + /** + * Ordinal will be used for ascending sorting of columns + * + *

Default value is 0, because not all mapping entities require all fields to have unique + * ordinals, only @Tuple mapping entity requires all of them to be unique. + * + * @return number that used to sort columns, usually for @Tuple only + */ + int ordinal() default 0; - /** - * For reserved words in Cassandra we need quotation in CQL queries. This - * property marks that the name of the UDT type needs to be quoted. - * - *

- * Default value is false, we are quoting only selected names. - * - * @return true if name have to be quoted - */ - boolean forceQuote() default false; + /** + * For reserved words in Cassandra we need quotation in CQL queries. This property marks that the + * name of the UDT type needs to be quoted. + * + *

Default value is false, we are quoting only selected names. + * + * @return true if name have to be quoted + */ + boolean forceQuote() default false; } diff --git a/src/main/java/net/helenus/mapping/annotation/Constraints.java b/src/main/java/net/helenus/mapping/annotation/Constraints.java index 14961b7..d1b99d2 100644 --- a/src/main/java/net/helenus/mapping/annotation/Constraints.java +++ b/src/main/java/net/helenus/mapping/annotation/Constraints.java @@ -16,265 +16,222 @@ package net.helenus.mapping.annotation; import java.lang.annotation.*; - import javax.validation.Constraint; - import net.helenus.mapping.validator.*; /** - * Constraint annotations are using for data integrity mostly - * for @java.lang.String types. The place of the annotation is the particular - * method in model interface. + * Constraint annotations are using for data integrity mostly for @java.lang.String types. The place + * of the annotation is the particular method in model interface. * - *

- * All of them does not have effect on selects and data retrieval operations. + *

All of them does not have effect on selects and data retrieval operations. * - *

- * Support types: - @NotNull supports any @java.lang.Object type - All - * annotations support @java.lang.String type + *

Support types: - @NotNull supports any @java.lang.Object type - All annotations + * support @java.lang.String type */ public final class Constraints { - private Constraints() { - } + private Constraints() {} - /** - * NotNull annotation is using to check that value is not null before storing it - * - *

- * Applicable to use in any @java.lang.Object - * - *

- * It does not check on selects and data retrieval operations - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - @Constraint(validatedBy = NotNullValidator.class) - public @interface NotNull { - } + /** + * NotNull annotation is using to check that value is not null before storing it + * + *

Applicable to use in any @java.lang.Object + * + *

It does not check on selects and data retrieval operations + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + @Constraint(validatedBy = NotNullValidator.class) + public @interface NotNull {} - /** - * NotEmpty annotation is using to check that value has text before storing it - * - *

- * Also checks for the null and it is more strict annotation then @NotNull - * - *

- * Can be used for @java.lang.CharSequence, @ByteBuffer and any array - * - *

- * It does not check on selects and data retrieval operations - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - @Constraint(validatedBy = NotEmptyValidator.class) - public @interface NotEmpty { - } + /** + * NotEmpty annotation is using to check that value has text before storing it + * + *

Also checks for the null and it is more strict annotation then @NotNull + * + *

Can be used for @java.lang.CharSequence, @ByteBuffer and any array + * + *

It does not check on selects and data retrieval operations + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + @Constraint(validatedBy = NotEmptyValidator.class) + public @interface NotEmpty {} - /** - * Email annotation is using to check that value has a valid email before - * storing it - * - *

- * Can be used only for @CharSequence - * - *

- * It does not check on selects and data retrieval operations - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - @Constraint(validatedBy = EmailValidator.class) - public @interface Email { - } + /** + * Email annotation is using to check that value has a valid email before storing it + * + *

Can be used only for @CharSequence + * + *

It does not check on selects and data retrieval operations + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + @Constraint(validatedBy = EmailValidator.class) + public @interface Email {} - /** - * Number annotation is using to check that all letters in value are digits - * before storing it - * - *

- * Can be used only for @java.lang.CharSequence - * - *

- * It does not check on selects and data retrieval operations - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - @Constraint(validatedBy = NumberValidator.class) - public @interface Number { - } + /** + * Number annotation is using to check that all letters in value are digits before storing it + * + *

Can be used only for @java.lang.CharSequence + * + *

It does not check on selects and data retrieval operations + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + @Constraint(validatedBy = NumberValidator.class) + public @interface Number {} - /** - * Alphabet annotation is using to check that all letters in value are in - * specific alphabet before storing it - * - *

- * Can be used only for @java.lang.CharSequence - * - *

- * It does not check on selects and data retrieval operations - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - @Constraint(validatedBy = AlphabetValidator.class) - public @interface Alphabet { + /** + * Alphabet annotation is using to check that all letters in value are in specific alphabet before + * storing it + * + *

Can be used only for @java.lang.CharSequence + * + *

It does not check on selects and data retrieval operations + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + @Constraint(validatedBy = AlphabetValidator.class) + public @interface Alphabet { - /** - * Defines alphabet that will be used to check value - * - * @return alphabet characters in the string - */ - String value(); - } + /** + * Defines alphabet that will be used to check value + * + * @return alphabet characters in the string + */ + String value(); + } - /** - * Length annotation is using to ensure that value has exact length before - * storing it - * - *

- * Can be used for @java.lang.CharSequence, @ByteBuffer and any array - * - *

- * It does not have effect on selects and data retrieval operations - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - @Constraint(validatedBy = LengthValidator.class) - public @interface Length { + /** + * Length annotation is using to ensure that value has exact length before storing it + * + *

Can be used for @java.lang.CharSequence, @ByteBuffer and any array + * + *

It does not have effect on selects and data retrieval operations + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + @Constraint(validatedBy = LengthValidator.class) + public @interface Length { - int value(); - } + int value(); + } - /** - * MaxLength annotation is using to ensure that value has length less or equal - * to some threshold before storing it - * - *

- * Can be used for @java.lang.CharSequence, @ByteBuffer and byte[] - * - *

- * It does not have effect on selects and data retrieval operations - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - @Constraint(validatedBy = MaxLengthValidator.class) - public @interface MaxLength { + /** + * MaxLength annotation is using to ensure that value has length less or equal to some threshold + * before storing it + * + *

Can be used for @java.lang.CharSequence, @ByteBuffer and byte[] + * + *

It does not have effect on selects and data retrieval operations + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + @Constraint(validatedBy = MaxLengthValidator.class) + public @interface MaxLength { - int value(); - } + int value(); + } - /** - * MinLength annotation is using to ensure that value has length greater or - * equal to some threshold before storing it - * - *

- * Can be used for @java.lang.CharSequence, @ByteBuffer and byte[] - * - *

- * It does not have effect on selects and data retrieval operations - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - @Constraint(validatedBy = MinLengthValidator.class) - public @interface MinLength { + /** + * MinLength annotation is using to ensure that value has length greater or equal to some + * threshold before storing it + * + *

Can be used for @java.lang.CharSequence, @ByteBuffer and byte[] + * + *

It does not have effect on selects and data retrieval operations + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + @Constraint(validatedBy = MinLengthValidator.class) + public @interface MinLength { - int value(); - } + int value(); + } - /** - * LowerCase annotation is using to ensure that value is in lower case before - * storing it - * - *

- * Can be used only for @java.lang.CharSequence - * - *

- * It does not have effect on selects and data retrieval operations - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - @Constraint(validatedBy = LowerCaseValidator.class) - public @interface LowerCase { - } + /** + * LowerCase annotation is using to ensure that value is in lower case before storing it + * + *

Can be used only for @java.lang.CharSequence + * + *

It does not have effect on selects and data retrieval operations + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + @Constraint(validatedBy = LowerCaseValidator.class) + public @interface LowerCase {} - /** - * UpperCase annotation is using to ensure that value is in upper case before - * storing it - * - *

- * Can be used only for @java.lang.CharSequence - * - *

- * It does not have effect on selects and data retrieval operations - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - @Constraint(validatedBy = UpperCaseValidator.class) - public @interface UpperCase { - } + /** + * UpperCase annotation is using to ensure that value is in upper case before storing it + * + *

Can be used only for @java.lang.CharSequence + * + *

It does not have effect on selects and data retrieval operations + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + @Constraint(validatedBy = UpperCaseValidator.class) + public @interface UpperCase {} - /** - * Pattern annotation is LowerCase annotation is using to ensure that value is - * upper case before storing it - * - *

- * Can be used only for @java.lang.CharSequence - * - *

- * It does not have effect on selects and data retrieval operations - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - @Constraint(validatedBy = PatternValidator.class) - public @interface Pattern { + /** + * Pattern annotation is LowerCase annotation is using to ensure that value is upper case before + * storing it + * + *

Can be used only for @java.lang.CharSequence + * + *

It does not have effect on selects and data retrieval operations + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + @Constraint(validatedBy = PatternValidator.class) + public @interface Pattern { - /** - * User defined regex expression to check match of the value - * - * @return Java regex pattern - */ - String value(); + /** + * User defined regex expression to check match of the value + * + * @return Java regex pattern + */ + String value(); - /** - * Regex flags composition - * - * @return Java regex flags - */ - int flags(); - } + /** + * Regex flags composition + * + * @return Java regex flags + */ + int flags(); + } - /** - * Distinct annotation is used to signal, but not ensure that a value should be - * distinct in the database. - * - *

- * Can be used only for @java.lang.CharSequence - * - *

- * It does not have effect on selects and data retrieval operations - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - @Constraint(validatedBy = DistinctValidator.class) - public @interface Distinct { + /** + * Distinct annotation is used to signal, but not ensure that a value should be distinct in the + * database. + * + *

Can be used only for @java.lang.CharSequence + * + *

It does not have effect on selects and data retrieval operations + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + @Constraint(validatedBy = DistinctValidator.class) + public @interface Distinct { - /** - * User defined Enum to further restrict the items in the set. - * - * @return Java - */ - Class value() default Enum.class; - - } + /** + * User defined Enum to further restrict the items in the set. + * + * @return Java + */ + Class value() default Enum.class; + } } diff --git a/src/main/java/net/helenus/mapping/annotation/CoveringIndex.java b/src/main/java/net/helenus/mapping/annotation/CoveringIndex.java index 33d8bf4..6271e8e 100644 --- a/src/main/java/net/helenus/mapping/annotation/CoveringIndex.java +++ b/src/main/java/net/helenus/mapping/annotation/CoveringIndex.java @@ -3,52 +3,48 @@ package net.helenus.mapping.annotation; import java.lang.annotation.*; /** - * CoveringIndex annotation is using under the specific column or method in - * entity interface with @Table annotation. + * CoveringIndex annotation is using under the specific column or method in entity interface + * with @Table annotation. * - *

- * A corresponding materialized view will be created based on the - * underline @Table for the specific column. + *

A corresponding materialized view will be created based on the underline @Table for the + * specific column. * - *

- * This is useful when you need to perform IN or SORT/ORDER-BY queries and to do - * so you'll need different materialized table on disk in Cassandra. + *

This is useful when you need to perform IN or SORT/ORDER-BY queries and to do so you'll need + * different materialized table on disk in Cassandra. * - *

- * For each @Table annotated interface Helenus will create/update/verify - * Cassandra Materialized Views and some indexes if needed on startup. + *

For each @Table annotated interface Helenus will create/update/verify Cassandra Materialized + * Views and some indexes if needed on startup. */ @Inherited @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) public @interface CoveringIndex { - /** - * Defined the name of the index. By default the entity name with column name as - * suffix. - * - * @return name of the covering index - */ - String name() default ""; + /** + * Defined the name of the index. By default the entity name with column name as suffix. + * + * @return name of the covering index + */ + String name() default ""; - /** - * Set of fields in this entity to replicate in the index. - * - * @return array of the string names of the fields. - */ - String[] covering() default ""; + /** + * Set of fields in this entity to replicate in the index. + * + * @return array of the string names of the fields. + */ + String[] covering() default ""; - /** - * Set of fields to use as the partition keys for this projection. - * - * @return array of the string names of the fields. - */ - String[] partitionKeys() default ""; + /** + * Set of fields to use as the partition keys for this projection. + * + * @return array of the string names of the fields. + */ + String[] partitionKeys() default ""; - /** - * Set of fields to use as the clustering columns for this projection. - * - * @return array of the string names of the fields. - */ - String[] clusteringColumns() default ""; + /** + * Set of fields to use as the clustering columns for this projection. + * + * @return array of the string names of the fields. + */ + String[] clusteringColumns() default ""; } diff --git a/src/main/java/net/helenus/mapping/annotation/Index.java b/src/main/java/net/helenus/mapping/annotation/Index.java index f0e59d4..7765c44 100644 --- a/src/main/java/net/helenus/mapping/annotation/Index.java +++ b/src/main/java/net/helenus/mapping/annotation/Index.java @@ -18,57 +18,48 @@ package net.helenus.mapping.annotation; import java.lang.annotation.*; /** - * Index annotation is using under the specific column or method in entity - * interface with @Table annotation. + * Index annotation is using under the specific column or method in entity interface with @Table + * annotation. * - *

- * The corresponding secondary index will be created in the underline @Table for - * the specific column. + *

The corresponding secondary index will be created in the underline @Table for the specific + * column. * - *

- * Currently Cassandra supports only single column index, so this index works - * only for single column. + *

Currently Cassandra supports only single column index, so this index works only for single + * column. * - *

- * Make sure that you are using low cardinality columns for this index, that is - * the requirement of the Cassandra. Low cardinality fields examples: gender, - * country, age, status and etc High cardinality fields examples: id, email, - * timestamp, UUID and etc + *

Make sure that you are using low cardinality columns for this index, that is the requirement + * of the Cassandra. Low cardinality fields examples: gender, country, age, status and etc High + * cardinality fields examples: id, email, timestamp, UUID and etc */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) public @interface Index { - /** - * Defined the name of the index. By default will be used the column name. - * - * @return name of the index - */ - String value() default ""; + /** + * Defined the name of the index. By default will be used the column name. + * + * @return name of the index + */ + String value() default ""; - /** - * For reserved words in Cassandra we need quotation in CQL queries. This - * property marks that the name of the UDT type needs to be quoted. - * - *

- * Default value is false, we are quoting only selected names. - * - * @return true if name have to be quoted - */ - boolean forceQuote() default false; + /** + * For reserved words in Cassandra we need quotation in CQL queries. This property marks that the + * name of the UDT type needs to be quoted. + * + *

Default value is false, we are quoting only selected names. + * + * @return true if name have to be quoted + */ + boolean forceQuote() default false; - /** - * Create a case-insensitive index using Cassandra 3.x+ support for SASI - * indexing. - * - * @return true if the index should ignore case when comparing - */ - boolean caseSensitive() default true; + /** + * Create a case-insensitive index using Cassandra 3.x+ support for SASI indexing. + * + * @return true if the index should ignore case when comparing + */ + boolean caseSensitive() default true; - /** - * - * @return - */ - boolean distinct() default false; + /** @return */ + boolean distinct() default false; } diff --git a/src/main/java/net/helenus/mapping/annotation/InheritedTable.java b/src/main/java/net/helenus/mapping/annotation/InheritedTable.java index a7649bd..af1f1dd 100644 --- a/src/main/java/net/helenus/mapping/annotation/InheritedTable.java +++ b/src/main/java/net/helenus/mapping/annotation/InheritedTable.java @@ -20,13 +20,11 @@ import java.lang.annotation.*; /** * Inherited Entity annotation * - *

- * Inherited Table annotation is used to indicate that the methods should also - * be mapped + *

Inherited Table annotation is used to indicate that the methods should also be mapped */ @Inherited @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) public @interface InheritedTable { - String value() default ""; + String value() default ""; } diff --git a/src/main/java/net/helenus/mapping/annotation/MaterializedView.java b/src/main/java/net/helenus/mapping/annotation/MaterializedView.java index 6e8468d..112d8ce 100644 --- a/src/main/java/net/helenus/mapping/annotation/MaterializedView.java +++ b/src/main/java/net/helenus/mapping/annotation/MaterializedView.java @@ -20,38 +20,33 @@ import java.lang.annotation.*; /** * Materialized alternate view of another Entity annotation * - *

- * MaterializedView annotation is used to define different mapping to some other - * Table interface + *

MaterializedView annotation is used to define different mapping to some other Table interface * - *

- * This is useful when you need to perform IN or SORT/ORDER-BY queries and to do - * so you'll need different materialized table on disk in Cassandra. + *

This is useful when you need to perform IN or SORT/ORDER-BY queries and to do so you'll need + * different materialized table on disk in Cassandra. * - *

- * For each @Table annotated interface Helenus will create/update/verify - * Cassandra Materialized Views and some indexes if needed on startup. + *

For each @Table annotated interface Helenus will create/update/verify Cassandra Materialized + * Views and some indexes if needed on startup. */ @Inherited @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) public @interface MaterializedView { - /** - * Default value is the SimpleName of the interface normalized to underscore - * - * @return name of the type - */ - String value() default ""; + /** + * Default value is the SimpleName of the interface normalized to underscore + * + * @return name of the type + */ + String value() default ""; - /** - * For reserved words in Cassandra we need quotation in CQL queries. This - * property marks that the name of the type needs to be quoted. - * - *

- * Default value is false, we are quoting only selected names. - * - * @return true if name have to be quoted - */ - boolean forceQuote() default false; + /** + * For reserved words in Cassandra we need quotation in CQL queries. This property marks that the + * name of the type needs to be quoted. + * + *

Default value is false, we are quoting only selected names. + * + * @return true if name have to be quoted + */ + boolean forceQuote() default false; } diff --git a/src/main/java/net/helenus/mapping/annotation/PartitionKey.java b/src/main/java/net/helenus/mapping/annotation/PartitionKey.java index bca9204..946d9e4 100644 --- a/src/main/java/net/helenus/mapping/annotation/PartitionKey.java +++ b/src/main/java/net/helenus/mapping/annotation/PartitionKey.java @@ -21,55 +21,48 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * PartitionKey annotation is using to define that particular column is the part - * of partition key in the table. + * PartitionKey annotation is using to define that particular column is the part of partition key in + * the table. * - *

- * Partition Key is the routing key. Cassandra is using it to find the primary - * data node in the cluster that holds data. Cassandra combines all parts of the - * partition key to byte array and then calculates hash function by using good - * distribution algorithm (by default MurMur3). After that it uses hash number - * as a token in the ring to find a virtual and then a physical data server. + *

Partition Key is the routing key. Cassandra is using it to find the primary data node in the + * cluster that holds data. Cassandra combines all parts of the partition key to byte array and then + * calculates hash function by using good distribution algorithm (by default MurMur3). After that it + * uses hash number as a token in the ring to find a virtual and then a physical data server. * - *

- * For @Table mapping entity it is required to have as minimum one PartitionKey - * column. For @UDT and @Tuple mapping entities @PartitionKey annotation is not - * using. + *

For @Table mapping entity it is required to have as minimum one PartitionKey column. For @UDT + * and @Tuple mapping entities @PartitionKey annotation is not using. */ @Retention(value = RetentionPolicy.RUNTIME) @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) public @interface PartitionKey { - /** - * Default value is the name of the method normalized to underscore - * - * @return name of the column - */ - String value() default ""; + /** + * Default value is the name of the method normalized to underscore + * + * @return name of the column + */ + String value() default ""; - /** - * PartitionKey parts must be ordered in the @Table. It is the requirement of - * Cassandra. That is how the partition key calculation works, column parts will - * be joined based on some order and final hash/token will be calculated. - * - *

- * Be default ordinal has 0 value, that's because in most cases @Table have - * single column for @PartitionKey If you have 2 and more parts of the - * PartitionKey, then you need to use ordinal() to define the sequence of the - * parts - * - * @return number that used to sort columns in PartitionKey - */ - int ordinal() default 0; + /** + * PartitionKey parts must be ordered in the @Table. It is the requirement of Cassandra. That is + * how the partition key calculation works, column parts will be joined based on some order and + * final hash/token will be calculated. + * + *

Be default ordinal has 0 value, that's because in most cases @Table have single column + * for @PartitionKey If you have 2 and more parts of the PartitionKey, then you need to use + * ordinal() to define the sequence of the parts + * + * @return number that used to sort columns in PartitionKey + */ + int ordinal() default 0; - /** - * For reserved words in Cassandra we need quotation in CQL queries. This - * property marks that the name of the UDT type needs to be quoted. - * - *

- * Default value is false, we are quoting only selected names. - * - * @return true if name have to be quoted - */ - boolean forceQuote() default false; + /** + * For reserved words in Cassandra we need quotation in CQL queries. This property marks that the + * name of the UDT type needs to be quoted. + * + *

Default value is false, we are quoting only selected names. + * + * @return true if name have to be quoted + */ + boolean forceQuote() default false; } diff --git a/src/main/java/net/helenus/mapping/annotation/StaticColumn.java b/src/main/java/net/helenus/mapping/annotation/StaticColumn.java index dde3a2d..c538560 100644 --- a/src/main/java/net/helenus/mapping/annotation/StaticColumn.java +++ b/src/main/java/net/helenus/mapping/annotation/StaticColumn.java @@ -23,41 +23,38 @@ import java.lang.annotation.Target; /** * StaticColumn annotation is using to define a static column in Cassandra Table * - *

- * It does not have effect in @UDT and @Tuple types and in @Table-s that does - * not have @ClusteringColumn-s + *

It does not have effect in @UDT and @Tuple types and in @Table-s that does not + * have @ClusteringColumn-s * - *

- * In case of using @ClusteringColumn we can repeat some information that is - * unique for a row. For this purpose we can define @StaticColumn annotation, - * that will create static column in the table + *

In case of using @ClusteringColumn we can repeat some information that is unique for a row. + * For this purpose we can define @StaticColumn annotation, that will create static column in the + * table */ @Retention(value = RetentionPolicy.RUNTIME) @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) public @interface StaticColumn { - /** - * Default value is the name of the method normalized to underscore - * - * @return name of the column - */ - String value() default ""; + /** + * Default value is the name of the method normalized to underscore + * + * @return name of the column + */ + String value() default ""; - /** - * Ordinal will be used for ascending sorting of static columns - * - * @return number that used to sort columns in PartitionKey - */ - int ordinal() default 0; + /** + * Ordinal will be used for ascending sorting of static columns + * + * @return number that used to sort columns in PartitionKey + */ + int ordinal() default 0; - /** - * For reserved words in Cassandra we need quotation in CQL queries. This - * property marks that the name of the UDT type needs to be quoted. - * - *

- * Default value is false, we are quoting only selected names. - * - * @return true if name have to be quoted - */ - boolean forceQuote() default false; + /** + * For reserved words in Cassandra we need quotation in CQL queries. This property marks that the + * name of the UDT type needs to be quoted. + * + *

Default value is false, we are quoting only selected names. + * + * @return true if name have to be quoted + */ + boolean forceQuote() default false; } diff --git a/src/main/java/net/helenus/mapping/annotation/Table.java b/src/main/java/net/helenus/mapping/annotation/Table.java index 352f6d8..0ddd4af 100644 --- a/src/main/java/net/helenus/mapping/annotation/Table.java +++ b/src/main/java/net/helenus/mapping/annotation/Table.java @@ -20,36 +20,32 @@ import java.lang.annotation.*; /** * Entity annotation * - *

- * Table annotation is used to define Table mapping to some interface + *

Table annotation is used to define Table mapping to some interface * - *

- * There are three types of Entity mapping annotations: @Table, @UDT, @Tuple + *

There are three types of Entity mapping annotations: @Table, @UDT, @Tuple * - *

- * For each @Table annotated interface Helenus will create/update/verify - * Cassandra Table and some indexes if needed on startup. + *

For each @Table annotated interface Helenus will create/update/verify Cassandra Table and some + * indexes if needed on startup. */ @Inherited @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) public @interface Table { - /** - * Default value is the SimpleName of the interface normalized to underscore - * - * @return name of the UDT type - */ - String value() default ""; + /** + * Default value is the SimpleName of the interface normalized to underscore + * + * @return name of the UDT type + */ + String value() default ""; - /** - * For reserved words in Cassandra we need quotation in CQL queries. This - * property marks that the name of the UDT type needs to be quoted. - * - *

- * Default value is false, we are quoting only selected names. - * - * @return true if name have to be quoted - */ - boolean forceQuote() default false; + /** + * For reserved words in Cassandra we need quotation in CQL queries. This property marks that the + * name of the UDT type needs to be quoted. + * + *

Default value is false, we are quoting only selected names. + * + * @return true if name have to be quoted + */ + boolean forceQuote() default false; } diff --git a/src/main/java/net/helenus/mapping/annotation/Transient.java b/src/main/java/net/helenus/mapping/annotation/Transient.java index d4c67fd..b988122 100644 --- a/src/main/java/net/helenus/mapping/annotation/Transient.java +++ b/src/main/java/net/helenus/mapping/annotation/Transient.java @@ -17,12 +17,8 @@ package net.helenus.mapping.annotation; import java.lang.annotation.*; -/** - * Transient annotation is used to mark properties that are need not be mapped - * to the database. - */ +/** Transient annotation is used to mark properties that are need not be mapped to the database. */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) -public @interface Transient { -} +public @interface Transient {} diff --git a/src/main/java/net/helenus/mapping/annotation/Tuple.java b/src/main/java/net/helenus/mapping/annotation/Tuple.java index 5f1ca4a..e2da9e1 100644 --- a/src/main/java/net/helenus/mapping/annotation/Tuple.java +++ b/src/main/java/net/helenus/mapping/annotation/Tuple.java @@ -20,19 +20,15 @@ import java.lang.annotation.*; /** * Entity annotation * - *

- * Tuple annotation is used to define Tuple type mapping to some interface + *

Tuple annotation is used to define Tuple type mapping to some interface * - *

- * There are three types of Entity mapping annotations: @Table, @UDT, @Tuple + *

There are three types of Entity mapping annotations: @Table, @UDT, @Tuple * - *

- * Tuple is fully embedded type, it is the sequence of the underline types and - * the order of the sub-types is important, therefore all @Column-s must have - * ordinal() and only @Column annotation supported for underline types + *

Tuple is fully embedded type, it is the sequence of the underline types and the order of the + * sub-types is important, therefore all @Column-s must have ordinal() and only @Column annotation + * supported for underline types */ @Inherited @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) -public @interface Tuple { -} +public @interface Tuple {} diff --git a/src/main/java/net/helenus/mapping/annotation/Types.java b/src/main/java/net/helenus/mapping/annotation/Types.java index 682396d..eedb398 100644 --- a/src/main/java/net/helenus/mapping/annotation/Types.java +++ b/src/main/java/net/helenus/mapping/annotation/Types.java @@ -15,511 +15,412 @@ */ package net.helenus.mapping.annotation; +import com.datastax.driver.core.DataType; import java.lang.annotation.*; -import com.datastax.driver.core.DataType; - /** - * Types annotations are using for clarification of Cassandra data type for - * particular Java type. + * Types annotations are using for clarification of Cassandra data type for particular Java type. * - *

- * Sometimes it is possible to have for single Java type multiple Cassandra data - * types: - @String can be @DataType.Name.ASCII or @DataType.Name.TEXT - * or @DataType.Name.VARCHAR - @Long can be @DataType.Name.BIGINT - * or @DataType.Name.COUNTER + *

Sometimes it is possible to have for single Java type multiple Cassandra data types: - @String + * can be @DataType.Name.ASCII or @DataType.Name.TEXT or @DataType.Name.VARCHAR - @Long can + * be @DataType.Name.BIGINT or @DataType.Name.COUNTER * - *

- * All those type annotations simplify mapping between Java types and Cassandra - * data types. They are not required, for each Java type there is a default - * Cassandra data type in Helenus, but in some cases you would like to control - * mapping to make sure that the right Cassandra data type is using. + *

All those type annotations simplify mapping between Java types and Cassandra data types. They + * are not required, for each Java type there is a default Cassandra data type in Helenus, but in + * some cases you would like to control mapping to make sure that the right Cassandra data type is + * using. * - *

- * For complex types like collections, UDF and Tuple types all those annotations - * are using to clarify the sub-type(s) or class/UDF names. + *

For complex types like collections, UDF and Tuple types all those annotations are using to + * clarify the sub-type(s) or class/UDF names. * - *

- * Has significant effect on schema operations. + *

Has significant effect on schema operations. */ public final class Types { - private Types() { - } + private Types() {} - /** Says to use @DataType.Name.ASCII data type in schema Java type is @String */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface Ascii { - } + /** Says to use @DataType.Name.ASCII data type in schema Java type is @String */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface Ascii {} - /** Says to use @DataType.Name.BIGINT data type in schema Java type is @Long */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface Bigint { - } + /** Says to use @DataType.Name.BIGINT data type in schema Java type is @Long */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface Bigint {} - /** - * Says to use @DataType.Name.BLOB data type in schema Java type is @ByteBuffer - * or @byte[] Using by default - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface Blob { - } + /** + * Says to use @DataType.Name.BLOB data type in schema Java type is @ByteBuffer or @byte[] Using + * by default + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface Blob {} - /** - * Says to use @DataType.Name.LIST data type in schema with specific sub-type - * Java type is @List - * - *

- * Helenus does not allow to use a specific implementation of the collection - * thereof data retrieval operation result can be a collection with another - * implementation. - * - *

- * This annotation is usually used only for sub-types clarification and only in - * case if sub-type is Java type that corresponds to multiple Cassandra data - * types. - * - *

- * For this type there are special operations: prepend, prependAll, setIdx, - * append, appendAll, discard and discardAll in @UpdateOperation - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface List { + /** + * Says to use @DataType.Name.LIST data type in schema with specific sub-type Java type is @List + * + *

Helenus does not allow to use a specific implementation of the collection thereof data + * retrieval operation result can be a collection with another implementation. + * + *

This annotation is usually used only for sub-types clarification and only in case if + * sub-type is Java type that corresponds to multiple Cassandra data types. + * + *

For this type there are special operations: prepend, prependAll, setIdx, append, appendAll, + * discard and discardAll in @UpdateOperation + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface List { - /** - * Clarification of using the sub-type data type in the collection. It supports - * only simple data type (not Collection, UDT or Tuple) - * - *

- * In case if you need UDT sub-type in the list, consider @UDTList annotation - * - * @return data type name of the value - */ - DataType.Name value(); - } + /** + * Clarification of using the sub-type data type in the collection. It supports only simple data + * type (not Collection, UDT or Tuple) + * + *

In case if you need UDT sub-type in the list, consider @UDTList annotation + * + * @return data type name of the value + */ + DataType.Name value(); + } - /** - * Says to use @DataType.Name.MAP data type in schema with specific sub-types - * Java type is @Map - * - *

- * Helenus does not allow to use a specific implementation of the collection - * thereof data retrieval operation result can be a collection with another - * implementation. - * - *

- * This annotation is usually used only for sub-types clarification and only in - * case if sub-type is Java type that corresponds to multiple Cassandra data - * types. - * - *

- * For this type there are special operations: put and putAll - * in @UpdateOperation. - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface Map { + /** + * Says to use @DataType.Name.MAP data type in schema with specific sub-types Java type is @Map + * + *

Helenus does not allow to use a specific implementation of the collection thereof data + * retrieval operation result can be a collection with another implementation. + * + *

This annotation is usually used only for sub-types clarification and only in case if + * sub-type is Java type that corresponds to multiple Cassandra data types. + * + *

For this type there are special operations: put and putAll in @UpdateOperation. + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface Map { - /** - * Clarification of using the sub-type data type in the collection. It supports - * only simple data type (not Collection, UDT or Tuple) - * - *

- * In case if you need UDT key sub-type in the map, consider @UDTKeyMap - * or @UDTMap annotations - * - * @return data type name of the key - */ - DataType.Name key(); + /** + * Clarification of using the sub-type data type in the collection. It supports only simple data + * type (not Collection, UDT or Tuple) + * + *

In case if you need UDT key sub-type in the map, consider @UDTKeyMap or @UDTMap + * annotations + * + * @return data type name of the key + */ + DataType.Name key(); - /** - * Clarification of using the sub-type data type in the collection. It supports - * only simple data type (not Collection, UDT or Tuple) - * - *

- * In case if you need UDT value sub-type in the map, consider @UDTValueMap - * or @UDTMap annotations - * - * @return data type name of the value - */ - DataType.Name value(); - } + /** + * Clarification of using the sub-type data type in the collection. It supports only simple data + * type (not Collection, UDT or Tuple) + * + *

In case if you need UDT value sub-type in the map, consider @UDTValueMap or @UDTMap + * annotations + * + * @return data type name of the value + */ + DataType.Name value(); + } - /** - * Says to use @DataType.Name.COUNTER type in schema Java type is @Long - * - *

- * For this type there are special operations: increment and decrement - * in @UpdateOperation. You do not need to initialize counter value, it will be - * done automatically by Cassandra. - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface Counter { - } + /** + * Says to use @DataType.Name.COUNTER type in schema Java type is @Long + * + *

For this type there are special operations: increment and decrement in @UpdateOperation. You + * do not need to initialize counter value, it will be done automatically by Cassandra. + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface Counter {} - /** - * Says to use @DataType.Name.SET data type in schema with specific sub-type - * Java type is @Set - * - *

- * Helenus does not allow to use a specific implementation of the collection - * thereof data retrieval operation result can be a collection with another - * implementation. - * - *

- * This annotation is usually used only for sub-types clarification and only in - * case if sub-type is Java type that corresponds to multiple Cassandra data - * types. - * - *

- * For this type there are special operations: add, addAll, remove and removeAll - * in @UpdateOperation. - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface Set { + /** + * Says to use @DataType.Name.SET data type in schema with specific sub-type Java type is @Set + * + *

Helenus does not allow to use a specific implementation of the collection thereof data + * retrieval operation result can be a collection with another implementation. + * + *

This annotation is usually used only for sub-types clarification and only in case if + * sub-type is Java type that corresponds to multiple Cassandra data types. + * + *

For this type there are special operations: add, addAll, remove and removeAll + * in @UpdateOperation. + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface Set { - /** - * Clarification of using the sub-type data type in the collection. It supports - * only simple data type (not Collection, UDT or Tuple) - * - *

- * In case if you need UDT sub-type in the set, consider @UDTSet annotation - * - * @return data type name of the value - */ - DataType.Name value(); - } + /** + * Clarification of using the sub-type data type in the collection. It supports only simple data + * type (not Collection, UDT or Tuple) + * + *

In case if you need UDT sub-type in the set, consider @UDTSet annotation + * + * @return data type name of the value + */ + DataType.Name value(); + } - /** - * Says to use @DataType.Name.CUSTOM type in schema Java type is @ByteBuffer - * or @byte[] - * - *

- * Uses for custom user types that has special implementation. Helenus does not - * deal with this class directly for now, uses only in serialized form. - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface Custom { + /** + * Says to use @DataType.Name.CUSTOM type in schema Java type is @ByteBuffer or @byte[] + * + *

Uses for custom user types that has special implementation. Helenus does not deal with this + * class directly for now, uses only in serialized form. + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface Custom { - /** - * Class name of the custom user type that is implementation of the type - * - * @return class name of the custom type implementation - */ - String className(); - } + /** + * Class name of the custom user type that is implementation of the type + * + * @return class name of the custom type implementation + */ + String className(); + } - /** - * Says to use @DataType.Name.TEXT type in schema Java type is @String Using by - * default - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface Text { - } + /** Says to use @DataType.Name.TEXT type in schema Java type is @String Using by default */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface Text {} - /** - * Says to use @DataType.Name.TIMESTAMP type in schema Java type is @Date Using - * by default - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface Timestamp { - } + /** Says to use @DataType.Name.TIMESTAMP type in schema Java type is @Date Using by default */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface Timestamp {} - /** - * Says to use @DataType.Name.TIMEUUID type in schema Java type is @UUID - * or @Date - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface Timeuuid { - } + /** Says to use @DataType.Name.TIMEUUID type in schema Java type is @UUID or @Date */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface Timeuuid {} - /** - * Says to use @DataType.Name.TUPLE type in schema Java type is @TupleValue or - * model interface with @Tuple annotation - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - public @interface Tuple { + /** + * Says to use @DataType.Name.TUPLE type in schema Java type is @TupleValue or model interface + * with @Tuple annotation + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + public @interface Tuple { - /** - * If Java type is the @TupleValue then this field is required. Any Cassandra - * Tuple is the sequence of Cassandra types. For now Helenus supports only - * simple data types in tuples for @TupleValue Java type - * - *

- * In case if Java type is the model interface with @Tuple annotation then all - * methods in this interface can have Types annotations that can be complex - * types as well. - * - * @return data type name sequence - */ - DataType.Name[] value() default {}; - } + /** + * If Java type is the @TupleValue then this field is required. Any Cassandra Tuple is the + * sequence of Cassandra types. For now Helenus supports only simple data types in tuples + * for @TupleValue Java type + * + *

In case if Java type is the model interface with @Tuple annotation then all methods in + * this interface can have Types annotations that can be complex types as well. + * + * @return data type name sequence + */ + DataType.Name[] value() default {}; + } - /** - * Says to use @DataType.Name.UDT type in schema Java type is @UDTValue or model - * interface with @UDT annotation - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface UDT { + /** + * Says to use @DataType.Name.UDT type in schema Java type is @UDTValue or model interface + * with @UDT annotation + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface UDT { - /** - * If Java type is the @UDTValue then this field is required. Any Cassandra UDT - * has name and must be created before this use as a Cassandra Type. - * - *

- * This value is the UDT name of the Cassandra Type that was already created in - * the schema - * - *

- * In case of Java type is the model interface with @UDT annotation then this - * field is not using since model interface defines UserDefinedType with - * specific name - * - * @return UDT name - */ - String value() default ""; + /** + * If Java type is the @UDTValue then this field is required. Any Cassandra UDT has name and + * must be created before this use as a Cassandra Type. + * + *

This value is the UDT name of the Cassandra Type that was already created in the schema + * + *

In case of Java type is the model interface with @UDT annotation then this field is not + * using since model interface defines UserDefinedType with specific name + * + * @return UDT name + */ + String value() default ""; - /** - * Only used for JavaType @UDTValue - * - *

- * In case if value() method returns reserved word that can not be used as a - * name of UDT then forceQuote will add additional quotes around this name in - * all CQL queries. - * - *

- * Default value is false. - * - * @return true if quotation is needed - */ - boolean forceQuote() default false; - } + /** + * Only used for JavaType @UDTValue + * + *

In case if value() method returns reserved word that can not be used as a name of UDT then + * forceQuote will add additional quotes around this name in all CQL queries. + * + *

Default value is false. + * + * @return true if quotation is needed + */ + boolean forceQuote() default false; + } - /** - * Says to use @DataType.Name.MAP data type in schema with specific UDT sub-type - * as a key and simple sub-type as a value Java type is @Map - * - *

- * Helenus does not allow to use a specific implementation of the collection - * thereof data retrieval operation result can be a collection with another - * implementation. - * - *

- * This annotation is usually used only for sub-types clarification and only in - * case if sub-type is Java type that corresponds to multiple Cassandra data - * types. - * - *

- * For this type there are special operations: put and putAll - * in @UpdateOperation. - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface UDTKeyMap { + /** + * Says to use @DataType.Name.MAP data type in schema with specific UDT sub-type as a key and + * simple sub-type as a value Java type is @Map + * + *

Helenus does not allow to use a specific implementation of the collection thereof data + * retrieval operation result can be a collection with another implementation. + * + *

This annotation is usually used only for sub-types clarification and only in case if + * sub-type is Java type that corresponds to multiple Cassandra data types. + * + *

For this type there are special operations: put and putAll in @UpdateOperation. + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface UDTKeyMap { - /** - * Clarification of using the UDT data type as a key sub-type in the collection. - * - * @return annotation of UDT type - */ - UDT key(); + /** + * Clarification of using the UDT data type as a key sub-type in the collection. + * + * @return annotation of UDT type + */ + UDT key(); - /** - * Clarification of using the sub-type data type in the collection. It supports - * only simple data type (not Collection, UDT or Tuple) - * - *

- * In case if you need UDT value sub-type in the map, consider @UDTMap - * annotations - * - * @return data type name of the value - */ - DataType.Name value(); - } + /** + * Clarification of using the sub-type data type in the collection. It supports only simple data + * type (not Collection, UDT or Tuple) + * + *

In case if you need UDT value sub-type in the map, consider @UDTMap annotations + * + * @return data type name of the value + */ + DataType.Name value(); + } - /** - * Says to use @DataType.Name.LIST data type in schema with specific UDT - * sub-type Java type is @List - * - *

- * Helenus does not allow to use a specific implementation of the collection - * thereof data retrieval operation result can be a collection with another - * implementation. - * - *

- * This annotation is usually used only for sub-types clarification and only in - * case if sub-type is Java type that corresponds to multiple Cassandra data - * types. - * - *

- * For this type there are special operations: prepend, prependAll, setIdx, - * append, appendAll, discard and discardAll in @UpdateOperation - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface UDTList { + /** + * Says to use @DataType.Name.LIST data type in schema with specific UDT sub-type Java type + * is @List + * + *

Helenus does not allow to use a specific implementation of the collection thereof data + * retrieval operation result can be a collection with another implementation. + * + *

This annotation is usually used only for sub-types clarification and only in case if + * sub-type is Java type that corresponds to multiple Cassandra data types. + * + *

For this type there are special operations: prepend, prependAll, setIdx, append, appendAll, + * discard and discardAll in @UpdateOperation + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface UDTList { - /** - * Clarification of using the UDT data type as a sub-type in the collection. - * - * @return annotation of the UDT value - */ - UDT value(); - } + /** + * Clarification of using the UDT data type as a sub-type in the collection. + * + * @return annotation of the UDT value + */ + UDT value(); + } - /** - * Says to use @DataType.Name.MAP data type in schema with specific UDT - * sub-types Java type is @Map - * - *

- * Helenus does not allow to use a specific implementation of the collection - * thereof data retrieval operation result can be a collection with another - * implementation. - * - *

- * This annotation is usually used only for sub-types clarification and only in - * case if sub-type is Java type that corresponds to multiple Cassandra data - * types. - * - *

- * For this type there are special operations: put and putAll - * in @UpdateOperation. - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface UDTMap { + /** + * Says to use @DataType.Name.MAP data type in schema with specific UDT sub-types Java type + * is @Map + * + *

Helenus does not allow to use a specific implementation of the collection thereof data + * retrieval operation result can be a collection with another implementation. + * + *

This annotation is usually used only for sub-types clarification and only in case if + * sub-type is Java type that corresponds to multiple Cassandra data types. + * + *

For this type there are special operations: put and putAll in @UpdateOperation. + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface UDTMap { - /** - * Clarification of using the UDT data type as a key sub-type in the collection. - * - * @return annotation of the UDT key - */ - UDT key(); + /** + * Clarification of using the UDT data type as a key sub-type in the collection. + * + * @return annotation of the UDT key + */ + UDT key(); - /** - * Clarification of using the UDT data type as a value sub-type in the - * collection. - * - * @return annotation of the UDT value - */ - UDT value(); - } + /** + * Clarification of using the UDT data type as a value sub-type in the collection. + * + * @return annotation of the UDT value + */ + UDT value(); + } - /** - * Says to use @DataType.Name.SET data type in schema with specific UDT sub-type - * Java type is @Set - * - *

- * Helenus does not allow to use a specific implementation of the collection - * thereof data retrieval operation result can be a collection with another - * implementation. - * - *

- * This annotation is usually used only for sub-types clarification and only in - * case if sub-type is Java type that corresponds to multiple Cassandra data - * types. - * - *

- * For this type there are special operations: add, addAll, remove and removeAll - * in @UpdateOperation. - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface UDTSet { + /** + * Says to use @DataType.Name.SET data type in schema with specific UDT sub-type Java type is @Set + * + *

Helenus does not allow to use a specific implementation of the collection thereof data + * retrieval operation result can be a collection with another implementation. + * + *

This annotation is usually used only for sub-types clarification and only in case if + * sub-type is Java type that corresponds to multiple Cassandra data types. + * + *

For this type there are special operations: add, addAll, remove and removeAll + * in @UpdateOperation. + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface UDTSet { - /** - * Clarification of using the UDT data type as a sub-type in the collection. - * - * @return annotation of the UDT value - */ - UDT value(); - } + /** + * Clarification of using the UDT data type as a sub-type in the collection. + * + * @return annotation of the UDT value + */ + UDT value(); + } - /** - * Says to use @DataType.Name.MAP data type in schema with specific simple - * sub-type as a key and UDT sub-type as a value Java type is @Map - * - *

- * Helenus does not allow to use a specific implementation of the collection - * thereof data retrieval operation result can be a collection with another - * implementation. - * - *

- * This annotation is usually used only for sub-types clarification and only in - * case if sub-type is Java type that corresponds to multiple Cassandra data - * types. - * - *

- * For this type there are special operations: put and putAll - * in @UpdateOperation. - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface UDTValueMap { + /** + * Says to use @DataType.Name.MAP data type in schema with specific simple sub-type as a key and + * UDT sub-type as a value Java type is @Map + * + *

Helenus does not allow to use a specific implementation of the collection thereof data + * retrieval operation result can be a collection with another implementation. + * + *

This annotation is usually used only for sub-types clarification and only in case if + * sub-type is Java type that corresponds to multiple Cassandra data types. + * + *

For this type there are special operations: put and putAll in @UpdateOperation. + */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface UDTValueMap { - /** - * Clarification of using the sub-type data type in the collection. It supports - * only simple data type (not Collection, UDT or Tuple) - * - *

- * In case if you need UDT key sub-type in the map, consider @UDTMap annotations - * - * @return data type name of the key - */ - DataType.Name key(); + /** + * Clarification of using the sub-type data type in the collection. It supports only simple data + * type (not Collection, UDT or Tuple) + * + *

In case if you need UDT key sub-type in the map, consider @UDTMap annotations + * + * @return data type name of the key + */ + DataType.Name key(); - /** - * Clarification of using the UDT data type as a value sub-type in the - * collection. - * - * @return annotation of the UDT value - */ - UDT value(); - } + /** + * Clarification of using the UDT data type as a value sub-type in the collection. + * + * @return annotation of the UDT value + */ + UDT value(); + } - /** - * Says to use @DataType.Name.UUID type in schema Java type is @UUID Using by - * default - */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface Uuid { - } + /** Says to use @DataType.Name.UUID type in schema Java type is @UUID Using by default */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface Uuid {} - /** Says to use @DataType.Name.VARCHAR type in schema Java type is @String */ - @Documented - @Retention(RetentionPolicy.RUNTIME) - @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) - public @interface Varchar { - } + /** Says to use @DataType.Name.VARCHAR type in schema Java type is @String */ + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) + public @interface Varchar {} } diff --git a/src/main/java/net/helenus/mapping/annotation/UDT.java b/src/main/java/net/helenus/mapping/annotation/UDT.java index 573688a..e330221 100644 --- a/src/main/java/net/helenus/mapping/annotation/UDT.java +++ b/src/main/java/net/helenus/mapping/annotation/UDT.java @@ -31,21 +31,20 @@ import java.lang.annotation.*; @Target({ElementType.TYPE}) public @interface UDT { - /** - * Default value is the SimpleName of the interface normalized to underscore - * - * @return name of the UDT type - */ - String value() default ""; + /** + * Default value is the SimpleName of the interface normalized to underscore + * + * @return name of the UDT type + */ + String value() default ""; - /** - * For reserved words in Cassandra we need quotation in CQL queries. This - * property marks that the name of the UDT type needs to be quoted. - * - *

- * Default value is false, we are quoting only selected names. - * - * @return true if name have to be quoted - */ - boolean forceQuote() default false; + /** + * For reserved words in Cassandra we need quotation in CQL queries. This property marks that the + * name of the UDT type needs to be quoted. + * + *

Default value is false, we are quoting only selected names. + * + * @return true if name have to be quoted + */ + boolean forceQuote() default false; } diff --git a/src/main/java/net/helenus/mapping/convert/AbstractEntityValueWriter.java b/src/main/java/net/helenus/mapping/convert/AbstractEntityValueWriter.java index 91b3374..749806b 100644 --- a/src/main/java/net/helenus/mapping/convert/AbstractEntityValueWriter.java +++ b/src/main/java/net/helenus/mapping/convert/AbstractEntityValueWriter.java @@ -16,7 +16,6 @@ package net.helenus.mapping.convert; import java.util.Map; - import net.helenus.core.Helenus; import net.helenus.core.reflect.MapExportable; import net.helenus.mapping.HelenusEntity; @@ -25,48 +24,48 @@ import net.helenus.mapping.value.BeanColumnValueProvider; public abstract class AbstractEntityValueWriter { - final HelenusEntity entity; + final HelenusEntity entity; - public AbstractEntityValueWriter(Class iface) { - this.entity = Helenus.entity(iface); - } + public AbstractEntityValueWriter(Class iface) { + this.entity = Helenus.entity(iface); + } - abstract void writeColumn(V outValue, Object value, HelenusProperty prop); + abstract void writeColumn(V outValue, Object value, HelenusProperty prop); - public void write(V outValue, Object source) { + public void write(V outValue, Object source) { - if (source instanceof MapExportable) { + if (source instanceof MapExportable) { - MapExportable exportable = (MapExportable) source; + MapExportable exportable = (MapExportable) source; - Map propertyToValueMap = exportable.toMap(); + Map propertyToValueMap = exportable.toMap(); - for (Map.Entry entry : propertyToValueMap.entrySet()) { + for (Map.Entry entry : propertyToValueMap.entrySet()) { - Object value = entry.getValue(); + Object value = entry.getValue(); - if (value == null) { - continue; - } + if (value == null) { + continue; + } - HelenusProperty prop = entity.getProperty(entry.getKey()); + HelenusProperty prop = entity.getProperty(entry.getKey()); - if (prop != null) { + if (prop != null) { - writeColumn(outValue, value, prop); - } - } + writeColumn(outValue, value, prop); + } + } - } else { + } else { - for (HelenusProperty prop : entity.getOrderedProperties()) { + for (HelenusProperty prop : entity.getOrderedProperties()) { - Object value = BeanColumnValueProvider.INSTANCE.getColumnValue(source, -1, prop); + Object value = BeanColumnValueProvider.INSTANCE.getColumnValue(source, -1, prop); - if (value != null) { - writeColumn(outValue, value, prop); - } - } - } - } + if (value != null) { + writeColumn(outValue, value, prop); + } + } + } + } } diff --git a/src/main/java/net/helenus/mapping/convert/ByteArrayToByteBufferConverter.java b/src/main/java/net/helenus/mapping/convert/ByteArrayToByteBufferConverter.java index 21a121b..f54b332 100644 --- a/src/main/java/net/helenus/mapping/convert/ByteArrayToByteBufferConverter.java +++ b/src/main/java/net/helenus/mapping/convert/ByteArrayToByteBufferConverter.java @@ -19,15 +19,15 @@ import java.nio.ByteBuffer; import java.util.function.Function; public enum ByteArrayToByteBufferConverter implements Function { - INSTANCE; + INSTANCE; - @Override - public ByteBuffer apply(byte[] t) { + @Override + public ByteBuffer apply(byte[] t) { - if (t == null) { - return null; - } + if (t == null) { + return null; + } - return ByteBuffer.wrap(t); - } + return ByteBuffer.wrap(t); + } } diff --git a/src/main/java/net/helenus/mapping/convert/ByteBufferToByteArrayConverter.java b/src/main/java/net/helenus/mapping/convert/ByteBufferToByteArrayConverter.java index bf9e130..c8183ab 100644 --- a/src/main/java/net/helenus/mapping/convert/ByteBufferToByteArrayConverter.java +++ b/src/main/java/net/helenus/mapping/convert/ByteBufferToByteArrayConverter.java @@ -19,15 +19,15 @@ import java.nio.ByteBuffer; import java.util.function.Function; public enum ByteBufferToByteArrayConverter implements Function { - INSTANCE; + INSTANCE; - @Override - public byte[] apply(ByteBuffer t) { + @Override + public byte[] apply(ByteBuffer t) { - if (t == null) { - return null; - } + if (t == null) { + return null; + } - return t.array(); - } + return t.array(); + } } diff --git a/src/main/java/net/helenus/mapping/convert/CamelCaseToUnderscoreConverter.java b/src/main/java/net/helenus/mapping/convert/CamelCaseToUnderscoreConverter.java index b09d820..6f2ce20 100644 --- a/src/main/java/net/helenus/mapping/convert/CamelCaseToUnderscoreConverter.java +++ b/src/main/java/net/helenus/mapping/convert/CamelCaseToUnderscoreConverter.java @@ -15,20 +15,19 @@ */ package net.helenus.mapping.convert; +import com.google.common.base.CaseFormat; import java.util.function.Function; -import com.google.common.base.CaseFormat; - public enum CamelCaseToUnderscoreConverter implements Function { - INSTANCE; + INSTANCE; - @Override - public String apply(String source) { + @Override + public String apply(String source) { - if (source == null) { - throw new IllegalArgumentException("empty parameter"); - } + if (source == null) { + throw new IllegalArgumentException("empty parameter"); + } - return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, source); - } + return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, source); + } } diff --git a/src/main/java/net/helenus/mapping/convert/DateToTimeuuidConverter.java b/src/main/java/net/helenus/mapping/convert/DateToTimeuuidConverter.java index c4eae86..7db03d9 100644 --- a/src/main/java/net/helenus/mapping/convert/DateToTimeuuidConverter.java +++ b/src/main/java/net/helenus/mapping/convert/DateToTimeuuidConverter.java @@ -18,16 +18,15 @@ package net.helenus.mapping.convert; import java.util.Date; import java.util.UUID; import java.util.function.Function; - import net.helenus.support.Timeuuid; /** Simple Date to TimeUUID Converter */ public enum DateToTimeuuidConverter implements Function { - INSTANCE; + INSTANCE; - @Override - public UUID apply(Date source) { - long milliseconds = source.getTime(); - return Timeuuid.of(milliseconds); - } + @Override + public UUID apply(Date source) { + long milliseconds = source.getTime(); + return Timeuuid.of(milliseconds); + } } diff --git a/src/main/java/net/helenus/mapping/convert/EnumToStringConverter.java b/src/main/java/net/helenus/mapping/convert/EnumToStringConverter.java index 7020f7e..15e7d04 100644 --- a/src/main/java/net/helenus/mapping/convert/EnumToStringConverter.java +++ b/src/main/java/net/helenus/mapping/convert/EnumToStringConverter.java @@ -19,10 +19,10 @@ import java.util.function.Function; /** Enum to String Converter */ public enum EnumToStringConverter implements Function { - INSTANCE; + INSTANCE; - @Override - public String apply(Enum source) { - return source.name(); - } + @Override + public String apply(Enum source) { + return source.name(); + } } diff --git a/src/main/java/net/helenus/mapping/convert/ProxyValueReader.java b/src/main/java/net/helenus/mapping/convert/ProxyValueReader.java index 07b4419..8100679 100644 --- a/src/main/java/net/helenus/mapping/convert/ProxyValueReader.java +++ b/src/main/java/net/helenus/mapping/convert/ProxyValueReader.java @@ -17,7 +17,6 @@ package net.helenus.mapping.convert; import java.util.Map; import java.util.function.Function; - import net.helenus.core.Helenus; import net.helenus.mapping.HelenusEntity; import net.helenus.mapping.value.ColumnValueProvider; @@ -25,23 +24,23 @@ import net.helenus.mapping.value.ValueProviderMap; public class ProxyValueReader implements Function { - private final Class iface; - private final HelenusEntity entity; - private final ColumnValueProvider valueProvider; + private final Class iface; + private final HelenusEntity entity; + private final ColumnValueProvider valueProvider; - public ProxyValueReader(Class iface, ColumnValueProvider valueProvider) { - this.iface = iface; - this.entity = Helenus.entity(iface); - this.valueProvider = valueProvider; - } + public ProxyValueReader(Class iface, ColumnValueProvider valueProvider) { + this.iface = iface; + this.entity = Helenus.entity(iface); + this.valueProvider = valueProvider; + } - @Override - public Object apply(T source) { - if (source != null) { - Map map = new ValueProviderMap(source, valueProvider, entity); + @Override + public Object apply(T source) { + if (source != null) { + Map map = new ValueProviderMap(source, valueProvider, entity); - return Helenus.map(iface, map); - } - return null; - } + return Helenus.map(iface, map); + } + return null; + } } diff --git a/src/main/java/net/helenus/mapping/convert/StringToEnumConverter.java b/src/main/java/net/helenus/mapping/convert/StringToEnumConverter.java index 41358ab..ec632f5 100644 --- a/src/main/java/net/helenus/mapping/convert/StringToEnumConverter.java +++ b/src/main/java/net/helenus/mapping/convert/StringToEnumConverter.java @@ -19,14 +19,14 @@ import java.util.function.Function; public class StringToEnumConverter implements Function { - private final Class enumClass; + private final Class enumClass; - public StringToEnumConverter(Class enumClass) { - this.enumClass = (Class) enumClass; - } + public StringToEnumConverter(Class enumClass) { + this.enumClass = (Class) enumClass; + } - @Override - public Enum apply(String source) { - return Enum.valueOf(enumClass, source); - } + @Override + public Enum apply(String source) { + return Enum.valueOf(enumClass, source); + } } diff --git a/src/main/java/net/helenus/mapping/convert/TimeuuidToDateConverter.java b/src/main/java/net/helenus/mapping/convert/TimeuuidToDateConverter.java index 51ee3df..352e019 100644 --- a/src/main/java/net/helenus/mapping/convert/TimeuuidToDateConverter.java +++ b/src/main/java/net/helenus/mapping/convert/TimeuuidToDateConverter.java @@ -18,14 +18,13 @@ package net.helenus.mapping.convert; import java.util.Date; import java.util.UUID; import java.util.function.Function; - import net.helenus.support.Timeuuid; public enum TimeuuidToDateConverter implements Function { - INSTANCE; + INSTANCE; - @Override - public Date apply(UUID source) { - return new Date(Timeuuid.getTimestampMillis(source)); - } + @Override + public Date apply(UUID source) { + return new Date(Timeuuid.getTimestampMillis(source)); + } } diff --git a/src/main/java/net/helenus/mapping/convert/TupleValueWriter.java b/src/main/java/net/helenus/mapping/convert/TupleValueWriter.java index 4ab4ebb..b197d5c 100644 --- a/src/main/java/net/helenus/mapping/convert/TupleValueWriter.java +++ b/src/main/java/net/helenus/mapping/convert/TupleValueWriter.java @@ -15,45 +15,44 @@ */ package net.helenus.mapping.convert; -import java.nio.ByteBuffer; -import java.util.function.Function; - import com.datastax.driver.core.TupleType; import com.datastax.driver.core.TupleValue; - +import java.nio.ByteBuffer; +import java.util.function.Function; import net.helenus.core.SessionRepository; import net.helenus.mapping.HelenusProperty; import net.helenus.mapping.value.TupleColumnValuePreparer; -public class TupleValueWriter extends AbstractEntityValueWriter implements Function { +public class TupleValueWriter extends AbstractEntityValueWriter + implements Function { - private final TupleType tupleType; - private final TupleColumnValuePreparer valuePreparer; + private final TupleType tupleType; + private final TupleColumnValuePreparer valuePreparer; - public TupleValueWriter(Class iface, TupleType tupleType, SessionRepository repository) { - super(iface); + public TupleValueWriter(Class iface, TupleType tupleType, SessionRepository repository) { + super(iface); - this.tupleType = tupleType; - this.valuePreparer = new TupleColumnValuePreparer(tupleType, repository); - } + this.tupleType = tupleType; + this.valuePreparer = new TupleColumnValuePreparer(tupleType, repository); + } - @Override - void writeColumn(TupleValue udtValue, Object value, HelenusProperty prop) { + @Override + void writeColumn(TupleValue udtValue, Object value, HelenusProperty prop) { - ByteBuffer bytes = (ByteBuffer) valuePreparer.prepareColumnValue(value, prop); + ByteBuffer bytes = (ByteBuffer) valuePreparer.prepareColumnValue(value, prop); - if (bytes != null) { - udtValue.setBytesUnsafe(prop.getOrdinal(), bytes); - } - } + if (bytes != null) { + udtValue.setBytesUnsafe(prop.getOrdinal(), bytes); + } + } - @Override - public TupleValue apply(Object source) { - if (source != null) { - TupleValue outValue = tupleType.newValue(); - write(outValue, source); - return outValue; - } - return null; - } + @Override + public TupleValue apply(Object source) { + if (source != null) { + TupleValue outValue = tupleType.newValue(); + write(outValue, source); + return outValue; + } + return null; + } } diff --git a/src/main/java/net/helenus/mapping/convert/TypedConverter.java b/src/main/java/net/helenus/mapping/convert/TypedConverter.java index d9242ce..52642d2 100644 --- a/src/main/java/net/helenus/mapping/convert/TypedConverter.java +++ b/src/main/java/net/helenus/mapping/convert/TypedConverter.java @@ -16,50 +16,50 @@ package net.helenus.mapping.convert; import java.util.function.Function; - import net.helenus.support.HelenusMappingException; public class TypedConverter implements Function { - private final Class inputType; - private final Class outputType; - private final Function delegate; + private final Class inputType; + private final Class outputType; + private final Function delegate; - public TypedConverter(Class inputType, Class outputType, Function delegate) { - this.inputType = inputType; - this.outputType = outputType; - this.delegate = delegate; - } + public TypedConverter(Class inputType, Class outputType, Function delegate) { + this.inputType = inputType; + this.outputType = outputType; + this.delegate = delegate; + } - public static TypedConverter create(Class inputType, Class outputType, Function delegate) { - return new TypedConverter(inputType, outputType, delegate); - } + public static TypedConverter create( + Class inputType, Class outputType, Function delegate) { + return new TypedConverter(inputType, outputType, delegate); + } - @Override - public Object apply(Object inputUnknown) { + @Override + public Object apply(Object inputUnknown) { - if (inputUnknown == null) { - return null; - } + if (inputUnknown == null) { + return null; + } - if (!inputType.isAssignableFrom(inputUnknown.getClass())) { - throw new HelenusMappingException( - "expected " + inputType + " type for input parameter " + inputUnknown.getClass()); - } + if (!inputType.isAssignableFrom(inputUnknown.getClass())) { + throw new HelenusMappingException( + "expected " + inputType + " type for input parameter " + inputUnknown.getClass()); + } - I input = (I) inputUnknown; + I input = (I) inputUnknown; - O outputUnknown = delegate.apply(input); + O outputUnknown = delegate.apply(input); - if (outputUnknown == null) { - return null; - } + if (outputUnknown == null) { + return null; + } - if (!outputType.isAssignableFrom(outputUnknown.getClass())) { - throw new HelenusMappingException( - "expected " + outputType + " type for output result " + outputUnknown.getClass()); - } + if (!outputType.isAssignableFrom(outputUnknown.getClass())) { + throw new HelenusMappingException( + "expected " + outputType + " type for output result " + outputUnknown.getClass()); + } - return outputUnknown; - } + return outputUnknown; + } } diff --git a/src/main/java/net/helenus/mapping/convert/UDTValueWriter.java b/src/main/java/net/helenus/mapping/convert/UDTValueWriter.java index 75d2c9e..8156470 100644 --- a/src/main/java/net/helenus/mapping/convert/UDTValueWriter.java +++ b/src/main/java/net/helenus/mapping/convert/UDTValueWriter.java @@ -15,45 +15,44 @@ */ package net.helenus.mapping.convert; -import java.nio.ByteBuffer; -import java.util.function.Function; - import com.datastax.driver.core.UDTValue; import com.datastax.driver.core.UserType; - +import java.nio.ByteBuffer; +import java.util.function.Function; import net.helenus.core.SessionRepository; import net.helenus.mapping.HelenusProperty; import net.helenus.mapping.value.UDTColumnValuePreparer; -public class UDTValueWriter extends AbstractEntityValueWriter implements Function { +public class UDTValueWriter extends AbstractEntityValueWriter + implements Function { - final UserType userType; - final UDTColumnValuePreparer valuePreparer; + final UserType userType; + final UDTColumnValuePreparer valuePreparer; - public UDTValueWriter(Class iface, UserType userType, SessionRepository repository) { - super(iface); + public UDTValueWriter(Class iface, UserType userType, SessionRepository repository) { + super(iface); - this.userType = userType; - this.valuePreparer = new UDTColumnValuePreparer(userType, repository); - } + this.userType = userType; + this.valuePreparer = new UDTColumnValuePreparer(userType, repository); + } - @Override - void writeColumn(UDTValue udtValue, Object value, HelenusProperty prop) { + @Override + void writeColumn(UDTValue udtValue, Object value, HelenusProperty prop) { - ByteBuffer bytes = (ByteBuffer) valuePreparer.prepareColumnValue(value, prop); + ByteBuffer bytes = (ByteBuffer) valuePreparer.prepareColumnValue(value, prop); - if (bytes != null) { - udtValue.setBytesUnsafe(prop.getColumnName().getName(), bytes); - } - } + if (bytes != null) { + udtValue.setBytesUnsafe(prop.getColumnName().getName(), bytes); + } + } - @Override - public UDTValue apply(Object source) { - if (source != null) { - UDTValue outValue = userType.newValue(); - write(outValue, source); - return outValue; - } - return null; - } + @Override + public UDTValue apply(Object source) { + if (source != null) { + UDTValue outValue = userType.newValue(); + write(outValue, source); + return outValue; + } + return null; + } } diff --git a/src/main/java/net/helenus/mapping/convert/tuple/EntityToTupleValueConverter.java b/src/main/java/net/helenus/mapping/convert/tuple/EntityToTupleValueConverter.java index 0717c04..ded9f55 100644 --- a/src/main/java/net/helenus/mapping/convert/tuple/EntityToTupleValueConverter.java +++ b/src/main/java/net/helenus/mapping/convert/tuple/EntityToTupleValueConverter.java @@ -15,17 +15,17 @@ */ package net.helenus.mapping.convert.tuple; -import java.util.function.Function; - import com.datastax.driver.core.TupleType; import com.datastax.driver.core.TupleValue; - +import java.util.function.Function; import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.TupleValueWriter; -public final class EntityToTupleValueConverter extends TupleValueWriter implements Function { +public final class EntityToTupleValueConverter extends TupleValueWriter + implements Function { - public EntityToTupleValueConverter(Class iface, TupleType tupleType, SessionRepository repository) { - super(iface, tupleType, repository); - } + public EntityToTupleValueConverter( + Class iface, TupleType tupleType, SessionRepository repository) { + super(iface, tupleType, repository); + } } diff --git a/src/main/java/net/helenus/mapping/convert/tuple/MapToTupleKeyMapConverter.java b/src/main/java/net/helenus/mapping/convert/tuple/MapToTupleKeyMapConverter.java index 9b4ee51..f8f1c81 100644 --- a/src/main/java/net/helenus/mapping/convert/tuple/MapToTupleKeyMapConverter.java +++ b/src/main/java/net/helenus/mapping/convert/tuple/MapToTupleKeyMapConverter.java @@ -15,25 +15,24 @@ */ package net.helenus.mapping.convert.tuple; +import com.datastax.driver.core.TupleType; import java.util.Map; import java.util.function.Function; - -import com.datastax.driver.core.TupleType; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.TupleValueWriter; import net.helenus.support.Transformers; public final class MapToTupleKeyMapConverter implements Function { - final TupleValueWriter writer; + final TupleValueWriter writer; - public MapToTupleKeyMapConverter(Class iface, TupleType tupleType, SessionRepository repository) { - this.writer = new TupleValueWriter(iface, tupleType, repository); - } + public MapToTupleKeyMapConverter( + Class iface, TupleType tupleType, SessionRepository repository) { + this.writer = new TupleValueWriter(iface, tupleType, repository); + } - @Override - public Object apply(Object t) { - return Transformers.transformMapKey((Map) t, writer); - } + @Override + public Object apply(Object t) { + return Transformers.transformMapKey((Map) t, writer); + } } diff --git a/src/main/java/net/helenus/mapping/convert/tuple/MapToTupleMapConverter.java b/src/main/java/net/helenus/mapping/convert/tuple/MapToTupleMapConverter.java index 821855f..d99a7a1 100644 --- a/src/main/java/net/helenus/mapping/convert/tuple/MapToTupleMapConverter.java +++ b/src/main/java/net/helenus/mapping/convert/tuple/MapToTupleMapConverter.java @@ -15,28 +15,30 @@ */ package net.helenus.mapping.convert.tuple; +import com.datastax.driver.core.TupleType; import java.util.Map; import java.util.function.Function; - -import com.datastax.driver.core.TupleType; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.TupleValueWriter; import net.helenus.support.Transformers; public final class MapToTupleMapConverter implements Function { - final TupleValueWriter keyWriter; - final TupleValueWriter valueWriter; + final TupleValueWriter keyWriter; + final TupleValueWriter valueWriter; - public MapToTupleMapConverter(Class keyClass, TupleType keyType, Class valueClass, TupleType valueType, - SessionRepository repository) { - this.keyWriter = new TupleValueWriter(keyClass, keyType, repository); - this.valueWriter = new TupleValueWriter(valueClass, valueType, repository); - } + public MapToTupleMapConverter( + Class keyClass, + TupleType keyType, + Class valueClass, + TupleType valueType, + SessionRepository repository) { + this.keyWriter = new TupleValueWriter(keyClass, keyType, repository); + this.valueWriter = new TupleValueWriter(valueClass, valueType, repository); + } - @Override - public Object apply(Object t) { - return Transformers.transformMap((Map) t, keyWriter, valueWriter); - } + @Override + public Object apply(Object t) { + return Transformers.transformMap((Map) t, keyWriter, valueWriter); + } } diff --git a/src/main/java/net/helenus/mapping/convert/tuple/MapToTupleValueMapConverter.java b/src/main/java/net/helenus/mapping/convert/tuple/MapToTupleValueMapConverter.java index 4186487..33901f5 100644 --- a/src/main/java/net/helenus/mapping/convert/tuple/MapToTupleValueMapConverter.java +++ b/src/main/java/net/helenus/mapping/convert/tuple/MapToTupleValueMapConverter.java @@ -15,25 +15,24 @@ */ package net.helenus.mapping.convert.tuple; +import com.datastax.driver.core.TupleType; import java.util.Map; import java.util.function.Function; - -import com.datastax.driver.core.TupleType; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.TupleValueWriter; import net.helenus.support.Transformers; public final class MapToTupleValueMapConverter implements Function { - final TupleValueWriter writer; + final TupleValueWriter writer; - public MapToTupleValueMapConverter(Class iface, TupleType tupleType, SessionRepository repository) { - this.writer = new TupleValueWriter(iface, tupleType, repository); - } + public MapToTupleValueMapConverter( + Class iface, TupleType tupleType, SessionRepository repository) { + this.writer = new TupleValueWriter(iface, tupleType, repository); + } - @Override - public Object apply(Object t) { - return Transformers.transformMapValue((Map) t, writer); - } + @Override + public Object apply(Object t) { + return Transformers.transformMapValue((Map) t, writer); + } } diff --git a/src/main/java/net/helenus/mapping/convert/tuple/SetToTupleSetConverter.java b/src/main/java/net/helenus/mapping/convert/tuple/SetToTupleSetConverter.java index 6b1e0ed..db88523 100644 --- a/src/main/java/net/helenus/mapping/convert/tuple/SetToTupleSetConverter.java +++ b/src/main/java/net/helenus/mapping/convert/tuple/SetToTupleSetConverter.java @@ -15,25 +15,23 @@ */ package net.helenus.mapping.convert.tuple; +import com.datastax.driver.core.TupleType; import java.util.Set; import java.util.function.Function; - -import com.datastax.driver.core.TupleType; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.TupleValueWriter; import net.helenus.support.Transformers; public final class SetToTupleSetConverter implements Function { - final TupleValueWriter writer; + final TupleValueWriter writer; - public SetToTupleSetConverter(Class iface, TupleType tupleType, SessionRepository repository) { - this.writer = new TupleValueWriter(iface, tupleType, repository); - } + public SetToTupleSetConverter(Class iface, TupleType tupleType, SessionRepository repository) { + this.writer = new TupleValueWriter(iface, tupleType, repository); + } - @Override - public Object apply(Object t) { - return Transformers.transformSet((Set) t, writer); - } + @Override + public Object apply(Object t) { + return Transformers.transformSet((Set) t, writer); + } } diff --git a/src/main/java/net/helenus/mapping/convert/tuple/TupleKeyMapToMapConverter.java b/src/main/java/net/helenus/mapping/convert/tuple/TupleKeyMapToMapConverter.java index 5142dc5..2f6605b 100644 --- a/src/main/java/net/helenus/mapping/convert/tuple/TupleKeyMapToMapConverter.java +++ b/src/main/java/net/helenus/mapping/convert/tuple/TupleKeyMapToMapConverter.java @@ -15,11 +15,9 @@ */ package net.helenus.mapping.convert.tuple; +import com.datastax.driver.core.TupleValue; import java.util.Map; import java.util.function.Function; - -import com.datastax.driver.core.TupleValue; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.ProxyValueReader; import net.helenus.mapping.value.TupleColumnValueProvider; @@ -27,14 +25,14 @@ import net.helenus.support.Transformers; public final class TupleKeyMapToMapConverter implements Function { - final ProxyValueReader reader; + final ProxyValueReader reader; - public TupleKeyMapToMapConverter(Class iface, SessionRepository repository) { - this.reader = new ProxyValueReader(iface, new TupleColumnValueProvider(repository)); - } + public TupleKeyMapToMapConverter(Class iface, SessionRepository repository) { + this.reader = new ProxyValueReader(iface, new TupleColumnValueProvider(repository)); + } - @Override - public Object apply(Object t) { - return Transformers.transformMapKey((Map) t, reader); - } + @Override + public Object apply(Object t) { + return Transformers.transformMapKey((Map) t, reader); + } } diff --git a/src/main/java/net/helenus/mapping/convert/tuple/TupleListToListConverter.java b/src/main/java/net/helenus/mapping/convert/tuple/TupleListToListConverter.java index 4fae478..73a42b4 100644 --- a/src/main/java/net/helenus/mapping/convert/tuple/TupleListToListConverter.java +++ b/src/main/java/net/helenus/mapping/convert/tuple/TupleListToListConverter.java @@ -15,11 +15,9 @@ */ package net.helenus.mapping.convert.tuple; +import com.datastax.driver.core.TupleValue; import java.util.List; import java.util.function.Function; - -import com.datastax.driver.core.TupleValue; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.ProxyValueReader; import net.helenus.mapping.value.TupleColumnValueProvider; @@ -27,14 +25,14 @@ import net.helenus.support.Transformers; public final class TupleListToListConverter implements Function { - final ProxyValueReader reader; + final ProxyValueReader reader; - public TupleListToListConverter(Class iface, SessionRepository repository) { - this.reader = new ProxyValueReader(iface, new TupleColumnValueProvider(repository)); - } + public TupleListToListConverter(Class iface, SessionRepository repository) { + this.reader = new ProxyValueReader(iface, new TupleColumnValueProvider(repository)); + } - @Override - public Object apply(Object t) { - return Transformers.transformList((List) t, reader); - } + @Override + public Object apply(Object t) { + return Transformers.transformList((List) t, reader); + } } diff --git a/src/main/java/net/helenus/mapping/convert/tuple/TupleMapToMapConverter.java b/src/main/java/net/helenus/mapping/convert/tuple/TupleMapToMapConverter.java index dc3e2b3..50c6a44 100644 --- a/src/main/java/net/helenus/mapping/convert/tuple/TupleMapToMapConverter.java +++ b/src/main/java/net/helenus/mapping/convert/tuple/TupleMapToMapConverter.java @@ -15,11 +15,9 @@ */ package net.helenus.mapping.convert.tuple; +import com.datastax.driver.core.TupleValue; import java.util.Map; import java.util.function.Function; - -import com.datastax.driver.core.TupleValue; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.ProxyValueReader; import net.helenus.mapping.value.TupleColumnValueProvider; @@ -27,16 +25,19 @@ import net.helenus.support.Transformers; public final class TupleMapToMapConverter implements Function { - final ProxyValueReader keyReader; - final ProxyValueReader valueReader; + final ProxyValueReader keyReader; + final ProxyValueReader valueReader; - public TupleMapToMapConverter(Class keyClass, Class valueClass, SessionRepository repository) { - this.keyReader = new ProxyValueReader(keyClass, new TupleColumnValueProvider(repository)); - this.valueReader = new ProxyValueReader(valueClass, new TupleColumnValueProvider(repository)); - } + public TupleMapToMapConverter( + Class keyClass, Class valueClass, SessionRepository repository) { + this.keyReader = + new ProxyValueReader(keyClass, new TupleColumnValueProvider(repository)); + this.valueReader = + new ProxyValueReader(valueClass, new TupleColumnValueProvider(repository)); + } - @Override - public Object apply(Object t) { - return Transformers.transformMap((Map) t, keyReader, valueReader); - } + @Override + public Object apply(Object t) { + return Transformers.transformMap((Map) t, keyReader, valueReader); + } } diff --git a/src/main/java/net/helenus/mapping/convert/tuple/TupleSetToSetConverter.java b/src/main/java/net/helenus/mapping/convert/tuple/TupleSetToSetConverter.java index 5a9c8b5..125e93a 100644 --- a/src/main/java/net/helenus/mapping/convert/tuple/TupleSetToSetConverter.java +++ b/src/main/java/net/helenus/mapping/convert/tuple/TupleSetToSetConverter.java @@ -15,11 +15,9 @@ */ package net.helenus.mapping.convert.tuple; +import com.datastax.driver.core.TupleValue; import java.util.Set; import java.util.function.Function; - -import com.datastax.driver.core.TupleValue; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.ProxyValueReader; import net.helenus.mapping.value.TupleColumnValueProvider; @@ -27,14 +25,14 @@ import net.helenus.support.Transformers; public final class TupleSetToSetConverter implements Function { - final ProxyValueReader reader; + final ProxyValueReader reader; - public TupleSetToSetConverter(Class iface, SessionRepository repository) { - this.reader = new ProxyValueReader(iface, new TupleColumnValueProvider(repository)); - } + public TupleSetToSetConverter(Class iface, SessionRepository repository) { + this.reader = new ProxyValueReader(iface, new TupleColumnValueProvider(repository)); + } - @Override - public Object apply(Object t) { - return Transformers.transformSet((Set) t, reader); - } + @Override + public Object apply(Object t) { + return Transformers.transformSet((Set) t, reader); + } } diff --git a/src/main/java/net/helenus/mapping/convert/tuple/TupleValueMapToMapConverter.java b/src/main/java/net/helenus/mapping/convert/tuple/TupleValueMapToMapConverter.java index 2816c9c..95ccff3 100644 --- a/src/main/java/net/helenus/mapping/convert/tuple/TupleValueMapToMapConverter.java +++ b/src/main/java/net/helenus/mapping/convert/tuple/TupleValueMapToMapConverter.java @@ -15,11 +15,9 @@ */ package net.helenus.mapping.convert.tuple; +import com.datastax.driver.core.TupleValue; import java.util.Map; import java.util.function.Function; - -import com.datastax.driver.core.TupleValue; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.ProxyValueReader; import net.helenus.mapping.value.TupleColumnValueProvider; @@ -27,14 +25,14 @@ import net.helenus.support.Transformers; public final class TupleValueMapToMapConverter implements Function { - final ProxyValueReader reader; + final ProxyValueReader reader; - public TupleValueMapToMapConverter(Class iface, SessionRepository repository) { - this.reader = new ProxyValueReader(iface, new TupleColumnValueProvider(repository)); - } + public TupleValueMapToMapConverter(Class iface, SessionRepository repository) { + this.reader = new ProxyValueReader(iface, new TupleColumnValueProvider(repository)); + } - @Override - public Object apply(Object t) { - return Transformers.transformMapValue((Map) t, reader); - } + @Override + public Object apply(Object t) { + return Transformers.transformMapValue((Map) t, reader); + } } diff --git a/src/main/java/net/helenus/mapping/convert/tuple/TupleValueToEntityConverter.java b/src/main/java/net/helenus/mapping/convert/tuple/TupleValueToEntityConverter.java index 7e5b5f3..43d1d1f 100644 --- a/src/main/java/net/helenus/mapping/convert/tuple/TupleValueToEntityConverter.java +++ b/src/main/java/net/helenus/mapping/convert/tuple/TupleValueToEntityConverter.java @@ -15,19 +15,16 @@ */ package net.helenus.mapping.convert.tuple; -import java.util.function.Function; - import com.datastax.driver.core.TupleValue; - +import java.util.function.Function; import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.ProxyValueReader; import net.helenus.mapping.value.TupleColumnValueProvider; public final class TupleValueToEntityConverter extends ProxyValueReader - implements - Function { + implements Function { - public TupleValueToEntityConverter(Class iface, SessionRepository repository) { - super(iface, new TupleColumnValueProvider(repository)); - } + public TupleValueToEntityConverter(Class iface, SessionRepository repository) { + super(iface, new TupleColumnValueProvider(repository)); + } } diff --git a/src/main/java/net/helenus/mapping/convert/udt/EntityToUDTValueConverter.java b/src/main/java/net/helenus/mapping/convert/udt/EntityToUDTValueConverter.java index 58b9c9a..56f9932 100644 --- a/src/main/java/net/helenus/mapping/convert/udt/EntityToUDTValueConverter.java +++ b/src/main/java/net/helenus/mapping/convert/udt/EntityToUDTValueConverter.java @@ -16,13 +16,13 @@ package net.helenus.mapping.convert.udt; import com.datastax.driver.core.UserType; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.UDTValueWriter; public final class EntityToUDTValueConverter extends UDTValueWriter { - public EntityToUDTValueConverter(Class iface, UserType userType, SessionRepository repository) { - super(iface, userType, repository); - } + public EntityToUDTValueConverter( + Class iface, UserType userType, SessionRepository repository) { + super(iface, userType, repository); + } } diff --git a/src/main/java/net/helenus/mapping/convert/udt/ListToUDTListConverter.java b/src/main/java/net/helenus/mapping/convert/udt/ListToUDTListConverter.java index 66c3e15..4d3e318 100644 --- a/src/main/java/net/helenus/mapping/convert/udt/ListToUDTListConverter.java +++ b/src/main/java/net/helenus/mapping/convert/udt/ListToUDTListConverter.java @@ -15,25 +15,23 @@ */ package net.helenus.mapping.convert.udt; +import com.datastax.driver.core.UserType; import java.util.List; import java.util.function.Function; - -import com.datastax.driver.core.UserType; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.UDTValueWriter; import net.helenus.support.Transformers; public final class ListToUDTListConverter implements Function { - final UDTValueWriter writer; + final UDTValueWriter writer; - public ListToUDTListConverter(Class iface, UserType userType, SessionRepository repository) { - this.writer = new UDTValueWriter(iface, userType, repository); - } + public ListToUDTListConverter(Class iface, UserType userType, SessionRepository repository) { + this.writer = new UDTValueWriter(iface, userType, repository); + } - @Override - public Object apply(Object t) { - return Transformers.transformList((List) t, writer); - } + @Override + public Object apply(Object t) { + return Transformers.transformList((List) t, writer); + } } diff --git a/src/main/java/net/helenus/mapping/convert/udt/MapToUDTKeyMapConverter.java b/src/main/java/net/helenus/mapping/convert/udt/MapToUDTKeyMapConverter.java index 64c228c..63a51a2 100644 --- a/src/main/java/net/helenus/mapping/convert/udt/MapToUDTKeyMapConverter.java +++ b/src/main/java/net/helenus/mapping/convert/udt/MapToUDTKeyMapConverter.java @@ -15,25 +15,23 @@ */ package net.helenus.mapping.convert.udt; +import com.datastax.driver.core.UserType; import java.util.Map; import java.util.function.Function; - -import com.datastax.driver.core.UserType; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.UDTValueWriter; import net.helenus.support.Transformers; public final class MapToUDTKeyMapConverter implements Function { - final UDTValueWriter writer; + final UDTValueWriter writer; - public MapToUDTKeyMapConverter(Class iface, UserType userType, SessionRepository repository) { - this.writer = new UDTValueWriter(iface, userType, repository); - } + public MapToUDTKeyMapConverter(Class iface, UserType userType, SessionRepository repository) { + this.writer = new UDTValueWriter(iface, userType, repository); + } - @Override - public Object apply(Object t) { - return Transformers.transformMapKey((Map) t, writer); - } + @Override + public Object apply(Object t) { + return Transformers.transformMapKey((Map) t, writer); + } } diff --git a/src/main/java/net/helenus/mapping/convert/udt/MapToUDTMapConverter.java b/src/main/java/net/helenus/mapping/convert/udt/MapToUDTMapConverter.java index e40bc0e..a888647 100644 --- a/src/main/java/net/helenus/mapping/convert/udt/MapToUDTMapConverter.java +++ b/src/main/java/net/helenus/mapping/convert/udt/MapToUDTMapConverter.java @@ -15,28 +15,30 @@ */ package net.helenus.mapping.convert.udt; +import com.datastax.driver.core.UserType; import java.util.Map; import java.util.function.Function; - -import com.datastax.driver.core.UserType; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.UDTValueWriter; import net.helenus.support.Transformers; public final class MapToUDTMapConverter implements Function { - final UDTValueWriter keyWriter; - final UDTValueWriter valueWriter; + final UDTValueWriter keyWriter; + final UDTValueWriter valueWriter; - public MapToUDTMapConverter(Class keyClass, UserType keyType, Class valueClass, UserType valueType, - SessionRepository repository) { - this.keyWriter = new UDTValueWriter(keyClass, keyType, repository); - this.valueWriter = new UDTValueWriter(valueClass, valueType, repository); - } + public MapToUDTMapConverter( + Class keyClass, + UserType keyType, + Class valueClass, + UserType valueType, + SessionRepository repository) { + this.keyWriter = new UDTValueWriter(keyClass, keyType, repository); + this.valueWriter = new UDTValueWriter(valueClass, valueType, repository); + } - @Override - public Object apply(Object t) { - return Transformers.transformMap((Map) t, keyWriter, valueWriter); - } + @Override + public Object apply(Object t) { + return Transformers.transformMap((Map) t, keyWriter, valueWriter); + } } diff --git a/src/main/java/net/helenus/mapping/convert/udt/MapToUDTValueMapConverter.java b/src/main/java/net/helenus/mapping/convert/udt/MapToUDTValueMapConverter.java index 1c4f1ff..9ca2936 100644 --- a/src/main/java/net/helenus/mapping/convert/udt/MapToUDTValueMapConverter.java +++ b/src/main/java/net/helenus/mapping/convert/udt/MapToUDTValueMapConverter.java @@ -15,25 +15,24 @@ */ package net.helenus.mapping.convert.udt; +import com.datastax.driver.core.UserType; import java.util.Map; import java.util.function.Function; - -import com.datastax.driver.core.UserType; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.UDTValueWriter; import net.helenus.support.Transformers; public final class MapToUDTValueMapConverter implements Function { - final UDTValueWriter writer; + final UDTValueWriter writer; - public MapToUDTValueMapConverter(Class iface, UserType userType, SessionRepository repository) { - this.writer = new UDTValueWriter(iface, userType, repository); - } + public MapToUDTValueMapConverter( + Class iface, UserType userType, SessionRepository repository) { + this.writer = new UDTValueWriter(iface, userType, repository); + } - @Override - public Object apply(Object t) { - return Transformers.transformMapValue((Map) t, writer); - } + @Override + public Object apply(Object t) { + return Transformers.transformMapValue((Map) t, writer); + } } diff --git a/src/main/java/net/helenus/mapping/convert/udt/SetToUDTSetConverter.java b/src/main/java/net/helenus/mapping/convert/udt/SetToUDTSetConverter.java index d2f8a23..9de5185 100644 --- a/src/main/java/net/helenus/mapping/convert/udt/SetToUDTSetConverter.java +++ b/src/main/java/net/helenus/mapping/convert/udt/SetToUDTSetConverter.java @@ -15,25 +15,23 @@ */ package net.helenus.mapping.convert.udt; +import com.datastax.driver.core.UserType; import java.util.Set; import java.util.function.Function; - -import com.datastax.driver.core.UserType; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.UDTValueWriter; import net.helenus.support.Transformers; public final class SetToUDTSetConverter implements Function { - final UDTValueWriter writer; + final UDTValueWriter writer; - public SetToUDTSetConverter(Class iface, UserType userType, SessionRepository repository) { - this.writer = new UDTValueWriter(iface, userType, repository); - } + public SetToUDTSetConverter(Class iface, UserType userType, SessionRepository repository) { + this.writer = new UDTValueWriter(iface, userType, repository); + } - @Override - public Object apply(Object t) { - return Transformers.transformSet((Set) t, writer); - } + @Override + public Object apply(Object t) { + return Transformers.transformSet((Set) t, writer); + } } diff --git a/src/main/java/net/helenus/mapping/convert/udt/UDTKeyMapToMapConverter.java b/src/main/java/net/helenus/mapping/convert/udt/UDTKeyMapToMapConverter.java index a3e1775..7744fc0 100644 --- a/src/main/java/net/helenus/mapping/convert/udt/UDTKeyMapToMapConverter.java +++ b/src/main/java/net/helenus/mapping/convert/udt/UDTKeyMapToMapConverter.java @@ -15,11 +15,9 @@ */ package net.helenus.mapping.convert.udt; +import com.datastax.driver.core.UDTValue; import java.util.Map; import java.util.function.Function; - -import com.datastax.driver.core.UDTValue; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.ProxyValueReader; import net.helenus.mapping.value.UDTColumnValueProvider; @@ -27,14 +25,14 @@ import net.helenus.support.Transformers; public final class UDTKeyMapToMapConverter implements Function { - final ProxyValueReader reader; + final ProxyValueReader reader; - public UDTKeyMapToMapConverter(Class iface, SessionRepository repository) { - this.reader = new ProxyValueReader(iface, new UDTColumnValueProvider(repository)); - } + public UDTKeyMapToMapConverter(Class iface, SessionRepository repository) { + this.reader = new ProxyValueReader(iface, new UDTColumnValueProvider(repository)); + } - @Override - public Object apply(Object t) { - return Transformers.transformMapKey((Map) t, reader); - } + @Override + public Object apply(Object t) { + return Transformers.transformMapKey((Map) t, reader); + } } diff --git a/src/main/java/net/helenus/mapping/convert/udt/UDTListToListConverter.java b/src/main/java/net/helenus/mapping/convert/udt/UDTListToListConverter.java index f711ce2..078a7aa 100644 --- a/src/main/java/net/helenus/mapping/convert/udt/UDTListToListConverter.java +++ b/src/main/java/net/helenus/mapping/convert/udt/UDTListToListConverter.java @@ -15,11 +15,9 @@ */ package net.helenus.mapping.convert.udt; +import com.datastax.driver.core.UDTValue; import java.util.List; import java.util.function.Function; - -import com.datastax.driver.core.UDTValue; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.ProxyValueReader; import net.helenus.mapping.value.UDTColumnValueProvider; @@ -27,14 +25,14 @@ import net.helenus.support.Transformers; public final class UDTListToListConverter implements Function { - final ProxyValueReader reader; + final ProxyValueReader reader; - public UDTListToListConverter(Class iface, SessionRepository repository) { - this.reader = new ProxyValueReader(iface, new UDTColumnValueProvider(repository)); - } + public UDTListToListConverter(Class iface, SessionRepository repository) { + this.reader = new ProxyValueReader(iface, new UDTColumnValueProvider(repository)); + } - @Override - public Object apply(Object t) { - return Transformers.transformList((List) t, reader); - } + @Override + public Object apply(Object t) { + return Transformers.transformList((List) t, reader); + } } diff --git a/src/main/java/net/helenus/mapping/convert/udt/UDTMapToMapConverter.java b/src/main/java/net/helenus/mapping/convert/udt/UDTMapToMapConverter.java index e314bb3..4ae3de5 100644 --- a/src/main/java/net/helenus/mapping/convert/udt/UDTMapToMapConverter.java +++ b/src/main/java/net/helenus/mapping/convert/udt/UDTMapToMapConverter.java @@ -15,11 +15,9 @@ */ package net.helenus.mapping.convert.udt; +import com.datastax.driver.core.UDTValue; import java.util.Map; import java.util.function.Function; - -import com.datastax.driver.core.UDTValue; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.ProxyValueReader; import net.helenus.mapping.value.UDTColumnValueProvider; @@ -27,16 +25,19 @@ import net.helenus.support.Transformers; public final class UDTMapToMapConverter implements Function { - final ProxyValueReader keyReader; - final ProxyValueReader valueReader; + final ProxyValueReader keyReader; + final ProxyValueReader valueReader; - public UDTMapToMapConverter(Class keyClass, Class valueClass, SessionRepository repository) { - this.keyReader = new ProxyValueReader(keyClass, new UDTColumnValueProvider(repository)); - this.valueReader = new ProxyValueReader(valueClass, new UDTColumnValueProvider(repository)); - } + public UDTMapToMapConverter( + Class keyClass, Class valueClass, SessionRepository repository) { + this.keyReader = + new ProxyValueReader(keyClass, new UDTColumnValueProvider(repository)); + this.valueReader = + new ProxyValueReader(valueClass, new UDTColumnValueProvider(repository)); + } - @Override - public Object apply(Object t) { - return Transformers.transformMap((Map) t, keyReader, valueReader); - } + @Override + public Object apply(Object t) { + return Transformers.transformMap((Map) t, keyReader, valueReader); + } } diff --git a/src/main/java/net/helenus/mapping/convert/udt/UDTSetToSetConverter.java b/src/main/java/net/helenus/mapping/convert/udt/UDTSetToSetConverter.java index 3f5c267..0adae72 100644 --- a/src/main/java/net/helenus/mapping/convert/udt/UDTSetToSetConverter.java +++ b/src/main/java/net/helenus/mapping/convert/udt/UDTSetToSetConverter.java @@ -15,11 +15,9 @@ */ package net.helenus.mapping.convert.udt; +import com.datastax.driver.core.UDTValue; import java.util.Set; import java.util.function.Function; - -import com.datastax.driver.core.UDTValue; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.ProxyValueReader; import net.helenus.mapping.value.UDTColumnValueProvider; @@ -27,14 +25,14 @@ import net.helenus.support.Transformers; public final class UDTSetToSetConverter implements Function { - final ProxyValueReader reader; + final ProxyValueReader reader; - public UDTSetToSetConverter(Class iface, SessionRepository repository) { - this.reader = new ProxyValueReader(iface, new UDTColumnValueProvider(repository)); - } + public UDTSetToSetConverter(Class iface, SessionRepository repository) { + this.reader = new ProxyValueReader(iface, new UDTColumnValueProvider(repository)); + } - @Override - public Object apply(Object t) { - return Transformers.transformSet((Set) t, reader); - } + @Override + public Object apply(Object t) { + return Transformers.transformSet((Set) t, reader); + } } diff --git a/src/main/java/net/helenus/mapping/convert/udt/UDTValueMapToMapConverter.java b/src/main/java/net/helenus/mapping/convert/udt/UDTValueMapToMapConverter.java index 8772f5c..8bb4f27 100644 --- a/src/main/java/net/helenus/mapping/convert/udt/UDTValueMapToMapConverter.java +++ b/src/main/java/net/helenus/mapping/convert/udt/UDTValueMapToMapConverter.java @@ -15,11 +15,9 @@ */ package net.helenus.mapping.convert.udt; +import com.datastax.driver.core.UDTValue; import java.util.Map; import java.util.function.Function; - -import com.datastax.driver.core.UDTValue; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.ProxyValueReader; import net.helenus.mapping.value.UDTColumnValueProvider; @@ -27,14 +25,14 @@ import net.helenus.support.Transformers; public final class UDTValueMapToMapConverter implements Function { - final ProxyValueReader reader; + final ProxyValueReader reader; - public UDTValueMapToMapConverter(Class iface, SessionRepository repository) { - this.reader = new ProxyValueReader(iface, new UDTColumnValueProvider(repository)); - } + public UDTValueMapToMapConverter(Class iface, SessionRepository repository) { + this.reader = new ProxyValueReader(iface, new UDTColumnValueProvider(repository)); + } - @Override - public Object apply(Object t) { - return Transformers.transformMapValue((Map) t, reader); - } + @Override + public Object apply(Object t) { + return Transformers.transformMapValue((Map) t, reader); + } } diff --git a/src/main/java/net/helenus/mapping/convert/udt/UDTValueToEntityConverter.java b/src/main/java/net/helenus/mapping/convert/udt/UDTValueToEntityConverter.java index a48024a..6e7e082 100644 --- a/src/main/java/net/helenus/mapping/convert/udt/UDTValueToEntityConverter.java +++ b/src/main/java/net/helenus/mapping/convert/udt/UDTValueToEntityConverter.java @@ -15,17 +15,16 @@ */ package net.helenus.mapping.convert.udt; -import java.util.function.Function; - import com.datastax.driver.core.UDTValue; - +import java.util.function.Function; import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.ProxyValueReader; import net.helenus.mapping.value.UDTColumnValueProvider; -public final class UDTValueToEntityConverter extends ProxyValueReader implements Function { +public final class UDTValueToEntityConverter extends ProxyValueReader + implements Function { - public UDTValueToEntityConverter(Class iface, SessionRepository repository) { - super(iface, new UDTColumnValueProvider(repository)); - } + public UDTValueToEntityConverter(Class iface, SessionRepository repository) { + super(iface, new UDTColumnValueProvider(repository)); + } } diff --git a/src/main/java/net/helenus/mapping/javatype/AbstractCollectionJavaType.java b/src/main/java/net/helenus/mapping/javatype/AbstractCollectionJavaType.java index 090d202..a882398 100644 --- a/src/main/java/net/helenus/mapping/javatype/AbstractCollectionJavaType.java +++ b/src/main/java/net/helenus/mapping/javatype/AbstractCollectionJavaType.java @@ -2,7 +2,7 @@ package net.helenus.mapping.javatype; public abstract class AbstractCollectionJavaType extends AbstractJavaType { - public static boolean isCollectionType() { - return true; - } + public static boolean isCollectionType() { + return true; + } } diff --git a/src/main/java/net/helenus/mapping/javatype/AbstractJavaType.java b/src/main/java/net/helenus/mapping/javatype/AbstractJavaType.java index 6a9e441..167014c 100644 --- a/src/main/java/net/helenus/mapping/javatype/AbstractJavaType.java +++ b/src/main/java/net/helenus/mapping/javatype/AbstractJavaType.java @@ -15,15 +15,13 @@ */ package net.helenus.mapping.javatype; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.Metadata; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.Optional; import java.util.function.Function; - -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.Metadata; - import net.helenus.core.SessionRepository; import net.helenus.mapping.ColumnType; import net.helenus.mapping.IdentityName; @@ -35,106 +33,107 @@ import net.helenus.support.HelenusMappingException; public abstract class AbstractJavaType { - public static boolean isCollectionType() { - return false; - } + public static boolean isCollectionType() { + return false; + } - static IdentityName resolveUDT(Types.UDT annotation) { - return IdentityName.of(annotation.value(), annotation.forceQuote()); - } + static IdentityName resolveUDT(Types.UDT annotation) { + return IdentityName.of(annotation.value(), annotation.forceQuote()); + } - static DataType resolveSimpleType(Method getter, DataType.Name typeName) { - DataType dataType = SimpleJavaTypes.getDataTypeByName(typeName); - if (dataType == null) { - throw new HelenusMappingException( - "only primitive types are allowed inside collections for the property " + getter); - } - return dataType; - } + static DataType resolveSimpleType(Method getter, DataType.Name typeName) { + DataType dataType = SimpleJavaTypes.getDataTypeByName(typeName); + if (dataType == null) { + throw new HelenusMappingException( + "only primitive types are allowed inside collections for the property " + getter); + } + return dataType; + } - static void ensureTypeArguments(Method getter, int args, int expected) { - if (args != expected) { - throw new HelenusMappingException( - "expected " + expected + " of typed arguments for the property " + getter); - } - } + static void ensureTypeArguments(Method getter, int args, int expected) { + if (args != expected) { + throw new HelenusMappingException( + "expected " + expected + " of typed arguments for the property " + getter); + } + } - static Either autodetectParameterType(Method getter, Type type, Metadata metadata) { + static Either autodetectParameterType( + Method getter, Type type, Metadata metadata) { - DataType dataType = null; + DataType dataType = null; - if (type instanceof Class) { + if (type instanceof Class) { - Class javaType = (Class) type; - dataType = SimpleJavaTypes.getDataTypeByJavaClass(javaType); + Class javaType = (Class) type; + dataType = SimpleJavaTypes.getDataTypeByJavaClass(javaType); - if (dataType != null) { - return Either.left(dataType); - } + if (dataType != null) { + return Either.left(dataType); + } - if (MappingUtil.isTuple(javaType)) { - dataType = TupleValueJavaType.toTupleType(javaType, metadata); - return Either.left(dataType); - } + if (MappingUtil.isTuple(javaType)) { + dataType = TupleValueJavaType.toTupleType(javaType, metadata); + return Either.left(dataType); + } - IdentityName udtName = MappingUtil.getUserDefinedTypeName(javaType, false); + IdentityName udtName = MappingUtil.getUserDefinedTypeName(javaType, false); - if (udtName != null) { - return Either.right(udtName); - } - } + if (udtName != null) { + return Either.right(udtName); + } + } - throw new HelenusMappingException( - "unknown parameter type " + type + " in the collection for the property " + getter); - } + throw new HelenusMappingException( + "unknown parameter type " + type + " in the collection for the property " + getter); + } - static Type[] getTypeParameters(Type genericJavaType) { + static Type[] getTypeParameters(Type genericJavaType) { - if (genericJavaType instanceof ParameterizedType) { + if (genericJavaType instanceof ParameterizedType) { - ParameterizedType type = (ParameterizedType) genericJavaType; + ParameterizedType type = (ParameterizedType) genericJavaType; - return type.getActualTypeArguments(); - } + return type.getActualTypeArguments(); + } - return new Type[]{}; - } + return new Type[] {}; + } - public abstract Class getJavaClass(); + public abstract Class getJavaClass(); - public boolean isApplicable(Class javaClass) { - return false; - } + public boolean isApplicable(Class javaClass) { + return false; + } - public abstract AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata); + public abstract AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata); - public Optional> getPrimitiveJavaClass() { - return Optional.empty(); - } + public Optional> getPrimitiveJavaClass() { + return Optional.empty(); + } - public Optional> resolveReadConverter(AbstractDataType dataType, - SessionRepository repository) { - return Optional.empty(); - } + public Optional> resolveReadConverter( + AbstractDataType dataType, SessionRepository repository) { + return Optional.empty(); + } - public Optional> resolveWriteConverter(AbstractDataType dataType, - SessionRepository repository) { - return Optional.empty(); - } + public Optional> resolveWriteConverter( + AbstractDataType dataType, SessionRepository repository) { + return Optional.empty(); + } - static class DataTypeInfo { - final DataType dataType; - final Class typeArgument; + static class DataTypeInfo { + final DataType dataType; + final Class typeArgument; - DataTypeInfo(DataType dataType) { - this.dataType = dataType; - this.typeArgument = null; - } + DataTypeInfo(DataType dataType) { + this.dataType = dataType; + this.typeArgument = null; + } - DataTypeInfo(DataType dataType, Class typeArgument) { - this.dataType = dataType; - this.typeArgument = typeArgument; - } - } + DataTypeInfo(DataType dataType, Class typeArgument) { + this.dataType = dataType; + this.typeArgument = typeArgument; + } + } } diff --git a/src/main/java/net/helenus/mapping/javatype/ByteArrayJavaType.java b/src/main/java/net/helenus/mapping/javatype/ByteArrayJavaType.java index c566547..02ab5af 100644 --- a/src/main/java/net/helenus/mapping/javatype/ByteArrayJavaType.java +++ b/src/main/java/net/helenus/mapping/javatype/ByteArrayJavaType.java @@ -15,15 +15,13 @@ */ package net.helenus.mapping.javatype; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.Metadata; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.nio.ByteBuffer; import java.util.Optional; import java.util.function.Function; - -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.Metadata; - import net.helenus.core.SessionRepository; import net.helenus.mapping.ColumnType; import net.helenus.mapping.annotation.Types; @@ -35,41 +33,43 @@ import net.helenus.mapping.type.DTDataType; public final class ByteArrayJavaType extends AbstractJavaType { - @Override - public Class getJavaClass() { - return byte[].class; - } + @Override + public Class getJavaClass() { + return byte[].class; + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { - if (null != getter.getDeclaredAnnotation(Types.Blob.class)) { - return new DTDataType(columnType, DataType.blob()); - } + if (null != getter.getDeclaredAnnotation(Types.Blob.class)) { + return new DTDataType(columnType, DataType.blob()); + } - Types.Custom custom = getter.getDeclaredAnnotation(Types.Custom.class); + Types.Custom custom = getter.getDeclaredAnnotation(Types.Custom.class); - if (null != custom) { - return new DTDataType(columnType, DataType.custom(custom.className())); - } + if (null != custom) { + return new DTDataType(columnType, DataType.custom(custom.className())); + } - return new DTDataType(columnType, DataType.blob()); - } + return new DTDataType(columnType, DataType.blob()); + } - @Override - public Optional> resolveReadConverter(AbstractDataType dataType, - SessionRepository repository) { + @Override + public Optional> resolveReadConverter( + AbstractDataType dataType, SessionRepository repository) { - return Optional - .of(TypedConverter.create(ByteBuffer.class, byte[].class, ByteBufferToByteArrayConverter.INSTANCE)); - } + return Optional.of( + TypedConverter.create( + ByteBuffer.class, byte[].class, ByteBufferToByteArrayConverter.INSTANCE)); + } - @Override - public Optional> resolveWriteConverter(AbstractDataType dataType, - SessionRepository repository) { + @Override + public Optional> resolveWriteConverter( + AbstractDataType dataType, SessionRepository repository) { - return Optional - .of(TypedConverter.create(byte[].class, ByteBuffer.class, ByteArrayToByteBufferConverter.INSTANCE)); - } + return Optional.of( + TypedConverter.create( + byte[].class, ByteBuffer.class, ByteArrayToByteBufferConverter.INSTANCE)); + } } diff --git a/src/main/java/net/helenus/mapping/javatype/ByteBufferJavaType.java b/src/main/java/net/helenus/mapping/javatype/ByteBufferJavaType.java index b60e1db..c363a4f 100644 --- a/src/main/java/net/helenus/mapping/javatype/ByteBufferJavaType.java +++ b/src/main/java/net/helenus/mapping/javatype/ByteBufferJavaType.java @@ -15,13 +15,11 @@ */ package net.helenus.mapping.javatype; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.Metadata; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.nio.ByteBuffer; - -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.Metadata; - import net.helenus.mapping.ColumnType; import net.helenus.mapping.annotation.Types; import net.helenus.mapping.type.AbstractDataType; @@ -29,25 +27,25 @@ import net.helenus.mapping.type.DTDataType; public final class ByteBufferJavaType extends AbstractJavaType { - @Override - public Class getJavaClass() { - return ByteBuffer.class; - } + @Override + public Class getJavaClass() { + return ByteBuffer.class; + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { - if (null != getter.getDeclaredAnnotation(Types.Blob.class)) { - return new DTDataType(columnType, DataType.blob()); - } + if (null != getter.getDeclaredAnnotation(Types.Blob.class)) { + return new DTDataType(columnType, DataType.blob()); + } - Types.Custom custom = getter.getDeclaredAnnotation(Types.Custom.class); + Types.Custom custom = getter.getDeclaredAnnotation(Types.Custom.class); - if (null != custom) { - return new DTDataType(columnType, DataType.custom(custom.className())); - } + if (null != custom) { + return new DTDataType(columnType, DataType.custom(custom.className())); + } - return new DTDataType(columnType, DataType.blob()); - } + return new DTDataType(columnType, DataType.blob()); + } } diff --git a/src/main/java/net/helenus/mapping/javatype/DateJavaType.java b/src/main/java/net/helenus/mapping/javatype/DateJavaType.java index 133853d..2a97f30 100644 --- a/src/main/java/net/helenus/mapping/javatype/DateJavaType.java +++ b/src/main/java/net/helenus/mapping/javatype/DateJavaType.java @@ -15,16 +15,14 @@ */ package net.helenus.mapping.javatype; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.Metadata; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.Date; import java.util.Optional; import java.util.UUID; import java.util.function.Function; - -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.Metadata; - import net.helenus.core.SessionRepository; import net.helenus.mapping.ColumnType; import net.helenus.mapping.annotation.Types; @@ -36,49 +34,51 @@ import net.helenus.mapping.type.DTDataType; public final class DateJavaType extends AbstractJavaType { - @Override - public Class getJavaClass() { - return Date.class; - } + @Override + public Class getJavaClass() { + return Date.class; + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { - if (null != getter.getDeclaredAnnotation(Types.Timestamp.class)) { - return new DTDataType(columnType, DataType.timestamp()); - } + if (null != getter.getDeclaredAnnotation(Types.Timestamp.class)) { + return new DTDataType(columnType, DataType.timestamp()); + } - if (null != getter.getDeclaredAnnotation(Types.Timeuuid.class)) { - return new DTDataType(columnType, DataType.timeuuid()); - } + if (null != getter.getDeclaredAnnotation(Types.Timeuuid.class)) { + return new DTDataType(columnType, DataType.timeuuid()); + } - return new DTDataType(columnType, DataType.timestamp()); - } + return new DTDataType(columnType, DataType.timestamp()); + } - @Override - public Optional> resolveReadConverter(AbstractDataType dataType, - SessionRepository repository) { + @Override + public Optional> resolveReadConverter( + AbstractDataType dataType, SessionRepository repository) { - DataType dt = ((DTDataType) dataType).getDataType(); + DataType dt = ((DTDataType) dataType).getDataType(); - if (dt.getName() == DataType.Name.TIMEUUID) { - return Optional.of(TypedConverter.create(UUID.class, Date.class, TimeuuidToDateConverter.INSTANCE)); - } + if (dt.getName() == DataType.Name.TIMEUUID) { + return Optional.of( + TypedConverter.create(UUID.class, Date.class, TimeuuidToDateConverter.INSTANCE)); + } - return Optional.empty(); - } + return Optional.empty(); + } - @Override - public Optional> resolveWriteConverter(AbstractDataType dataType, - SessionRepository repository) { + @Override + public Optional> resolveWriteConverter( + AbstractDataType dataType, SessionRepository repository) { - DataType dt = ((DTDataType) dataType).getDataType(); + DataType dt = ((DTDataType) dataType).getDataType(); - if (dt.getName() == DataType.Name.TIMEUUID) { - return Optional.of(TypedConverter.create(Date.class, UUID.class, DateToTimeuuidConverter.INSTANCE)); - } + if (dt.getName() == DataType.Name.TIMEUUID) { + return Optional.of( + TypedConverter.create(Date.class, UUID.class, DateToTimeuuidConverter.INSTANCE)); + } - return Optional.empty(); - } + return Optional.empty(); + } } diff --git a/src/main/java/net/helenus/mapping/javatype/EnumJavaType.java b/src/main/java/net/helenus/mapping/javatype/EnumJavaType.java index a4b8dad..b205a6d 100644 --- a/src/main/java/net/helenus/mapping/javatype/EnumJavaType.java +++ b/src/main/java/net/helenus/mapping/javatype/EnumJavaType.java @@ -15,14 +15,12 @@ */ package net.helenus.mapping.javatype; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.Metadata; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.Optional; import java.util.function.Function; - -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.Metadata; - import net.helenus.core.SessionRepository; import net.helenus.mapping.ColumnType; import net.helenus.mapping.convert.EnumToStringConverter; @@ -33,31 +31,33 @@ import net.helenus.mapping.type.DTDataType; public final class EnumJavaType extends AbstractJavaType { - @Override - public Class getJavaClass() { - return Enum.class; - } + @Override + public Class getJavaClass() { + return Enum.class; + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { - return new DTDataType(columnType, DataType.ascii(), (Class) genericJavaType); - } + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { + return new DTDataType(columnType, DataType.ascii(), (Class) genericJavaType); + } - @Override - public Optional> resolveReadConverter(AbstractDataType dataType, - SessionRepository repository) { + @Override + public Optional> resolveReadConverter( + AbstractDataType dataType, SessionRepository repository) { - DTDataType dt = (DTDataType) dataType; + DTDataType dt = (DTDataType) dataType; - return Optional - .of(TypedConverter.create(String.class, Enum.class, new StringToEnumConverter(dt.getJavaClass()))); - } + return Optional.of( + TypedConverter.create( + String.class, Enum.class, new StringToEnumConverter(dt.getJavaClass()))); + } - @Override - public Optional> resolveWriteConverter(AbstractDataType dataType, - SessionRepository repository) { + @Override + public Optional> resolveWriteConverter( + AbstractDataType dataType, SessionRepository repository) { - return Optional.of(TypedConverter.create(Enum.class, String.class, EnumToStringConverter.INSTANCE)); - } + return Optional.of( + TypedConverter.create(Enum.class, String.class, EnumToStringConverter.INSTANCE)); + } } diff --git a/src/main/java/net/helenus/mapping/javatype/ListJavaType.java b/src/main/java/net/helenus/mapping/javatype/ListJavaType.java index e4f4a53..8e1f937 100644 --- a/src/main/java/net/helenus/mapping/javatype/ListJavaType.java +++ b/src/main/java/net/helenus/mapping/javatype/ListJavaType.java @@ -15,14 +15,12 @@ */ package net.helenus.mapping.javatype; +import com.datastax.driver.core.*; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.List; import java.util.Optional; import java.util.function.Function; - -import com.datastax.driver.core.*; - import net.helenus.core.SessionRepository; import net.helenus.mapping.ColumnType; import net.helenus.mapping.IdentityName; @@ -39,110 +37,112 @@ import net.helenus.support.HelenusMappingException; public final class ListJavaType extends AbstractCollectionJavaType { - @Override - public Class getJavaClass() { - return List.class; - } + @Override + public Class getJavaClass() { + return List.class; + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { - Types.List clist = getter.getDeclaredAnnotation(Types.List.class); - if (clist != null) { - return new DTDataType(columnType, DataType.list(resolveSimpleType(getter, clist.value()))); - } + Types.List clist = getter.getDeclaredAnnotation(Types.List.class); + if (clist != null) { + return new DTDataType(columnType, DataType.list(resolveSimpleType(getter, clist.value()))); + } - Types.UDTList udtList = getter.getDeclaredAnnotation(Types.UDTList.class); - if (udtList != null) { - return new UDTListDataType(columnType, resolveUDT(udtList.value()), UDTValue.class); - } + Types.UDTList udtList = getter.getDeclaredAnnotation(Types.UDTList.class); + if (udtList != null) { + return new UDTListDataType(columnType, resolveUDT(udtList.value()), UDTValue.class); + } - Type[] args = getTypeParameters(genericJavaType); - ensureTypeArguments(getter, args.length, 1); + Type[] args = getTypeParameters(genericJavaType); + ensureTypeArguments(getter, args.length, 1); - Either parameterType = autodetectParameterType(getter, args[0], metadata); + Either parameterType = + autodetectParameterType(getter, args[0], metadata); - if (parameterType.isLeft()) { - return DTDataType.list(columnType, parameterType.getLeft(), args[0]); - } else { - return new UDTListDataType(columnType, parameterType.getRight(), (Class) args[0]); - } - } + if (parameterType.isLeft()) { + return DTDataType.list(columnType, parameterType.getLeft(), args[0]); + } else { + return new UDTListDataType(columnType, parameterType.getRight(), (Class) args[0]); + } + } - @Override - public Optional> resolveReadConverter(AbstractDataType abstractDataType, - SessionRepository repository) { + @Override + public Optional> resolveReadConverter( + AbstractDataType abstractDataType, SessionRepository repository) { - if (abstractDataType instanceof DTDataType) { + if (abstractDataType instanceof DTDataType) { - DTDataType dt = (DTDataType) abstractDataType; - DataType elementType = dt.getDataType().getTypeArguments().get(0); - if (elementType instanceof TupleType) { + DTDataType dt = (DTDataType) abstractDataType; + DataType elementType = dt.getDataType().getTypeArguments().get(0); + if (elementType instanceof TupleType) { - Class tupleClass = dt.getTypeArguments()[0]; + Class tupleClass = dt.getTypeArguments()[0]; - if (TupleValue.class.isAssignableFrom(tupleClass)) { - return Optional.empty(); - } + if (TupleValue.class.isAssignableFrom(tupleClass)) { + return Optional.empty(); + } - return Optional.of(new TupleListToListConverter(tupleClass, repository)); - } - } else if (abstractDataType instanceof UDTListDataType) { + return Optional.of(new TupleListToListConverter(tupleClass, repository)); + } + } else if (abstractDataType instanceof UDTListDataType) { - UDTListDataType dt = (UDTListDataType) abstractDataType; + UDTListDataType dt = (UDTListDataType) abstractDataType; - Class javaClass = (Class) dt.getTypeArguments()[0]; + Class javaClass = (Class) dt.getTypeArguments()[0]; - if (UDTValue.class.isAssignableFrom(javaClass)) { - return Optional.empty(); - } + if (UDTValue.class.isAssignableFrom(javaClass)) { + return Optional.empty(); + } - return Optional.of(new UDTListToListConverter(javaClass, repository)); - } + return Optional.of(new UDTListToListConverter(javaClass, repository)); + } - return Optional.empty(); - } + return Optional.empty(); + } - @Override - public Optional> resolveWriteConverter(AbstractDataType abstractDataType, - SessionRepository repository) { + @Override + public Optional> resolveWriteConverter( + AbstractDataType abstractDataType, SessionRepository repository) { - if (abstractDataType instanceof DTDataType) { + if (abstractDataType instanceof DTDataType) { - DTDataType dt = (DTDataType) abstractDataType; - DataType elementType = dt.getDataType().getTypeArguments().get(0); + DTDataType dt = (DTDataType) abstractDataType; + DataType elementType = dt.getDataType().getTypeArguments().get(0); - if (elementType instanceof TupleType) { + if (elementType instanceof TupleType) { - Class tupleClass = dt.getTypeArguments()[0]; + Class tupleClass = dt.getTypeArguments()[0]; - if (TupleValue.class.isAssignableFrom(tupleClass)) { - return Optional.empty(); - } + if (TupleValue.class.isAssignableFrom(tupleClass)) { + return Optional.empty(); + } - return Optional.of(new ListToTupleListConverter(tupleClass, (TupleType) elementType, repository)); - } + return Optional.of( + new ListToTupleListConverter(tupleClass, (TupleType) elementType, repository)); + } - } else if (abstractDataType instanceof UDTListDataType) { + } else if (abstractDataType instanceof UDTListDataType) { - UDTListDataType dt = (UDTListDataType) abstractDataType; + UDTListDataType dt = (UDTListDataType) abstractDataType; - Class javaClass = (Class) dt.getTypeArguments()[0]; + Class javaClass = (Class) dt.getTypeArguments()[0]; - if (UDTValue.class.isAssignableFrom(javaClass)) { - return Optional.empty(); - } + if (UDTValue.class.isAssignableFrom(javaClass)) { + return Optional.empty(); + } - UserType userType = repository.findUserType(dt.getUdtName().getName()); - if (userType == null) { - throw new HelenusMappingException( - "UserType not found for " + dt.getUdtName() + " with type " + javaClass); - } + UserType userType = repository.findUserType(dt.getUdtName().getName()); + if (userType == null) { + throw new HelenusMappingException( + "UserType not found for " + dt.getUdtName() + " with type " + javaClass); + } - return Optional.of(new ListToUDTListConverter(javaClass, userType, repository)); - } + return Optional.of(new ListToUDTListConverter(javaClass, userType, repository)); + } - return Optional.empty(); - } + return Optional.empty(); + } } diff --git a/src/main/java/net/helenus/mapping/javatype/LongJavaType.java b/src/main/java/net/helenus/mapping/javatype/LongJavaType.java index 0ace985..b4d1b0d 100644 --- a/src/main/java/net/helenus/mapping/javatype/LongJavaType.java +++ b/src/main/java/net/helenus/mapping/javatype/LongJavaType.java @@ -15,13 +15,11 @@ */ package net.helenus.mapping.javatype; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.Metadata; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.Optional; - -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.Metadata; - import net.helenus.mapping.ColumnType; import net.helenus.mapping.annotation.Types; import net.helenus.mapping.type.AbstractDataType; @@ -29,28 +27,28 @@ import net.helenus.mapping.type.DTDataType; public final class LongJavaType extends AbstractJavaType { - @Override - public Class getJavaClass() { - return Long.class; - } + @Override + public Class getJavaClass() { + return Long.class; + } - @Override - public Optional> getPrimitiveJavaClass() { - return Optional.of(long.class); - } + @Override + public Optional> getPrimitiveJavaClass() { + return Optional.of(long.class); + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { - if (null != getter.getDeclaredAnnotation(Types.Counter.class)) { - return new DTDataType(columnType, DataType.counter()); - } + if (null != getter.getDeclaredAnnotation(Types.Counter.class)) { + return new DTDataType(columnType, DataType.counter()); + } - if (null != getter.getDeclaredAnnotation(Types.Bigint.class)) { - return new DTDataType(columnType, DataType.bigint()); - } + if (null != getter.getDeclaredAnnotation(Types.Bigint.class)) { + return new DTDataType(columnType, DataType.bigint()); + } - return new DTDataType(columnType, DataType.bigint()); - } + return new DTDataType(columnType, DataType.bigint()); + } } diff --git a/src/main/java/net/helenus/mapping/javatype/MapJavaType.java b/src/main/java/net/helenus/mapping/javatype/MapJavaType.java index e4c37ba..2a5d187 100644 --- a/src/main/java/net/helenus/mapping/javatype/MapJavaType.java +++ b/src/main/java/net/helenus/mapping/javatype/MapJavaType.java @@ -15,14 +15,12 @@ */ package net.helenus.mapping.javatype; +import com.datastax.driver.core.*; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.Map; import java.util.Optional; import java.util.function.Function; - -import com.datastax.driver.core.*; - import net.helenus.core.SessionRepository; import net.helenus.mapping.ColumnType; import net.helenus.mapping.IdentityName; @@ -35,285 +33,322 @@ import net.helenus.support.HelenusMappingException; public final class MapJavaType extends AbstractCollectionJavaType { - @Override - public Class getJavaClass() { - return Map.class; - } + @Override + public Class getJavaClass() { + return Map.class; + } + + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { + + Types.Map cmap = getter.getDeclaredAnnotation(Types.Map.class); + if (cmap != null) { + return new DTDataType( + columnType, + DataType.map( + resolveSimpleType(getter, cmap.key()), resolveSimpleType(getter, cmap.value()))); + } + + Types.UDTKeyMap udtKeyMap = getter.getDeclaredAnnotation(Types.UDTKeyMap.class); + if (udtKeyMap != null) { + return new UDTKeyMapDataType( + columnType, + resolveUDT(udtKeyMap.key()), + UDTValue.class, + resolveSimpleType(getter, udtKeyMap.value())); + } + + Types.UDTValueMap udtValueMap = getter.getDeclaredAnnotation(Types.UDTValueMap.class); + if (udtValueMap != null) { + return new UDTValueMapDataType( + columnType, + resolveSimpleType(getter, udtValueMap.key()), + resolveUDT(udtValueMap.value()), + UDTValue.class); + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { + Types.UDTMap udtMap = getter.getDeclaredAnnotation(Types.UDTMap.class); + if (udtMap != null) { + return new UDTMapDataType( + columnType, + resolveUDT(udtMap.key()), + UDTValue.class, + resolveUDT(udtMap.value()), + UDTValue.class); + } + + Type[] args = getTypeParameters(genericJavaType); + ensureTypeArguments(getter, args.length, 2); + + Either key = autodetectParameterType(getter, args[0], metadata); + Either value = autodetectParameterType(getter, args[1], metadata); - Types.Map cmap = getter.getDeclaredAnnotation(Types.Map.class); - if (cmap != null) { - return new DTDataType(columnType, - DataType.map(resolveSimpleType(getter, cmap.key()), resolveSimpleType(getter, cmap.value()))); - } + if (key.isLeft()) { - Types.UDTKeyMap udtKeyMap = getter.getDeclaredAnnotation(Types.UDTKeyMap.class); - if (udtKeyMap != null) { - return new UDTKeyMapDataType(columnType, resolveUDT(udtKeyMap.key()), UDTValue.class, - resolveSimpleType(getter, udtKeyMap.value())); - } + if (value.isLeft()) { + return DTDataType.map(columnType, key.getLeft(), args[0], value.getLeft(), args[1]); + } else { + return new UDTValueMapDataType( + columnType, key.getLeft(), value.getRight(), (Class) args[1]); + } + } else { - Types.UDTValueMap udtValueMap = getter.getDeclaredAnnotation(Types.UDTValueMap.class); - if (udtValueMap != null) { - return new UDTValueMapDataType(columnType, resolveSimpleType(getter, udtValueMap.key()), - resolveUDT(udtValueMap.value()), UDTValue.class); - } + if (value.isLeft()) { + return new UDTKeyMapDataType( + columnType, key.getRight(), (Class) args[0], value.getLeft()); + } else { + return new UDTMapDataType( + columnType, key.getRight(), (Class) args[0], value.getRight(), (Class) args[1]); + } + } + } - Types.UDTMap udtMap = getter.getDeclaredAnnotation(Types.UDTMap.class); - if (udtMap != null) { - return new UDTMapDataType(columnType, resolveUDT(udtMap.key()), UDTValue.class, resolveUDT(udtMap.value()), - UDTValue.class); - } + @Override + public Optional> resolveReadConverter( + AbstractDataType abstractDataType, SessionRepository repository) { - Type[] args = getTypeParameters(genericJavaType); - ensureTypeArguments(getter, args.length, 2); + if (abstractDataType instanceof DTDataType) { + return resolveDTReadConverter((DTDataType) abstractDataType, repository); + } else if (abstractDataType instanceof UDTKeyMapDataType) { - Either key = autodetectParameterType(getter, args[0], metadata); - Either value = autodetectParameterType(getter, args[1], metadata); + UDTKeyMapDataType dt = (UDTKeyMapDataType) abstractDataType; - if (key.isLeft()) { + Class keyClass = (Class) dt.getUdtKeyClass(); - if (value.isLeft()) { - return DTDataType.map(columnType, key.getLeft(), args[0], value.getLeft(), args[1]); - } else { - return new UDTValueMapDataType(columnType, key.getLeft(), value.getRight(), (Class) args[1]); - } - } else { + if (UDTValue.class.isAssignableFrom(keyClass)) { + return Optional.empty(); + } - if (value.isLeft()) { - return new UDTKeyMapDataType(columnType, key.getRight(), (Class) args[0], value.getLeft()); - } else { - return new UDTMapDataType(columnType, key.getRight(), (Class) args[0], value.getRight(), - (Class) args[1]); - } - } - } + return Optional.of(new UDTKeyMapToMapConverter(keyClass, repository)); - @Override - public Optional> resolveReadConverter(AbstractDataType abstractDataType, - SessionRepository repository) { + } else if (abstractDataType instanceof UDTValueMapDataType) { - if (abstractDataType instanceof DTDataType) { - return resolveDTReadConverter((DTDataType) abstractDataType, repository); - } else if (abstractDataType instanceof UDTKeyMapDataType) { + UDTValueMapDataType dt = (UDTValueMapDataType) abstractDataType; - UDTKeyMapDataType dt = (UDTKeyMapDataType) abstractDataType; + Class valueClass = (Class) dt.getUdtValueClass(); - Class keyClass = (Class) dt.getUdtKeyClass(); + if (UDTValue.class.isAssignableFrom(valueClass)) { + return Optional.empty(); + } - if (UDTValue.class.isAssignableFrom(keyClass)) { - return Optional.empty(); - } + return Optional.of(new UDTValueMapToMapConverter(valueClass, repository)); - return Optional.of(new UDTKeyMapToMapConverter(keyClass, repository)); + } else if (abstractDataType instanceof UDTMapDataType) { - } else if (abstractDataType instanceof UDTValueMapDataType) { + UDTMapDataType dt = (UDTMapDataType) abstractDataType; - UDTValueMapDataType dt = (UDTValueMapDataType) abstractDataType; + Class keyClass = (Class) dt.getUdtKeyClass(); + Class valueClass = (Class) dt.getUdtValueClass(); - Class valueClass = (Class) dt.getUdtValueClass(); + if (UDTValue.class.isAssignableFrom(keyClass)) { - if (UDTValue.class.isAssignableFrom(valueClass)) { - return Optional.empty(); - } + if (UDTValue.class.isAssignableFrom(valueClass)) { + return Optional.empty(); + } else { + return Optional.of(new UDTValueMapToMapConverter(valueClass, repository)); + } + } else if (UDTValue.class.isAssignableFrom(valueClass)) { + return Optional.of(new UDTKeyMapToMapConverter(keyClass, repository)); + } - return Optional.of(new UDTValueMapToMapConverter(valueClass, repository)); + return Optional.of(new UDTMapToMapConverter(keyClass, valueClass, repository)); + } - } else if (abstractDataType instanceof UDTMapDataType) { + return Optional.empty(); + } - UDTMapDataType dt = (UDTMapDataType) abstractDataType; + private Optional> resolveDTReadConverter( + DTDataType dt, SessionRepository repository) { - Class keyClass = (Class) dt.getUdtKeyClass(); - Class valueClass = (Class) dt.getUdtValueClass(); + DataType keyDataType = dt.getDataType().getTypeArguments().get(0); + DataType valueDataType = dt.getDataType().getTypeArguments().get(1); - if (UDTValue.class.isAssignableFrom(keyClass)) { + if (keyDataType instanceof TupleType) { - if (UDTValue.class.isAssignableFrom(valueClass)) { - return Optional.empty(); - } else { - return Optional.of(new UDTValueMapToMapConverter(valueClass, repository)); - } - } else if (UDTValue.class.isAssignableFrom(valueClass)) { - return Optional.of(new UDTKeyMapToMapConverter(keyClass, repository)); - } + if (valueDataType instanceof TupleType) { - return Optional.of(new UDTMapToMapConverter(keyClass, valueClass, repository)); - } + Class keyClass = dt.getTypeArguments()[0]; + Class valueClass = dt.getTypeArguments()[1]; - return Optional.empty(); - } + if (TupleValue.class.isAssignableFrom(keyClass)) { - private Optional> resolveDTReadConverter(DTDataType dt, SessionRepository repository) { + if (TupleValue.class.isAssignableFrom(valueClass)) { + return Optional.empty(); + } else { + return Optional.of(new TupleValueMapToMapConverter(valueClass, repository)); + } - DataType keyDataType = dt.getDataType().getTypeArguments().get(0); - DataType valueDataType = dt.getDataType().getTypeArguments().get(1); + } else if (TupleValue.class.isAssignableFrom(valueClass)) { + return Optional.of(new TupleKeyMapToMapConverter(keyClass, repository)); + } - if (keyDataType instanceof TupleType) { + return Optional.of(new TupleMapToMapConverter(keyClass, valueClass, repository)); - if (valueDataType instanceof TupleType) { + } else { + Class keyClass = dt.getTypeArguments()[0]; - Class keyClass = dt.getTypeArguments()[0]; - Class valueClass = dt.getTypeArguments()[1]; + if (TupleValue.class.isAssignableFrom(keyClass)) { + return Optional.empty(); + } - if (TupleValue.class.isAssignableFrom(keyClass)) { + return Optional.of(new TupleKeyMapToMapConverter(keyClass, repository)); + } + } else if (valueDataType instanceof TupleType) { - if (TupleValue.class.isAssignableFrom(valueClass)) { - return Optional.empty(); - } else { - return Optional.of(new TupleValueMapToMapConverter(valueClass, repository)); - } + Class valueClass = dt.getTypeArguments()[0]; - } else if (TupleValue.class.isAssignableFrom(valueClass)) { - return Optional.of(new TupleKeyMapToMapConverter(keyClass, repository)); - } + if (TupleValue.class.isAssignableFrom(valueClass)) { + return Optional.empty(); + } - return Optional.of(new TupleMapToMapConverter(keyClass, valueClass, repository)); + return Optional.of(new TupleValueMapToMapConverter(valueClass, repository)); + } - } else { - Class keyClass = dt.getTypeArguments()[0]; + return Optional.empty(); + } - if (TupleValue.class.isAssignableFrom(keyClass)) { - return Optional.empty(); - } + @Override + public Optional> resolveWriteConverter( + AbstractDataType abstractDataType, SessionRepository repository) { - return Optional.of(new TupleKeyMapToMapConverter(keyClass, repository)); - } - } else if (valueDataType instanceof TupleType) { + if (abstractDataType instanceof DTDataType) { + return resolveDTWriteConverter((DTDataType) abstractDataType, repository); + } else if (abstractDataType instanceof UDTKeyMapDataType) { - Class valueClass = dt.getTypeArguments()[0]; + UDTKeyMapDataType dt = (UDTKeyMapDataType) abstractDataType; - if (TupleValue.class.isAssignableFrom(valueClass)) { - return Optional.empty(); - } + Class keyClass = (Class) dt.getUdtKeyClass(); - return Optional.of(new TupleValueMapToMapConverter(valueClass, repository)); - } + if (UDTValue.class.isAssignableFrom(keyClass)) { + return Optional.empty(); + } - return Optional.empty(); - } + return Optional.of( + new MapToUDTKeyMapConverter( + keyClass, getUserType(dt.getUdtKeyName(), keyClass, repository), repository)); - @Override - public Optional> resolveWriteConverter(AbstractDataType abstractDataType, - SessionRepository repository) { + } else if (abstractDataType instanceof UDTValueMapDataType) { - if (abstractDataType instanceof DTDataType) { - return resolveDTWriteConverter((DTDataType) abstractDataType, repository); - } else if (abstractDataType instanceof UDTKeyMapDataType) { + UDTValueMapDataType dt = (UDTValueMapDataType) abstractDataType; - UDTKeyMapDataType dt = (UDTKeyMapDataType) abstractDataType; + Class valueClass = (Class) dt.getUdtValueClass(); - Class keyClass = (Class) dt.getUdtKeyClass(); + if (UDTValue.class.isAssignableFrom(valueClass)) { + return Optional.empty(); + } - if (UDTValue.class.isAssignableFrom(keyClass)) { - return Optional.empty(); - } + return Optional.of( + new MapToUDTValueMapConverter( + valueClass, getUserType(dt.getUdtValueName(), valueClass, repository), repository)); - return Optional.of(new MapToUDTKeyMapConverter(keyClass, - getUserType(dt.getUdtKeyName(), keyClass, repository), repository)); + } else if (abstractDataType instanceof UDTMapDataType) { - } else if (abstractDataType instanceof UDTValueMapDataType) { + UDTMapDataType dt = (UDTMapDataType) abstractDataType; - UDTValueMapDataType dt = (UDTValueMapDataType) abstractDataType; + Class keyClass = (Class) dt.getUdtKeyClass(); + Class valueClass = (Class) dt.getUdtValueClass(); - Class valueClass = (Class) dt.getUdtValueClass(); + if (UDTValue.class.isAssignableFrom(keyClass)) { - if (UDTValue.class.isAssignableFrom(valueClass)) { - return Optional.empty(); - } + if (UDTValue.class.isAssignableFrom(valueClass)) { + return Optional.empty(); + } else { - return Optional.of(new MapToUDTValueMapConverter(valueClass, - getUserType(dt.getUdtValueName(), valueClass, repository), repository)); + return Optional.of( + new MapToUDTValueMapConverter( + valueClass, + getUserType(dt.getUdtValueName(), valueClass, repository), + repository)); + } + } else if (UDTValue.class.isAssignableFrom(valueClass)) { - } else if (abstractDataType instanceof UDTMapDataType) { + return Optional.of( + new MapToUDTKeyMapConverter( + keyClass, getUserType(dt.getUdtKeyName(), keyClass, repository), repository)); + } - UDTMapDataType dt = (UDTMapDataType) abstractDataType; + return Optional.of( + new MapToUDTMapConverter( + keyClass, + getUserType(dt.getUdtKeyName(), keyClass, repository), + valueClass, + getUserType(dt.getUdtValueName(), valueClass, repository), + repository)); + } - Class keyClass = (Class) dt.getUdtKeyClass(); - Class valueClass = (Class) dt.getUdtValueClass(); + return Optional.empty(); + } - if (UDTValue.class.isAssignableFrom(keyClass)) { + private Optional> resolveDTWriteConverter( + DTDataType dt, SessionRepository repository) { - if (UDTValue.class.isAssignableFrom(valueClass)) { - return Optional.empty(); - } else { + DataType keyDataType = dt.getDataType().getTypeArguments().get(0); + DataType valueDataType = dt.getDataType().getTypeArguments().get(1); - return Optional.of(new MapToUDTValueMapConverter(valueClass, - getUserType(dt.getUdtValueName(), valueClass, repository), repository)); - } - } else if (UDTValue.class.isAssignableFrom(valueClass)) { + if (keyDataType instanceof TupleType) { - return Optional.of(new MapToUDTKeyMapConverter(keyClass, - getUserType(dt.getUdtKeyName(), keyClass, repository), repository)); - } + if (valueDataType instanceof TupleType) { - return Optional.of(new MapToUDTMapConverter(keyClass, getUserType(dt.getUdtKeyName(), keyClass, repository), - valueClass, getUserType(dt.getUdtValueName(), valueClass, repository), repository)); - } + Class keyClass = dt.getTypeArguments()[0]; + Class valueClass = dt.getTypeArguments()[1]; - return Optional.empty(); - } + if (TupleValue.class.isAssignableFrom(keyClass)) { - private Optional> resolveDTWriteConverter(DTDataType dt, SessionRepository repository) { + if (TupleValue.class.isAssignableFrom(valueClass)) { + return Optional.empty(); + } else { + return Optional.of( + new MapToTupleValueMapConverter(valueClass, (TupleType) valueDataType, repository)); + } - DataType keyDataType = dt.getDataType().getTypeArguments().get(0); - DataType valueDataType = dt.getDataType().getTypeArguments().get(1); + } else if (TupleValue.class.isAssignableFrom(valueClass)) { - if (keyDataType instanceof TupleType) { + return Optional.of( + new MapToTupleKeyMapConverter(keyClass, (TupleType) keyDataType, repository)); + } - if (valueDataType instanceof TupleType) { + return Optional.of( + new MapToTupleMapConverter( + keyClass, + (TupleType) keyDataType, + valueClass, + (TupleType) valueDataType, + repository)); - Class keyClass = dt.getTypeArguments()[0]; - Class valueClass = dt.getTypeArguments()[1]; + } else { - if (TupleValue.class.isAssignableFrom(keyClass)) { + Class keyClass = dt.getTypeArguments()[0]; - if (TupleValue.class.isAssignableFrom(valueClass)) { - return Optional.empty(); - } else { - return Optional - .of(new MapToTupleValueMapConverter(valueClass, (TupleType) valueDataType, repository)); - } + if (TupleValue.class.isAssignableFrom(keyClass)) { + return Optional.empty(); + } - } else if (TupleValue.class.isAssignableFrom(valueClass)) { + return Optional.of( + new MapToTupleKeyMapConverter(keyClass, (TupleType) keyDataType, repository)); + } + } else if (valueDataType instanceof TupleType) { - return Optional.of(new MapToTupleKeyMapConverter(keyClass, (TupleType) keyDataType, repository)); - } + Class valueClass = dt.getTypeArguments()[0]; - return Optional.of(new MapToTupleMapConverter(keyClass, (TupleType) keyDataType, valueClass, - (TupleType) valueDataType, repository)); + if (TupleValue.class.isAssignableFrom(valueClass)) { + return Optional.empty(); + } - } else { + return Optional.of( + new MapToTupleValueMapConverter(valueClass, (TupleType) valueDataType, repository)); + } - Class keyClass = dt.getTypeArguments()[0]; + return Optional.empty(); + } - if (TupleValue.class.isAssignableFrom(keyClass)) { - return Optional.empty(); - } - - return Optional.of(new MapToTupleKeyMapConverter(keyClass, (TupleType) keyDataType, repository)); - } - } else if (valueDataType instanceof TupleType) { - - Class valueClass = dt.getTypeArguments()[0]; - - if (TupleValue.class.isAssignableFrom(valueClass)) { - return Optional.empty(); - } - - return Optional.of(new MapToTupleValueMapConverter(valueClass, (TupleType) valueDataType, repository)); - } - - return Optional.empty(); - } - - private UserType getUserType(IdentityName name, Class javaClass, SessionRepository repository) { - UserType userType = repository.findUserType(name.getName()); - if (userType == null) { - throw new HelenusMappingException("UserType not found for " + name + " with type " + javaClass); - } - return userType; - } + private UserType getUserType( + IdentityName name, Class javaClass, SessionRepository repository) { + UserType userType = repository.findUserType(name.getName()); + if (userType == null) { + throw new HelenusMappingException( + "UserType not found for " + name + " with type " + javaClass); + } + return userType; + } } diff --git a/src/main/java/net/helenus/mapping/javatype/MappingJavaTypes.java b/src/main/java/net/helenus/mapping/javatype/MappingJavaTypes.java index 224eb1f..beffe5a 100644 --- a/src/main/java/net/helenus/mapping/javatype/MappingJavaTypes.java +++ b/src/main/java/net/helenus/mapping/javatype/MappingJavaTypes.java @@ -15,17 +15,15 @@ */ package net.helenus.mapping.javatype; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.Metadata; +import com.google.common.collect.ImmutableMap; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.math.BigDecimal; import java.math.BigInteger; import java.net.InetAddress; import java.util.Optional; - -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.Metadata; -import com.google.common.collect.ImmutableMap; - import net.helenus.mapping.ColumnType; import net.helenus.mapping.type.AbstractDataType; import net.helenus.mapping.type.DTDataType; @@ -33,189 +31,189 @@ import net.helenus.support.HelenusMappingException; public final class MappingJavaTypes { - private static final EnumJavaType ENUM_JAVA_TYPE = new EnumJavaType(); - private static final UDTValueJavaType UDT_VALUE_JAVA_TYPE = new UDTValueJavaType(); - private static final TupleValueJavaType TUPLE_VALUE_JAVA_TYPE = new TupleValueJavaType(); + private static final EnumJavaType ENUM_JAVA_TYPE = new EnumJavaType(); + private static final UDTValueJavaType UDT_VALUE_JAVA_TYPE = new UDTValueJavaType(); + private static final TupleValueJavaType TUPLE_VALUE_JAVA_TYPE = new TupleValueJavaType(); - private static final ImmutableMap, AbstractJavaType> knownTypes; + private static final ImmutableMap, AbstractJavaType> knownTypes; - static { - ImmutableMap.Builder, AbstractJavaType> builder = ImmutableMap.builder(); + static { + ImmutableMap.Builder, AbstractJavaType> builder = ImmutableMap.builder(); - add(builder, new BooleanJavaType()); - add(builder, new BigDecimalJavaType()); - add(builder, new BigIntegerJavaType()); - add(builder, new DoubleJavaType()); - add(builder, new FloatJavaType()); - add(builder, new IntegerJavaType()); - add(builder, new InetAddressJavaType()); + add(builder, new BooleanJavaType()); + add(builder, new BigDecimalJavaType()); + add(builder, new BigIntegerJavaType()); + add(builder, new DoubleJavaType()); + add(builder, new FloatJavaType()); + add(builder, new IntegerJavaType()); + add(builder, new InetAddressJavaType()); - add(builder, new ByteBufferJavaType()); - add(builder, new ByteArrayJavaType()); - add(builder, new DateJavaType()); - add(builder, new UUIDJavaType()); - add(builder, new LongJavaType()); - add(builder, new StringJavaType()); - add(builder, ENUM_JAVA_TYPE); - add(builder, new ListJavaType()); - add(builder, new SetJavaType()); - add(builder, new MapJavaType()); - add(builder, TUPLE_VALUE_JAVA_TYPE); - add(builder, UDT_VALUE_JAVA_TYPE); + add(builder, new ByteBufferJavaType()); + add(builder, new ByteArrayJavaType()); + add(builder, new DateJavaType()); + add(builder, new UUIDJavaType()); + add(builder, new LongJavaType()); + add(builder, new StringJavaType()); + add(builder, ENUM_JAVA_TYPE); + add(builder, new ListJavaType()); + add(builder, new SetJavaType()); + add(builder, new MapJavaType()); + add(builder, TUPLE_VALUE_JAVA_TYPE); + add(builder, UDT_VALUE_JAVA_TYPE); - knownTypes = builder.build(); - } + knownTypes = builder.build(); + } - private MappingJavaTypes() { - } + private MappingJavaTypes() {} - private static void add(ImmutableMap.Builder, AbstractJavaType> builder, AbstractJavaType jt) { + private static void add( + ImmutableMap.Builder, AbstractJavaType> builder, AbstractJavaType jt) { - builder.put(jt.getJavaClass(), jt); + builder.put(jt.getJavaClass(), jt); - Optional> primitiveJavaClass = jt.getPrimitiveJavaClass(); - if (primitiveJavaClass.isPresent()) { - builder.put(primitiveJavaClass.get(), jt); - } - } + Optional> primitiveJavaClass = jt.getPrimitiveJavaClass(); + if (primitiveJavaClass.isPresent()) { + builder.put(primitiveJavaClass.get(), jt); + } + } - public static AbstractJavaType resolveJavaType(Class javaClass) { + public static AbstractJavaType resolveJavaType(Class javaClass) { - AbstractJavaType ajt = knownTypes.get(javaClass); - if (ajt != null) { - return ajt; - } + AbstractJavaType ajt = knownTypes.get(javaClass); + if (ajt != null) { + return ajt; + } - if (Enum.class.isAssignableFrom(javaClass)) { - return ENUM_JAVA_TYPE; - } + if (Enum.class.isAssignableFrom(javaClass)) { + return ENUM_JAVA_TYPE; + } - if (TUPLE_VALUE_JAVA_TYPE.isApplicable(javaClass)) { - return TUPLE_VALUE_JAVA_TYPE; - } + if (TUPLE_VALUE_JAVA_TYPE.isApplicable(javaClass)) { + return TUPLE_VALUE_JAVA_TYPE; + } - if (UDT_VALUE_JAVA_TYPE.isApplicable(javaClass)) { - return UDT_VALUE_JAVA_TYPE; - } + if (UDT_VALUE_JAVA_TYPE.isApplicable(javaClass)) { + return UDT_VALUE_JAVA_TYPE; + } - throw new HelenusMappingException("unknown java type " + javaClass); - } + throw new HelenusMappingException("unknown java type " + javaClass); + } - public static final class BooleanJavaType extends AbstractJavaType { + public static final class BooleanJavaType extends AbstractJavaType { - @Override - public Class getJavaClass() { - return Boolean.class; - } + @Override + public Class getJavaClass() { + return Boolean.class; + } - @Override - public Optional> getPrimitiveJavaClass() { - return Optional.of(boolean.class); - } + @Override + public Optional> getPrimitiveJavaClass() { + return Optional.of(boolean.class); + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { - return new DTDataType(columnType, DataType.cboolean()); - } - } + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { + return new DTDataType(columnType, DataType.cboolean()); + } + } - public static final class BigDecimalJavaType extends AbstractJavaType { + public static final class BigDecimalJavaType extends AbstractJavaType { - @Override - public Class getJavaClass() { - return BigDecimal.class; - } + @Override + public Class getJavaClass() { + return BigDecimal.class; + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { - return new DTDataType(columnType, DataType.decimal()); - } - } + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { + return new DTDataType(columnType, DataType.decimal()); + } + } - public static final class BigIntegerJavaType extends AbstractJavaType { + public static final class BigIntegerJavaType extends AbstractJavaType { - @Override - public Class getJavaClass() { - return BigInteger.class; - } + @Override + public Class getJavaClass() { + return BigInteger.class; + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { - return new DTDataType(columnType, DataType.varint()); - } - } + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { + return new DTDataType(columnType, DataType.varint()); + } + } - public static final class DoubleJavaType extends AbstractJavaType { + public static final class DoubleJavaType extends AbstractJavaType { - @Override - public Class getJavaClass() { - return Double.class; - } + @Override + public Class getJavaClass() { + return Double.class; + } - @Override - public Optional> getPrimitiveJavaClass() { - return Optional.of(double.class); - } + @Override + public Optional> getPrimitiveJavaClass() { + return Optional.of(double.class); + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { - return new DTDataType(columnType, DataType.cdouble()); - } - } + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { + return new DTDataType(columnType, DataType.cdouble()); + } + } - public static final class FloatJavaType extends AbstractJavaType { + public static final class FloatJavaType extends AbstractJavaType { - @Override - public Class getJavaClass() { - return Float.class; - } + @Override + public Class getJavaClass() { + return Float.class; + } - @Override - public Optional> getPrimitiveJavaClass() { - return Optional.of(float.class); - } + @Override + public Optional> getPrimitiveJavaClass() { + return Optional.of(float.class); + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { - return new DTDataType(columnType, DataType.cfloat()); - } - } + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { + return new DTDataType(columnType, DataType.cfloat()); + } + } - public static final class IntegerJavaType extends AbstractJavaType { + public static final class IntegerJavaType extends AbstractJavaType { - @Override - public Class getJavaClass() { - return Integer.class; - } + @Override + public Class getJavaClass() { + return Integer.class; + } - @Override - public Optional> getPrimitiveJavaClass() { - return Optional.of(int.class); - } + @Override + public Optional> getPrimitiveJavaClass() { + return Optional.of(int.class); + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { - return new DTDataType(columnType, DataType.cint()); - } - } + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { + return new DTDataType(columnType, DataType.cint()); + } + } - public static final class InetAddressJavaType extends AbstractJavaType { + public static final class InetAddressJavaType extends AbstractJavaType { - @Override - public Class getJavaClass() { - return InetAddress.class; - } + @Override + public Class getJavaClass() { + return InetAddress.class; + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { - return new DTDataType(columnType, DataType.inet()); - } - } + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { + return new DTDataType(columnType, DataType.inet()); + } + } } diff --git a/src/main/java/net/helenus/mapping/javatype/SetJavaType.java b/src/main/java/net/helenus/mapping/javatype/SetJavaType.java index 7d07ad3..818d862 100644 --- a/src/main/java/net/helenus/mapping/javatype/SetJavaType.java +++ b/src/main/java/net/helenus/mapping/javatype/SetJavaType.java @@ -15,14 +15,12 @@ */ package net.helenus.mapping.javatype; +import com.datastax.driver.core.*; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.Optional; import java.util.Set; import java.util.function.Function; - -import com.datastax.driver.core.*; - import net.helenus.core.SessionRepository; import net.helenus.mapping.ColumnType; import net.helenus.mapping.IdentityName; @@ -39,110 +37,112 @@ import net.helenus.support.HelenusMappingException; public final class SetJavaType extends AbstractCollectionJavaType { - @Override - public Class getJavaClass() { - return Set.class; - } + @Override + public Class getJavaClass() { + return Set.class; + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { - Types.Set cset = getter.getDeclaredAnnotation(Types.Set.class); - if (cset != null) { - return new DTDataType(columnType, DataType.set(resolveSimpleType(getter, cset.value()))); - } + Types.Set cset = getter.getDeclaredAnnotation(Types.Set.class); + if (cset != null) { + return new DTDataType(columnType, DataType.set(resolveSimpleType(getter, cset.value()))); + } - Types.UDTSet udtSet = getter.getDeclaredAnnotation(Types.UDTSet.class); - if (udtSet != null) { - return new UDTSetDataType(columnType, resolveUDT(udtSet.value()), UDTValue.class); - } + Types.UDTSet udtSet = getter.getDeclaredAnnotation(Types.UDTSet.class); + if (udtSet != null) { + return new UDTSetDataType(columnType, resolveUDT(udtSet.value()), UDTValue.class); + } - Type[] args = getTypeParameters(genericJavaType); - ensureTypeArguments(getter, args.length, 1); + Type[] args = getTypeParameters(genericJavaType); + ensureTypeArguments(getter, args.length, 1); - Either parameterType = autodetectParameterType(getter, args[0], metadata); + Either parameterType = + autodetectParameterType(getter, args[0], metadata); - if (parameterType.isLeft()) { - return DTDataType.set(columnType, parameterType.getLeft(), args[0]); - } else { - return new UDTSetDataType(columnType, parameterType.getRight(), (Class) args[0]); - } - } + if (parameterType.isLeft()) { + return DTDataType.set(columnType, parameterType.getLeft(), args[0]); + } else { + return new UDTSetDataType(columnType, parameterType.getRight(), (Class) args[0]); + } + } - @Override - public Optional> resolveReadConverter(AbstractDataType abstractDataType, - SessionRepository repository) { + @Override + public Optional> resolveReadConverter( + AbstractDataType abstractDataType, SessionRepository repository) { - if (abstractDataType instanceof DTDataType) { + if (abstractDataType instanceof DTDataType) { - DTDataType dt = (DTDataType) abstractDataType; - DataType elementType = dt.getDataType().getTypeArguments().get(0); - if (elementType instanceof TupleType) { + DTDataType dt = (DTDataType) abstractDataType; + DataType elementType = dt.getDataType().getTypeArguments().get(0); + if (elementType instanceof TupleType) { - Class tupleClass = dt.getTypeArguments()[0]; + Class tupleClass = dt.getTypeArguments()[0]; - if (TupleValue.class.isAssignableFrom(tupleClass)) { - return Optional.empty(); - } + if (TupleValue.class.isAssignableFrom(tupleClass)) { + return Optional.empty(); + } - return Optional.of(new TupleSetToSetConverter(tupleClass, repository)); - } - } else if (abstractDataType instanceof UDTSetDataType) { + return Optional.of(new TupleSetToSetConverter(tupleClass, repository)); + } + } else if (abstractDataType instanceof UDTSetDataType) { - UDTSetDataType dt = (UDTSetDataType) abstractDataType; + UDTSetDataType dt = (UDTSetDataType) abstractDataType; - Class udtClass = (Class) dt.getTypeArguments()[0]; + Class udtClass = (Class) dt.getTypeArguments()[0]; - if (UDTValue.class.isAssignableFrom(udtClass)) { - return Optional.empty(); - } + if (UDTValue.class.isAssignableFrom(udtClass)) { + return Optional.empty(); + } - return Optional.of(new UDTSetToSetConverter(udtClass, repository)); - } + return Optional.of(new UDTSetToSetConverter(udtClass, repository)); + } - return Optional.empty(); - } + return Optional.empty(); + } - @Override - public Optional> resolveWriteConverter(AbstractDataType abstractDataType, - SessionRepository repository) { + @Override + public Optional> resolveWriteConverter( + AbstractDataType abstractDataType, SessionRepository repository) { - if (abstractDataType instanceof DTDataType) { + if (abstractDataType instanceof DTDataType) { - DTDataType dt = (DTDataType) abstractDataType; - DataType elementType = dt.getDataType().getTypeArguments().get(0); + DTDataType dt = (DTDataType) abstractDataType; + DataType elementType = dt.getDataType().getTypeArguments().get(0); - if (elementType instanceof TupleType) { + if (elementType instanceof TupleType) { - Class tupleClass = dt.getTypeArguments()[0]; + Class tupleClass = dt.getTypeArguments()[0]; - if (TupleValue.class.isAssignableFrom(tupleClass)) { - return Optional.empty(); - } + if (TupleValue.class.isAssignableFrom(tupleClass)) { + return Optional.empty(); + } - return Optional.of(new SetToTupleSetConverter(tupleClass, (TupleType) elementType, repository)); - } + return Optional.of( + new SetToTupleSetConverter(tupleClass, (TupleType) elementType, repository)); + } - } else if (abstractDataType instanceof UDTSetDataType) { + } else if (abstractDataType instanceof UDTSetDataType) { - UDTSetDataType dt = (UDTSetDataType) abstractDataType; + UDTSetDataType dt = (UDTSetDataType) abstractDataType; - Class udtClass = (Class) dt.getTypeArguments()[0]; + Class udtClass = (Class) dt.getTypeArguments()[0]; - if (UDTValue.class.isAssignableFrom(udtClass)) { - return Optional.empty(); - } + if (UDTValue.class.isAssignableFrom(udtClass)) { + return Optional.empty(); + } - UserType userType = repository.findUserType(dt.getUdtName().getName()); - if (userType == null) { - throw new HelenusMappingException( - "UserType not found for " + dt.getUdtName() + " with type " + udtClass); - } + UserType userType = repository.findUserType(dt.getUdtName().getName()); + if (userType == null) { + throw new HelenusMappingException( + "UserType not found for " + dt.getUdtName() + " with type " + udtClass); + } - return Optional.of(new SetToUDTSetConverter(udtClass, userType, repository)); - } + return Optional.of(new SetToUDTSetConverter(udtClass, userType, repository)); + } - return Optional.empty(); - } + return Optional.empty(); + } } diff --git a/src/main/java/net/helenus/mapping/javatype/SimpleJavaTypes.java b/src/main/java/net/helenus/mapping/javatype/SimpleJavaTypes.java index 9d2324b..2484f24 100644 --- a/src/main/java/net/helenus/mapping/javatype/SimpleJavaTypes.java +++ b/src/main/java/net/helenus/mapping/javatype/SimpleJavaTypes.java @@ -15,50 +15,53 @@ */ package net.helenus.mapping.javatype; +import com.datastax.driver.core.CodecRegistry; +import com.datastax.driver.core.DataType; import java.time.LocalTime; import java.util.HashMap; import java.util.Map; -import com.datastax.driver.core.CodecRegistry; -import com.datastax.driver.core.DataType; - public final class SimpleJavaTypes { - private static final Map, DataType> javaClassToDataTypeMap = new HashMap, DataType>(); + private static final Map, DataType> javaClassToDataTypeMap = + new HashMap, DataType>(); - private static final Map nameToDataTypeMap = new HashMap(); + private static final Map nameToDataTypeMap = + new HashMap(); - static { - for (DataType dataType : DataType.allPrimitiveTypes()) { + static { + for (DataType dataType : DataType.allPrimitiveTypes()) { - nameToDataTypeMap.put(dataType.getName(), dataType); + nameToDataTypeMap.put(dataType.getName(), dataType); - if (dataType.equals(DataType.counter()) || dataType.equals(DataType.ascii()) - || dataType.equals(DataType.timeuuid()) || dataType.equals(DataType.time())) { - continue; - } + if (dataType.equals(DataType.counter()) + || dataType.equals(DataType.ascii()) + || dataType.equals(DataType.timeuuid()) + || dataType.equals(DataType.time())) { + continue; + } - Class javaClass = CodecRegistry.DEFAULT_INSTANCE.codecFor(dataType).getJavaType().getRawType(); + Class javaClass = + CodecRegistry.DEFAULT_INSTANCE.codecFor(dataType).getJavaType().getRawType(); - DataType dt = javaClassToDataTypeMap.putIfAbsent(javaClass, dataType); - if (dt != null) { - throw new IllegalStateException( - "java type " + javaClass + " is has two datatypes " + dt + " and " + dataType); - } - } + DataType dt = javaClassToDataTypeMap.putIfAbsent(javaClass, dataType); + if (dt != null) { + throw new IllegalStateException( + "java type " + javaClass + " is has two datatypes " + dt + " and " + dataType); + } + } - javaClassToDataTypeMap.put(String.class, DataType.text()); - javaClassToDataTypeMap.put(LocalTime.class, DataType.time()); - } + javaClassToDataTypeMap.put(String.class, DataType.text()); + javaClassToDataTypeMap.put(LocalTime.class, DataType.time()); + } - private SimpleJavaTypes() { - } + private SimpleJavaTypes() {} - public static DataType getDataTypeByName(DataType.Name name) { - return nameToDataTypeMap.get(name); - } + public static DataType getDataTypeByName(DataType.Name name) { + return nameToDataTypeMap.get(name); + } - public static DataType getDataTypeByJavaClass(Class javaType) { - return javaClassToDataTypeMap.get(javaType); - } + public static DataType getDataTypeByJavaClass(Class javaType) { + return javaClassToDataTypeMap.get(javaType); + } } diff --git a/src/main/java/net/helenus/mapping/javatype/StringJavaType.java b/src/main/java/net/helenus/mapping/javatype/StringJavaType.java index d4e9253..333e857 100644 --- a/src/main/java/net/helenus/mapping/javatype/StringJavaType.java +++ b/src/main/java/net/helenus/mapping/javatype/StringJavaType.java @@ -15,12 +15,10 @@ */ package net.helenus.mapping.javatype; -import java.lang.reflect.Method; -import java.lang.reflect.Type; - import com.datastax.driver.core.DataType; import com.datastax.driver.core.Metadata; - +import java.lang.reflect.Method; +import java.lang.reflect.Type; import net.helenus.mapping.ColumnType; import net.helenus.mapping.annotation.Types; import net.helenus.mapping.type.AbstractDataType; @@ -28,27 +26,27 @@ import net.helenus.mapping.type.DTDataType; public final class StringJavaType extends AbstractJavaType { - @Override - public Class getJavaClass() { - return String.class; - } + @Override + public Class getJavaClass() { + return String.class; + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { - if (null != getter.getDeclaredAnnotation(Types.Ascii.class)) { - return new DTDataType(columnType, DataType.ascii()); - } + if (null != getter.getDeclaredAnnotation(Types.Ascii.class)) { + return new DTDataType(columnType, DataType.ascii()); + } - if (null != getter.getDeclaredAnnotation(Types.Text.class)) { - return new DTDataType(columnType, DataType.text()); - } + if (null != getter.getDeclaredAnnotation(Types.Text.class)) { + return new DTDataType(columnType, DataType.text()); + } - if (null != getter.getDeclaredAnnotation(Types.Varchar.class)) { - return new DTDataType(columnType, DataType.varchar()); - } + if (null != getter.getDeclaredAnnotation(Types.Varchar.class)) { + return new DTDataType(columnType, DataType.varchar()); + } - return new DTDataType(columnType, DataType.text()); - } + return new DTDataType(columnType, DataType.text()); + } } diff --git a/src/main/java/net/helenus/mapping/javatype/TupleValueJavaType.java b/src/main/java/net/helenus/mapping/javatype/TupleValueJavaType.java index 9374ed8..28b602e 100644 --- a/src/main/java/net/helenus/mapping/javatype/TupleValueJavaType.java +++ b/src/main/java/net/helenus/mapping/javatype/TupleValueJavaType.java @@ -15,18 +15,16 @@ */ package net.helenus.mapping.javatype; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.Metadata; +import com.datastax.driver.core.TupleType; +import com.datastax.driver.core.TupleValue; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.List; import java.util.Optional; import java.util.function.Function; import java.util.stream.Collectors; - -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.Metadata; -import com.datastax.driver.core.TupleType; -import com.datastax.driver.core.TupleValue; - import net.helenus.core.Helenus; import net.helenus.core.SessionRepository; import net.helenus.mapping.ColumnType; @@ -43,94 +41,112 @@ import net.helenus.support.HelenusMappingException; public final class TupleValueJavaType extends AbstractJavaType { - public static TupleType toTupleType(Class javaType, Metadata metadata) { - HelenusEntity tupleEntity = Helenus.entity(javaType, metadata); + public static TupleType toTupleType(Class javaType, Metadata metadata) { + HelenusEntity tupleEntity = Helenus.entity(javaType, metadata); - List tupleTypes = tupleEntity.getOrderedProperties().stream().map(p -> p.getDataType()) - .filter(d -> d instanceof DTDataType).map(d -> (DTDataType) d).map(d -> d.getDataType()) - .collect(Collectors.toList()); + List tupleTypes = + tupleEntity + .getOrderedProperties() + .stream() + .map(p -> p.getDataType()) + .filter(d -> d instanceof DTDataType) + .map(d -> (DTDataType) d) + .map(d -> d.getDataType()) + .collect(Collectors.toList()); - if (tupleTypes.size() < tupleEntity.getOrderedProperties().size()) { + if (tupleTypes.size() < tupleEntity.getOrderedProperties().size()) { - List wrongColumns = tupleEntity.getOrderedProperties().stream() - .filter(p -> !(p.getDataType() instanceof DTDataType)).map(p -> p.getColumnName()) - .collect(Collectors.toList()); + List wrongColumns = + tupleEntity + .getOrderedProperties() + .stream() + .filter(p -> !(p.getDataType() instanceof DTDataType)) + .map(p -> p.getColumnName()) + .collect(Collectors.toList()); - throw new HelenusMappingException( - "non simple types in tuple " + tupleEntity.getMappingInterface() + " in columns: " + wrongColumns); - } + throw new HelenusMappingException( + "non simple types in tuple " + + tupleEntity.getMappingInterface() + + " in columns: " + + wrongColumns); + } - return metadata.newTupleType(tupleTypes.toArray(new DataType[tupleTypes.size()])); - } + return metadata.newTupleType(tupleTypes.toArray(new DataType[tupleTypes.size()])); + } - @Override - public Class getJavaClass() { - return TupleValue.class; - } + @Override + public Class getJavaClass() { + return TupleValue.class; + } - @Override - public boolean isApplicable(Class javaClass) { - return MappingUtil.isTuple(javaClass); - } + @Override + public boolean isApplicable(Class javaClass) { + return MappingUtil.isTuple(javaClass); + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { - Class javaType = (Class) genericJavaType; + Class javaType = (Class) genericJavaType; - if (TupleValue.class.isAssignableFrom(javaType)) { + if (TupleValue.class.isAssignableFrom(javaType)) { - Types.Tuple tuple = getter.getDeclaredAnnotation(Types.Tuple.class); - if (tuple == null) { - throw new HelenusMappingException("tuple must be annotated by @Tuple annotation in " + getter); - } + Types.Tuple tuple = getter.getDeclaredAnnotation(Types.Tuple.class); + if (tuple == null) { + throw new HelenusMappingException( + "tuple must be annotated by @Tuple annotation in " + getter); + } - DataType.Name[] tupleArguments = tuple.value(); - int len = tupleArguments.length; - DataType[] arguments = new DataType[len]; + DataType.Name[] tupleArguments = tuple.value(); + int len = tupleArguments.length; + DataType[] arguments = new DataType[len]; - for (int i = 0; i != len; ++i) { - arguments[i] = resolveSimpleType(getter, tupleArguments[i]); - } + for (int i = 0; i != len; ++i) { + arguments[i] = resolveSimpleType(getter, tupleArguments[i]); + } - TupleType tupleType = metadata.newTupleType(arguments); - return new DTDataType(columnType, tupleType, javaType); + TupleType tupleType = metadata.newTupleType(arguments); + return new DTDataType(columnType, tupleType, javaType); - } else { - return new DTDataType(columnType, toTupleType(javaType, metadata), javaType); - } - } + } else { + return new DTDataType(columnType, toTupleType(javaType, metadata), javaType); + } + } - @Override - public Optional> resolveReadConverter(AbstractDataType dataType, - SessionRepository repository) { + @Override + public Optional> resolveReadConverter( + AbstractDataType dataType, SessionRepository repository) { - DTDataType dt = (DTDataType) dataType; + DTDataType dt = (DTDataType) dataType; - Class javaClass = (Class) dt.getJavaClass(); + Class javaClass = (Class) dt.getJavaClass(); - if (TupleValue.class.isAssignableFrom(javaClass)) { - return Optional.empty(); - } + if (TupleValue.class.isAssignableFrom(javaClass)) { + return Optional.empty(); + } - return Optional.of(TypedConverter.create(TupleValue.class, javaClass, - new TupleValueToEntityConverter(javaClass, repository))); - } + return Optional.of( + TypedConverter.create( + TupleValue.class, javaClass, new TupleValueToEntityConverter(javaClass, repository))); + } - @Override - public Optional> resolveWriteConverter(AbstractDataType dataType, - SessionRepository repository) { + @Override + public Optional> resolveWriteConverter( + AbstractDataType dataType, SessionRepository repository) { - DTDataType dt = (DTDataType) dataType; + DTDataType dt = (DTDataType) dataType; - Class javaClass = (Class) dt.getJavaClass(); + Class javaClass = (Class) dt.getJavaClass(); - if (TupleValue.class.isAssignableFrom(javaClass)) { - return Optional.empty(); - } + if (TupleValue.class.isAssignableFrom(javaClass)) { + return Optional.empty(); + } - return Optional.of(TypedConverter.create(javaClass, TupleValue.class, - new EntityToTupleValueConverter(javaClass, (TupleType) dt.getDataType(), repository))); - } + return Optional.of( + TypedConverter.create( + javaClass, + TupleValue.class, + new EntityToTupleValueConverter(javaClass, (TupleType) dt.getDataType(), repository))); + } } diff --git a/src/main/java/net/helenus/mapping/javatype/UDTValueJavaType.java b/src/main/java/net/helenus/mapping/javatype/UDTValueJavaType.java index 7c6246c..240bfbd 100644 --- a/src/main/java/net/helenus/mapping/javatype/UDTValueJavaType.java +++ b/src/main/java/net/helenus/mapping/javatype/UDTValueJavaType.java @@ -15,15 +15,13 @@ */ package net.helenus.mapping.javatype; +import com.datastax.driver.core.Metadata; +import com.datastax.driver.core.UDTValue; +import com.datastax.driver.core.UserType; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.Optional; import java.util.function.Function; - -import com.datastax.driver.core.Metadata; -import com.datastax.driver.core.UDTValue; -import com.datastax.driver.core.UserType; - import net.helenus.core.SessionRepository; import net.helenus.mapping.ColumnType; import net.helenus.mapping.IdentityName; @@ -38,77 +36,82 @@ import net.helenus.support.HelenusMappingException; public final class UDTValueJavaType extends AbstractJavaType { - @Override - public Class getJavaClass() { - return UDTValue.class; - } + @Override + public Class getJavaClass() { + return UDTValue.class; + } - @Override - public boolean isApplicable(Class javaClass) { - return MappingUtil.isUDT(javaClass); - } + @Override + public boolean isApplicable(Class javaClass) { + return MappingUtil.isUDT(javaClass); + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { - Class javaType = (Class) genericJavaType; + Class javaType = (Class) genericJavaType; - IdentityName udtName = null; + IdentityName udtName = null; - if (UDTValue.class.isAssignableFrom(javaType)) { + if (UDTValue.class.isAssignableFrom(javaType)) { - Types.UDT userTypeName = getter.getDeclaredAnnotation(Types.UDT.class); - if (userTypeName == null) { - throw new HelenusMappingException("absent UserTypeName annotation for " + getter); - } + Types.UDT userTypeName = getter.getDeclaredAnnotation(Types.UDT.class); + if (userTypeName == null) { + throw new HelenusMappingException("absent UserTypeName annotation for " + getter); + } - udtName = new IdentityName(userTypeName.value(), userTypeName.forceQuote()); - } else { - udtName = MappingUtil.getUserDefinedTypeName(javaType, false); - } + udtName = new IdentityName(userTypeName.value(), userTypeName.forceQuote()); + } else { + udtName = MappingUtil.getUserDefinedTypeName(javaType, false); + } - if (udtName != null) { - return new UDTDataType(columnType, udtName, javaType); - } + if (udtName != null) { + return new UDTDataType(columnType, udtName, javaType); + } - throw new HelenusMappingException("unknown type " + javaType + " in " + getter); - } + throw new HelenusMappingException("unknown type " + javaType + " in " + getter); + } - @Override - public Optional> resolveReadConverter(AbstractDataType dataType, - SessionRepository repository) { + @Override + public Optional> resolveReadConverter( + AbstractDataType dataType, SessionRepository repository) { - UDTDataType dt = (UDTDataType) dataType; + UDTDataType dt = (UDTDataType) dataType; - Class javaClass = (Class) dt.getTypeArguments()[0]; + Class javaClass = (Class) dt.getTypeArguments()[0]; - if (UDTValue.class.isAssignableFrom(javaClass)) { - return Optional.empty(); - } + if (UDTValue.class.isAssignableFrom(javaClass)) { + return Optional.empty(); + } - return Optional.of( - TypedConverter.create(UDTValue.class, javaClass, new UDTValueToEntityConverter(javaClass, repository))); - } + return Optional.of( + TypedConverter.create( + UDTValue.class, javaClass, new UDTValueToEntityConverter(javaClass, repository))); + } - @Override - public Optional> resolveWriteConverter(AbstractDataType dataType, - SessionRepository repository) { + @Override + public Optional> resolveWriteConverter( + AbstractDataType dataType, SessionRepository repository) { - UDTDataType dt = (UDTDataType) dataType; + UDTDataType dt = (UDTDataType) dataType; - Class javaClass = (Class) dt.getTypeArguments()[0]; + Class javaClass = (Class) dt.getTypeArguments()[0]; - if (UDTValue.class.isAssignableFrom(javaClass)) { - return Optional.empty(); - } + if (UDTValue.class.isAssignableFrom(javaClass)) { + return Optional.empty(); + } - UserType userType = repository.findUserType(dt.getUdtName().getName()); - if (userType == null) { - throw new HelenusMappingException("UserType not found for " + dt.getUdtName() + " with type " + javaClass); - } + UserType userType = repository.findUserType(dt.getUdtName().getName()); + if (userType == null) { + throw new HelenusMappingException( + "UserType not found for " + dt.getUdtName() + " with type " + javaClass); + } - return Optional.of(TypedConverter.create(javaClass, UDTValue.class, - new EntityToUDTValueConverter(javaClass, userType, repository))); - } + return Optional.of( + TypedConverter.create( + javaClass, + UDTValue.class, + new EntityToUDTValueConverter(javaClass, userType, repository))); + } } diff --git a/src/main/java/net/helenus/mapping/javatype/UUIDJavaType.java b/src/main/java/net/helenus/mapping/javatype/UUIDJavaType.java index 52aca62..d3c712b 100644 --- a/src/main/java/net/helenus/mapping/javatype/UUIDJavaType.java +++ b/src/main/java/net/helenus/mapping/javatype/UUIDJavaType.java @@ -15,13 +15,11 @@ */ package net.helenus.mapping.javatype; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.Metadata; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.UUID; - -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.Metadata; - import net.helenus.mapping.ColumnType; import net.helenus.mapping.annotation.Types; import net.helenus.mapping.type.AbstractDataType; @@ -29,23 +27,23 @@ import net.helenus.mapping.type.DTDataType; public final class UUIDJavaType extends AbstractJavaType { - @Override - public Class getJavaClass() { - return UUID.class; - } + @Override + public Class getJavaClass() { + return UUID.class; + } - @Override - public AbstractDataType resolveDataType(Method getter, Type genericJavaType, ColumnType columnType, - Metadata metadata) { + @Override + public AbstractDataType resolveDataType( + Method getter, Type genericJavaType, ColumnType columnType, Metadata metadata) { - if (null != getter.getDeclaredAnnotation(Types.Uuid.class)) { - return new DTDataType(columnType, DataType.uuid()); - } + if (null != getter.getDeclaredAnnotation(Types.Uuid.class)) { + return new DTDataType(columnType, DataType.uuid()); + } - if (null != getter.getDeclaredAnnotation(Types.Timeuuid.class)) { - return new DTDataType(columnType, DataType.timeuuid()); - } + if (null != getter.getDeclaredAnnotation(Types.Timeuuid.class)) { + return new DTDataType(columnType, DataType.timeuuid()); + } - return new DTDataType(columnType, DataType.uuid()); - } + return new DTDataType(columnType, DataType.uuid()); + } } diff --git a/src/main/java/net/helenus/mapping/type/AbstractCollectionDataType.java b/src/main/java/net/helenus/mapping/type/AbstractCollectionDataType.java index ef8b9a1..7e76d92 100644 --- a/src/main/java/net/helenus/mapping/type/AbstractCollectionDataType.java +++ b/src/main/java/net/helenus/mapping/type/AbstractCollectionDataType.java @@ -4,11 +4,11 @@ import net.helenus.mapping.ColumnType; public abstract class AbstractCollectionDataType extends AbstractDataType { - public AbstractCollectionDataType(ColumnType columnType) { - super(columnType); - } + public AbstractCollectionDataType(ColumnType columnType) { + super(columnType); + } - public boolean isCollectionType() { - return true; - } + public boolean isCollectionType() { + return true; + } } diff --git a/src/main/java/net/helenus/mapping/type/AbstractDataType.java b/src/main/java/net/helenus/mapping/type/AbstractDataType.java index 7115b57..5a1c2ac 100644 --- a/src/main/java/net/helenus/mapping/type/AbstractDataType.java +++ b/src/main/java/net/helenus/mapping/type/AbstractDataType.java @@ -19,44 +19,43 @@ import com.datastax.driver.core.schemabuilder.Alter; import com.datastax.driver.core.schemabuilder.Create; import com.datastax.driver.core.schemabuilder.CreateType; import com.datastax.driver.core.schemabuilder.SchemaStatement; - import net.helenus.mapping.ColumnType; import net.helenus.mapping.IdentityName; import net.helenus.support.HelenusMappingException; public abstract class AbstractDataType { - final ColumnType columnType; + final ColumnType columnType; - public AbstractDataType(ColumnType columnType) { - this.columnType = columnType; - } + public AbstractDataType(ColumnType columnType) { + this.columnType = columnType; + } - public abstract void addColumn(Create create, IdentityName columnName); + public abstract void addColumn(Create create, IdentityName columnName); - public abstract void addColumn(CreateType create, IdentityName columnName); + public abstract void addColumn(CreateType create, IdentityName columnName); - public abstract SchemaStatement alterColumn(Alter alter, IdentityName columnName, - OptionalColumnMetadata columnInformation); + public abstract SchemaStatement alterColumn( + Alter alter, IdentityName columnName, OptionalColumnMetadata columnInformation); - public abstract Class[] getTypeArguments(); + public abstract Class[] getTypeArguments(); - public ColumnType getColumnType() { - return columnType; - } + public ColumnType getColumnType() { + return columnType; + } - void ensureSimpleColumn(IdentityName columnName) { - if (columnType != ColumnType.COLUMN) { - throwWrongColumnType(columnName); - } - } + void ensureSimpleColumn(IdentityName columnName) { + if (columnType != ColumnType.COLUMN) { + throwWrongColumnType(columnName); + } + } - void throwWrongColumnType(IdentityName columnName) { - throw new HelenusMappingException( - "wrong column type " + columnType + " for UserDefinedType in columnName " + columnName); - } + void throwWrongColumnType(IdentityName columnName) { + throw new HelenusMappingException( + "wrong column type " + columnType + " for UserDefinedType in columnName " + columnName); + } - public boolean isCollectionType() { - return false; - } + public boolean isCollectionType() { + return false; + } } diff --git a/src/main/java/net/helenus/mapping/type/DTDataType.java b/src/main/java/net/helenus/mapping/type/DTDataType.java index d4d2e05..e61d171 100644 --- a/src/main/java/net/helenus/mapping/type/DTDataType.java +++ b/src/main/java/net/helenus/mapping/type/DTDataType.java @@ -15,8 +15,6 @@ */ package net.helenus.mapping.type; -import java.lang.reflect.Type; - import com.datastax.driver.core.CodecRegistry; import com.datastax.driver.core.DataType; import com.datastax.driver.core.TupleType; @@ -24,166 +22,185 @@ import com.datastax.driver.core.schemabuilder.Alter; import com.datastax.driver.core.schemabuilder.Create; import com.datastax.driver.core.schemabuilder.CreateType; import com.datastax.driver.core.schemabuilder.SchemaStatement; - +import java.lang.reflect.Type; import net.helenus.mapping.ColumnType; import net.helenus.mapping.IdentityName; import net.helenus.support.HelenusMappingException; public final class DTDataType extends AbstractDataType { - private static final Class[] EMPTY_CLASSES = new Class[]{}; + private static final Class[] EMPTY_CLASSES = new Class[] {}; - private final DataType dataType; - private final Class javaClass; - private final Class[] typeArguments; - private final boolean isCollectionType; + private final DataType dataType; + private final Class javaClass; + private final Class[] typeArguments; + private final boolean isCollectionType; - public DTDataType(ColumnType columnType, DataType dataType) { - this(columnType, dataType, CodecRegistry.DEFAULT_INSTANCE.codecFor(dataType).getJavaType().getClass(), - EMPTY_CLASSES); - } + public DTDataType(ColumnType columnType, DataType dataType) { + this( + columnType, + dataType, + CodecRegistry.DEFAULT_INSTANCE.codecFor(dataType).getJavaType().getClass(), + EMPTY_CLASSES); + } - public DTDataType(ColumnType columnType, DataType dataType, Class javaClass) { - this(columnType, dataType, javaClass, EMPTY_CLASSES); - } + public DTDataType(ColumnType columnType, DataType dataType, Class javaClass) { + this(columnType, dataType, javaClass, EMPTY_CLASSES); + } - public DTDataType(ColumnType columnType, DataType dataType, Class javaClass, Class[] typeArguments) { - super(columnType); - this.dataType = dataType; - this.javaClass = javaClass; - this.typeArguments = typeArguments; - this.isCollectionType = dataType.isCollection(); - } + public DTDataType( + ColumnType columnType, DataType dataType, Class javaClass, Class[] typeArguments) { + super(columnType); + this.dataType = dataType; + this.javaClass = javaClass; + this.typeArguments = typeArguments; + this.isCollectionType = dataType.isCollection(); + } - public static DTDataType list(ColumnType columnType, DataType argumentDataType, Type argumentType) { + public static DTDataType list( + ColumnType columnType, DataType argumentDataType, Type argumentType) { - DataType listDataType = DataType.list(argumentDataType); + DataType listDataType = DataType.list(argumentDataType); - if (argumentDataType instanceof TupleType) { - return new DTDataType(columnType, listDataType, - CodecRegistry.DEFAULT_INSTANCE.codecFor(listDataType).getClass(), - new Class[]{(Class) argumentType}); - } else { - return new DTDataType(columnType, listDataType); - } - } + if (argumentDataType instanceof TupleType) { + return new DTDataType( + columnType, + listDataType, + CodecRegistry.DEFAULT_INSTANCE.codecFor(listDataType).getClass(), + new Class[] {(Class) argumentType}); + } else { + return new DTDataType(columnType, listDataType); + } + } - public static DTDataType set(ColumnType columnType, DataType argumentDataType, Type argumentType) { + public static DTDataType set( + ColumnType columnType, DataType argumentDataType, Type argumentType) { - DataType setDataType = DataType.set(argumentDataType); + DataType setDataType = DataType.set(argumentDataType); - if (argumentDataType instanceof TupleType) { - return new DTDataType(columnType, setDataType, - CodecRegistry.DEFAULT_INSTANCE.codecFor(setDataType).getClass(), - new Class[]{(Class) argumentType}); - } else { - return new DTDataType(columnType, setDataType); - } - } + if (argumentDataType instanceof TupleType) { + return new DTDataType( + columnType, + setDataType, + CodecRegistry.DEFAULT_INSTANCE.codecFor(setDataType).getClass(), + new Class[] {(Class) argumentType}); + } else { + return new DTDataType(columnType, setDataType); + } + } - public static DTDataType map(ColumnType columnType, DataType keyDataType, Type keyType, DataType valueDataType, - Type valueType) { + public static DTDataType map( + ColumnType columnType, + DataType keyDataType, + Type keyType, + DataType valueDataType, + Type valueType) { - DataType mapDataType = DataType.map(keyDataType, valueDataType); + DataType mapDataType = DataType.map(keyDataType, valueDataType); - Class[] typeArguments = EMPTY_CLASSES; + Class[] typeArguments = EMPTY_CLASSES; - if (keyDataType instanceof TupleType) { - if (valueDataType instanceof TupleType) { - typeArguments = new Class[]{(Class) keyType, (Class) valueType}; - } else { - typeArguments = new Class[]{(Class) keyType}; - } - } else if (valueDataType instanceof TupleType) { - typeArguments = new Class[]{(Class) valueType}; - } + if (keyDataType instanceof TupleType) { + if (valueDataType instanceof TupleType) { + typeArguments = new Class[] {(Class) keyType, (Class) valueType}; + } else { + typeArguments = new Class[] {(Class) keyType}; + } + } else if (valueDataType instanceof TupleType) { + typeArguments = new Class[] {(Class) valueType}; + } - return new DTDataType(columnType, mapDataType, CodecRegistry.DEFAULT_INSTANCE.codecFor(mapDataType).getClass(), - typeArguments); - } + return new DTDataType( + columnType, + mapDataType, + CodecRegistry.DEFAULT_INSTANCE.codecFor(mapDataType).getClass(), + typeArguments); + } - public DataType getDataType() { - return dataType; - } + public DataType getDataType() { + return dataType; + } - public Class getJavaClass() { - return javaClass; - } + public Class getJavaClass() { + return javaClass; + } - @Override - public Class[] getTypeArguments() { - return typeArguments; - } + @Override + public Class[] getTypeArguments() { + return typeArguments; + } - @Override - public void addColumn(Create create, IdentityName columnName) { + @Override + public void addColumn(Create create, IdentityName columnName) { - switch (columnType) { - case PARTITION_KEY : - create.addPartitionKey(columnName.toCql(), dataType); - break; + switch (columnType) { + case PARTITION_KEY: + create.addPartitionKey(columnName.toCql(), dataType); + break; - case CLUSTERING_COLUMN : - create.addClusteringColumn(columnName.toCql(), dataType); - break; + case CLUSTERING_COLUMN: + create.addClusteringColumn(columnName.toCql(), dataType); + break; - case STATIC_COLUMN : - create.addStaticColumn(columnName.toCql(), dataType); - break; + case STATIC_COLUMN: + create.addStaticColumn(columnName.toCql(), dataType); + break; - case COLUMN : - create.addColumn(columnName.toCql(), dataType); - break; + case COLUMN: + create.addColumn(columnName.toCql(), dataType); + break; - default : - throwWrongColumnType(columnName); - } - } + default: + throwWrongColumnType(columnName); + } + } - @Override - public void addColumn(CreateType create, IdentityName columnName) { + @Override + public void addColumn(CreateType create, IdentityName columnName) { - if (columnType != ColumnType.COLUMN) { - throwWrongColumnType(columnName); - } + if (columnType != ColumnType.COLUMN) { + throwWrongColumnType(columnName); + } - create.addColumn(columnName.toCql(), dataType); - } + create.addColumn(columnName.toCql(), dataType); + } - @Override - public SchemaStatement alterColumn(Alter alter, IdentityName columnName, OptionalColumnMetadata columnMetadata) { + @Override + public SchemaStatement alterColumn( + Alter alter, IdentityName columnName, OptionalColumnMetadata columnMetadata) { - if (columnMetadata != null) { + if (columnMetadata != null) { - if (!dataType.equals(columnMetadata.getType())) { - ensureSimpleColumn(columnName); + if (!dataType.equals(columnMetadata.getType())) { + ensureSimpleColumn(columnName); - return alter.alterColumn(columnName.toCql()).type(dataType); - } + return alter.alterColumn(columnName.toCql()).type(dataType); + } - } else { + } else { - switch (columnType) { - case STATIC_COLUMN : - return alter.addStaticColumn(columnName.toCql()).type(dataType); + switch (columnType) { + case STATIC_COLUMN: + return alter.addStaticColumn(columnName.toCql()).type(dataType); - case COLUMN : - return alter.addColumn(columnName.toCql()).type(dataType); + case COLUMN: + return alter.addColumn(columnName.toCql()).type(dataType); - default : - throw new HelenusMappingException("unable to alter " + columnType + " column " + columnName); - } - } + default: + throw new HelenusMappingException( + "unable to alter " + columnType + " column " + columnName); + } + } - return null; - } + return null; + } - public boolean isCollectionType() { - return isCollectionType; - } + public boolean isCollectionType() { + return isCollectionType; + } - @Override - public String toString() { - return dataType.toString(); - } + @Override + public String toString() { + return dataType.toString(); + } } diff --git a/src/main/java/net/helenus/mapping/type/ListToTupleListConverter.java b/src/main/java/net/helenus/mapping/type/ListToTupleListConverter.java index a7fb9d4..a364c48 100644 --- a/src/main/java/net/helenus/mapping/type/ListToTupleListConverter.java +++ b/src/main/java/net/helenus/mapping/type/ListToTupleListConverter.java @@ -15,25 +15,24 @@ */ package net.helenus.mapping.type; +import com.datastax.driver.core.TupleType; import java.util.List; import java.util.function.Function; - -import com.datastax.driver.core.TupleType; - import net.helenus.core.SessionRepository; import net.helenus.mapping.convert.TupleValueWriter; import net.helenus.support.Transformers; public final class ListToTupleListConverter implements Function { - final TupleValueWriter writer; + final TupleValueWriter writer; - public ListToTupleListConverter(Class iface, TupleType tupleType, SessionRepository repository) { - this.writer = new TupleValueWriter(iface, tupleType, repository); - } + public ListToTupleListConverter( + Class iface, TupleType tupleType, SessionRepository repository) { + this.writer = new TupleValueWriter(iface, tupleType, repository); + } - @Override - public Object apply(Object t) { - return Transformers.transformList((List) t, writer); - } + @Override + public Object apply(Object t) { + return Transformers.transformList((List) t, writer); + } } diff --git a/src/main/java/net/helenus/mapping/type/OptionalColumnMetadata.java b/src/main/java/net/helenus/mapping/type/OptionalColumnMetadata.java index 0589c76..f6d240c 100644 --- a/src/main/java/net/helenus/mapping/type/OptionalColumnMetadata.java +++ b/src/main/java/net/helenus/mapping/type/OptionalColumnMetadata.java @@ -19,7 +19,7 @@ import com.datastax.driver.core.DataType; public interface OptionalColumnMetadata { - String getName(); + String getName(); - DataType getType(); + DataType getType(); } diff --git a/src/main/java/net/helenus/mapping/type/UDTDataType.java b/src/main/java/net/helenus/mapping/type/UDTDataType.java index d24e9a3..281529c 100644 --- a/src/main/java/net/helenus/mapping/type/UDTDataType.java +++ b/src/main/java/net/helenus/mapping/type/UDTDataType.java @@ -18,100 +18,100 @@ package net.helenus.mapping.type; import com.datastax.driver.core.DataType; import com.datastax.driver.core.UserType; import com.datastax.driver.core.schemabuilder.*; - import net.helenus.mapping.ColumnType; import net.helenus.mapping.IdentityName; public final class UDTDataType extends AbstractDataType { - private final IdentityName udtName; - private final Class udtClass; + private final IdentityName udtName; + private final Class udtClass; - public UDTDataType(ColumnType columnType, IdentityName udtName, Class udtClass) { - super(columnType); - this.udtName = udtName; - this.udtClass = udtClass; - } + public UDTDataType(ColumnType columnType, IdentityName udtName, Class udtClass) { + super(columnType); + this.udtName = udtName; + this.udtClass = udtClass; + } - @Override - public Class[] getTypeArguments() { - return new Class[]{udtClass}; - } + @Override + public Class[] getTypeArguments() { + return new Class[] {udtClass}; + } - public IdentityName getUdtName() { - return udtName; - } + public IdentityName getUdtName() { + return udtName; + } - @Override - public void addColumn(Create create, IdentityName columnName) { + @Override + public void addColumn(Create create, IdentityName columnName) { - UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); + UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); - switch (columnType) { - case PARTITION_KEY : - create.addUDTPartitionKey(columnName.toCql(), udtType); - break; + switch (columnType) { + case PARTITION_KEY: + create.addUDTPartitionKey(columnName.toCql(), udtType); + break; - case CLUSTERING_COLUMN : - create.addUDTClusteringColumn(columnName.toCql(), udtType); - break; + case CLUSTERING_COLUMN: + create.addUDTClusteringColumn(columnName.toCql(), udtType); + break; - case STATIC_COLUMN : - create.addUDTStaticColumn(columnName.toCql(), udtType); - break; + case STATIC_COLUMN: + create.addUDTStaticColumn(columnName.toCql(), udtType); + break; - case COLUMN : - create.addUDTColumn(columnName.toCql(), udtType); - break; + case COLUMN: + create.addUDTColumn(columnName.toCql(), udtType); + break; - default : - throwWrongColumnType(columnName); - } - } + default: + throwWrongColumnType(columnName); + } + } - @Override - public void addColumn(CreateType create, IdentityName columnName) { - ensureSimpleColumn(columnName); + @Override + public void addColumn(CreateType create, IdentityName columnName) { + ensureSimpleColumn(columnName); - UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); - create.addUDTColumn(columnName.toCql(), udtType); - } + UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); + create.addUDTColumn(columnName.toCql(), udtType); + } - @Override - public SchemaStatement alterColumn(Alter alter, IdentityName columnName, OptionalColumnMetadata columnMetadata) { + @Override + public SchemaStatement alterColumn( + Alter alter, IdentityName columnName, OptionalColumnMetadata columnMetadata) { - ensureSimpleColumn(columnName); + ensureSimpleColumn(columnName); - if (columnMetadata != null) { + if (columnMetadata != null) { - DataType metadataType = columnMetadata.getType(); - if (metadataType.getName() == DataType.Name.UDT && metadataType instanceof UserType) { + DataType metadataType = columnMetadata.getType(); + if (metadataType.getName() == DataType.Name.UDT && metadataType instanceof UserType) { - UserType metadataUserType = (UserType) metadataType; + UserType metadataUserType = (UserType) metadataType; - if (!udtName.getName().equals(metadataUserType.getTypeName())) { + if (!udtName.getName().equals(metadataUserType.getTypeName())) { - UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); - return alter.alterColumn(columnName.toCql()).udtType(udtType); - } + UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); + return alter.alterColumn(columnName.toCql()).udtType(udtType); + } - } else { + } else { - UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); - return alter.alterColumn(columnName.toCql()).udtType(udtType); - } + UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); + return alter.alterColumn(columnName.toCql()).udtType(udtType); + } - } else { + } else { - UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); - return alter.addColumn(columnName.toCql()).udtType(udtType); - } + UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); + return alter.addColumn(columnName.toCql()).udtType(udtType); + } - return null; - } + return null; + } - @Override - public String toString() { - return "UDT<" + udtName + ">"; - } + @Override + public String toString() { + return "UDT<" + udtName + ">"; + } } diff --git a/src/main/java/net/helenus/mapping/type/UDTKeyMapDataType.java b/src/main/java/net/helenus/mapping/type/UDTKeyMapDataType.java index 25c0939..e04685c 100644 --- a/src/main/java/net/helenus/mapping/type/UDTKeyMapDataType.java +++ b/src/main/java/net/helenus/mapping/type/UDTKeyMapDataType.java @@ -15,97 +15,97 @@ */ package net.helenus.mapping.type; -import java.util.List; - import com.datastax.driver.core.DataType; import com.datastax.driver.core.UserType; import com.datastax.driver.core.schemabuilder.*; - +import java.util.List; import net.helenus.mapping.ColumnType; import net.helenus.mapping.IdentityName; import net.helenus.support.HelenusMappingException; public final class UDTKeyMapDataType extends AbstractCollectionDataType { - private final IdentityName keyType; - private final Class udtKeyClass; - private final DataType valueType; + private final IdentityName keyType; + private final Class udtKeyClass; + private final DataType valueType; - public UDTKeyMapDataType(ColumnType columnType, IdentityName keyType, Class udtKeyClass, DataType valueType) { - super(columnType); - this.keyType = keyType; - this.udtKeyClass = udtKeyClass; - this.valueType = valueType; - } + public UDTKeyMapDataType( + ColumnType columnType, IdentityName keyType, Class udtKeyClass, DataType valueType) { + super(columnType); + this.keyType = keyType; + this.udtKeyClass = udtKeyClass; + this.valueType = valueType; + } - @Override - public Class[] getTypeArguments() { - return new Class[]{udtKeyClass}; - } + @Override + public Class[] getTypeArguments() { + return new Class[] {udtKeyClass}; + } - public IdentityName getUdtKeyName() { - return keyType; - } + public IdentityName getUdtKeyName() { + return keyType; + } - public Class getUdtKeyClass() { - return udtKeyClass; - } + public Class getUdtKeyClass() { + return udtKeyClass; + } - @Override - public void addColumn(Create create, IdentityName columnName) { - ensureSimpleColumn(columnName); + @Override + public void addColumn(Create create, IdentityName columnName) { + ensureSimpleColumn(columnName); - UDTType keyUdtType = SchemaBuilder.frozen(keyType.toCql()); - create.addUDTMapColumn(columnName.toCql(), keyUdtType, valueType); - } + UDTType keyUdtType = SchemaBuilder.frozen(keyType.toCql()); + create.addUDTMapColumn(columnName.toCql(), keyUdtType, valueType); + } - @Override - public void addColumn(CreateType create, IdentityName columnName) { - ensureSimpleColumn(columnName); + @Override + public void addColumn(CreateType create, IdentityName columnName) { + ensureSimpleColumn(columnName); - UDTType keyUdtType = SchemaBuilder.frozen(keyType.toCql()); - create.addUDTMapColumn(columnName.toCql(), keyUdtType, valueType); - } + UDTType keyUdtType = SchemaBuilder.frozen(keyType.toCql()); + create.addUDTMapColumn(columnName.toCql(), keyUdtType, valueType); + } - @Override - public SchemaStatement alterColumn(Alter alter, IdentityName columnName, OptionalColumnMetadata columnMetadata) { + @Override + public SchemaStatement alterColumn( + Alter alter, IdentityName columnName, OptionalColumnMetadata columnMetadata) { - if (columnMetadata == null) { - return notSupportedOperation("add", columnName); - } + if (columnMetadata == null) { + return notSupportedOperation("add", columnName); + } - DataType schemaDataType = columnMetadata.getType(); - if (schemaDataType.getName() != DataType.Name.MAP) { - return notSupportedOperation("alter", columnName); - } + DataType schemaDataType = columnMetadata.getType(); + if (schemaDataType.getName() != DataType.Name.MAP) { + return notSupportedOperation("alter", columnName); + } - List args = columnMetadata.getType().getTypeArguments(); - if (args.size() != 2 || !args.get(1).equals(valueType)) { - return notSupportedOperation("alter", columnName); - } + List args = columnMetadata.getType().getTypeArguments(); + if (args.size() != 2 || !args.get(1).equals(valueType)) { + return notSupportedOperation("alter", columnName); + } - DataType keyDataType = args.get(0); - if (keyDataType.getName() != DataType.Name.UDT || !(keyDataType instanceof UserType)) { - return notSupportedOperation("alter", columnName); - } + DataType keyDataType = args.get(0); + if (keyDataType.getName() != DataType.Name.UDT || !(keyDataType instanceof UserType)) { + return notSupportedOperation("alter", columnName); + } - UserType udtKeyType = (UserType) keyDataType; + UserType udtKeyType = (UserType) keyDataType; - if (!keyType.getName().equals(udtKeyType.getTypeName())) { - return notSupportedOperation("alter", columnName); - } + if (!keyType.getName().equals(udtKeyType.getTypeName())) { + return notSupportedOperation("alter", columnName); + } - // equals - return null; - } + // equals + return null; + } - private SchemaStatement notSupportedOperation(String op, IdentityName columnName) { - throw new HelenusMappingException( - op + " UDTMap column is not supported by Cassandra Driver for column '" + columnName + "'"); - } + private SchemaStatement notSupportedOperation(String op, IdentityName columnName) { + throw new HelenusMappingException( + op + " UDTMap column is not supported by Cassandra Driver for column '" + columnName + "'"); + } - @Override - public String toString() { - return "UDTKeyMap<" + keyType + "," + valueType + ">"; - } + @Override + public String toString() { + return "UDTKeyMap<" + keyType + "," + valueType + ">"; + } } diff --git a/src/main/java/net/helenus/mapping/type/UDTListDataType.java b/src/main/java/net/helenus/mapping/type/UDTListDataType.java index 2a08410..21b2956 100644 --- a/src/main/java/net/helenus/mapping/type/UDTListDataType.java +++ b/src/main/java/net/helenus/mapping/type/UDTListDataType.java @@ -15,91 +15,93 @@ */ package net.helenus.mapping.type; -import java.util.List; - import com.datastax.driver.core.DataType; import com.datastax.driver.core.UserType; import com.datastax.driver.core.schemabuilder.*; - +import java.util.List; import net.helenus.mapping.ColumnType; import net.helenus.mapping.IdentityName; import net.helenus.support.HelenusMappingException; public final class UDTListDataType extends AbstractCollectionDataType { - private final IdentityName udtName; - private final Class udtClass; + private final IdentityName udtName; + private final Class udtClass; - public UDTListDataType(ColumnType columnType, IdentityName udtName, Class udtClass) { - super(columnType); - this.udtName = udtName; - this.udtClass = udtClass; - } + public UDTListDataType(ColumnType columnType, IdentityName udtName, Class udtClass) { + super(columnType); + this.udtName = udtName; + this.udtClass = udtClass; + } - @Override - public Class[] getTypeArguments() { - return new Class[]{udtClass}; - } + @Override + public Class[] getTypeArguments() { + return new Class[] {udtClass}; + } - public IdentityName getUdtName() { - return udtName; - } + public IdentityName getUdtName() { + return udtName; + } - @Override - public void addColumn(Create create, IdentityName columnName) { - ensureSimpleColumn(columnName); + @Override + public void addColumn(Create create, IdentityName columnName) { + ensureSimpleColumn(columnName); - UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); - create.addUDTListColumn(columnName.toCql(), udtType); - } + UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); + create.addUDTListColumn(columnName.toCql(), udtType); + } - @Override - public void addColumn(CreateType create, IdentityName columnName) { - ensureSimpleColumn(columnName); + @Override + public void addColumn(CreateType create, IdentityName columnName) { + ensureSimpleColumn(columnName); - UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); - create.addUDTListColumn(columnName.toCql(), udtType); - } + UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); + create.addUDTListColumn(columnName.toCql(), udtType); + } - @Override - public SchemaStatement alterColumn(Alter alter, IdentityName columnName, OptionalColumnMetadata columnMetadata) { + @Override + public SchemaStatement alterColumn( + Alter alter, IdentityName columnName, OptionalColumnMetadata columnMetadata) { - if (columnMetadata == null) { - return notSupportedOperation("add", columnName); - } + if (columnMetadata == null) { + return notSupportedOperation("add", columnName); + } - DataType schemaDataType = columnMetadata.getType(); - if (schemaDataType.getName() != DataType.Name.LIST) { - return notSupportedOperation("alter", columnName); - } + DataType schemaDataType = columnMetadata.getType(); + if (schemaDataType.getName() != DataType.Name.LIST) { + return notSupportedOperation("alter", columnName); + } - List args = columnMetadata.getType().getTypeArguments(); - if (args.size() != 1) { - return notSupportedOperation("alter", columnName); - } + List args = columnMetadata.getType().getTypeArguments(); + if (args.size() != 1) { + return notSupportedOperation("alter", columnName); + } - DataType valueDataType = args.get(0); - if (valueDataType.getName() != DataType.Name.UDT || !(valueDataType instanceof UserType)) { - return notSupportedOperation("alter", columnName); - } + DataType valueDataType = args.get(0); + if (valueDataType.getName() != DataType.Name.UDT || !(valueDataType instanceof UserType)) { + return notSupportedOperation("alter", columnName); + } - UserType udtValueType = (UserType) valueDataType; + UserType udtValueType = (UserType) valueDataType; - if (!udtName.getName().equals(udtValueType.getTypeName())) { - return notSupportedOperation("alter", columnName); - } + if (!udtName.getName().equals(udtValueType.getTypeName())) { + return notSupportedOperation("alter", columnName); + } - // equals - return null; - } + // equals + return null; + } - private SchemaStatement notSupportedOperation(String op, IdentityName columnName) { - throw new HelenusMappingException( - op + " UDTList column is not supported by Cassandra Driver for column '" + columnName + "'"); - } + private SchemaStatement notSupportedOperation(String op, IdentityName columnName) { + throw new HelenusMappingException( + op + + " UDTList column is not supported by Cassandra Driver for column '" + + columnName + + "'"); + } - @Override - public String toString() { - return "UDTList<" + udtName + ">"; - } + @Override + public String toString() { + return "UDTList<" + udtName + ">"; + } } diff --git a/src/main/java/net/helenus/mapping/type/UDTMapDataType.java b/src/main/java/net/helenus/mapping/type/UDTMapDataType.java index 960cfab..d672fa9 100644 --- a/src/main/java/net/helenus/mapping/type/UDTMapDataType.java +++ b/src/main/java/net/helenus/mapping/type/UDTMapDataType.java @@ -15,121 +15,124 @@ */ package net.helenus.mapping.type; -import java.util.List; - import com.datastax.driver.core.DataType; import com.datastax.driver.core.UserType; import com.datastax.driver.core.schemabuilder.*; - +import java.util.List; import net.helenus.mapping.ColumnType; import net.helenus.mapping.IdentityName; import net.helenus.support.HelenusMappingException; public final class UDTMapDataType extends AbstractCollectionDataType { - private final IdentityName keyType; - private final Class udtKeyClass; - private final IdentityName valueType; - private final Class udtValueClass; + private final IdentityName keyType; + private final Class udtKeyClass; + private final IdentityName valueType; + private final Class udtValueClass; - public UDTMapDataType(ColumnType columnType, IdentityName keyType, Class udtKeyClass, IdentityName valueType, - Class udtValueClass) { - super(columnType); - this.keyType = keyType; - this.udtKeyClass = udtKeyClass; - this.valueType = valueType; - this.udtValueClass = udtValueClass; - } + public UDTMapDataType( + ColumnType columnType, + IdentityName keyType, + Class udtKeyClass, + IdentityName valueType, + Class udtValueClass) { + super(columnType); + this.keyType = keyType; + this.udtKeyClass = udtKeyClass; + this.valueType = valueType; + this.udtValueClass = udtValueClass; + } - @Override - public Class[] getTypeArguments() { - return new Class[]{udtKeyClass, udtValueClass}; - } + @Override + public Class[] getTypeArguments() { + return new Class[] {udtKeyClass, udtValueClass}; + } - public IdentityName getUdtKeyName() { - return keyType; - } + public IdentityName getUdtKeyName() { + return keyType; + } - public Class getUdtKeyClass() { - return udtKeyClass; - } + public Class getUdtKeyClass() { + return udtKeyClass; + } - public IdentityName getUdtValueName() { - return valueType; - } + public IdentityName getUdtValueName() { + return valueType; + } - public Class getUdtValueClass() { - return udtValueClass; - } + public Class getUdtValueClass() { + return udtValueClass; + } - @Override - public void addColumn(Create create, IdentityName columnName) { - ensureSimpleColumn(columnName); + @Override + public void addColumn(Create create, IdentityName columnName) { + ensureSimpleColumn(columnName); - UDTType keyUdtType = SchemaBuilder.frozen(keyType.toCql()); - UDTType valueUdtType = SchemaBuilder.frozen(valueType.toCql()); - create.addUDTMapColumn(columnName.toCql(), keyUdtType, valueUdtType); - } + UDTType keyUdtType = SchemaBuilder.frozen(keyType.toCql()); + UDTType valueUdtType = SchemaBuilder.frozen(valueType.toCql()); + create.addUDTMapColumn(columnName.toCql(), keyUdtType, valueUdtType); + } - @Override - public void addColumn(CreateType create, IdentityName columnName) { - ensureSimpleColumn(columnName); + @Override + public void addColumn(CreateType create, IdentityName columnName) { + ensureSimpleColumn(columnName); - UDTType keyUdtType = SchemaBuilder.frozen(keyType.toCql()); - UDTType valueUdtType = SchemaBuilder.frozen(valueType.toCql()); - create.addUDTMapColumn(columnName.toCql(), keyUdtType, valueUdtType); - } + UDTType keyUdtType = SchemaBuilder.frozen(keyType.toCql()); + UDTType valueUdtType = SchemaBuilder.frozen(valueType.toCql()); + create.addUDTMapColumn(columnName.toCql(), keyUdtType, valueUdtType); + } - @Override - public SchemaStatement alterColumn(Alter alter, IdentityName columnName, OptionalColumnMetadata columnMetadata) { + @Override + public SchemaStatement alterColumn( + Alter alter, IdentityName columnName, OptionalColumnMetadata columnMetadata) { - if (columnMetadata == null) { - return notSupportedOperation("add", columnName); - } + if (columnMetadata == null) { + return notSupportedOperation("add", columnName); + } - DataType schemaDataType = columnMetadata.getType(); - if (schemaDataType.getName() != DataType.Name.MAP) { - return notSupportedOperation("alter", columnName); - } + DataType schemaDataType = columnMetadata.getType(); + if (schemaDataType.getName() != DataType.Name.MAP) { + return notSupportedOperation("alter", columnName); + } - List args = columnMetadata.getType().getTypeArguments(); - if (args.size() != 2) { - return notSupportedOperation("alter", columnName); - } + List args = columnMetadata.getType().getTypeArguments(); + if (args.size() != 2) { + return notSupportedOperation("alter", columnName); + } - DataType keyDataType = args.get(0); - if (keyDataType.getName() != DataType.Name.UDT || !(keyDataType instanceof UserType)) { - return notSupportedOperation("alter", columnName); - } + DataType keyDataType = args.get(0); + if (keyDataType.getName() != DataType.Name.UDT || !(keyDataType instanceof UserType)) { + return notSupportedOperation("alter", columnName); + } - UserType udtKeyType = (UserType) keyDataType; + UserType udtKeyType = (UserType) keyDataType; - if (!keyType.getName().equals(udtKeyType.getTypeName())) { - return notSupportedOperation("alter", columnName); - } + if (!keyType.getName().equals(udtKeyType.getTypeName())) { + return notSupportedOperation("alter", columnName); + } - DataType valueDataType = args.get(1); - if (valueDataType.getName() != DataType.Name.UDT || !(valueDataType instanceof UserType)) { - return notSupportedOperation("alter", columnName); - } + DataType valueDataType = args.get(1); + if (valueDataType.getName() != DataType.Name.UDT || !(valueDataType instanceof UserType)) { + return notSupportedOperation("alter", columnName); + } - UserType udtValueType = (UserType) valueDataType; + UserType udtValueType = (UserType) valueDataType; - if (!valueType.getName().equals(udtValueType.getTypeName())) { - return notSupportedOperation("alter", columnName); - } + if (!valueType.getName().equals(udtValueType.getTypeName())) { + return notSupportedOperation("alter", columnName); + } - // equals - return null; - } + // equals + return null; + } - private SchemaStatement notSupportedOperation(String op, IdentityName columnName) { - throw new HelenusMappingException( - op + " UDTMap column is not supported by Cassandra Driver for column '" + columnName + "'"); - } + private SchemaStatement notSupportedOperation(String op, IdentityName columnName) { + throw new HelenusMappingException( + op + " UDTMap column is not supported by Cassandra Driver for column '" + columnName + "'"); + } - @Override - public String toString() { - return "UDTMap<" + keyType + "," + valueType + ">"; - } + @Override + public String toString() { + return "UDTMap<" + keyType + "," + valueType + ">"; + } } diff --git a/src/main/java/net/helenus/mapping/type/UDTSetDataType.java b/src/main/java/net/helenus/mapping/type/UDTSetDataType.java index c22965f..72ffcb0 100644 --- a/src/main/java/net/helenus/mapping/type/UDTSetDataType.java +++ b/src/main/java/net/helenus/mapping/type/UDTSetDataType.java @@ -15,91 +15,90 @@ */ package net.helenus.mapping.type; -import java.util.List; - import com.datastax.driver.core.DataType; import com.datastax.driver.core.UserType; import com.datastax.driver.core.schemabuilder.*; - +import java.util.List; import net.helenus.mapping.ColumnType; import net.helenus.mapping.IdentityName; import net.helenus.support.HelenusMappingException; public final class UDTSetDataType extends AbstractCollectionDataType { - private final IdentityName udtName; - private final Class udtClass; + private final IdentityName udtName; + private final Class udtClass; - public UDTSetDataType(ColumnType columnType, IdentityName udtName, Class udtClass) { - super(columnType); - this.udtName = udtName; - this.udtClass = udtClass; - } + public UDTSetDataType(ColumnType columnType, IdentityName udtName, Class udtClass) { + super(columnType); + this.udtName = udtName; + this.udtClass = udtClass; + } - @Override - public Class[] getTypeArguments() { - return new Class[]{udtClass}; - } + @Override + public Class[] getTypeArguments() { + return new Class[] {udtClass}; + } - public IdentityName getUdtName() { - return udtName; - } + public IdentityName getUdtName() { + return udtName; + } - @Override - public void addColumn(Create create, IdentityName columnName) { - ensureSimpleColumn(columnName); + @Override + public void addColumn(Create create, IdentityName columnName) { + ensureSimpleColumn(columnName); - UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); - create.addUDTSetColumn(columnName.toCql(), udtType); - } + UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); + create.addUDTSetColumn(columnName.toCql(), udtType); + } - @Override - public void addColumn(CreateType create, IdentityName columnName) { - ensureSimpleColumn(columnName); + @Override + public void addColumn(CreateType create, IdentityName columnName) { + ensureSimpleColumn(columnName); - UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); - create.addUDTSetColumn(columnName.toCql(), udtType); - } + UDTType udtType = SchemaBuilder.frozen(udtName.toCql()); + create.addUDTSetColumn(columnName.toCql(), udtType); + } - @Override - public SchemaStatement alterColumn(Alter alter, IdentityName columnName, OptionalColumnMetadata columnMetadata) { + @Override + public SchemaStatement alterColumn( + Alter alter, IdentityName columnName, OptionalColumnMetadata columnMetadata) { - if (columnMetadata == null) { - return notSupportedOperation("add", columnName); - } + if (columnMetadata == null) { + return notSupportedOperation("add", columnName); + } - DataType schemaDataType = columnMetadata.getType(); - if (schemaDataType.getName() != DataType.Name.SET) { - return notSupportedOperation("alter", columnName); - } + DataType schemaDataType = columnMetadata.getType(); + if (schemaDataType.getName() != DataType.Name.SET) { + return notSupportedOperation("alter", columnName); + } - List args = columnMetadata.getType().getTypeArguments(); - if (args.size() != 1) { - return notSupportedOperation("alter", columnName); - } + List args = columnMetadata.getType().getTypeArguments(); + if (args.size() != 1) { + return notSupportedOperation("alter", columnName); + } - DataType valueDataType = args.get(0); - if (valueDataType.getName() != DataType.Name.UDT || !(valueDataType instanceof UserType)) { - return notSupportedOperation("alter", columnName); - } + DataType valueDataType = args.get(0); + if (valueDataType.getName() != DataType.Name.UDT || !(valueDataType instanceof UserType)) { + return notSupportedOperation("alter", columnName); + } - UserType udtValueType = (UserType) valueDataType; + UserType udtValueType = (UserType) valueDataType; - if (!udtName.getName().equals(udtValueType.getTypeName())) { - return notSupportedOperation("alter", columnName); - } + if (!udtName.getName().equals(udtValueType.getTypeName())) { + return notSupportedOperation("alter", columnName); + } - // equals - return null; - } + // equals + return null; + } - private SchemaStatement notSupportedOperation(String op, IdentityName columnName) { - throw new HelenusMappingException( - op + " UDTSet column is not supported by Cassandra Driver for column '" + columnName + "'"); - } + private SchemaStatement notSupportedOperation(String op, IdentityName columnName) { + throw new HelenusMappingException( + op + " UDTSet column is not supported by Cassandra Driver for column '" + columnName + "'"); + } - @Override - public String toString() { - return "UDTSet<" + udtName + ">"; - } + @Override + public String toString() { + return "UDTSet<" + udtName + ">"; + } } diff --git a/src/main/java/net/helenus/mapping/type/UDTValueMapDataType.java b/src/main/java/net/helenus/mapping/type/UDTValueMapDataType.java index 140f84b..48683c2 100644 --- a/src/main/java/net/helenus/mapping/type/UDTValueMapDataType.java +++ b/src/main/java/net/helenus/mapping/type/UDTValueMapDataType.java @@ -15,98 +15,97 @@ */ package net.helenus.mapping.type; -import java.util.List; - import com.datastax.driver.core.DataType; import com.datastax.driver.core.UserType; import com.datastax.driver.core.schemabuilder.*; - +import java.util.List; import net.helenus.mapping.ColumnType; import net.helenus.mapping.IdentityName; import net.helenus.support.HelenusMappingException; public final class UDTValueMapDataType extends AbstractCollectionDataType { - private final DataType keyType; - private final IdentityName valueType; - private final Class udtValueClass; + private final DataType keyType; + private final IdentityName valueType; + private final Class udtValueClass; - public UDTValueMapDataType(ColumnType columnType, DataType keyType, IdentityName valueType, - Class udtValueClass) { - super(columnType); - this.keyType = keyType; - this.valueType = valueType; - this.udtValueClass = udtValueClass; - } + public UDTValueMapDataType( + ColumnType columnType, DataType keyType, IdentityName valueType, Class udtValueClass) { + super(columnType); + this.keyType = keyType; + this.valueType = valueType; + this.udtValueClass = udtValueClass; + } - @Override - public Class[] getTypeArguments() { - return new Class[]{udtValueClass}; - } + @Override + public Class[] getTypeArguments() { + return new Class[] {udtValueClass}; + } - public IdentityName getUdtValueName() { - return valueType; - } + public IdentityName getUdtValueName() { + return valueType; + } - public Class getUdtValueClass() { - return udtValueClass; - } + public Class getUdtValueClass() { + return udtValueClass; + } - @Override - public void addColumn(Create create, IdentityName columnName) { - ensureSimpleColumn(columnName); + @Override + public void addColumn(Create create, IdentityName columnName) { + ensureSimpleColumn(columnName); - UDTType valueUdtType = SchemaBuilder.frozen(valueType.toCql()); - create.addUDTMapColumn(columnName.toCql(), keyType, valueUdtType); - } + UDTType valueUdtType = SchemaBuilder.frozen(valueType.toCql()); + create.addUDTMapColumn(columnName.toCql(), keyType, valueUdtType); + } - @Override - public void addColumn(CreateType create, IdentityName columnName) { - ensureSimpleColumn(columnName); + @Override + public void addColumn(CreateType create, IdentityName columnName) { + ensureSimpleColumn(columnName); - UDTType valueUdtType = SchemaBuilder.frozen(valueType.toCql()); - create.addUDTMapColumn(columnName.toCql(), keyType, valueUdtType); - } + UDTType valueUdtType = SchemaBuilder.frozen(valueType.toCql()); + create.addUDTMapColumn(columnName.toCql(), keyType, valueUdtType); + } - @Override - public SchemaStatement alterColumn(Alter alter, IdentityName columnName, OptionalColumnMetadata columnMetadata) { + @Override + public SchemaStatement alterColumn( + Alter alter, IdentityName columnName, OptionalColumnMetadata columnMetadata) { - if (columnMetadata == null) { - return notSupportedOperation("add", columnName); - } + if (columnMetadata == null) { + return notSupportedOperation("add", columnName); + } - DataType schemaDataType = columnMetadata.getType(); - if (schemaDataType.getName() != DataType.Name.MAP) { - return notSupportedOperation("alter", columnName); - } + DataType schemaDataType = columnMetadata.getType(); + if (schemaDataType.getName() != DataType.Name.MAP) { + return notSupportedOperation("alter", columnName); + } - List args = columnMetadata.getType().getTypeArguments(); - if (args.size() != 2 || !args.get(0).equals(keyType)) { - return notSupportedOperation("alter", columnName); - } + List args = columnMetadata.getType().getTypeArguments(); + if (args.size() != 2 || !args.get(0).equals(keyType)) { + return notSupportedOperation("alter", columnName); + } - DataType valueDataType = args.get(1); - if (valueDataType.getName() != DataType.Name.UDT || !(valueDataType instanceof UserType)) { - return notSupportedOperation("alter", columnName); - } + DataType valueDataType = args.get(1); + if (valueDataType.getName() != DataType.Name.UDT || !(valueDataType instanceof UserType)) { + return notSupportedOperation("alter", columnName); + } - UserType udtValueType = (UserType) valueDataType; + UserType udtValueType = (UserType) valueDataType; - if (!valueType.getName().equals(udtValueType.getTypeName())) { - return notSupportedOperation("alter", columnName); - } + if (!valueType.getName().equals(udtValueType.getTypeName())) { + return notSupportedOperation("alter", columnName); + } - // equals - return null; - } + // equals + return null; + } - private SchemaStatement notSupportedOperation(String op, IdentityName columnName) { - throw new HelenusMappingException( - op + " UDTMap column is not supported by Cassandra Driver for column '" + columnName + "'"); - } + private SchemaStatement notSupportedOperation(String op, IdentityName columnName) { + throw new HelenusMappingException( + op + " UDTMap column is not supported by Cassandra Driver for column '" + columnName + "'"); + } - @Override - public String toString() { - return "UDTValueMap<" + keyType + "," + valueType + ">"; - } + @Override + public String toString() { + return "UDTValueMap<" + keyType + "," + valueType + ">"; + } } diff --git a/src/main/java/net/helenus/mapping/validator/AlphabetValidator.java b/src/main/java/net/helenus/mapping/validator/AlphabetValidator.java index a69f447..07d67db 100644 --- a/src/main/java/net/helenus/mapping/validator/AlphabetValidator.java +++ b/src/main/java/net/helenus/mapping/validator/AlphabetValidator.java @@ -16,39 +16,38 @@ package net.helenus.mapping.validator; import java.util.Arrays; - import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; - import net.helenus.mapping.annotation.Constraints; -public final class AlphabetValidator implements ConstraintValidator { +public final class AlphabetValidator + implements ConstraintValidator { - char[] alphabet; + char[] alphabet; - @Override - public void initialize(Constraints.Alphabet constraintAnnotation) { - alphabet = constraintAnnotation.value().toCharArray(); - Arrays.sort(alphabet); - } + @Override + public void initialize(Constraints.Alphabet constraintAnnotation) { + alphabet = constraintAnnotation.value().toCharArray(); + Arrays.sort(alphabet); + } - @Override - public boolean isValid(CharSequence value, ConstraintValidatorContext context) { + @Override + public boolean isValid(CharSequence value, ConstraintValidatorContext context) { - if (value == null) { - return true; - } + if (value == null) { + return true; + } - final int len = value.length(); - for (int i = 0; i != len; ++i) { + final int len = value.length(); + for (int i = 0; i != len; ++i) { - char ch = value.charAt(i); + char ch = value.charAt(i); - if (Arrays.binarySearch(alphabet, ch) < 0) { - return false; - } - } + if (Arrays.binarySearch(alphabet, ch) < 0) { + return false; + } + } - return true; - } + return true; + } } diff --git a/src/main/java/net/helenus/mapping/validator/DistinctValidator.java b/src/main/java/net/helenus/mapping/validator/DistinctValidator.java index 1dc1778..8dc4711 100644 --- a/src/main/java/net/helenus/mapping/validator/DistinctValidator.java +++ b/src/main/java/net/helenus/mapping/validator/DistinctValidator.java @@ -17,19 +17,18 @@ package net.helenus.mapping.validator; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; - import net.helenus.mapping.annotation.Constraints; -public final class DistinctValidator implements ConstraintValidator { +public final class DistinctValidator + implements ConstraintValidator { - @Override - public void initialize(Constraints.Distinct constraintAnnotation) { - } + @Override + public void initialize(Constraints.Distinct constraintAnnotation) {} - @Override - public boolean isValid(CharSequence value, ConstraintValidatorContext context) { - // TODO(gburd): if there is an Enum type supplied, check that value is valid - // Enum.name() - return true; - } + @Override + public boolean isValid(CharSequence value, ConstraintValidatorContext context) { + // TODO(gburd): if there is an Enum type supplied, check that value is valid + // Enum.name() + return true; + } } diff --git a/src/main/java/net/helenus/mapping/validator/EmailValidator.java b/src/main/java/net/helenus/mapping/validator/EmailValidator.java index a00d7f8..9cfbdc0 100644 --- a/src/main/java/net/helenus/mapping/validator/EmailValidator.java +++ b/src/main/java/net/helenus/mapping/validator/EmailValidator.java @@ -17,35 +17,33 @@ package net.helenus.mapping.validator; import java.net.IDN; import java.util.regex.Pattern; - import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; - import net.helenus.mapping.annotation.Constraints.Email; public final class EmailValidator implements ConstraintValidator { - static final String ATOM = "[a-z0-9!#$%&'*+/=?^_`{|}~-]"; - static final String DOMAIN = "(" + ATOM + "+(\\." + ATOM + "+)*"; - static final String IP_DOMAIN = "\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\]"; + static final String ATOM = "[a-z0-9!#$%&'*+/=?^_`{|}~-]"; + static final String DOMAIN = "(" + ATOM + "+(\\." + ATOM + "+)*"; + static final String IP_DOMAIN = "\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\]"; - static final String PATTERN = "^" + ATOM + "+(\\." + ATOM + "+)*@" + DOMAIN + "|" + IP_DOMAIN + ")$"; + static final String PATTERN = + "^" + ATOM + "+(\\." + ATOM + "+)*@" + DOMAIN + "|" + IP_DOMAIN + ")$"; - private static final Pattern pattern = Pattern.compile(PATTERN, Pattern.CASE_INSENSITIVE); + private static final Pattern pattern = Pattern.compile(PATTERN, Pattern.CASE_INSENSITIVE); - @Override - public void initialize(Email constraintAnnotation) { - } + @Override + public void initialize(Email constraintAnnotation) {} - @Override - public boolean isValid(CharSequence value, ConstraintValidatorContext context) { + @Override + public boolean isValid(CharSequence value, ConstraintValidatorContext context) { - if (value == null) { - return true; - } + if (value == null) { + return true; + } - String asciiString = IDN.toASCII(value.toString()); + String asciiString = IDN.toASCII(value.toString()); - return pattern.matcher(asciiString).matches(); - } + return pattern.matcher(asciiString).matches(); + } } diff --git a/src/main/java/net/helenus/mapping/validator/LengthValidator.java b/src/main/java/net/helenus/mapping/validator/LengthValidator.java index dc58b69..1b18bcd 100644 --- a/src/main/java/net/helenus/mapping/validator/LengthValidator.java +++ b/src/main/java/net/helenus/mapping/validator/LengthValidator.java @@ -17,27 +17,26 @@ package net.helenus.mapping.validator; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; - import net.helenus.mapping.annotation.Constraints.Length; public final class LengthValidator implements ConstraintValidator, SizeConstraint { - int length; + int length; - @Override - public void initialize(Length constraintAnnotation) { - this.length = constraintAnnotation.value(); - } + @Override + public void initialize(Length constraintAnnotation) { + this.length = constraintAnnotation.value(); + } - @Override - public boolean isValid(Object value, ConstraintValidatorContext context) { + @Override + public boolean isValid(Object value, ConstraintValidatorContext context) { - int[] size = getSize(value); + int[] size = getSize(value); - if (size == null || size.length == 0) { - return true; - } + if (size == null || size.length == 0) { + return true; + } - return size[0] == length; - } + return size[0] == length; + } } diff --git a/src/main/java/net/helenus/mapping/validator/LowerCaseValidator.java b/src/main/java/net/helenus/mapping/validator/LowerCaseValidator.java index e5ee50c..8bb3faa 100644 --- a/src/main/java/net/helenus/mapping/validator/LowerCaseValidator.java +++ b/src/main/java/net/helenus/mapping/validator/LowerCaseValidator.java @@ -17,39 +17,38 @@ package net.helenus.mapping.validator; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; - import net.helenus.mapping.annotation.Constraints; -public final class LowerCaseValidator implements ConstraintValidator { +public final class LowerCaseValidator + implements ConstraintValidator { - private static boolean isUpperCaseLetter(char ch) { - return ch >= 'A' && ch <= 'Z'; - } + private static boolean isUpperCaseLetter(char ch) { + return ch >= 'A' && ch <= 'Z'; + } - @Override - public void initialize(Constraints.LowerCase constraintAnnotation) { - } + @Override + public void initialize(Constraints.LowerCase constraintAnnotation) {} - @Override - public boolean isValid(CharSequence value, ConstraintValidatorContext context) { + @Override + public boolean isValid(CharSequence value, ConstraintValidatorContext context) { - if (value == null) { - return true; - } + if (value == null) { + return true; + } - final int len = value.length(); - for (int i = 0; i != len; ++i) { - char c = value.charAt(i); - if (c <= 0x7F) { - if (isUpperCaseLetter(c)) { - return false; - } - } - if (c != Character.toLowerCase(c)) { - return false; - } - } + final int len = value.length(); + for (int i = 0; i != len; ++i) { + char c = value.charAt(i); + if (c <= 0x7F) { + if (isUpperCaseLetter(c)) { + return false; + } + } + if (c != Character.toLowerCase(c)) { + return false; + } + } - return true; - } + return true; + } } diff --git a/src/main/java/net/helenus/mapping/validator/MaxLengthValidator.java b/src/main/java/net/helenus/mapping/validator/MaxLengthValidator.java index c30e159..31fec70 100644 --- a/src/main/java/net/helenus/mapping/validator/MaxLengthValidator.java +++ b/src/main/java/net/helenus/mapping/validator/MaxLengthValidator.java @@ -17,27 +17,27 @@ package net.helenus.mapping.validator; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; - import net.helenus.mapping.annotation.Constraints; -public final class MaxLengthValidator implements ConstraintValidator, SizeConstraint { +public final class MaxLengthValidator + implements ConstraintValidator, SizeConstraint { - int maxLength; + int maxLength; - @Override - public void initialize(Constraints.MaxLength constraintAnnotation) { - this.maxLength = constraintAnnotation.value(); - } + @Override + public void initialize(Constraints.MaxLength constraintAnnotation) { + this.maxLength = constraintAnnotation.value(); + } - @Override - public boolean isValid(Object value, ConstraintValidatorContext context) { + @Override + public boolean isValid(Object value, ConstraintValidatorContext context) { - int[] size = getSize(value); + int[] size = getSize(value); - if (size == null || size.length == 0) { - return true; - } + if (size == null || size.length == 0) { + return true; + } - return size[0] <= maxLength; - } + return size[0] <= maxLength; + } } diff --git a/src/main/java/net/helenus/mapping/validator/MinLengthValidator.java b/src/main/java/net/helenus/mapping/validator/MinLengthValidator.java index 74116df..f2f3f50 100644 --- a/src/main/java/net/helenus/mapping/validator/MinLengthValidator.java +++ b/src/main/java/net/helenus/mapping/validator/MinLengthValidator.java @@ -17,27 +17,27 @@ package net.helenus.mapping.validator; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; - import net.helenus.mapping.annotation.Constraints; -public final class MinLengthValidator implements ConstraintValidator, SizeConstraint { +public final class MinLengthValidator + implements ConstraintValidator, SizeConstraint { - int minLength; + int minLength; - @Override - public void initialize(Constraints.MinLength constraintAnnotation) { - this.minLength = constraintAnnotation.value(); - } + @Override + public void initialize(Constraints.MinLength constraintAnnotation) { + this.minLength = constraintAnnotation.value(); + } - @Override - public boolean isValid(Object value, ConstraintValidatorContext context) { + @Override + public boolean isValid(Object value, ConstraintValidatorContext context) { - int[] size = getSize(value); + int[] size = getSize(value); - if (size == null || size.length == 0) { - return true; - } + if (size == null || size.length == 0) { + return true; + } - return size[0] >= minLength; - } + return size[0] >= minLength; + } } diff --git a/src/main/java/net/helenus/mapping/validator/NotEmptyValidator.java b/src/main/java/net/helenus/mapping/validator/NotEmptyValidator.java index 9c7e03e..a378b61 100644 --- a/src/main/java/net/helenus/mapping/validator/NotEmptyValidator.java +++ b/src/main/java/net/helenus/mapping/validator/NotEmptyValidator.java @@ -17,29 +17,28 @@ package net.helenus.mapping.validator; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; - import net.helenus.mapping.annotation.Constraints; import net.helenus.mapping.annotation.Constraints.NotEmpty; -public final class NotEmptyValidator implements ConstraintValidator, SizeConstraint { +public final class NotEmptyValidator + implements ConstraintValidator, SizeConstraint { - @Override - public void initialize(NotEmpty constraintAnnotation) { - } + @Override + public void initialize(NotEmpty constraintAnnotation) {} - @Override - public boolean isValid(Object value, ConstraintValidatorContext context) { + @Override + public boolean isValid(Object value, ConstraintValidatorContext context) { - int[] size = getSize(value); + int[] size = getSize(value); - if (size == null) { - return false; - } + if (size == null) { + return false; + } - if (size.length == 0) { - return true; - } + if (size.length == 0) { + return true; + } - return size[0] > 0; - } + return size[0] > 0; + } } diff --git a/src/main/java/net/helenus/mapping/validator/NotNullValidator.java b/src/main/java/net/helenus/mapping/validator/NotNullValidator.java index 7191e53..5fc6820 100644 --- a/src/main/java/net/helenus/mapping/validator/NotNullValidator.java +++ b/src/main/java/net/helenus/mapping/validator/NotNullValidator.java @@ -17,18 +17,16 @@ package net.helenus.mapping.validator; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; - import net.helenus.mapping.annotation.Constraints; import net.helenus.mapping.annotation.Constraints.NotNull; public final class NotNullValidator implements ConstraintValidator { - @Override - public void initialize(NotNull constraintAnnotation) { - } + @Override + public void initialize(NotNull constraintAnnotation) {} - @Override - public boolean isValid(Object value, ConstraintValidatorContext context) { - return value != null; - } + @Override + public boolean isValid(Object value, ConstraintValidatorContext context) { + return value != null; + } } diff --git a/src/main/java/net/helenus/mapping/validator/NumberValidator.java b/src/main/java/net/helenus/mapping/validator/NumberValidator.java index 9a5b06d..fa233ad 100644 --- a/src/main/java/net/helenus/mapping/validator/NumberValidator.java +++ b/src/main/java/net/helenus/mapping/validator/NumberValidator.java @@ -17,36 +17,34 @@ package net.helenus.mapping.validator; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; - import net.helenus.mapping.annotation.Constraints; public class NumberValidator implements ConstraintValidator { - private static boolean isNumber(char ch) { - return ch >= '0' && ch <= '9'; - } + private static boolean isNumber(char ch) { + return ch >= '0' && ch <= '9'; + } - @Override - public void initialize(Constraints.Number constraintAnnotation) { - } + @Override + public void initialize(Constraints.Number constraintAnnotation) {} - @Override - public boolean isValid(CharSequence value, ConstraintValidatorContext context) { + @Override + public boolean isValid(CharSequence value, ConstraintValidatorContext context) { - if (value == null) { - return true; - } + if (value == null) { + return true; + } - final int len = value.length(); - for (int i = 0; i != len; ++i) { + final int len = value.length(); + for (int i = 0; i != len; ++i) { - char ch = value.charAt(i); + char ch = value.charAt(i); - if (!isNumber(ch)) { - return false; - } - } + if (!isNumber(ch)) { + return false; + } + } - return true; - } + return true; + } } diff --git a/src/main/java/net/helenus/mapping/validator/PatternValidator.java b/src/main/java/net/helenus/mapping/validator/PatternValidator.java index ac768ca..5603179 100644 --- a/src/main/java/net/helenus/mapping/validator/PatternValidator.java +++ b/src/main/java/net/helenus/mapping/validator/PatternValidator.java @@ -16,28 +16,27 @@ package net.helenus.mapping.validator; import java.util.regex.Pattern; - import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; - import net.helenus.mapping.annotation.Constraints; -public final class PatternValidator implements ConstraintValidator { +public final class PatternValidator + implements ConstraintValidator { - private Pattern pattern; + private Pattern pattern; - @Override - public void initialize(Constraints.Pattern constraintAnnotation) { - pattern = Pattern.compile(constraintAnnotation.value(), constraintAnnotation.flags()); - } + @Override + public void initialize(Constraints.Pattern constraintAnnotation) { + pattern = Pattern.compile(constraintAnnotation.value(), constraintAnnotation.flags()); + } - @Override - public boolean isValid(CharSequence value, ConstraintValidatorContext context) { + @Override + public boolean isValid(CharSequence value, ConstraintValidatorContext context) { - if (value == null) { - return true; - } + if (value == null) { + return true; + } - return pattern.matcher(value).matches(); - } + return pattern.matcher(value).matches(); + } } diff --git a/src/main/java/net/helenus/mapping/validator/SizeConstraint.java b/src/main/java/net/helenus/mapping/validator/SizeConstraint.java index a17f72e..e6ff107 100644 --- a/src/main/java/net/helenus/mapping/validator/SizeConstraint.java +++ b/src/main/java/net/helenus/mapping/validator/SizeConstraint.java @@ -22,38 +22,38 @@ import java.util.Map; public interface SizeConstraint { - static final int[] EMPTY = new int[0]; + static final int[] EMPTY = new int[0]; - default int[] getSize(Object value) { + default int[] getSize(Object value) { - if (value == null) { - return null; - } + if (value == null) { + return null; + } - if (value.getClass().isArray()) { - return new int[]{Array.getLength(value)}; - } + if (value.getClass().isArray()) { + return new int[] {Array.getLength(value)}; + } - if (value instanceof CharSequence) { - CharSequence seq = (CharSequence) value; - return new int[]{seq.length()}; - } + if (value instanceof CharSequence) { + CharSequence seq = (CharSequence) value; + return new int[] {seq.length()}; + } - if (value instanceof ByteBuffer) { - ByteBuffer bb = (ByteBuffer) value; - return new int[]{bb.position()}; - } + if (value instanceof ByteBuffer) { + ByteBuffer bb = (ByteBuffer) value; + return new int[] {bb.position()}; + } - if (value instanceof Collection) { - Collection col = (Collection) value; - return new int[]{col.size()}; - } + if (value instanceof Collection) { + Collection col = (Collection) value; + return new int[] {col.size()}; + } - if (value instanceof Map) { - Map map = (Map) value; - return new int[]{map.size()}; - } + if (value instanceof Map) { + Map map = (Map) value; + return new int[] {map.size()}; + } - return EMPTY; - } + return EMPTY; + } } diff --git a/src/main/java/net/helenus/mapping/validator/UpperCaseValidator.java b/src/main/java/net/helenus/mapping/validator/UpperCaseValidator.java index e04c6a5..de6ea2f 100644 --- a/src/main/java/net/helenus/mapping/validator/UpperCaseValidator.java +++ b/src/main/java/net/helenus/mapping/validator/UpperCaseValidator.java @@ -17,39 +17,38 @@ package net.helenus.mapping.validator; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; - import net.helenus.mapping.annotation.Constraints; -public final class UpperCaseValidator implements ConstraintValidator { +public final class UpperCaseValidator + implements ConstraintValidator { - private static boolean isLowerCaseLetter(char ch) { - return ch >= 'a' && ch <= 'z'; - } + private static boolean isLowerCaseLetter(char ch) { + return ch >= 'a' && ch <= 'z'; + } - @Override - public void initialize(Constraints.UpperCase constraintAnnotation) { - } + @Override + public void initialize(Constraints.UpperCase constraintAnnotation) {} - @Override - public boolean isValid(CharSequence value, ConstraintValidatorContext context) { + @Override + public boolean isValid(CharSequence value, ConstraintValidatorContext context) { - if (value == null) { - return true; - } + if (value == null) { + return true; + } - final int len = value.length(); - for (int i = 0; i != len; ++i) { - char c = value.charAt(i); - if (c <= 0x7F) { - if (isLowerCaseLetter(c)) { - return false; - } - } - if (c != Character.toUpperCase(c)) { - return false; - } - } + final int len = value.length(); + for (int i = 0; i != len; ++i) { + char c = value.charAt(i); + if (c <= 0x7F) { + if (isLowerCaseLetter(c)) { + return false; + } + } + if (c != Character.toUpperCase(c)) { + return false; + } + } - return true; - } + return true; + } } diff --git a/src/main/java/net/helenus/mapping/value/BeanColumnValueProvider.java b/src/main/java/net/helenus/mapping/value/BeanColumnValueProvider.java index ae18793..9c1c44d 100644 --- a/src/main/java/net/helenus/mapping/value/BeanColumnValueProvider.java +++ b/src/main/java/net/helenus/mapping/value/BeanColumnValueProvider.java @@ -17,32 +17,32 @@ package net.helenus.mapping.value; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; - import net.helenus.mapping.HelenusProperty; import net.helenus.support.HelenusException; import net.helenus.support.HelenusMappingException; public enum BeanColumnValueProvider implements ColumnValueProvider { - INSTANCE; + INSTANCE; - @Override - public V getColumnValue(Object bean, int columnIndexUnused, HelenusProperty property, boolean immutable) { + @Override + public V getColumnValue( + Object bean, int columnIndexUnused, HelenusProperty property, boolean immutable) { - Method getter = property.getGetterMethod(); + Method getter = property.getGetterMethod(); - Object value = null; - try { - value = getter.invoke(bean, new Object[]{}); - } catch (InvocationTargetException e) { - if (e.getCause() != null) { - throw new HelenusException("getter threw an exception", e.getCause()); - } - } catch (ReflectiveOperationException e) { - throw new HelenusMappingException("fail to call getter " + getter, e); - } catch (IllegalArgumentException e) { - throw new HelenusMappingException("invalid getter " + getter, e); - } + Object value = null; + try { + value = getter.invoke(bean, new Object[] {}); + } catch (InvocationTargetException e) { + if (e.getCause() != null) { + throw new HelenusException("getter threw an exception", e.getCause()); + } + } catch (ReflectiveOperationException e) { + throw new HelenusMappingException("fail to call getter " + getter, e); + } catch (IllegalArgumentException e) { + throw new HelenusMappingException("invalid getter " + getter, e); + } - return (V) value; - } + return (V) value; + } } diff --git a/src/main/java/net/helenus/mapping/value/ColumnValuePreparer.java b/src/main/java/net/helenus/mapping/value/ColumnValuePreparer.java index d4e0e5e..294f63b 100644 --- a/src/main/java/net/helenus/mapping/value/ColumnValuePreparer.java +++ b/src/main/java/net/helenus/mapping/value/ColumnValuePreparer.java @@ -18,14 +18,13 @@ package net.helenus.mapping.value; import com.datastax.driver.core.CodecRegistry; import com.datastax.driver.core.DataType; import com.datastax.driver.core.TypeCodec; - import net.helenus.mapping.HelenusProperty; public interface ColumnValuePreparer { - Object prepareColumnValue(Object source, HelenusProperty prop); + Object prepareColumnValue(Object source, HelenusProperty prop); - default TypeCodec codecFor(DataType type) { - return CodecRegistry.DEFAULT_INSTANCE.codecFor(type); - } + default TypeCodec codecFor(DataType type) { + return CodecRegistry.DEFAULT_INSTANCE.codecFor(type); + } } diff --git a/src/main/java/net/helenus/mapping/value/ColumnValueProvider.java b/src/main/java/net/helenus/mapping/value/ColumnValueProvider.java index 447acbd..45c604b 100644 --- a/src/main/java/net/helenus/mapping/value/ColumnValueProvider.java +++ b/src/main/java/net/helenus/mapping/value/ColumnValueProvider.java @@ -18,18 +18,17 @@ package net.helenus.mapping.value; import com.datastax.driver.core.CodecRegistry; import com.datastax.driver.core.DataType; import com.datastax.driver.core.TypeCodec; - import net.helenus.mapping.HelenusProperty; public interface ColumnValueProvider { - V getColumnValue(Object source, int columnIndex, HelenusProperty property, boolean immutable); + V getColumnValue(Object source, int columnIndex, HelenusProperty property, boolean immutable); - default V getColumnValue(Object source, int columnIndex, HelenusProperty property) { - return getColumnValue(source, columnIndex, property, false); - } + default V getColumnValue(Object source, int columnIndex, HelenusProperty property) { + return getColumnValue(source, columnIndex, property, false); + } - default TypeCodec codecFor(DataType type) { - return CodecRegistry.DEFAULT_INSTANCE.codecFor(type); - } + default TypeCodec codecFor(DataType type) { + return CodecRegistry.DEFAULT_INSTANCE.codecFor(type); + } } diff --git a/src/main/java/net/helenus/mapping/value/RowColumnValueProvider.java b/src/main/java/net/helenus/mapping/value/RowColumnValueProvider.java index 33c5db7..9693d24 100644 --- a/src/main/java/net/helenus/mapping/value/RowColumnValueProvider.java +++ b/src/main/java/net/helenus/mapping/value/RowColumnValueProvider.java @@ -15,13 +15,6 @@ */ package net.helenus.mapping.value; -import java.nio.ByteBuffer; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.function.Function; - import com.datastax.driver.core.ColumnDefinitions; import com.datastax.driver.core.DataType; import com.datastax.driver.core.ProtocolVersion; @@ -29,107 +22,119 @@ import com.datastax.driver.core.Row; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; - +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.function.Function; import net.helenus.core.SessionRepository; import net.helenus.mapping.HelenusProperty; public final class RowColumnValueProvider implements ColumnValueProvider { - private final SessionRepository repository; + private final SessionRepository repository; - public RowColumnValueProvider(SessionRepository repository) { - this.repository = repository; - } + public RowColumnValueProvider(SessionRepository repository) { + this.repository = repository; + } - @Override - public V getColumnValue(Object sourceObj, int columnIndex, HelenusProperty property, boolean immutable) { + @Override + public V getColumnValue( + Object sourceObj, int columnIndex, HelenusProperty property, boolean immutable) { - Row source = (Row) sourceObj; + Row source = (Row) sourceObj; - Object value = null; - if (columnIndex != -1) { - value = readValueByIndex(source, columnIndex, immutable); - } else { - value = readValueByName(source, property.getColumnName().getName(), immutable); - } + Object value = null; + if (columnIndex != -1) { + value = readValueByIndex(source, columnIndex, immutable); + } else { + value = readValueByName(source, property.getColumnName().getName(), immutable); + } - if (value != null) { + if (value != null) { - Optional> converter = property.getReadConverter(repository); + Optional> converter = property.getReadConverter(repository); - if (converter.isPresent()) { - value = converter.get().apply(value); - } - } + if (converter.isPresent()) { + value = converter.get().apply(value); + } + } - return (V) value; - } + return (V) value; + } - private Object readValueByIndex(Row source, int columnIndex, boolean immutable) { + private Object readValueByIndex(Row source, int columnIndex, boolean immutable) { - if (source.isNull(columnIndex)) { - return null; - } + if (source.isNull(columnIndex)) { + return null; + } - ColumnDefinitions columnDefinitions = source.getColumnDefinitions(); + ColumnDefinitions columnDefinitions = source.getColumnDefinitions(); - DataType columnType = columnDefinitions.getType(columnIndex); + DataType columnType = columnDefinitions.getType(columnIndex); - if (columnType.isCollection()) { + if (columnType.isCollection()) { - List typeArguments = columnType.getTypeArguments(); + List typeArguments = columnType.getTypeArguments(); - switch (columnType.getName()) { - case SET : - Set set = source.getSet(columnIndex, codecFor(typeArguments.get(0)).getJavaType()); - return immutable ? ImmutableSet.copyOf(set) : set; - case MAP : - Map map = source.getMap(columnIndex, codecFor(typeArguments.get(0)).getJavaType(), - codecFor(typeArguments.get(1)).getJavaType()); - return immutable ? ImmutableMap.copyOf(map) : map; - case LIST : - List list = source.getList(columnIndex, codecFor(typeArguments.get(0)).getJavaType()); - return immutable ? ImmutableList.copyOf(list) : list; - } - } + switch (columnType.getName()) { + case SET: + Set set = source.getSet(columnIndex, codecFor(typeArguments.get(0)).getJavaType()); + return immutable ? ImmutableSet.copyOf(set) : set; + case MAP: + Map map = + source.getMap( + columnIndex, + codecFor(typeArguments.get(0)).getJavaType(), + codecFor(typeArguments.get(1)).getJavaType()); + return immutable ? ImmutableMap.copyOf(map) : map; + case LIST: + List list = source.getList(columnIndex, codecFor(typeArguments.get(0)).getJavaType()); + return immutable ? ImmutableList.copyOf(list) : list; + } + } - ByteBuffer bytes = source.getBytesUnsafe(columnIndex); - Object value = codecFor(columnType).deserialize(bytes, ProtocolVersion.NEWEST_SUPPORTED); + ByteBuffer bytes = source.getBytesUnsafe(columnIndex); + Object value = codecFor(columnType).deserialize(bytes, ProtocolVersion.NEWEST_SUPPORTED); - return value; - } + return value; + } - private Object readValueByName(Row source, String columnName, boolean immutable) { + private Object readValueByName(Row source, String columnName, boolean immutable) { - if (source.isNull(columnName)) { - return null; - } + if (source.isNull(columnName)) { + return null; + } - ColumnDefinitions columnDefinitions = source.getColumnDefinitions(); + ColumnDefinitions columnDefinitions = source.getColumnDefinitions(); - DataType columnType = columnDefinitions.getType(columnName); + DataType columnType = columnDefinitions.getType(columnName); - if (columnType.isCollection()) { + if (columnType.isCollection()) { - List typeArguments = columnType.getTypeArguments(); + List typeArguments = columnType.getTypeArguments(); - switch (columnType.getName()) { - case SET : - Set set = source.getSet(columnName, codecFor(typeArguments.get(0)).getJavaType()); - return immutable ? ImmutableSet.copyOf(set) : set; - case MAP : - Map map = source.getMap(columnName, codecFor(typeArguments.get(0)).getJavaType(), - codecFor(typeArguments.get(1)).getJavaType()); - return immutable ? ImmutableMap.copyOf(map) : map; - case LIST : - List list = source.getList(columnName, codecFor(typeArguments.get(0)).getJavaType()); - return immutable ? ImmutableList.copyOf(list) : list; - } - } + switch (columnType.getName()) { + case SET: + Set set = source.getSet(columnName, codecFor(typeArguments.get(0)).getJavaType()); + return immutable ? ImmutableSet.copyOf(set) : set; + case MAP: + Map map = + source.getMap( + columnName, + codecFor(typeArguments.get(0)).getJavaType(), + codecFor(typeArguments.get(1)).getJavaType()); + return immutable ? ImmutableMap.copyOf(map) : map; + case LIST: + List list = source.getList(columnName, codecFor(typeArguments.get(0)).getJavaType()); + return immutable ? ImmutableList.copyOf(list) : list; + } + } - ByteBuffer bytes = source.getBytesUnsafe(columnName); - Object value = codecFor(columnType).deserialize(bytes, ProtocolVersion.NEWEST_SUPPORTED); + ByteBuffer bytes = source.getBytesUnsafe(columnName); + Object value = codecFor(columnType).deserialize(bytes, ProtocolVersion.NEWEST_SUPPORTED); - return value; - } + return value; + } } diff --git a/src/main/java/net/helenus/mapping/value/StatementColumnValuePreparer.java b/src/main/java/net/helenus/mapping/value/StatementColumnValuePreparer.java index 1e32b8b..e4d1203 100644 --- a/src/main/java/net/helenus/mapping/value/StatementColumnValuePreparer.java +++ b/src/main/java/net/helenus/mapping/value/StatementColumnValuePreparer.java @@ -15,44 +15,38 @@ */ package net.helenus.mapping.value; +import com.datastax.driver.core.querybuilder.BindMarker; import java.util.Optional; import java.util.function.Function; - -import com.datastax.driver.core.querybuilder.BindMarker; - import net.helenus.core.HelenusValidator; import net.helenus.core.SessionRepository; import net.helenus.mapping.HelenusProperty; public final class StatementColumnValuePreparer implements ColumnValuePreparer { - private final SessionRepository repository; + private final SessionRepository repository; - public StatementColumnValuePreparer(SessionRepository repository) { - this.repository = repository; - } + public StatementColumnValuePreparer(SessionRepository repository) { + this.repository = repository; + } - @Override - public Object prepareColumnValue(Object value, HelenusProperty prop) { + @Override + public Object prepareColumnValue(Object value, HelenusProperty prop) { - if (value == null) - return null; + if (value == null) return null; - if (value instanceof BindMarker) { - return value; - } + if (value instanceof BindMarker) { + return value; + } - HelenusValidator.INSTANCE.validate(prop, value); + HelenusValidator.INSTANCE.validate(prop, value); - if (value != null) { + Optional> converter = prop.getWriteConverter(repository); - Optional> converter = prop.getWriteConverter(repository); + if (converter.isPresent()) { + value = converter.get().apply(value); + } - if (converter.isPresent()) { - value = converter.get().apply(value); - } - } - - return value; - } + return value; + } } diff --git a/src/main/java/net/helenus/mapping/value/TupleColumnValuePreparer.java b/src/main/java/net/helenus/mapping/value/TupleColumnValuePreparer.java index 552b425..df286ea 100644 --- a/src/main/java/net/helenus/mapping/value/TupleColumnValuePreparer.java +++ b/src/main/java/net/helenus/mapping/value/TupleColumnValuePreparer.java @@ -15,52 +15,50 @@ */ package net.helenus.mapping.value; -import java.util.Optional; -import java.util.function.Function; - import com.datastax.driver.core.DataType; import com.datastax.driver.core.ProtocolVersion; import com.datastax.driver.core.TupleType; import com.datastax.driver.core.querybuilder.BindMarker; - +import java.util.Optional; +import java.util.function.Function; import net.helenus.core.HelenusValidator; import net.helenus.core.SessionRepository; import net.helenus.mapping.HelenusProperty; public final class TupleColumnValuePreparer implements ColumnValuePreparer { - private final TupleType tupleType; - private final SessionRepository repository; + private final TupleType tupleType; + private final SessionRepository repository; - public TupleColumnValuePreparer(TupleType tupleType, SessionRepository repository) { - this.tupleType = tupleType; - this.repository = repository; - } + public TupleColumnValuePreparer(TupleType tupleType, SessionRepository repository) { + this.tupleType = tupleType; + this.repository = repository; + } - @Override - public Object prepareColumnValue(Object value, HelenusProperty prop) { + @Override + public Object prepareColumnValue(Object value, HelenusProperty prop) { - if (value instanceof BindMarker) { - return value; - } + if (value instanceof BindMarker) { + return value; + } - HelenusValidator.INSTANCE.validate(prop, value); + HelenusValidator.INSTANCE.validate(prop, value); - if (value != null) { + if (value != null) { - Optional> converter = prop.getWriteConverter(repository); + Optional> converter = prop.getWriteConverter(repository); - if (converter.isPresent()) { - value = converter.get().apply(value); - } + if (converter.isPresent()) { + value = converter.get().apply(value); + } - int columnIndex = prop.getOrdinal(); + int columnIndex = prop.getOrdinal(); - DataType dataType = tupleType.getComponentTypes().get(columnIndex); + DataType dataType = tupleType.getComponentTypes().get(columnIndex); - return codecFor(dataType).serialize(value, ProtocolVersion.NEWEST_SUPPORTED); - } + return codecFor(dataType).serialize(value, ProtocolVersion.NEWEST_SUPPORTED); + } - return null; - } + return null; + } } diff --git a/src/main/java/net/helenus/mapping/value/TupleColumnValueProvider.java b/src/main/java/net/helenus/mapping/value/TupleColumnValueProvider.java index d005884..569598c 100644 --- a/src/main/java/net/helenus/mapping/value/TupleColumnValueProvider.java +++ b/src/main/java/net/helenus/mapping/value/TupleColumnValueProvider.java @@ -15,52 +15,51 @@ */ package net.helenus.mapping.value; -import java.nio.ByteBuffer; -import java.util.Optional; -import java.util.function.Function; - import com.datastax.driver.core.DataType; import com.datastax.driver.core.ProtocolVersion; import com.datastax.driver.core.TupleType; import com.datastax.driver.core.TupleValue; - +import java.nio.ByteBuffer; +import java.util.Optional; +import java.util.function.Function; import net.helenus.core.SessionRepository; import net.helenus.mapping.HelenusProperty; public final class TupleColumnValueProvider implements ColumnValueProvider { - private final SessionRepository repository; + private final SessionRepository repository; - public TupleColumnValueProvider(SessionRepository repository) { - this.repository = repository; - } + public TupleColumnValueProvider(SessionRepository repository) { + this.repository = repository; + } - @Override - public V getColumnValue(Object sourceObj, int columnIndexUnused, HelenusProperty property, boolean immutable) { + @Override + public V getColumnValue( + Object sourceObj, int columnIndexUnused, HelenusProperty property, boolean immutable) { - int columnIndex = property.getOrdinal(); + int columnIndex = property.getOrdinal(); - TupleValue source = (TupleValue) sourceObj; + TupleValue source = (TupleValue) sourceObj; - ByteBuffer bytes = source.getBytesUnsafe(columnIndex); - if (bytes == null) { - return null; - } + ByteBuffer bytes = source.getBytesUnsafe(columnIndex); + if (bytes == null) { + return null; + } - TupleType tupleType = source.getType(); - DataType fieldType = tupleType.getComponentTypes().get(columnIndex); + TupleType tupleType = source.getType(); + DataType fieldType = tupleType.getComponentTypes().get(columnIndex); - Object value = codecFor(fieldType).deserialize(bytes, ProtocolVersion.NEWEST_SUPPORTED); + Object value = codecFor(fieldType).deserialize(bytes, ProtocolVersion.NEWEST_SUPPORTED); - if (value != null) { + if (value != null) { - Optional> converter = property.getReadConverter(repository); + Optional> converter = property.getReadConverter(repository); - if (converter.isPresent()) { - value = converter.get().apply(value); - } - } + if (converter.isPresent()) { + value = converter.get().apply(value); + } + } - return (V) value; - } + return (V) value; + } } diff --git a/src/main/java/net/helenus/mapping/value/UDTColumnValuePreparer.java b/src/main/java/net/helenus/mapping/value/UDTColumnValuePreparer.java index 1ff5b2c..6331aa3 100644 --- a/src/main/java/net/helenus/mapping/value/UDTColumnValuePreparer.java +++ b/src/main/java/net/helenus/mapping/value/UDTColumnValuePreparer.java @@ -15,50 +15,48 @@ */ package net.helenus.mapping.value; -import java.util.Optional; -import java.util.function.Function; - import com.datastax.driver.core.DataType; import com.datastax.driver.core.ProtocolVersion; import com.datastax.driver.core.UserType; import com.datastax.driver.core.querybuilder.BindMarker; - +import java.util.Optional; +import java.util.function.Function; import net.helenus.core.HelenusValidator; import net.helenus.core.SessionRepository; import net.helenus.mapping.HelenusProperty; public final class UDTColumnValuePreparer implements ColumnValuePreparer { - private final UserType userType; - private final SessionRepository repository; + private final UserType userType; + private final SessionRepository repository; - public UDTColumnValuePreparer(UserType userType, SessionRepository repository) { - this.userType = userType; - this.repository = repository; - } + public UDTColumnValuePreparer(UserType userType, SessionRepository repository) { + this.userType = userType; + this.repository = repository; + } - @Override - public Object prepareColumnValue(Object value, HelenusProperty prop) { + @Override + public Object prepareColumnValue(Object value, HelenusProperty prop) { - if (value instanceof BindMarker) { - return value; - } + if (value instanceof BindMarker) { + return value; + } - HelenusValidator.INSTANCE.validate(prop, value); + HelenusValidator.INSTANCE.validate(prop, value); - if (value != null) { + if (value != null) { - Optional> converter = prop.getWriteConverter(repository); + Optional> converter = prop.getWriteConverter(repository); - if (converter.isPresent()) { - value = converter.get().apply(value); - } + if (converter.isPresent()) { + value = converter.get().apply(value); + } - DataType dataType = userType.getFieldType(prop.getColumnName().getName()); + DataType dataType = userType.getFieldType(prop.getColumnName().getName()); - return codecFor(dataType).serialize(value, ProtocolVersion.NEWEST_SUPPORTED); - } + return codecFor(dataType).serialize(value, ProtocolVersion.NEWEST_SUPPORTED); + } - return null; - } + return null; + } } diff --git a/src/main/java/net/helenus/mapping/value/UDTColumnValueProvider.java b/src/main/java/net/helenus/mapping/value/UDTColumnValueProvider.java index 13474d3..d6f0c5a 100644 --- a/src/main/java/net/helenus/mapping/value/UDTColumnValueProvider.java +++ b/src/main/java/net/helenus/mapping/value/UDTColumnValueProvider.java @@ -15,53 +15,52 @@ */ package net.helenus.mapping.value; -import java.nio.ByteBuffer; -import java.util.Optional; -import java.util.function.Function; - import com.datastax.driver.core.DataType; import com.datastax.driver.core.ProtocolVersion; import com.datastax.driver.core.UDTValue; import com.datastax.driver.core.UserType; - +import java.nio.ByteBuffer; +import java.util.Optional; +import java.util.function.Function; import net.helenus.core.SessionRepository; import net.helenus.mapping.HelenusProperty; public final class UDTColumnValueProvider implements ColumnValueProvider { - private final SessionRepository repository; + private final SessionRepository repository; - public UDTColumnValueProvider(SessionRepository repository) { - this.repository = repository; - } + public UDTColumnValueProvider(SessionRepository repository) { + this.repository = repository; + } - @Override - @SuppressWarnings("unchecked") - public V getColumnValue(Object sourceObj, int columnIndexUnused, HelenusProperty property, boolean immutable) { + @Override + @SuppressWarnings("unchecked") + public V getColumnValue( + Object sourceObj, int columnIndexUnused, HelenusProperty property, boolean immutable) { - UDTValue source = (UDTValue) sourceObj; + UDTValue source = (UDTValue) sourceObj; - UserType userType = source.getType(); + UserType userType = source.getType(); - String name = property.getColumnName().getName(); + String name = property.getColumnName().getName(); - ByteBuffer bytes = source.getBytesUnsafe(name); - if (bytes == null) { - return null; - } + ByteBuffer bytes = source.getBytesUnsafe(name); + if (bytes == null) { + return null; + } - DataType fieldType = userType.getFieldType(name); - Object value = codecFor(fieldType).deserialize(bytes, ProtocolVersion.NEWEST_SUPPORTED); + DataType fieldType = userType.getFieldType(name); + Object value = codecFor(fieldType).deserialize(bytes, ProtocolVersion.NEWEST_SUPPORTED); - if (value != null) { + if (value != null) { - Optional> converter = property.getReadConverter(repository); + Optional> converter = property.getReadConverter(repository); - if (converter.isPresent()) { - value = converter.get().apply(value); - } - } + if (converter.isPresent()) { + value = converter.get().apply(value); + } + } - return (V) value; - } + return (V) value; + } } diff --git a/src/main/java/net/helenus/mapping/value/ValueProviderMap.java b/src/main/java/net/helenus/mapping/value/ValueProviderMap.java index 562acca..24b60ab 100644 --- a/src/main/java/net/helenus/mapping/value/ValueProviderMap.java +++ b/src/main/java/net/helenus/mapping/value/ValueProviderMap.java @@ -19,7 +19,6 @@ import java.util.Collection; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; - import net.helenus.core.reflect.Drafted; import net.helenus.mapping.HelenusEntity; import net.helenus.mapping.HelenusProperty; @@ -27,118 +26,164 @@ import net.helenus.support.HelenusMappingException; public final class ValueProviderMap implements Map { - private final Object source; - private final ColumnValueProvider valueProvider; - private final HelenusEntity entity; - private final boolean immutable; + private final Object source; + private final ColumnValueProvider valueProvider; + private final HelenusEntity entity; + private final boolean immutable; - public ValueProviderMap(Object source, ColumnValueProvider valueProvider, HelenusEntity entity) { - this.source = source; - this.valueProvider = valueProvider; - this.entity = entity; - this.immutable = entity.getMappingInterface().isAssignableFrom(Drafted.class); - } + public ValueProviderMap(Object source, ColumnValueProvider valueProvider, HelenusEntity entity) { + this.source = source; + this.valueProvider = valueProvider; + this.entity = entity; + this.immutable = entity.getMappingInterface().isAssignableFrom(Drafted.class); + } - @Override - public Object get(Object key) { - if (key instanceof String) { - String name = (String) key; - HelenusProperty prop = entity.getProperty(name); - if (prop != null) { - return valueProvider.getColumnValue(source, -1, prop, immutable); - } - } - return null; - } + private static void throwShouldNeverCall(String methodName) { + throw new HelenusMappingException( + String.format( + "the method %s should never be called on an instance of a Helenus ValueProviderMap", + methodName)); + } - @Override - public Set keySet() { - return entity.getOrderedProperties().stream().map(p -> p.getPropertyName()).collect(Collectors.toSet()); - } + @Override + public Object get(Object key) { + if (key instanceof String) { + String name = (String) key; + HelenusProperty prop = entity.getProperty(name); + if (prop != null) { + return valueProvider.getColumnValue(source, -1, prop, immutable); + } + } + return null; + } - @Override - public int size() { - return entity.getOrderedProperties().size(); - } + @Override + public Set keySet() { + return entity + .getOrderedProperties() + .stream() + .map(p -> p.getPropertyName()) + .collect(Collectors.toSet()); + } - @Override - public boolean isEmpty() { - return entity.getOrderedProperties().size() > 0; - } + @Override + public int size() { + return entity.getOrderedProperties().size(); + } - @Override - public boolean containsKey(Object key) { - if (key instanceof Object) { - String s = (String) key; - return keySet().contains(s); - } - return false; - } + @Override + public boolean isEmpty() { + return entity.getOrderedProperties().size() > 0; + } - @Override - public boolean containsValue(Object value) { - throwShouldNeverCall("containsValue()"); - return false; - } + @Override + public boolean containsKey(Object key) { + if (key instanceof Object) { + String s = (String) key; + return keySet().contains(s); + } + return false; + } - @Override - public Object put(String key, Object value) { - throwShouldNeverCall("put()"); - return null; - } + @Override + public boolean containsValue(Object value) { + throwShouldNeverCall("containsValue()"); + return false; + } - @Override - public Object remove(Object key) { - throwShouldNeverCall("remove()"); - return null; - } + @Override + public Object put(String key, Object value) { + throwShouldNeverCall("put()"); + return null; + } - @Override - public void putAll(Map m) { - throwShouldNeverCall("putAll()"); - } + @Override + public Object remove(Object key) { + throwShouldNeverCall("remove()"); + return null; + } - @Override - public void clear() { - throwShouldNeverCall("clear()"); - } + @Override + public void putAll(Map m) { + throwShouldNeverCall("putAll()"); + } - @Override - public Collection values() { - throwShouldNeverCall("values()"); - return null; - } + @Override + public void clear() { + throwShouldNeverCall("clear()"); + } - @Override - public Set> entrySet() { - throwShouldNeverCall("entrySet()"); - return null; - } + @Override + public Collection values() { + throwShouldNeverCall("values()"); + return null; + } - private void throwShouldNeverCall(String methodName) { - throw new HelenusMappingException(String.format( - "the method {} should never be called on an instance of a Helenus ValueProviderMap", methodName)); - } + @Override + public Set> entrySet() { + return entity + .getOrderedProperties() + .stream() + .map( + p -> { + return new ValueProviderMap.Entry( + p.getPropertyName(), valueProvider.getColumnValue(source, -1, p, immutable)); + }) + .collect(Collectors.toSet()); + } - @Override - public String toString() { - return source.toString(); - } + @Override + public String toString() { + return source.toString(); + } - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || (!o.getClass().isAssignableFrom(Map.class) && getClass() != o.getClass())) - return false; + @Override + public int hashCode() { + int result = source.hashCode(); + result = 31 * result + valueProvider.hashCode(); + result = 31 * result + entity.hashCode(); + result = 31 * result + (immutable ? 1 : 0); + return result; + } - Map that = (Map) o; - if (this.size() != that.size()) - return false; - for (String key : this.keySet()) - if (!this.get(key).equals(that.get(key))) - return false; + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || (!o.getClass().isAssignableFrom(Map.class) && getClass() != o.getClass())) + return false; - return true; - } + Map that = (Map) o; + if (this.size() != that.size()) return false; + for (Map.Entry e : this.entrySet()) + if (!e.getValue().equals(that.get(e.getKey()))) return false; + + return true; + } + + public static class Entry implements Map.Entry { + + private final K key; + private final V value; + + public Entry(K key, V value) { + this.key = key; + this.value = value; + } + + @Override + public K getKey() { + return key; + } + + @Override + public V getValue() { + return value; + } + + @Override + public V setValue(V value) { + throwShouldNeverCall("Entry.setValue()"); + return null; + } + } } diff --git a/src/main/java/net/helenus/support/CqlUtil.java b/src/main/java/net/helenus/support/CqlUtil.java index 7de2dad..5598af4 100644 --- a/src/main/java/net/helenus/support/CqlUtil.java +++ b/src/main/java/net/helenus/support/CqlUtil.java @@ -17,18 +17,17 @@ package net.helenus.support; public final class CqlUtil { - private CqlUtil() { - } + private CqlUtil() {} - public static String forceQuote(String identity) { + public static String forceQuote(String identity) { - if (identity == null) { - return null; - } + if (identity == null) { + return null; + } - if (identity.startsWith("\"")) { - return identity; - } - return "\"" + identity + "\""; - } + if (identity.startsWith("\"")) { + return identity; + } + return "\"" + identity + "\""; + } } diff --git a/src/main/java/net/helenus/support/DslPropertyException.java b/src/main/java/net/helenus/support/DslPropertyException.java index a62a831..91cce07 100644 --- a/src/main/java/net/helenus/support/DslPropertyException.java +++ b/src/main/java/net/helenus/support/DslPropertyException.java @@ -19,16 +19,16 @@ import net.helenus.core.reflect.HelenusPropertyNode; public final class DslPropertyException extends HelenusException { - private static final long serialVersionUID = -2745598205929757758L; + private static final long serialVersionUID = -2745598205929757758L; - private final HelenusPropertyNode propertyNode; + private final HelenusPropertyNode propertyNode; - public DslPropertyException(HelenusPropertyNode propertyNode) { - super("DSL PropertyNode Exception"); - this.propertyNode = propertyNode; - } + public DslPropertyException(HelenusPropertyNode propertyNode) { + super("DSL PropertyNode Exception"); + this.propertyNode = propertyNode; + } - public HelenusPropertyNode getPropertyNode() { - return propertyNode; - } + public HelenusPropertyNode getPropertyNode() { + return propertyNode; + } } diff --git a/src/main/java/net/helenus/support/Either.java b/src/main/java/net/helenus/support/Either.java index ae87cd2..4ff54dc 100644 --- a/src/main/java/net/helenus/support/Either.java +++ b/src/main/java/net/helenus/support/Either.java @@ -19,62 +19,62 @@ import java.util.function.Function; public final class Either { - private final L left; - private final R right; + private final L left; + private final R right; - private Either(L left, R right) { - this.left = left; - this.right = right; - } + private Either(L left, R right) { + this.left = left; + this.right = right; + } - public static Either left(L left) { - return new Either(left, null); - } + public static Either left(L left) { + return new Either(left, null); + } - public static Either right(R right) { - return new Either(null, right); - } + public static Either right(R right) { + return new Either(null, right); + } - public boolean isLeft() { - return left != null; - } + public boolean isLeft() { + return left != null; + } - public L getLeft() { - return left; - } + public L getLeft() { + return left; + } - public boolean isRight() { - return right != null; - } + public boolean isRight() { + return right != null; + } - public R getRight() { - return right; - } + public R getRight() { + return right; + } - public EitherCase getCase() { - if (left != null) { - return EitherCase.LEFT; - } else if (right != null) { - return EitherCase.RIGHT; - } - throw new IllegalStateException("unexpected state"); - } + public EitherCase getCase() { + if (left != null) { + return EitherCase.LEFT; + } else if (right != null) { + return EitherCase.RIGHT; + } + throw new IllegalStateException("unexpected state"); + } - public T fold(Function leftFunction, Function rightFunction) { - switch (getCase()) { - case LEFT : - return leftFunction.apply(left); - case RIGHT : - return rightFunction.apply(right); - } - throw new IllegalStateException("unexpected state"); - } + public T fold(Function leftFunction, Function rightFunction) { + switch (getCase()) { + case LEFT: + return leftFunction.apply(left); + case RIGHT: + return rightFunction.apply(right); + } + throw new IllegalStateException("unexpected state"); + } - @Override - public String toString() { - if (left != null) { - return "[" + left + ",]"; - } - return "[," + right + "]"; - } + @Override + public String toString() { + if (left != null) { + return "[" + left + ",]"; + } + return "[," + right + "]"; + } } diff --git a/src/main/java/net/helenus/support/EitherCase.java b/src/main/java/net/helenus/support/EitherCase.java index 9ca0db1..19db08c 100644 --- a/src/main/java/net/helenus/support/EitherCase.java +++ b/src/main/java/net/helenus/support/EitherCase.java @@ -16,5 +16,6 @@ package net.helenus.support; public enum EitherCase { - LEFT, RIGHT; + LEFT, + RIGHT; } diff --git a/src/main/java/net/helenus/support/Fun.java b/src/main/java/net/helenus/support/Fun.java index aa3c896..722cca4 100644 --- a/src/main/java/net/helenus/support/Fun.java +++ b/src/main/java/net/helenus/support/Fun.java @@ -19,193 +19,219 @@ import java.util.Arrays; public final class Fun { - private Fun() { - } + private Fun() {} - public static final class ArrayTuple { + public static final class ArrayTuple { - public final Object[] _a; + public final Object[] _a; - public ArrayTuple(Object[] a) { - this._a = a; - } + public ArrayTuple(Object[] a) { + this._a = a; + } - public static ArrayTuple of(Object[] a) { - return new ArrayTuple(a); - } + public static ArrayTuple of(Object[] a) { + return new ArrayTuple(a); + } - @Override - public String toString() { - return "ArrayTuple " + Arrays.toString(_a); - } - } + @Override + public String toString() { + return "ArrayTuple " + Arrays.toString(_a); + } + } - public static final class Tuple1 { + public static final class Tuple1 { - public final A _1; + public final A _1; - public Tuple1(A v1) { - this._1 = v1; - } + public Tuple1(A v1) { + this._1 = v1; + } - public static Tuple1 of(A _1) { - return new Tuple1(_1); - } + public static Tuple1 of(A _1) { + return new Tuple1(_1); + } - @Override - public String toString() { - return "Tuple1 [_1=" + _1 + "]"; - } - } + @Override + public String toString() { + return "Tuple1 [_1=" + _1 + "]"; + } + } - public static final class Tuple2 { + public static final class Tuple2 { - public final A _1; - public final B _2; + public final A _1; + public final B _2; - public Tuple2(A v1, B v2) { - this._1 = v1; - this._2 = v2; - } + public Tuple2(A v1, B v2) { + this._1 = v1; + this._2 = v2; + } - public static Tuple2 of(A _1, B _2) { - return new Tuple2(_1, _2); - } + public static Tuple2 of(A _1, B _2) { + return new Tuple2(_1, _2); + } - @Override - public String toString() { - return "Tuple2 [_1=" + _1 + ", _2=" + _2 + "]"; - } - } + @Override + public String toString() { + return "Tuple2 [_1=" + _1 + ", _2=" + _2 + "]"; + } + } - public static final class Tuple3 { + public static final class Tuple3 { - public final A _1; - public final B _2; - public final C _3; + public final A _1; + public final B _2; + public final C _3; - public Tuple3(A v1, B v2, C v3) { - this._1 = v1; - this._2 = v2; - this._3 = v3; - } + public Tuple3(A v1, B v2, C v3) { + this._1 = v1; + this._2 = v2; + this._3 = v3; + } - public static Tuple3 of(A _1, B _2, C _3) { - return new Tuple3(_1, _2, _3); - } + public static Tuple3 of(A _1, B _2, C _3) { + return new Tuple3(_1, _2, _3); + } - @Override - public String toString() { - return "Tuple3 [_1=" + _1 + ", _2=" + _2 + ", _3=" + _3 + "]"; - } - } + @Override + public String toString() { + return "Tuple3 [_1=" + _1 + ", _2=" + _2 + ", _3=" + _3 + "]"; + } + } - public static final class Tuple4 { + public static final class Tuple4 { - public final A _1; - public final B _2; - public final C _3; - public final D _4; + public final A _1; + public final B _2; + public final C _3; + public final D _4; - public Tuple4(A v1, B v2, C v3, D v4) { - this._1 = v1; - this._2 = v2; - this._3 = v3; - this._4 = v4; - } + public Tuple4(A v1, B v2, C v3, D v4) { + this._1 = v1; + this._2 = v2; + this._3 = v3; + this._4 = v4; + } - public static Tuple4 of(A _1, B _2, C _3, D _4) { - return new Tuple4(_1, _2, _3, _4); - } + public static Tuple4 of(A _1, B _2, C _3, D _4) { + return new Tuple4(_1, _2, _3, _4); + } - @Override - public String toString() { - return "Tuple4 [_1=" + _1 + ", _2=" + _2 + ", _3=" + _3 + ", _4=" + _4 + "]"; - } - } + @Override + public String toString() { + return "Tuple4 [_1=" + _1 + ", _2=" + _2 + ", _3=" + _3 + ", _4=" + _4 + "]"; + } + } - public static final class Tuple5 { + public static final class Tuple5 { - public final A _1; - public final B _2; - public final C _3; - public final D _4; - public final E _5; + public final A _1; + public final B _2; + public final C _3; + public final D _4; + public final E _5; - public Tuple5(A v1, B v2, C v3, D v4, E v5) { - this._1 = v1; - this._2 = v2; - this._3 = v3; - this._4 = v4; - this._5 = v5; - } + public Tuple5(A v1, B v2, C v3, D v4, E v5) { + this._1 = v1; + this._2 = v2; + this._3 = v3; + this._4 = v4; + this._5 = v5; + } - public static Tuple5 of(A _1, B _2, C _3, D _4, E _5) { - return new Tuple5(_1, _2, _3, _4, _5); - } + public static Tuple5 of(A _1, B _2, C _3, D _4, E _5) { + return new Tuple5(_1, _2, _3, _4, _5); + } - @Override - public String toString() { - return "Tuple5 [_1=" + _1 + ", _2=" + _2 + ", _3=" + _3 + ", _4=" + _4 + ", _5=" + _5 + "]"; - } - } + @Override + public String toString() { + return "Tuple5 [_1=" + _1 + ", _2=" + _2 + ", _3=" + _3 + ", _4=" + _4 + ", _5=" + _5 + "]"; + } + } - public static final class Tuple6 { + public static final class Tuple6 { - public final A _1; - public final B _2; - public final C _3; - public final D _4; - public final E _5; - public final F _6; + public final A _1; + public final B _2; + public final C _3; + public final D _4; + public final E _5; + public final F _6; - public Tuple6(A v1, B v2, C v3, D v4, E v5, F v6) { - this._1 = v1; - this._2 = v2; - this._3 = v3; - this._4 = v4; - this._5 = v5; - this._6 = v6; - } + public Tuple6(A v1, B v2, C v3, D v4, E v5, F v6) { + this._1 = v1; + this._2 = v2; + this._3 = v3; + this._4 = v4; + this._5 = v5; + this._6 = v6; + } - public static Tuple6 of(A _1, B _2, C _3, D _4, E _5, F _6) { - return new Tuple6(_1, _2, _3, _4, _5, _6); - } + public static Tuple6 of( + A _1, B _2, C _3, D _4, E _5, F _6) { + return new Tuple6(_1, _2, _3, _4, _5, _6); + } - @Override - public String toString() { - return "Tuple6 [_1=" + _1 + ", _2=" + _2 + ", _3=" + _3 + ", _4=" + _4 + ", _5=" + _5 + ", _6=" + _6 + "]"; - } - } + @Override + public String toString() { + return "Tuple6 [_1=" + + _1 + + ", _2=" + + _2 + + ", _3=" + + _3 + + ", _4=" + + _4 + + ", _5=" + + _5 + + ", _6=" + + _6 + + "]"; + } + } - public static final class Tuple7 { + public static final class Tuple7 { - public final A _1; - public final B _2; - public final C _3; - public final D _4; - public final E _5; - public final F _6; - public final G _7; + public final A _1; + public final B _2; + public final C _3; + public final D _4; + public final E _5; + public final F _6; + public final G _7; - public Tuple7(A v1, B v2, C v3, D v4, E v5, F v6, G v7) { - this._1 = v1; - this._2 = v2; - this._3 = v3; - this._4 = v4; - this._5 = v5; - this._6 = v6; - this._7 = v7; - } + public Tuple7(A v1, B v2, C v3, D v4, E v5, F v6, G v7) { + this._1 = v1; + this._2 = v2; + this._3 = v3; + this._4 = v4; + this._5 = v5; + this._6 = v6; + this._7 = v7; + } - public static Tuple7 of(A _1, B _2, C _3, D _4, E _5, F _6, G _7) { - return new Tuple7(_1, _2, _3, _4, _5, _6, _7); - } + public static Tuple7 of( + A _1, B _2, C _3, D _4, E _5, F _6, G _7) { + return new Tuple7(_1, _2, _3, _4, _5, _6, _7); + } - @Override - public String toString() { - return "Tuple7 [_1=" + _1 + ", _2=" + _2 + ", _3=" + _3 + ", _4=" + _4 + ", _5=" + _5 + ", _6=" + _6 - + ", _7=" + _7 + "]"; - } - } + @Override + public String toString() { + return "Tuple7 [_1=" + + _1 + + ", _2=" + + _2 + + ", _3=" + + _3 + + ", _4=" + + _4 + + ", _5=" + + _5 + + ", _6=" + + _6 + + ", _7=" + + _7 + + "]"; + } + } } diff --git a/src/main/java/net/helenus/support/HelenusException.java b/src/main/java/net/helenus/support/HelenusException.java index 2777e8e..9fb613e 100644 --- a/src/main/java/net/helenus/support/HelenusException.java +++ b/src/main/java/net/helenus/support/HelenusException.java @@ -17,17 +17,17 @@ package net.helenus.support; public class HelenusException extends RuntimeException { - private static final long serialVersionUID = 7711799134283942588L; + private static final long serialVersionUID = 7711799134283942588L; - public HelenusException(String msg) { - super(msg); - } + public HelenusException(String msg) { + super(msg); + } - public HelenusException(Throwable t) { - super(t); - } + public HelenusException(Throwable t) { + super(t); + } - public HelenusException(String msg, Throwable t) { - super(msg, t); - } + public HelenusException(String msg, Throwable t) { + super(msg, t); + } } diff --git a/src/main/java/net/helenus/support/HelenusMappingException.java b/src/main/java/net/helenus/support/HelenusMappingException.java index 2284775..92d80e2 100644 --- a/src/main/java/net/helenus/support/HelenusMappingException.java +++ b/src/main/java/net/helenus/support/HelenusMappingException.java @@ -17,17 +17,17 @@ package net.helenus.support; public class HelenusMappingException extends HelenusException { - private static final long serialVersionUID = -4730562130753392363L; + private static final long serialVersionUID = -4730562130753392363L; - public HelenusMappingException(String msg) { - super(msg); - } + public HelenusMappingException(String msg) { + super(msg); + } - public HelenusMappingException(Throwable t) { - super(t); - } + public HelenusMappingException(Throwable t) { + super(t); + } - public HelenusMappingException(String msg, Throwable t) { - super(msg, t); - } + public HelenusMappingException(String msg, Throwable t) { + super(msg, t); + } } diff --git a/src/main/java/net/helenus/support/Immutables.java b/src/main/java/net/helenus/support/Immutables.java index 841d80a..d008b42 100644 --- a/src/main/java/net/helenus/support/Immutables.java +++ b/src/main/java/net/helenus/support/Immutables.java @@ -19,348 +19,349 @@ import java.util.*; public final class Immutables { - private Immutables() { - } - - public static Set setOf(T value) { - return new SingleEntrySet(value); - } - - public static List listOf(T value) { - return new SingleEntryList(value); - } - - public static Map mapOf(K key, V value) { - return new SingleEntryMap(key, value); - } - - static class SingleEntryIterator implements Iterator { - - T entry; - boolean processed = false; - - SingleEntryIterator(T entry) { - this.entry = entry; - } - - @Override - public boolean hasNext() { - return !processed; - } - - @Override - public T next() { - processed = true; - return entry; - } - } - - static class SingleEntryListIterator extends SingleEntryIterator implements ListIterator { - - SingleEntryListIterator(T entry) { - super(entry); - } - - @Override - public boolean hasPrevious() { - return processed; - } - - @Override - public T previous() { - processed = false; - return entry; - } - - @Override - public int nextIndex() { - return processed ? 1 : 0; - } - - @Override - public int previousIndex() { - return processed ? 0 : -1; - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - - @Override - public void set(T e) { - throw new UnsupportedOperationException(); - } - - @Override - public void add(T e) { - throw new UnsupportedOperationException(); - } - } - - static class SingleEntryCollection implements Collection { - - final T entry; - - SingleEntryCollection(T entry) { - this.entry = entry; - } - - @Override - public int size() { - return 1; - } - - @Override - public boolean isEmpty() { - return false; - } - - @Override - public boolean contains(Object o) { - if (entry == null) { - return o == null; - } - return entry.equals(o); - } - - @Override - public Iterator iterator() { - return new SingleEntryIterator(entry); - } - - @Override - public Object[] toArray() { - return new Object[]{entry}; - } - - @Override - public V[] toArray(V[] a) { - if (a.length != 1) { - a = Arrays.copyOf(a, 1); - } - a[0] = (V) entry; - return a; - } - - @Override - public boolean add(T e) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean remove(Object o) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean containsAll(Collection c) { - int size = c.size(); - if (size != 1) { - return false; - } - for (Object o : c) { - return contains(o); - } - return false; - } - - @Override - public boolean addAll(Collection c) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean removeAll(Collection c) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean retainAll(Collection c) { - throw new UnsupportedOperationException(); - } - - @Override - public void clear() { - throw new UnsupportedOperationException(); - } - } - - static final class SingleEntrySet extends SingleEntryCollection implements Set { - - SingleEntrySet(T entry) { - super(entry); - } - } - - static final class SingleEntryList extends SingleEntryCollection implements List { - - SingleEntryList(T entry) { - super(entry); - } - - @Override - public boolean addAll(int index, Collection c) { - throw new UnsupportedOperationException(); - } - - @Override - public T get(int index) { - if (index == 0) { - return entry; - } - throw new IndexOutOfBoundsException(); - } - - @Override - public T set(int index, T element) { - throw new UnsupportedOperationException(); - } - - @Override - public void add(int index, T element) { - throw new UnsupportedOperationException(); - } - - @Override - public T remove(int index) { - throw new UnsupportedOperationException(); - } - - @Override - public int indexOf(Object o) { - return contains(o) ? 0 : -1; - } - - @Override - public int lastIndexOf(Object o) { - return contains(o) ? 0 : -1; - } - - @Override - public ListIterator listIterator() { - return new SingleEntryListIterator(entry); - } - - @Override - public ListIterator listIterator(int index) { - if (index != 0) { - throw new IndexOutOfBoundsException(); - } - return listIterator(); - } - - @Override - public List subList(int fromIndex, int toIndex) { - if (fromIndex == 0) { - if (toIndex == 0) { - return Collections.emptyList(); - } else if (toIndex == 1) { - return this; - } else { - throw new IndexOutOfBoundsException(); - } - } else if (fromIndex == 1 && toIndex == 1) { - return Collections.emptyList(); - } else { - throw new IndexOutOfBoundsException(); - } - } - } - - static final class SingleEntryMap implements Map { - - final K key; - final V value; - - SingleEntryMap(K key, V value) { - this.key = key; - this.value = value; - } - - @Override - public int size() { - return 1; - } - - @Override - public boolean isEmpty() { - return false; - } - - @Override - public boolean containsKey(Object key) { - if (this.key == null) { - return key == null; - } - return this.key.equals(key); - } - - @Override - public boolean containsValue(Object value) { - if (this.value == null) { - return value == null; - } - return this.value.equals(value); - } - - @Override - public V get(Object key) { - if (this.key == null) { - return key == null ? this.value : null; - } - return this.key.equals(key) ? this.value : null; - } - - @Override - public V put(K key, V value) { - throw new UnsupportedOperationException(); - } - - @Override - public V remove(Object key) { - throw new UnsupportedOperationException(); - } - - @Override - public void putAll(Map m) { - throw new UnsupportedOperationException(); - } - - @Override - public void clear() { - throw new UnsupportedOperationException(); - } - - @Override - public Set keySet() { - return new SingleEntrySet(this.key); - } - - @Override - public Collection values() { - return new SingleEntrySet(this.value); - } - - @Override - public Set> entrySet() { - return new SingleEntrySet>(new Map.Entry() { - - @Override - public K getKey() { - return key; - } - - @Override - public V getValue() { - return value; - } - - @Override - public V setValue(V value) { - throw new UnsupportedOperationException(); - } - }); - } - } + private Immutables() {} + + public static Set setOf(T value) { + return new SingleEntrySet(value); + } + + public static List listOf(T value) { + return new SingleEntryList(value); + } + + public static Map mapOf(K key, V value) { + return new SingleEntryMap(key, value); + } + + static class SingleEntryIterator implements Iterator { + + T entry; + boolean processed = false; + + SingleEntryIterator(T entry) { + this.entry = entry; + } + + @Override + public boolean hasNext() { + return !processed; + } + + @Override + public T next() { + processed = true; + return entry; + } + } + + static class SingleEntryListIterator extends SingleEntryIterator + implements ListIterator { + + SingleEntryListIterator(T entry) { + super(entry); + } + + @Override + public boolean hasPrevious() { + return processed; + } + + @Override + public T previous() { + processed = false; + return entry; + } + + @Override + public int nextIndex() { + return processed ? 1 : 0; + } + + @Override + public int previousIndex() { + return processed ? 0 : -1; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + + @Override + public void set(T e) { + throw new UnsupportedOperationException(); + } + + @Override + public void add(T e) { + throw new UnsupportedOperationException(); + } + } + + static class SingleEntryCollection implements Collection { + + final T entry; + + SingleEntryCollection(T entry) { + this.entry = entry; + } + + @Override + public int size() { + return 1; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public boolean contains(Object o) { + if (entry == null) { + return o == null; + } + return entry.equals(o); + } + + @Override + public Iterator iterator() { + return new SingleEntryIterator(entry); + } + + @Override + public Object[] toArray() { + return new Object[] {entry}; + } + + @Override + public V[] toArray(V[] a) { + if (a.length != 1) { + a = Arrays.copyOf(a, 1); + } + a[0] = (V) entry; + return a; + } + + @Override + public boolean add(T e) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean containsAll(Collection c) { + int size = c.size(); + if (size != 1) { + return false; + } + for (Object o : c) { + return contains(o); + } + return false; + } + + @Override + public boolean addAll(Collection c) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean removeAll(Collection c) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean retainAll(Collection c) { + throw new UnsupportedOperationException(); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } + } + + static final class SingleEntrySet extends SingleEntryCollection implements Set { + + SingleEntrySet(T entry) { + super(entry); + } + } + + static final class SingleEntryList extends SingleEntryCollection implements List { + + SingleEntryList(T entry) { + super(entry); + } + + @Override + public boolean addAll(int index, Collection c) { + throw new UnsupportedOperationException(); + } + + @Override + public T get(int index) { + if (index == 0) { + return entry; + } + throw new IndexOutOfBoundsException(); + } + + @Override + public T set(int index, T element) { + throw new UnsupportedOperationException(); + } + + @Override + public void add(int index, T element) { + throw new UnsupportedOperationException(); + } + + @Override + public T remove(int index) { + throw new UnsupportedOperationException(); + } + + @Override + public int indexOf(Object o) { + return contains(o) ? 0 : -1; + } + + @Override + public int lastIndexOf(Object o) { + return contains(o) ? 0 : -1; + } + + @Override + public ListIterator listIterator() { + return new SingleEntryListIterator(entry); + } + + @Override + public ListIterator listIterator(int index) { + if (index != 0) { + throw new IndexOutOfBoundsException(); + } + return listIterator(); + } + + @Override + public List subList(int fromIndex, int toIndex) { + if (fromIndex == 0) { + if (toIndex == 0) { + return Collections.emptyList(); + } else if (toIndex == 1) { + return this; + } else { + throw new IndexOutOfBoundsException(); + } + } else if (fromIndex == 1 && toIndex == 1) { + return Collections.emptyList(); + } else { + throw new IndexOutOfBoundsException(); + } + } + } + + static final class SingleEntryMap implements Map { + + final K key; + final V value; + + SingleEntryMap(K key, V value) { + this.key = key; + this.value = value; + } + + @Override + public int size() { + return 1; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public boolean containsKey(Object key) { + if (this.key == null) { + return key == null; + } + return this.key.equals(key); + } + + @Override + public boolean containsValue(Object value) { + if (this.value == null) { + return value == null; + } + return this.value.equals(value); + } + + @Override + public V get(Object key) { + if (this.key == null) { + return key == null ? this.value : null; + } + return this.key.equals(key) ? this.value : null; + } + + @Override + public V put(K key, V value) { + throw new UnsupportedOperationException(); + } + + @Override + public V remove(Object key) { + throw new UnsupportedOperationException(); + } + + @Override + public void putAll(Map m) { + throw new UnsupportedOperationException(); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } + + @Override + public Set keySet() { + return new SingleEntrySet(this.key); + } + + @Override + public Collection values() { + return new SingleEntrySet(this.value); + } + + @Override + public Set> entrySet() { + return new SingleEntrySet>( + new Map.Entry() { + + @Override + public K getKey() { + return key; + } + + @Override + public V getValue() { + return value; + } + + @Override + public V setValue(V value) { + throw new UnsupportedOperationException(); + } + }); + } + } } diff --git a/src/main/java/net/helenus/support/Mutable.java b/src/main/java/net/helenus/support/Mutable.java index 9a44d99..622b1d3 100644 --- a/src/main/java/net/helenus/support/Mutable.java +++ b/src/main/java/net/helenus/support/Mutable.java @@ -17,17 +17,17 @@ package net.helenus.support; public final class Mutable { - private volatile T value; + private volatile T value; - public Mutable(T initialValue) { - this.value = initialValue; - } + public Mutable(T initialValue) { + this.value = initialValue; + } - public T get() { - return value; - } + public T get() { + return value; + } - public void set(T value) { - this.value = value; - } + public void set(T value) { + this.value = value; + } } diff --git a/src/main/java/net/helenus/support/PackageUtil.java b/src/main/java/net/helenus/support/PackageUtil.java index 1b8fc8a..7f05c2b 100644 --- a/src/main/java/net/helenus/support/PackageUtil.java +++ b/src/main/java/net/helenus/support/PackageUtil.java @@ -25,114 +25,116 @@ import java.util.HashSet; import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarFile; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class PackageUtil { - public static final String JAR_URL_SEPARATOR = "!/"; - private static final Logger log = LoggerFactory.getLogger(PackageUtil.class); + public static final String JAR_URL_SEPARATOR = "!/"; + private static final Logger log = LoggerFactory.getLogger(PackageUtil.class); - private static void doFetchInPath(Set> classes, File directory, String packageName, - ClassLoader classLoader) throws ClassNotFoundException { - File[] dirContents = directory.listFiles(); - if (dirContents == null) { - throw new ClassNotFoundException("invalid directory " + directory.getAbsolutePath()); - } - for (File file : dirContents) { - String fileName = file.getName(); - if (file.isDirectory()) { - doFetchInPath(classes, file, packageName + "." + fileName, classLoader); - } else if (fileName.endsWith(".class")) { - classes.add(classLoader.loadClass(packageName + '.' + fileName.substring(0, fileName.length() - 6))); - } - } - } + private static void doFetchInPath( + Set> classes, File directory, String packageName, ClassLoader classLoader) + throws ClassNotFoundException { + File[] dirContents = directory.listFiles(); + if (dirContents == null) { + throw new ClassNotFoundException("invalid directory " + directory.getAbsolutePath()); + } + for (File file : dirContents) { + String fileName = file.getName(); + if (file.isDirectory()) { + doFetchInPath(classes, file, packageName + "." + fileName, classLoader); + } else if (fileName.endsWith(".class")) { + classes.add( + classLoader.loadClass( + packageName + '.' + fileName.substring(0, fileName.length() - 6))); + } + } + } - public static Set> getClasses(String packagePath) throws ClassNotFoundException, IOException { - Set> classes = new HashSet>(); - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - if (classLoader == null) { - throw new ClassNotFoundException("class loader not found for current thread"); - } - Enumeration resources = null; - try { - resources = classLoader.getResources(packagePath.replace('.', '/')); - } catch (IOException e) { - throw new ClassNotFoundException("invalid package " + packagePath, e); - } - while (resources.hasMoreElements()) { - URL url = resources.nextElement(); - if (url == null) { - throw new ClassNotFoundException(packagePath + " - package not found"); - } - String dirPath = fastReplace(url.getFile(), "%20", " "); - int jarSeparator = dirPath.indexOf(JAR_URL_SEPARATOR); - if (jarSeparator == -1) { - File directory = new File(dirPath); - if (!directory.exists()) { - throw new ClassNotFoundException(packagePath + " - invalid package"); - } - doFetchInPath(classes, directory, packagePath, classLoader); - } else { - String rootEntry = dirPath.substring(jarSeparator + JAR_URL_SEPARATOR.length()); - if (!"".equals(rootEntry) && !rootEntry.endsWith("/")) { - rootEntry = rootEntry + "/"; - } - JarFile jarFile = null; - try { - URLConnection con = url.openConnection(); - if (con instanceof JarURLConnection) { - JarURLConnection jarCon = (JarURLConnection) con; - jarCon.setUseCaches(false); - jarFile = jarCon.getJarFile(); - } else { - String jarName = dirPath.substring(0, jarSeparator); - jarName = fastReplace(jarName, " ", "%20"); - jarFile = new JarFile(jarName); - } - for (Enumeration entries = jarFile.entries(); entries.hasMoreElements();) { - JarEntry entry = entries.nextElement(); - String fileName = entry.getName(); - if (fileName.startsWith(rootEntry) && fileName.endsWith(".class")) { - fileName = fileName.replace('/', '.'); - try { - classes.add(classLoader.loadClass(fileName.substring(0, fileName.length() - 6))); - } catch (ClassNotFoundException e) { - log.error("class load fail", e); - } - } - } - } catch (IOException e) { - throw new ClassNotFoundException("jar fail", e); - } finally { - if (jarFile != null) - jarFile.close(); - } - } - } - return classes; - } + public static Set> getClasses(String packagePath) + throws ClassNotFoundException, IOException { + Set> classes = new HashSet>(); + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + if (classLoader == null) { + throw new ClassNotFoundException("class loader not found for current thread"); + } + Enumeration resources = null; + try { + resources = classLoader.getResources(packagePath.replace('.', '/')); + } catch (IOException e) { + throw new ClassNotFoundException("invalid package " + packagePath, e); + } + while (resources.hasMoreElements()) { + URL url = resources.nextElement(); + if (url == null) { + throw new ClassNotFoundException(packagePath + " - package not found"); + } + String dirPath = fastReplace(url.getFile(), "%20", " "); + int jarSeparator = dirPath.indexOf(JAR_URL_SEPARATOR); + if (jarSeparator == -1) { + File directory = new File(dirPath); + if (!directory.exists()) { + throw new ClassNotFoundException(packagePath + " - invalid package"); + } + doFetchInPath(classes, directory, packagePath, classLoader); + } else { + String rootEntry = dirPath.substring(jarSeparator + JAR_URL_SEPARATOR.length()); + if (!"".equals(rootEntry) && !rootEntry.endsWith("/")) { + rootEntry = rootEntry + "/"; + } + JarFile jarFile = null; + try { + URLConnection con = url.openConnection(); + if (con instanceof JarURLConnection) { + JarURLConnection jarCon = (JarURLConnection) con; + jarCon.setUseCaches(false); + jarFile = jarCon.getJarFile(); + } else { + String jarName = dirPath.substring(0, jarSeparator); + jarName = fastReplace(jarName, " ", "%20"); + jarFile = new JarFile(jarName); + } + for (Enumeration entries = jarFile.entries(); entries.hasMoreElements(); ) { + JarEntry entry = entries.nextElement(); + String fileName = entry.getName(); + if (fileName.startsWith(rootEntry) && fileName.endsWith(".class")) { + fileName = fileName.replace('/', '.'); + try { + classes.add(classLoader.loadClass(fileName.substring(0, fileName.length() - 6))); + } catch (ClassNotFoundException e) { + log.error("class load fail", e); + } + } + } + } catch (IOException e) { + throw new ClassNotFoundException("jar fail", e); + } finally { + if (jarFile != null) jarFile.close(); + } + } + } + return classes; + } - public static String fastReplace(String inString, String oldPattern, String newPattern) { - if (inString == null) { - return null; - } - if (oldPattern == null || newPattern == null) { - return inString; - } - StringBuilder sbuf = new StringBuilder(); - int pos = 0; - int index = inString.indexOf(oldPattern); - int patLen = oldPattern.length(); - while (index >= 0) { - sbuf.append(inString.substring(pos, index)); - sbuf.append(newPattern); - pos = index + patLen; - index = inString.indexOf(oldPattern, pos); - } - sbuf.append(inString.substring(pos)); - return sbuf.toString(); - } + public static String fastReplace(String inString, String oldPattern, String newPattern) { + if (inString == null) { + return null; + } + if (oldPattern == null || newPattern == null) { + return inString; + } + StringBuilder sbuf = new StringBuilder(); + int pos = 0; + int index = inString.indexOf(oldPattern); + int patLen = oldPattern.length(); + while (index >= 0) { + sbuf.append(inString.substring(pos, index)); + sbuf.append(newPattern); + pos = index + patLen; + index = inString.indexOf(oldPattern, pos); + } + sbuf.append(inString.substring(pos)); + return sbuf.toString(); + } } diff --git a/src/main/java/net/helenus/support/Requires.java b/src/main/java/net/helenus/support/Requires.java index cc8e4bf..51543fa 100644 --- a/src/main/java/net/helenus/support/Requires.java +++ b/src/main/java/net/helenus/support/Requires.java @@ -20,14 +20,13 @@ import java.util.Objects; public final class Requires { - private Requires() { - } + private Requires() {} - public static void nonNullArray(T[] arr) { - Objects.requireNonNull(arr, "array is null"); - int len = Array.getLength(arr); - for (int i = 0; i != len; ++i) { - Objects.requireNonNull(Array.get(arr, i), "element " + i + " is empty in array"); - } - } + public static void nonNullArray(T[] arr) { + Objects.requireNonNull(arr, "array is null"); + int len = Array.getLength(arr); + for (int i = 0; i != len; ++i) { + Objects.requireNonNull(Array.get(arr, i), "element " + i + " is empty in array"); + } + } } diff --git a/src/main/java/net/helenus/support/Timeuuid.java b/src/main/java/net/helenus/support/Timeuuid.java index 072f8a7..7649326 100644 --- a/src/main/java/net/helenus/support/Timeuuid.java +++ b/src/main/java/net/helenus/support/Timeuuid.java @@ -21,55 +21,66 @@ import java.util.UUID; public final class Timeuuid { - private Timeuuid() { - } + private Timeuuid() {} - public static UUID of(long timestampMillis, int clockSequence, long node) { - return new UuidBuilder().addVersion(1).addTimestampMillis(timestampMillis).addClockSequence(clockSequence) - .addNode(node).build(); - } + public static UUID of(long timestampMillis, int clockSequence, long node) { + return new UuidBuilder() + .addVersion(1) + .addTimestampMillis(timestampMillis) + .addClockSequence(clockSequence) + .addNode(node) + .build(); + } - public static UUID of(Date date, int clockSequence, long node) { - return of(date.getTime(), clockSequence, node); - } + public static UUID of(Date date, int clockSequence, long node) { + return of(date.getTime(), clockSequence, node); + } - public static UUID of(long timestampMillis) { - return of(timestampMillis, randomClockSequence(), randomNode()); - } + public static UUID of(long timestampMillis) { + return of(timestampMillis, randomClockSequence(), randomNode()); + } - public static UUID of(Date date) { - return of(date.getTime()); - } + public static UUID of(Date date) { + return of(date.getTime()); + } - public static UUID minOf(long timestampMillis) { - return new UuidBuilder().addVersion(1).addTimestampMillis(timestampMillis).setMinClockSeqAndNode().build(); - } + public static UUID minOf(long timestampMillis) { + return new UuidBuilder() + .addVersion(1) + .addTimestampMillis(timestampMillis) + .setMinClockSeqAndNode() + .build(); + } - public static UUID minOf(Date date) { - return minOf(date.getTime()); - } + public static UUID minOf(Date date) { + return minOf(date.getTime()); + } - public static UUID maxOf(long timestampMillis) { - return new UuidBuilder().addVersion(1).addTimestampMillis(timestampMillis).setMaxClockSeqAndNode().build(); - } + public static UUID maxOf(long timestampMillis) { + return new UuidBuilder() + .addVersion(1) + .addTimestampMillis(timestampMillis) + .setMaxClockSeqAndNode() + .build(); + } - public static UUID maxOf(Date date) { - return maxOf(date.getTime()); - } + public static UUID maxOf(Date date) { + return maxOf(date.getTime()); + } - public static int randomClockSequence() { - return Holder.numberGenerator.nextInt(0x3fff); - } + public static int randomClockSequence() { + return Holder.numberGenerator.nextInt(0x3fff); + } - public static long randomNode() { - return Holder.numberGenerator.nextLong() & 0xFFFFFFFFFFFFL; - } + public static long randomNode() { + return Holder.numberGenerator.nextLong() & 0xFFFFFFFFFFFFL; + } - public static long getTimestampMillis(UUID uuid) { - return UuidBuilder.getTimestampMillis(uuid); - } + public static long getTimestampMillis(UUID uuid) { + return UuidBuilder.getTimestampMillis(uuid); + } - private static class Holder { - static final SecureRandom numberGenerator = new SecureRandom(); - } + private static class Holder { + static final SecureRandom numberGenerator = new SecureRandom(); + } } diff --git a/src/main/java/net/helenus/support/Transformers.java b/src/main/java/net/helenus/support/Transformers.java index 2b21ca3..dd62ddf 100644 --- a/src/main/java/net/helenus/support/Transformers.java +++ b/src/main/java/net/helenus/support/Transformers.java @@ -15,68 +15,66 @@ */ package net.helenus.support; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import java.util.*; import java.util.function.Function; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; - public final class Transformers { - private Transformers() { - } + private Transformers() {} - public static Set transformSet(Set inputSet, Function func) { - Set set = Sets.newHashSet(); - for (I in : inputSet) { - set.add(func.apply(in)); - } - return set; - } + public static Set transformSet(Set inputSet, Function func) { + Set set = Sets.newHashSet(); + for (I in : inputSet) { + set.add(func.apply(in)); + } + return set; + } - public static List transformList(List inputList, Function func) { - return new TransformedImmutableList(inputList, func); - } + public static List transformList(List inputList, Function func) { + return new TransformedImmutableList(inputList, func); + } - public static Map transformMapKey(Map inputMap, Function func) { - Map map = Maps.newHashMap(); - for (Map.Entry e : inputMap.entrySet()) { - map.put(func.apply(e.getKey()), e.getValue()); - } - return map; - } + public static Map transformMapKey(Map inputMap, Function func) { + Map map = Maps.newHashMap(); + for (Map.Entry e : inputMap.entrySet()) { + map.put(func.apply(e.getKey()), e.getValue()); + } + return map; + } - public static Map transformMapValue(Map inputMap, Function func) { - return Maps.transformValues(inputMap, func::apply); - } + public static Map transformMapValue(Map inputMap, Function func) { + return Maps.transformValues(inputMap, func::apply); + } - public static Map transformMap(Map inputMap, Function funcKey, - Function funcValue) { - Map map = Maps.newHashMap(); - for (Map.Entry e : inputMap.entrySet()) { - map.put(funcKey.apply(e.getKey()), funcValue.apply(e.getValue())); - } - return map; - } + public static Map transformMap( + Map inputMap, Function funcKey, Function funcValue) { + Map map = Maps.newHashMap(); + for (Map.Entry e : inputMap.entrySet()) { + map.put(funcKey.apply(e.getKey()), funcValue.apply(e.getValue())); + } + return map; + } - static final class TransformedImmutableList extends AbstractList implements List { + static final class TransformedImmutableList extends AbstractList implements List { - final List inputList; - final Function func; + final List inputList; + final Function func; - TransformedImmutableList(List inputList, Function func) { - this.inputList = Objects.requireNonNull(inputList, "inputList is null"); - this.func = Objects.requireNonNull(func, "func is null"); - } + TransformedImmutableList(List inputList, Function func) { + this.inputList = Objects.requireNonNull(inputList, "inputList is null"); + this.func = Objects.requireNonNull(func, "func is null"); + } - @Override - public O get(int index) { - return func.apply(inputList.get(index)); - } + @Override + public O get(int index) { + return func.apply(inputList.get(index)); + } - @Override - public int size() { - return inputList.size(); - } - } + @Override + public int size() { + return inputList.size(); + } + } } diff --git a/src/main/java/net/helenus/support/UuidBuilder.java b/src/main/java/net/helenus/support/UuidBuilder.java index 36fee8a..b59e41c 100644 --- a/src/main/java/net/helenus/support/UuidBuilder.java +++ b/src/main/java/net/helenus/support/UuidBuilder.java @@ -19,76 +19,76 @@ import java.util.UUID; public final class UuidBuilder { - public static final long NUM_100NS_IN_MILLISECOND = 10000L; + public static final long NUM_100NS_IN_MILLISECOND = 10000L; - public static final long NUM_100NS_SINCE_UUID_EPOCH = 0x01b21dd213814000L; + public static final long NUM_100NS_SINCE_UUID_EPOCH = 0x01b21dd213814000L; - private static final long MIN_CLOCK_SEQ_AND_NODE = 0x8080808080808080L; - private static final long MAX_CLOCK_SEQ_AND_NODE = 0x7f7f7f7f7f7f7f7fL; + private static final long MIN_CLOCK_SEQ_AND_NODE = 0x8080808080808080L; + private static final long MAX_CLOCK_SEQ_AND_NODE = 0x7f7f7f7f7f7f7f7fL; - private long leastSigBits = 0x8000000000000000L; + private long leastSigBits = 0x8000000000000000L; - private long mostSigBits = 0L; + private long mostSigBits = 0L; - public static long getTimestampMillis(UUID uuid) { - return (uuid.timestamp() - NUM_100NS_SINCE_UUID_EPOCH) / NUM_100NS_IN_MILLISECOND; - } + public static long getTimestampMillis(UUID uuid) { + return (uuid.timestamp() - NUM_100NS_SINCE_UUID_EPOCH) / NUM_100NS_IN_MILLISECOND; + } - public long getLeastSignificantBits() { - return leastSigBits; - } + public long getLeastSignificantBits() { + return leastSigBits; + } - public long getMostSignificantBits() { - return mostSigBits; - } + public long getMostSignificantBits() { + return mostSigBits; + } - public UUID build() { - return new UUID(mostSigBits, leastSigBits); - } + public UUID build() { + return new UUID(mostSigBits, leastSigBits); + } - public UuidBuilder addVersion(int version) { - if (version < 1 || version > 4) { - throw new IllegalArgumentException("unsupported version " + version); - } + public UuidBuilder addVersion(int version) { + if (version < 1 || version > 4) { + throw new IllegalArgumentException("unsupported version " + version); + } - mostSigBits |= ((long) (version & 0x0f)) << 12; + mostSigBits |= ((long) (version & 0x0f)) << 12; - return this; - } + return this; + } - public UuidBuilder addTimestamp100Nanos(long uuid100Nanos) { + public UuidBuilder addTimestamp100Nanos(long uuid100Nanos) { - long timeLow = uuid100Nanos & 0xffffffffL; - long timeMid = uuid100Nanos & 0xffff00000000L; - long timeHi = uuid100Nanos & 0xfff000000000000L; + long timeLow = uuid100Nanos & 0xffffffffL; + long timeMid = uuid100Nanos & 0xffff00000000L; + long timeHi = uuid100Nanos & 0xfff000000000000L; - mostSigBits |= (timeLow << 32) | (timeMid >> 16) | (timeHi >> 48); + mostSigBits |= (timeLow << 32) | (timeMid >> 16) | (timeHi >> 48); - return this; - } + return this; + } - public UuidBuilder addTimestampMillis(long milliseconds) { - long uuid100Nanos = milliseconds * NUM_100NS_IN_MILLISECOND + NUM_100NS_SINCE_UUID_EPOCH; - return addTimestamp100Nanos(uuid100Nanos); - } + public UuidBuilder addTimestampMillis(long milliseconds) { + long uuid100Nanos = milliseconds * NUM_100NS_IN_MILLISECOND + NUM_100NS_SINCE_UUID_EPOCH; + return addTimestamp100Nanos(uuid100Nanos); + } - public UuidBuilder addClockSequence(int clockSequence) { - leastSigBits |= ((long) (clockSequence & 0x3fff)) << 48; - return this; - } + public UuidBuilder addClockSequence(int clockSequence) { + leastSigBits |= ((long) (clockSequence & 0x3fff)) << 48; + return this; + } - public UuidBuilder addNode(long node) { - leastSigBits |= node & 0xffffffffffffL; - return this; - } + public UuidBuilder addNode(long node) { + leastSigBits |= node & 0xffffffffffffL; + return this; + } - public UuidBuilder setMinClockSeqAndNode() { - this.leastSigBits = MIN_CLOCK_SEQ_AND_NODE; - return this; - } + public UuidBuilder setMinClockSeqAndNode() { + this.leastSigBits = MIN_CLOCK_SEQ_AND_NODE; + return this; + } - public UuidBuilder setMaxClockSeqAndNode() { - this.leastSigBits = MAX_CLOCK_SEQ_AND_NODE; - return this; - } + public UuidBuilder setMaxClockSeqAndNode() { + this.leastSigBits = MAX_CLOCK_SEQ_AND_NODE; + return this; + } } diff --git a/src/test/java/net/helenus/test/integration/build/AbstractEmbeddedCassandraTest.java b/src/test/java/net/helenus/test/integration/build/AbstractEmbeddedCassandraTest.java index b951287..f52bb89 100644 --- a/src/test/java/net/helenus/test/integration/build/AbstractEmbeddedCassandraTest.java +++ b/src/test/java/net/helenus/test/integration/build/AbstractEmbeddedCassandraTest.java @@ -15,83 +15,87 @@ */ 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(); + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/ContextInitTest.java b/src/test/java/net/helenus/test/integration/core/ContextInitTest.java index ee76dc3..0c8f6fd 100644 --- a/src/test/java/net/helenus/test/integration/core/ContextInitTest.java +++ b/src/test/java/net/helenus/test/integration/core/ContextInitTest.java @@ -15,19 +15,18 @@ */ 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); + } } diff --git a/src/test/java/net/helenus/test/integration/core/HelenusValidatorTest.java b/src/test/java/net/helenus/test/integration/core/HelenusValidatorTest.java index dcfe9ba..a089510 100644 --- a/src/test/java/net/helenus/test/integration/core/HelenusValidatorTest.java +++ b/src/test/java/net/helenus/test/integration/core/HelenusValidatorTest.java @@ -1,8 +1,5 @@ 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; @@ -13,40 +10,42 @@ 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 { - HelenusEntity entity; - HelenusProperty prop; + HelenusEntity entity; + HelenusProperty prop; - @Before - public void begin() { - Helenus.init(getSession()).singleton(); + @Before + public void begin() { + Helenus.init(getSession()).singleton(); - entity = Helenus.entity(ModelForValidation.class); + entity = Helenus.entity(ModelForValidation.class); - prop = entity.getProperty("id"); - } + prop = entity.getProperty("id"); + } - @Test(expected = HelenusMappingException.class) - public void testWrongType() { - HelenusValidator.INSTANCE.validate(prop, Integer.valueOf(123)); - } + @Test(expected = HelenusMappingException.class) + public void testWrongType() { + HelenusValidator.INSTANCE.validate(prop, Integer.valueOf(123)); + } - @Test(expected = HelenusException.class) - public void testWrongValue() { - HelenusValidator.INSTANCE.validate(prop, "123"); - } + @Test(expected = HelenusException.class) + public void testWrongValue() { + HelenusValidator.INSTANCE.validate(prop, "123"); + } - public void testOk() { - HelenusValidator.INSTANCE.validate(prop, "a@b.c"); - } + public void testOk() { + HelenusValidator.INSTANCE.validate(prop, "a@b.c"); + } - @Table - interface ModelForValidation { + @Table + interface ModelForValidation { - @Constraints.Email - @PartitionKey - String id(); - } + @Constraints.Email + @PartitionKey + String id(); + } } diff --git a/src/test/java/net/helenus/test/integration/core/collection/CollectionTest.java b/src/test/java/net/helenus/test/integration/core/collection/CollectionTest.java index 50a0cd3..114ff1d 100644 --- a/src/test/java/net/helenus/test/integration/core/collection/CollectionTest.java +++ b/src/test/java/net/helenus/test/integration/core/collection/CollectionTest.java @@ -19,336 +19,395 @@ import static net.helenus.core.Query.*; 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 aliases = new HashSet(); - aliases.add("Alex"); - aliases.add("Albert"); + Set aliases = new HashSet(); + 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.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.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 actualSet = session.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst() - .get()._1; - Assert.assertEquals(aliases, actualSet); + Set actualSet = + session.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1; + Assert.assertEquals(aliases, actualSet); - // UPDATE + // UPDATE - Set expected = new HashSet(); - expected.add("unknown"); + Set expected = new HashSet(); + 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.select(customer).where(customer::id, eq(id)).single().sync().orElse(null); + actual = + session.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 names = new ArrayList(); - names.add("Alex"); - names.add("Albert"); + List names = new ArrayList(); + 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.select(customer).where(customer::id, eq(id)).single().sync().orElse(null); + Customer actual = + session.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 actualList = session.select(customer::names).where(customer::id, eq(id)).sync().findFirst() - .get()._1; - Assert.assertEquals(names, actualList); + List 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 expected = new ArrayList(); - expected.add("unknown"); + List expected = new ArrayList(); + 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.select(customer).where(customer::id, eq(id)).single().sync().orElse(null); + actual = + session.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 props = new HashMap(); - props.put("key1", "value1"); - props.put("key2", "value2"); + Map props = new HashMap(); + 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.select(customer).where(customer::id, eq(id)).single().sync().orElse(null); + Customer actual = + session.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 actualMap = session.select(customer::properties).where(customer::id, eq(id)).sync() - .findFirst().get()._1; - Assert.assertEquals(props, actualMap); + Map 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 expected = new HashMap(); - expected.put("k1", "v1"); - expected.put("k2", "v2"); + Map expected = new HashMap(); + 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.select(customer).where(customer::id, eq(id)).single().sync().orElse(null); - Assert.assertEquals(id, actual.id()); - Assert.assertEquals(expected, actual.properties()); + actual = + session.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); + } } diff --git a/src/test/java/net/helenus/test/integration/core/collection/Customer.java b/src/test/java/net/helenus/test/integration/core/collection/Customer.java index 165538f..c9b18fa 100644 --- a/src/test/java/net/helenus/test/integration/core/collection/Customer.java +++ b/src/test/java/net/helenus/test/integration/core/collection/Customer.java @@ -15,13 +15,11 @@ */ 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; @@ -29,15 +27,15 @@ import net.helenus.mapping.annotation.Types; @Table public interface Customer { - @PartitionKey - UUID id(); + @PartitionKey + UUID id(); - @Types.Set(Name.TEXT) - Set aliases(); + @Types.Set(Name.TEXT) + Set aliases(); - @Types.List(Name.TEXT) - List names(); + @Types.List(Name.TEXT) + List names(); - @Types.Map(key = Name.TEXT, value = Name.TEXT) - Map properties(); + @Types.Map(key = Name.TEXT, value = Name.TEXT) + Map properties(); } diff --git a/src/test/java/net/helenus/test/integration/core/compound/CompondKeyTest.java b/src/test/java/net/helenus/test/integration/core/compound/CompondKeyTest.java index fc4169a..5ded7ca 100644 --- a/src/test/java/net/helenus/test/integration/core/compound/CompondKeyTest.java +++ b/src/test/java/net/helenus/test/integration/core/compound/CompondKeyTest.java @@ -17,89 +17,92 @@ 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; - @Before - public void beforeTest() { - session = Helenus.init(getSession()).showCql().add(Timeline.class).autoCreateDrop().get(); - timeline = Helenus.dsl(Timeline.class, session.getMetadata()); - } + @Before + public void beforeTest() { + session = Helenus.init(getSession()).showCql().add(Timeline.class).autoCreateDrop().get(); + timeline = Helenus.dsl(Timeline.class, session.getMetadata()); + } - @Test - public void test() throws Exception { + @Test + public void test() throws Exception { - UUID userId = UUID.randomUUID(); - long postTime = System.currentTimeMillis() - 100000L; + UUID userId = UUID.randomUUID(); + long postTime = System.currentTimeMillis() - 100000L; - session.showCql(false); + session.showCql(false); - for (int i = 0; i != 100; ++i) { + for (int i = 0; i != 100; ++i) { - TimelineImpl post = new TimelineImpl(); - post.userId = userId; - post.timestamp = new Date(postTime + 1000L * i); - post.text = "hello"; + TimelineImpl post = new TimelineImpl(); + post.userId = userId; + post.timestamp = new Date(postTime + 1000L * i); + post.text = "hello"; - session.upsert(post).sync(); - } + session.upsert(post).sync(); + } - session.showCql(true); + session.showCql(true); - final Mutable d = new Mutable(null); - final Mutable c = new Mutable(0); + final Mutable d = new Mutable(null); + final Mutable c = new Mutable(0); - session.select(timeline::userId, timeline::timestamp, timeline::text) - .where(timeline::userId, Operator.EQ, userId).orderBy(Query.desc(timeline::timestamp)).limit(5).sync() - .forEach(t -> { + session + .select(timeline::userId, timeline::timestamp, timeline::text) + .where(timeline::userId, Operator.EQ, userId) + .orderBy(Query.desc(timeline::timestamp)) + .limit(5) + .sync() + .forEach( + t -> { - // System.out.println(t); - c.set(c.get() + 1); + // System.out.println(t); + c.set(c.get() + 1); - Date cd = d.get(); - if (cd != null) { - Assert.assertTrue(cd.after(t._2)); - } - d.set(t._2); - }); + Date cd = d.get(); + if (cd != null) { + Assert.assertTrue(cd.after(t._2)); + } + d.set(t._2); + }); - Assert.assertEquals(Integer.valueOf(5), c.get()); - } + Assert.assertEquals(Integer.valueOf(5), c.get()); + } - public static class TimelineImpl implements Timeline { + public static class TimelineImpl implements Timeline { - UUID userId; - Date timestamp; - String text; + UUID userId; + Date timestamp; + String text; - @Override - public UUID userId() { - return userId; - } + @Override + public UUID userId() { + return userId; + } - @Override - public Date timestamp() { - return timestamp; - } + @Override + public Date timestamp() { + return timestamp; + } - @Override - public String text() { - return text; - } - } + @Override + public String text() { + return text; + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/compound/Timeline.java b/src/test/java/net/helenus/test/integration/core/compound/Timeline.java index f5e1948..cc5cbb2 100644 --- a/src/test/java/net/helenus/test/integration/core/compound/Timeline.java +++ b/src/test/java/net/helenus/test/integration/core/compound/Timeline.java @@ -17,19 +17,18 @@ package net.helenus.test.integration.core.compound; import java.util.Date; import java.util.UUID; - 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(); } diff --git a/src/test/java/net/helenus/test/integration/core/counter/CounterTest.java b/src/test/java/net/helenus/test/integration/core/counter/CounterTest.java index 2fbd0b7..b1c8c0f 100644 --- a/src/test/java/net/helenus/test/integration/core/counter/CounterTest.java +++ b/src/test/java/net/helenus/test/integration/core/counter/CounterTest.java @@ -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); + } } diff --git a/src/test/java/net/helenus/test/integration/core/counter/Page.java b/src/test/java/net/helenus/test/integration/core/counter/Page.java index 577a53a..bfbb27e 100644 --- a/src/test/java/net/helenus/test/integration/core/counter/Page.java +++ b/src/test/java/net/helenus/test/integration/core/counter/Page.java @@ -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(); } diff --git a/src/test/java/net/helenus/test/integration/core/draft/EntityDraftBuilderTest.java b/src/test/java/net/helenus/test/integration/core/draft/EntityDraftBuilderTest.java index 72c088e..88da2b0 100644 --- a/src/test/java/net/helenus/test/integration/core/draft/EntityDraftBuilderTest.java +++ b/src/test/java/net/helenus/test/integration/core/draft/EntityDraftBuilderTest.java @@ -15,100 +15,116 @@ */ package net.helenus.test.integration.core.draft; +import static net.helenus.core.Query.eq; + import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; 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; - -import static net.helenus.core.Query.eq; - public class EntityDraftBuilderTest extends AbstractEmbeddedCassandraTest { - static Supply supply; - static HelenusSession session; - static Supply.Draft draft = null; + static Supply supply; + static HelenusSession session; + static Supply.Draft draft = null; - @BeforeClass - public static void beforeTest() throws TimeoutException { - session = Helenus.init(getSession()).showCql().add(Supply.class).autoCreateDrop().get(); - supply = session.dsl(Supply.class); + @BeforeClass + public static void beforeTest() throws TimeoutException { + session = Helenus.init(getSession()).showCql().add(Supply.class).autoCreateDrop().get(); + supply = session.dsl(Supply.class); - draft = Supply.draft("APAC").code("WIDGET-002").description("Our second Widget!") - .demand(new HashMap() { - { - put("APAC", 100L); - put("EMEA", 10000L); - put("NORAM", 2000000L); - } - }).shipments(new HashSet() { - { - add("HMS Puddle in transit to APAC, 100 units."); - add("Frigate Jimmy in transit to EMEA, 10000 units."); - } - }).suppliers(new ArrayList() { - { - add("Puddle, Inc."); - add("Jimmy Town, LTD."); - } + draft = + Supply.draft("APAC") + .code("WIDGET-002") + .description("Our second Widget!") + .demand( + new HashMap() { + { + put("APAC", 100L); + put("EMEA", 10000L); + put("NORAM", 2000000L); + } + }) + .shipments( + new HashSet() { + { + add("HMS Puddle in transit to APAC, 100 units."); + add("Frigate Jimmy in transit to EMEA, 10000 units."); + } + }) + .suppliers( + new ArrayList() { + { + add("Puddle, Inc."); + add("Jimmy Town, LTD."); + } }); - Supply s1 = session.insert(draft).sync(); + Supply s1 = session.insert(draft).sync(); + } + + @Test + public void testFoo() throws Exception { + + Supply s1 = + session + .select(Supply.class) + .where(supply::id, eq(draft.id())) + .single() + .sync() + .orElse(null); + + // List + Supply s2 = + session + .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.update(s2.update()).add(supply::shipments, shipment).sync(); + Assert.assertTrue(s3.shipments().contains(shipment)); + + // Map + Supply s4 = session.update(s3.update()).put(supply::demand, "NORAM", 10L).sync(); + Assert.assertEquals((long) s4.demand().get("NORAM"), 10L); + } + + @Test + public void testSerialization() throws Exception { + Supply s1, s2; + + s1 = + session + .select(Supply.class) + .where(supply::id, eq(draft.id())) + .single() + .sync() + .orElse(null); + + byte[] data; + try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutput out = new ObjectOutputStream(bos)) { + out.writeObject(s1); + out.flush(); + data = bos.toByteArray(); } - @Test - public void testFoo() throws Exception { - - Supply s1 = session.select(Supply.class).where(supply::id, eq(draft.id())) - .single() - .sync() - .orElse(null); - - // List - Supply s2 = session.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.update(s2.update()).add(supply::shipments, shipment).sync(); - Assert.assertTrue(s3.shipments().contains(shipment)); - - // Map - Supply s4 = session.update(s3.update()).put(supply::demand, "NORAM", 10L).sync(); - Assert.assertEquals((long) s4.demand().get("NORAM"), 10L); - } - - @Test - public void testSerialization() throws Exception { - Supply s1, s2; - - s1 = session.select(Supply.class).where(supply::id, eq(draft.id())) - .single() - .sync() - .orElse(null); - - byte[] data; - try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); - ObjectOutput out = new ObjectOutputStream(bos)) { - out.writeObject(s1); - out.flush(); - data = bos.toByteArray(); - } - - try (ByteArrayInputStream bis = new ByteArrayInputStream(data); - ObjectInput in = new ObjectInputStream(bis)) { - s2 = (Supply)in.readObject(); - } - - Assert.assertEquals(s2.id(), s1.id()); - Assert.assertEquals(s2, s1); + try (ByteArrayInputStream bis = new ByteArrayInputStream(data); + ObjectInput in = new ObjectInputStream(bis)) { + s2 = (Supply) in.readObject(); } + + Assert.assertEquals(s2.id(), s1.id()); + Assert.assertEquals(s2, s1); + } } diff --git a/src/test/java/net/helenus/test/integration/core/draft/Inventory.java b/src/test/java/net/helenus/test/integration/core/draft/Inventory.java index 57fa020..0689fcb 100644 --- a/src/test/java/net/helenus/test/integration/core/draft/Inventory.java +++ b/src/test/java/net/helenus/test/integration/core/draft/Inventory.java @@ -1,7 +1,6 @@ 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; @@ -10,85 +9,85 @@ import net.helenus.mapping.annotation.*; @Table public interface Inventory { - static Inventory inventory = Helenus.dsl(Inventory.class); + static Inventory inventory = Helenus.dsl(Inventory.class); - @Transient - static Draft draft(UUID id) { - return new Draft(id); - } + @Transient + static Draft draft(UUID id) { + return new Draft(id); + } - @PartitionKey - UUID id(); + @PartitionKey + UUID id(); - @Column("emea") - @Types.Counter - long EMEA(); + @Column("emea") + @Types.Counter + long EMEA(); - @Column("noram") - @Types.Counter - long NORAM(); + @Column("noram") + @Types.Counter + long NORAM(); - @Column("apac") - @Types.Counter - long APAC(); + @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 { + class Draft extends AbstractAuditedEntityDraft { - // 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 getEntityClass() { - return Inventory.class; - } + public Class getEntityClass() { + return Inventory.class; + } - protected String getCurrentAuditor() { - return "unknown"; - } + protected String getCurrentAuditor() { + return "unknown"; + } - // Immutable properties: - public UUID id() { - return this.get(inventory::id, UUID.class); - } + // Immutable properties: + public UUID id() { + return this.get(inventory::id, UUID.class); + } - public long EMEA() { - return this.get(inventory::EMEA, long.class); - } + public long EMEA() { + return this.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.get(inventory::APAC, long.class); - } + public long APAC() { + return this.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.get(inventory::NORAM, long.class); - } + public long NORAM() { + return this.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; + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/draft/Supply.java b/src/test/java/net/helenus/test/integration/core/draft/Supply.java index 2576532..b7134ff 100644 --- a/src/test/java/net/helenus/test/integration/core/draft/Supply.java +++ b/src/test/java/net/helenus/test/integration/core/draft/Supply.java @@ -1,12 +1,10 @@ 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; @@ -15,133 +13,133 @@ import net.helenus.mapping.annotation.*; @Table public interface Supply { - static Supply supply = Helenus.dsl(Supply.class); + static Supply supply = Helenus.dsl(Supply.class); - @Transient - static Draft draft(String region) { - return new Draft(region); - } + @Transient + static Draft draft(String region) { + return new Draft(region); + } - @PartitionKey - UUID id(); + @PartitionKey + UUID id(); - @ClusteringColumn(ordinal = 0) - default String region() { - return "NORAM"; - } + @ClusteringColumn(ordinal = 0) + default String region() { + return "NORAM"; + } - @Index(caseSensitive = false) - String code(); + @Index(caseSensitive = false) + String code(); - @Index - String description(); // @IndexText == lucene index + @Index + String description(); // @IndexText == lucene index - @Index - Map demand(); + @Index + Map demand(); - @Index - List suppliers(); + @Index + List suppliers(); - @Index - Set shipments(); + @Index + Set shipments(); - @Transient - default Draft update() { - return new Draft(this); - } + @Transient + default Draft update() { + return new Draft(this); + } - class Draft extends AbstractEntityDraft { + class Draft extends AbstractEntityDraft { - // 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 getEntityClass() { - return Supply.class; - } + public Class getEntityClass() { + return Supply.class; + } - // Immutable properties: - public UUID id() { - return this.get(supply::id, UUID.class); - } + // Immutable properties: + public UUID id() { + return this.get(supply::id, UUID.class); + } - public String region() { - return this.get(supply::region, String.class); - } + public String region() { + return this.get(supply::region, String.class); + } - // Mutable properties: - public String code() { - return this.get(supply::code, String.class); - } + // Mutable properties: + public String code() { + return this.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.get(supply::description, String.class); - } + public String description() { + return this.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 demand() { - return this.>get(supply::demand, Map.class); - } + public Map demand() { + return this.>get(supply::demand, Map.class); + } - public Draft demand(Map demand) { - mutate(supply::demand, demand); - return this; - } + public Draft demand(Map demand) { + mutate(supply::demand, demand); + return this; + } - public Draft setDemand(Map demand) { - return demand(demand); - } + public Draft setDemand(Map demand) { + return demand(demand); + } - public List suppliers() { - return this.>get(supply::suppliers, List.class); - } + public List suppliers() { + return this.>get(supply::suppliers, List.class); + } - public Draft suppliers(List suppliers) { - mutate(supply::suppliers, suppliers); - return this; - } + public Draft suppliers(List suppliers) { + mutate(supply::suppliers, suppliers); + return this; + } - public Draft setSuppliers(List suppliers) { - return suppliers(suppliers); - } + public Draft setSuppliers(List suppliers) { + return suppliers(suppliers); + } - public Set shipments() { - return this.>get(supply::shipments, Set.class); - } + public Set shipments() { + return this.>get(supply::shipments, Set.class); + } - public Draft shipments(Set shipments) { - mutate(supply::shipments, shipments); - return this; - } + public Draft shipments(Set shipments) { + mutate(supply::shipments, shipments); + return this; + } - public Draft setshipments(Set shipments) { - return shipments(shipments); - } - } + public Draft setshipments(Set shipments) { + return shipments(shipments); + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/hierarchy/Animal.java b/src/test/java/net/helenus/test/integration/core/hierarchy/Animal.java index e5df536..fe6392f 100644 --- a/src/test/java/net/helenus/test/integration/core/hierarchy/Animal.java +++ b/src/test/java/net/helenus/test/integration/core/hierarchy/Animal.java @@ -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; + } } diff --git a/src/test/java/net/helenus/test/integration/core/hierarchy/Cat.java b/src/test/java/net/helenus/test/integration/core/hierarchy/Cat.java index 51ae101..d0089e0 100644 --- a/src/test/java/net/helenus/test/integration/core/hierarchy/Cat.java +++ b/src/test/java/net/helenus/test/integration/core/hierarchy/Cat.java @@ -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(); } diff --git a/src/test/java/net/helenus/test/integration/core/hierarchy/HierarchyTest.java b/src/test/java/net/helenus/test/integration/core/hierarchy/HierarchyTest.java index bad1cab..f5073bf 100644 --- a/src/test/java/net/helenus/test/integration/core/hierarchy/HierarchyTest.java +++ b/src/test/java/net/helenus/test/integration/core/hierarchy/HierarchyTest.java @@ -5,59 +5,73 @@ 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 animal = session.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 animal = + session.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 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 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); + } } diff --git a/src/test/java/net/helenus/test/integration/core/hierarchy/Mammal.java b/src/test/java/net/helenus/test/integration/core/hierarchy/Mammal.java index 53d8475..950287a 100644 --- a/src/test/java/net/helenus/test/integration/core/hierarchy/Mammal.java +++ b/src/test/java/net/helenus/test/integration/core/hierarchy/Mammal.java @@ -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; + } } diff --git a/src/test/java/net/helenus/test/integration/core/hierarchy/Pig.java b/src/test/java/net/helenus/test/integration/core/hierarchy/Pig.java index 45253cf..8d12aff 100644 --- a/src/test/java/net/helenus/test/integration/core/hierarchy/Pig.java +++ b/src/test/java/net/helenus/test/integration/core/hierarchy/Pig.java @@ -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(); } diff --git a/src/test/java/net/helenus/test/integration/core/index/Book.java b/src/test/java/net/helenus/test/integration/core/index/Book.java index 0ae9ab0..1dd86f2 100644 --- a/src/test/java/net/helenus/test/integration/core/index/Book.java +++ b/src/test/java/net/helenus/test/integration/core/index/Book.java @@ -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(); } diff --git a/src/test/java/net/helenus/test/integration/core/index/SecondaryIndexTest.java b/src/test/java/net/helenus/test/integration/core/index/SecondaryIndexTest.java index 19ccdb0..5882d8f 100644 --- a/src/test/java/net/helenus/test/integration/core/index/SecondaryIndexTest.java +++ b/src/test/java/net/helenus/test/integration/core/index/SecondaryIndexTest.java @@ -16,35 +16,39 @@ package net.helenus.test.integration.core.index; import java.util.concurrent.TimeoutException; - -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; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; 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); + } } diff --git a/src/test/java/net/helenus/test/integration/core/prepared/Car.java b/src/test/java/net/helenus/test/integration/core/prepared/Car.java index e13fe6e..c5fa342 100644 --- a/src/test/java/net/helenus/test/integration/core/prepared/Car.java +++ b/src/test/java/net/helenus/test/integration/core/prepared/Car.java @@ -16,7 +16,6 @@ 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; @@ -25,13 +24,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(); } diff --git a/src/test/java/net/helenus/test/integration/core/prepared/PreparedStatementTest.java b/src/test/java/net/helenus/test/integration/core/prepared/PreparedStatementTest.java index 1af35b8..4ea8271 100644 --- a/src/test/java/net/helenus/test/integration/core/prepared/PreparedStatementTest.java +++ b/src/test/java/net/helenus/test/integration/core/prepared/PreparedStatementTest.java @@ -15,14 +15,8 @@ */ package net.helenus.test.integration.core.prepared; -import java.math.BigDecimal; - -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - import com.datastax.driver.core.ResultSet; - +import java.math.BigDecimal; import net.helenus.core.Helenus; import net.helenus.core.HelenusSession; import net.helenus.core.Query; @@ -30,86 +24,115 @@ 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 insertOp; + static PreparedOperation insertOp; - static PreparedOperation updateOp; + static PreparedOperation updateOp; - static PreparedStreamOperation selectOp; + static PreparedStreamOperation selectOp; - static PreparedStreamOperation> selectPriceOp; + static PreparedStreamOperation> selectPriceOp; - static PreparedOperation deleteOp; + static PreparedOperation deleteOp; - static PreparedOperation countOp; + static PreparedOperation 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.insert().value(car::make, Query.marker()).value(car::model, Query.marker()) - .value(car::year, 2004).prepare(); + insertOp = + session + .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.select(car).where(car::make, Query.eq(Query.marker())) - .and(car::model, Query.eq(Query.marker())).prepare(); + selectOp = + session + .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); + } } diff --git a/src/test/java/net/helenus/test/integration/core/simple/InsertPartialTest.java b/src/test/java/net/helenus/test/integration/core/simple/InsertPartialTest.java index 6445186..0ba0d1d 100644 --- a/src/test/java/net/helenus/test/integration/core/simple/InsertPartialTest.java +++ b/src/test/java/net/helenus/test/integration/core/simple/InsertPartialTest.java @@ -18,49 +18,48 @@ package net.helenus.test.integration.core.simple; import java.util.HashMap; import java.util.Map; import java.util.Random; - -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; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; 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 map = new HashMap(); - Long id = rnd.nextLong(); - map.put("id", id); - map.put("age", 5); - InsertOperation insert = session.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 map = new HashMap(); + Long id = rnd.nextLong(); + map.put("id", id); + map.put("age", 5); + InsertOperation insert = session.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 map = new HashMap(); - 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 map = new HashMap(); + 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(); + } } diff --git a/src/test/java/net/helenus/test/integration/core/simple/Message.java b/src/test/java/net/helenus/test/integration/core/simple/Message.java index f7c94c7..7ee3a81 100644 --- a/src/test/java/net/helenus/test/integration/core/simple/Message.java +++ b/src/test/java/net/helenus/test/integration/core/simple/Message.java @@ -16,25 +16,24 @@ package net.helenus.test.integration.core.simple; import java.util.Date; - 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(); } diff --git a/src/test/java/net/helenus/test/integration/core/simple/SimpleUserTest.java b/src/test/java/net/helenus/test/integration/core/simple/SimpleUserTest.java index 27edf73..341f4f7 100644 --- a/src/test/java/net/helenus/test/integration/core/simple/SimpleUserTest.java +++ b/src/test/java/net/helenus/test/integration/core/simple/SimpleUserTest.java @@ -17,173 +17,242 @@ package net.helenus.test.integration.core.simple; import static net.helenus.core.Query.eq; +import com.datastax.driver.core.ResultSet; 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()); + } - @Test - public void testCruid() throws Exception { + @Test + public void testCruid() throws Exception { - UserImpl newUser = new UserImpl(); - newUser.id = 100L; - newUser.name = "alex"; - newUser.age = 34; - newUser.type = UserType.USER; + UserImpl newUser = new UserImpl(); + newUser.id = 100L; + newUser.name = "alex"; + newUser.age = 34; + newUser.type = UserType.USER; - // CREATE + // CREATE - session.upsert(newUser).sync(); + session.upsert(newUser).sync(); - // READ + // READ - // select row and map to entity + // select row and map to entity - User actual = session.selectAll(User.class).mapTo(User.class).where(user::id, eq(100L)).sync().findFirst() - .get(); - assertUsers(newUser, actual); + User actual = + session + .selectAll(User.class) + .mapTo(User.class) + .where(user::id, eq(100L)) + .sync() + .findFirst() + .get(); + assertUsers(newUser, actual); - // select as object + // select as object - actual = session.select(user).where(user::id, eq(100L)).single().sync().orElse(null); - assertUsers(newUser, actual); + actual = session.select(user).where(user::id, eq(100L)).single().sync().orElse(null); + assertUsers(newUser, actual); - // select by columns + // select by columns - 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); + 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 by columns + // select by columns - actual = session.select(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 and mapTo + // select as object and mapTo - 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); + 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 single column + // select single column - String name = session.select(user::name).where(user::id, eq(100L)).sync().findFirst().get()._1; + String name = session.select(user::name).where(user::id, eq(100L)).sync().findFirst().get()._1; - Assert.assertEquals(newUser.name(), name); + Assert.assertEquals(newUser.name(), name); - // select single column in array tuple + // select single column in array tuple - name = (String) session.select().column(user::name).where(user::id, eq(100L)).sync().findFirst().get()._a[0]; + name = + (String) + session + .select() + .column(user::name) + .where(user::id, eq(100L)) + .sync() + .findFirst() + .get() + ._a[0]; - Assert.assertEquals(newUser.name(), name); + Assert.assertEquals(newUser.name(), name); - // UPDATE + // UPDATE - session.update(user::name, "albert").set(user::age, 35).where(user::id, Operator.EQ, 100L).sync(); + session + .update(user::name, "albert") + .set(user::age, 35) + .where(user::id, Operator.EQ, 100L) + .sync(); - long cnt = session.count(user).where(user::id, Operator.EQ, 100L).sync(); - Assert.assertEquals(1L, cnt); + long cnt = session.count(user).where(user::id, Operator.EQ, 100L).sync(); + Assert.assertEquals(1L, cnt); - name = session.select(user::name).where(user::id, Operator.EQ, 100L).map(t -> "_" + t._1).sync().findFirst() - .get(); + name = + session + .select(user::name) + .where(user::id, Operator.EQ, 100L) + .map(t -> "_" + t._1) + .sync() + .findFirst() + .get(); - Assert.assertEquals("_albert", name); + Assert.assertEquals("_albert", name); - User u2 = session.select(user).where(user::id, eq(100L)).single().sync().orElse(null); + User u2 = session.select(user).where(user::id, eq(100L)).single().sync().orElse(null); - Assert.assertEquals(Long.valueOf(100L), u2.id()); - Assert.assertEquals("albert", u2.name()); - Assert.assertEquals(Integer.valueOf(35), u2.age()); + Assert.assertEquals(Long.valueOf(100L), u2.id()); + Assert.assertEquals("albert", u2.name()); + Assert.assertEquals(Integer.valueOf(35), u2.age()); - // - User greg = session.insert(user).value(user::name, "greg").value(user::age, 44) - .value(user::type, UserType.USER).value(user::id, 1234L).sync(); + // + User greg = + session + .insert(user) + .value(user::name, "greg") + .value(user::age, 44) + .value(user::type, UserType.USER) + .value(user::id, 1234L) + .sync(); - Optional maybeGreg = session.select(user).where(user::id, eq(1234L)).single().sync(); + Optional maybeGreg = + session.select(user).where(user::id, eq(1234L)).single().sync(); - // INSERT + // INSERT - session.update().set(user::name, null).set(user::age, null).set(user::type, null).where(user::id, eq(100L)) - .zipkinContext(null).sync(); + session + .update() + .set(user::name, null) + .set(user::age, null) + .set(user::type, null) + .where(user::id, eq(100L)) + .zipkinContext(null) + .sync(); - Fun.Tuple3 tuple = session.select(user::name, user::age, user::type) - .where(user::id, eq(100L)).sync().findFirst().get(); + Fun.Tuple3 tuple = + session + .select(user::name, user::age, user::type) + .where(user::id, eq(100L)) + .sync() + .findFirst() + .get(); - Assert.assertNull(tuple._1); - Assert.assertNull(tuple._2); - Assert.assertNull(tuple._3); + Assert.assertNull(tuple._1); + Assert.assertNull(tuple._2); + Assert.assertNull(tuple._3); - // DELETE + // DELETE - session.delete(user).where(user::id, eq(100L)).sync(); + session.delete(user).where(user::id, eq(100L)).sync(); - cnt = session.select().count().where(user::id, eq(100L)).sync(); - Assert.assertEquals(0L, cnt); - } + cnt = session.select().count().where(user::id, eq(100L)).sync(); + Assert.assertEquals(0L, cnt); + } - 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(); + 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(); - UpdateOperation update = session.update(); - update.set(user::name, null).zipkinContext(null).sync(); - } + UpdateOperation update = session.update(); + update.set(user::name, null).zipkinContext(null).sync(); + } - 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()); - } + 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()); + } - public static class UserImpl implements User { + public static class UserImpl implements User { - Long id; - String name; - Integer age; - UserType type; + Long id; + String name; + Integer age; + UserType type; - @Override - public Long id() { - return id; - } + @Override + public Long id() { + return id; + } - @Override - public String name() { - return name; - } + @Override + public String name() { + return name; + } - @Override - public Integer age() { - return age; - } + @Override + public Integer age() { + return age; + } - @Override - public UserType type() { - return type; - } - } + @Override + public UserType type() { + return type; + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/simple/StaticColumnTest.java b/src/test/java/net/helenus/test/integration/core/simple/StaticColumnTest.java index 4f4f83b..a872d9b 100644 --- a/src/test/java/net/helenus/test/integration/core/simple/StaticColumnTest.java +++ b/src/test/java/net/helenus/test/integration/core/simple/StaticColumnTest.java @@ -19,129 +19,152 @@ import java.util.Date; import java.util.List; import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; - -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; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; 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); + } - @Test - public void testCRUID() throws TimeoutException { + @Test + public void testCRUID() throws TimeoutException { - MessageImpl msg = new MessageImpl(); - msg.id = 123; - msg.timestamp = new Date(); - msg.from = "Alex"; - msg.to = "Bob"; - msg.msg = "hi"; + MessageImpl msg = new MessageImpl(); + msg.id = 123; + msg.timestamp = new Date(); + msg.from = "Alex"; + msg.to = "Bob"; + msg.msg = "hi"; - // CREATE + // CREATE - session.insert(msg).sync(); + session.insert(msg).sync(); - msg.id = 123; - msg.to = "Craig"; + msg.id = 123; + msg.to = "Craig"; - session.insert(msg).sync(); + session.insert(msg).sync(); - // READ + // READ - List actual = session.select(message).where(message::id, Query.eq(123)).sync() - .collect(Collectors.toList()); + List actual = + session + .select(message) + .where(message::id, Query.eq(123)) + .sync() + .collect(Collectors.toList()); - Assert.assertEquals(2, actual.size()); + Assert.assertEquals(2, actual.size()); - Message toCraig = actual.stream().filter(m -> m.to().equals("Craig")).findFirst().get(); - assertMessages(msg, toCraig); + Message toCraig = actual.stream().filter(m -> m.to().equals("Craig")).findFirst().get(); + assertMessages(msg, toCraig); - // UPDATE + // UPDATE - session.update().set(message::from, "Albert").where(message::id, Query.eq(123)) - .onlyIf(message::from, Query.eq("Alex")).sync(); + session + .update() + .set(message::from, "Albert") + .where(message::id, Query.eq(123)) + .onlyIf(message::from, Query.eq("Alex")) + .sync(); - long cnt = session.select(message::from).where(message::id, Query.eq(123)).sync() - .filter(t -> t._1.equals("Albert")).count(); + long cnt = + session + .select(message::from) + .where(message::id, Query.eq(123)) + .sync() + .filter(t -> t._1.equals("Albert")) + .count(); - Assert.assertEquals(2, cnt); + Assert.assertEquals(2, cnt); - // INSERT + // INSERT - session.update().set(message::from, null).where(message::id, Query.eq(123)).sync(); + session.update().set(message::from, null).where(message::id, Query.eq(123)).sync(); - session.select(message::from).where(message::id, Query.eq(123)).sync().map(t -> t._1) - .forEach(Assert::assertNull); + session + .select(message::from) + .where(message::id, Query.eq(123)) + .sync() + .map(t -> t._1) + .forEach(Assert::assertNull); - session.update().set(message::from, "Alex").where(message::id, Query.eq(123)) - .onlyIf(message::from, Query.eq(null)).sync(); + session + .update() + .set(message::from, "Alex") + .where(message::id, Query.eq(123)) + .onlyIf(message::from, Query.eq(null)) + .sync(); - // DELETE + // DELETE - session.delete().where(message::id, Query.eq(123)).sync(); + session.delete().where(message::id, Query.eq(123)).sync(); - cnt = session.count().where(message::id, Query.eq(123)).sync(); - Assert.assertEquals(0, cnt); - } + cnt = session.count().where(message::id, Query.eq(123)).sync(); + Assert.assertEquals(0, cnt); + } - 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()); - } + 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()); + } - private static class MessageImpl implements Message { + private static class MessageImpl implements Message { - int id; - Date timestamp; - String from; - String to; - String msg; + int id; + Date timestamp; + String from; + String to; + String msg; - @Override - public int id() { - return id; - } + @Override + public int id() { + return id; + } - @Override - public Date timestamp() { - return timestamp; - } + @Override + public Date timestamp() { + return timestamp; + } - @Override - public String from() { - return from; - } + @Override + public String from() { + return from; + } - @Override - public String to() { - return to; - } + @Override + public String to() { + return to; + } - @Override - public String message() { - return msg; - } - } + @Override + public String message() { + return msg; + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/simple/User.java b/src/test/java/net/helenus/test/integration/core/simple/User.java index f0d5ad4..9d4ac28 100644 --- a/src/test/java/net/helenus/test/integration/core/simple/User.java +++ b/src/test/java/net/helenus/test/integration/core/simple/User.java @@ -22,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(); } diff --git a/src/test/java/net/helenus/test/integration/core/simple/UserType.java b/src/test/java/net/helenus/test/integration/core/simple/UserType.java index 0a425ce..495c799 100644 --- a/src/test/java/net/helenus/test/integration/core/simple/UserType.java +++ b/src/test/java/net/helenus/test/integration/core/simple/UserType.java @@ -16,5 +16,6 @@ package net.helenus.test.integration.core.simple; public enum UserType { - USER, ADMIN; + USER, + ADMIN; } diff --git a/src/test/java/net/helenus/test/integration/core/simple/Username.java b/src/test/java/net/helenus/test/integration/core/simple/Username.java index 3643e3a..7c884f3 100644 --- a/src/test/java/net/helenus/test/integration/core/simple/Username.java +++ b/src/test/java/net/helenus/test/integration/core/simple/Username.java @@ -16,12 +16,10 @@ package net.helenus.test.integration.core.simple; 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 {} diff --git a/src/test/java/net/helenus/test/integration/core/tuple/Album.java b/src/test/java/net/helenus/test/integration/core/tuple/Album.java index 05f381d..8cc2c43 100644 --- a/src/test/java/net/helenus/test/integration/core/tuple/Album.java +++ b/src/test/java/net/helenus/test/integration/core/tuple/Album.java @@ -17,7 +17,6 @@ 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; @@ -26,12 +25,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(); } diff --git a/src/test/java/net/helenus/test/integration/core/tuple/AlbumInformation.java b/src/test/java/net/helenus/test/integration/core/tuple/AlbumInformation.java index 3b2235c..7211999 100644 --- a/src/test/java/net/helenus/test/integration/core/tuple/AlbumInformation.java +++ b/src/test/java/net/helenus/test/integration/core/tuple/AlbumInformation.java @@ -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(); } diff --git a/src/test/java/net/helenus/test/integration/core/tuple/DslTest.java b/src/test/java/net/helenus/test/integration/core/tuple/DslTest.java index 4a3999a..41667d5 100644 --- a/src/test/java/net/helenus/test/integration/core/tuple/DslTest.java +++ b/src/test/java/net/helenus/test/integration/core/tuple/DslTest.java @@ -15,21 +15,20 @@ */ 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))); + } } diff --git a/src/test/java/net/helenus/test/integration/core/tuple/InnerTupleTest.java b/src/test/java/net/helenus/test/integration/core/tuple/InnerTupleTest.java index 8e23606..d42182a 100644 --- a/src/test/java/net/helenus/test/integration/core/tuple/InnerTupleTest.java +++ b/src/test/java/net/helenus/test/integration/core/tuple/InnerTupleTest.java @@ -16,108 +16,130 @@ package net.helenus.test.integration.core.tuple; import java.util.concurrent.TimeoutException; - -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; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; 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); + } } diff --git a/src/test/java/net/helenus/test/integration/core/tuple/Photo.java b/src/test/java/net/helenus/test/integration/core/tuple/Photo.java index 1bf0215..ffae61c 100644 --- a/src/test/java/net/helenus/test/integration/core/tuple/Photo.java +++ b/src/test/java/net/helenus/test/integration/core/tuple/Photo.java @@ -20,5 +20,5 @@ import net.helenus.mapping.annotation.Tuple; @Tuple public interface Photo { - byte[] blob(); + byte[] blob(); } diff --git a/src/test/java/net/helenus/test/integration/core/tuple/PhotoAlbum.java b/src/test/java/net/helenus/test/integration/core/tuple/PhotoAlbum.java index 1f6d40a..a499e2c 100644 --- a/src/test/java/net/helenus/test/integration/core/tuple/PhotoAlbum.java +++ b/src/test/java/net/helenus/test/integration/core/tuple/PhotoAlbum.java @@ -21,8 +21,8 @@ import net.helenus.mapping.annotation.Table; @Table public interface PhotoAlbum { - @PartitionKey - int id(); + @PartitionKey + int id(); - PhotoFolder folder(); + PhotoFolder folder(); } diff --git a/src/test/java/net/helenus/test/integration/core/tuple/PhotoFolder.java b/src/test/java/net/helenus/test/integration/core/tuple/PhotoFolder.java index 4352a37..2b3a541 100644 --- a/src/test/java/net/helenus/test/integration/core/tuple/PhotoFolder.java +++ b/src/test/java/net/helenus/test/integration/core/tuple/PhotoFolder.java @@ -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(); } diff --git a/src/test/java/net/helenus/test/integration/core/tuple/TupleTest.java b/src/test/java/net/helenus/test/integration/core/tuple/TupleTest.java index 42885e6..b22f069 100644 --- a/src/test/java/net/helenus/test/integration/core/tuple/TupleTest.java +++ b/src/test/java/net/helenus/test/integration/core/tuple/TupleTest.java @@ -17,155 +17,158 @@ 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); + } } diff --git a/src/test/java/net/helenus/test/integration/core/tuplecollection/Author.java b/src/test/java/net/helenus/test/integration/core/tuplecollection/Author.java index c1487e0..8e4af61 100644 --- a/src/test/java/net/helenus/test/integration/core/tuplecollection/Author.java +++ b/src/test/java/net/helenus/test/integration/core/tuplecollection/Author.java @@ -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(); } diff --git a/src/test/java/net/helenus/test/integration/core/tuplecollection/Book.java b/src/test/java/net/helenus/test/integration/core/tuplecollection/Book.java index 53862a3..a243efb 100644 --- a/src/test/java/net/helenus/test/integration/core/tuplecollection/Book.java +++ b/src/test/java/net/helenus/test/integration/core/tuplecollection/Book.java @@ -18,23 +18,22 @@ 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 authors(); + List authors(); - Set reviewers(); + Set reviewers(); - Map contents(); + Map contents(); - Map notes(); + Map notes(); - Map writers(); + Map writers(); } diff --git a/src/test/java/net/helenus/test/integration/core/tuplecollection/Section.java b/src/test/java/net/helenus/test/integration/core/tuplecollection/Section.java index 9581df3..f2edc33 100644 --- a/src/test/java/net/helenus/test/integration/core/tuplecollection/Section.java +++ b/src/test/java/net/helenus/test/integration/core/tuplecollection/Section.java @@ -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(); } diff --git a/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleCollectionTest.java b/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleCollectionTest.java index 628ab79..526cf8b 100644 --- a/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleCollectionTest.java +++ b/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleCollectionTest.java @@ -15,138 +15,124 @@ */ package net.helenus.test.integration.core.tuplecollection; -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; 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 + "]"; + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleKeyMapTest.java b/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleKeyMapTest.java index 41308fc..b5501e3 100644 --- a/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleKeyMapTest.java +++ b/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleKeyMapTest.java @@ -18,123 +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 TupleKeyMapTest extends TupleCollectionTest { - @Test - public void testKeyMapCRUID() throws TimeoutException { + @Test + public void testKeyMapCRUID() throws TimeoutException { - int id = 888; + int id = 888; - Map notes = new HashMap(); - notes.put(new SectionImpl("first", 1), "value1"); - notes.put(new SectionImpl("second", 2), "value2"); + Map notes = new HashMap(); + 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.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.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 actualMap = session.select(book::notes).where(book::id, Query.eq(id)).sync().findFirst() - .get()._1; - assertEqualMaps(notes, actualMap); + Map 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 expected = new HashMap(); - expected.put(new SectionImpl("f", 1), "v1"); - expected.put(new SectionImpl("s", 1), "v2"); + Map expected = new HashMap(); + 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.select(book).where(book::id, Query.eq(id)).sync().findFirst().get(); - Assert.assertEquals(id, actual.id()); - assertEqualMaps(expected, actual.notes()); + actual = session.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 expected, Map actual) { + private void assertEqualMaps(Map expected, Map 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)); + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleListTest.java b/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleListTest.java index 97a92c9..c09685d 100644 --- a/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleListTest.java +++ b/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleListTest.java @@ -18,145 +18,157 @@ 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 authors = new ArrayList(); - authors.add(new AuthorImpl("Alex", "San Jose")); - authors.add(new AuthorImpl("Bob", "San Francisco")); + List authors = new ArrayList(); + 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 actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst() - .get()._1; - assertEqualLists(authors, actualList); + List 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 expected = new ArrayList(); - expected.add(new AuthorImpl("Unknown", "City 17")); + List expected = new ArrayList(); + 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 expected, List actual) { - Assert.assertEquals(expected.size(), actual.size()); + private void assertEqualLists(List expected, List 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()); + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleMapTest.java b/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleMapTest.java index 4e1b381..ef95e1f 100644 --- a/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleMapTest.java +++ b/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleMapTest.java @@ -18,130 +18,140 @@ 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 writers = new HashMap(); - 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 writers = new HashMap(); + 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.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.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 actualMap = session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst() - .get()._1; - assertEqualMaps(writers, actualMap); + Map 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 expected = new HashMap(); - expected.put(new SectionImpl("f", 1), new TupleCollectionTest.AuthorImpl("A", "SJ")); - expected.put(new SectionImpl("s", 1), new TupleCollectionTest.AuthorImpl("B", "SF")); + Map expected = new HashMap(); + 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.select(book).where(book::id, Query.eq(id)).single().sync().orElse(null); + actual = session.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 expected, Map actual) { + private void assertEqualMaps(Map expected, Map 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()); + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleSetTest.java b/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleSetTest.java index 257956a..24c1da5 100644 --- a/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleSetTest.java +++ b/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleSetTest.java @@ -18,100 +18,107 @@ 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 reviewers = new HashSet(); - reviewers.add(new AuthorImpl("Alex", "San Jose")); - reviewers.add(new AuthorImpl("Bob", "San Francisco")); + Set reviewers = new HashSet(); + 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 expected = new HashSet(); - expected.add(new AuthorImpl("Craig", "Los Altos")); + Set expected = new HashSet(); + 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 actualSet = session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst() - .get()._1; - assertEqualSets(expected, actualSet); + Set 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 expected, Set actual) { - Assert.assertEquals(expected.size(), actual.size()); + private void assertEqualSets(Set expected, Set 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()); + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleValueMapTest.java b/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleValueMapTest.java index f1f776e..c0de2a3 100644 --- a/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleValueMapTest.java +++ b/src/test/java/net/helenus/test/integration/core/tuplecollection/TupleValueMapTest.java @@ -18,123 +18,126 @@ 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 contents = new HashMap(); - contents.put(1, new SectionImpl("first", 1)); - contents.put(2, new SectionImpl("second", 2)); + Map contents = new HashMap(); + 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 actualMap = session.select(book::contents).where(book::id, Query.eq(id)).sync() - .findFirst().get()._1; - assertEqualMaps(contents, actualMap); + Map 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 expected = new HashMap(); - expected.put(4, new SectionImpl("4", 4)); - expected.put(5, new SectionImpl("5", 5)); + Map expected = new HashMap(); + 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 expected, Map actual) { + private void assertEqualMaps(Map expected, Map 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()); + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/udtcollection/Author.java b/src/test/java/net/helenus/test/integration/core/udtcollection/Author.java index 7571ba5..b1266af 100644 --- a/src/test/java/net/helenus/test/integration/core/udtcollection/Author.java +++ b/src/test/java/net/helenus/test/integration/core/udtcollection/Author.java @@ -20,7 +20,7 @@ import net.helenus.mapping.annotation.UDT; @UDT public interface Author { - String name(); + String name(); - String city(); + String city(); } diff --git a/src/test/java/net/helenus/test/integration/core/udtcollection/Book.java b/src/test/java/net/helenus/test/integration/core/udtcollection/Book.java index 0e30234..4ad9c7f 100644 --- a/src/test/java/net/helenus/test/integration/core/udtcollection/Book.java +++ b/src/test/java/net/helenus/test/integration/core/udtcollection/Book.java @@ -18,23 +18,22 @@ 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 authors(); + List authors(); - Set reviewers(); + Set reviewers(); - Map contents(); + Map contents(); - Map notes(); + Map notes(); - Map writers(); + Map writers(); } diff --git a/src/test/java/net/helenus/test/integration/core/udtcollection/Section.java b/src/test/java/net/helenus/test/integration/core/udtcollection/Section.java index 394084d..0dba42b 100644 --- a/src/test/java/net/helenus/test/integration/core/udtcollection/Section.java +++ b/src/test/java/net/helenus/test/integration/core/udtcollection/Section.java @@ -20,7 +20,7 @@ import net.helenus.mapping.annotation.UDT; @UDT public interface Section { - String title(); + String title(); - int page(); + int page(); } diff --git a/src/test/java/net/helenus/test/integration/core/udtcollection/UDTCollectionTest.java b/src/test/java/net/helenus/test/integration/core/udtcollection/UDTCollectionTest.java index c199afd..f50e34d 100644 --- a/src/test/java/net/helenus/test/integration/core/udtcollection/UDTCollectionTest.java +++ b/src/test/java/net/helenus/test/integration/core/udtcollection/UDTCollectionTest.java @@ -15,139 +15,125 @@ */ package net.helenus.test.integration.core.udtcollection; -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; 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 + "]"; + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/udtcollection/UDTKeyMapTest.java b/src/test/java/net/helenus/test/integration/core/udtcollection/UDTKeyMapTest.java index 44cd0b6..c6d55e3 100644 --- a/src/test/java/net/helenus/test/integration/core/udtcollection/UDTKeyMapTest.java +++ b/src/test/java/net/helenus/test/integration/core/udtcollection/UDTKeyMapTest.java @@ -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 UDTKeyMapTest extends UDTCollectionTest { - @Test - public void testKeyMapCRUID() throws TimeoutException { + @Test + public void testKeyMapCRUID() throws TimeoutException { - int id = 888; + int id = 888; - Map notes = new HashMap(); - notes.put(new SectionImpl("first", 1), "value1"); - notes.put(new SectionImpl("second", 2), "value2"); + Map notes = new HashMap(); + 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.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.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 actualMap = session.select(book::notes).where(book::id, eq(id)).sync().findFirst() - .get()._1; - assertEqualMaps(notes, actualMap); + Map 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 expected = new HashMap(); - expected.put(new SectionImpl("f", 1), "v1"); - expected.put(new SectionImpl("s", 1), "v2"); + Map expected = new HashMap(); + 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.select(book).where(book::id, eq(id)).sync().findFirst().get(); - Assert.assertEquals(id, actual.id()); - assertEqualMaps(expected, actual.notes()); + actual = session.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 expected, Map actual) { + private void assertEqualMaps(Map expected, Map 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)); + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/udtcollection/UDTListTest.java b/src/test/java/net/helenus/test/integration/core/udtcollection/UDTListTest.java index 48aec46..378d559 100644 --- a/src/test/java/net/helenus/test/integration/core/udtcollection/UDTListTest.java +++ b/src/test/java/net/helenus/test/integration/core/udtcollection/UDTListTest.java @@ -18,145 +18,157 @@ 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 authors = new ArrayList(); - authors.add(new AuthorImpl("Alex", "San Jose")); - authors.add(new AuthorImpl("Bob", "San Francisco")); + List authors = new ArrayList(); + 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).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).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 actualList = session.select(book::authors).where(book::id, Query.eq(id)).sync().findFirst() - .get()._1; - assertEqualLists(authors, actualList); + List 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 expected = new ArrayList(); - expected.add(new AuthorImpl("Unknown", "City 17")); + List expected = new ArrayList(); + 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).where(book::id, Query.eq(id)).sync().findFirst().get(); - Assert.assertEquals(id, actual.id()); - assertEqualLists(expected, actual.authors()); + actual = session.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 expected, List actual) { - Assert.assertEquals(expected.size(), actual.size()); + private void assertEqualLists(List expected, List 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()); + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/udtcollection/UDTMapTest.java b/src/test/java/net/helenus/test/integration/core/udtcollection/UDTMapTest.java index 63a2422..b563202 100644 --- a/src/test/java/net/helenus/test/integration/core/udtcollection/UDTMapTest.java +++ b/src/test/java/net/helenus/test/integration/core/udtcollection/UDTMapTest.java @@ -18,129 +18,136 @@ 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 writers = new HashMap(); - writers.put(new SectionImpl("first", 1), new AuthorImpl("Alex", "San Jose")); - writers.put(new SectionImpl("second", 2), new AuthorImpl("Bob", "San Francisco")); + Map writers = new HashMap(); + 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.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.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 actualMap = session.select(book::writers).where(book::id, Query.eq(id)).sync().findFirst() - .get()._1; - assertEqualMaps(writers, actualMap); + Map 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 expected = new HashMap(); - expected.put(new SectionImpl("f", 1), new AuthorImpl("A", "SJ")); - expected.put(new SectionImpl("s", 1), new AuthorImpl("B", "SF")); + Map expected = new HashMap(); + 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.select(book).where(book::id, Query.eq(id)).sync().findFirst().get(); - Assert.assertEquals(id, actual.id()); - assertEqualMaps(expected, actual.writers()); + actual = session.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 expected, Map actual) { + private void assertEqualMaps(Map expected, Map 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()); + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/udtcollection/UDTSetTest.java b/src/test/java/net/helenus/test/integration/core/udtcollection/UDTSetTest.java index b9f6538..7a23687 100644 --- a/src/test/java/net/helenus/test/integration/core/udtcollection/UDTSetTest.java +++ b/src/test/java/net/helenus/test/integration/core/udtcollection/UDTSetTest.java @@ -18,100 +18,107 @@ 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 reviewers = new HashSet(); - reviewers.add(new AuthorImpl("Alex", "San Jose")); - reviewers.add(new AuthorImpl("Bob", "San Francisco")); + Set reviewers = new HashSet(); + 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 expected = new HashSet(); - expected.add(new AuthorImpl("Craig", "Los Altos")); + Set expected = new HashSet(); + 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 actualSet = session.select(book::reviewers).where(book::id, Query.eq(id)).sync().findFirst() - .get()._1; - assertEqualSets(expected, actualSet); + Set 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 expected, Set actual) { - Assert.assertEquals(expected.size(), actual.size()); + private void assertEqualSets(Set expected, Set 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()); + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/udtcollection/UDTValueMapTest.java b/src/test/java/net/helenus/test/integration/core/udtcollection/UDTValueMapTest.java index a90de36..e1b50f0 100644 --- a/src/test/java/net/helenus/test/integration/core/udtcollection/UDTValueMapTest.java +++ b/src/test/java/net/helenus/test/integration/core/udtcollection/UDTValueMapTest.java @@ -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 UDTValueMapTest extends UDTCollectionTest { - @Test - public void testValueMapCRUID() throws TimeoutException { + @Test + public void testValueMapCRUID() throws TimeoutException { - int id = 999; + int id = 999; - Map contents = new HashMap(); - contents.put(1, new SectionImpl("first", 1)); - contents.put(2, new SectionImpl("second", 2)); + Map contents = new HashMap(); + 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 actualMap = session.select(book::contents).where(book::id, eq(id)).sync().findFirst() - .get()._1; - assertEqualMaps(contents, actualMap); + Map 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 expected = new HashMap(); - expected.put(4, new SectionImpl("4", 4)); - expected.put(5, new SectionImpl("5", 5)); + Map expected = new HashMap(); + 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 expected, Map actual) { + private void assertEqualMaps(Map expected, Map 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()); + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/unitofwork/AndThenOrderTest.java b/src/test/java/net/helenus/test/integration/core/unitofwork/AndThenOrderTest.java index 9a20ad2..2b3ebcd 100644 --- a/src/test/java/net/helenus/test/integration/core/unitofwork/AndThenOrderTest.java +++ b/src/test/java/net/helenus/test/integration/core/unitofwork/AndThenOrderTest.java @@ -18,112 +18,133 @@ package net.helenus.test.integration.core.unitofwork; import java.util.ArrayList; import java.util.Arrays; import java.util.List; - -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; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; 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 q = new ArrayList(5); - UnitOfWork uow1, uow2, uow3, uow4, uow5; + @Test + public void testAndThenOrdering() throws Exception { + List q = new ArrayList(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 q = new ArrayList(5); - UnitOfWork uow1, uow2, uow3, uow4, uow5; + @Test + public void testExceptionWithinAndThen() throws Exception { + List q = new ArrayList(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()); + } } diff --git a/src/test/java/net/helenus/test/integration/core/unitofwork/Directory.java b/src/test/java/net/helenus/test/integration/core/unitofwork/Directory.java index 282a666..0ee79f2 100644 --- a/src/test/java/net/helenus/test/integration/core/unitofwork/Directory.java +++ b/src/test/java/net/helenus/test/integration/core/unitofwork/Directory.java @@ -15,16 +15,14 @@ */ package net.helenus.test.integration.core.unitofwork; -import java.util.Set; - import com.datastax.driver.core.DataType.Name; - +import java.util.Set; import net.helenus.mapping.annotation.Types; import net.helenus.mapping.annotation.UDT; @UDT public interface Directory extends FilesystemNode { - @Types.Set(Name.TIMEUUID) - Set inodes(); + @Types.Set(Name.TIMEUUID) + Set inodes(); } diff --git a/src/test/java/net/helenus/test/integration/core/unitofwork/File.java b/src/test/java/net/helenus/test/integration/core/unitofwork/File.java index 2997650..1ff3e93 100644 --- a/src/test/java/net/helenus/test/integration/core/unitofwork/File.java +++ b/src/test/java/net/helenus/test/integration/core/unitofwork/File.java @@ -21,6 +21,6 @@ import net.helenus.mapping.annotation.UDT; @UDT public interface File extends FilesystemNode { - @Column - byte[] data(); + @Column + byte[] data(); } diff --git a/src/test/java/net/helenus/test/integration/core/unitofwork/FileAttributes.java b/src/test/java/net/helenus/test/integration/core/unitofwork/FileAttributes.java index 934eb32..f2eaccc 100644 --- a/src/test/java/net/helenus/test/integration/core/unitofwork/FileAttributes.java +++ b/src/test/java/net/helenus/test/integration/core/unitofwork/FileAttributes.java @@ -20,5 +20,5 @@ import net.helenus.mapping.annotation.UDT; @UDT public interface FileAttributes { - String owner(); + String owner(); } diff --git a/src/test/java/net/helenus/test/integration/core/unitofwork/FilesystemNode.java b/src/test/java/net/helenus/test/integration/core/unitofwork/FilesystemNode.java index 0fcaea7..c5ebe3d 100644 --- a/src/test/java/net/helenus/test/integration/core/unitofwork/FilesystemNode.java +++ b/src/test/java/net/helenus/test/integration/core/unitofwork/FilesystemNode.java @@ -16,7 +16,6 @@ package net.helenus.test.integration.core.unitofwork; import java.util.UUID; - import net.helenus.mapping.annotation.ClusteringColumn; import net.helenus.mapping.annotation.Column; import net.helenus.mapping.annotation.PartitionKey; @@ -25,12 +24,12 @@ 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(); } diff --git a/src/test/java/net/helenus/test/integration/core/unitofwork/UnitOfWorkTest.java b/src/test/java/net/helenus/test/integration/core/unitofwork/UnitOfWorkTest.java index 2369393..1b5e01f 100644 --- a/src/test/java/net/helenus/test/integration/core/unitofwork/UnitOfWorkTest.java +++ b/src/test/java/net/helenus/test/integration/core/unitofwork/UnitOfWorkTest.java @@ -17,14 +17,9 @@ package net.helenus.test.integration.core.unitofwork; import static net.helenus.core.Query.eq; -import java.util.UUID; - -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - +import com.datastax.driver.core.ConsistencyLevel; import com.datastax.driver.core.utils.UUIDs; - +import java.util.UUID; import net.bytebuddy.utility.RandomString; import net.helenus.core.Helenus; import net.helenus.core.HelenusSession; @@ -35,224 +30,323 @@ 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() + .consistencyLevel(ConsistencyLevel.ONE) + .idempotentQueryExecution(true) + .get(); + widget = session.dsl(Widget.class); + } - @Test - public void testSelectAfterSelect() throws Exception { - Widget w1, w2, w3, w4; - 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. - w1 = session.insert(widget).value(widget::id, key).value(widget::name, RandomString.make(20)).sync(); + // This should inserted Widget, but not cache it. + w1 = + session + .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. - w2 = session.select(widget).where(widget::id, eq(key)).single().sync(uow).orElse(null); + // This should read from the database and return a Widget. + w2 = + session.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.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.select(widget).where(widget::id, eq(key)).single().sync(uow).orElse(null); - uow.commit().andThen(() -> { - Assert.assertEquals(w2, w3); - }); - } - - w4 = session.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(); - - // This should inserted Widget, and not cache it in uow1. - try (UnitOfWork uow1 = session.begin()) { - w1 = session.insert(widget).value(widget::id, key1).value(widget::name, RandomString.make(20)) - .sync(uow1); - - try (UnitOfWork uow2 = session.begin(uow1)) { - - // This should read from uow1's cache and return the same Widget. - w2 = session.select(widget).where(widget::id, eq(key1)).single().sync(uow2).orElse(null); - - Assert.assertEquals(w1, w2); - - w3 = session.insert(widget).value(widget::id, key2).value(widget::name, RandomString.make(20)) - .sync(uow2); - - uow2.commit().andThen(() -> { - Assert.assertEquals(w1, w2); - }); - } - - // This should read from the cache and get the same instance of a Widget. - w4 = session.select(widget).where(widget::id, eq(key2)).single().sync(uow1).orElse(null); - - uow1.commit().andThen(() -> { - Assert.assertEquals(w3, w4); - }); - } - } - - @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.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.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.select(widget).where(widget::name, eq(w1.name())).single().sync(uow).orElse(null); - - uow.commit().andThen(() -> { - Assert.assertEquals(w1, w2); - }); - } - } - - @Test - public void testSelectAfterUpdated() throws Exception { - Widget w1, w2, w3, w4, w5, w6; - UUID key = UUIDs.timeBased(); - - // This should inserted Widget, but not cache it. - w1 = session.insert(widget).value(widget::id, key).value(widget::name, RandomString.make(20)).sync(); - - try (UnitOfWork uow = session.begin()) { - - // This should read from the database and return a Widget. - w2 = session.select(widget).where(widget::id, eq(key)).single() - .sync(uow).orElse(null); - Assert.assertEquals(w1, w2); - - // This should remove the object from the cache. - //TODO(gburd): w3 = session. - session.update(w2) - .set(widget::name, "Bill") - .where(widget::id, eq(key)) - .sync(uow); - - // Fetch from session cache, should have old name. - w4 = session.select(widget).where(widget::id, eq(key)).single() - .sync().orElse(null); - Assert.assertEquals(w4, w2); - Assert.assertEquals(w4.name(), w1.name()); - - // This should skip the cache. - w5 = session.select(widget).where(widget::id, eq(key)).single() - .uncached() - .sync().orElse(null); - - Assert.assertNotEquals(w5, w2); // Not the same instance - Assert.assertTrue(w2.equals(w5)); // But they have the same values - Assert.assertFalse(w5.equals(w2)); // TODO(gburd): should also work - Assert.assertEquals(w5.name(), "Bill"); - - uow.commit().andThen(() -> { - Assert.assertEquals(w1, w2); - }); - } - - // The name changed, this should miss cache and not find anything in the database. - w6 = session.select(widget).where(widget::name, eq(w1.name())).single() - .sync().orElse(null); - Assert.assertTrue(w2.equals(w5)); + uow.commit() + .andThen( + () -> { + Assert.assertEquals(w2, w3); + }); } + w4 = + session + .select(widget) + .where(widget::name, eq(w1.name())) + .single() + .sync() + .orElse(null); + Assert.assertEquals(w4, w1); + } - @Test - public void testSelectAfterDeleted() throws Exception { - Widget w1, w2, w3, w4; - UUID key = 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, but not cache it. - w1 = session.insert(widget).value(widget::id, key).value(widget::name, RandomString.make(20)).sync(); + // This should inserted Widget, and not cache it in uow1. + try (UnitOfWork uow1 = session.begin()) { + w1 = + session + .insert(widget) + .value(widget::id, key1) + .value(widget::name, RandomString.make(20)) + .sync(uow1); - try (UnitOfWork uow = session.begin()) { + try (UnitOfWork uow2 = session.begin(uow1)) { - // This should read from the database and return a Widget. - w2 = session.select(widget).where(widget::id, eq(key)).single() - .sync(uow).orElse(null); + // This should read from uow1's cache and return the same Widget. + w2 = + session + .select(widget) + .where(widget::id, eq(key1)) + .single() + .sync(uow2) + .orElse(null); - // This should remove the object from the cache. - session.delete(widget).where(widget::id, eq(key)) - .sync(uow); + Assert.assertEquals(w1, w2); - // This should fail to read from the cache. - w3 = session.select(widget).where(widget::id, eq(key)).single() - .sync(uow).orElse(null); + w3 = + session + .insert(widget) + .value(widget::id, key2) + .value(widget::name, RandomString.make(20)) + .sync(uow2); - Assert.assertEquals(w3, null); + uow2.commit() + .andThen( + () -> { + Assert.assertEquals(w1, w2); + }); + } - uow.commit().andThen(() -> { + // This should read from the cache and get the same instance of a Widget. + w4 = + session + .select(widget) + .where(widget::id, eq(key2)) + .single() + .sync(uow1) + .orElse(null); + + uow1.commit() + .andThen( + () -> { + Assert.assertEquals(w3, w4); + }); + } + } + + @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 + .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.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 + .select(widget) + .where(widget::name, eq(w1.name())) + .single() + .sync(uow) + .orElse(null); + + uow.commit() + .andThen( + () -> { + Assert.assertEquals(w1, w2); + }); + } + } + + @Test + public void testSelectAfterUpdated() throws Exception { + Widget w1, w2, w3, w4, w5, w6; + UUID key = UUIDs.timeBased(); + + // This should inserted Widget, but not cache it. + w1 = + session + .insert(widget) + .value(widget::id, key) + .value(widget::name, RandomString.make(20)) + .sync(); + + try (UnitOfWork uow = session.begin()) { + + // This should read from the database and return a Widget. + w2 = + session.select(widget).where(widget::id, eq(key)).single().sync(uow).orElse(null); + Assert.assertEquals(w1, w2); + + // This should remove the object from the session cache. + w3 = + session.update(w2).set(widget::name, "Bill").where(widget::id, eq(key)).sync(uow); + + // Fetch from session cache, should have old name. + w4 = session.select(widget).where(widget::id, eq(key)).single().sync().orElse(null); + Assert.assertEquals(w4, w2); + Assert.assertEquals(w4.name(), w1.name()); + + // This should skip the cache. + w5 = + session + .select(widget) + .where(widget::id, eq(key)) + .single() + .uncached() + .sync() + .orElse(null); + + Assert.assertNotEquals(w5, w2); // Not the same instance, + Assert.assertTrue(w2.equals(w5)); // but they have the same values, + Assert.assertFalse(w5.equals(w2)); // regardless of the order when comparing. + Assert.assertEquals(w5.name(), "Bill"); + + uow.commit() + .andThen( + () -> { + Assert.assertEquals(w1, w2); + }); + } + + // The name changed, this should miss cache and not find anything in the database. + w6 = + session + .select(widget) + .where(widget::name, eq(w1.name())) + .single() + .sync() + .orElse(null); + Assert.assertTrue(w2.equals(w5)); + } + + @Test + public void testSelectAfterDeleted() throws Exception { + Widget w1, w2, w3, w4; + UUID key = UUIDs.timeBased(); + + // This should inserted Widget, but not cache it. + w1 = + session + .insert(widget) + .value(widget::id, key) + .value(widget::name, RandomString.make(20)) + .sync(); + + try (UnitOfWork uow = session.begin()) { + + // This should read from the database and return a Widget. + w2 = + session.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); + + // This should fail to read from the cache. + w3 = + session.select(widget).where(widget::id, eq(key)).single().sync(uow).orElse(null); + + Assert.assertEquals(w3, null); + + uow.commit() + .andThen( + () -> { Assert.assertEquals(w1, w2); Assert.assertEquals(w3, null); - }); - } - - w4 = session.select(widget).where(widget::name, eq(w1.name())).single() - .sync().orElse(null); - - 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.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.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.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.select(widget) .where(widget::id, - * eq(key)) .uncached() .single() .sync() .orElse(null); - * - * Assert.assertNotEquals(w1, w4); Assert.assertTrue(w1.equals(w4)); } - */ + w4 = + session + .select(widget) + .where(widget::name, eq(w1.name())) + .single() + .sync() + .orElse(null); + + Assert.assertEquals(w4, null); + } + /* + @Test + public void testInsertNoOp() throws Exception { + Widget w1, w2; + UUID key = UUIDs.timeBased(); + + + try (UnitOfWork uow = session.begin()) { + // This should inserted Widget, but not cache it. + w1 = session.insert(widget).value(widget::id, key).value(widget::name, RandomString.make(20)).sync(uow); + w2 = session.insert(w1).value(widget::id, key).sync(uow); + } + Assert.assertEquals(w1, w2); + } + */ + /* + * @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.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.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.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.select(widget) .where(widget::id, + * eq(key)) .uncached() .single() .sync() .orElse(null); + * + * Assert.assertNotEquals(w1, w4); Assert.assertTrue(w1.equals(w4)); } + */ } diff --git a/src/test/java/net/helenus/test/integration/core/usertype/Account.java b/src/test/java/net/helenus/test/integration/core/usertype/Account.java index ec26e2d..27d0f2e 100644 --- a/src/test/java/net/helenus/test/integration/core/usertype/Account.java +++ b/src/test/java/net/helenus/test/integration/core/usertype/Account.java @@ -16,7 +16,6 @@ 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; @@ -25,13 +24,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(); } diff --git a/src/test/java/net/helenus/test/integration/core/usertype/Address.java b/src/test/java/net/helenus/test/integration/core/usertype/Address.java index 0e6e965..0d00876 100644 --- a/src/test/java/net/helenus/test/integration/core/usertype/Address.java +++ b/src/test/java/net/helenus/test/integration/core/usertype/Address.java @@ -15,10 +15,8 @@ */ package net.helenus.test.integration.core.usertype; -import java.util.Set; - import com.datastax.driver.core.DataType; - +import java.util.Set; import net.helenus.mapping.annotation.Column; import net.helenus.mapping.annotation.Types; import net.helenus.mapping.annotation.UDT; @@ -26,19 +24,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 phones(); + @Column + @Types.Set(DataType.Name.TEXT) + Set phones(); } diff --git a/src/test/java/net/helenus/test/integration/core/usertype/AddressInformation.java b/src/test/java/net/helenus/test/integration/core/usertype/AddressInformation.java index 82f5cf1..5c8c9ee 100644 --- a/src/test/java/net/helenus/test/integration/core/usertype/AddressInformation.java +++ b/src/test/java/net/helenus/test/integration/core/usertype/AddressInformation.java @@ -6,6 +6,6 @@ import net.helenus.mapping.annotation.UDT; @UDT public interface AddressInformation { - @Column - Address address(); + @Column + Address address(); } diff --git a/src/test/java/net/helenus/test/integration/core/usertype/Customer.java b/src/test/java/net/helenus/test/integration/core/usertype/Customer.java index a5afdf0..9290d0a 100644 --- a/src/test/java/net/helenus/test/integration/core/usertype/Customer.java +++ b/src/test/java/net/helenus/test/integration/core/usertype/Customer.java @@ -1,7 +1,6 @@ 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; @@ -9,9 +8,9 @@ import net.helenus.mapping.annotation.Table; @Table public interface Customer { - @PartitionKey - UUID id(); + @PartitionKey + UUID id(); - @Column - AddressInformation addressInformation(); + @Column + AddressInformation addressInformation(); } diff --git a/src/test/java/net/helenus/test/integration/core/usertype/InnerUserDefinedTypeTest.java b/src/test/java/net/helenus/test/integration/core/usertype/InnerUserDefinedTypeTest.java index d5496b3..dd35e7a 100644 --- a/src/test/java/net/helenus/test/integration/core/usertype/InnerUserDefinedTypeTest.java +++ b/src/test/java/net/helenus/test/integration/core/usertype/InnerUserDefinedTypeTest.java @@ -15,111 +15,120 @@ */ 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 phones() { - return Sets.newHashSet("14080000000"); - } - }; + @Override + public Set 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); + } } diff --git a/src/test/java/net/helenus/test/integration/core/usertype/UserDefinedTypeTest.java b/src/test/java/net/helenus/test/integration/core/usertype/UserDefinedTypeTest.java index 8c8ffaa..25906ea 100644 --- a/src/test/java/net/helenus/test/integration/core/usertype/UserDefinedTypeTest.java +++ b/src/test/java/net/helenus/test/integration/core/usertype/UserDefinedTypeTest.java @@ -15,204 +15,242 @@ */ package net.helenus.test.integration.core.usertype; -import java.util.Set; -import java.util.concurrent.TimeoutException; - -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 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; public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest { - static Address address; - static Account account; + static Address address; + static Account account; - static HelenusSession session; + static HelenusSession session; - @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); - } + @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); + } - @Test - public void testPrint() { - System.out.println(address); - System.out.println(account); - } + @Test + public void testPrint() { + System.out.println(address); + System.out.println(account); + } - @Test - public void testMappingCRUID() throws TimeoutException { + @Test + public void testMappingCRUID() throws TimeoutException { - AddressImpl addr = new AddressImpl(); - addr.street = "1 st"; - addr.city = "San Jose"; + AddressImpl addr = new AddressImpl(); + addr.street = "1 st"; + addr.city = "San Jose"; - AccountImpl acc = new AccountImpl(); - acc.id = 123L; - acc.address = addr; + AccountImpl acc = new AccountImpl(); + acc.id = 123L; + acc.address = addr; - // CREATE + // CREATE - session.upsert(acc).sync(); + session.upsert(acc).sync(); - // READ + // READ - String streetName = session.select(account.address()::street).where(account::id, Query.eq(123L)).sync() - .findFirst().get()._1; + String streetName = + session + .select(account.address()::street) + .where(account::id, Query.eq(123L)) + .sync() + .findFirst() + .get() + ._1; - Assert.assertEquals("1 st", streetName); + Assert.assertEquals("1 st", streetName); - // UPDATE + // UPDATE - AddressImpl expected = new AddressImpl(); - expected.street = "2 st"; - expected.city = "San Francisco"; + AddressImpl expected = new AddressImpl(); + expected.street = "2 st"; + expected.city = "San Francisco"; - session.update().set(account::address, expected).where(account::id, Query.eq(123L)).sync(); + session.update().set(account::address, expected).where(account::id, Query.eq(123L)).sync(); - Address actual = session.select(account::address).where(account::id, Query.eq(123L)).sync().findFirst() - .get()._1; + Address actual = + session + .select(account::address) + .where(account::id, Query.eq(123L)) + .sync() + .findFirst() + .get() + ._1; - Assert.assertEquals(expected.street(), actual.street()); - Assert.assertEquals(expected.city(), actual.city()); - Assert.assertNull(actual.country()); - Assert.assertEquals(0, actual.zip()); + Assert.assertEquals(expected.street(), actual.street()); + Assert.assertEquals(expected.city(), actual.city()); + Assert.assertNull(actual.country()); + Assert.assertEquals(0, actual.zip()); - // INSERT using UPDATE - session.update().set(account::address, null).where(account::id, Query.eq(123L)).sync(); + // INSERT using UPDATE + session.update().set(account::address, null).where(account::id, Query.eq(123L)).sync(); - Address adrNull = session.select(account::address).where(account::id, Query.eq(123L)).sync().findFirst() - .get()._1; - Assert.assertNull(adrNull); + Address adrNull = + session + .select(account::address) + .where(account::id, Query.eq(123L)) + .sync() + .findFirst() + .get() + ._1; + Assert.assertNull(adrNull); - // DELETE + // DELETE - session.delete().where(account::id, Query.eq(123L)).sync(); + session.delete().where(account::id, Query.eq(123L)).sync(); - Long cnt = session.count().where(account::id, Query.eq(123L)).sync(); - Assert.assertEquals(Long.valueOf(0), cnt); - } + Long cnt = session.count().where(account::id, Query.eq(123L)).sync(); + Assert.assertEquals(Long.valueOf(0), cnt); + } - @Test - public void testNoMapping() throws TimeoutException { + @Test + public void testNoMapping() throws TimeoutException { - String ks = getSession().getLoggedKeyspace(); - UserType addressType = getSession().getCluster().getMetadata().getKeyspace(ks).getUserType("address"); + String ks = getSession().getLoggedKeyspace(); + UserType addressType = + getSession().getCluster().getMetadata().getKeyspace(ks).getUserType("address"); - UDTValue addressNoMapping = addressType.newValue(); - addressNoMapping.setString("line_1", "1st street"); - addressNoMapping.setString("city", "San Jose"); + UDTValue addressNoMapping = addressType.newValue(); + addressNoMapping.setString("line_1", "1st street"); + addressNoMapping.setString("city", "San Jose"); - AccountImpl acc = new AccountImpl(); - acc.id = 777L; - acc.addressNoMapping = addressNoMapping; + AccountImpl acc = new AccountImpl(); + acc.id = 777L; + acc.addressNoMapping = addressNoMapping; - // CREATE + // CREATE - session.upsert(acc).sync(); + session.upsert(acc).sync(); - // READ + // READ - UDTValue found = session.select(account::addressNoMapping).where(account::id, Query.eq(777L)).sync().findFirst() - .get()._1; + UDTValue found = + session + .select(account::addressNoMapping) + .where(account::id, Query.eq(777L)) + .sync() + .findFirst() + .get() + ._1; - Assert.assertEquals(addressNoMapping.getType(), found.getType()); - Assert.assertEquals(addressNoMapping.getString("line_1"), found.getString("line_1")); - Assert.assertEquals(addressNoMapping.getString("city"), found.getString("city")); + Assert.assertEquals(addressNoMapping.getType(), found.getType()); + Assert.assertEquals(addressNoMapping.getString("line_1"), found.getString("line_1")); + Assert.assertEquals(addressNoMapping.getString("city"), found.getString("city")); - // UPDATE + // UPDATE - addressNoMapping = addressType.newValue(); - addressNoMapping.setString("line_1", "Market street"); - addressNoMapping.setString("city", "San Francisco"); + addressNoMapping = addressType.newValue(); + addressNoMapping.setString("line_1", "Market street"); + addressNoMapping.setString("city", "San Francisco"); - session.update().set(account::addressNoMapping, addressNoMapping).where(account::id, Query.eq(777L)).sync(); + session + .update() + .set(account::addressNoMapping, addressNoMapping) + .where(account::id, Query.eq(777L)) + .sync(); - found = session.select(account::addressNoMapping).where(account::id, Query.eq(777L)).sync().findFirst() - .get()._1; + found = + session + .select(account::addressNoMapping) + .where(account::id, Query.eq(777L)) + .sync() + .findFirst() + .get() + ._1; - Assert.assertEquals(addressNoMapping.getType(), found.getType()); - Assert.assertEquals(addressNoMapping.getString("line_1"), found.getString("line_1")); - Assert.assertEquals(addressNoMapping.getString("city"), found.getString("city")); + Assert.assertEquals(addressNoMapping.getType(), found.getType()); + Assert.assertEquals(addressNoMapping.getString("line_1"), found.getString("line_1")); + Assert.assertEquals(addressNoMapping.getString("city"), found.getString("city")); - // INSERT using UPDATE - session.update().set(account::addressNoMapping, null).where(account::id, Query.eq(777L)).sync(); + // INSERT using UPDATE + session.update().set(account::addressNoMapping, null).where(account::id, Query.eq(777L)).sync(); - found = session.select(account::addressNoMapping).where(account::id, Query.eq(777L)).sync().findFirst() - .get()._1; - Assert.assertNull(found); + found = + session + .select(account::addressNoMapping) + .where(account::id, Query.eq(777L)) + .sync() + .findFirst() + .get() + ._1; + Assert.assertNull(found); - // DELETE + // DELETE - session.delete().where(account::id, Query.eq(777L)).sync(); + session.delete().where(account::id, Query.eq(777L)).sync(); - Long cnt = session.count().where(account::id, Query.eq(777L)).sync(); - Assert.assertEquals(Long.valueOf(0), cnt); - } + Long cnt = session.count().where(account::id, Query.eq(777L)).sync(); + Assert.assertEquals(Long.valueOf(0), cnt); + } - public static class AccountImpl implements Account { + public static class AccountImpl implements Account { - long id; - Address address; - UDTValue addressNoMapping; + long id; + Address address; + UDTValue addressNoMapping; - @Override - public long id() { - return id; - } + @Override + public long id() { + return id; + } - @Override - public Address address() { - return address; - } + @Override + public Address address() { + return address; + } - @Override - public UDTValue addressNoMapping() { - return addressNoMapping; - } - } + @Override + public UDTValue addressNoMapping() { + return addressNoMapping; + } + } - public static class AddressImpl implements Address { + public static class AddressImpl implements Address { - String street; - String city; - int zip; - String country; - Set phones; + String street; + String city; + int zip; + String country; + Set phones; - @Override - public String street() { - return street; - } + @Override + public String street() { + return street; + } - @Override - public String city() { - return city; - } + @Override + public String city() { + return city; + } - @Override - public int zip() { - return zip; - } + @Override + public int zip() { + return zip; + } - @Override - public String country() { - return country; - } + @Override + public String country() { + return country; + } - @Override - public Set phones() { - return phones; - } - } + @Override + public Set phones() { + return phones; + } + } } diff --git a/src/test/java/net/helenus/test/integration/core/views/Cyclist.java b/src/test/java/net/helenus/test/integration/core/views/Cyclist.java index 2829a0c..0ab5a73 100644 --- a/src/test/java/net/helenus/test/integration/core/views/Cyclist.java +++ b/src/test/java/net/helenus/test/integration/core/views/Cyclist.java @@ -2,25 +2,28 @@ 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(); } diff --git a/src/test/java/net/helenus/test/integration/core/views/CyclistsByAge.java b/src/test/java/net/helenus/test/integration/core/views/CyclistsByAge.java index 0ce45bb..67e94c6 100644 --- a/src/test/java/net/helenus/test/integration/core/views/CyclistsByAge.java +++ b/src/test/java/net/helenus/test/integration/core/views/CyclistsByAge.java @@ -2,7 +2,6 @@ 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.ClusteringColumn; import net.helenus.mapping.annotation.Index; @@ -11,14 +10,14 @@ 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(); } diff --git a/src/test/java/net/helenus/test/integration/core/views/MaterializedViewTest.java b/src/test/java/net/helenus/test/integration/core/views/MaterializedViewTest.java index 8d4a431..cfd3b7f 100644 --- a/src/test/java/net/helenus/test/integration/core/views/MaterializedViewTest.java +++ b/src/test/java/net/helenus/test/integration/core/views/MaterializedViewTest.java @@ -22,13 +22,11 @@ import java.text.SimpleDateFormat; 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 @@ -36,35 +34,50 @@ import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest; // 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)) + .allowFiltering() + .sync(); + } } diff --git a/src/test/java/net/helenus/test/performance/core/dsl/CachedElevatorImpl.java b/src/test/java/net/helenus/test/performance/core/dsl/CachedElevatorImpl.java index 0167259..ed797f7 100644 --- a/src/test/java/net/helenus/test/performance/core/dsl/CachedElevatorImpl.java +++ b/src/test/java/net/helenus/test/performance/core/dsl/CachedElevatorImpl.java @@ -16,75 +16,74 @@ 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 backingMap; + private final Map 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 backingMap) { - this.backingMap = backingMap; - } + public CachedElevatorImpl(Map 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 toMap() { - return backingMap; - } + @Override + public Map toMap() { + return backingMap; + } - @Override - public String toString() { - return backingMap.toString(); - } + @Override + public String toString() { + return backingMap.toString(); + } } diff --git a/src/test/java/net/helenus/test/performance/core/dsl/Elevator.java b/src/test/java/net/helenus/test/performance/core/dsl/Elevator.java index bb91c59..26707cd 100644 --- a/src/test/java/net/helenus/test/performance/core/dsl/Elevator.java +++ b/src/test/java/net/helenus/test/performance/core/dsl/Elevator.java @@ -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(); } diff --git a/src/test/java/net/helenus/test/performance/core/dsl/ElevatorImpl.java b/src/test/java/net/helenus/test/performance/core/dsl/ElevatorImpl.java index 0e86dc9..4eaa3d7 100644 --- a/src/test/java/net/helenus/test/performance/core/dsl/ElevatorImpl.java +++ b/src/test/java/net/helenus/test/performance/core/dsl/ElevatorImpl.java @@ -16,51 +16,50 @@ 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 backingMap; + private final Map backingMap; - public ElevatorImpl(Map backingMap) { - this.backingMap = backingMap; - } + public ElevatorImpl(Map 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 toMap() { - return backingMap; - } + @Override + public Map toMap() { + return backingMap; + } - @Override - public String toString() { - return backingMap.toString(); - } + @Override + public String toString() { + return backingMap.toString(); + } } diff --git a/src/test/java/net/helenus/test/performance/core/dsl/MappingTest.java b/src/test/java/net/helenus/test/performance/core/dsl/MappingTest.java index 5e6bd7d..0f58c97 100644 --- a/src/test/java/net/helenus/test/performance/core/dsl/MappingTest.java +++ b/src/test/java/net/helenus/test/performance/core/dsl/MappingTest.java @@ -17,101 +17,99 @@ package net.helenus.test.performance.core.dsl; import java.util.HashMap; import java.util.Map; - -import org.junit.Test; - import net.helenus.core.Helenus; +import org.junit.Test; public class MappingTest { - static Map fixture; + static Map fixture; - static { - fixture = new HashMap(); - fixture.put("height", Integer.valueOf(55)); - fixture.put("price", Double.valueOf(44.99)); - fixture.put("name", "first"); - } + static { + fixture = new HashMap(); + 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); + } } diff --git a/src/test/java/net/helenus/test/unit/core/dsl/Account.java b/src/test/java/net/helenus/test/unit/core/dsl/Account.java index 62da359..626363f 100644 --- a/src/test/java/net/helenus/test/unit/core/dsl/Account.java +++ b/src/test/java/net/helenus/test/unit/core/dsl/Account.java @@ -18,43 +18,42 @@ 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 mutated() { - return null; - } + @Override + public Set mutated() { + return null; + } - @Override - public Object build() { - return null; - } + @Override + public Object build() { + return null; + } - @Override - public Map toMap() { - return null; - } - } + @Override + public Map toMap() { + return null; + } + } } diff --git a/src/test/java/net/helenus/test/unit/core/dsl/AccountWithCollections.java b/src/test/java/net/helenus/test/unit/core/dsl/AccountWithCollections.java index 53f2e35..ce78d99 100644 --- a/src/test/java/net/helenus/test/unit/core/dsl/AccountWithCollections.java +++ b/src/test/java/net/helenus/test/unit/core/dsl/AccountWithCollections.java @@ -15,12 +15,10 @@ */ 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; @@ -28,15 +26,15 @@ import net.helenus.mapping.annotation.Types; @Table public interface AccountWithCollections { - @PartitionKey - long id(); + @PartitionKey + long id(); - @Types.Set(Name.TEXT) - Set aliases(); + @Types.Set(Name.TEXT) + Set aliases(); - @Types.List(Name.TEXT) - List name(); + @Types.List(Name.TEXT) + List name(); - @Types.Map(key = Name.TEXT, value = Name.TEXT) - Map properties(); + @Types.Map(key = Name.TEXT, value = Name.TEXT) + Map properties(); } diff --git a/src/test/java/net/helenus/test/unit/core/dsl/CollectionsDlsTest.java b/src/test/java/net/helenus/test/unit/core/dsl/CollectionsDlsTest.java index 1cb9e4b..14f2576 100644 --- a/src/test/java/net/helenus/test/unit/core/dsl/CollectionsDlsTest.java +++ b/src/test/java/net/helenus/test/unit/core/dsl/CollectionsDlsTest.java @@ -15,64 +15,63 @@ */ 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 getter = Query.get(account::properties, "key1"); + Getter 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 getter = Query.getIdx(account::name, 2); + Getter 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); + } } diff --git a/src/test/java/net/helenus/test/unit/core/dsl/DslTest.java b/src/test/java/net/helenus/test/unit/core/dsl/DslTest.java index a151bb4..ca9ecae 100644 --- a/src/test/java/net/helenus/test/unit/core/dsl/DslTest.java +++ b/src/test/java/net/helenus/test/unit/core/dsl/DslTest.java @@ -15,28 +15,27 @@ */ 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(); + } } diff --git a/src/test/java/net/helenus/test/unit/core/dsl/Rocket.java b/src/test/java/net/helenus/test/unit/core/dsl/Rocket.java index e0e44c0..dcd9a43 100644 --- a/src/test/java/net/helenus/test/unit/core/dsl/Rocket.java +++ b/src/test/java/net/helenus/test/unit/core/dsl/Rocket.java @@ -5,7 +5,7 @@ import net.helenus.mapping.annotation.UDT; @UDT public interface Rocket { - int length(); + int length(); - double price(); + double price(); } diff --git a/src/test/java/net/helenus/test/unit/core/dsl/UDTCollectionsDlsTest.java b/src/test/java/net/helenus/test/unit/core/dsl/UDTCollectionsDlsTest.java index 65ee050..4beccef 100644 --- a/src/test/java/net/helenus/test/unit/core/dsl/UDTCollectionsDlsTest.java +++ b/src/test/java/net/helenus/test/unit/core/dsl/UDTCollectionsDlsTest.java @@ -17,42 +17,40 @@ 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 comments = new HashMap(); + Map comments = new HashMap(); - Map firstMap = new HashMap(); - firstMap.put("length", 100); - firstMap.put("price", 100.0); + Map firstMap = new HashMap(); + firstMap.put("length", 100); + firstMap.put("price", 100.0); - Rocket first = Helenus.map(Rocket.class, firstMap); + Rocket first = Helenus.map(Rocket.class, firstMap); - Map secondMap = new HashMap(); - secondMap.put("length", 50); - secondMap.put("price", 70.0); + Map secondMap = new HashMap(); + 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)); + } } diff --git a/src/test/java/net/helenus/test/unit/core/dsl/WrapperTest.java b/src/test/java/net/helenus/test/unit/core/dsl/WrapperTest.java index 74ac6e7..ae26137 100644 --- a/src/test/java/net/helenus/test/unit/core/dsl/WrapperTest.java +++ b/src/test/java/net/helenus/test/unit/core/dsl/WrapperTest.java @@ -17,57 +17,55 @@ 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 map = new HashMap(); + Map map = new HashMap(); - 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 map = new HashMap(); + // 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 map = new HashMap(); - 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()); + WrongAccount wrongAccount = Helenus.map(WrongAccount.class, new HashMap()); - wrongAccount.id(); - } + wrongAccount.id(); + } } diff --git a/src/test/java/net/helenus/test/unit/core/dsl/WrongAccount.java b/src/test/java/net/helenus/test/unit/core/dsl/WrongAccount.java index b7cd833..77f6971 100644 --- a/src/test/java/net/helenus/test/unit/core/dsl/WrongAccount.java +++ b/src/test/java/net/helenus/test/unit/core/dsl/WrongAccount.java @@ -17,5 +17,5 @@ package net.helenus.test.unit.core.dsl; public interface WrongAccount { - void id(); + void id(); } diff --git a/src/test/java/net/helenus/test/unit/support/ImmutablesTest.java b/src/test/java/net/helenus/test/unit/support/ImmutablesTest.java index ee29193..c7cd5a4 100644 --- a/src/test/java/net/helenus/test/unit/support/ImmutablesTest.java +++ b/src/test/java/net/helenus/test/unit/support/ImmutablesTest.java @@ -18,88 +18,86 @@ 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 set = Immutables.setOf(123); + Set 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 list = Immutables.listOf(123); + List 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 map = Immutables.mapOf(123, 555); + Map 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 e : map.entrySet()) { - Assert.assertEquals(Integer.valueOf(123), e.getKey()); - Assert.assertEquals(Integer.valueOf(555), e.getValue()); - c++; - } + c = 0; + for (Map.Entry 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); + } } diff --git a/src/test/java/net/helenus/test/unit/support/TransformersTest.java b/src/test/java/net/helenus/test/unit/support/TransformersTest.java index 7f0868a..fd787f9 100644 --- a/src/test/java/net/helenus/test/unit/support/TransformersTest.java +++ b/src/test/java/net/helenus/test/unit/support/TransformersTest.java @@ -19,68 +19,66 @@ 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 source = new ArrayList(); - source.add(555); - source.add(777); - source.add(999); + List source = new ArrayList(); + source.add(555); + source.add(777); + source.add(999); - List out = Transformers.transformList(source, x -> "_" + x); + List 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 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 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 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 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 source = new ArrayList(); - source.add(555); - source.add(null); - source.add(999); + List source = new ArrayList(); + source.add(555); + source.add(null); + source.add(999); - List out = Transformers.transformList(source, x -> "_" + x); + List 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)); + } }