Search in sources :

Example 41 with Relationship

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

the class PartialTransactionFailureIT method concurrentlyCommittingTransactionsMustNotRotateOutLoggedCommandsOfFailingTransaction.

@Test
public void concurrentlyCommittingTransactionsMustNotRotateOutLoggedCommandsOfFailingTransaction() throws Exception {
    final ClassGuardedAdversary adversary = new ClassGuardedAdversary(new CountingAdversary(1, false), Command.RelationshipCommand.class);
    adversary.disable();
    File storeDir = dir.graphDbDir();
    final Map<String, String> params = stringMap(GraphDatabaseSettings.pagecache_memory.name(), "8m");
    final EmbeddedGraphDatabase db = new TestEmbeddedGraphDatabase(storeDir, params) {

        @Override
        protected void create(File storeDir, Map<String, String> params, GraphDatabaseFacadeFactory.Dependencies dependencies) {
            new GraphDatabaseFacadeFactory(DatabaseInfo.COMMUNITY, CommunityEditionModule::new) {

                @Override
                protected PlatformModule createPlatform(File storeDir, Config config, Dependencies dependencies, GraphDatabaseFacade graphDatabaseFacade) {
                    return new PlatformModule(storeDir, config, databaseInfo, dependencies, graphDatabaseFacade) {

                        @Override
                        protected FileSystemAbstraction createFileSystemAbstraction() {
                            return new AdversarialFileSystemAbstraction(adversary);
                        }
                    };
                }
            }.initFacade(storeDir, params, dependencies, this);
        }
    };
    Node a, b, c, d;
    try (Transaction tx = db.beginTx()) {
        a = db.createNode();
        b = db.createNode();
        c = db.createNode();
        d = db.createNode();
        tx.success();
    }
    adversary.enable();
    CountDownLatch latch = new CountDownLatch(1);
    Thread t1 = new Thread(createRelationship(db, a, b, latch), "T1");
    Thread t2 = new Thread(createRelationship(db, c, d, latch), "T2");
    t1.start();
    t2.start();
    // Wait for both threads to get going
    t1.join(10);
    t2.join(10);
    latch.countDown();
    // Wait for the transactions to finish
    t1.join(25000);
    t2.join(25000);
    db.shutdown();
    // We should observe the store in a consistent state
    EmbeddedGraphDatabase db2 = new TestEmbeddedGraphDatabase(storeDir, params);
    try (Transaction tx = db2.beginTx()) {
        Node x = db2.getNodeById(a.getId());
        Node y = db2.getNodeById(b.getId());
        Node z = db2.getNodeById(c.getId());
        Node w = db2.getNodeById(d.getId());
        Iterator<Relationship> itrRelX = x.getRelationships().iterator();
        Iterator<Relationship> itrRelY = y.getRelationships().iterator();
        Iterator<Relationship> itrRelZ = z.getRelationships().iterator();
        Iterator<Relationship> itrRelW = w.getRelationships().iterator();
        if (itrRelX.hasNext() != itrRelY.hasNext()) {
            fail("Node x and y have inconsistent relationship counts");
        } else if (itrRelX.hasNext()) {
            Relationship rel = itrRelX.next();
            assertEquals(rel, itrRelY.next());
            assertFalse(itrRelX.hasNext());
            assertFalse(itrRelY.hasNext());
        }
        if (itrRelZ.hasNext() != itrRelW.hasNext()) {
            fail("Node z and w have inconsistent relationship counts");
        } else if (itrRelZ.hasNext()) {
            Relationship rel = itrRelZ.next();
            assertEquals(rel, itrRelW.next());
            assertFalse(itrRelZ.hasNext());
            assertFalse(itrRelW.hasNext());
        }
    } finally {
        db2.shutdown();
    }
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.internal.EmbeddedGraphDatabase) CountingAdversary(org.neo4j.adversaries.CountingAdversary) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) ClassGuardedAdversary(org.neo4j.adversaries.ClassGuardedAdversary) Config(org.neo4j.kernel.configuration.Config) Node(org.neo4j.graphdb.Node) PlatformModule(org.neo4j.kernel.impl.factory.PlatformModule) CountDownLatch(java.util.concurrent.CountDownLatch) Transaction(org.neo4j.graphdb.Transaction) Command(org.neo4j.kernel.impl.transaction.command.Command) GraphDatabaseFacadeFactory(org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory) Relationship(org.neo4j.graphdb.Relationship) GraphDatabaseFacade(org.neo4j.kernel.impl.factory.GraphDatabaseFacade) File(java.io.File) Map(java.util.Map) MapUtil.stringMap(org.neo4j.helpers.collection.MapUtil.stringMap) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) Test(org.junit.Test)

