Search in sources :

Example 26 with SchemaReadCore

use of org.neo4j.internal.kernel.api.SchemaReadCore in project neo4j by neo4j.

the class BuiltInProceduresIT method listAllIndexesMustNotBlockOnConstraintCreatingTransaction.

@Test
@Timeout(value = 6, unit = MINUTES)
void listAllIndexesMustNotBlockOnConstraintCreatingTransaction() throws Throwable {
    // Given
    KernelTransaction transaction = newTransaction(AUTH_DISABLED);
    int labelId1 = transaction.tokenWrite().labelGetOrCreateForName("Person");
    int labelId2 = transaction.tokenWrite().labelGetOrCreateForName("Age");
    int propertyKeyId1 = transaction.tokenWrite().propertyKeyGetOrCreateForName("foo");
    int propertyKeyId2 = transaction.tokenWrite().propertyKeyGetOrCreateForName("bar");
    int propertyKeyId3 = transaction.tokenWrite().propertyKeyGetOrCreateForName("baz");
    LabelSchemaDescriptor personFooDescriptor = forLabel(labelId1, propertyKeyId1);
    LabelSchemaDescriptor ageFooDescriptor = forLabel(labelId2, propertyKeyId1);
    LabelSchemaDescriptor personFooBarDescriptor = forLabel(labelId1, propertyKeyId1, propertyKeyId2);
    LabelSchemaDescriptor personBazDescriptor = forLabel(labelId1, propertyKeyId3);
    transaction.schemaWrite().indexCreate(personFooDescriptor, "person foo index");
    transaction.schemaWrite().uniquePropertyConstraintCreate(IndexPrototype.uniqueForSchema(ageFooDescriptor).withName("age foo constraint"));
    transaction.schemaWrite().indexCreate(personFooBarDescriptor, "person foo bar index");
    commit();
    // let indexes come online
    try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
        tx.schema().awaitIndexesOnline(2, MINUTES);
        tx.commit();
    }
    CountDownLatch constraintLatch = new CountDownLatch(1);
    CountDownLatch commitLatch = new CountDownLatch(1);
    AtomicLong personBazIndexId = new AtomicLong();
    FutureTask<Void> createConstraintTask = new FutureTask<>(() -> {
        SchemaWrite schemaWrite = schemaWriteInNewTransaction();
        try (Resource ignore = captureTransaction()) {
            ConstraintDescriptor personBazConstraint = schemaWrite.uniquePropertyConstraintCreate(IndexPrototype.uniqueForSchema(personBazDescriptor).withName("person baz constraint"));
            personBazIndexId.set(personBazConstraint.asIndexBackedConstraint().ownedIndexId());
            // We now hold a schema lock on the "MyLabel" label. Let the procedure calling transaction have a go.
            constraintLatch.countDown();
            commitLatch.await();
        }
        rollback();
        return null;
    });
    Thread constraintCreator = new Thread(createConstraintTask);
    constraintCreator.start();
    // When
    constraintLatch.await();
    transaction = newTransaction();
    final SchemaReadCore schemaRead = transaction.schemaRead().snapshot();
    IndexDescriptor personFooIndex = single(schemaRead.index(personFooDescriptor));
    IndexDescriptor ageFooIndex = single(schemaRead.index(ageFooDescriptor));
    IndexDescriptor personFooBarIndex = single(schemaRead.index(personFooBarDescriptor));
    IndexDescriptor personBazIndex = single(schemaRead.index(personBazDescriptor));
    commit();
    RawIterator<AnyValue[], ProcedureException> stream = procs().procedureCallRead(procs().procedureGet(procedureName("db", "indexes")).id(), new AnyValue[0], ProcedureCallContext.EMPTY);
    Set<Object[]> result = new HashSet<>();
    while (stream.hasNext()) {
        result.add(stream.next());
    }
    // Then
    try {
        assertThat(result).contains(dbIndexesResult(ageFooIndex.getId(), ageFooIndex.getName(), "ONLINE", 100D, "UNIQUE", "BTREE", "NODE", singletonList("Age"), singletonList("foo"), ageFooIndex.getIndexProvider().name()), dbIndexesResult(personFooIndex.getId(), personFooIndex.getName(), "ONLINE", 100D, "NONUNIQUE", "BTREE", "NODE", singletonList("Person"), singletonList("foo"), personFooIndex.getIndexProvider().name()), dbIndexesResult(personFooBarIndex.getId(), personFooBarIndex.getName(), "ONLINE", 100D, "NONUNIQUE", "BTREE", "NODE", singletonList("Person"), Arrays.asList("foo", "bar"), personFooBarIndex.getIndexProvider().name()), dbIndexesResult(personBazIndex.getId(), personBazIndex.getName(), /*???*/
        "POPULATING", 100D, "UNIQUE", "BTREE", "NODE", singletonList("Person"), singletonList("baz"), personBazIndex.getIndexProvider().name()));
        commit();
    } finally {
        commitLatch.countDown();
    }
    createConstraintTask.get();
    constraintCreator.join();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) Resource(org.neo4j.graphdb.Resource) CountDownLatch(java.util.concurrent.CountDownLatch) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) AtomicLong(java.util.concurrent.atomic.AtomicLong) FutureTask(java.util.concurrent.FutureTask) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 27 with SchemaReadCore

use of org.neo4j.internal.kernel.api.SchemaReadCore in project neo4j by neo4j.

the class SchemaStatementProcedureTest method schemaStatementsMustIncludeOnlineIndexes.

