Search in sources :

Example 91 with Node

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

the class Mknode method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws Exception {
    GraphDatabaseAPI db = getServer().getDb();
    Node node = db.createNode(parseLabels(parser));
    setProperties(node, parser.option("np", null));
    if (parser.options().containsKey("cd")) {
        cdTo(session, node);
    }
    if (parser.options().containsKey("v")) {
        out.println("Node " + getDisplayName(getServer(), session, node, false) + " created");
    }
    return Continuation.INPUT_COMPLETE;
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Node(org.neo4j.graphdb.Node)

Example 92 with Node

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

the class Rm method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws Exception {
    NodeOrRelationship thing = getCurrent(session);
    boolean forProperty = parser.options().containsKey("p");
    boolean forLabel = parser.options().containsKey("l");
    if (forProperty || !forLabel) {
        // Property
        if (parser.arguments().isEmpty()) {
            throw new ShellException("Must supply the property key or label name to " + "remove, like: rm title");
        }
        String key = parser.arguments().get(0);
        if (thing.removeProperty(key) == null) {
            out.println("Property '" + key + "' not found");
        }
    } else {
        Node node = thing.asNode();
        for (Label label : parseLabels(parser)) {
            node.removeLabel(label);
        }
    }
    return Continuation.INPUT_COMPLETE;
}
Also used : Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) ShellException(org.neo4j.shell.ShellException)

Example 93 with Node

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

the class AbstractShellIT method getCurrentNode.

protected Node getCurrentNode() throws RemoteException, ShellException {
    Serializable current = shellServer.interpretVariable(shellClient.getId(), Variables.CURRENT_KEY);
    int nodeId = parseInt(current.toString().substring(1));
    try (Transaction tx = db.beginTx()) {
        Node nodeById = db.getNodeById(nodeId);
        tx.success();
        return nodeById;
    }
}
Also used : Serializable(java.io.Serializable) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node)

Example 94 with Node

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

the class ClusterLocksIT method takeExclusiveLockOnSameNodeAfterSwitch.

private void takeExclusiveLockOnSameNodeAfterSwitch(Label testLabel, HighlyAvailableGraphDatabase master, HighlyAvailableGraphDatabase db) throws EntityNotFoundException {
    try (Transaction transaction = db.beginTx()) {
        Node node = getNode(master, testLabel);
        transaction.acquireWriteLock(node);
        node.setProperty("key", "value");
        transaction.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node)

Example 95 with Node

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

the class TransactionThroughMasterSwitchStressIT method oneRound.

private void oneRound() throws Throwable {
    // GIVEN a cluster and a node
    final String key = "key";
    ManagedCluster cluster = clusterRule.startCluster();
    final GraphDatabaseService master = cluster.getMaster();
    final long nodeId = createNode(master);
    cluster.sync();
    // and a bunch of workers contending on that node, each changing it
    Workers<Runnable> transactors = new Workers<>("Transactors");
    final AtomicInteger successes = new AtomicInteger();
    final AtomicBoolean end = new AtomicBoolean();
    for (int i = 0; i < 10; i++) {
        transactors.start(new Runnable() {

            @Override
            public void run() {
                Random random = ThreadLocalRandom.current();
                while (!end.get()) {
                    boolean committed = true;
                    try (Transaction tx = master.beginTx()) {
                        Node node = master.getNodeById(nodeId);
                        // Acquiring lock, read int property value, increment, set incremented int property
                        // should not break under any circumstances.
                        tx.acquireWriteLock(node);
                        node.setProperty(key, (Integer) node.getProperty(key, 0) + 1);
                        // Throw in relationship for good measure
                        node.createRelationshipTo(master.createNode(), TEST);
                        Thread.sleep(random.nextInt(1_000));
                        tx.success();
                    } catch (Throwable e) {
                        // It's OK
                        committed = false;
                    }
                    if (committed) {
                        successes.incrementAndGet();
                    }
                }
            }
        });
    }
    // WHEN entering a period of induced cluster instabilities
    reelectTheSameMasterMakingItGoToPendingAndBack(cluster);
    // ... letting transactions run a bit after the role switch as well.
    long targetSuccesses = successes.get() + 20;
    while (successes.get() < targetSuccesses) {
        Thread.sleep(100);
    }
    end.set(true);
    transactors.awaitAndThrowOnError(RuntimeException.class);
    // THEN verify that the count is equal to the number of successful transactions
    assertEquals(successes.get(), getNodePropertyValue(master, nodeId, key));
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Transaction(org.neo4j.graphdb.Transaction) Workers(org.neo4j.unsafe.impl.batchimport.cache.idmapping.string.Workers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ManagedCluster(org.neo4j.kernel.impl.ha.ClusterManager.ManagedCluster)

Aggregations

Node (org.neo4j.graphdb.Node)1281 Test (org.junit.Test)781 Transaction (org.neo4j.graphdb.Transaction)540 Relationship (org.neo4j.graphdb.Relationship)375 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)146 NotFoundException (org.neo4j.graphdb.NotFoundException)78 File (java.io.File)65 LinkedList (java.util.LinkedList)60 RelationshipType (org.neo4j.graphdb.RelationshipType)58 HashMap (java.util.HashMap)57 Label (org.neo4j.graphdb.Label)57 Path (org.neo4j.graphdb.Path)52 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)46 HashSet (java.util.HashSet)45 Map (java.util.Map)45 WeightedPath (org.neo4j.graphalgo.WeightedPath)37 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)35 ArrayList (java.util.ArrayList)30 List (java.util.List)27 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)26