Search in sources :

Example 61 with IndexDefinition

use of org.neo4j.graphdb.schema.IndexDefinition in project neo4j by neo4j.

the class SchemaAcceptanceTest method awaitingAllIndexesComingOnlineWorks.

@Test
public void awaitingAllIndexesComingOnlineWorks() {
    // GIVEN
    // WHEN
    IndexDefinition index = createIndex(db, label, propertyKey);
    createIndex(db, label, "other_property");
    // PASS
    waitForIndex(db, index);
    try (Transaction tx = db.beginTx()) {
        db.schema().awaitIndexesOnline(1L, TimeUnit.MINUTES);
        // THEN
        assertEquals(Schema.IndexState.ONLINE, db.schema().getIndexState(index));
    }
}
Also used : IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Test(org.junit.Test)

Example 62 with IndexDefinition

use of org.neo4j.graphdb.schema.IndexDefinition in project neo4j by neo4j.

the class SchemaAcceptanceTest method droppingAnUnexistingIndexShouldGiveHelpfulExceptionInSameTransaction.

@Test
public void droppingAnUnexistingIndexShouldGiveHelpfulExceptionInSameTransaction() throws Exception {
    // GIVEN
    IndexDefinition index = createIndex(db, label, propertyKey);
    // WHEN
    try (Transaction tx = db.beginTx()) {
        index.drop();
        try {
            index.drop();
            fail("Should not be able to drop index twice");
        } catch (ConstraintViolationException e) {
            assertThat(e.getMessage(), containsString("No index was found for :MY_LABEL(my_property_key)."));
        }
        tx.success();
    }
    // THEN
    assertThat("Index should have been deleted", getIndexes(db, label), not(contains(index)));
}
Also used : IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Test(org.junit.Test)

Example 63 with IndexDefinition

use of org.neo4j.graphdb.schema.IndexDefinition in project neo4j by neo4j.

the class IndexingAcceptanceTest method shouldConsiderNodesChangedInSameTxInIndexPrefixSearch.

@Test
public void shouldConsiderNodesChangedInSameTxInIndexPrefixSearch() throws SchemaRuleNotFoundException, IndexNotFoundKernelException, IndexNotApplicableKernelException {
    // GIVEN
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    IndexDefinition index = Neo4jMatchers.createIndex(db, LABEL1, "name");
    createNodes(db, LABEL1, "name", "Mattias");
    PrimitiveLongSet toChangeToMatch = createNodes(db, LABEL1, "name", "Mats");
    PrimitiveLongSet toChangeToNotMatch = createNodes(db, LABEL1, "name", "Karlsson");
    PrimitiveLongSet expected = createNodes(db, LABEL1, "name", "Karl");
    String prefix = "Karl";
    // WHEN
    PrimitiveLongSet found = Primitive.longSet();
    try (Transaction tx = db.beginTx()) {
        PrimitiveLongIterator toMatching = toChangeToMatch.iterator();
        while (toMatching.hasNext()) {
            long id = toMatching.next();
            db.getNodeById(id).setProperty("name", prefix + "X" + id);
            expected.add(id);
        }
        PrimitiveLongIterator toNotMatching = toChangeToNotMatch.iterator();
        while (toNotMatching.hasNext()) {
            long id = toNotMatching.next();
            db.getNodeById(id).setProperty("name", "X" + id);
            expected.remove(id);
        }
        Statement statement = getStatement((GraphDatabaseAPI) db);
        ReadOperations readOperations = statement.readOperations();
        NewIndexDescriptor descriptor = indexDescriptor(readOperations, index);
        int propertyKeyId = descriptor.schema().getPropertyId();
        found.addAll(readOperations.indexQuery(descriptor, stringPrefix(propertyKeyId, prefix)));
    }
    // THEN
    assertThat(found, equalTo(expected));
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) ReadOperations(org.neo4j.kernel.api.ReadOperations) PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Statement(org.neo4j.kernel.api.Statement) Test(org.junit.Test)