@Test
void schemaStatementsMustIncludeOnlineIndexes() throws IndexNotFoundKernelException, ProcedureException {
    IndexDescriptor index = someIndex();
    InternalIndexState indexState = InternalIndexState.ONLINE;
    SchemaReadCore schemaReadCore = getSchemaReadCore(index, indexState);
    TokenRead tokenRead = mock(TokenRead.class);
    Collection<BuiltInProcedures.SchemaStatementResult> result = createSchemaStatementResults(schemaReadCore, tokenRead);
    assertEquals(1, result.size());
}
Also used : InternalIndexState(org.neo4j.internal.kernel.api.InternalIndexState) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Test(org.junit.jupiter.api.Test)

Example 28 with SchemaReadCore

use of org.neo4j.internal.kernel.api.SchemaReadCore in project neo4j by neo4j.

the class SchemaStatementProcedureTest method schemaStatementsShouldHandleConstraintWithBackticks.

@Test
void schemaStatementsShouldHandleConstraintWithBackticks() throws IndexNotFoundKernelException, ProcedureException, LabelNotFoundKernelException, PropertyKeyIdNotFoundKernelException {
    ConstraintDescriptor constraint = ConstraintDescriptorFactory.existsForSchema(forLabel(1, 1)).withName(NAME_WITH_BACKTICKS);
    SchemaReadCore schemaReadCore = getSchemaReadCore(constraint);
    TokenRead tokenRead = mock(TokenRead.class);
    when(tokenRead.nodeLabelName(1)).thenReturn(LABEL_WITH_BACKTICKS);
    when(tokenRead.propertyKeyName(1)).thenReturn(PROPERTY_KEY_WITH_BACKTICKS);
    Collection<BuiltInProcedures.SchemaStatementResult> result = createSchemaStatementResults(schemaReadCore, tokenRead);
    Iterator<BuiltInProcedures.SchemaStatementResult> iter = result.iterator();
    assertTrue(iter.hasNext());
    BuiltInProcedures.SchemaStatementResult next = iter.next();
    assertEquals(NAME_WITH_BACKTICKS, next.name);
    assertEquals(format("CREATE CONSTRAINT %s ON (a:%s) ASSERT (a.%s) IS NOT NULL", ESCAPED_NAME_WITH_BACKTICKS, ESCAPED_LABEL_WITH_BACKTICKS, ESCAPED_PROPERTY_KEY_WITH_BACKTICKS), next.createStatement);
    assertEquals(format("DROP CONSTRAINT %s", ESCAPED_NAME_WITH_BACKTICKS), next.dropStatement);
    assertEquals(NAME_WITH_BACKTICKS, next.name);
    assertFalse(iter.hasNext());
}
Also used : ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Test(org.junit.jupiter.api.Test)

Example 29 with SchemaReadCore

use of org.neo4j.internal.kernel.api.SchemaReadCore in project neo4j by neo4j.

the class SchemaStatementProcedureTest method schemaStatementsMustNotIncludePopulatingIndexes.

@Test
void schemaStatementsMustNotIncludePopulatingIndexes() throws ProcedureException, IndexNotFoundKernelException {
    IndexDescriptor index = someIndex();
    InternalIndexState indexState = InternalIndexState.POPULATING;
    SchemaReadCore schemaReadCore = getSchemaReadCore(index, indexState);
    TokenRead tokenRead = mock(TokenRead.class);
    Collection<BuiltInProcedures.SchemaStatementResult> result = createSchemaStatementResults(schemaReadCore, tokenRead);
    assertEquals(0, result.size());
}
Also used : InternalIndexState(org.neo4j.internal.kernel.api.InternalIndexState) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Test(org.junit.jupiter.api.Test)

Example 30 with SchemaReadCore

use of org.neo4j.internal.kernel.api.SchemaReadCore in project neo4j by neo4j.

the class SchemaStatementProcedureTest method getSchemaReadCore.

private static SchemaReadCore getSchemaReadCore(ConstraintDescriptor constraint, IndexDescriptor index, InternalIndexState indexState) throws IndexNotFoundKernelException {
    SchemaReadCore schemaReadCore = mock(SchemaReadCore.class);
    when(schemaReadCore.indexesGetAll()).thenReturn(iterator());
    when(schemaReadCore.constraintsGetAll()).thenReturn(iterator());
    if (index != null) {
        when(schemaReadCore.indexesGetAll()).thenReturn(singleton(index).iterator());
        when(schemaReadCore.indexGetForName(index.getName())).thenReturn(index);
        when(schemaReadCore.indexGetState(index)).thenReturn(indexState);
    }
    if (constraint != null) {
        when(schemaReadCore.constraintsGetAll()).thenReturn(singleton(constraint).iterator());
    }
    return schemaReadCore;
}
Also used : SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore)

Aggregations

SchemaReadCore (org.neo4j.internal.kernel.api.SchemaReadCore)32 Test (org.junit.jupiter.api.Test)27 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)21 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)17 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)17 TokenRead (org.neo4j.internal.kernel.api.TokenRead)13 InternalIndexState (org.neo4j.internal.kernel.api.InternalIndexState)10 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)9 ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)5 SystemProcedure (org.neo4j.kernel.api.procedure.SystemProcedure)4 IndexingService (org.neo4j.kernel.impl.api.index.IndexingService)4 Description (org.neo4j.procedure.Description)4 Procedure (org.neo4j.procedure.Procedure)4 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 DependencyResolver (org.neo4j.common.DependencyResolver)3 Config (org.neo4j.configuration.Config)3