port CQL data mapping functions into cqld4

This commit is contained in:
Jonathan Shook 2022-02-15 21:24:09 -06:00
parent df9ee20f0a
commit 542ae8369f
32 changed files with 1432 additions and 0 deletions

View File

@ -67,6 +67,12 @@
<version>4.13.0</version> <version>4.13.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.xerial.snappy</groupId>
<artifactId>snappy-java</artifactId>
<version>1.1.7.3</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -0,0 +1,87 @@
package io.nosqlbench.datamappers.functions.diagnostics;
import com.datastax.oss.driver.api.core.type.DataTypes;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.function.Function;
/**
* Shows the compatible CQL type most associated with the incoming Java type.
*/
//@ThreadSafeMapper
//@Categories({Category.diagnostics})
public class ToCqlType implements Function<Object, String> {
private final static Map<String, String> typemap = new HashMap<String, String>() {{
for (Field field : DataTypes.class.getFields()) {
int mods = field.getModifiers();
int req = Modifier.STATIC & Modifier.FINAL & Modifier.PUBLIC;
if ((mods&req)<req) {
continue;
}
if (!field.getName().toUpperCase(Locale.ROOT).equals(field.getName())) {
continue;
}
}
put("unsupported in this version"," additional work required ");
}};
private final ThreadLocal<StringBuilder> tlsb = ThreadLocal.withInitial(StringBuilder::new);
@Override
public String apply(Object o) {
String canonicalName = o.getClass().getCanonicalName();
String cqlTypeName = typemap.get(canonicalName);
StringBuilder sb = tlsb.get();
sb.setLength(0);
if (cqlTypeName!=null) {
return sb.append(canonicalName).append(" -> ").append(cqlTypeName).toString();
}
return findAlternates(o,canonicalName);
}
private String findAlternates(Object o, String canonicalName) {
StringBuilder sb = tlsb.get();
if (List.class.isAssignableFrom(o.getClass())) {
sb.append(canonicalName).append("<");
if (((List)o).size()>0) {
Object o1 = ((List) o).get(0);
String elementType = o1.getClass().getCanonicalName();
sb.append(elementType).append("> -> List<");
sb.append(typemap.getOrDefault(elementType,"UNKNOWN")).append(">");
return sb.toString();
}
return sb.append("?> -> List<?>").toString();
}
if (Map.class.isAssignableFrom(o.getClass())) {
sb.append(canonicalName).append("<");
if (((Map)o).size()>0) {
Map.Entry next = (Map.Entry) ((Map) o).entrySet().iterator().next();
String keyType = next.getKey().getClass().getCanonicalName();
String valType = next.getValue().getClass().getCanonicalName();
sb.append(keyType).append(",").append(valType).append("> -> Map<");
sb.append(typemap.getOrDefault(keyType,"UNKNOWN")).append(",");
sb.append(typemap.getOrDefault(valType,"UNKNOWN")).append(">");
return sb.toString();
}
return sb.append("?,?> -> Map<?,?>").toString();
}
if (Set.class.isAssignableFrom(o.getClass())) {
sb.append(canonicalName).append("<");
if (((Set)o).size()>0) {
Object o1=((Set)o).iterator().next();
String elementType = o1.getClass().getCanonicalName();
sb.append(elementType).append("> -> Set<");
sb.append(typemap.getOrDefault(elementType,"UNKNOWN")).append(">");
return sb.toString();
}
return sb.append("?> -> Set<?>").toString();
}
return typemap.getOrDefault(o.getClass().getSuperclass().getCanonicalName(), "UNKNOWN");
}
}

View File

@ -0,0 +1,21 @@
package io.nosqlbench.datamappers.functions.double_to_cqlduration;
import com.datastax.oss.driver.api.core.data.CqlDuration;
import java.util.function.DoubleFunction;
/**
* Convert the input double value into a CQL {@link CqlDuration} object,
* by setting months to zero, and using the fractional part as part
* of a day, assuming 24-hour days.
*/
public class ToCqlDuration implements DoubleFunction<CqlDuration> {
private final static double NS_PER_DAY = 1_000_000_000L * 60 * 60 * 24;
@Override
public CqlDuration apply(double value) {
double fraction = value - (long) value;
return CqlDuration.newInstance(0,(int)value,(long)(fraction*NS_PER_DAY));
}
}

View File

@ -0,0 +1,68 @@
package io.nosqlbench.datamappers.functions.geometry;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.function.LongFunction;
import java.util.function.LongToDoubleFunction;
/**
* Create a Distance generator which produces
* com.datastax.driver.dse.geometry.Distance objects.
*/
@ThreadSafeMapper
@Categories({Category.objects})
public class Distance implements LongFunction<com.datastax.dse.driver.internal.core.data.geometry.Distance> {
private final Point pointfunc;
private final LongToDoubleFunction rfunc;
public Distance(LongToDoubleFunction xfunc, LongToDoubleFunction yfunc, LongToDoubleFunction rfunc) {
pointfunc = new Point(xfunc,yfunc);
this.rfunc = rfunc;
}
public Distance(double x, LongToDoubleFunction yfunc, LongToDoubleFunction rfunc) {
pointfunc = new Point((u)->x,yfunc);
this.rfunc = rfunc;
}
public Distance(LongToDoubleFunction xfunc, double y, LongToDoubleFunction rfunc) {
pointfunc = new Point(xfunc,(v)->y);
this.rfunc = rfunc;
}
public Distance(double x, double y, LongToDoubleFunction rfunc) {
pointfunc = new Point((u)->x,(v)->y);
this.rfunc = rfunc;
}
public Distance(LongToDoubleFunction xfunc, LongToDoubleFunction yfunc, double r) {
pointfunc = new Point(xfunc,yfunc);
this.rfunc = (w) -> r;
}
public Distance(double x, LongToDoubleFunction yfunc, double r) {
pointfunc = new Point((u)->x,yfunc);
this.rfunc = (w) -> r;
}
public Distance(LongToDoubleFunction xfunc, double y, double r) {
pointfunc = new Point(xfunc,(v)->y);
this.rfunc = (w) -> r;
}
public Distance(double x, double y, double r) {
pointfunc = new Point((u) -> x, (v) -> y);
this.rfunc = (w) -> r;
}
@Override
public com.datastax.dse.driver.internal.core.data.geometry.Distance apply(long value) {
com.datastax.dse.driver.api.core.data.geometry.Point apoint = pointfunc.apply(value);
double aradius = rfunc.applyAsDouble(value);
return new com.datastax.dse.driver.internal.core.data.geometry.Distance(apoint,aradius);
}
}