Example 64 with IndexDefinition

use of org.neo4j.graphdb.schema.IndexDefinition in project neo4j by neo4j.

the class IndexingAcceptanceTest method shouldIncludeNodesCreatedInSameTxInIndexSeekByPrefix.

@Test
public void shouldIncludeNodesCreatedInSameTxInIndexSeekByPrefix() throws SchemaRuleNotFoundException, IndexNotFoundKernelException, IndexNotApplicableKernelException {
    // GIVEN
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    IndexDefinition index = Neo4jMatchers.createIndex(db, LABEL1, "name");
    createNodes(db, LABEL1, "name", "Mattias", "Mats");
    PrimitiveLongSet expected = createNodes(db, LABEL1, "name", "Carl", "Carlsson");
    // WHEN
    PrimitiveLongSet found = Primitive.longSet();
    try (Transaction tx = db.beginTx()) {
        expected.add(createNode(db, map("name", "Carlchen"), LABEL1).getId());
        createNode(db, map("name", "Karla"), LABEL1);
        Statement statement = getStatement((GraphDatabaseAPI) db);
        ReadOperations readOperations = statement.readOperations();
        NewIndexDescriptor descriptor = indexDescriptor(readOperations, index);
        int propertyKeyId = descriptor.schema().getPropertyId();
        found.addAll(readOperations.indexQuery(descriptor, stringPrefix(propertyKeyId, "Carl")));
    }
    // THEN
    assertThat(found, equalTo(expected));
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Statement(org.neo4j.kernel.api.Statement) Test(org.junit.Test)

Example 65 with IndexDefinition

use of org.neo4j.graphdb.schema.IndexDefinition in project neo4j by neo4j.

the class SchemaWithPECAcceptanceTest method createUniquenessConstraint.

private ConstraintDefinition createUniquenessConstraint(Label label, String propertyKey) {
    SchemaHelper.createUniquenessConstraint(db, label, propertyKey);
    SchemaHelper.awaitIndexes(db);
    InternalSchemaActions actions = mock(InternalSchemaActions.class);
    IndexDefinition index = new IndexDefinitionImpl(actions, label, new String[] { propertyKey }, true);
    return new UniquenessConstraintDefinition(actions, index);
}
Also used : InternalSchemaActions(org.neo4j.kernel.impl.coreapi.schema.InternalSchemaActions) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) UniquenessConstraintDefinition(org.neo4j.kernel.impl.coreapi.schema.UniquenessConstraintDefinition) IndexDefinitionImpl(org.neo4j.kernel.impl.coreapi.schema.IndexDefinitionImpl)

Aggregations

IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)84 Test (org.junit.Test)56 Transaction (org.neo4j.graphdb.Transaction)32 StringContains.containsString (org.hamcrest.core.StringContains.containsString)11 Node (org.neo4j.graphdb.Node)9 Statement (org.neo4j.kernel.api.Statement)7 ArrayList (java.util.ArrayList)4 PrimitiveLongSet (org.neo4j.collection.primitive.PrimitiveLongSet)4 Label (org.neo4j.graphdb.Label)4 ReadOperations (org.neo4j.kernel.api.ReadOperations)4 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)4 HighlyAvailableGraphDatabase (org.neo4j.kernel.ha.HighlyAvailableGraphDatabase)4 ManagedCluster (org.neo4j.kernel.impl.ha.ClusterManager.ManagedCluster)4 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)3 Iterator (java.util.Iterator)2 PrimitiveLongIterator (org.neo4j.collection.primitive.PrimitiveLongIterator)2 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)2 TransactionData (org.neo4j.graphdb.event.TransactionData)2 TransactionEventHandler (org.neo4j.graphdb.event.TransactionEventHandler)2 ConstraintDefinition (org.neo4j.graphdb.schema.ConstraintDefinition)2