Search in sources :

Example 56 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.

the class SchemaProcedure method buildSchemaGraph.

public GraphResult buildSchemaGraph() {
    final Map<String, NodeImpl> nodes = new HashMap<>();
    final Map<String, Set<RelationshipImpl>> relationships = new HashMap<>();
    try (Statement statement = kernelTransaction.acquireStatement()) {
        ReadOperations readOperations = statement.readOperations();
        StatementTokenNameLookup statementTokenNameLookup = new StatementTokenNameLookup(readOperations);
        try (Transaction transaction = graphDatabaseAPI.beginTx()) {
            // add all labelsInDatabase
            ResourceIterator<Label> labelsInDatabase = graphDatabaseAPI.getAllLabelsInUse().iterator();
            while (labelsInDatabase.hasNext()) {
                Label label = labelsInDatabase.next();
                Map<String, Object> properties = new HashMap<>();
                Iterator<NewIndexDescriptor> indexDescriptorIterator = readOperations.indexesGetForLabel(readOperations.labelGetForName(label.name()));
                ArrayList<String> indexes = new ArrayList<>();
                while (indexDescriptorIterator.hasNext()) {
                    NewIndexDescriptor index = indexDescriptorIterator.next();
                    String[] propertyNames = PropertyNameUtils.getPropertyKeys(statementTokenNameLookup, index.schema().getPropertyIds());
                    indexes.add(String.join(",", propertyNames));
                }
                properties.put("indexes", indexes);
                Iterator<ConstraintDescriptor> nodePropertyConstraintIterator = readOperations.constraintsGetForLabel(readOperations.labelGetForName(label.name()));
                ArrayList<String> constraints = new ArrayList<>();
                while (nodePropertyConstraintIterator.hasNext()) {
                    PropertyConstraint constraint = ConstraintBoundary.map(nodePropertyConstraintIterator.next());
                    constraints.add(constraint.userDescription(statementTokenNameLookup));
                }
                properties.put("constraints", constraints);
                getOrCreateLabel(label.name(), properties, nodes);
            }
            //add all relationships
            Iterator<RelationshipType> relationshipTypeIterator = graphDatabaseAPI.getAllRelationshipTypesInUse().iterator();
            while (relationshipTypeIterator.hasNext()) {
                RelationshipType relationshipType = relationshipTypeIterator.next();
                String relationshipTypeGetName = relationshipType.name();
                int relId = readOperations.relationshipTypeGetForName(relationshipTypeGetName);
                ResourceIterator<Label> labelsInUse = graphDatabaseAPI.getAllLabelsInUse().iterator();
                List<NodeImpl> startNodes = new LinkedList<>();
                List<NodeImpl> endNodes = new LinkedList<>();
                while (labelsInUse.hasNext()) {
                    Label labelToken = labelsInUse.next();
                    String labelName = labelToken.name();
                    Map<String, Object> properties = new HashMap<>();
                    NodeImpl node = getOrCreateLabel(labelName, properties, nodes);
                    int labelId = readOperations.labelGetForName(labelName);
                    if (readOperations.countsForRelationship(labelId, relId, ReadOperations.ANY_LABEL) > 0) {
                        startNodes.add(node);
                    }
                    if (readOperations.countsForRelationship(ReadOperations.ANY_LABEL, relId, labelId) > 0) {
                        endNodes.add(node);
                    }
                }
                for (NodeImpl startNode : startNodes) {
                    for (NodeImpl endNode : endNodes) {
                        RelationshipImpl relationship = addRelationship(startNode, endNode, relationshipTypeGetName, relationships);
                    }
                }
            }
            transaction.success();
            return getGraphResult(nodes, relationships);
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Label(org.neo4j.graphdb.Label) ArrayList(java.util.ArrayList) RelationshipType(org.neo4j.graphdb.RelationshipType) PropertyConstraint(org.neo4j.kernel.api.constraints.PropertyConstraint) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) StatementTokenNameLookup(org.neo4j.kernel.api.StatementTokenNameLookup) Statement(org.neo4j.kernel.api.Statement) PropertyConstraint(org.neo4j.kernel.api.constraints.PropertyConstraint) LinkedList(java.util.LinkedList) ReadOperations(org.neo4j.kernel.api.ReadOperations) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)

Example 57 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.

the class ValueRelationship method unpackFields.

public static ValueRelationship unpackFields(Neo4jPack.Unpacker unpacker) throws IOException {
    long relId = unpacker.unpackLong();
    long startNodeId = unpacker.unpackLong();
    long endNodeId = unpacker.unpackLong();
    String relTypeName = unpacker.unpackString();
    Map<String, Object> props = unpacker.unpackMap();
    RelationshipType relType = RelationshipType.withName(relTypeName);
    return new ValueRelationship(relId, startNodeId, endNodeId, relType, props);
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType)

Example 58 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.

the class ValueUnboundRelationship method unpackFields.

public static ValueUnboundRelationship unpackFields(Neo4jPack.Unpacker unpacker) throws IOException {
    long relId = unpacker.unpackLong();
    String relTypeName = unpacker.unpackString();
    Map<String, Object> props = unpacker.unpackMap();
    RelationshipType relType = RelationshipType.withName(relTypeName);
    return new ValueUnboundRelationship(relId, relType, props);
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType)

Example 59 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.

the class ParallelBatchImporterTest method assertDegrees.

private void assertDegrees(Node node) {
    for (RelationshipType type : node.getRelationshipTypes()) {
        for (Direction direction : Direction.values()) {
            long degree = node.getDegree(type, direction);
            long actualDegree = count(node.getRelationships(type, direction));
            assertEquals(actualDegree, degree);
        }
    }
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) Direction(org.neo4j.graphdb.Direction)

Example 60 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.

the class TestShortestPath method shouldAbortAsSoonAsPossible.

// Attempt at recreating this issue without cypher
// https://github.com/neo4j/neo4j/issues/4160
@Test
public void shouldAbortAsSoonAsPossible() {
    final Label A = Label.label("A");
    final Label B = Label.label("B");
    final Label C = Label.label("C");
    final Label D = Label.label("D");
    final Label E = Label.label("E");
    final Label F = Label.label("F");
    final RelationshipType relType = RelationshipType.withName("TO");
    recursiveSnowFlake(null, 0, 4, 5, new Label[] { A, B, C, D, E }, relType);
    final Node a = graphDb.findNodes(A).next();
    final ResourceIterator<Node> allE = graphDb.findNodes(E);
    while (allE.hasNext()) {
        final Node e = allE.next();
        final Node f = graphDb.createNode(F);
        f.createRelationshipTo(e, relType);
    }
    final CountingPathExpander countingPathExpander = new CountingPathExpander(PathExpanders.forTypeAndDirection(relType, Direction.OUTGOING));
    final ShortestPath shortestPath = new ShortestPath(Integer.MAX_VALUE, countingPathExpander, Integer.MAX_VALUE);
    final ResourceIterator<Node> allF = graphDb.findNodes(F);
    final long start = System.currentTimeMillis();
    while (allF.hasNext()) {
        final Node f = allF.next();
        shortestPath.findAllPaths(a, f);
    }
    final long totalTime = System.currentTimeMillis() - start;
    assertEquals("There are 625 different end nodes. The algorithm should start one traversal for each such node. " + "That is 625*2 visited nodes if traversal is interrupted correctly.", 1250, countingPathExpander.nodesVisited.intValue());
}
Also used : Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.Test)

Aggregations

RelationshipType (org.neo4j.graphdb.RelationshipType)97 Node (org.neo4j.graphdb.Node)53 Test (org.junit.Test)45 Relationship (org.neo4j.graphdb.Relationship)38 Transaction (org.neo4j.graphdb.Transaction)18 Direction (org.neo4j.graphdb.Direction)15 Traverser (org.neo4j.graphdb.Traverser)10 NotFoundException (org.neo4j.graphdb.NotFoundException)9 DynamicRelationshipType (org.neo4j.graphdb.DynamicRelationshipType)7 Label (org.neo4j.graphdb.Label)7 RelationshipRecord (org.neo4j.kernel.impl.nioneo.store.RelationshipRecord)7 Collection (java.util.Collection)6 HashSet (java.util.HashSet)6 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)6 Path (org.neo4j.graphdb.Path)6 StopEvaluator (org.neo4j.graphdb.StopEvaluator)6 TraversalPosition (org.neo4j.graphdb.TraversalPosition)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)4