Search in sources :

Example 6 with Relationship

use of org.neo4j.graphdb.Relationship in project neo4j-clean-remote-db-addon by jexp.

the class Neo4jDatabaseCleaner method removeNodes.

private void removeNodes(Map<String, Object> result, long maxNodesToDelete) {
    Node refNode = graph.getReferenceNode();
    long nodes = 0, relationships = 0;
    for (Node node : graph.getAllNodes()) {
        for (Relationship rel : node.getRelationships()) {
            rel.delete();
            relationships++;
        }
        if (!refNode.equals(node)) {
            node.delete();
            nodes++;
        }
        if (nodes >= maxNodesToDelete)
            break;
    }
    result.put("maxNodesToDelete", maxNodesToDelete);
    result.put("nodes", nodes);
    result.put("relationships", relationships);
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship)

Example 7 with Relationship

use of org.neo4j.graphdb.Relationship in project neo4j-clean-remote-db-addon by jexp.

the class DeleteDatabaseTest method createData.

private void createData(GraphDatabaseAPI db, int max) {
    Transaction tx = db.beginTx();
    try {
        final IndexManager indexManager = db.index();
        Node[] nodes = new Node[max];
        for (int i = 0; i < max; i++) {
            nodes[i] = db.createNode();
            final Index<Node> index = indexManager.forNodes("node_index_" + String.valueOf(i % 5));
            index.add(nodes[i], "ID", i);
        }
        Random random = new Random();
        for (int i = 0; i < max * 2; i++) {
            int from = random.nextInt(max);
            final int to = (from + 1 + random.nextInt(max - 1)) % max;
            final Relationship relationship = nodes[from].createRelationshipTo(nodes[to], DynamicRelationshipType.withName("TEST_" + i));
            final Index<Relationship> index = indexManager.forRelationships("rel_index_" + String.valueOf(i % 5));
            index.add(relationship, "ID", i);
        }
        tx.success();
    } finally {
        tx.finish();
    }
}
Also used : IndexManager(org.neo4j.graphdb.index.IndexManager) Transaction(org.neo4j.graphdb.Transaction) Random(java.util.Random) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship)

Example 8 with Relationship

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

the class ImportToolTest method verifyData.