View File

@ -0,0 +1,54 @@
package io.nosqlbench.datamappers.functions.geometry;
//import com.datastax.driver.dse.geometry.Point;
import com.datastax.dse.driver.internal.core.data.geometry.DefaultLineString;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.function.LongFunction;
import java.util.function.LongToDoubleFunction;
import java.util.function.LongToIntFunction;
/**
* Create a com.datastax.driver.dse.geometry.LineString
*/
@SuppressWarnings("Duplicates")
@ThreadSafeMapper
@Categories({Category.objects})
public class LineString implements LongFunction<com.datastax.dse.driver.api.core.data.geometry.LineString > {
private final LongFunction<com.datastax.dse.driver.api.core.data.geometry.Point > pointfunc;
private final LongToIntFunction lenfunc;
public LineString(LongToIntFunction lenfunc, LongFunction<com.datastax.dse.driver.api.core.data.geometry.Point > pointfunc) {
this.pointfunc = pointfunc;
this.lenfunc = lenfunc;
}
public LineString(LongToIntFunction lenfunc, LongToDoubleFunction xfunc, LongToDoubleFunction yfunc) {
this.lenfunc = lenfunc;
this.pointfunc=new Point(xfunc,yfunc);
}
public LineString(int len, LongFunction<com.datastax.dse.driver.api.core.data.geometry.Point > pointfunc) {
this.lenfunc = (i) -> len;
this.pointfunc = pointfunc;
}
@Override
public com.datastax.dse.driver.api.core.data.geometry.LineString apply(long value) {
int linelen = Math.max(lenfunc.applyAsInt(value),2);
com.datastax.dse.driver.api.core.data.geometry.Point p0 = pointfunc.apply(value);
com.datastax.dse.driver.api.core.data.geometry.Point p1 = pointfunc.apply(value+1);
com.datastax.dse.driver.api.core.data.geometry.Point[] points =
new com.datastax.dse.driver.api.core.data.geometry.Point [linelen-2];
for (int i = 2; i < linelen; i++) {
points[i-2]=pointfunc.apply(value+i);
}
return new DefaultLineString(p0,p1,points);
}
}

View File

@ -0,0 +1,48 @@
package io.nosqlbench.datamappers.functions.geometry;
import com.datastax.dse.driver.internal.core.data.geometry.DefaultPoint;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.function.LongFunction;
import java.util.function.LongToDoubleFunction;
/**
* Create a Point generator which generates com.datastax.driver.dse.geometry.Point
* objects.
*/
@ThreadSafeMapper
@Categories({Category.objects})
public class Point implements LongFunction<com.datastax.dse.driver.api.core.data.geometry.Point> {
private final LongToDoubleFunction xfunc;
private final LongToDoubleFunction yfunc;
public Point(double x, double y) {
this.xfunc = (u) -> x;
this.yfunc = (v) -> y;
}
public Point(double x, LongToDoubleFunction yfunc) {
this.xfunc = (u) -> x;
this.yfunc = yfunc;
}
public Point(LongToDoubleFunction xfunc, double y) {
this.xfunc = xfunc;
this.yfunc = (v) -> y;
}
public Point(LongToDoubleFunction xfunc, LongToDoubleFunction yfunc) {
this.xfunc = xfunc;
this.yfunc = yfunc;
}
@Override
public com.datastax.dse.driver.api.core.data.geometry.Point apply(long value) {
return new DefaultPoint(xfunc.applyAsDouble(value), yfunc.applyAsDouble(value));
}
}

View File

@ -0,0 +1,52 @@
package io.nosqlbench.datamappers.functions.geometry;
import com.datastax.dse.driver.internal.core.data.geometry.DefaultPolygon;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.function.LongFunction;
import java.util.function.LongToDoubleFunction;
import java.util.function.LongToIntFunction;
/**
* Create a com.datastax.driver.dse.geometry.Polygon
*/
@SuppressWarnings("ALL")
@ThreadSafeMapper
@Categories({Category.objects})
public class Polygon implements LongFunction<com.datastax.dse.driver.api.core.data.geometry.Polygon > {
private final LongFunction<com.datastax.dse.driver.api.core.data.geometry.Point> pointfunc;
private final LongToIntFunction lenfunc;
public Polygon(LongToIntFunction lenfunc, LongFunction<com.datastax.dse.driver.api.core.data.geometry.Point> pointfunc) {
this.pointfunc = pointfunc;
this.lenfunc = lenfunc;
}
public Polygon(LongToIntFunction lenfunc, LongToDoubleFunction xfunc, LongToDoubleFunction yfunc) {
this.lenfunc = lenfunc;
this.pointfunc=new Point(xfunc,yfunc);
}
public Polygon(int len, LongFunction<com.datastax.dse.driver.api.core.data.geometry.Point> pointfunc) {
this.lenfunc = (i) -> len;
this.pointfunc = pointfunc;
}
@Override
public com.datastax.dse.driver.api.core.data.geometry.Polygon apply(long value) {
int linelen = Math.max(lenfunc.applyAsInt(value),3);
com.datastax.dse.driver.api.core.data.geometry.Point p0 = pointfunc.apply(value);
com.datastax.dse.driver.api.core.data.geometry.Point p1 = pointfunc.apply(value+1);
com.datastax.dse.driver.api.core.data.geometry.Point p2 = pointfunc.apply(value+2);
com.datastax.dse.driver.api.core.data.geometry.Point[] points =
new com.datastax.dse.driver.api.core.data.geometry.Point[linelen-3];
for (int i = 3; i < linelen; i++) {
points[i-3]=pointfunc.apply(value+i);
}
return new DefaultPolygon(p0,p1,p2,points);
}
}

View File

