Search in sources :

Example 21 with SchemaDescriptor

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

the class ConstraintsProcedureUtil method prettyPrint.

static String prettyPrint(ConstraintDescriptor constraintDescriptor, TokenNameLookup tokenNameLookup) {
    SchemaDescriptor schema = constraintDescriptor.schema();
    int[] entityTokenIds = schema.getEntityTokenIds();
    if (entityTokenIds.length != 1) {
        throw new IllegalArgumentException("Cannot pretty-print multi-token constraints: " + constraintDescriptor.userDescription(tokenNameLookup));
    }
    String entityTypeName = schema.entityType() == EntityType.NODE ? tokenNameLookup.labelGetName(entityTokenIds[0]) : tokenNameLookup.relationshipTypeGetName(entityTokenIds[0]);
    entityTypeName = escapeLabelOrRelTyp(entityTypeName);
    String entityName = entityTypeName.toLowerCase();
    String properties = formatProperties(schema.getPropertyIds(), tokenNameLookup, entityName);
    ConstraintType type = constraintDescriptor.type();
    switch(type) {
        case EXISTS:
            switch(schema.entityType()) {
                case NODE:
                    return "CONSTRAINT ON ( " + entityName + ":" + entityTypeName + " ) ASSERT " + properties + " IS NOT NULL";
                case RELATIONSHIP:
                    return "CONSTRAINT ON ()-[ " + entityName + ":" + entityTypeName + " ]-() ASSERT " + properties + " IS NOT NULL";
                default:
                    throw new IllegalStateException("Unknown schema entity type: " + schema.entityType() + ".");
            }
        case UNIQUE:
            return "CONSTRAINT ON ( " + entityName + ":" + entityTypeName + " ) ASSERT " + properties + " IS UNIQUE";
        case UNIQUE_EXISTS:
            return "CONSTRAINT ON ( " + entityName + ":" + entityTypeName + " ) ASSERT " + properties + " IS NODE KEY";
        default:
            throw new IllegalStateException("Unknown constraint type: " + type + ".");
    }
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) ConstraintType(org.neo4j.internal.schema.ConstraintType)

Example 22 with SchemaDescriptor

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

the class SimpleIndexPopulatorCompatibility method shouldApplyUpdatesIdempotently.

@Test
public void shouldApplyUpdatesIdempotently() throws Exception {
    // GIVEN
    IndexSamplingConfig indexSamplingConfig = new IndexSamplingConfig(Config.defaults());
    final Value propertyValue = Values.of("value1");
    withPopulator(indexProvider.getPopulator(descriptor, indexSamplingConfig, heapBufferFactory(1024), INSTANCE, tokenNameLookup), p -> {
        long nodeId = 1;
        // update using populator...
        IndexEntryUpdate<SchemaDescriptor> update = add(nodeId, descriptor.schema(), propertyValue);
        p.add(singletonList(update), NULL);
        // ...is the same as update using updater
        try (IndexUpdater updater = p.newPopulatingUpdater((node, propertyId, cursorContext) -> propertyValue, NULL)) {
            updater.process(update);
        }
    });
    // THEN
    try (IndexAccessor accessor = indexProvider.getOnlineAccessor(descriptor, indexSamplingConfig, tokenNameLookup)) {
        try (ValueIndexReader reader = accessor.newValueReader();
            NodeValueIterator nodes = new NodeValueIterator()) {
            int propertyKeyId = descriptor.schema().getPropertyId();
            reader.query(NULL_CONTEXT, nodes, unconstrained(), PropertyIndexQuery.exact(propertyKeyId, propertyValue));
            assertEquals(asSet(1L), PrimitiveLongCollections.toSet(nodes));
        }
    }
}
Also used : NodeValueIterator(org.neo4j.kernel.impl.index.schema.NodeValueIterator) IndexSamplingConfig(org.neo4j.kernel.impl.api.index.IndexSamplingConfig) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) Value(org.neo4j.values.storable.Value) Values.stringValue(org.neo4j.values.storable.Values.stringValue) Test(org.junit.Test)

Example 23 with SchemaDescriptor

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

the class SchemaProcessorTest method shouldHandleCorrectDescriptorVersions.

