Search in sources :

Example 46 with Transaction

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

the class IndexingServiceIntegrationTest method createData.

private void createData(GraphDatabaseService database, int numberOfNodes) {
    int index = 0;
    while (index < numberOfNodes) {
        try (Transaction transaction = database.beginTx()) {
            Node node = database.createNode(Label.label(FOOD_LABEL), Label.label(CLOTHES_LABEL), Label.label(WEATHER_LABEL));
            node.setProperty(PROPERTY_NAME, "Node" + index++);
            transaction.success();
        }
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node)

Example 47 with Transaction

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

the class TestTransactionEventsWithIndexes method nodeCanBeLegacyIndexedInBeforeCommit.

@Test
public void nodeCanBeLegacyIndexedInBeforeCommit() throws Exception {
    // Given we have a legacy index...
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    final Index<Node> index;
    try (Transaction tx = db.beginTx()) {
        index = db.index().forNodes("index");
        tx.success();
    }
    // ... and a transaction event handler that likes to add nodes to that index
    db.registerTransactionEventHandler(new TransactionEventHandler<Object>() {

        @Override
        public Object beforeCommit(TransactionData data) throws Exception {
            Iterator<Node> nodes = data.createdNodes().iterator();
            if (nodes.hasNext()) {
                Node node = nodes.next();
                index.add(node, "key", "value");
            }
            return null;
        }

        @Override
        public void afterCommit(TransactionData data, Object state) {
        }

        @Override
        public void afterRollback(TransactionData data, Object state) {
        }
    });
    // When we create a node...
    try (Transaction tx = db.beginTx()) {
        db.schema().awaitIndexesOnline(10, TimeUnit.SECONDS);
        Node node = db.createNode();
        node.setProperty("random", 42);
        tx.success();
    }
    // Then we should be able to look it up through the index.
    try (Transaction ignore = db.beginTx()) {
        Node node = single(index.get("key", "value"));
        assertThat(node.getProperty("random"), is((Object) 42));
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Iterator(java.util.Iterator) TransactionData(org.neo4j.graphdb.event.TransactionData) Test(org.junit.Test)

Example 48 with Transaction

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

the class TestLuceneBatchInsert method shouldCreateAutoIndexThatIsUsableInEmbedded.

@Test
public void shouldCreateAutoIndexThatIsUsableInEmbedded() throws Exception {
    BatchInserterIndexProvider provider = new LuceneBatchInserterIndexProviderNewImpl(inserter);
    BatchInserterIndex index = provider.nodeIndex("node_auto_index", EXACT_CONFIG);
    long id = inserter.createNode(null);
    Map<String, Object> props = new HashMap<>();
    props.put("name", "peter");
    index.add(id, props);
    index.flush();
    provider.shutdown();
    shutdownInserter();
    switchToGraphDatabaseService(configure(GraphDatabaseSettings.node_keys_indexable, "name"), configure(GraphDatabaseSettings.relationship_keys_indexable, "relProp1,relProp2"), configure(GraphDatabaseSettings.node_auto_indexing, "true"), configure(GraphDatabaseSettings.relationship_auto_indexing, "true"));
    try (Transaction tx = db.beginTx()) {
        // Create the primitives
        Node node1 = db.createNode();
        // Add indexable and non-indexable properties
        node1.setProperty("name", "bob");
        // Make things persistent
        tx.success();
    }
    try (Transaction tx = db.beginTx()) {
        assertTrue(db.index().getNodeAutoIndexer().getAutoIndex().get("name", "peter").hasNext());
        assertTrue(db.index().getNodeAutoIndexer().getAutoIndex().get("name", "bob").hasNext());
        assertFalse(db.index().getNodeAutoIndexer().getAutoIndex().get("name", "joe").hasNext());
        tx.success();
    }
}
Also used : LuceneBatchInserterIndexProviderNewImpl(org.neo4j.index.impl.lucene.legacy.LuceneBatchInserterIndexProviderNewImpl) LuceneBatchInserterIndexProvider(org.neo4j.index.lucene.unsafe.batchinsert.LuceneBatchInserterIndexProvider) Transaction(org.neo4j.graphdb.Transaction) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 49 with Transaction

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

the class TestLuceneBatchInsert method testSome.

@Test
public void testSome() throws Exception {
    BatchInserterIndexProvider provider = new LuceneBatchInserterIndexProviderNewImpl(inserter);
    String indexName = "users";
    BatchInserterIndex index = provider.nodeIndex(indexName, EXACT_CONFIG);
    Map<Integer, Long> ids = new HashMap<>();
    int count = 5;
    for (int i = 0; i < count; i++) {
        long id = inserter.createNode(null);
        index.add(id, map("name", "Joe" + i, "other", "Schmoe"));
        ids.put(i, id);
    }
    index.flush();
    for (int i = 0; i < count; i++) {
        assertContains(index.get("name", "Joe" + i), ids.get(i));
    }
    assertContains(index.query("name:Joe0 AND other:Schmoe"), ids.get(0));
    assertContains(index.query("name", "Joe*"), ids.values().toArray(new Long[ids.size()]));
    provider.shutdown();
    switchToGraphDatabaseService();
    try (Transaction transaction = db.beginTx()) {
        IndexManager indexManager = db.index();
        assertFalse(indexManager.existsForRelationships(indexName));
        assertTrue(indexManager.existsForNodes(indexName));
        assertNotNull(indexManager.forNodes(indexName));
        Index<Node> dbIndex = db.index().forNodes("users");
        for (int i = 0; i < count; i++) {
            assertContains(dbIndex.get("name", "Joe" + i), db.getNodeById(ids.get(i)));
        }
        Collection<Node> nodes = new ArrayList<>();
        for (long id : ids.values()) {
            nodes.add(db.getNodeById(id));
        }
        assertContains(dbIndex.query("name", "Joe*"), nodes.toArray(new Node[nodes.size()]));
        assertContains(dbIndex.query("name:Joe0 AND other:Schmoe"), db.getNodeById(ids.get(0)));
        transaction.success();
    }
}
Also used : HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) LuceneBatchInserterIndexProviderNewImpl(org.neo4j.index.impl.lucene.legacy.LuceneBatchInserterIndexProviderNewImpl) IndexManager(org.neo4j.graphdb.index.IndexManager) LuceneBatchInserterIndexProvider(org.neo4j.index.lucene.unsafe.batchinsert.LuceneBatchInserterIndexProvider) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.Test)

Example 50 with Transaction

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

the class TestLuceneBatchInsert method testNumericValues.

@Test
public void testNumericValues() {
    BatchInserterIndexProvider provider = new LuceneBatchInserterIndexProviderNewImpl(inserter);
    BatchInserterIndex index = provider.nodeIndex("mine", EXACT_CONFIG);
    long node1 = inserter.createNode(null);
    index.add(node1, map("number", numeric(45)));
    long node2 = inserter.createNode(null);
    index.add(node2, map("number", numeric(21)));
    index.flush();
    assertContains(index.query("number", newIntRange("number", 21, 50, true, true)), node1, node2);
    provider.shutdown();
    switchToGraphDatabaseService();
    try (Transaction transaction = db.beginTx()) {
        Node n1 = db.getNodeById(node1);
        db.getNodeById(node2);
        Index<Node> idx = db.index().forNodes("mine");
        assertContains(idx.query("number", newIntRange("number", 21, 45, false, true)), n1);
        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)

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