@ -0,0 +1,90 @@
package io.nosqlbench.datamappers.functions.geometry;
import com.datastax.dse.driver.api.core.data.geometry.Polygon;
import com.datastax.dse.driver.internal.core.data.geometry.DefaultPoint;
import com.datastax.dse.driver.internal.core.data.geometry.DefaultPolygon;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import io.nosqlbench.virtdata.library.curves4.discrete.long_int.Uniform;
import java.util.function.LongFunction;
/**
* This function will return a polygon in the form of a rectangle from the specified
* grid system. The coordinates define the top left and bottom right coordinates in
* (x1,y1),(x2,y2) order, while the number of rows and columns divides these ranges
* into the unit-length for each square.
* x1 must be greater than x2. y1 must be less than y2.
*
* This grid system can be used to construct a set of overlapping grids such that the
* likelyhood of overlap is somewhat easy to reason about. For example, if you create
* one grid system as a refernce grid, then attempt to map another grid system which
* half overlaps the original grid, you can easily determine that half the time, a
* random rectangle selected from the second grid will overlap a rectangle from the
* first, for simple even-numbered grids and the expected uniform sampling on the
* internal coordinate selector functions.
*/
@SuppressWarnings("ALL")
@ThreadSafeMapper
@Categories({Category.objects})
public class PolygonOnGrid implements LongFunction<com.datastax.dse.driver.api.core.data.geometry.Polygon> {
private final double rows;
private final double columns;
private final double x_topleft;
private final double y_topleft;
private final double x_bottomright;
private final double y_bottomright;
private final Uniform rowfunc;
private final Uniform colfunc;
private final double xwidth;
private final double yheight;
@Example({"PolygonOnGrid(1, 11, 11, 1, 10, 10)","Create a 10x10 grid with cells 1x1, spaced one off the y=0 and x=0 axes"})
public PolygonOnGrid(double x_topleft, double y_topleft, double x_bottomright, double y_bottomright, int rows, int columns) {
if (x_topleft>=x_bottomright) {
throw new RuntimeException("x_topleft should be less than x_bottomright");
}
if (y_topleft<=y_bottomright) {
throw new RuntimeException("y_topleft should be more than y_bottomright");
}
this.x_topleft = x_topleft;
this.y_topleft = y_topleft;
this.x_bottomright = x_bottomright;
this.y_bottomright = y_bottomright;
this.rows = rows;
this.columns = columns;
this.xwidth = (x_bottomright-x_topleft) / columns;
this.yheight = (y_topleft-y_bottomright) / columns;
this.rowfunc = new Uniform(0, rows - 1);
this.colfunc = new Uniform(0,columns-1);
}
@Override
public Polygon apply(long value) {
int row = rowfunc.applyAsInt(value);
int column = colfunc.applyAsInt(value+33);
double left=x_topleft + (column*xwidth);
double top =y_topleft - (row*yheight);
double right = left+xwidth;
double bottom = top - yheight;
com.datastax.dse.driver.api.core.data.geometry.Polygon polygon = new DefaultPolygon(
new DefaultPoint(left, bottom),
new DefaultPoint(left, top),
new DefaultPoint(right, top),
new DefaultPoint(right, bottom)
);
return polygon;
}
}

View File

@ -0,0 +1,42 @@
package io.nosqlbench.datamappers.functions.long_localdate;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.function.LongFunction;
/**
* Converts epoch millis to a java.time.LocalDate, which takes the place
* of the previous CQL-specific LocalDate. if a zoneid of 'default' is provided,
* then the time zone is set by the default for the JVM. If not, then
* a valid ZoneId is looked up. The no-args version uses GMT.
*/
@ThreadSafeMapper
@Categories({Category.datetime})
public class EpochMillisToCqlLocalDate implements LongFunction<LocalDate> {
private final ZoneId zoneId;
public EpochMillisToCqlLocalDate(String zoneid) {
if (zoneid.equals("default")) {
this.zoneId = ZoneId.systemDefault();
} else {
this.zoneId = ZoneId.of(zoneid);
}
}
@Example({"EpochMillisToJavaLocalDate()", "Yields the LocalDate for the millis in GMT"})
public EpochMillisToCqlLocalDate() {
this.zoneId = ZoneId.of("GMT");
}
@Override
public LocalDate apply(long value) {
return LocalDate.ofInstant(Instant.ofEpochMilli(value), ZoneId.systemDefault());
}
}

View File

@ -0,0 +1,46 @@
package io.nosqlbench.datamappers.functions.long_localdate;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.function.LongFunction;
/**
* Converts epoch millis to a java.time.{@link LocalDate} object,
* using either the system
* default timezone or the timezone provided. If the specified ZoneId is not
* the same as the time base of the epoch millis instant, then conversion
* errors will occur.
*
* Short form ZoneId values like 'CST' can be used, although US Domestic names
* which specify the daylight savings hours are not supported. The full list of
* short Ids at @see <a href="https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/time/ZoneId.html#SHORT_IDS">JavaSE ZoneId Ids</a>
*
* Any timezone specifier may be used which can be read by {@link ZoneId#of(String)}
*/
@ThreadSafeMapper
@Categories({Category.datetime})
public class EpochMillisToJavaLocalDate implements LongFunction<LocalDate> {
ZoneId timezone;
@Example({"EpochMillisToJavaLocalDate()","Yields the LocalDate for the system default ZoneId"})
public EpochMillisToJavaLocalDate() {
this.timezone = ZoneId.systemDefault();
}
@Example({"EpochMillisToJavaLocalDate('ECT')","Yields the LocalDate for the ZoneId entry for 'Europe/Paris'"})
public EpochMillisToJavaLocalDate(String zoneid) {
this.timezone = ZoneId.of(zoneid);
}
@Override
public LocalDate apply(long value) {
return Instant.ofEpochMilli(value).atZone(timezone).toLocalDate();
}
}

View File

