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

io.nosqlbench.nb.api.markdown.aggregator.MDGraph Maven / Gradle / Ivy

Go to download

The top level API module for NoSQLBench. This module should have no internal module dependencies other than the mvn-default module. All modules within NoSQLBench can safely depend on this module with circular dependencies. This module provides cross-cutting code infrastracture, such as path utilities and ways of describing services used between modules. It is also the transitive aggregation point for system-wide library dependencies for logging and testing or similar needs.

There is a newer version: 5.17.0
Show newest version
package io.nosqlbench.nb.api.markdown.aggregator;

import io.nosqlbench.nb.api.markdown.types.MarkdownInfo;

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

public class MDGraph {

    private Map> topicsByPattern;
    private final Map>> elementsByPattern = new HashMap<>();
    private final Map>> elementsByTopic = new HashMap<>();
    private final List> elements = new LinkedList<>();

    public void add(MarkdownInfo addingElem) {

        Edge edge = new Edge<>(addingElem);

        elements.add(edge);

        for (String topic : addingElem.getTopics()) {
            elementsByTopic.computeIfAbsent(topic, t -> new ArrayList<>()).add(edge);
        }

        // Always add elements to the "none" at a minimum
        if (addingElem.getTopics().size() == 0) {
            elementsByTopic.computeIfAbsent("none", t -> new ArrayList<>()).add(edge);
        }

        for (Pattern pattern : addingElem.getTopicGlobs()) {
            elementsByPattern.computeIfAbsent(pattern.pattern(),
                    p -> new ArrayList<>()).add(edge);
        }
    }

    public List processed() {

        if (topicsByPattern == null) {
            topicsByPattern = topicsByPattern();
        }

        LinkedList> resolved = new LinkedList<>(elements);

        ListIterator> iter = resolved.listIterator();

        while (iter.hasNext()) {
            Edge elementEdge = iter.next();
            MarkdownInfo element = elementEdge.get();

            List topicGlobs = element.getTopicGlobs();

            if (topicGlobs.size() != 0) {
                List> included = new ArrayList<>();
                boolean leafnodes=true;
                for (Pattern topicGlob : topicGlobs) {
                    for (String matchedTopic : topicsByPattern.get(topicGlob.pattern())) {
                        List> edges = elementsByTopic.get(matchedTopic);
                        for (Edge edge : edges) {
                            if (edge.get().getTopicGlobs().size()!=0) {
                                leafnodes=false;
                            }
                            included.add(edge);
                        }
                    }
                    if (leafnodes) {
                        CompositeMarkdownInfo mdinfo =
                                new CompositeMarkdownInfo();
                        mdinfo.add(element);
                        for (Edge tEdge : included) {
                            mdinfo.add(tEdge.get());
                        }
                        // TODO: Add included
                        MarkdownInfo withTopics = mdinfo.withTopics(element.getTopics());
                        elementEdge.set(withTopics);
                    } else {
                        // Move this to the end of the list.
                        iter.remove();
                        resolved.addLast(elementEdge);
                    }
                }
            }
        }

        return resolved.stream().map(Edge::get).collect(Collectors.toList());
    }

    private Map> topicsByPattern() {
        Map> tbp = new HashMap<>();
        for (String pattern : this.elementsByPattern.keySet()) {
            List matchingTopics = tbp.computeIfAbsent(pattern, p -> new ArrayList<>());
            for (String topic : this.elementsByTopic.keySet()) {
                if (Pattern.compile(pattern).matcher(topic).matches()) {
                    matchingTopics.add(topic);
                }
            }
        }
        return tbp;
    }


    /**
     * Allow edges to point to mutable vertices
     * @param 
     */
    private final static class Edge {
        private T element;

        public Edge(T element) {
            this.element = element;
        }

        public T get() {
            return element;
        }

        public void set(T element) {
            this.element = element;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy