Search in sources :

Example 1 with StringBindings

use of io.nosqlbench.virtdata.core.templates.StringBindings 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);
        }
    });
}
Also used : ListBinder(io.nosqlbench.engine.api.templating.binders.ListBinder) StringBindings(io.nosqlbench.virtdata.core.templates.StringBindings) java.util(java.util) ParsedTemplate(io.nosqlbench.virtdata.core.templates.ParsedTemplate) LongFunction(java.util.function.LongFunction) ArrayBinder(io.nosqlbench.engine.api.templating.binders.ArrayBinder) CapturePoint(io.nosqlbench.virtdata.core.templates.CapturePoint) BindPoint(io.nosqlbench.virtdata.core.templates.BindPoint) Function(java.util.function.Function) BasicError(io.nosqlbench.nb.api.errors.BasicError) NBConfigError(io.nosqlbench.nb.api.config.standard.NBConfigError) NBTypeConverter(io.nosqlbench.nb.api.config.standard.NBTypeConverter) DynamicFieldReader(io.nosqlbench.nb.api.config.fieldreaders.DynamicFieldReader) OpConfigError(io.nosqlbench.nb.api.errors.OpConfigError) OrderedMapBinder(io.nosqlbench.engine.api.templating.binders.OrderedMapBinder) Logger(org.apache.logging.log4j.Logger) StaticFieldReader(io.nosqlbench.nb.api.config.fieldreaders.StaticFieldReader) VirtData(io.nosqlbench.virtdata.core.bindings.VirtData) DataMapper(io.nosqlbench.virtdata.core.bindings.DataMapper) LogManager(org.apache.logging.log4j.LogManager) OpConfigError(io.nosqlbench.nb.api.errors.OpConfigError) DataMapper(io.nosqlbench.virtdata.core.bindings.DataMapper) StringBindings(io.nosqlbench.virtdata.core.templates.StringBindings) ParsedTemplate(io.nosqlbench.virtdata.core.templates.ParsedTemplate)

Example 2 with StringBindings

use of io.nosqlbench.virtdata.core.templates.StringBindings 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;
}
Also used : OpConfigError(io.nosqlbench.nb.api.errors.OpConfigError) StringBindings(io.nosqlbench.virtdata.core.templates.StringBindings) List(java.util.List) ParsedTemplate(io.nosqlbench.virtdata.core.templates.ParsedTemplate) LongFunction(java.util.function.LongFunction) Map(java.util.Map) CapturePoint(io.nosqlbench.virtdata.core.templates.CapturePoint) Optional(java.util.Optional) VirtData(io.nosqlbench.virtdata.core.bindings.VirtData) DataMapper(io.nosqlbench.virtdata.core.bindings.DataMapper) ArrayList(java.util.ArrayList) OpConfigError(io.nosqlbench.nb.api.errors.OpConfigError) DataMapper(io.nosqlbench.virtdata.core.bindings.DataMapper) StringBindings(io.nosqlbench.virtdata.core.templates.StringBindings) List(java.util.List) ArrayList(java.util.ArrayList) ParsedTemplate(io.nosqlbench.virtdata.core.templates.ParsedTemplate) Map(java.util.Map)

Example 3 with StringBindings

use of io.nosqlbench.virtdata.core.templates.StringBindings in project nosqlbench by nosqlbench.

the class IntegratedStringBindingsTest method testEven.

@Test
public void testEven() {
    StringBindings c = new StringBindings("A{ident}B{ident}C{mod5}D{mod-5}", template);
    String bind3 = c.bind(3);
    assertThat(bind3).isEqualTo("A3B3C3D3");
}
Also used : StringBindings(io.nosqlbench.virtdata.core.templates.StringBindings) Test(org.junit.jupiter.api.Test)

Example 4 with StringBindings

use of io.nosqlbench.virtdata.core.templates.StringBindings in project nosqlbench by nosqlbench.

the class IntegratedStringBindingsTest method testBindValuesSpecialChars.

