DO-NOT-MERGE: this needs some repairs first

This commit is contained in:
Jonathan Shook
2023-05-30 23:14:29 -05:00
committed by jeffbanks
parent 9518b622bf
commit fa0afdd388
30 changed files with 314 additions and 203 deletions

View File

@@ -0,0 +1,59 @@
/*
* Copyright (c) 2023 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.nosqlbench.virtdata.library.basics.shared.conversions.from_long;
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.LongToDoubleFunction;
/**
* Create a double by converting values. This function works in the following modes:
* <UL>
* <LI>If a double is provided, then the long input is scaled to be between 0.0f and the value provided,
* as a fraction of the highest possible long.</LI>
* <LI>If any other type of number is provided, then this function returns the equivalent double value.</LI>
* <LI>Otherwise, the input is assumed to be a function which takes a long input, and, if necessary,
* adapts to return a double with an appropriate type conversion.</LI>
* </UL>
*/
@Categories(Category.conversion)
@ThreadSafeMapper
public class ToDouble implements LongToDoubleFunction {
private final LongToDoubleFunction func;
ToDouble(Object func) {
if (func instanceof Double aDouble) {
double scale = aDouble.doubleValue();
this.func = l -> (double) (l % scale);
} else if (func instanceof Number number) {
final double aDouble = number.doubleValue();
this.func = l -> aDouble;
} else {
this.func = VirtDataConversions.adaptFunction(func, LongToDoubleFunction.class);
}
}
@Override
public double applyAsDouble(long value) {
return func.applyAsDouble(value);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 nosqlbench
* Copyright (c) 2023 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,23 +19,41 @@ package io.nosqlbench.virtdata.library.basics.shared.conversions.from_long;
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;
@ThreadSafeMapper
@Categories({Category.conversion})
public class ToFloat implements LongFunction<Float> {
private final long scale;
public ToFloat(long scale) {
this.scale = scale;
}
public ToFloat() {
this.scale = Long.MAX_VALUE;
/**
* Create a float by converting values. This function works in the following modes:
* <UL>
* <LI>If a float is provided, then the long input is scaled to be between 0.0f and the value provided,
* as a fraction of the highest possible long.</LI>
* <LI>If any other type of number is provided, then this function returns the equivalent float value.</LI>
* <LI>Otherwise, the input is assumed to be a function which takes a long input, and, if necessary,
* adapts to return a float with an appropriate type conversion.</LI>
* </UL>
*/
@Categories(Category.conversion)
@ThreadSafeMapper
public class ToFloat implements LongFunction<Float> {
private final LongFunction<Float> func;
ToFloat(Object func) {
if (func instanceof Float afloat) {
float scale = afloat.floatValue();
this.func = l -> (float) (l % scale);
} else if (func instanceof Number number) {
final float afloat = number.floatValue();
this.func = l -> afloat;
} else {
this.func = VirtDataConversions.adaptFunction(func, LongFunction.class, Float.class);
}
}
@Override
public Float apply(long input) {
return (float)(input % scale);
public Float apply(long value) {
return func.apply(value);
}
}

View File

@@ -1,50 +0,0 @@
/*
* Copyright (c) 2023 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.nosqlbench.virtdata.library.basics.shared.from_long;
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;
/**
* Wrap any function producing a valid numeric value as a float.
*/
@Categories(Category.conversion)
@ThreadSafeMapper
public class ToFloat implements LongFunction<Float> {
private final LongFunction<Float> func;
ToFloat(Object funcOrValue) {
if (funcOrValue instanceof Number number) {
final float afloat = number.floatValue();
this.func = l -> afloat;
} else {
this.func = VirtDataConversions.adaptFunction(funcOrValue,LongFunction.class,Float.class);
}
}
@Override
public Float apply(long value) {
return func.apply(value);
}
}

View File

@@ -59,10 +59,6 @@ public class ListSized implements LongFunction<List<Object>> {
}
this.valueFuncs = VirtDataConversions.adaptFunctionList(funcs, LongFunction.class, Object.class);
}
public ListSized(int size, Object... funcs) {
this.sizeFunc = (s) -> size;
this.valueFuncs = VirtDataConversions.adaptFunctionList(funcs, LongFunction.class, Object.class);
}
@Override
public List<Object> apply(long value) {

View File

@@ -61,11 +61,6 @@ public class ListSizedHashed implements LongFunction<List<Object>> {
this.valueFuncs = VirtDataConversions.adaptFunctionList(funcs, LongFunction.class, Object.class);
}
public ListSizedHashed(int size, Object... funcs) {
this.sizeFunc = s -> size;
this.valueFuncs = VirtDataConversions.adaptFunctionList(funcs, LongFunction.class, Object.class);
}
@Override
public List<Object> apply(long value) {
int size = sizeFunc.applyAsInt(value);

View File

@@ -49,7 +49,7 @@ import java.util.function.LongToDoubleFunction;
*/
@ThreadSafeMapper
@Categories({Category.general})
public class HashedDoubleRange implements LongToDoubleFunction {
public class HashRange implements LongToDoubleFunction {
private final double min;
private final double max;
@@ -57,7 +57,7 @@ public class HashedDoubleRange implements LongToDoubleFunction {
private final static double MAX_DOUBLE_VIA_LONG = (double) Long.MAX_VALUE;
private final Hash hash = new Hash();
public HashedDoubleRange(double min, double max) {
public HashRange(double min, double max) {
this.min = min;
this.max = max;
this.interval = max - min;

View File

@@ -1,36 +0,0 @@
/*
* Copyright (c) 2022 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_double;
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.LongToDoubleFunction;
/**
* Convert the input value to a double.
*/
@ThreadSafeMapper
@Categories({Category.conversion})
public class ToDouble implements LongToDoubleFunction {
@Override
public double applyAsDouble(long value) {
return (double) value;
}
}

View File

@@ -23,33 +23,9 @@ import io.nosqlbench.virtdata.library.basics.shared.from_long.to_long.Hash;
import java.util.function.LongFunction;
/*
* <p>This simulates a uniform sample from a range of double values
* via long hashing. This function attempts to take a double
* unit interval value from a long/long division over the whole
* range of long values but via double value types, thus providing
* a very linear sample. This means that the range of double
* values to be accessed will not fall along all possible doubles,
* but will still provide suitable values for ranges close to
* high-precision points in the IEEE floating point number line.
* This suffices for most reasonable ranges in practice outside
* of scientific computing, where large exponents put adjacent
* IEEE floating point values much further apart.</p>
*
* <p>This should be consider the default double range sampling
* function for most uses, when the exponent is not needed for
* readability.</p>
*/
/**
* Return a double value within the specified range. This function
* uses an intermediate long to arrive at the sampled value before
* conversion to double, thus providing a more linear sample at the
* expense of some precision at extremely large values.
*/
@ThreadSafeMapper
@Categories({Category.general})
public class HashedFloatRange implements LongFunction<Float> {
public class HashRange implements LongFunction<Float> {
private final float min;
private final float max;
@@ -57,7 +33,7 @@ public class HashedFloatRange implements LongFunction<Float> {
private final static float MAX_FLOAT_VIA_LONG = (float) Long.MAX_VALUE;
private final Hash hash = new Hash();
public HashedFloatRange(float min, float max) {
public HashRange(float min, float max) {
this.min = min;
this.max = max;
this.interval = max - min;

View File

@@ -1,36 +0,0 @@
/*
* Copyright (c) 2022 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_float;
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.LongToDoubleFunction;
/**
* Convert the input value to a double.
*/
@ThreadSafeMapper
@Categories({Category.conversion})
public class ToDouble implements LongToDoubleFunction {
@Override
public double applyAsDouble(long value) {
return (double) value;
}
}

View File

@@ -24,9 +24,10 @@ import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.function.LongUnaryOperator;
/**
* Return a value within a range, pseudo-randomly. This is equivalent to
* returning a value with in range between 0 and some maximum value, but
* with a minimum value added.
* The various HashRange functions take an input long, hash it to a random
* long value, and then use to interpolate a fractional value between the minimum
* and maximum values. To select a specific type of HashRange function,
* simply use the same datatype in the min and max values you wish to have on output.
*
* You can specify hash ranges as small as a single-element range, like
* (5,5), or as wide as the relevant data type allows.

View File

@@ -20,7 +20,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.library.basics.shared.from_long.to_double.HashedDoubleRange;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_double.HashRange;
import java.util.ArrayList;
import java.util.List;
@@ -40,7 +40,7 @@ public class WeightedLongs implements LongFunction<Long> {
private final String valuesAndWeights;
private double[] unitWeights; // Positional weights after parsing and unit weight normalization
private double[] cumulativeWeights;
private final HashedDoubleRange unitRange = new HashedDoubleRange(0.0D, 1.0D);
private final HashRange unitRange = new HashRange(0.0D, 1.0D);
private long[] values;
@Example({"WeightedLongs('1:10;3;5;12345;1","Yield 1 62.5% of the time, 3 31.25% of the time, and 12345 6.2% of the time"})

View File

@@ -20,7 +20,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.library.basics.shared.from_long.to_double.HashedDoubleRange;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_double.HashRange;
import io.nosqlbench.virtdata.api.bindings.VirtDataFunctions;
import java.util.function.Function;
@@ -54,7 +54,7 @@ public class CoinFunc implements Function<Long, Object> {
private final double threshold;
private final LongFunction first;
private final LongFunction second;
private final HashedDoubleRange cointoss = new HashedDoubleRange(0.0d, 1.0d);
private final HashRange cointoss = new HashRange(0.0d, 1.0d);
@Example({"CoinFunc(0.15,NumberNameToString(),Combinations('A:1:B:23'))", "use the first function 15% of the time"})

View File

@@ -21,7 +21,7 @@ import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import io.nosqlbench.virtdata.library.basics.core.stathelpers.AliasSamplerDoubleInt;
import io.nosqlbench.virtdata.library.basics.core.stathelpers.EvProbD;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_double.HashedDoubleRange;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_double.HashRange;
import io.nosqlbench.virtdata.api.bindings.VirtDataFunctions;
import java.util.ArrayList;
@@ -39,7 +39,7 @@ public class WeightedFuncs implements LongFunction<Object> {
private final LongFunction<Object>[] funcs;
private final AliasSamplerDoubleInt functionSampler;
private final HashedDoubleRange unitSampler = new HashedDoubleRange(0.0d, 1.0d);
private final HashRange unitSampler = new HashRange(0.0d, 1.0d);
public WeightedFuncs(Object... weightsAndFuncs) {
List<EvProbD> probabilities = new ArrayList<>();

View File

@@ -19,7 +19,7 @@ package io.nosqlbench.virtdata.library.basics.shared.from_long.to_string;
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.library.basics.shared.from_long.to_double.HashedDoubleRange;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_double.HashRange;
import java.util.ArrayList;
import java.util.List;
@@ -39,7 +39,7 @@ public class WeightedStrings implements LongFunction<String> {
private final String valuesAndWeights;
private double[] unitWeights; // Positional weights after parsing and unit weight normalization
private double[] cumulativeWeights;
private final HashedDoubleRange unitRange = new HashedDoubleRange(0.0D, 1.0D);
private final HashRange unitRange = new HashRange(0.0D, 1.0D);
private String[] values;
public WeightedStrings(String valuesAndWeights) {

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2023 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_vector;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.List;
import java.util.function.Function;
/**
* Normalize a vector in List<Number> form, calling the appropriate conversion function
* depending on the component (Class) type of the incoming List values.
*/
@ThreadSafeMapper
@Categories(Category.experimental)
public class NormalizeVector implements Function<List,List> {
private final NormalizeDoubleVectorList ndv = new NormalizeDoubleVectorList();
private final NormalizeFloatVectorList nfv = new NormalizeFloatVectorList();
@Override
public List apply(List list) {
if (list.size()==0) {
return List.of();
} else if (list.get(0) instanceof Float) {
return nfv.apply(list);
} else if (list.get(1) instanceof Double) {
return ndv.apply(list);
} else {
throw new RuntimeException("Only Doubles and Floats are recognized.");
}
}
}

View File

@@ -20,7 +20,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.library.basics.shared.from_long.to_double.HashedDoubleRange;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_double.HashRange;
import java.util.Arrays;
import java.util.List;
@@ -58,9 +58,9 @@ public class UniformVectorSizedStepped implements LongFunction<List<Double>> {
this.funcs = new LongToDoubleFunction[dim];
for (int i = 0; i < dim; i++) {
if (i<dims.length/2) {
funcs[i]=new HashedDoubleRange(dims[i<<1].doubleValue(),dims[(i<<1)+1].doubleValue());
funcs[i]=new HashRange(dims[i<<1].doubleValue(),dims[(i<<1)+1].doubleValue());
} else {
funcs[i]=new HashedDoubleRange(0.0d,1.0d);
funcs[i]=new HashRange(0.0d,1.0d);
}
}
}

View File

@@ -19,7 +19,7 @@ package io.nosqlbench.virtdata.library.basics.shared.stateful;
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.library.basics.shared.from_long.to_double.HashedDoubleRange;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_double.HashRange;
import java.util.function.LongFunction;
@@ -34,7 +34,7 @@ public class NullOrLoad implements LongFunction<Object> {
private final String varname;
private final double ratio;
private final HashedDoubleRange rangefunc = new HashedDoubleRange(0.0D,1.0D);
private final HashRange rangefunc = new HashRange(0.0D,1.0D);
private final Load load;
public NullOrLoad(double ratio, String varname) {

View File

@@ -20,7 +20,7 @@ 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.library.basics.core.threadstate.SharedState;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_double.HashedDoubleRange;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_double.HashRange;
import java.util.function.Function;
@@ -35,7 +35,7 @@ public class NullOrPass implements Function<Object, Object> {
private final String varname;
private final double ratio;
private final HashedDoubleRange rangefunc = new HashedDoubleRange(0.0D, 1.0D);
private final HashRange rangefunc = new HashRange(0.0D, 1.0D);
public NullOrPass(double ratio, String varname) {
if (ratio < 0.0D || ratio > 1.0D) {

View File

@@ -20,7 +20,7 @@ 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.VALUE;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_double.HashedDoubleRange;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_double.HashRange;
import java.util.function.LongFunction;
@@ -35,7 +35,7 @@ public class UnsetOrLoad implements LongFunction<Object> {
private final String varname;
private final double ratio;
private final HashedDoubleRange rangefunc = new HashedDoubleRange(0.0D,1.0D);
private final HashRange rangefunc = new HashRange(0.0D,1.0D);
private final Load load;
public UnsetOrLoad(double ratio, String varname) {

View File

@@ -21,7 +21,7 @@ import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import io.nosqlbench.virtdata.api.bindings.VALUE;
import io.nosqlbench.virtdata.library.basics.core.threadstate.SharedState;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_double.HashedDoubleRange;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_double.HashRange;
import java.util.function.Function;
@@ -36,7 +36,7 @@ public class UnsetOrPass implements Function<Object,Object> {
private final String varname;
private final double ratio;
private final HashedDoubleRange rangefunc = new HashedDoubleRange(0.0D,1.0D);
private final HashRange rangefunc = new HashRange(0.0D,1.0D);
public UnsetOrPass(double ratio, String varname) {
if (ratio<0.0D || ratio >1.0D) {

View File

@@ -16,7 +16,7 @@
package io.nosqlbench.virtdata.library.basics.tests.long_double;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_double.HashedDoubleRange;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_double.HashRange;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@@ -25,7 +25,7 @@ public class HashedDoubleRangeTest {
@Test
public void testBasicRanges() {
HashedDoubleRange r = new HashedDoubleRange(0.0D, 100.0D);
HashRange r = new HashRange(0.0D, 100.0D);
for(long i=1;i<1000;i++) {
assertThat(r.applyAsDouble(i)).isBetween(0.0D,100.0D);
}