Search in sources :

Example 66 with Label

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

the class BatchInsertTest method shouldNotAllowCreationOfUniquenessConstraintAndIndexOnSameLabelAndProperty.

@Test
public void shouldNotAllowCreationOfUniquenessConstraintAndIndexOnSameLabelAndProperty() throws Exception {
    // Given
    Label label = label("Person1-" + denseNodeThreshold);
    String property = "name";
    BatchInserter inserter = globalInserter;
    // When
    inserter.createDeferredConstraint(label).assertPropertyIsUnique(property).create();
    try {
        inserter.createDeferredSchemaIndex(label).on(property).create();
        fail("Exception expected");
    } catch (ConstraintViolationException e) {
        // Then
        assertEquals("Index for given {label;property} already exists", e.getMessage());
    }
}
Also used : BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) Label(org.neo4j.graphdb.Label) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) Test(org.junit.Test)

Example 67 with Label

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

the class BatchInsertTest method uniquenessConstraintShouldBeCheckedOnBatchInserterShutdownAndFailIfViolated.

@Test
public void uniquenessConstraintShouldBeCheckedOnBatchInserterShutdownAndFailIfViolated() throws Exception {
    // Given
    Label label = label("Foo");
    String property = "Bar";
    String value = "Baz";
    BatchInserter inserter = newBatchInserter();
    // When
    inserter.createDeferredConstraint(label).assertPropertyIsUnique(property).create();
    inserter.createNode(Collections.<String, Object>singletonMap(property, value), label);
    inserter.createNode(Collections.<String, Object>singletonMap(property, value), label);
    // Then
    try {
        inserter.shutdown();
        fail("Node that violates uniqueness constraint was created by batch inserter");
    } catch (RuntimeException ex) {
        // good
        assertEquals(new IndexEntryConflictException(0, 1, value), ex.getCause());
    }
}
Also used : BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) Label(org.neo4j.graphdb.Label) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) Test(org.junit.Test)

Example 68 with Label

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

the class TestLuceneIndex method queryIndexWithSortByNumericAfterSamePropertyUpdate.

@Test
public void queryIndexWithSortByNumericAfterSamePropertyUpdate() {
    Index<Node> index = nodeIndex(stringMap());
    commitTx();
    String numericProperty = "PRODUCT_ID";
    Label updatableIndexesProperty = Label.label("updatableIndexes");
    try (Transaction transaction = graphDb.beginTx()) {
        for (int i = 0; i < 15; i++) {
            Node node = graphDb.createNode(updatableIndexesProperty);
            node.setProperty(numericProperty, i);
            index.add(node, numericProperty, new ValueContext(i).indexNumeric());
        }
        transaction.success();
    }
    doubleNumericPropertyValueForAllNodesWithLabel(index, numericProperty, updatableIndexesProperty);
    queryAndSortNodesByNumericProperty(index, numericProperty, i -> i * 2);
}
Also used : ValueContext(org.neo4j.index.lucene.ValueContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) Test(org.junit.Test)

Example 69 with Label

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

the class TestLuceneIndex method queryIndexWithSortByStringAfterSamePropertyUpdate.

@Test
public void queryIndexWithSortByStringAfterSamePropertyUpdate() {
    Index<Node> index = nodeIndex(stringMap());
    commitTx();
    String nameProperty = "NODE_NAME";
    Label heroes = Label.label("heroes");
    String[] names = new String[] { "Fry", "Leela", "Bender", "Amy", "Hubert", "Calculon" };
    try (Transaction transaction = graphDb.beginTx()) {
        for (String name : names) {
            Node node = graphDb.createNode(heroes);
            node.setProperty(nameProperty, name);
            index.add(node, nameProperty, name);
        }
        transaction.success();
    }
    try (Transaction transaction = graphDb.beginTx()) {
        ResourceIterator<Node> nodes = graphDb.findNodes(heroes);
        nodes.stream().forEach(node -> {
            node.setProperty(nameProperty, "junior " + node.getProperty(nameProperty));
            index.remove(node, nameProperty);
            index.add(node, nameProperty, node.getProperty(nameProperty));
        });
        transaction.success();
    }
    String[] sortedNames = new String[] { "junior Leela", "junior Hubert", "junior Fry", "junior Calculon", "junior Bender", "junior Amy" };
    queryAndSortNodesByStringProperty(index, nameProperty, sortedNames);
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) Test(org.junit.Test)

Example 70 with Label

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

the class TestLuceneIndex method queryIndexWithSortByNumericAfterOtherPropertyUpdate.

@Test
public void queryIndexWithSortByNumericAfterOtherPropertyUpdate() {
    Index<Node> index = nodeIndex();
    commitTx();
    String yearProperty = "year";
    String priceProperty = "price";
    Label nodeLabel = Label.label("priceyNodes");
    try (Transaction transaction = graphDb.beginTx()) {
        for (int i = 0; i < 15; i++) {
            Node node = graphDb.createNode(nodeLabel);
            node.setProperty(yearProperty, i);
            node.setProperty(priceProperty, i);
            index.add(node, yearProperty, new ValueContext(i).indexNumeric());
            index.add(node, priceProperty, new ValueContext(i).indexNumeric());
        }
        transaction.success();
    }
    doubleNumericPropertyValueForAllNodesWithLabel(index, priceProperty, nodeLabel);
    queryAndSortNodesByNumericProperty(index, yearProperty);
    queryAndSortNodesByNumericProperty(index, priceProperty, (value) -> value * 2);
}
Also used : ValueContext(org.neo4j.index.lucene.ValueContext) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) Test(org.junit.Test)

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