Search in sources :

Example 6 with Statement

use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.

the class IndexIT method shouldListConstraintIndexesInTheBeansAPI.

@Test
public void shouldListConstraintIndexesInTheBeansAPI() throws Exception {
    // given
    Statement statement = statementInNewTransaction(SecurityContext.AUTH_DISABLED);
    statement.schemaWriteOperations().uniquePropertyConstraintCreate(SchemaDescriptorFactory.forLabel(statement.tokenWriteOperations().labelGetOrCreateForName("Label1"), statement.tokenWriteOperations().propertyKeyGetOrCreateForName("property1")));
    commit();
    // when
    try (Transaction tx = db.beginTx()) {
        Set<IndexDefinition> indexes;
        IndexDefinition index;
        indexes = Iterables.asSet(db.schema().getIndexes());
        // then
        assertEquals(1, indexes.size());
        index = indexes.iterator().next();
        assertEquals("Label1", index.getLabel().name());
        assertEquals(asSet("property1"), Iterables.asSet(index.getPropertyKeys()));
        assertTrue("index should be a constraint index", index.isConstraintIndex());
        // when
        try {
            index.drop();
            fail("expected exception");
        }// then
         catch (IllegalStateException e) {
            assertEquals("Constraint indexes cannot be dropped directly, " + "instead drop the owning uniqueness constraint.", e.getMessage());
        }
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Statement(org.neo4j.kernel.api.Statement) Test(org.junit.Test) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)

Example 7 with Statement

use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.

the class IndexPopulationJobTest method indexDescriptor.

private NewIndexDescriptor indexDescriptor(Label label, String propertyKey, boolean constraint) throws TransactionFailureException {
    try (KernelTransaction tx = kernel.newTransaction(KernelTransaction.Type.implicit, AnonymousContext.read());
        Statement statement = tx.acquireStatement()) {
        int labelId = statement.readOperations().labelGetForName(label.name());
        int propertyKeyId = statement.readOperations().propertyKeyGetForName(propertyKey);
        NewIndexDescriptor descriptor = constraint ? NewIndexDescriptorFactory.uniqueForLabel(labelId, propertyKeyId) : NewIndexDescriptorFactory.forLabel(labelId, propertyKeyId);
        tx.success();
        return descriptor;
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Statement(org.neo4j.kernel.api.Statement)

Example 8 with Statement

use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.

the class IndexStatisticsTest method awaitOnline.

private NewIndexDescriptor awaitOnline(NewIndexDescriptor index) throws KernelException {
    long start = System.currentTimeMillis();
    long end = start + 20_000;
    while (System.currentTimeMillis() < end) {
        try (Transaction tx = db.beginTx()) {
            Statement statement = bridge.get();
            switch(statement.readOperations().indexGetState(index)) {
                case ONLINE:
                    return index;
                case FAILED:
                    throw new IllegalStateException("Index failed instead of becoming ONLINE");
                default:
                    break;
            }
            tx.success();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            // ignored
            }
        }
    }
    throw new IllegalStateException("Index did not become ONLINE within reasonable time");
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Statement(org.neo4j.kernel.api.Statement)

Example 9 with Statement

use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.

the class IndexStatisticsTest method createSomePersons.

private void createSomePersons() throws KernelException {
    try (Transaction tx = db.beginTx()) {
        Statement statement = bridge.get();
        createNode(statement, "Person", "name", "Davide");
        createNode(statement, "Person", "name", "Stefan");
        createNode(statement, "Person", "name", "John");
        createNode(statement, "Person", "name", "John");
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Statement(org.neo4j.kernel.api.Statement)

Example 10 with Statement

use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.

the class IndexStatisticsTest method createIndex.

private NewIndexDescriptor createIndex(String labelName, String propertyKeyName) throws KernelException {
    try (Transaction tx = db.beginTx()) {
        Statement statement = bridge.get();
        int labelId = statement.tokenWriteOperations().labelGetOrCreateForName(labelName);
        int propertyKeyId = statement.tokenWriteOperations().propertyKeyGetOrCreateForName(propertyKeyName);
        LabelSchemaDescriptor descriptor = SchemaDescriptorFactory.forLabel(labelId, propertyKeyId);
        NewIndexDescriptor index = statement.schemaWriteOperations().indexCreate(descriptor);
        tx.success();
        return index;
    }
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Transaction(org.neo4j.graphdb.Transaction) Statement(org.neo4j.kernel.api.Statement) LabelSchemaDescriptor(org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor)

Aggregations

Statement (org.neo4j.kernel.api.Statement)158 Test (org.junit.Test)76 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)56 Transaction (org.neo4j.graphdb.Transaction)44 ReadOperations (org.neo4j.kernel.api.ReadOperations)40 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)30 EntityNotFoundException (org.neo4j.kernel.api.exceptions.EntityNotFoundException)23 NotFoundException (org.neo4j.graphdb.NotFoundException)21 Node (org.neo4j.graphdb.Node)20 ThreadToStatementContextBridge (org.neo4j.kernel.impl.core.ThreadToStatementContextBridge)19 PropertyNotFoundException (org.neo4j.kernel.api.exceptions.PropertyNotFoundException)13 KeyReadOperations (org.neo4j.kernel.impl.api.operations.KeyReadOperations)11 ArrayList (java.util.ArrayList)10 DependencyResolver (org.neo4j.graphdb.DependencyResolver)10 IndexNotFoundKernelException (org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException)10 Label (org.neo4j.graphdb.Label)9 KernelAPI (org.neo4j.kernel.api.KernelAPI)9 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)9 InvalidTransactionTypeKernelException (org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException)8 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)8