All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.nosqlbench.engine.api.activityconfig.yaml.StmtsDocList Maven / Gradle / Ivy

/*
 * Copyright (c) 2022 nosqlbench
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.nosqlbench.engine.api.activityconfig.yaml;

import io.nosqlbench.engine.api.activityconfig.StatementsLoader;
import io.nosqlbench.engine.api.activityconfig.rawyaml.RawStmtsDoc;
import io.nosqlbench.engine.api.activityconfig.rawyaml.RawStmtsDocList;
import io.nosqlbench.engine.api.util.TagFilter;
import io.nosqlbench.api.config.standard.ConfigModel;
import io.nosqlbench.api.config.standard.NBConfigModel;
import io.nosqlbench.api.config.standard.NBConfigModelExpander;
import io.nosqlbench.api.config.standard.Param;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class StmtsDocList implements Iterable {
    private final static Logger logger = LogManager.getLogger(StmtsDocList.class);

    private final RawStmtsDocList rawStmtsDocList;
    private final Map templateVariables = new LinkedHashMap<>();

    public StmtsDocList(RawStmtsDocList rawStmtsDocList) {
        this.rawStmtsDocList = rawStmtsDocList;
    }

    public List getStmtDocs(String tagFilter) {
        TagFilter tf = new TagFilter(tagFilter);
        return getStmtDocs().stream()
            .filter(tf::matchesTagged)
            .collect(Collectors.toList());
    }

    public List getStmtDocs() {
        return rawStmtsDocList.getStmtsDocs().stream()
            .map(StmtsDoc::new)
            .collect(Collectors.toList());
    }

    public List getStmts() {
        return getStmts("");
    }

    /**
     * @param tagFilterSpec a comma-separated tag filter spec
     * @return The list of all included statements for all included blocks of  in this document,
     * including the inherited and overridden values from the this doc and the parent block.
     */
    public List getStmts(String tagFilterSpec) {
        TagFilter ts = new TagFilter(tagFilterSpec);
        List opTemplates = new ArrayList<>();


        getStmtDocs().stream()
            .flatMap(d -> d.getStmts().stream())
            .filter(ts::matchesTagged)
            .forEach(opTemplates::add);

        return opTemplates;
    }


    @Override
    public Iterator iterator() {
        return getStmtDocs().iterator();
    }

    /**
     * Return the list of all bindings combined across all docs, not including
     * the block or statement level bindings.
     *
     * @return A map of all bindings at the doc level.
     */
    public Map getDocBindings() {
        LinkedHashMap docBindings = new LinkedHashMap<>();
        getStmtDocs().stream()
            .map(StmtsDoc::getBindings)
            .forEach(docBindings::putAll);
        return docBindings;
    }

    /**
     * This returns all the `scenarios` blocs across multiple docs, per the description in issue-67 there should only be one
     * on the first doc, any `scenarios` defined in different docs will be ignored.
     */

    /**
     * @return the list of named scenarios for the first document in the list.
     */
    public Scenarios getDocScenarios() {
        if (this.getStmtDocs().size() == 0) {
            throw new RuntimeException("No statement docs were found, so source file is empty.");
        }
        return this.getStmtDocs().get(0).getScenarios();
    }

    /**
     * @return the description of the first document in the list.
     */
    public String getDescription() {
        return this.getStmtDocs().get(0).getDescription();
    }

    public Map getTemplateVariables() {
        return templateVariables;
    }

    public void addTemplateVariable(String key, String defaultValue) {
        this.templateVariables.put(key, defaultValue);
    }

    public NBConfigModel getConfigModel() {
        ConfigModel cfgmodel = ConfigModel.of(StmtsDocList.class);
        getTemplateVariables().forEach((k, v) -> {
            cfgmodel.add(Param.defaultTo(k, v, "template parameter found in the yaml workload"));
        });
        return cfgmodel.asReadOnly();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        int docscount = 0;
        int blockscount = 0;
        int opscount = 0;

        for (StmtsDoc stmtDoc : this.getStmtDocs()) {
            docscount++;
            for (StmtsBlock block : stmtDoc.getBlocks()) {
                blockscount++;
                for (OpTemplate op : block.getOps()) {
                    opscount++;
                }
            }
        }

        sb.append("docs: " + docscount + " blocks:" + blockscount + " ops:" + opscount);
//        String names = this.rawStmtsDocList.getStmtsDocs().stream().flatMap(sd -> sd.getRawStmtDefs().stream()).map(d->d.getName()).collect(Collectors.joining(","));
//        sb.append(", names:").append(names);
        return sb.toString();
    }

    public static NBConfigModelExpander TEMPLATE_VAR_EXPANDER = workload -> {
        StmtsDocList loaded = StatementsLoader.loadPath(logger, (String) workload, "activities");
        return loaded.getConfigModel();
    };

    public Pattern getVersionRegex() {
        List stmtDocs = rawStmtsDocList.getStmtsDocs();
        return Pattern.compile(stmtDocs.size()>0 ? stmtDocs.get(0).getVersionRegex() : ".*");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy