Search in sources :

Example 91 with GraphDatabaseService

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

the class LuceneRecoveryIT method testHardCoreRecovery.

@Test
public void testHardCoreRecovery() throws Exception {
    String path = "target/hcdb";
    File hcdb = new File(path);
    FileUtils.deleteRecursively(hcdb);
    Process process = Runtime.getRuntime().exec(new String[] { ProcessUtil.getJavaExecutable().toString(), "-cp", ProcessUtil.getClassPath(), Inserter.class.getName(), path });
    // Let it run for a while and then kill it, and wait for it to die
    awaitFile(new File(path, "started"));
    Thread.sleep(5000);
    process.destroy();
    process.waitFor();
    final GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase(hcdb);
    try (Transaction transaction = db.beginTx()) {
        assertTrue(db.index().existsForNodes("myIndex"));
        Index<Node> index = db.index().forNodes("myIndex");
        for (Node node : db.getAllNodes()) {
            for (String key : node.getPropertyKeys()) {
                String value = (String) node.getProperty(key);
                boolean found = false;
                for (Node indexedNode : index.get(key, value)) {
                    if (indexedNode.equals(node)) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    throw new IllegalStateException(node + " has property '" + key + "'='" + value + "', but not in index");
                }
            }
        }
    } catch (Throwable e) {
        if (Exceptions.contains(e, CorruptIndexException.class) || exceptionContainsStackTraceElementFromPackage(e, "org.apache.lucene")) {
            // On some machines and during some circumstances a lucene index may become
            // corrupted during a crash. This is out of our control and since this test
            // is about a legacy (a.k.a. manual index) the db cannot just re-populate the
            // index automatically. We have to consider this an OK scenario and we cannot
            // verify the index any further if it happens.
            System.err.println("Lucene exception happened during recovery after a real crash. " + "It may be that the index is corrupt somehow and this is out of control and not " + "something this test can reall improve on right now. Printing the exception for reference");
            e.printStackTrace();
            return;
        }
        // This was another unknown exception, throw it so that the test fails with it
        throw e;
    }
    // Added due to a recovery issue where the lucene data source write wasn't released properly after recovery.
    Thread t = new Thread() {

        @Override
        public void run() {
            try (Transaction tx = db.beginTx()) {
                Index<Node> index = db.index().forNodes("myIndex");
                index.add(db.createNode(), "one", "two");
                tx.success();
            }
        }
    };
    t.start();
    t.join();
    db.shutdown();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) Transaction(org.neo4j.graphdb.Transaction) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) File(java.io.File) Test(org.junit.Test)

Example 92 with GraphDatabaseService

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

the class IndexNodeIT method already_indexed_node_should_not_fail_on_create_or_fail.

@Test
public void already_indexed_node_should_not_fail_on_create_or_fail() throws Exception {
    // Given
    final String index = indexes.newInstance(), key = "name", value = "Peter";
    GraphDatabaseService graphdb = graphdb();
    helper.createNodeIndex(index);
    Node node;
    try (Transaction tx = graphdb.beginTx()) {
        node = graphdb.createNode();
        graphdb.index().forNodes(index).add(node, key, value);
        tx.success();
    }
    // When & Then
    gen.get().expectedStatus(201).payloadType(MediaType.APPLICATION_JSON_TYPE).payload("{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"uri\":\"" + functionalTestHelper.nodeUri(node.getId()) + "\"}").post(functionalTestHelper.nodeIndexUri() + index + "?uniqueness=create_or_fail");
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 93 with GraphDatabaseService

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

the class IndexNodeIT method get_or_create_unique_node_if_already_existing.

@Documented("Get or create unique node (existing).\n" + "\n" + "Here,\n" + "a node is not created but the existing unique node returned, since another node\n" + "is indexed with the same data already. The node data returned is then that of the\n" + "already existing node.")
@Test
public void get_or_create_unique_node_if_already_existing() throws Exception {
    final String index = indexes.newInstance(), key = "name", value = "Peter";
    GraphDatabaseService graphdb = graphdb();
    try (Transaction tx = graphdb().beginTx()) {
        Node peter = graphdb.createNode();
        peter.setProperty(key, value);
        peter.setProperty("sequence", 1);
        graphdb.index().forNodes(index).add(peter, key, value);
        tx.success();
    }
    helper.createNodeIndex(index);
    ResponseEntity response = gen().expectedStatus(200).payloadType(MediaType.APPLICATION_JSON_TYPE).payload("{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"properties\": {\"" + key + "\": \"" + value + "\", \"sequence\": 2}}").post(functionalTestHelper.nodeIndexUri() + index + "?uniqueness=get_or_create");
    Map<String, Object> result = JsonHelper.jsonToMap(response.entity());
    Map<String, Object> data = assertCast(Map.class, result.get("data"));
    assertEquals(value, data.get(key));
    assertEquals(1, data.get("sequence"));
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) ResponseEntity(org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test)

Example 94 with GraphDatabaseService

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

the class IndexNodeIT method create_a_unique_node_or_return_fail___fail.

@Documented("Create a unique node or return fail (fail).\n" + "\n" + "Here, in case\n" + "of an already existing node, an error should be returned. In this\n" + "example, an existing node indexed with the same data\n" + "is found and an error is returned.")
@Test
public void create_a_unique_node_or_return_fail___fail() throws Exception {
    final String index = indexes.newInstance(), key = "name", value = "Peter";
    GraphDatabaseService graphdb = graphdb();
    helper.createNodeIndex(index);
    try (Transaction tx = graphdb.beginTx()) {
        Node peter = graphdb.createNode();
        peter.setProperty(key, value);
        peter.setProperty("sequence", 1);
        graphdb.index().forNodes(index).add(peter, key, value);
        tx.success();
    }
    RestRequest.req();
    ResponseEntity response = gen.get().expectedStatus(409).payloadType(MediaType.APPLICATION_JSON_TYPE).payload("{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"properties\": {\"" + key + "\": \"" + value + "\", \"sequence\": 2}}").post(functionalTestHelper.nodeIndexUri() + index + "?uniqueness=create_or_fail");
    Map<String, Object> result = JsonHelper.jsonToMap(response.entity());
    Map<String, Object> data = assertCast(Map.class, result.get("data"));
    assertEquals(value, data.get(key));
    assertEquals(1, data.get("sequence"));
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) ResponseEntity(org.neo4j.server.rest.RESTRequestGenerator.ResponseEntity) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test)

Example 95 with GraphDatabaseService

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

the class IndexRelationshipIT method put_relationship_if_absent_only_fail.

@Documented("Add an existing relationship to a unique index (already indexed).")
@Test
public void put_relationship_if_absent_only_fail() throws Exception {
    // Given
    final String index = indexes.newInstance(), key = "name", value = "Peter";
    GraphDatabaseService graphdb = graphdb();
    helper.createRelationshipIndex(index);
    try (Transaction tx = graphdb.beginTx()) {
        Node node1 = graphdb.createNode();
        Node node2 = graphdb.createNode();
        Relationship rel = node1.createRelationshipTo(node2, MyRelationshipTypes.KNOWS);
        graphdb.index().forRelationships(index).add(rel, key, value);
        tx.success();
    }
    Relationship rel;
    try (Transaction tx = graphdb.beginTx()) {
        Node node1 = graphdb.createNode();
        Node node2 = graphdb.createNode();
        rel = node1.createRelationshipTo(node2, MyRelationshipTypes.KNOWS);
        tx.success();
    }
    // When & Then
    gen.get().expectedStatus(409).payloadType(MediaType.APPLICATION_JSON_TYPE).payload("{\"key\": \"" + key + "\", \"value\": \"" + value + "\", \"uri\":\"" + functionalTestHelper.relationshipUri(rel.getId()) + "\"}").post(functionalTestHelper.relationshipIndexUri() + index + "?uniqueness=create_or_fail");
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test)

Aggregations

GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)322 Test (org.junit.Test)225 Transaction (org.neo4j.graphdb.Transaction)182 Node (org.neo4j.graphdb.Node)142 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)77 File (java.io.File)70 Relationship (org.neo4j.graphdb.Relationship)49 EmbeddedGraphDatabase (org.neo4j.kernel.EmbeddedGraphDatabase)32 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)17 Result (org.neo4j.graphdb.Result)14 Label (org.neo4j.graphdb.Label)13 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)12 HashMap (java.util.HashMap)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)10 PageCache (org.neo4j.io.pagecache.PageCache)10 DbRepresentation (org.neo4j.test.DbRepresentation)10 GraphDatabaseFactory (org.neo4j.graphdb.factory.GraphDatabaseFactory)9 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)8