Search in sources :

Example 31 with Label

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

the class ManyMergesStressTest method shouldWorkFine.

@Test
public void shouldWorkFine() throws Throwable {
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    GraphDatabaseQueryService graph = new GraphDatabaseCypherService(db);
    Label person = Label.label("Person");
    try (Transaction tx = db.beginTx()) {
        // THIS USED TO CAUSE OUT OF FILE HANDLES
        // (maybe look at:  http://stackoverflow.com/questions/6210348/too-many-open-files-error-on-lucene)
        db.schema().indexFor(person).on("id").create();
        // THIS SHOULD ALSO WORK
        db.schema().constraintFor(person).assertPropertyIsUnique("id").create();
        tx.success();
    }
    try (Transaction tx = db.beginTx()) {
        db.schema().indexFor(person).on("name").create();
        tx.success();
    }
    try (Transaction tx = db.beginTx()) {
        db.schema().awaitIndexesOnline(1, TimeUnit.MINUTES);
        tx.success();
    }
    for (int count = 0; count < TRIES; count++) {
        Pair<String, String> stringPair = getRandomName();
        String ident = stringPair.first();
        String name = stringPair.other();
        String id = Long.toString(Math.abs(random.nextLong()));
        String query = format("MERGE (%s:Person {id: %s}) ON CREATE SET %s.name = \"%s\";", ident, id, ident, name);
        try (InternalTransaction tx = graph.beginTransaction(KernelTransaction.Type.implicit, SecurityContext.AUTH_DISABLED)) {
            Result result = db.execute(query);
            result.close();
            tx.success();
        }
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Transaction(org.neo4j.graphdb.Transaction) GraphDatabaseQueryService(org.neo4j.kernel.GraphDatabaseQueryService) Label(org.neo4j.graphdb.Label) GraphDatabaseCypherService(org.neo4j.cypher.javacompat.internal.GraphDatabaseCypherService) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Result(org.neo4j.graphdb.Result) Test(org.junit.Test)

Example 32 with Label

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

the class ExportTest method testExportConstraintsViaCypherResult.

@Test
public void testExportConstraintsViaCypherResult() throws Exception {
    final Label label = Label.label("Foo");
    gdb.schema().constraintFor(label).assertPropertyIsUnique("bar").create();
    gdb.schema().constraintFor(label).assertPropertyIsUnique("bar2").create();
    commitAndStartNewTransactionAfterSchemaChanges();
    Node n = gdb.createNode(label);
    final ExecutionResult result = result("node", n);
    final SubGraph graph = CypherResultSubGraph.from(result, gdb, true);
    assertEquals("create constraint on (n:`Foo`) assert n.`bar2` is unique;" + lineSeparator() + "create constraint on (n:`Foo`) assert n.`bar` is unique;" + lineSeparator() + "create (_0:`Foo`)" + lineSeparator() + ";" + lineSeparator(), doExportGraph(graph));
}
Also used : Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) ExecutionResult(org.neo4j.cypher.internal.javacompat.ExecutionResult) Test(org.junit.Test)

Example 33 with Label

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

the class ExportTest method testExportIndexesViaCypherResult.

@Test
public void testExportIndexesViaCypherResult() throws Exception {
    final Label label = Label.label("Foo");
    gdb.schema().indexFor(label).on("bar").create();
    gdb.schema().indexFor(label).on("bar2").create();
    commitAndStartNewTransactionAfterSchemaChanges();
    Node n = gdb.createNode(label);
    final ExecutionResult result = result("node", n);
    final SubGraph graph = CypherResultSubGraph.from(result, gdb, true);
    assertEquals("create index on :`Foo`(`bar2`);" + lineSeparator() + "create index on :`Foo`(`bar`);" + lineSeparator() + "create (_0:`Foo`)" + lineSeparator() + ";" + lineSeparator(), doExportGraph(graph));
}
Also used : Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) ExecutionResult(org.neo4j.cypher.internal.javacompat.ExecutionResult) Test(org.junit.Test)

Example 34 with Label

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

the class ImportToolTest method shouldSkipDuplicateNodesIfToldTo.

