Search in sources :

Example 56 with Transaction

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

the class HaCountsIT method createAnIndex.

private NewIndexDescriptor createAnIndex(HighlyAvailableGraphDatabase db, Label label, String propertyName) throws KernelException {
    try (Transaction tx = db.beginTx()) {
        Statement statement = statement(db);
        int labelId = statement.tokenWriteOperations().labelGetOrCreateForName(label.name());
        int propertyKeyId = statement.tokenWriteOperations().propertyKeyGetOrCreateForName(propertyName);
        NewIndexDescriptor index = statement.schemaWriteOperations().indexCreate(SchemaDescriptorFactory.forLabel(labelId, propertyKeyId));
        tx.success();
        return index;
    }
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Transaction(org.neo4j.graphdb.Transaction) Statement(org.neo4j.kernel.api.Statement)

Example 57 with Transaction

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

the class HaCountsIT method awaitOnline.

private long awaitOnline(HighlyAvailableGraphDatabase db, NewIndexDescriptor index) throws KernelException {
    long start = System.currentTimeMillis();
    long end = start + 3000;
    while (System.currentTimeMillis() < end) {
        try (Transaction tx = db.beginTx()) {
            switch(statement(db).readOperations().indexGetState(index)) {
                case ONLINE:
                    return indexingService(db).getIndexId(index.schema());
                case FAILED:
                    throw new IllegalStateException("Index failed instead of becoming ONLINE");
                default:
                    break;
            }
            tx.success();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            // ignored
            }
        }
    }
    throw new IllegalStateException("Index did not become ONLINE within reasonable time");
}
Also used : Transaction(org.neo4j.graphdb.Transaction)

Example 58 with Transaction

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

the class HaCountsIT method assertOnNodeCounts.

private void assertOnNodeCounts(int expectedTotalNodes, int expectedLabelledNodes, Label label, HighlyAvailableGraphDatabase db) {
    try (Transaction ignored = db.beginTx()) {
        final Statement statement = statement(db);
        final int labelId = statement.readOperations().labelGetForName(label.name());
        assertEquals(expectedTotalNodes, statement.readOperations().countsForNode(-1));
        assertEquals(expectedLabelledNodes, statement.readOperations().countsForNode(labelId));
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Statement(org.neo4j.kernel.api.Statement)

Example 59 with Transaction

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

the class HaCountsIT method createANode.

private void createANode(HighlyAvailableGraphDatabase db, Label label, String value, String property) {
    try (Transaction tx = db.beginTx()) {
        Node node = db.createNode(label);
        node.setProperty(property, value);
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node)

Example 60 with Transaction

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

the class TestBasicHaOperations method testBasicPropagationFromSlaveToMaster.

@Test
public void testBasicPropagationFromSlaveToMaster() throws Throwable {
    // given
    ManagedCluster cluster = clusterRule.startCluster();
    HighlyAvailableGraphDatabase master = cluster.getMaster();
    HighlyAvailableGraphDatabase slave = cluster.getAnySlave();
    long nodeId;
    // a node with a property
    try (Transaction tx = master.beginTx()) {
        Node node = master.createNode();
        nodeId = node.getId();
        node.setProperty("foo", "bar");
        tx.success();
    }
    cluster.sync();
    // the slave does a change
    try (Transaction tx = slave.beginTx()) {
        slave.getNodeById(nodeId).setProperty("foo", "bar2");
        tx.success();
    }
    // the master must pick up the change
    try (Transaction tx = master.beginTx()) {
        assertEquals("bar2", master.getNodeById(nodeId).getProperty("foo"));
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) ManagedCluster(org.neo4j.kernel.impl.ha.ClusterManager.ManagedCluster) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Aggregations

Transaction (org.neo4j.graphdb.Transaction)2409 Node (org.neo4j.graphdb.Node)1086 Test (org.junit.jupiter.api.Test)751 Test (org.junit.Test)607 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)352 Relationship (org.neo4j.graphdb.Relationship)307 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)302 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)241 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)177 Label (org.neo4j.graphdb.Label)154 Result (org.neo4j.graphdb.Result)142 HashMap (java.util.HashMap)105 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)104 MethodSource (org.junit.jupiter.params.provider.MethodSource)103 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)86 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)77 File (java.io.File)74 ArrayList (java.util.ArrayList)73 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)67 Path (java.nio.file.Path)64