Search in sources :

Example 16 with IndexDescriptor

use of org.neo4j.internal.schema.IndexDescriptor in project neo4j by neo4j.

the class SchemaStorageIT method shouldWriteAndReadIndexConfig.

@Test
void shouldWriteAndReadIndexConfig() throws KernelException {
    // given
    IndexConfig expected = IndexConfig.with(MapUtil.genericMap("value.string", Values.stringValue("value"), "value.int", Values.intValue(1), "value.doubleArray", Values.doubleArray(new double[] { 0.4, 0.6, 1.0 }), "value.boolean", Values.booleanValue(true)));
    var cursorContext = NULL;
    SchemaDescriptor schema = forLabel(labelId(LABEL1), propId(PROP1));
    long id = schemaStore.nextId(cursorContext);
    IndexDescriptor storeIndexDescriptor = forSchema(schema).withName("index_" + id).materialise(id).withIndexConfig(expected);
    storage.writeSchemaRule(storeIndexDescriptor, cursorContext, INSTANCE);
    // when
    IndexDescriptor schemaRule = (IndexDescriptor) storage.loadSingleSchemaRule(id, NULL);
    // Clean up after ourselves.
    storage.deleteSchemaRule(schemaRule, NULL);
    // then
    IndexConfig actual = schemaRule.getIndexConfig();
    assertEquals(expected, actual, "Read index config not same as written, expected " + expected + ", actual " + actual);
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) IndexConfig(org.neo4j.internal.schema.IndexConfig) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Test(org.junit.jupiter.api.Test)

Example 17 with IndexDescriptor

use of org.neo4j.internal.schema.IndexDescriptor in project neo4j by neo4j.

the class SchemaStorageIT method shouldReturnIndexRuleForLabelAndVeryManyPropertiesComposite.

@Test
void shouldReturnIndexRuleForLabelAndVeryManyPropertiesComposite() {
    String[] props = "abcdefghijklmnopqrstuvwxyzABCDEFGHJILKMNOPQRSTUVWXYZ".split("\\B");
    createSchema(tx -> {
        IndexCreator indexCreator = tx.schema().indexFor(Label.label(LABEL1));
        for (String prop : props) {
            indexCreator = indexCreator.on(prop);
        }
        indexCreator.create();
    });
    IndexDescriptor rule = single(storage.indexGetForSchema(TestIndexDescriptorFactory.forLabel(labelId(LABEL1), Arrays.stream(props).mapToInt(this::propId).toArray()), NULL));
    assertNotNull(rule);
    assertTrue(SchemaDescriptorPredicates.hasLabel(rule, labelId(LABEL1)));
    for (String prop : props) {
        assertTrue(SchemaDescriptorPredicates.hasProperty(rule, propId(prop)));
    }
    assertFalse(rule.isUnique());
}
Also used : IndexCreator(org.neo4j.graphdb.schema.IndexCreator) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Test(org.junit.jupiter.api.Test)

Example 18 with IndexDescriptor

use of org.neo4j.internal.schema.IndexDescriptor in project neo4j by neo4j.

the class SchemaStorageIT method shouldListIndexRulesForLabelPropertyAndKind.

@Test
void shouldListIndexRulesForLabelPropertyAndKind() {
    // Given
    createSchema(uniquenessConstraint(LABEL1, PROP1), index(LABEL1, PROP2));
    // When
    IndexDescriptor rule = single(storage.indexGetForSchema(uniqueIndexDescriptor(LABEL1, PROP1), NULL));
    // Then
    assertNotNull(rule);
    assertRule(rule, LABEL1, PROP1, true);
}
Also used : IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Test(org.junit.jupiter.api.Test)

Example 19 with IndexDescriptor

use of org.neo4j.internal.schema.IndexDescriptor in project neo4j by neo4j.

the class Operations method indexDrop.

@Override
public void indexDrop(SchemaDescriptor schema) throws SchemaKernelException {
    exclusiveSchemaLock(schema);
    Iterator<IndexDescriptor> iterator = Iterators.filter(index -> index.getIndexType() != IndexType.FULLTEXT, allStoreHolder.index(schema));
    if (!iterator.hasNext()) {
        String description = schema.userDescription(token);
        throw new DropIndexFailureException("Unable to drop index on " + description + ". There is no such index.");
    }
    do {
        IndexDescriptor existingIndex = iterator.next();
        indexDrop(existingIndex);
    } while (iterator.hasNext());
}
Also used : IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) DropIndexFailureException(org.neo4j.kernel.api.exceptions.schema.DropIndexFailureException)

