use of io.nosqlbench.virtdata.core.bindings.DataMapper in project nosqlbench by nosqlbench.
the class ParsedTemplateMap method applyTemplateFields.
// For now, we only allow bind points to reference bindings, not other op template
// fields. This seems like the saner and less confusing approach, so implementing
// op field references should be left until it is requested if at all
private void applyTemplateFields(Map<String, Object> map, Map<String, String> bindings) {
this.specmap = map;
this.bindings = bindings;
map.forEach((k, v) -> {
if (v instanceof CharSequence) {
ParsedTemplate pt = ParsedTemplate.of(((CharSequence) v).toString(), bindings);
this.captures.add(pt.getCaptures());
switch(pt.getType()) {
case literal:
statics.put(k, ((CharSequence) v).toString());
protomap.put(k, ((CharSequence) v).toString());
break;
case bindref:
String spec = pt.asBinding().orElseThrow().getBindspec();
if (spec == null) {
throw new OpConfigError("Empty binding spec for '" + k + "'");
}
Optional<DataMapper<Object>> mapper = VirtData.getOptionalMapper(spec);
dynamics.put(k, mapper.orElseThrow());
protomap.put(k, null);
break;
case concat:
StringBindings sb = new StringBindings(pt);
dynamics.put(k, sb);
protomap.put(k, null);
break;
}
} else if (v instanceof Map) {
((Map) v).keySet().forEach(smk -> {
if (!CharSequence.class.isAssignableFrom(smk.getClass())) {
throw new OpConfigError("Only string keys are allowed in submaps.");
}
});
Map<String, Object> submap = (Map<String, Object>) v;
ParsedTemplateMap subtpl = new ParsedTemplateMap(submap, bindings, cfgsources);
if (subtpl.isStatic()) {
statics.put(k, submap);
protomap.put(k, submap);
} else {
dynamics.put(k, subtpl);
protomap.put(k, null);
}
} else if (v instanceof List) {
List<Object> sublist = (List<Object>) v;
ParsedTemplateList subtpl = new ParsedTemplateList(sublist, bindings, cfgsources);
if (subtpl.isStatic()) {
statics.put(k, sublist);
protomap.put(k, sublist);
} else {
dynamics.put(k, subtpl);
protomap.put(k, null);
}
} else {
// Eventually, nested and mixed static dynamic structure could be supported, but
// it would be complex to implement and also not that efficient, so let's just copy
// structure for now
statics.put(k, v);
protomap.put(k, v);
}
});
}
use of io.nosqlbench.virtdata.core.bindings.DataMapper in project nosqlbench by nosqlbench.
the class Templatizer method make.
public static Result make(Map<String, String> bindings, Object v, String name, List<Map<String, Object>> cfgsources) {
Result result = new Result();
result.setName(name);
if (v instanceof CharSequence) {
ParsedTemplate pt = ParsedTemplate.of(((CharSequence) v).toString(), bindings);
result.addCaptures(pt.getCaptures());
result.setType(pt.getType());
switch(pt.getType()) {
case literal:
result.setValue(((CharSequence) v).toString());
break;
case bindref:
String spec = pt.asBinding().orElseThrow().getBindspec();
if (spec == null) {
throw new OpConfigError("Empty binding spec for '" + (name != null ? name : "anonymous binding") + "'");
}
Optional<DataMapper<Object>> mapper = VirtData.getOptionalMapper(spec);
result.setFunction(mapper.orElseThrow());
break;
case concat:
StringBindings sb = new StringBindings(pt);
result.setFunction(sb);
break;
}
} else if (v instanceof Map) {
((Map) v).keySet().forEach(smk -> {
if (!CharSequence.class.isAssignableFrom(smk.getClass())) {
throw new OpConfigError("Only string keys are allowed in submaps.");
}
});
Map<String, Object> submap = (Map<String, Object>) v;
ParsedTemplateMap subtpl = new ParsedTemplateMap(submap, bindings, cfgsources);
if (subtpl.isStatic()) {
result.setValue(submap);
} else {
result.setFunction(subtpl);
}
} else if (v instanceof List) {
List<Object> sublist = (List<Object>) v;
ParsedTemplateList subtpl = new ParsedTemplateList(sublist, bindings, cfgsources);
if (subtpl.isStatic()) {
result.setValue(sublist);
} else {
result.setFunction(subtpl);
}
} else {
result.setValue(v);
// Eventually, nested and mixed static dynamic structure could be supported, but
// it would be complex to implement and also not that efficient, so let's just copy
// structure for now
}
return result;
}
use of io.nosqlbench.virtdata.core.bindings.DataMapper in project nosqlbench by nosqlbench.
the class IntegratedComposerLogicTest method testAPILevelQualifier.
@Test
public void testAPILevelQualifier() {
DataMapper mapper1 = VirtData.getMapper("Uniform(0,100)");
Object o1 = mapper1.get(5L);
assertThat(o1).isOfAnyClassIn(Long.class);
DataMapper mapper2 = VirtData.getMapper("Uniform(0,100)", double.class);
Object o2 = mapper2.get(5L);
assertThat(o2).isOfAnyClassIn(Double.class);
}
use of io.nosqlbench.virtdata.core.bindings.DataMapper in project nosqlbench by nosqlbench.
the class IntegratedComposerLogicTest method testConversionMatchingManual.
@Test
public void testConversionMatchingManual() {
Optional<DataMapper<String>> tm = VirtData.getOptionalMapper("ToEpochTimeUUID(); java.lang.Object -> ToString() -> String");
assertThat(tm).isPresent();
String s = tm.get().get(55L);
assertThat(s).isEqualTo("1389a470-1dd2-11b2-8000-000000000000");
}
use of io.nosqlbench.virtdata.core.bindings.DataMapper in project nosqlbench by nosqlbench.
the class IntegratedComposerLogicTest method testConversionMatchingAuto.
@Test
public void testConversionMatchingAuto() {
Optional<DataMapper<String>> tm = VirtData.getOptionalMapper("ToEpochTimeUUID(); ToString()");
assertThat(tm).isPresent();
String s = tm.get().get(55L);
assertThat(s).isEqualTo("1389a470-1dd2-11b2-8000-000000000000");
}
Aggregations