Search in sources :

Example 91 with IndexDescriptor

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

the class SchemaStatementProcedure method createStatement.

public static String createStatement(Function<String, IndexDescriptor> indexLookup, TokenRead tokenRead, ConstraintDescriptor constraint) throws ProcedureException {
    try {
        String name = constraint.getName();
        if (constraint.isIndexBackedConstraint()) {
            final String labelsOrRelTypes = labelsOrRelTypesAsStringArray(tokenRead, constraint.schema());
            final String properties = propertiesAsStringArray(tokenRead, constraint.schema());
            IndexDescriptor backingIndex = indexLookup.apply(name);
            String providerName = backingIndex.getIndexProvider().name();
            String config = btreeConfigAsString(backingIndex);
            if (constraint.isUniquenessConstraint()) {
                return format(CREATE_UNIQUE_PROPERTY_CONSTRAINT, name, labelsOrRelTypes, properties, providerName, config);
            }
            if (constraint.isNodeKeyConstraint()) {
                return format(CREATE_NODE_KEY_CONSTRAINT, name, labelsOrRelTypes, properties, providerName, config);
            }
        }
        if (constraint.isNodePropertyExistenceConstraint()) {
            // "create CONSTRAINT ON (a:A) ASSERT (a.p) IS NOT NULL"
            int labelId = constraint.schema().getLabelId();
            String label = tokenRead.nodeLabelName(labelId);
            int propertyId = constraint.schema().getPropertyId();
            String property = tokenRead.propertyKeyName(propertyId);
            return format(CREATE_NODE_EXISTENCE_CONSTRAINT, escapeBackticks(name), escapeBackticks(label), escapeBackticks(property));
        }
        if (constraint.isRelationshipPropertyExistenceConstraint()) {
            // "create CONSTRAINT ON ()-[r:R]-() ASSERT (r.p) IS NOT NULL"
            int relationshipTypeId = constraint.schema().getRelTypeId();
            String relationshipType = tokenRead.relationshipTypeName(relationshipTypeId);
            int propertyId = constraint.schema().getPropertyId();
            String property = tokenRead.propertyKeyName(propertyId);
            return format(CREATE_RELATIONSHIP_EXISTENCE_CONSTRAINT, escapeBackticks(name), escapeBackticks(relationshipType), escapeBackticks(property));
        }
        throw new IllegalArgumentException("Did not recognize constraint type " + constraint);
    } catch (KernelException e) {
        throw new ProcedureException(Status.General.UnknownError, e, "Failed to re-create create statement.");
    }
}
Also used : ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) PropertyKeyIdNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) KernelException(org.neo4j.exceptions.KernelException)

Example 92 with IndexDescriptor

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

the class SchemaStatementProcedure method createSchemaStatementResults.

static Collection<BuiltInProcedures.SchemaStatementResult> createSchemaStatementResults(SchemaReadCore schemaRead, TokenRead tokenRead) throws ProcedureException {
    Map<String, BuiltInProcedures.SchemaStatementResult> schemaStatements = new HashMap<>();
    // Indexes
    // If index is unique the assumption is that it is backing a constraint and will not be included.
    final Iterator<IndexDescriptor> allIndexes = schemaRead.indexesGetAll();
    while (allIndexes.hasNext()) {
        final IndexDescriptor index = allIndexes.next();
        if (includeIndex(schemaRead, index)) {
            final String name = index.getName();
            String type = SchemaRuleType.INDEX.name();
            final String createStatement = createStatement(tokenRead, index);
            final String dropStatement = dropStatement(index);
            schemaStatements.put(name, new BuiltInProcedures.SchemaStatementResult(name, type, createStatement, dropStatement));
        }
    }
    // Constraints
    Iterator<ConstraintDescriptor> allConstraints = schemaRead.constraintsGetAll();
    while (allConstraints.hasNext()) {
        ConstraintDescriptor constraint = allConstraints.next();
        if (includeConstraint(schemaRead, constraint)) {
            String name = constraint.getName();
            String type = SchemaRuleType.CONSTRAINT.name();
            String createStatement = createStatement(schemaRead::indexGetForName, tokenRead, constraint);
            String dropStatement = dropStatement(constraint);
            schemaStatements.put(name, new BuiltInProcedures.SchemaStatementResult(name, type, createStatement, dropStatement));
        }
    }
    return schemaStatements.values();
}
Also used : HashMap(java.util.HashMap) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Example 93 with IndexDescriptor

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

the class IndexProcedures method resampleIndex.

void resampleIndex(String indexName) throws ProcedureException {
    final IndexDescriptor index = getIndex(indexName);
    triggerSampling(index);
}
Also used : IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Example 94 with IndexDescriptor

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

the class SchemaProcedure method buildSchemaGraph.