@ -0,0 +1,46 @@
package io.nosqlbench.datamappers.functions.long_localdate;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.function.LongFunction;
/**
* Converts epoch millis to a
* java.time.{@link LocalDateTime} object, using either the system
* default timezone or the timezone provided. If the specified ZoneId is not
* the same as the time base of the epoch millis instant, then conversion
* errors will occur.
*
* Short form ZoneId values like 'CST' can be used, although US Domestic names
* which specify the daylight savings hours are not supported. The full list of
* short Ids at @see <a href="https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/time/ZoneId.html#SHORT_IDS">JavaSE ZoneId Ids</a>
*
* Any timezone specifier may be used which can be read by {@link ZoneId#of(String)}
*/
@ThreadSafeMapper
@Categories({Category.datetime})
public class EpochMillisToJavaLocalDateTime implements LongFunction<LocalDateTime> {
ZoneId timezone;
@Example({"EpochMillisToJavaLocalDateTime()","Yields the LocalDateTime for the system default ZoneId"})
public EpochMillisToJavaLocalDateTime() {
this.timezone = ZoneId.systemDefault();
}
@Example({"EpochMillisToJavaLocalDateTime('ECT')","Yields the LocalDateTime for the ZoneId entry for 'Europe/Paris'"})
public EpochMillisToJavaLocalDateTime(String zoneid) {
this.timezone = ZoneId.of(zoneid);
}
@Override
public LocalDateTime apply(long value) {
return Instant.ofEpochMilli(value).atZone(timezone).toLocalDateTime();
}
}

View File

@ -0,0 +1,27 @@
package io.nosqlbench.datamappers.functions.long_localdate;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.time.LocalDate;
import java.util.function.LongFunction;
/**
* Days since Jan 1st 1970
*/
@ThreadSafeMapper
@Categories({Category.datetime})
public class LongToLocalDateDays implements LongFunction<LocalDate> {
@Example({"LongToLocalDateDays()","take the cycle number and turn it into a LocalDate based on days since 1970"})
public LongToLocalDateDays (){
}
@Override
public LocalDate apply(long value) {
return LocalDate.ofEpochDay((int) value & Integer.MAX_VALUE);
}
}

View File

@ -0,0 +1,57 @@
package io.nosqlbench.datamappers.functions.long_to_cqlduration;
import com.datastax.oss.driver.api.core.data.CqlDuration;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import io.nosqlbench.virtdata.api.bindings.VirtDataConversions;
import java.util.function.LongFunction;
import java.util.function.LongToIntFunction;
import java.util.function.LongUnaryOperator;
/**
* Map a long value into a CQL Duration object based on a set of field functions.
*/
@ThreadSafeMapper
@Categories({Category.datetime})
public class CqlDurationFunctions implements LongFunction<CqlDuration> {
private final LongToIntFunction monthsfunc;
private final LongToIntFunction daysfunc;
private final LongUnaryOperator nanosfunc;
/**
* Create a CQL Duration object from the two provided field functions. The months field is always set to
* zero in this form.
* @param monthsFunc A function that will yield the months value
* @param daysFunc A function that will yield the days value
* @param nanosFunc A function that will yeild the nanos value
*/
public CqlDurationFunctions(Object monthsFunc, Object daysFunc, Object nanosFunc) {
this.monthsfunc = VirtDataConversions.adaptFunction(monthsFunc, LongToIntFunction.class);
this.daysfunc = VirtDataConversions.adaptFunction(daysFunc, LongToIntFunction.class);
this.nanosfunc = VirtDataConversions.adaptFunction(nanosFunc, LongUnaryOperator.class);
}
/**
* Create a CQL Duration object from the two provided field functions. The months field is always set to
* zero in this form.
* @param daysFunc A function that will yield the days value
* @param nanosFunc A function that will yeild the nanos value
*/
public CqlDurationFunctions(Object daysFunc, Object nanosFunc) {
this.monthsfunc = (v) -> 0;
this.daysfunc = VirtDataConversions.adaptFunction(daysFunc, LongToIntFunction.class);
this.nanosfunc = VirtDataConversions.adaptFunction(nanosFunc, LongUnaryOperator.class);
}
@Override
public CqlDuration apply(long value) {
int months = monthsfunc.applyAsInt(value);
int days = daysfunc.applyAsInt(value);
long nanos = nanosfunc.applyAsLong(value);
return CqlDuration.newInstance(months,days,nanos);
}
}

View File

@ -0,0 +1,31 @@
package io.nosqlbench.datamappers.functions.long_to_cqlduration;
import com.datastax.oss.driver.api.core.data.CqlDuration;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.function.LongFunction;
/**
* Convert the input value into a {@link CqlDuration}
* by reading the input as total nanoseconds, assuming 30-month days.
*/
@ThreadSafeMapper
@Categories({Category.conversion,Category.datetime})
public class ToCqlDurationNanos implements LongFunction<CqlDuration> {
private final static long NS_PER_S = 1_000_000_000L;
private final static long NS_PER_DAY = NS_PER_S * 60*60*24;
private final static long NS_PER_MONTH = NS_PER_DAY * 30;
@Override
public CqlDuration apply(long value) {
long nanos = value % NS_PER_DAY;
value -= nanos;
long days = value / NS_PER_DAY;
value -= days*NS_PER_DAY;
long months = value / NS_PER_MONTH;
return CqlDuration.newInstance((int) months,(int) days, nanos);
}
}

View File

@ -0,0 +1,25 @@
package io.nosqlbench.datamappers.functions.long_uuid;
//import com.datastax.driver.core.utils.UUIDs;
import com.datastax.oss.driver.api.core.uuid.Uuids;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.UUID;
import java.util.function.LongFunction;
/**
* Converts a long timestamp in epoch millis form into a Version 1 TimeUUID
* according to <a href="https://www.ietf.org/rfc/rfc4122.txt">RFC 4122</a>.
* This form uses {@link Uuids#endOf(long)} (long)}
*/
@Categories({Category.datetime})
@ThreadSafeMapper
public class ToTimeUUIDMax implements LongFunction<UUID> {
@Override
public UUID apply(long value) {
return Uuids.endOf(value);
}
}

View File

@ -0,0 +1,23 @@
package io.nosqlbench.datamappers.functions.long_uuid;
import com.datastax.oss.driver.api.core.uuid.Uuids;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.UUID;
import java.util.function.LongFunction;
/**
* Converts a long timestamp in epoch millis form into a Version 1 TimeUUID
* according to <a href="https://www.ietf.org/rfc/rfc4122.txt">RFC 4122</a>.
* This form uses {@link Uuids#startOf(long)}
*/
@Categories({Category.datetime})
@ThreadSafeMapper
public class ToTimeUUIDMin implements LongFunction<UUID> {
@Override
public UUID apply(long value) {
return Uuids.startOf(value);
}
}

