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

io.nosqlbench.engine.api.activityconfig.rawyaml.RawStmtsLoader Maven / Gradle / Ivy

Go to download

The DriverAdapter API for nosqlbench; Provides the interfaces needed to build drivers that can be loaded by nosqlbench core, using a minimal and direct API for op mapping.

There is a newer version: 5.17.0
Show newest version
/*
 * 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.rawyaml;

import io.nosqlbench.engine.api.templating.StrInterpolator;
import io.nosqlbench.api.content.Content;
import io.nosqlbench.api.content.NBIO;
import io.nosqlbench.api.errors.BasicError;
import io.nosqlbench.api.errors.OpConfigError;
import org.apache.logging.log4j.Logger;
import org.yaml.snakeyaml.Yaml;

import java.util.*;
import java.util.function.Function;

public class RawStmtsLoader {

    public static String[] YAML_EXTENSIONS = new String[]{"yaml","yml"};

    List> stringTransformers = new ArrayList<>();
    private final ArrayList> transformers = new ArrayList<>();

    public RawStmtsLoader(Function transformer) {
        addTransformer(transformer);
    }

    public RawStmtsLoader() {
        addTransformer(new StrInterpolator());
    }

    private void addTransformer(Function newTransformer) {
        Collections.addAll(this.transformers, newTransformer);
    }

    public RawStmtsDocList loadString(Logger logger, String data) {

        try {
            if (logger != null) logger.trace("Applying string transformer to yaml data:" + data);
            for (Function transformer : transformers) {
                data = transformer.apply(data);
            }
        } catch (Exception e) {
            RuntimeException t = new OpConfigError("Error applying string transforms to input", e);
            throw t;
        }

        return parseYaml(logger, data);
    }

    public RawStmtsDocList loadPath(
            Logger logger,
            String path,
            String... searchPaths) {

        String data = null;
        try {
            Optional> oyaml = NBIO.all().prefix(searchPaths).name(path).extension(YAML_EXTENSIONS).first();
            data = oyaml.map(Content::asString).orElseThrow(() -> new BasicError("Unable to load " + path));
            return loadString(logger, data);
        } catch (Exception e) {
            throw new RuntimeException("error while reading file " + path, e);
        }

    }

    private RawStmtsDocList parseYaml(Logger logger, String data) {
        Yaml yaml = new Yaml();
        Iterable objects = yaml.loadAll(data);
        List newDocList = new ArrayList<>();

        for (Object object : objects) {
            if (object instanceof Map) {
                RawStmtsDoc doc = new RawStmtsDoc();
                Map docfields = (Map) object;
                doc.setFieldsByReflection(docfields);
                if (docfields.size()>0) {
                    throw new OpConfigError("Some fields were not recognized from the yaml provided:" + docfields.keySet());
                }

                newDocList.add(doc);
            } else {
                throw new RuntimeException("Unable to coerce a non-map type to a statements yaml doc: " + object.getClass().getCanonicalName());
            }
        }
        RawStmtsDocList rawStmtsDocList = new RawStmtsDocList(newDocList);
        return rawStmtsDocList;
    }

    protected String applyTransforms(Logger logger, String data) {
        for (Function xform : stringTransformers) {
            try {
                if (logger != null) logger.trace("Applying string transformer to yaml data:" + xform);
                data = xform.apply(data);
            } catch (Exception e) {
                RuntimeException t = new OpConfigError("Error applying string transforms to input", e);
                throw t;
            }
        }
        return data;
    }

}