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

apoc.export.util.NodesAndRelsSubGraph Maven / Gradle / Ivy

There is a newer version: 5.25.1
Show newest version
package apoc.export.util;

import apoc.util.collection.Iterables;
import org.neo4j.cypher.export.SubGraph;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.schema.ConstraintDefinition;
import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.graphdb.schema.Schema;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author mh
 * @since 22.05.16
 */
public class NodesAndRelsSubGraph implements SubGraph {
    private final Collection nodes;
    private final Collection rels;
    private final Transaction tx;
    private final Set labels = new HashSet<>(20);
    private final Set types = new HashSet<>(20);

    public NodesAndRelsSubGraph(Transaction tx, Collection nodes, Collection rels) {
        this.tx = tx;
        this.nodes = new ArrayList<>(nodes.size());
        for (Node node : nodes) {
            for (Label label : node.getLabels()) labels.add(label.name());
            this.nodes.add(node);
        }
        this.rels = new HashSet<>(rels);
        for (Relationship rel : rels) {
            this.types.add(rel.getType().name());
        }
    }

    @Override
    public Iterable getNodes() {
        return nodes;
    }

    @Override
    public Iterable getRelationships() {
        return rels;
    }

    @Override
    public Iterable getIndexes() {
        Schema schema = tx.schema();
        ArrayList indexes = new ArrayList<>(labels.size() * 2);
        for (String label : labels) {
            Iterables.addAll(indexes, schema.getIndexes(Label.label(label)));
        }
        return indexes;
    }

    @Override
    public Iterable getConstraints(Label label) {
        if (!labels.contains(label.name())) {
            return Collections.emptyList();
        }
        return tx.schema().getConstraints(label);
    }

    @Override
    public Iterable getConstraints(RelationshipType type) {
        if (!types.contains(type.name())) {
            return Collections.emptyList();
        }
        return tx.schema().getConstraints(type);
    }

    @Override
    public Iterable getIndexes(Label label) {
        if (!labels.contains(label.name())) {
            return Collections.emptyList();
        }
        return tx.schema().getIndexes(label);
    }

    @Override
    public Iterable getIndexes(RelationshipType type) {
        if (!types.contains(type.name())) {
            return Collections.emptyList();
        }
        return tx.schema().getIndexes(type);
    }

    @Override
    public Iterable getAllRelationshipTypesInUse() {
        return types.stream()
                .map(RelationshipType::withName)
                .collect(Collectors.toSet());
    }

    @Override
    public Iterable




© 2015 - 2024 Weber Informatics LLC | Privacy Policy