@Test
void shouldHandleCorrectDescriptorVersions() {
    List<String> callHistory = new ArrayList<>();
    SchemaProcessor processor = new SchemaProcessor() {

        @Override
        public void processSpecific(LabelSchemaDescriptor schema) {
            callHistory.add("LabelSchemaDescriptor");
        }

        @Override
        public void processSpecific(RelationTypeSchemaDescriptor schema) {
            callHistory.add("RelationTypeSchemaDescriptor");
        }

        @Override
        public void processSpecific(SchemaDescriptor schemaDescriptor) {
            callHistory.add("SchemaDescriptor");
        }
    };
    disguisedLabel().processWith(processor);
    disguisedLabel().processWith(processor);
    disguisedRelType().processWith(processor);
    disguisedLabel().processWith(processor);
    disguisedRelType().processWith(processor);
    disguisedRelType().processWith(processor);
    assertThat(callHistory).containsExactly("LabelSchemaDescriptor", "LabelSchemaDescriptor", "RelationTypeSchemaDescriptor", "LabelSchemaDescriptor", "RelationTypeSchemaDescriptor", "RelationTypeSchemaDescriptor");
}
Also used : RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) ArrayList(java.util.ArrayList) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) SchemaProcessor(org.neo4j.internal.schema.SchemaProcessor) Test(org.junit.jupiter.api.Test)

Example 24 with SchemaDescriptor

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

the class SchemaRuleSerialization35 method readConstraintRule.

// READ CONSTRAINT
private static ConstraintDescriptor readConstraintRule(long id, ByteBuffer source) throws MalformedSchemaRuleException {
    SchemaDescriptor schema;
    byte constraintRuleType = source.get();
    String name;
    switch(constraintRuleType) {
        case EXISTS_CONSTRAINT:
            schema = readSchema(source);
            name = readRuleName(source).orElse(null);
            return ConstraintDescriptorFactory.existsForSchema(schema).withId(id).withName(name);
        case UNIQUE_CONSTRAINT:
            long ownedUniqueIndex = source.getLong();
            schema = readSchema(source);
            UniquenessConstraintDescriptor descriptor = ConstraintDescriptorFactory.uniqueForSchema(schema);
            name = readRuleName(source).orElse(null);
            return descriptor.withId(id).withOwnedIndexId(ownedUniqueIndex).withName(name);
        case UNIQUE_EXISTS_CONSTRAINT:
            long ownedNodeKeyIndex = source.getLong();
            schema = readSchema(source);
            NodeKeyConstraintDescriptor nodeKeyConstraintDescriptor = ConstraintDescriptorFactory.nodeKeyForSchema(schema);
            name = readRuleName(source).orElse(null);
            return nodeKeyConstraintDescriptor.withId(id).withOwnedIndexId(ownedNodeKeyIndex).withName(name);
        default:
            throw new MalformedSchemaRuleException(format("Got unknown constraint rule type '%d'.", constraintRuleType));
    }
}
Also used : LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) MalformedSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)

Example 25 with SchemaDescriptor

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

the class SchemaStore method buildIndexRule.

private static SchemaRule buildIndexRule(long schemaRuleId, Map<String, Value> props) throws MalformedSchemaRuleException {
    SchemaDescriptor schema = buildSchemaDescriptor(props);
    String indexRuleType = getString(PROP_INDEX_RULE_TYPE, props);
    boolean unique = parseIndexType(indexRuleType);
    IndexPrototype prototype = unique ? IndexPrototype.uniqueForSchema(schema) : IndexPrototype.forSchema(schema);
    prototype = prototype.withName(getString(PROP_SCHEMA_RULE_NAME, props));
    prototype = prototype.withIndexType(getIndexType(getString(PROP_INDEX_TYPE, props)));
    String providerKey = getString(PROP_INDEX_PROVIDER_NAME, props);
    String providerVersion = getString(PROP_INDEX_PROVIDER_VERSION, props);
    IndexProviderDescriptor providerDescriptor = new IndexProviderDescriptor(providerKey, providerVersion);
    prototype = prototype.withIndexProvider(providerDescriptor);
    IndexDescriptor index = prototype.materialise(schemaRuleId);
    IndexConfig indexConfig = extractIndexConfig(props);
    index = index.withIndexConfig(indexConfig);
    if (props.containsKey(PROP_OWNING_CONSTRAINT)) {
        index = index.withOwningConstraintId(getLong(PROP_OWNING_CONSTRAINT, props));
    }
    return index;
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) IndexConfig(org.neo4j.internal.schema.IndexConfig) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) IndexProviderDescriptor(org.neo4j.internal.schema.IndexProviderDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

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