Search in sources :

Example 86 with Transaction

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

the class Inserter method getIndex.

private static Index<Node> getIndex(GraphDatabaseService db) {
    try (Transaction transaction = db.beginTx()) {
        Index<Node> index = db.index().forNodes("myIndex");
        transaction.success();
        return index;
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node)

Example 87 with Transaction

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

the class TestLuceneBatchInsert method testFulltext.

@Test
public void testFulltext() {
    BatchInserterIndexProvider provider = new LuceneBatchInserterIndexProviderNewImpl(inserter);
    String name = "users";
    BatchInserterIndex index = provider.nodeIndex(name, stringMap("type", "fulltext"));
    long id1 = inserter.createNode(null);
    index.add(id1, map("name", "Mattias Persson", "email", "something@somewhere", "something", "bad"));
    long id2 = inserter.createNode(null);
    index.add(id2, map("name", "Lars PerssoN"));
    index.flush();
    assertContains(index.get("name", "Mattias Persson"), id1);
    assertContains(index.query("name", "mattias"), id1);
    assertContains(index.query("name", "bla"));
    assertContains(index.query("name", "persson"), id1, id2);
    assertContains(index.query("email", "*@*"), id1);
    assertContains(index.get("something", "bad"), id1);
    long id3 = inserter.createNode(null);
    index.add(id3, map("name", new String[] { "What Ever", "Anything" }));
    index.flush();
    assertContains(index.get("name", "What Ever"), id3);
    assertContains(index.get("name", "Anything"), id3);
    provider.shutdown();
    switchToGraphDatabaseService();
    try (Transaction transaction = db.beginTx()) {
        Index<Node> dbIndex = db.index().forNodes(name);
        Node node1 = db.getNodeById(id1);
        Node node2 = db.getNodeById(id2);
        assertContains(dbIndex.query("name", "persson"), node1, node2);
        transaction.success();
    }
}
Also used : LuceneBatchInserterIndexProviderNewImpl(org.neo4j.index.impl.lucene.legacy.LuceneBatchInserterIndexProviderNewImpl) LuceneBatchInserterIndexProvider(org.neo4j.index.lucene.unsafe.batchinsert.LuceneBatchInserterIndexProvider) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 88 with Transaction

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

the class Fixtures method applyTo.

public void applyTo(InProcessServerControls controls) {
    GraphDatabaseService db = controls.graph();
    for (String fixtureStatement : fixtureStatements) {
        try (Transaction tx = db.beginTx()) {
            db.execute(fixtureStatement);
            tx.success();
        }
    }
    for (Function<GraphDatabaseService, Void> fixtureFunction : fixtureFunctions) {
        fixtureFunction.apply(db);
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction)

Example 89 with Transaction

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

the class InProcessBuilderTest method shouldRunBuilderOnExistingStoreDir.

@Test
public void shouldRunBuilderOnExistingStoreDir() throws Exception {
    // When
    // create graph db with one node upfront
    Path dir = Files.createTempDirectory(getClass().getSimpleName() + "_shouldRunBuilderOnExistingStorageDir");
    File storeDir = Config.embeddedDefaults(stringMap(DatabaseManagementSystemSettings.data_directory.name(), dir.toString())).get(DatabaseManagementSystemSettings.database_path);
    try {
        GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase(storeDir);
        try {
            db.execute("create ()");
        } finally {
            db.shutdown();
        }
        try (ServerControls server = getTestServerBuilder(testDir.directory()).copyFrom(dir.toFile()).newServer()) {
            // Then
            try (Transaction tx = server.graph().beginTx()) {
                ResourceIterable<Node> allNodes = Iterables.asResourceIterable(server.graph().getAllNodes());
                assertTrue(Iterables.count(allNodes) > 0);
                // When: create another node
                server.graph().createNode();
                tx.success();
            }
        }
        // Then: we still only have one node since the server is supposed to work on a copy
        db = new TestGraphDatabaseFactory().newEmbeddedDatabase(storeDir);
        try {
            try (Transaction tx = db.beginTx()) {
                assertEquals(1, Iterables.count(db.getAllNodes()));
                tx.success();
            }
        } finally {
            db.shutdown();
        }
    } finally {
        FileUtils.forceDelete(dir.toFile());
    }
}
Also used : Path(java.nio.file.Path) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) JsonNode(org.codehaus.jackson.JsonNode) Node(org.neo4j.graphdb.Node) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) File(java.io.File) Test(org.junit.Test)

Example 90 with Transaction

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

the class GraphDbHelper method setRelationshipProperties.

public void setRelationshipProperties(long relationshipId, Map<String, Object> properties) {
    try (Transaction tx = database.getGraph().beginTransaction(implicit, AnonymousContext.writeToken())) {
        Relationship relationship = database.getGraph().getRelationshipById(relationshipId);
        for (Map.Entry<String, Object> propertyEntry : properties.entrySet()) {
            relationship.setProperty(propertyEntry.getKey(), propertyEntry.getValue());
        }
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Relationship(org.neo4j.graphdb.Relationship) Map(java.util.Map)

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