switch list functions over to better function adapter

This commit is contained in:
Jonathan Shook 2020-07-09 05:47:44 -05:00
parent e8f76ddfb7
commit eb470df351
9 changed files with 57 additions and 167 deletions

View File

@ -1,6 +1,7 @@
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_collection;
import io.nosqlbench.virtdata.api.annotations.*;
import io.nosqlbench.virtdata.api.bindings.VirtDataConversions;
import java.util.ArrayList;
import java.util.function.LongFunction;
@ -26,10 +27,10 @@ public class List implements LongFunction<java.util.List<Object>> {
private final LongFunction<Object> valueFunc;
@Example({"List(HashRange(3,7),Add(15L))", "create a list between 3 and 7 elements of Long values"})
public List(LongToIntFunction sizeFunc,
LongFunction<Object> valueFunc) {
this.sizeFunc = sizeFunc;
this.valueFunc = valueFunc;
public List(Object sizeFunc,
Object valueFunc) {
this.sizeFunc = VirtDataConversions.adaptFunction(sizeFunc, LongToIntFunction.class);
this.valueFunc = VirtDataConversions.adaptFunction(valueFunc, LongFunction.class, Object.class);
}
@Override

View File

@ -4,13 +4,10 @@ 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.VirtDataConversions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.LongFunction;
import java.util.function.LongUnaryOperator;
/**
* Create a List from a long input based on a set of provided functions.
@ -25,36 +22,19 @@ import java.util.function.LongUnaryOperator;
@ThreadSafeMapper
public class ListFunctions implements LongFunction<java.util.List<Object>> {
private final java.util.List<LongFunction<? extends Object>> valueFuncs;
private final java.util.List<LongFunction> valueFuncs;
private final int size;
@Example({
"ListFunctions(NumberNameToString(),NumberNameToString(),NumberNameToString())",
"Create a list of object values of each function output. Produces values like ['one','one','one']"
})
public ListFunctions(LongFunction<? extends Object>... funcs) {
this.valueFuncs = Arrays.asList(funcs);
public ListFunctions(Object... funcs) {
this.valueFuncs = VirtDataConversions.adaptFunctionList(funcs, LongFunction.class, Object.class);
this.size = valueFuncs.size();
}
public ListFunctions(LongUnaryOperator... funcs) {
List<LongFunction<?>> building = new ArrayList<>(funcs.length);
for (LongUnaryOperator func : funcs) {
building.add(func::applyAsLong);
}
this.valueFuncs = building;
this.size = building.size();
}
public ListFunctions(Function<Long, Object>... funcs) {
List<LongFunction<?>> building = new ArrayList<>(funcs.length);
for (Function<Long, Object> func : funcs) {
building.add(func::apply);
}
this.valueFuncs = building;
this.size = building.size();
}
@Override
public java.util.List<Object> apply(long value) {
java.util.List<Object> list = new ArrayList<>(size);

View File

@ -4,6 +4,7 @@ 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.VirtDataConversions;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_long.Hash;
import java.util.ArrayList;
@ -26,7 +27,7 @@ import java.util.function.LongUnaryOperator;
@ThreadSafeMapper
public class ListHashed implements LongFunction<List<Object>> {
private final List<LongFunction<? extends Object>> valueFuncs;
private final List<LongFunction> valueFuncs;
private final int size;
private final Hash hasher = new Hash();
@ -34,29 +35,11 @@ public class ListHashed implements LongFunction<List<Object>> {
"ListHashed(ToString(), WeightedStrings('text:1'))",
"Create a hash list of object values of each function output. ListHashed output ['2945182322382062539','text']"
})
public ListHashed(LongFunction<? extends Object>... funcs) {
this.valueFuncs = Arrays.asList(funcs);
public ListHashed(Object... funcs) {
this.valueFuncs = VirtDataConversions.adaptFunctionList(funcs, LongFunction.class, Object.class);
this.size = valueFuncs.size();
}
public ListHashed(LongUnaryOperator... funcs) {
List<LongFunction<?>> building = new ArrayList<>(funcs.length);
for (LongUnaryOperator func : funcs) {
building.add(func::applyAsLong);
}
this.valueFuncs = building;
this.size = building.size();
}
public ListHashed(Function<Long, Object>... funcs) {
List<LongFunction<?>> building = new ArrayList<>(funcs.length);
for (Function<Long, Object> func : funcs) {
building.add(func::apply);
}
this.valueFuncs = building;
this.size = building.size();
}
@Override
public List<Object> apply(long value) {
long hash = value;

View File

@ -4,9 +4,9 @@ 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.VirtDataConversions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.LongFunction;
@ -27,7 +27,7 @@ import java.util.function.LongUnaryOperator;
@ThreadSafeMapper
public class ListSized implements LongFunction<List<Object>> {
private final List<LongFunction<? extends Object>> valueFuncs;
private final List<LongFunction> valueFuncs;
private final LongToIntFunction sizeFunc;
@Example({
@ -36,27 +36,9 @@ public class ListSized implements LongFunction<List<Object>> {
"end of the list size functions",
"ListSized output ['one','one','text','text','text']"
})
public ListSized(LongToIntFunction sizeFunc, LongFunction<? extends Object>... funcs) {
this.sizeFunc = sizeFunc;
this.valueFuncs = Arrays.asList(funcs);
}
public ListSized(LongToIntFunction sizeFunc, LongUnaryOperator... funcs) {
List<LongFunction<?>> building = new ArrayList<>(funcs.length);
for (LongUnaryOperator func : funcs) {
building.add(func::applyAsLong);
}
this.sizeFunc = sizeFunc;
this.valueFuncs = building;
}
public ListSized(LongToIntFunction sizeFunc, Function<Long, Object>... funcs) {
List<LongFunction<?>> building = new ArrayList<>(funcs.length);
for (Function<Long, Object> func : funcs) {
building.add(func::apply);
}
this.sizeFunc = sizeFunc;
this.valueFuncs = building;
public ListSized(Object sizeFunc, Object... funcs) {
this.sizeFunc = VirtDataConversions.adaptFunction(sizeFunc, LongToIntFunction.class);
this.valueFuncs = VirtDataConversions.adaptFunctionList(funcs, LongFunction.class, Object.class);
}
@Override

View File

@ -4,6 +4,7 @@ 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.VirtDataConversions;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_long.Hash;
import java.util.ArrayList;
@ -27,7 +28,7 @@ import java.util.function.LongUnaryOperator;
@ThreadSafeMapper
public class ListSizedHashed implements LongFunction<List<Object>> {
private final List<LongFunction<? extends Object>> valueFuncs;
private final List<LongFunction> valueFuncs;
private final Hash hasher = new Hash();
private final LongToIntFunction sizeFunc;
@ -37,27 +38,9 @@ public class ListSizedHashed implements LongFunction<List<Object>> {
"end of the list size functions",
"ListSizedHashed output ['2945182322382062539', 'text', '37945690212757860', '287864597160630738', '3299224200079606887']"
})
public ListSizedHashed(LongToIntFunction sizeFunc, LongFunction<? extends Object>... funcs) {
this.sizeFunc = sizeFunc;
this.valueFuncs = Arrays.asList(funcs);
}
public ListSizedHashed(LongToIntFunction sizeFunc, LongUnaryOperator... funcs) {
List<LongFunction<?>> building = new ArrayList<>(funcs.length);
for (LongUnaryOperator func : funcs) {
building.add(func::applyAsLong);
}
this.sizeFunc = sizeFunc;
this.valueFuncs = building;
}
public ListSizedHashed(LongToIntFunction sizeFunc, Function<Long,Object>... funcs) {
List<LongFunction<?>> building = new ArrayList<>(funcs.length);
for (Function<Long,Object> func : funcs) {
building.add(func::apply);
}
this.sizeFunc = sizeFunc;
this.valueFuncs = building;
public ListSizedHashed(Object sizeFunc, Object... funcs) {
this.sizeFunc = VirtDataConversions.adaptFunction(sizeFunc, LongToIntFunction.class);
this.valueFuncs = VirtDataConversions.adaptFunctionList(funcs, LongFunction.class, Object.class);
}
@Override

View File

@ -4,6 +4,7 @@ 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.VirtDataConversions;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_long.Hash;
import java.util.ArrayList;
@ -27,42 +28,16 @@ import java.util.function.LongUnaryOperator;
@ThreadSafeMapper
public class ListSizedStepped implements LongFunction<List<Object>> {
private final List<LongFunction<? extends Object>> valueFuncs;
private final List<LongFunction> valueFuncs;
private final LongToIntFunction sizeFunc;
@Example({
"ListFunctions(NumberNameToString(),NumberNameToString())",
"Create a list of ['one','one']"
})
public ListSizedStepped(LongToIntFunction sizeFunc, LongFunction<? extends Object>... funcs) {
this.sizeFunc = sizeFunc;
this.valueFuncs = Arrays.asList(funcs);
}
@Example({
"ListFunctions(NumberNameToString(),NumberNameToString())",
"Create a list of ['one','one']"
})
public ListSizedStepped(LongToIntFunction sizeFunc, LongUnaryOperator... funcs) {
List<LongFunction<?>> building = new ArrayList<>(funcs.length);
for (LongUnaryOperator func : funcs) {
building.add(func::applyAsLong);
}
this.sizeFunc = sizeFunc;
this.valueFuncs = building;
}
@Example({
"ListFunctions(NumberNameToString(),NumberNameToString())",
"Create a list of ['one','one']"
})
public ListSizedStepped(LongToIntFunction sizeFunc, Function<Long,Object>... funcs) {
List<LongFunction<?>> building = new ArrayList<>(funcs.length);
for (Function<Long,Object> func : funcs) {
building.add(func::apply);
}
this.sizeFunc = sizeFunc;
this.valueFuncs = building;
public ListSizedStepped(Object sizeFunc, Object... funcs) {
this.sizeFunc = VirtDataConversions.adaptFunction(sizeFunc,LongToIntFunction.class);
this.valueFuncs = VirtDataConversions.adaptFunctionList(funcs, LongFunction.class, Object.class);
}
@Override

View File

@ -4,6 +4,7 @@ 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.VirtDataConversions;
import java.util.ArrayList;
import java.util.Arrays;
@ -25,44 +26,18 @@ import java.util.function.LongUnaryOperator;
@ThreadSafeMapper
public class ListStepped implements LongFunction<List<Object>> {
private final List<LongFunction<? extends Object>> valueFuncs;
private final List<LongFunction> valueFuncs;
private final int size;
@Example({
"ListFunctions(NumberNameToString(),NumberNameToString())",
"Create a list of ['one','one']"
})
public ListStepped(LongFunction<? extends Object>... funcs) {
this.valueFuncs = Arrays.asList(funcs);
public ListStepped(Object... funcs) {
this.valueFuncs = VirtDataConversions.adaptFunctionList(funcs,LongFunction.class,Object.class);
this.size = valueFuncs.size();
}
@Example({
"ListFunctions(NumberNameToString(),NumberNameToString())",
"Create a list of ['one','one']"
})
public ListStepped(LongUnaryOperator... funcs) {
List<LongFunction<?>> building = new ArrayList<>(funcs.length);
for (LongUnaryOperator func : funcs) {
building.add(func::applyAsLong);
}
this.valueFuncs = building;
this.size = building.size();
}
@Example({
"ListFunctions(NumberNameToString(),NumberNameToString())",
"Create a list of ['one','one']"
})
public ListStepped(Function<Long,Object>... funcs) {
List<LongFunction<?>> building = new ArrayList<>(funcs.length);
for (Function<Long,Object> func : funcs) {
building.add(func::apply);
}
this.valueFuncs = building;
this.size = building.size();
}
@Override
public List<Object> apply(long value) {
List<Object> list = new ArrayList<>(size);

View File

@ -2,7 +2,7 @@ package io.nosqlbench.virtdata.library.basics.shared.from_long.to_string;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import io.nosqlbench.virtdata.api.bindings.VirtDataFunctions;
import io.nosqlbench.virtdata.api.bindings.VirtDataConversions;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
@ -45,7 +45,7 @@ public class Template implements LongFunction<String> {
private final String rawTemplate;
private LongUnaryOperator iterOp;
private String[] literals;
private LongFunction<?>[] adaptedFuncs;
private List<LongFunction> functions;
@Example({"Template('{}-{}',Add(10),Hash())","concatenate input+10, '-', and a pseudo-random long"})
public Template(String template, Object...funcs) {
@ -54,21 +54,29 @@ public class Template implements LongFunction<String> {
@Example({"Template(true, '{}-{}', Add(10),Hash())", "throws an error, as the Add(10) function causes a narrowing conversion for a long input"})
public Template(boolean truncate, String template, Object... funcs) {
this.adaptedFuncs = adapt(funcs, truncate);
this.functions = functions = VirtDataConversions.adaptFunctionList(funcs, LongFunction.class, Object.class);
this.rawTemplate = template;
this.literals = parseTemplate(template, funcs.length);
}
private LongFunction<?>[] adapt(Object[] funcs, boolean truncate) {
List<LongFunction<?>> adapted = new ArrayList<>();
for (Object func : funcs) {
LongFunction lf= VirtDataFunctions.adapt(func, LongFunction.class, Object.class, truncate);
adapted.add(lf);
private LongFunction<?>[] adapt(Object[] funcs) {
LongFunction<?>[] aryFuncs = new LongFunction[funcs.length];
for (int i = 0; i < funcs.length; i++) {
aryFuncs[i] = VirtDataConversions.adaptFunction(funcs[i],LongFunction.class, Object.class);
}
return adapted.toArray(new LongFunction<?>[0]);
return aryFuncs;
}
// private LongFunction<?>[] adapt(Object[] funcs, boolean truncate) {
//
// List<LongFunction<?>> adapted = new ArrayList<>();
// for (Object func : funcs) {
// LongFunction lf= VirtDataConversions.adapt(func, LongFunction.class, Object.class);
// adapted.add(lf);
// }
// return adapted.toArray(new LongFunction<?>[0]);
// }
//
/**
* If an operator is provided, it is used to change the function input value in an additional way before each function.
@ -134,9 +142,9 @@ public class Template implements LongFunction<String> {
buffer.setLength(0);
buffer.append(literals[0]);
if (literals.length > 1) {
for (int i = 0; i < adaptedFuncs.length; i++) {
for (int i = 0; i < functions.size(); i++) {
long input = iterOp != null ? iterOp.applyAsLong(value + i) : value + i;
String genString = String.valueOf(adaptedFuncs[i].apply(input));
String genString = String.valueOf(functions.get(i).apply(input));
buffer.append(genString);
buffer.append(literals[i + 1]);
}

View File

@ -3,13 +3,16 @@ package io.nosqlbench.virtdata.library.basics.shared.from_long.to_collection;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_int.HashRange;
import org.junit.Test;
import java.util.function.LongFunction;
import static org.assertj.core.api.Assertions.assertThat;
public class ListTest {
@Test
public void testList() {
io.nosqlbench.virtdata.library.basics.shared.from_long.to_collection.List lf = new io.nosqlbench.virtdata.library.basics.shared.from_long.to_collection.List(new HashRange(2, 3), (l) -> "_" + l);
io.nosqlbench.virtdata.library.basics.shared.from_long.to_collection.List lf =
new io.nosqlbench.virtdata.library.basics.shared.from_long.to_collection.List(new HashRange(2, 3), (LongFunction) (l) -> "_" + l);
java.util.List<Object> l1 = lf.apply(2L);
assertThat(l1).containsExactly("_2","_3","_4");
}