notes: HashRange and HashInterval do (incl,incl) and (incl,excl) respectively

This commit is contained in:
Jonathan Shook 2020-06-17 14:43:12 -05:00
parent 244217c6a7
commit d79ff1fa9e
11 changed files with 191 additions and 17 deletions

View File

@ -0,0 +1,45 @@
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_int;
import io.nosqlbench.nb.api.errors.BasicError;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_long.Hash;
import java.util.function.LongToIntFunction;
@ThreadSafeMapper
public class HashInterval implements LongToIntFunction {
private final long minValue;
private final long width;
private final Hash hash = new Hash();
/**
* Create a hash interval based on a minimum value of 0 and a specified width.
* @param width The maximum value, which is excluded.
*/
@Example({"HashInterval(4)","return values which could include 0, 1, 2, 3, but not 4"})
public HashInterval(int width) {
this.width=width;
this.minValue=0L;
}
/**
* Create a hash interval
* @param minIncl The minimum value, which is included
* @param maxExcl The maximum value, which is excluded
*/
@Example({"HashInterval(2,5)","return values which could include 2, 3, 4, but not 5"})
public HashInterval(int minIncl, int maxExcl) {
if (maxExcl<=minIncl) {
throw new BasicError("HashInterval must have min and max value in that order, where the min is less than the max.");
}
this.minValue = minIncl;
this.width = (maxExcl - minIncl);
}
@Override
public int applyAsInt(long operand) {
return (int) ((minValue + (hash.applyAsLong(operand) % width)) & Integer.MAX_VALUE);
}
}

View File

