Merge pull request #1276 from nosqlbench/nosqlbench-1273-poplist

make list template work for all types, and go faster
This commit is contained in:
Jonathan Shook 2023-05-18 15:17:20 -05:00 committed by GitHub
commit 97e1bb032d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 24 deletions

View File

@ -159,5 +159,50 @@ public class ParsedOpTest {
assertThat(objects).isEqualTo(new Object[]{"three", "three", 3L, 3L}); assertThat(objects).isEqualTo(new Object[]{"three", "three", 3L, 3L});
} }
@Test
public void testLayeredListBinder() {
ParsedOp pc = new ParsedOp(
new OpData().applyFields(
Map.of(
"op", Map.of(
"alist", List.of(
List.of(
"item1",
"item2-{dyna1}"
),
Map.of(
"akey", "avalue",
"akey2", "a {dyna1} value2"
)
)
),
"bindings", Map.of(
"dyna1", "NumberNameToString()"
)
)
),
ConfigModel.of(ParsedOpTest.class)
.add(Param.defaultTo("testcfg", "testval"))
.asReadOnly()
.apply(Map.of()),
List.of(),
NBLabeledElement.forMap(Map.of())
);
Map<String, Object> result = pc.getTemplateMap().apply(1);
assertThat(result).isEqualTo(
Map.of(
"alist", List.of(
List.of("item1", "item2-one"),
Map.of(
"akey", "avalue",
"akey2", "a one value2"
)
)
)
);
}
} }

View File

@ -19,36 +19,58 @@ package io.nosqlbench.engine.api.templating;
import io.nosqlbench.virtdata.core.templates.CapturePoint; import io.nosqlbench.virtdata.core.templates.CapturePoint;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.LongFunction; import java.util.function.LongFunction;
public class ParsedTemplateList implements LongFunction<List<?>> { public class ParsedTemplateList implements LongFunction<List<?>> {
private final List<Object> protolist = new ArrayList<>(); private final Object[] protolist;
private final int[] dynamic_idx; private final int[] dynamic_idx;
private final LongFunction<?>[] functions; private final LongFunction<?>[] functions;
private final List<CapturePoint> captures = new ArrayList<>(); private final List<CapturePoint> captures = new ArrayList<>();
public ParsedTemplateList(List<Object> sublist, Map<String, String> bindings, List<Map<String, Object>> cfgsources) { public ParsedTemplateList(List<Object> list, Map<String, String> bindings, List<Map<String, Object>> cfgsources) {
List<LongFunction<?>> funcs = new ArrayList<>(); List<LongFunction<?>> funcs = new ArrayList<>();
List<Integer> dindexes = new ArrayList<>(); List<Integer> dindexes = new ArrayList<>();
protolist = new Object[list.size()];
for (int i = 0; i < sublist.size(); i++) { for (int i = 0; i < list.size(); i++) {
Object item = sublist.get(i); Object item = list.get(i);
Templatizer.Result result = Templatizer.make(bindings, item, null, cfgsources); Templatizer.Result result = Templatizer.make(bindings, item, null, cfgsources);
this.captures.addAll(result.getCaptures()); this.captures.addAll(result.getCaptures());
if (item instanceof String string) {
switch (result.getType()) { switch (result.getType()) {
case literal: case literal:
protolist.add(result.getValue()); protolist[i]=string;
break; break;
case bindref: case bindref:
case concat: case concat:
protolist.add(null);
funcs.add(result.getFunction()); funcs.add(result.getFunction());
dindexes.add(i); dindexes.add(i);
break;
} }
} else if (item instanceof List sublist) {
ParsedTemplateList listTemplate = new ParsedTemplateList(sublist, bindings, cfgsources);
if (listTemplate.isStatic()) {
protolist[i]=sublist;
} else {
funcs.add(result.getFunction());
dindexes.add(i);
}
} else if (item instanceof Map submap) {
ParsedTemplateMap mapTemplate = new ParsedTemplateMap("anonymous", submap, bindings, cfgsources);
if (mapTemplate.isStatic()) {
protolist[i]=submap;
} else {
funcs.add(result.getFunction());
dindexes.add(i);
}
} else {
protolist[i]=item;
}
} }
this.dynamic_idx = dindexes.stream().mapToInt(Integer::intValue).toArray(); this.dynamic_idx = dindexes.stream().mapToInt(Integer::intValue).toArray();
this.functions = funcs.toArray(new LongFunction<?>[0]); this.functions = funcs.toArray(new LongFunction<?>[0]);
@ -57,12 +79,12 @@ public class ParsedTemplateList implements LongFunction<List<?>> {
@Override @Override
public List<?> apply(long value) { public List<?> apply(long value) {
List<Object> list = new ArrayList<>(protolist); Object[] resultAry=new Object[protolist.length];
System.arraycopy(protolist,0,resultAry,0,protolist.length);
for (int i = 0; i < dynamic_idx.length; i++) { for (int i = 0; i < dynamic_idx.length; i++) {
Object obj = functions[i].apply(value); resultAry[dynamic_idx[i]]=functions[i].apply(value);
list.set(dynamic_idx[i], obj);
} }
return list; return Arrays.asList(resultAry);
} }
public boolean isStatic() { public boolean isStatic() {