Search in sources :

Example 11 with Transaction

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

the class TestGraphProperties method testEquals.

@Test
public void testEquals() {
    GraphDatabaseAPI db = (GraphDatabaseAPI) factory.newImpermanentDatabase();
    PropertyContainer graphProperties = properties(db);
    try (Transaction tx = db.beginTx()) {
        graphProperties.setProperty("test", "test");
        tx.success();
    }
    assertEquals(graphProperties, properties(db));
    db.shutdown();
    db = (GraphDatabaseAPI) factory.newImpermanentDatabase();
    assertFalse(graphProperties.equals(properties(db)));
    db.shutdown();
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.Test)

Example 12 with Transaction

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

the class TestGraphProperties method produceUncleanStore.

private EphemeralFileSystemAbstraction produceUncleanStore(EphemeralFileSystemAbstraction fileSystem, File storeDir) {
    GraphDatabaseService db = new TestGraphDatabaseFactory().setFileSystem(fileSystem).newImpermanentDatabase(storeDir);
    Transaction tx = db.beginTx();
    Node node = db.createNode();
    node.setProperty("name", "Something");
    properties((GraphDatabaseAPI) db).setProperty("prop", "Some value");
    tx.success();
    tx.close();
    EphemeralFileSystemAbstraction snapshot = fileSystem.snapshot();
    db.shutdown();
    return snapshot;
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) Node(org.neo4j.graphdb.Node) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory)

Example 13 with Transaction

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

the class RelationshipChainExplorerTest method createStoreWithOneHighDegreeNodeAndSeveralDegreeTwoNodes.

private StoreAccess createStoreWithOneHighDegreeNodeAndSeveralDegreeTwoNodes(int nDegreeTwoNodes) {
    File storeDirectory = storeLocation.graphDbDir();
    GraphDatabaseService database = new TestGraphDatabaseFactory().newEmbeddedDatabaseBuilder(storeDirectory).setConfig(GraphDatabaseSettings.record_format, getRecordFormatName()).newGraphDatabase();
    try (Transaction transaction = database.beginTx()) {
        Node denseNode = database.createNode();
        for (int i = 0; i < nDegreeTwoNodes; i++) {
            Node degreeTwoNode = database.createNode();
            Node leafNode = database.createNode();
            if (i % 2 == 0) {
                denseNode.createRelationshipTo(degreeTwoNode, TestRelationshipType.CONNECTED);
            } else {
                degreeTwoNode.createRelationshipTo(denseNode, TestRelationshipType.CONNECTED);
            }
            degreeTwoNode.createRelationshipTo(leafNode, TestRelationshipType.CONNECTED);
        }
        transaction.success();
    }
    database.shutdown();
    PageCache pageCache = pageCacheRule.getPageCache(fileSystemRule.get());
    StoreAccess storeAccess = new StoreAccess(fileSystemRule.get(), pageCache, storeDirectory, Config.empty());
    return storeAccess.initialize();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) StoreAccess(org.neo4j.kernel.impl.store.StoreAccess) Node(org.neo4j.graphdb.Node) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) File(java.io.File) PageCache(org.neo4j.io.pagecache.PageCache)

Example 14 with Transaction

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

the class RecoveryIT method recoveryShouldFixPartiallyAppliedSchemaIndexUpdates.

@Test(timeout = 60_000)
public void recoveryShouldFixPartiallyAppliedSchemaIndexUpdates() {
    Label label = Label.label("Foo");
    String property = "Bar";
    // cause failure during 'relationship.delete()' command application
    ClassGuardedAdversary adversary = new ClassGuardedAdversary(new CountingAdversary(1, true), Command.RelationshipCommand.class);
    adversary.disable();
    File storeDir = directory.graphDbDir();
    GraphDatabaseService db = AdversarialPageCacheGraphDatabaseFactory.create(fileSystemRule.get(), adversary).newEmbeddedDatabaseBuilder(storeDir).newGraphDatabase();
    try {
        try (Transaction tx = db.beginTx()) {
            db.schema().constraintFor(label).assertPropertyIsUnique(property).create();
            tx.success();
        }
        long relationshipId = createRelationship(db);
        TransactionFailureException txFailure = null;
        try (Transaction tx = db.beginTx()) {
            Node node = db.createNode(label);
            node.setProperty(property, "B");
            // this should fail because of the adversary
            db.getRelationshipById(relationshipId).delete();
            tx.success();
            adversary.enable();
        } catch (TransactionFailureException e) {
            txFailure = e;
        }
        assertNotNull(txFailure);
        adversary.disable();
        // heal the db so it is possible to inspect the data
        healthOf(db).healed();
        // now we can observe partially committed state: node is in the index and relationship still present
        try (Transaction tx = db.beginTx()) {
            assertNotNull(findNode(db, label, property, "B"));
            assertNotNull(db.getRelationshipById(relationshipId));
            tx.success();
        }
        // panic the db again to force recovery on the next startup
        healthOf(db).panic(txFailure.getCause());
        // restart the database, now with regular page cache
        db.shutdown();
        db = startDatabase(storeDir);
        // now we observe correct state: node is in the index and relationship is removed
        try (Transaction tx = db.beginTx()) {
            assertNotNull(findNode(db, label, property, "B"));
            assertRelationshipNotExist(db, relationshipId);
            tx.success();
        }
    } finally {
        db.shutdown();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) CountingAdversary(org.neo4j.adversaries.CountingAdversary) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) ClassGuardedAdversary(org.neo4j.adversaries.ClassGuardedAdversary) Transaction(org.neo4j.graphdb.Transaction) Command(org.neo4j.kernel.impl.transaction.command.Command) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) File(java.io.File) Test(org.junit.Test)

Example 15 with Transaction

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

the class CompositeIndexingIT method shouldSeeAllNodesAddedBeforeTransaction.

@Test
public void shouldSeeAllNodesAddedBeforeTransaction() throws Exception {
    if (// this test does not make any sense for UNIQUE indexes
    index.type() != UNIQUE) {
        long nodeID1 = createNode();
        long nodeID2 = createNode();
        long nodeID3 = createNode();
        try (Transaction ignore = graphDatabaseAPI.beginTx()) {
            PrimitiveLongIterator resultIterator = seek();
            Set<Long> result = PrimitiveLongCollections.toSet(resultIterator);
            assertThat(result, contains(nodeID1, nodeID2, nodeID3));
        }
    }
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) Transaction(org.neo4j.graphdb.Transaction) 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