@ -1,5 +1,6 @@
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_int;
import io.nosqlbench.nb.api.errors.BasicError;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_long.Hash;
@ -22,7 +23,7 @@ public class HashRange implements LongToIntFunction {
@Example({"HashRange(35L,39L)","map the input to a number in the range 35-38, inclusive, of type int"})
public HashRange(int minValue, int maxValue) {
if (maxValue<minValue) {
throw new RuntimeException("HashRange must have min and max value in that order.");
throw new BasicError("HashRange must have min and max value in that order.");
}
this.minValue = minValue;
this.width = (maxValue - minValue) +1;

View File

@ -0,0 +1,54 @@
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_long;
import io.nosqlbench.nb.api.errors.BasicError;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.function.LongUnaryOperator;
/**
* Return a value within a range, pseudo-randomly, using interval semantics,
* where the range of values return does not include the last value.
* This function behaves exactly like HashRange except for the exclusion
* of the last value. This allows you to stack intervals using known
* reference points without duplicating or skipping any given value.
*
* You can specify hash intervals as small as a single-element range, like
* (5,6), or as wide as the relevant data type allows.
*/
@ThreadSafeMapper
public class HashInterval implements LongUnaryOperator {
private final long minValue;
private final long width;
private final Hash hash = new Hash();
/**
* Create a hash interval based on a minimum value of 0 and a specified width.
* @param width The maximum value, which is excluded.
*/
@Example({"HashInterval(4L)","return values which could include 0L, 1L, 2L, 3L, but not 4L"})
public HashInterval(long width) {
this.minValue=0L;
this.width=width;
}
/**
* Create a hash interval
* @param minIncl The minimum value, which is included
* @param maxExcl The maximum value, which is excluded
*/
@Example({"HashInterval(2L,5L)","return values which could include 2L, 3L, 4L, but not 5L"})
public HashInterval(long minIncl, long maxExcl) {
if (maxExcl<=minIncl) {
throw new BasicError("HashInterval must have min and max value in that order, where the min is less than the max.");
}
this.minValue = minIncl;
this.width = (maxExcl - minIncl);
}
@Override
public long applyAsLong(long operand) {
return minValue + (hash.applyAsLong(operand) % width);
}
}

View File

@ -1,5 +1,6 @@
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_long;
import io.nosqlbench.nb.api.errors.BasicError;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.function.LongUnaryOperator;
@ -26,7 +27,7 @@ public class HashRange implements LongUnaryOperator {
public HashRange(long minValue, long maxValue) {
if (maxValue<minValue) {
throw new RuntimeException("HashRange must have min and max value in that order.");
throw new BasicError("HashRange must have min and max value in that order.");
}
this.minValue = minValue;
this.width = (maxValue - minValue)+1;

View File

@ -19,8 +19,9 @@
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_string;
import io.nosqlbench.nb.api.content.NBIO;
import io.nosqlbench.nb.api.errors.BasicError;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_int.HashRange;
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_int.HashInterval;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
@ -35,7 +36,7 @@ import java.util.function.LongFunction;
@ThreadSafeMapper
public class HashedLineToString implements LongFunction<String> {
private final static Logger logger = LogManager.getLogger(HashedLineToString.class);
private final HashRange indexRange;
private final HashInterval indexRange;
private List<String> lines = new ArrayList<>();
@ -44,7 +45,10 @@ public class HashedLineToString implements LongFunction<String> {
public HashedLineToString(String filename) {
this.filename = filename;
this.lines = NBIO.readLines(filename);
this.indexRange = new HashRange(0, lines.size() - 2);
if (lines.size()<1) {
throw new BasicError("Read " + lines.size() + " lines from " + filename + ", empty files are not supported");
}
this.indexRange = new HashInterval(0, lines.size());
}
public String toString() {

View File

@ -0,0 +1,45 @@
package io.nosqlbench.virtdata.library.basics.shared.unary_int;
import io.nosqlbench.nb.api.errors.BasicError;
import io.nosqlbench.virtdata.api.annotations.Example;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.function.IntUnaryOperator;
@ThreadSafeMapper
public class HashInterval implements IntUnaryOperator {
private final int minValue;
private final int width;
private final Hash hash = new Hash();
/**
* Create a hash interval based on a minimum value of 0 and a specified width.
* @param width The maximum value, which is excluded.
*/
@Example({"HashInterval(4)","return values which could include 0, 1, 2, 3, but not 4"})
public HashInterval(int width) {
this.minValue=0;
this.width=width;
}
/**
* Create a hash interval
* @param minIncl The minimum value, which is included
* @param maxExcl The maximum value, which is excluded
*/
@Example({"HashInterval(2,5)","return values which could include 2, 3, 4, but not 5"})
public HashInterval(int minIncl, int maxExcl) {
if (maxExcl<=minIncl) {
throw new BasicError("HashInterval must have min and max value in that order, where the min is less than the max.");
}
this.minValue = minIncl;
this.width = (maxExcl - minIncl);
}
@Override
public int applyAsInt(int operand) {
return minValue + (hash.applyAsInt(operand) & width);
}
}

View File

@ -1,5 +1,6 @@
package io.nosqlbench.virtdata.library.basics.shared.unary_int;
import io.nosqlbench.nb.api.errors.BasicError;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.function.IntUnaryOperator;
@ -18,7 +19,7 @@ public class HashRange implements IntUnaryOperator {
public HashRange(int minValue, int maxValue) {
if (maxValue<minValue) {
throw new RuntimeException("HashRange must have min and max value in that order.");
throw new BasicError("HashRange must have min and max value in that order.");
}
this.minValue = minValue;
this.width = (maxValue - minValue) +1;

View File

@ -9,9 +9,9 @@ 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(1, 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), (l) -> "_" + l);
java.util.List<Object> l1 = lf.apply(2L);
assertThat(l1).containsExactly("_2","_3");
assertThat(l1).containsExactly("_2","_3","_4");
}
@Test

View File

@ -0,0 +1,22 @@
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_long;
import io.nosqlbench.nb.api.errors.BasicError;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class HashIntervalTest {
@Test
public void testBasicRange() {
HashInterval hi = new HashInterval(3L, 5L);
long r1 = hi.applyAsLong(43L);
assertThat(r1).isEqualTo(4L);
}
@Test(expected = BasicError.class)
public void testRangeError() {
HashInterval hi = new HashInterval(3L, 3L);
}
}

View File

@ -13,6 +13,7 @@ public class HashedRangeToLongListTest {
public void longListRangeTest() {
HashedRangeToLongList gener = new HashedRangeToLongList(3, 6, 9, 12);
for (int i = 0; i < 100; i++) {
System.out.println("long list range test size: " + i);
List<Long> list= gener.apply(i);
assertThat(list.size()).isBetween(9,12);
for (Long longVal : list) {

View File

@ -13,11 +13,11 @@ public class RangeTests {
@Test
public void testHashRange() {
HashRange hr = new HashRange(10,13);
assertThat(hr.applyAsLong(0)).isEqualTo(11);
assertThat(hr.applyAsLong(10)).isEqualTo(12);
assertThat(hr.applyAsLong(100)).isEqualTo(12);
assertThat(hr.applyAsLong(1000)).isEqualTo(11);
assertThat(hr.applyAsLong(10000)).isEqualTo(10);
assertThat(hr.applyAsLong(0)).isEqualTo(13);
assertThat(hr.applyAsLong(10)).isEqualTo(11);
assertThat(hr.applyAsLong(100)).isEqualTo(13);
assertThat(hr.applyAsLong(1000)).isEqualTo(13);
assertThat(hr.applyAsLong(10000)).isEqualTo(11);
}
@Test
@ -32,10 +32,10 @@ public class RangeTests {
@Test
public void testAddHashRange() {
AddHashRange ahr = new AddHashRange(14,17);
assertThat(ahr.applyAsLong(1)).isEqualTo(16);
assertThat(ahr.applyAsLong(2)).isEqualTo(16);
assertThat(ahr.applyAsLong(3)).isEqualTo(18);
assertThat(ahr.applyAsLong(4)).isEqualTo(20);
assertThat(ahr.applyAsLong(1)).isEqualTo(17);
assertThat(ahr.applyAsLong(2)).isEqualTo(17);
assertThat(ahr.applyAsLong(3)).isEqualTo(20);
assertThat(ahr.applyAsLong(4)).isEqualTo(19);
assertThat(ahr.applyAsLong(5)).isEqualTo(19);
}