Search in sources :

Example 46 with SchemaDescriptor

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

the class SchemaImpl method getIndexReference.

private static IndexDescriptor getIndexReference(SchemaRead schemaRead, TokenRead tokenRead, IndexDefinitionImpl index) throws SchemaRuleException {
    // Use the precise embedded index reference when available.
    IndexDescriptor reference = index.getIndexReference();
    if (reference != null) {
        return reference;
    }
    // Otherwise attempt to reverse engineer the schema that will let us look up the real IndexReference.
    int[] propertyKeyIds = resolveAndValidatePropertyKeys(tokenRead, index.getPropertyKeysArrayShared());
    SchemaDescriptor schema;
    if (index.isNodeIndex()) {
        int[] labelIds = resolveAndValidateTokens("Label", index.getLabelArrayShared(), Label::name, tokenRead::nodeLabel);
        if (index.isMultiTokenIndex()) {
            schema = fulltext(EntityType.NODE, labelIds, propertyKeyIds);
        } else if (index.getIndexType() == IndexType.LOOKUP) {
            schema = forAnyEntityTokens(EntityType.NODE);
        } else {
            schema = forLabel(labelIds[0], propertyKeyIds);
        }
    } else if (index.isRelationshipIndex()) {
        int[] relTypes = resolveAndValidateTokens("Relationship type", index.getRelationshipTypesArrayShared(), RelationshipType::name, tokenRead::relationshipType);
        if (index.isMultiTokenIndex()) {
            schema = fulltext(EntityType.RELATIONSHIP, relTypes, propertyKeyIds);
        } else if (index.getIndexType() == IndexType.LOOKUP) {
            schema = forAnyEntityTokens(EntityType.RELATIONSHIP);
        } else {
            schema = forRelType(relTypes[0], propertyKeyIds);
        }
    } else {
        throw new IllegalArgumentException("The given index is neither a node index, nor a relationship index: " + index + ".");
    }
    Iterator<IndexDescriptor> iterator = schemaRead.index(schema);
    if (!iterator.hasNext()) {
        throw new SchemaRuleNotFoundException(schema, tokenRead);
    }
    reference = iterator.next();
    if (iterator.hasNext()) {
        throw new DuplicateSchemaRuleException(schema, tokenRead);
    }
    return reference;
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) Label(org.neo4j.graphdb.Label) SchemaDescriptor.forLabel(org.neo4j.internal.schema.SchemaDescriptor.forLabel) SchemaRuleNotFoundException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaRuleNotFoundException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) DuplicateSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.DuplicateSchemaRuleException)

Example 47 with SchemaDescriptor

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

the class EntityUpdates method gatherUpdatesForPotentials.

@SuppressWarnings("ConstantConditions")
private <INDEX_KEY extends SchemaDescriptorSupplier> Iterable<IndexEntryUpdate<INDEX_KEY>> gatherUpdatesForPotentials(Iterable<INDEX_KEY> potentiallyRelevant) {
    List<IndexEntryUpdate<INDEX_KEY>> indexUpdates = new ArrayList<>();
    for (INDEX_KEY indexKey : potentiallyRelevant) {
        SchemaDescriptor schema = indexKey.schema();
        boolean relevantBefore = relevantBefore(schema);
        boolean relevantAfter = relevantAfter(schema);
        int[] propertyIds = schema.getPropertyIds();
        if (relevantBefore && !relevantAfter) {
            indexUpdates.add(IndexEntryUpdate.remove(entityId, indexKey, valuesBefore(propertyIds)));
        } else if (!relevantBefore && relevantAfter) {
            indexUpdates.add(IndexEntryUpdate.add(entityId, indexKey, valuesAfter(propertyIds)));
        } else if (relevantBefore && relevantAfter) {
            if (valuesChanged(propertyIds, schema.propertySchemaType())) {
                indexUpdates.add(IndexEntryUpdate.change(entityId, indexKey, valuesBefore(propertyIds), valuesAfter(propertyIds)));
            }
        }
    }
    return indexUpdates;
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) ArrayList(java.util.ArrayList)

Example 48 with SchemaDescriptor

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

the class IndexIT method shouldListCompositeIndexesInTheCoreAPI.

@Test
void shouldListCompositeIndexesInTheCoreAPI() throws Exception {
    KernelTransaction transaction = newTransaction(AUTH_DISABLED);
    long initialIndexCount = Iterators.count(transaction.schemaRead().indexesGetAll());
    SchemaDescriptor schema = SchemaDescriptor.forLabel(labelId, propertyKeyId, propertyKeyId2);
    transaction.schemaWrite().indexCreate(schema, "my index");
    commit();
    try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
        // then
        Set<IndexDefinition> indexes = Iterables.asSet(tx.schema().getIndexes());
        assertThat(indexes.size()).isEqualTo(initialIndexCount + 1);
        IndexDefinition index = tx.schema().getIndexByName("my index");
        assertThrows(IllegalStateException.class, index::getRelationshipTypes);
        assertThat(index.getLabels()).containsOnly(label(LABEL));
        assertThat(index.getPropertyKeys()).containsOnly(PROPERTY_KEY, PROPERTY_KEY2);
        assertFalse(index.isConstraintIndex(), "should not be a constraint index");
        assertFalse(index.isMultiTokenIndex(), "should not be a multi-token index");
        assertTrue(index.isCompositeIndex(), "should be a composite index");
        assertTrue(index.isNodeIndex(), "should be a node index");
        assertFalse(index.isRelationshipIndex(), "should not be a relationship index");
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Test(org.junit.jupiter.api.Test) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)

