Search in sources :

Example 51 with Transaction

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

the class TestMigrateToDenseNodeSupport method migrateDbWithDenseNodes.

@Test
public void migrateDbWithDenseNodes() throws Exception {
    // migrate
    new TestGraphDatabaseFactory().newEmbeddedDatabaseBuilder(dir).setConfig(allow_store_upgrade, "true").newGraphDatabase().shutdown();
    // check consistency
    assertConsistentStore(dir);
    // open again to do extra checks
    GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabaseBuilder(dir).newGraphDatabase();
    try (Transaction tx = db.beginTx()) {
        ResourceIterator<Node> allNodesWithLabel = db.findNodes(referenceNode);
        Node refNode = Iterators.single(allNodesWithLabel);
        int sparseCount = 0;
        for (Relationship relationship : refNode.getRelationships(Types.SPARSE, OUTGOING)) {
            verifySparseNode(db, relationship.getEndNode());
            sparseCount++;
        }
        int denseCount = 0;
        for (Relationship relationship : refNode.getRelationships(Types.DENSE, OUTGOING)) {
            verifyDenseNode(db, relationship.getEndNode());
            denseCount++;
        }
        assertEquals(10, sparseCount);
        assertEquals(10, denseCount);
        tx.success();
    }
    db.shutdown();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) Test(org.junit.Test)

Example 52 with Transaction

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

the class TestPropertyReadOnNewEntityBeforeLockRelease method initializeIndex.

@BeforeClass
public static void initializeIndex() throws Exception {
    try (Transaction tx = db.beginTx()) {
        Node node = db.createNode();
        db.index().forNodes(INDEX_NAME).add(node, "foo", "bar");
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) BeforeClass(org.junit.BeforeClass)

Example 53 with Transaction

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

the class RestoreDatabaseCommandTest method shouldAllowForcedCopyOverAnExistingDatabase.

@Test
public void shouldAllowForcedCopyOverAnExistingDatabase() throws Exception {
    // given
    String databaseName = "to";
    Config config = configWith(Config.empty(), databaseName, directory.absolutePath().getAbsolutePath());
    File fromPath = new File(directory.absolutePath(), "from");
    File toPath = config.get(DatabaseManagementSystemSettings.database_path);
    int fromNodeCount = 10;
    int toNodeCount = 20;
    createDbAt(fromPath, fromNodeCount);
    createDbAt(toPath, toNodeCount);
    // when
    new RestoreDatabaseCommand(fileSystemRule.get(), fromPath, config, databaseName, true).execute();
    // then
    GraphDatabaseService copiedDb = new GraphDatabaseFactory().newEmbeddedDatabase(toPath);
    try (Transaction ignored = copiedDb.beginTx()) {
        assertEquals(fromNodeCount, Iterables.count(copiedDb.getAllNodes()));
    }
    copiedDb.shutdown();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) GraphDatabaseFactory(org.neo4j.graphdb.factory.GraphDatabaseFactory) Config(org.neo4j.kernel.configuration.Config) File(java.io.File) Test(org.junit.Test)

Example 54 with Transaction

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

the class ConcurrentInstanceStartupIT method concurrentStartupShouldWork.

@Test
public void concurrentStartupShouldWork() throws Exception {
    // Ensures that the instances don't race to create the test's base directory and only care about their own.
    testDirectory.directory("nothingToSeeHereMoveAlong");
    StringBuffer initialHostsBuffer = new StringBuffer("127.0.0.1:5001");
    for (int i = 2; i <= INSTANCE_COUNT; i++) {
        initialHostsBuffer.append(",127.0.0.1:500" + i);
    }
    final String initialHosts = initialHostsBuffer.toString();
    final CyclicBarrier barrier = new CyclicBarrier(INSTANCE_COUNT);
    final List<Thread> daThreads = new ArrayList<Thread>(INSTANCE_COUNT);
    final HighlyAvailableGraphDatabase[] dbs = new HighlyAvailableGraphDatabase[INSTANCE_COUNT];
    for (int i = 1; i <= INSTANCE_COUNT; i++) {
        final int finalI = i;
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    barrier.await();
                    dbs[finalI - 1] = startDbAtBase(finalI, initialHosts);
                } catch (InterruptedException | BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        daThreads.add(t);
        t.start();
    }
    for (Thread daThread : daThreads) {
        daThread.join();
    }
    boolean masterDone = false;
    for (HighlyAvailableGraphDatabase db : dbs) {
        if (db.isMaster()) {
            if (masterDone) {
                throw new Exception("Two masters discovered");
            }
            masterDone = true;
        }
        try (Transaction tx = db.beginTx()) {
            db.createNode();
            tx.success();
        }
    }
    assertTrue(masterDone);
    for (HighlyAvailableGraphDatabase db : dbs) {
        db.shutdown();
    }
}
Also used : ArrayList(java.util.ArrayList) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.Test)

Example 55 with Transaction

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

the class DeletionTest method shouldDeleteRecords.

/**
     * The problem would manifest even if the transaction was performed on the Master, it would then occur when the
     * Slave pulls updates and tries to apply the transaction. The reason for the test to run transactions against the
     * Slave is because it makes guarantees for when the master has to apply the transaction.
     */
@Test
public void shouldDeleteRecords() throws Throwable {
    // given
    ManagedCluster cluster = clusterRule.startCluster();
    HighlyAvailableGraphDatabase master = cluster.getMaster();
    HighlyAvailableGraphDatabase slave = cluster.getAnySlave();
    Relationship rel;
    try (Transaction tx = slave.beginTx()) {
        rel = slave.createNode().createRelationshipTo(slave.createNode(), withName("FOO"));
        tx.success();
    }
    try (Transaction transaction = master.beginTx()) {
        assertNotNull(master.getRelationshipById(rel.getId()));
    }
    // when
    try (Transaction tx = slave.beginTx()) {
        rel.delete();
        tx.success();
    }
    // then - there should have been no exceptions
    slave.shutdown();
    master.shutdown();
}
Also used : Transaction(org.neo4j.graphdb.Transaction) ManagedCluster(org.neo4j.kernel.impl.ha.ClusterManager.ManagedCluster) Relationship(org.neo4j.graphdb.Relationship) 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