View File

@ -0,0 +1,98 @@
package io.nosqlbench.datamappers.functions.rainbow;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
/**
* <p>This class provides <em>cursor-like</em> access to a set of data from
* a binary file using Java nio buffers. Calling {@link #next()} causes
* the next record to be loaded, after which the getter methods return
* the loaded values. You must call next before access each record's fields.</p>
*
* <p>The {@link #next(int)} method may be used for absolute offset access.
* In this mode, no thread safety is imposed, as there is no chance of the
* internal buffer's position to affect the result.</p>
*
* <p>Buffers may be accessed as shared or not. If</p>
*
*/
public class TokenMapFileAPIService {
// public static ThreadLocal<Map<String, BinaryCursorForTokenCycle>> tl_cll =
// ThreadLocal.withInitial(HashMap::new);
//
private final int recordCount;
private final ByteBuffer buffer;
private final int RECORD_LEN = Long.BYTES * 2;
private final int recordPosition;
private long token;
private final int TOKEN_OFFSET = 0;
private long cycle;
private final int CYCLE_OFFSET = Long.BYTES;
private final boolean loopdata;
/**
* Create a new binary cursor for data in a binary file which consists of a (long,long) tuple of
* token values (murmur3 partitioner tokens) and cycle values that correspond to them. The cycles
* are the ones responsible for producing the associated token values.
* @param datafile The data file to read from
* @param loopdata Whether or not to loop around to the beginning of the data. For positional reads this is also
* modulo-based, such that relatively prime sizes and increments will loop not simply repeat
* values at the start of the buffer
* @param instanced Whether or not to provide an instanced view into the byte buffer, where each thread can have
* its own read tracking state
* @param ascending Whether to reverse the order othe long,long tuples when the file is read.
*/
public TokenMapFileAPIService(String datafile, boolean loopdata, boolean instanced, boolean ascending) {
this.loopdata = loopdata;
buffer = TokenMapFileSharedBuffers.getByteBuffer(datafile,instanced,ascending).asReadOnlyBuffer();
this.recordCount = buffer.capacity() / RECORD_LEN;
this.recordPosition = 0;
}
public synchronized void next() {
try {
token = buffer.getLong();
cycle = buffer.getLong();
} catch (BufferUnderflowException bue) {
if (loopdata) {
buffer.position(0);
next();
}
else {
throw bue;
}
}
}
/**
* Do a read of [token,cycle] record without incremental read state.
* @param position The logical record within the buffer to read
*/
public void next(int position) {
if (loopdata) {
position = (position % recordCount) * RECORD_LEN;
}
token = buffer.getLong(position+TOKEN_OFFSET);
cycle = buffer.getLong(position+CYCLE_OFFSET);
}
public long getToken() {
return token;
}
public long getCycle() {
return cycle;
}
// public static BinaryCursorForTokenCycle get(String mapname) {
// BinaryCursorForTokenCycle cursorLongLong = tl_cll.get().get(mapname);
// return cursorLongLong;
// }
}

View File

@ -0,0 +1,22 @@
package io.nosqlbench.datamappers.functions.rainbow;
import java.util.function.IntToLongFunction;
public abstract class TokenMapFileBaseFunction implements IntToLongFunction {
protected static ThreadLocal<TokenMapFileAPIService> tl_DataSvc;
public TokenMapFileBaseFunction(String filename, boolean loopdata, boolean instanced, boolean ascending) {
tl_DataSvc = ThreadLocal.withInitial(() -> new TokenMapFileAPIService(filename, loopdata, instanced, ascending));
}
public TokenMapFileBaseFunction(String filename) {
this(filename, false, true, true);
}
// @Override
// public long applyAsLong(long operand) {
// BinaryCursorForTokenCycle bc;
// bc.next(operand);
// return 0;
// }
}

View File

@ -0,0 +1,23 @@
package io.nosqlbench.datamappers.functions.rainbow;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
/**
* Utility function used for advanced data generation experiments.
*/
@ThreadSafeMapper
@Categories({Category.experimental})
public class TokenMapFileCycle extends TokenMapFileBaseFunction {
public TokenMapFileCycle(String filename, boolean loopdata, boolean ascending) {
super(filename, loopdata, false, ascending);
}
@Override
public long applyAsLong(int value) {
TokenMapFileAPIService datasvc = tl_DataSvc.get();
return datasvc.getCycle();
}
}

View File

@ -0,0 +1,24 @@
package io.nosqlbench.datamappers.functions.rainbow;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
/**
* Utility function used for advanced data generation experiments.
*/
@ThreadSafeMapper
@Categories({Category.experimental})
public class TokenMapFileNextCycle extends TokenMapFileBaseFunction {
public TokenMapFileNextCycle(String filename, boolean loopdata, boolean ascending) {
super(filename, loopdata, false, ascending);
}
@Override
public long applyAsLong(int value) {
TokenMapFileAPIService datasvc = tl_DataSvc.get();
datasvc.next(value);
return datasvc.getCycle();
}
}

View File

@ -0,0 +1,24 @@
package io.nosqlbench.datamappers.functions.rainbow;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
/**
* Utility function used for advanced data generation experiments.
*/
@ThreadSafeMapper
@Categories({Category.experimental})
public class TokenMapFileNextToken extends TokenMapFileBaseFunction {
public TokenMapFileNextToken(String filename, boolean loopdata, boolean ascending) {
super(filename, loopdata, false, ascending);
}
@Override
public long applyAsLong(int value) {
TokenMapFileAPIService datasvc = tl_DataSvc.get();
datasvc.next(value);
return datasvc.getToken();
}
}

View File