Example 49 with SchemaDescriptor

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

the class IndexIT method shouldListCompositeMultiTokenRelationshipIndexesInTheCoreAPI.

@Test
void shouldListCompositeMultiTokenRelationshipIndexesInTheCoreAPI() throws Exception {
    KernelTransaction transaction = newTransaction(AUTH_DISABLED);
    long initialIndexCount = Iterators.count(transaction.schemaRead().indexesGetAll());
    SchemaDescriptor schema = SchemaDescriptor.fulltext(EntityType.RELATIONSHIP, new int[] { relType, relType2 }, new int[] { propertyKeyId, propertyKeyId2 });
    IndexPrototype prototype = IndexPrototype.forSchema(schema, FulltextIndexProviderFactory.DESCRIPTOR).withIndexType(IndexType.FULLTEXT).withName("index name");
    transaction.schemaWrite().indexCreate(prototype);
    commit();
    try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
        // then
        Set<IndexDefinition> indexes = Iterables.asSet(tx.schema().getIndexes());
        assertThat(indexes.size()).isEqualTo(initialIndexCount + 1);
        IndexDefinition index = tx.schema().getIndexByName("index name");
        assertThrows(IllegalStateException.class, index::getLabels);
        assertThat(index.getRelationshipTypes()).containsOnly(withName(REL_TYPE), withName(REL_TYPE2));
        assertThat(index.getPropertyKeys()).containsOnly(PROPERTY_KEY, PROPERTY_KEY2);
        assertFalse(index.isConstraintIndex(), "should not be a constraint index");
        assertTrue(index.isMultiTokenIndex(), "should be a multi-token index");
        assertTrue(index.isCompositeIndex(), "should be a composite index");
        assertFalse(index.isNodeIndex(), "should not be a node index");
        assertTrue(index.isRelationshipIndex(), "should be a relationship index");
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) Test(org.junit.jupiter.api.Test) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)

Example 50 with SchemaDescriptor

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

the class IndexIT method shouldListRelationshipIndexesInTheCoreAPI.

@Test
void shouldListRelationshipIndexesInTheCoreAPI() throws Exception {
    KernelTransaction transaction = newTransaction(AUTH_DISABLED);
    long initialIndexCount = Iterators.count(transaction.schemaRead().indexesGetAll());
    SchemaDescriptor schema = SchemaDescriptor.forRelType(relType, propertyKeyId);
    transaction.schemaWrite().indexCreate(schema, "my index");
    commit();
    try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
        // then
        Set<IndexDefinition> indexes = Iterables.asSet(tx.schema().getIndexes());
        assertThat(indexes.size()).isEqualTo(initialIndexCount + 1);
        IndexDefinition index = tx.schema().getIndexByName("my index");
        assertThrows(IllegalStateException.class, index::getLabels);
        assertThat(index.getRelationshipTypes()).containsOnly(withName(REL_TYPE));
        assertThat(index.getPropertyKeys()).containsOnly(PROPERTY_KEY);
        assertFalse(index.isConstraintIndex(), "should not be a constraint index");
        assertFalse(index.isMultiTokenIndex(), "should not be a multi-token index");
        assertFalse(index.isCompositeIndex(), "should not be a composite index");
        assertFalse(index.isNodeIndex(), "should not be a node index");
        assertTrue(index.isRelationshipIndex(), "should be a relationship index");
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Test(org.junit.jupiter.api.Test) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)

Aggregations

SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)58 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)27 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)23 Test (org.junit.jupiter.api.Test)18 IndexPrototype (org.neo4j.internal.schema.IndexPrototype)18 RelationTypeSchemaDescriptor (org.neo4j.internal.schema.RelationTypeSchemaDescriptor)18 Value (org.neo4j.values.storable.Value)11 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)10 ArrayList (java.util.ArrayList)6 KernelTransactionImplementation (org.neo4j.kernel.impl.api.KernelTransactionImplementation)6 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)5 IndexConfig (org.neo4j.internal.schema.IndexConfig)5 MemoryTracker (org.neo4j.memory.MemoryTracker)5 KernelException (org.neo4j.exceptions.KernelException)4 UnspecifiedKernelException (org.neo4j.exceptions.UnspecifiedKernelException)4 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)4 IndexNotApplicableKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException)4 SchemaKernelException (org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException)4 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)4 IndexProviderDescriptor (org.neo4j.internal.schema.IndexProviderDescriptor)4