@Test
public void testBindValuesSpecialChars() {
    StringBindings c = new StringBindings("A{mod-5}C", template);
    String s = c.apply(6L);
    assertThat(s).isEqualTo("A1C");
    c = new StringBindings("A{5_mod_5}C", template);
    s = c.apply(7L);
    assertThat(s).isEqualTo("A2C");
}
Also used : StringBindings(io.nosqlbench.virtdata.core.templates.StringBindings) Test(org.junit.jupiter.api.Test)

Example 5 with StringBindings

use of io.nosqlbench.virtdata.core.templates.StringBindings in project nosqlbench by nosqlbench.

the class StdoutActivity method initOpSequencer.

private OpSequence<StringBindings> initOpSequencer() {
    // List<StringBindingsTemplate> stringBindingsTemplates = new ArrayList<>();
    SequencerType sequencerType = SequencerType.valueOf(getParams().getOptionalString("seq").orElse("bucket"));
    SequencePlanner<StringBindings> sequencer = new SequencePlanner<>(sequencerType);
    String tagfilter = activityDef.getParams().getOptionalString("tags").orElse("");
    List<OpTemplate> stmts = stmtsDocList.getStmts(tagfilter);
    String format = getParams().getOptionalString("format").orElse(null);
    if ((stmts.size() == 0 && stmtsDocList.getDocBindings().size() > 0) || format != null) {
        if (format != null && format.startsWith("diag")) {
            logger.info("Creating diagnostic log for resolver construction...");
            BindingsTemplate bt = new BindingsTemplate();
            stmtsDocList.getDocBindings().forEach(bt::addFieldBinding);
            String diagnostics = bt.getDiagnostics();
            getConsoleOut().println(diagnostics);
            getConsoleOut().flush();
            System.exit(2);
        } else {
            logger.info("Creating stdout statement template from bindings, since none is otherwise defined.");
            Set<String> activeBindingNames = new LinkedHashSet<>();
            String bindings = getActivityDef().getParams().getOptionalString("bindings").orElse("doc");
            activeBindingNames.addAll(stmtsDocList.getDocBindings().keySet());
            Pattern bindingsFilter = Pattern.compile(bindings.equalsIgnoreCase("doc") ? ".*" : bindings);
            Set<String> filteredBindingNames = new LinkedHashSet<>();
            activeBindingNames.stream().filter(n -> {
                if (bindingsFilter.matcher(n).matches()) {
                    logger.trace("bindings filter kept binding '" + n + "'");
                    return true;
                } else {
                    logger.trace("bindings filter removed binding '" + n + "'");
                    return false;
                }
            }).forEach(filteredBindingNames::add);
            activeBindingNames = filteredBindingNames;
            String generatedStmt = genStatementTemplate(activeBindingNames);
            BindingsTemplate bt = new BindingsTemplate();
            stmtsDocList.getDocBindings().forEach(bt::addFieldBinding);
            StringBindings sb = new StringBindings(generatedStmt, bt.getMap());
            sequencer.addOp(sb, 1L);
        }
    } else if (stmts.size() > 0) {
        for (OpTemplate stmt : stmts) {
            ParsedTemplate parsed = stmt.getParsed().orElseThrow();
            BindingsTemplate bt = new BindingsTemplate(parsed.getBindPoints());
            String statement = parsed.getPositionalStatement(Function.identity());
            Objects.requireNonNull(statement);
            if (!statement.endsWith("\n") && getParams().getOptionalBoolean("newline").orElse(true)) {
                statement = statement + "\n";
            }
            StringBindingsTemplate sbt = new StringBindingsTemplate(stmt.getStmt().orElseThrow(), bt);
            StringBindings sb = sbt.resolve();
            sequencer.addOp(sb, stmt.getParamOrDefault("ratio", 1));
        }
    } else {
        logger.error("Unable to create a stdout statement if you have no active statements or bindings configured.");
    }
    OpSequence<StringBindings> opSequence = sequencer.resolve();
    return opSequence;
}
Also used : Histogram(com.codahale.metrics.Histogram) OpSequence(io.nosqlbench.engine.api.activityapi.planning.OpSequence) StringBindings(io.nosqlbench.virtdata.core.templates.StringBindings) java.util(java.util) ParsedTemplate(io.nosqlbench.virtdata.core.templates.ParsedTemplate) ActivityDef(io.nosqlbench.engine.api.activityimpl.ActivityDef) SimpleActivity(io.nosqlbench.engine.api.activityimpl.SimpleActivity) Function(java.util.function.Function) ExceptionMeterMetrics(io.nosqlbench.engine.api.metrics.ExceptionMeterMetrics) ActivityDefObserver(io.nosqlbench.engine.api.activityapi.core.ActivityDefObserver) StmtsDocList(io.nosqlbench.engine.api.activityconfig.yaml.StmtsDocList) StatementsLoader(io.nosqlbench.engine.api.activityconfig.StatementsLoader) OpTemplate(io.nosqlbench.engine.api.activityconfig.yaml.OpTemplate) SequencePlanner(io.nosqlbench.engine.api.activityapi.planning.SequencePlanner) PrintWriter(java.io.PrintWriter) TemplateFormat(io.nosqlbench.adapters.stdout.TemplateFormat) BindingsTemplate(io.nosqlbench.virtdata.core.bindings.BindingsTemplate) FileNotFoundException(java.io.FileNotFoundException) ActivityMetrics(io.nosqlbench.engine.api.metrics.ActivityMetrics) SequencerType(io.nosqlbench.engine.api.activityapi.planning.SequencerType) Logger(org.apache.logging.log4j.Logger) ParameterMap(io.nosqlbench.engine.api.activityimpl.ParameterMap) Writer(java.io.Writer) Timer(com.codahale.metrics.Timer) StringBindingsTemplate(io.nosqlbench.virtdata.core.templates.StringBindingsTemplate) Pattern(java.util.regex.Pattern) LogManager(org.apache.logging.log4j.LogManager) Pattern(java.util.regex.Pattern) StringBindingsTemplate(io.nosqlbench.virtdata.core.templates.StringBindingsTemplate) SequencerType(io.nosqlbench.engine.api.activityapi.planning.SequencerType) StringBindings(io.nosqlbench.virtdata.core.templates.StringBindings) OpTemplate(io.nosqlbench.engine.api.activityconfig.yaml.OpTemplate) SequencePlanner(io.nosqlbench.engine.api.activityapi.planning.SequencePlanner) ParsedTemplate(io.nosqlbench.virtdata.core.templates.ParsedTemplate) BindingsTemplate(io.nosqlbench.virtdata.core.bindings.BindingsTemplate) StringBindingsTemplate(io.nosqlbench.virtdata.core.templates.StringBindingsTemplate)