public GraphResult buildSchemaGraph() {
    final Map<String, VirtualNodeHack> nodes = new HashMap<>();
    final Map<String, Set<VirtualRelationshipHack>> relationships = new HashMap<>();
    final KernelTransaction kernelTransaction = internalTransaction.kernelTransaction();
    AccessMode mode = kernelTransaction.securityContext().mode();
    try (KernelTransaction.Revertable ignore = kernelTransaction.overrideWith(SecurityContext.AUTH_DISABLED)) {
        Read dataRead = kernelTransaction.dataRead();
        TokenRead tokenRead = kernelTransaction.tokenRead();
        SchemaRead schemaRead = kernelTransaction.schemaRead();
        List<Pair<String, Integer>> labelNamesAndIds = new ArrayList<>();
        // Get all labels that are in use as seen by a super user
        List<Label> labelsInUse = stream(LABELS.inUse(kernelTransaction)).collect(Collectors.toList());
        for (Label label : labelsInUse) {
            String labelName = label.name();
            int labelId = tokenRead.nodeLabel(labelName);
            // Filter out labels that are denied or aren't explicitly allowed
            if (mode.allowsTraverseNode(labelId)) {
                labelNamesAndIds.add(Pair.of(labelName, labelId));
                Map<String, Object> properties = new HashMap<>();
                Iterator<IndexDescriptor> indexReferences = schemaRead.indexesGetForLabel(labelId);
                List<String> indexes = new ArrayList<>();
                while (indexReferences.hasNext()) {
                    IndexDescriptor index = indexReferences.next();
                    if (!index.isUnique()) {
                        String[] propertyNames = PropertyNameUtils.getPropertyKeys(tokenRead, index.schema().getPropertyIds());
                        indexes.add(String.join(",", propertyNames));
                    }
                }
                properties.put("indexes", indexes);
                Iterator<ConstraintDescriptor> nodePropertyConstraintIterator = schemaRead.constraintsGetForLabel(labelId);
                List<String> constraints = new ArrayList<>();
                while (nodePropertyConstraintIterator.hasNext()) {
                    ConstraintDescriptor constraint = nodePropertyConstraintIterator.next();
                    constraints.add(constraint.userDescription(tokenRead));
                }
                properties.put("constraints", constraints);
                getOrCreateLabel(label.name(), properties, nodes);
            }
        }
        // Get all relTypes that are in use as seen by a super user
        List<RelationshipType> relTypesInUse = stream(RELATIONSHIP_TYPES.inUse(kernelTransaction)).collect(Collectors.toList());
        for (RelationshipType relationshipType : relTypesInUse) {
            String relationshipTypeGetName = relationshipType.name();
            int relId = tokenRead.relationshipType(relationshipTypeGetName);
            // Filter out relTypes that are denied or aren't explicitly allowed
            if (mode.allowsTraverseRelType(relId)) {
                List<VirtualNodeHack> startNodes = new LinkedList<>();
                List<VirtualNodeHack> endNodes = new LinkedList<>();
                for (Pair<String, Integer> labelNameAndId : labelNamesAndIds) {
                    String labelName = labelNameAndId.first();
                    int labelId = labelNameAndId.other();
                    Map<String, Object> properties = new HashMap<>();
                    VirtualNodeHack node = getOrCreateLabel(labelName, properties, nodes);
                    if (dataRead.countsForRelationship(labelId, relId, TokenRead.ANY_LABEL) > 0) {
                        startNodes.add(node);
                    }
                    if (dataRead.countsForRelationship(TokenRead.ANY_LABEL, relId, labelId) > 0) {
                        endNodes.add(node);
                    }
                }
                for (VirtualNodeHack startNode : startNodes) {
                    for (VirtualNodeHack endNode : endNodes) {
                        addRelationship(startNode, endNode, relationshipTypeGetName, relationships);
                    }
                }
            }
        }
    }
    return getGraphResult(nodes, relationships);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Label(org.neo4j.graphdb.Label) RelationshipType(org.neo4j.graphdb.RelationshipType) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) TokenRead(org.neo4j.internal.kernel.api.TokenRead) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) Read(org.neo4j.internal.kernel.api.Read) Pair(org.neo4j.internal.helpers.collection.Pair) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) LinkedList(java.util.LinkedList) TokenRead(org.neo4j.internal.kernel.api.TokenRead) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) AccessMode(org.neo4j.internal.kernel.api.security.AccessMode)

Example 95 with IndexDescriptor

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

the class SchemaRuleSerialization35Test method assertSerializeAndDeserializeIndexRule.

// HELPERS
private static void assertSerializeAndDeserializeIndexRule(IndexDescriptor indexRule) throws MalformedSchemaRuleException {
    IndexDescriptor deserialized = assertIndexRule(serialiseAndDeserialise(indexRule));
    assertThat(deserialized.getId()).isEqualTo(indexRule.getId());
    assertThat(deserialized).isEqualTo(indexRule);
    assertThat(deserialized.schema()).isEqualTo(indexRule.schema());
    assertThat(deserialized.getIndexProvider()).isEqualTo(indexRule.getIndexProvider());
}
Also used : 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