Example 20 with IndexDescriptor

use of org.neo4j.internal.schema.IndexDescriptor in project neo4j by neo4j.

the class Operations method assertNoBlockingSchemaRulesExists.

private void assertNoBlockingSchemaRulesExists(ConstraintDescriptor constraint) throws EquivalentSchemaRuleAlreadyExistsException, IndexWithNameAlreadyExistsException, ConstraintWithNameAlreadyExistsException, AlreadyConstrainedException, AlreadyIndexedException {
    final String name = constraint.getName();
    if (name == null) {
        throw new IllegalStateException("Expected constraint to always have a name by this point");
    }
    // Equivalent constraint
    final List<ConstraintDescriptor> constraintsWithSameSchema = Iterators.asList(allStoreHolder.constraintsGetForSchema(constraint.schema()));
    for (ConstraintDescriptor constraintWithSameSchema : constraintsWithSameSchema) {
        if (constraint.equals(constraintWithSameSchema) && constraint.getName().equals(constraintWithSameSchema.getName())) {
            throw new EquivalentSchemaRuleAlreadyExistsException(constraintWithSameSchema, CONSTRAINT_CREATION, token);
        }
    }
    // Name conflict with other schema rule
    assertSchemaRuleWithNameDoesNotExist(name);
    // Already constrained
    for (ConstraintDescriptor constraintWithSameSchema : constraintsWithSameSchema) {
        final boolean creatingExistenceConstraint = constraint.type() == ConstraintType.EXISTS;
        final boolean existingIsExistenceConstraint = constraintWithSameSchema.type() == ConstraintType.EXISTS;
        if (creatingExistenceConstraint == existingIsExistenceConstraint) {
            throw new AlreadyConstrainedException(constraintWithSameSchema, CONSTRAINT_CREATION, token);
        }
    }
    // Already indexed
    if (constraint.type() != ConstraintType.EXISTS) {
        Iterator<IndexDescriptor> existingIndexes = allStoreHolder.index(constraint.schema());
        if (existingIndexes.hasNext()) {
            IndexDescriptor existingIndex = existingIndexes.next();
            throw new AlreadyIndexedException(existingIndex.schema(), CONSTRAINT_CREATION, token);
        }
    }
    // is being populated we will end up with two indexes on the same schema.
    if (constraint.isIndexBackedConstraint() && ktx.hasTxStateWithChanges()) {
        for (ConstraintDescriptor droppedConstraint : ktx.txState().constraintsChanges().getRemoved()) {
            // If dropped and new constraint have similar backing index we cannot allow this constraint creation
            if (droppedConstraint.isIndexBackedConstraint() && constraint.schema().equals(droppedConstraint.schema())) {
                throw new UnsupportedOperationException(format("Trying to create constraint '%s' in same transaction as dropping '%s'. " + "This is not supported because they are both backed by similar indexes. " + "Please drop constraint in a separate transaction before creating the new one.", constraint.getName(), droppedConstraint.getName()));
            }
        }
    }
}
Also used : AlreadyIndexedException(org.neo4j.kernel.api.exceptions.schema.AlreadyIndexedException) EquivalentSchemaRuleAlreadyExistsException(org.neo4j.kernel.api.exceptions.schema.EquivalentSchemaRuleAlreadyExistsException) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Aggregations

IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)404 Test (org.junit.jupiter.api.Test)231 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)81 Value (org.neo4j.values.storable.Value)43 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)33 ArrayList (java.util.ArrayList)31 Transaction (org.neo4j.graphdb.Transaction)31 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)31 IndexPrototype (org.neo4j.internal.schema.IndexPrototype)30 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)30 TokenRead (org.neo4j.internal.kernel.api.TokenRead)29 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)26 IndexUpdater (org.neo4j.kernel.api.index.IndexUpdater)25 IndexProxy (org.neo4j.kernel.impl.api.index.IndexProxy)25 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)23 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)23 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)23 IndexProviderDescriptor (org.neo4j.internal.schema.IndexProviderDescriptor)22 KernelException (org.neo4j.exceptions.KernelException)20