private void verifyData(Validator<Node> nodeAdditionalValidation, Validator<Relationship> relationshipAdditionalValidation) {
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    try (Transaction tx = db.beginTx()) {
        int nodeCount = 0, relationshipCount = 0;
        for (Node node : db.getAllNodes()) {
            assertTrue(node.hasProperty("name"));
            nodeAdditionalValidation.validate(node);
            nodeCount++;
        }
        assertEquals(NODE_COUNT, nodeCount);
        for (Relationship relationship : db.getAllRelationships()) {
            assertTrue(relationship.hasProperty("created"));
            relationshipAdditionalValidation.validate(relationship);
            relationshipCount++;
        }
        assertEquals(RELATIONSHIP_COUNT, relationshipCount);
        tx.success();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship)

Example 9 with Relationship

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

the class TraversalBranchImpl method next.

@Override
public TraversalBranch next(PathExpander expander, TraversalContext context) {
    if (relationships == null) {
        expandRelationships(expander);
    }
    while (relationships.hasNext()) {
        Relationship relationship = relationships.next();
        if (relationship.equals(howIGotHere)) {
            context.unnecessaryRelationshipTraversed();
            continue;
        }
        expandedCount++;
        Node node = relationship.getOtherNode(source);
        // TODO maybe an unnecessary instantiation. Instead pass in this+node+relationship to uniqueness check
        TraversalBranch next = newNextBranch(node, relationship);
        if (context.isUnique(next)) {
            context.relationshipTraversed();
            next.initialize(expander, context);
            return next;
        } else {
            context.unnecessaryRelationshipTraversed();
        }
    }
    // Just to help GC
    relationships = PRUNED_ITERATOR;
    return null;
}
Also used : Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Example 10 with Relationship

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

the class BigStoreIT method testHighIds.

private void testHighIds(long highMark, int minus, int requiredHeapMb) throws IOException {
    if (!machineIsOkToRunThisTest(requiredHeapMb)) {
        return;
    }
    long idBelow = highMark - minus;
    setHighIds(idBelow);
    String propertyKey = "name";
    int intPropertyValue = 123;
    String stringPropertyValue = "Long string, longer than would fit in shortstring";
    long[] arrayPropertyValue = new long[] { 1021L, 321L, 343212L };
    Transaction tx = db.beginTx();
    Node nodeBelowTheLine = db.createNode();
    nodeBelowTheLine.setProperty(propertyKey, intPropertyValue);
    assertEquals(idBelow, nodeBelowTheLine.getId());
    Node nodeAboveTheLine = db.createNode();
    nodeAboveTheLine.setProperty(propertyKey, stringPropertyValue);
    Relationship relBelowTheLine = nodeBelowTheLine.createRelationshipTo(nodeAboveTheLine, this);
    relBelowTheLine.setProperty(propertyKey, arrayPropertyValue);
    assertEquals(idBelow, relBelowTheLine.getId());
    Relationship relAboveTheLine = nodeAboveTheLine.createRelationshipTo(nodeBelowTheLine, this);
    assertEquals(highMark, relAboveTheLine.getId());
    assertEquals(highMark, nodeAboveTheLine.getId());
    assertEquals(intPropertyValue, nodeBelowTheLine.getProperty(propertyKey));
    assertEquals(stringPropertyValue, nodeAboveTheLine.getProperty(propertyKey));
    assertTrue(Arrays.equals(arrayPropertyValue, (long[]) relBelowTheLine.getProperty(propertyKey)));
    tx.success();
    tx.close();
    for (int i = 0; i < 2; i++) {
        try (Transaction transaction = db.beginTx()) {
            assertEquals(nodeAboveTheLine, db.getNodeById(highMark));
            assertEquals(idBelow, nodeBelowTheLine.getId());
            assertEquals(highMark, nodeAboveTheLine.getId());
            assertEquals(idBelow, relBelowTheLine.getId());
            assertEquals(highMark, relAboveTheLine.getId());
            assertEquals(relBelowTheLine, db.getNodeById(idBelow).getSingleRelationship(this, Direction.OUTGOING));
            assertEquals(relAboveTheLine, db.getNodeById(idBelow).getSingleRelationship(this, Direction.INCOMING));
            assertEquals(idBelow, relBelowTheLine.getId());
            assertEquals(highMark, relAboveTheLine.getId());
            assertEquals(asSet(asList(relBelowTheLine, relAboveTheLine)), asSet(Iterables.asCollection(db.getNodeById(idBelow).getRelationships())));
            transaction.success();
        }
        if (i == 0) {
            db = dbRule.restartDatabase();
        }
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship)

Aggregations

Relationship (org.neo4j.graphdb.Relationship)484 Node (org.neo4j.graphdb.Node)367 Test (org.junit.Test)250 Transaction (org.neo4j.graphdb.Transaction)138 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)48 LinkedList (java.util.LinkedList)45 RelationshipType (org.neo4j.graphdb.RelationshipType)37 NotFoundException (org.neo4j.graphdb.NotFoundException)35 Path (org.neo4j.graphdb.Path)29 HashMap (java.util.HashMap)27 Direction (org.neo4j.graphdb.Direction)25 HashSet (java.util.HashSet)24 ArrayList (java.util.ArrayList)18 RelationshipIndex (org.neo4j.graphdb.index.RelationshipIndex)18 WeightedPath (org.neo4j.graphalgo.WeightedPath)14 EmbeddedGraphDatabase (org.neo4j.kernel.EmbeddedGraphDatabase)14 List (java.util.List)13 Map (java.util.Map)13 PropertyContainer (org.neo4j.graphdb.PropertyContainer)12 Graph (org.neo4j.test.GraphDescription.Graph)11