Aggregations

StringBindings (io.nosqlbench.virtdata.core.templates.StringBindings)8 Test (org.junit.jupiter.api.Test)4 ParsedTemplate (io.nosqlbench.virtdata.core.templates.ParsedTemplate)3 Timer (com.codahale.metrics.Timer)2 OpConfigError (io.nosqlbench.nb.api.errors.OpConfigError)2 DataMapper (io.nosqlbench.virtdata.core.bindings.DataMapper)2 VirtData (io.nosqlbench.virtdata.core.bindings.VirtData)2 CapturePoint (io.nosqlbench.virtdata.core.templates.CapturePoint)2 java.util (java.util)2 Function (java.util.function.Function)2 LongFunction (java.util.function.LongFunction)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 Histogram (com.codahale.metrics.Histogram)1 TemplateFormat (io.nosqlbench.adapters.stdout.TemplateFormat)1 ActivityDefObserver (io.nosqlbench.engine.api.activityapi.core.ActivityDefObserver)1 OpSequence (io.nosqlbench.engine.api.activityapi.planning.OpSequence)1 SequencePlanner (io.nosqlbench.engine.api.activityapi.planning.SequencePlanner)1 SequencerType (io.nosqlbench.engine.api.activityapi.planning.SequencerType)1 StatementsLoader (io.nosqlbench.engine.api.activityconfig.StatementsLoader)1