@ -0,0 +1,60 @@
package io.nosqlbench.datamappers.functions.rainbow;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;
public class TokenMapFileSharedBuffers {
public final static TokenMapFileSharedBuffers INSTANCE = new TokenMapFileSharedBuffers();
private final static Map<String,ByteBuffer> BUFFERS = new HashMap<>();
private TokenMapFileSharedBuffers() {}
/**
* Find and load the {@link ByteBuffer} which can be read at the specified
* location. This will only be loaded into memory once. All callers will
* get access to the same logical source data. Whether or not the caller
* gets its own buffer tracking state (see {@link java.nio.Buffer}).
* If each caller will use the Buffer API for incremental reads, where
* callers could possibly read the same records, then separate instanced
* buffers are advised.
*
* <p>However, if you are planning to use position-oriented access to the
* buffer only, then it is not necessary to ask for instanced buffers. In
* some usage patterns, it may be desirable to provide a single logical
* view of buffer reader position across multiple threads. In this case,
* setting instanced to false is necessary.</p>
*
* @param filename The location of the source data for the buffer.
* @param instanced If true, each caller gets a wrapped buffer object with its own
* tracking state
* @param ascending
* @return An instance of a ByteBuffer
*/
public synchronized static ByteBuffer getByteBuffer(String filename, boolean instanced, boolean ascending) {
ByteBuffer foundBuffer = BUFFERS.computeIfAbsent(filename, f->load(f,ascending));
return instanced ? foundBuffer.asReadOnlyBuffer() : foundBuffer;
}
private static ByteBuffer load(String filename, boolean ascending) {
try {
RandomAccessFile image = new RandomAccessFile(filename, "rw");
ByteBuffer mbb = image.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, image.length());
if (!ascending) {
int RECORD_LEN = Long.BYTES * 2;
ByteBuffer descendingByteBuffer = ByteBuffer.allocate(mbb.capacity());
for (int i = mbb.capacity()-RECORD_LEN; i >= 0 ; i-=RECORD_LEN) {
long v1 = mbb.getLong(i);
long v2 = mbb.getLong(i+Long.BYTES);
descendingByteBuffer.putLong(v1);
descendingByteBuffer.putLong(v2);
}
mbb = descendingByteBuffer;
}
return mbb;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,23 @@
package io.nosqlbench.datamappers.functions.rainbow;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
/**
* Utility function used for advanced data generation experiments.
*/
@ThreadSafeMapper
@Categories({Category.experimental})
public class TokenMapFileToken extends TokenMapFileBaseFunction {
public TokenMapFileToken(String filename, boolean loopdata, boolean ascending) {
super(filename, loopdata, false, ascending);
}
@Override
public long applyAsLong(int value) {
TokenMapFileAPIService datasvc = tl_DataSvc.get();
return datasvc.getToken();
}
}

View File

@ -0,0 +1,29 @@
package io.nosqlbench.datamappers.functions.string_string;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import org.xerial.snappy.Snappy;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.function.Function;
/**
* Compress the input using snappy compression
*/
@Categories({Category.conversion})
@ThreadSafeMapper
public class SnappyComp implements Function<String, ByteBuffer> {
private final Snappy snappy = new Snappy();
@Override
public ByteBuffer apply(String s) {
try {
return ByteBuffer.wrap(Snappy.compress(s));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,58 @@
package io.nosqlbench.datamappers.functions.to_daterange;
import com.datastax.dse.driver.api.core.data.time.DateRange;
import com.datastax.dse.driver.api.core.data.time.DateRangeBound;
import com.datastax.dse.driver.api.core.data.time.DateRangePrecision;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.function.LongFunction;
/**
* Takes an input as a reference point in epoch time, and converts it to a DateRange,
* with the bounds set to the lower and upper timestamps which align to the
* specified precision. You can use any of these precisions to control the bounds
* around the provided timestamp: millisecond, second, minute, hour, day, month, or year.
*
* If the zoneid is not specified, it defaults to "GMT". If the zoneid is set to "default",
* then the zoneid is set to the default for the JVM. Otherwise, the specified zone is used.
*/
@ThreadSafeMapper
@Categories(Category.datetime)
public class DateRangeDuring implements LongFunction<DateRange> {
private final DateRangePrecision precision;
private final ZoneId zoneid;
@Example({"DateRangeDuring('millisecond')}","Convert the incoming millisecond to an equivalent DateRange"})
@Example({"DateRangeDuring('minute')}","Convert the incoming millisecond to a DateRange for the minute in which the " +
"millisecond falls"})
public DateRangeDuring(String precision) {
this(precision,"GMT");
}
public DateRangeDuring(String precision, String zoneid) {
this.precision = DateRangePrecision.valueOf(precision.toUpperCase());
if (zoneid.equals("default")) {
this.zoneid = ZoneId.systemDefault();
} else {
this.zoneid = ZoneId.of(zoneid);
}
}
@Override
public DateRange apply(long value) {
ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochMilli(value), zoneid);
DateRangeBound lower = DateRangeBound.lowerBound(date, precision);
DateRangeBound upper = DateRangeBound.upperBound(date, precision);
DateRange dateRange = new DateRange(lower, upper);
return dateRange;
}
}

View File

@ -0,0 +1,62 @@
package io.nosqlbench.datamappers.functions.to_daterange;
import com.datastax.dse.driver.api.core.data.time.DateRange;
import com.datastax.dse.driver.api.core.data.time.DateRangeBound;
import com.datastax.dse.driver.api.core.data.time.DateRangePrecision;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import io.nosqlbench.virtdata.api.bindings.VirtDataFunctions;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.function.LongFunction;
import java.util.function.LongUnaryOperator;
/**
* Uses the precision and the two functions provided to create a DateRange.
* You can use any of these precisions to control the bounds
* around the provided timestamp: millisecond, second, minute, hour, day, month, or year.
*
* If the zoneid is not specified, it defaults to "GMT". If the zoneid is set to "default",
* then the zoneid is set to the default for the JVM. Otherwise, the specified zone is used.
*/
@ThreadSafeMapper
@Categories(Category.datetime)
public class DateRangeFunc implements LongFunction<DateRange> {
private final DateRangePrecision precision;
private final LongUnaryOperator lower;
private final LongUnaryOperator upper;
private final ZoneId zoneid;
@Example({
"StartingEpochMillis('2017-01-01 23:59:59'); DateRangeFunc('second',Identity(),Add(3600000L)",
"Create 1-minute date ranges starting at 2017-01-01 23:59:59"})
public DateRangeFunc(String precision, Object lowerFunc, Object upperFunc) {
this(precision, lowerFunc, upperFunc, "GMT");
}
public DateRangeFunc(String precision, Object lowerFunc, Object upperFunc, String zoneid) {
this.precision = DateRangePrecision.valueOf(precision.toUpperCase());
this.lower = VirtDataFunctions.adapt(lowerFunc,LongUnaryOperator.class, long.class, false);
this.upper = VirtDataFunctions.adapt(upperFunc,LongUnaryOperator.class, long.class, false);
if (zoneid.equals("default")) {
this.zoneid = ZoneId.systemDefault();
} else {
this.zoneid = ZoneId.of(zoneid);
}
}
@Override
public DateRange apply(long value) {
ZonedDateTime lowerDate = ZonedDateTime.ofInstant(Instant.ofEpochMilli(lower.applyAsLong(value)), zoneid);
DateRangeBound lower = DateRangeBound.lowerBound(lowerDate,precision);
ZonedDateTime upperDate = ZonedDateTime.ofInstant(Instant.ofEpochMilli(upper.applyAsLong(value)), zoneid);
DateRangeBound upper = DateRangeBound.upperBound(upperDate,precision);
DateRange dateRange = new DateRange(lower, upper);
return dateRange;
}
}

View File

@ -0,0 +1,59 @@
package io.nosqlbench.datamappers.functions.to_daterange;
import com.datastax.dse.driver.api.core.data.time.DateRange;
import com.datastax.dse.driver.api.core.data.time.DateRangeBound;
import com.datastax.dse.driver.api.core.data.time.DateRangePrecision;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.function.LongFunction;
/**
* Takes an input as a reference point in epoch time, and converts it to a DateRange,
* with the lower bounds set to the lower bound of the precision and millisecond
* provided, and with no upper bound.
* You can use any of these precisions to control the bounds
* around the provided timestamp: millisecond, second, minute, hour, day, month, or year.
*
* If the zoneid is not specified, it defaults to "GMT". If the zoneid is set to "default",
* then the zoneid is set to the default for the JVM. Otherwise, the specified zone is used.
*/
@ThreadSafeMapper
@Categories(Category.datetime)
public class DateRangeOnOrAfter implements LongFunction<DateRange> {
private final DateRangePrecision precision;
private final ZoneId zoneid;
public DateRangeOnOrAfter(String precision, String zoneid) {
this.precision = DateRangePrecision.valueOf(precision.toUpperCase());
if (zoneid.equals("default")) {
this.zoneid = ZoneId.systemDefault();
} else {
this.zoneid = ZoneId.of(zoneid);
}
}
@Example({"DateRangeOnOrAfter('millisecond')}","Convert the incoming millisecond to an match any time on or after"})
@Example({"DateRangeOnOrAfter('minute')}","Convert the incoming millisecond to mach any time on or after the" +
" minute in which the " +
"millisecond falls"})
public DateRangeOnOrAfter(String precision) {
this(precision,"GMT");
}
@Override
public DateRange apply(long value) {
ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochMilli(value), zoneid);
DateRangeBound lower = DateRangeBound.lowerBound(date, precision);
DateRangeBound upper = DateRangeBound.UNBOUNDED;
DateRange dateRange = new DateRange(lower, upper);
return dateRange;
}
}

View File

@ -0,0 +1,59 @@
package io.nosqlbench.datamappers.functions.to_daterange;
import com.datastax.dse.driver.api.core.data.time.DateRange;
import com.datastax.dse.driver.api.core.data.time.DateRangeBound;
import com.datastax.dse.driver.api.core.data.time.DateRangePrecision;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.function.LongFunction;
/**
* Takes an input as a reference point in epoch time, and converts it to a DateRange,
* with the upper bound set to the upper bound of the precision and millisecond
* provided, and with no lower bound.
* You can use any of these precisions to control the bounds
* around the provided timestamp: millisecond, second, minute, hour, day, month, or year.
*
* If the zoneid is not specified, it defaults to "GMT". If the zoneid is set to "default",
* then the zoneid is set to the default for the JVM. Otherwise, the specified zone is used.
*/
@ThreadSafeMapper
@Categories(Category.datetime)
public class DateRangeOnOrBefore implements LongFunction<DateRange> {
private final DateRangePrecision precision;
private final ZoneId zoneid;
@Example({"DateRangeOnOrBefore('millisecond')}","Convert the incoming millisecond to match anything on or before it."})
@Example({"DateRangeOnOrBefore('minute')}","Convert the incoming millisecond to match anything on or before the minute in" +
" which the millisecond falls"})
public DateRangeOnOrBefore(String precision) {
this(precision,"GMT");
}
public DateRangeOnOrBefore(String precision, String zoneid) {
this.precision = DateRangePrecision.valueOf(precision.toUpperCase());
if (zoneid.equals("default")) {
this.zoneid = ZoneId.systemDefault();
} else {
this.zoneid = ZoneId.of(zoneid);
}
}
@Override
public DateRange apply(long value) {
ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochMilli(value),zoneid);
DateRangeBound lower = DateRangeBound.UNBOUNDED;
DateRangeBound upper = DateRangeBound.upperBound(date,precision);
DateRange dateRange = new DateRange(lower, upper);
return dateRange;
}
}

View File

@ -0,0 +1,39 @@
package io.nosqlbench.datamappers.functions.to_daterange;
import com.datastax.dse.driver.api.core.data.time.DateRange;
import com.datastax.dse.driver.api.core.data.time.DateRangePrecision;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.text.ParseException;
import java.util.function.Function;
/**
* Parses the DateRange format according to <A HREF="https://lucene.apache.org/solr/guide/6_6/working-with-dates
* .html#WorkingwithDates-DateRangeFormatting">Date Range Formatting</A>.
* When possible it is more efficient to use the other DateRange methods since they do not require parsing.
*/
@ThreadSafeMapper
@Categories(Category.datetime)
public class DateRangeParser implements Function<String, DateRange> {
private final DateRangePrecision precision;
@Example({"DateRangeParser()}","Convert inputs like '[1970-01-01T00:00:00 TO 1970-01-01T00:00:00]' into " +
"DateRanges" +
" "})
public DateRangeParser(String precision) {
this.precision = DateRangePrecision.valueOf(precision.toUpperCase());
}
@Override
public DateRange apply(String value) {
try {
return DateRange.parse(value);
} catch (ParseException e) {
throw new RuntimeException("unable to parse date range input '" + value + "': " + e.getMessage());
}
}
}

View File

@ -0,0 +1,43 @@
package io.nosqlbench.datamappers.functions.double_to_cqlduration;
import com.datastax.oss.driver.api.core.data.CqlDuration;
import io.nosqlbench.datamappers.functions.long_to_cqlduration.CqlDurationFunctions;
import io.nosqlbench.datamappers.functions.long_to_cqlduration.ToCqlDurationNanos;
import org.junit.jupiter.api.Test;
import java.util.function.LongToIntFunction;
import java.util.function.LongUnaryOperator;
import static org.assertj.core.api.Assertions.assertThat;
public class CqlDurationTests {
@Test
public void testFractionalCqlDuration() {
ToCqlDuration cd = new ToCqlDuration();
// only precise enough on the unit interval for this type of test
CqlDuration oneDayPlusOneHour = cd.apply(1.0d + (1d/24D));
assertThat(oneDayPlusOneHour).isEqualTo(CqlDuration.newInstance(0,1,1_000_000_000L*60*60));
}
@Test
public void testLongToCqlDuration() {
ToCqlDurationNanos toNanos = new ToCqlDurationNanos();
// assertThat(toNanos.apply(1_000_000_000l * 2)).isEqualTo(Duration.newInstance(0,0,1_000_000_000*2));
assertThat(toNanos.apply(1_000_000_000L*86401L)).isEqualTo(CqlDuration.newInstance(0,1,1_000_000_000));
}
@Test
public void testFunctionCqlDuration() {
CqlDurationFunctions composed = new CqlDurationFunctions(
(LongToIntFunction) m -> (int) (2 * m),
(LongToIntFunction) d -> (int) (d * 2),
(LongUnaryOperator) n -> n * 10
);
CqlDuration d2y10mo34d170ns = composed.apply(17);
assertThat(d2y10mo34d170ns).isEqualTo(
CqlDuration.newInstance(34,34,170));
}
}

View File

@ -0,0 +1,27 @@
package io.nosqlbench.datamappers.functions.long_localdate;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import static org.assertj.core.api.Assertions.assertThat;
public class EpochMillisToJavaLocalDateTest {
@Test
public void testDayAt2020Start() {
EpochMillisToJavaLocalDate func = new EpochMillisToJavaLocalDate();
LocalDate v1 = func.apply(0);
LocalDate zerodate = LocalDate.ofInstant(Instant.ofEpochMilli(0), ZoneId.systemDefault());
assertThat(v1).isEqualTo(zerodate);
LocalDate v2 = func.apply(533664000002000L);
DateTime dt2 = new DateTime(533664000002000L);
LocalDate d2instant = LocalDate.ofInstant(Instant.ofEpochMilli(533664000002000L), ZoneId.systemDefault());
assertThat(v2).isEqualTo(d2instant);
}
}

View File

@ -0,0 +1,59 @@
package io.nosqlbench.datamappers.functions.to_daterange;
import org.junit.jupiter.api.Test;
import java.util.function.Function;
import java.util.function.LongFunction;
import java.util.function.LongUnaryOperator;
import static org.assertj.core.api.Assertions.assertThat;
public class DateRangeFuncTest {
@Test
public void testDateRangeFuncs() {
LongFunction<Long> lf1 = value -> value;
DateRangeFunc function = new DateRangeFunc("second", lf1, lf1);
assertThat(function.apply(42L).toString())
.isEqualTo("[1970-01-01T00:00:00 TO 1970-01-01T00:00:00]");
assertThat(function.apply(42000L).toString())
.isEqualTo("[1970-01-01T00:00:42 TO 1970-01-01T00:00:42]");
assertThat(function.apply(42000000L).toString())
.isEqualTo("[1970-01-01T11:40:00 TO 1970-01-01T11:40:00]");
assertThat(function.apply(42000000000L).toString())
.isEqualTo("[1971-05-02T02:40:00 TO 1971-05-02T02:40:00]");
assertThat(function.apply(42000000000000L).toString())
.isEqualTo("[3300-12-05T02:40:00 TO 3300-12-05T02:40:00]");
LongUnaryOperator lf2 = value -> value;
function = new DateRangeFunc("second", lf2, lf2);
assertThat(function.apply(42L).toString())
.isEqualTo("[1970-01-01T00:00:00 TO 1970-01-01T00:00:00]");
assertThat(function.apply(42000L).toString())
.isEqualTo("[1970-01-01T00:00:42 TO 1970-01-01T00:00:42]");
assertThat(function.apply(42000000L).toString())
.isEqualTo("[1970-01-01T11:40:00 TO 1970-01-01T11:40:00]");
assertThat(function.apply(42000000000L).toString())
.isEqualTo("[1971-05-02T02:40:00 TO 1971-05-02T02:40:00]");
assertThat(function.apply(42000000000000L).toString())
.isEqualTo("[3300-12-05T02:40:00 TO 3300-12-05T02:40:00]");
Function<Long,Long> lf3 = value -> value;
function = new DateRangeFunc("second", lf3, lf3);
assertThat(function.apply(42L).toString())
.isEqualTo("[1970-01-01T00:00:00 TO 1970-01-01T00:00:00]");
assertThat(function.apply(42000L).toString())
.isEqualTo("[1970-01-01T00:00:42 TO 1970-01-01T00:00:42]");
assertThat(function.apply(42000000L).toString())
.isEqualTo("[1970-01-01T11:40:00 TO 1970-01-01T11:40:00]");
assertThat(function.apply(42000000000L).toString())
.isEqualTo("[1971-05-02T02:40:00 TO 1971-05-02T02:40:00]");
assertThat(function.apply(42000000000000L).toString())
.isEqualTo("[3300-12-05T02:40:00 TO 3300-12-05T02:40:00]");
}
}