@Test
public void shouldSkipDuplicateNodesIfToldTo() throws Exception {
    // GIVEN
    List<String> nodeIds = asList("a", "b", "c", "d", "e", "f", "a", "g");
    Configuration config = Configuration.COMMAS;
    File nodeHeaderFile = nodeHeader(config);
    File nodeData1 = nodeData(false, config, nodeIds, lines(0, 4));
    File nodeData2 = nodeData(false, config, nodeIds, lines(4, nodeIds.size()));
    // WHEN
    importTool("--into", dbRule.getStoreDirAbsolutePath(), "--skip-duplicate-nodes", "--nodes", nodeHeaderFile.getAbsolutePath() + MULTI_FILE_DELIMITER + nodeData1.getAbsolutePath() + MULTI_FILE_DELIMITER + nodeData2.getAbsolutePath());
    // THEN
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    try (Transaction tx = db.beginTx()) {
        // there should not be duplicates of any node
        Iterator<Node> nodes = db.getAllNodes().iterator();
        Iterator<String> expectedIds = FilteringIterator.noDuplicates(nodeIds.iterator());
        while (expectedIds.hasNext()) {
            assertTrue(nodes.hasNext());
            assertEquals(expectedIds.next(), nodes.next().getProperty("id"));
        }
        assertFalse(nodes.hasNext());
        // also all nodes in the label index should exist
        for (int i = 0; i < MAX_LABEL_ID; i++) {
            Label label = Label.label(labelName(i));
            try (ResourceIterator<Node> nodesByLabel = db.findNodes(label)) {
                while (nodesByLabel.hasNext()) {
                    assertTrue(nodesByLabel.next().hasLabel(label));
                }
            }
        }
        tx.success();
    } finally {
        db.shutdown();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Configuration(org.neo4j.unsafe.impl.batchimport.input.csv.Configuration) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) DynamicLabel(org.neo4j.graphdb.DynamicLabel) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 35 with Label

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

the class GraphDbStructureGuide method showRelCounts.

private void showRelCounts(ReadOperations read, DbStructureVisitor visitor) {
    // all wildcards
    noSide(read, visitor, WILDCARD_REL_TYPE, ANY_RELATIONSHIP_TYPE);
    // one label only
    for (Label label : db.getAllLabels()) {
        int labelId = read.labelGetForName(label.name());
        leftSide(read, visitor, label, labelId, WILDCARD_REL_TYPE, ANY_RELATIONSHIP_TYPE);
        rightSide(read, visitor, label, labelId, WILDCARD_REL_TYPE, ANY_RELATIONSHIP_TYPE);
    }
    // fixed rel type
    for (RelationshipType relType : db.getAllRelationshipTypes()) {
        int relTypeId = read.relationshipTypeGetForName(relType.name());
        noSide(read, visitor, relType, relTypeId);
        for (Label label : db.getAllLabels()) {
            int labelId = read.labelGetForName(label.name());
            // wildcard on right
            leftSide(read, visitor, label, labelId, relType, relTypeId);
            // wildcard on left
            rightSide(read, visitor, label, labelId, relType, relTypeId);
        }
    }
}
Also used : Label(org.neo4j.graphdb.Label) RelationshipType(org.neo4j.graphdb.RelationshipType) PropertyConstraint(org.neo4j.kernel.api.constraints.PropertyConstraint) RelationshipPropertyExistenceConstraint(org.neo4j.kernel.api.constraints.RelationshipPropertyExistenceConstraint) NodePropertyExistenceConstraint(org.neo4j.kernel.api.constraints.NodePropertyExistenceConstraint) UniquenessConstraint(org.neo4j.kernel.api.constraints.UniquenessConstraint)

Aggregations

Label (org.neo4j.graphdb.Label)82 Test (org.junit.Test)55 Node (org.neo4j.graphdb.Node)48 Transaction (org.neo4j.graphdb.Transaction)44 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)12 RelationshipType (org.neo4j.graphdb.RelationshipType)8 DependencyResolver (org.neo4j.graphdb.DependencyResolver)6 DynamicLabel (org.neo4j.graphdb.DynamicLabel)6 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)6 Statement (org.neo4j.kernel.api.Statement)6 ArrayList (java.util.ArrayList)5 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)5 Relationship (org.neo4j.graphdb.Relationship)5 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)5 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)5 File (java.io.File)4 HashSet (java.util.HashSet)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)4