From 0f3ee65a524004dc390d105e63c81db8a2470d72 Mon Sep 17 00:00:00 2001 From: Jonathan Shook Date: Fri, 24 Apr 2020 09:03:19 -0500 Subject: [PATCH] provide example function for fconfig --- .../functions/to_tuple/CustomFunc955.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 activitytype-cql/src/main/java/io/nosqlbench/activitytype/cql/datamappers/functions/to_tuple/CustomFunc955.java diff --git a/activitytype-cql/src/main/java/io/nosqlbench/activitytype/cql/datamappers/functions/to_tuple/CustomFunc955.java b/activitytype-cql/src/main/java/io/nosqlbench/activitytype/cql/datamappers/functions/to_tuple/CustomFunc955.java new file mode 100644 index 000000000..1fca59d2a --- /dev/null +++ b/activitytype-cql/src/main/java/io/nosqlbench/activitytype/cql/datamappers/functions/to_tuple/CustomFunc955.java @@ -0,0 +1,91 @@ +package io.nosqlbench.activitytype.cql.datamappers.functions.to_tuple; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.TupleType; +import com.datastax.driver.core.TupleValue; +import io.nosqlbench.nb.api.errors.BasicError; +import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper; +import io.nosqlbench.virtdata.core.config.ConfigAware; +import io.nosqlbench.virtdata.core.config.ConfigModel; +import io.nosqlbench.virtdata.core.config.MutableConfigModel; + +import java.math.BigInteger; +import java.util.*; +import java.util.function.Function; +import java.util.function.LongFunction; +import java.util.function.LongToIntFunction; +import java.util.function.LongUnaryOperator; +import java.util.regex.Pattern; + +/** + * Temporary function to test a specific nested type. This should be replaced + * with a general custom/tuple type aware binding function. + * The type supported is a CQL type: {@code map>} + * + * Functions are required for: + *
  • + *
  • map size {@code (LongToIntFunction)}
  • + *
  • key {@code (LongFunction)} + *
  • tuple field 1 {@code (LongToIntFunction)}
  • + *
  • tuple field 2 {@code {LongToIntFunction)}
  • + * + */ +@ThreadSafeMapper +public class CustomFunc955 implements LongFunction>, ConfigAware { + + private final LongToIntFunction sizefunc; + private final LongFunction keyfunc; + private final LongToIntFunction field1func; + private final LongUnaryOperator field2func; + private Cluster cluster; + private TupleType tupleType; + + public CustomFunc955(LongToIntFunction sizefunc, LongFunction keyfunc, + LongToIntFunction field1func, LongToIntFunction field2func) { + + this.sizefunc = sizefunc; + this.keyfunc = keyfunc; + this.field1func = field1func; + this.field2func = field2func::applyAsInt; + } + + public CustomFunc955(LongToIntFunction sizefunc, LongFunction keyfunc, + LongToIntFunction field1func, LongUnaryOperator field2func) { + + this.sizefunc = sizefunc; + this.keyfunc = keyfunc; + this.field1func = field1func; + this.field2func = field2func; + } + + @Override + public Map apply(long value) { + int size = sizefunc.applyAsInt(value); + + HashMap map = new HashMap<>(size); + for (int i = 0; i < size; i++) { + String key = keyfunc.apply(value+i).toString(); + int tuple1 = field1func.applyAsInt(value+i); + long tuple2 = field2func.applyAsLong(value+i); + TupleValue tupleValue = tupleType.newValue(tuple1, tuple2); + map.put(key,tupleValue); + } + return map; + } + + @Override + public void applyConfig(Map elements) { + this.cluster = Optional.ofNullable(elements.get("cluster")) + .map(Cluster.class::cast) + .orElseThrow(); + this.tupleType = cluster.getMetadata().newTupleType(DataType.cint(), DataType.bigint()); + } + + @Override + public ConfigModel getConfigModel() { + return new MutableConfigModel() + .add("", Cluster.class) + .asReadOnly(); + } +}