nosqlbench-2133 Map functions which work in nb4 appear to break in nb5

This commit is contained in:
Jonathan Shook 2025-01-10 12:38:06 -06:00
parent f35b0ca124
commit 144d7dea65
2 changed files with 50 additions and 12 deletions

View File

@ -18,6 +18,7 @@ package io.nosqlbench.virtdata.library.basics.shared.from_long.to_collection;
import io.nosqlbench.virtdata.api.annotations.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.function.LongFunction;
import java.util.function.LongToIntFunction;
@ -61,12 +62,23 @@ public class Map implements LongFunction<java.util.Map<Object, Object>> {
"create a map of size 2, with a specific function for each key and each value"})
@SafeVarargs
public Map(LongFunction<Object>... objfuncs) {
this.mode = Mode.Tuples;
if ((objfuncs.length%2)!=0) {
throw new RuntimeException("An even number of functions must be provided.");
Object testValue = objfuncs[0].apply(0L);
if (testValue instanceof Number n) {
LongFunction<Object>[] finalObjfuncs = objfuncs;
this.sizeFunc= l -> ((Number) finalObjfuncs[0].apply(l)).intValue();
objfuncs = Arrays.copyOfRange(objfuncs, 1, objfuncs.length);
this.mode=Mode.VarSized;
} else {
throw new RuntimeException("An even number of functions must be provided, unless "
+ "the first one produces a numeric value.");
}
} else {
this.mode = Mode.Tuples;
int size = objfuncs.length/2;
sizeFunc=(l) -> size;
}
int size = objfuncs.length / 2;
sizeFunc=(l) -> size;
keyFuncs = new LongFunction[size];
valueFuncs = new LongFunction[size];
for (int i = 0; i < size; i++) {

View File

@ -16,9 +16,14 @@
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_collection;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.function.LongFunction;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class MapTest {
@ -32,26 +37,47 @@ public class MapTest {
@Test
public void testStringMap() {
StringMap sm = new StringMap((s)->2,(k)->k,(v)->v);
StringMap sm = new StringMap((s) -> 2, (k) -> k, (v) -> v);
java.util.Map<String, String> m2 = sm.apply(11L);
assertThat(m2).containsOnlyKeys("11","12");
assertThat(m2).containsValues("11","12");
assertThat(m2).containsOnlyKeys("11", "12");
assertThat(m2).containsValues("11", "12");
}
@Test
public void testMapTuple() {
Map mf = new Map(s1 -> (int) s1, k2 -> (int) k2, s2 -> (int) s2, k2 -> (int)k2);
Map mf = new Map(s1 -> (int) s1, k2 -> (int) k2, s2 -> (int) s2, k2 -> (int) k2);
java.util.Map<Object, Object> mt = mf.apply(37L);
assertThat(mt).containsOnlyKeys(37,38);
assertThat(mt).containsValues(37,38);
assertThat(mt).containsOnlyKeys(37, 38);
assertThat(mt).containsValues(37, 38);
}
@Test
public void testStringMapTuple() {
StringMap mf = new StringMap(s1 -> (int) s1, k2 -> (int) k2, s2 -> (int) s2, k2 -> (int)k2);
StringMap mf =
new StringMap(s1 -> (int) s1, k2 -> (int) k2, s2 -> (int) s2, k2 -> (int) k2);
java.util.Map<String, String> mt = mf.apply(37L);
assertThat(mt).containsOnlyKeys("37","38");
assertThat(mt).containsValues("37","38");
assertThat(mt).containsOnlyKeys("37", "38");
assertThat(mt).containsValues("37", "38");
}
@Test
public void testLongFunctionsOnlyWithOddArity() {
LongFunction<Object> sizeFunc = l -> (double) (l % 5);
LongFunction<Object> keyfunc = String::valueOf;
LongFunction<Object> valueFunc = String::valueOf;
Map map = new Map(sizeFunc, keyfunc, valueFunc);
java.util.Map<Object, Object> apply = map.apply(3L);
assertThat(apply).isEqualTo(java.util.Map.of("3", "3", "4", "4", "5", "5"));
}
@Test
public void testLongFunctionsOnlyWithInvalidSizer() {
LongFunction<Object> sizeFunc = l -> Instant.ofEpochMilli(1L);
LongFunction<Object> keyfunc = String::valueOf;
LongFunction<Object> valueFunc = String::valueOf;
assertThatThrownBy(() -> new Map(sizeFunc, keyfunc, valueFunc)).hasMessageContaining("An "
+
"even number of functions must be provided, unless the first one produces a numeric value");
}
}