Example 42 with Relationship

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

the class Rmnode method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
    NodeOrRelationship node = null;
    if (parser.arguments().isEmpty()) {
        node = getCurrent(session);
    } else {
        long id = parseInt(parser.arguments().get(0));
        try {
            node = wrap(getNodeById(id));
        } catch (NotFoundException e) {
            throw new ShellException("No node " + id + " found");
        }
    }
    if (!node.isNode()) {
        out.println("Please select a node to delete");
        return Continuation.INPUT_COMPLETE;
    }
    boolean forceDeletion = parser.options().containsKey("f");
    if (forceDeletion) {
        for (Relationship relationship : node.asNode().getRelationships()) {
            out.println("Relationship " + getDisplayName(getServer(), session, relationship, true, false) + " deleted");
            relationship.delete();
        }
    }
    if (node.asNode().hasRelationship()) {
        throw new ShellException(getDisplayName(getServer(), session, node.asNode(), false) + " cannot be deleted because it still has relationships. Use -f to force deletion of its relationships");
    }
    node.asNode().delete();
    return Continuation.INPUT_COMPLETE;
}
Also used : Relationship(org.neo4j.graphdb.Relationship) NotFoundException(org.neo4j.graphdb.NotFoundException) ShellException(org.neo4j.shell.ShellException)

Example 43 with Relationship

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

the class Rmrel method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
    assertCurrentIsNode(session);
    if (parser.arguments().isEmpty()) {
        throw new ShellException("Must supply relationship id to delete as the first argument");
    }
    Node currentNode = this.getCurrent(session).asNode();
    Relationship rel = findRel(currentNode, Long.parseLong(parser.arguments().get(0)));
    rel.delete();
    Node otherNode = rel.getOtherNode(currentNode);
    boolean deleteOtherNodeIfEmpty = parser.options().containsKey("d");
    if (deleteOtherNodeIfEmpty && !otherNode.hasRelationship()) {
        out.println("Also deleted " + getDisplayName(getServer(), session, otherNode, false) + " due to it not having any relationships left");
        otherNode.delete();
    }
    return Continuation.INPUT_COMPLETE;
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) ShellException(org.neo4j.shell.ShellException)

Example 44 with Relationship

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

the class Ls method displayRelationships.

private void displayRelationships(AppCommandParser parser, NodeOrRelationship thing, Session session, Output out, boolean verbose, boolean quiet, Map<String, Object> filterMap, boolean caseInsensitiveFilters, boolean looseFilters, boolean brief) throws ShellException, RemoteException {
    boolean sortByType = parser.options().containsKey("s");
    Node node = thing.asNode();
    Iterable<Relationship> relationships = getRelationships(node, filterMap, caseInsensitiveFilters, looseFilters, sortByType | brief);
    if (brief) {
        Iterator<Relationship> iterator = relationships.iterator();
        if (!iterator.hasNext()) {
            return;
        }
        Relationship sampleRelationship = iterator.next();
        RelationshipType lastType = sampleRelationship.getType();
        int currentCounter = 1;
        while (iterator.hasNext()) {
            Relationship rel = iterator.next();
            if (!rel.isType(lastType)) {
                displayBriefRelationships(thing, session, out, sampleRelationship, currentCounter);
                sampleRelationship = rel;
                lastType = sampleRelationship.getType();
                currentCounter = 1;
            } else {
                currentCounter++;
            }
        }
        displayBriefRelationships(thing, session, out, sampleRelationship, currentCounter);
    } else {
        Iterator<Relationship> iterator = relationships.iterator();
        if (parser.options().containsKey("m")) {
            iterator = wrapInLimitingIterator(parser, iterator, filterMap, caseInsensitiveFilters, looseFilters);
        }
        while (iterator.hasNext()) {
            Relationship rel = iterator.next();
            StringBuffer buf = new StringBuffer(getDisplayName(getServer(), session, thing, true));
            String relDisplay = quiet ? "" : getDisplayName(getServer(), session, rel, verbose, true);
            buf.append(withArrows(rel, relDisplay, thing.asNode()));
            buf.append(getDisplayName(getServer(), session, rel.getOtherNode(node), true));
            out.println(buf);
        }
    }
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) RelationshipType(org.neo4j.graphdb.RelationshipType)

Example 45 with Relationship

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

the class AbstractShellIT method createRelationshipChain.

protected Relationship[] createRelationshipChain(RelationshipType type, int length) {
    try (Transaction transaction = db.beginTx()) {
        Relationship[] relationshipChain = createRelationshipChain(db.createNode(), type, length);
        transaction.success();
        return relationshipChain;
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship)

Aggregations

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