Search in sources :

Example 51 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j-apoc-procedures by neo4j-contrib.

the class Meta method collectMetaData.

private Map<String, Map<String, MetaResult>> collectMetaData() {
    Map<String, Map<String, MetaResult>> metaData = new LinkedHashMap<>(100);
    Schema schema = db.schema();
    Map<String, Iterable<ConstraintDefinition>> relConstraints = new HashMap<>(20);
    for (RelationshipType type : db.getAllRelationshipTypesInUse()) {
        metaData.put(type.name(), new LinkedHashMap<>(10));
        relConstraints.put(type.name(), schema.getConstraints(type));
    }
    for (Label label : db.getAllLabelsInUse()) {
        Map<String, MetaResult> nodeMeta = new LinkedHashMap<>(50);
        String labelName = label.name();
        metaData.put(labelName, nodeMeta);
        Iterable<ConstraintDefinition> constraints = schema.getConstraints(label);
        Set<String> indexed = new LinkedHashSet<>();
        for (IndexDefinition index : schema.getIndexes(label)) {
            for (String prop : index.getPropertyKeys()) {
                indexed.add(prop);
            }
        }
        try (ResourceIterator<Node> nodes = db.findNodes(label)) {
            int count = 0;
            while (nodes.hasNext() && count++ < SAMPLE) {
                Node node = nodes.next();
                addRelationships(metaData, nodeMeta, labelName, node, relConstraints);
                addProperties(nodeMeta, labelName, constraints, indexed, node, node);
            }
        }
    }
    return metaData;
}
Also used : Schema(org.neo4j.graphdb.schema.Schema) VirtualNode(apoc.result.VirtualNode) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Collectors.toMap(java.util.stream.Collectors.toMap) Collections.singletonMap(java.util.Collections.singletonMap)

Example 52 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j-apoc-procedures by neo4j-contrib.

the class NodesAndRelsSubGraph method getConstraints.

@Override
public Iterable<ConstraintDefinition> getConstraints() {
    Schema schema = db.schema();
    ArrayList<ConstraintDefinition> constraints = new ArrayList<>(labels.size() * 2);
    for (String label : labels) {
        Iterables.addAll(constraints, schema.getConstraints(Label.label(label)));
    }
    return constraints;
}
Also used : Schema(org.neo4j.graphdb.schema.Schema) ArrayList(java.util.ArrayList) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Example 53 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j-apoc-procedures by neo4j-contrib.

the class Schemas method assertConstraints.

public List<AssertSchemaResult> assertConstraints(Map<String, List<String>> constraints0, boolean dropExisting) throws ExecutionException, InterruptedException {
    Map<String, List<String>> constraints = copy(constraints0);
    List<AssertSchemaResult> result = new ArrayList<>(constraints.size());
    Schema schema = db.schema();
    for (ConstraintDefinition definition : schema.getConstraints()) {
        if (!definition.isConstraintType(ConstraintType.UNIQUENESS))
            continue;
        String label = definition.getLabel().name();
        String key = Iterables.single(definition.getPropertyKeys());
        AssertSchemaResult info = new AssertSchemaResult(label, key).unique();
        if (!constraints.containsKey(label) || !constraints.get(label).remove(key)) {
            if (dropExisting) {
                definition.drop();
                info.dropped();
            }
        }
        result.add(info);
    }
    for (Map.Entry<String, List<String>> constraint : constraints.entrySet()) {
        for (String key : constraint.getValue()) {
            schema.constraintFor(label(constraint.getKey())).assertPropertyIsUnique(key).create();
            result.add(new AssertSchemaResult(constraint.getKey(), key).unique().created());
        }
    }
    return result;
}
Also used : AssertSchemaResult(apoc.result.AssertSchemaResult) Schema(org.neo4j.graphdb.schema.Schema) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Example 54 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j-apoc-procedures by neo4j-contrib.

the class Schemas method indexesAndConstraintsForNode.

/**
 * Collects indexes and constraints for nodes
 *
 * @return
 */
private Stream<IndexConstraintNodeInfo> indexesAndConstraintsForNode() {
    Schema schema = db.schema();
    // Indexes
    Stream<IndexConstraintNodeInfo> indexes = StreamSupport.stream(schema.getIndexes().spliterator(), false).filter(indexDefinition -> !indexDefinition.isConstraintIndex()).map(indexDefinition -> this.nodeInfoFromIndexDefinition(indexDefinition, schema));
    // Constraints
    Stream<IndexConstraintNodeInfo> constraints = StreamSupport.stream(schema.getConstraints().spliterator(), false).filter(constraintDefinition -> !constraintDefinition.isConstraintType(ConstraintType.RELATIONSHIP_PROPERTY_EXISTENCE)).map(this::nodeInfoFromConstraintDefinition);
    return Stream.concat(indexes, constraints);
}
Also used : AssertSchemaResult(apoc.result.AssertSchemaResult) ConstraintRelationshipInfo(apoc.result.ConstraintRelationshipInfo) java.util(java.util) Label(org.neo4j.graphdb.Label) org.neo4j.procedure(org.neo4j.procedure) Label.label(org.neo4j.graphdb.Label.label) IndexConstraintNodeInfo(apoc.result.IndexConstraintNodeInfo) StringUtils(org.apache.commons.lang3.StringUtils) Schema(org.neo4j.graphdb.schema.Schema) ExecutionException(java.util.concurrent.ExecutionException) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) Iterables(org.neo4j.helpers.collection.Iterables) Stream(java.util.stream.Stream) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) ConstraintType(org.neo4j.graphdb.schema.ConstraintType) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) RelationshipType(org.neo4j.graphdb.RelationshipType) StreamSupport(java.util.stream.StreamSupport) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Schema(org.neo4j.graphdb.schema.Schema) IndexConstraintNodeInfo(apoc.result.IndexConstraintNodeInfo)

Example 55 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j-apoc-procedures by neo4j-contrib.

the class SchemasTest method testRetainSchemaWhenNotUsingDropExisting.

@Test
public void testRetainSchemaWhenNotUsingDropExisting() throws Exception {
    db.execute("CREATE CONSTRAINT ON (f:Foo) ASSERT f.bar IS UNIQUE").close();
    testResult(db, "CALL apoc.schema.assert(null, {Bar:['foo', 'bar']}, false)", (result) -> {
        Map<String, Object> r = result.next();
        assertEquals("Foo", r.get("label"));
        assertEquals("bar", r.get("key"));
        assertEquals(true, r.get("unique"));
        assertEquals("KEPT", r.get("action"));
        r = result.next();
        assertEquals("Bar", r.get("label"));
        assertEquals("foo", r.get("key"));
        assertEquals(true, r.get("unique"));
        assertEquals("CREATED", r.get("action"));
        r = result.next();
        assertEquals("Bar", r.get("label"));
        assertEquals("bar", r.get("key"));
        assertEquals(true, r.get("unique"));
        assertEquals("CREATED", r.get("action"));
    });
    try (Transaction tx = db.beginTx()) {
        List<ConstraintDefinition> constraints = Iterables.asList(db.schema().getConstraints());
        assertEquals(3, constraints.size());
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) Test(org.junit.Test)

Aggregations

ConstraintDefinition (org.neo4j.graphdb.schema.ConstraintDefinition)70 Transaction (org.neo4j.graphdb.Transaction)26 Test (org.junit.Test)18 Test (org.junit.jupiter.api.Test)18 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)18 RepeatedTest (org.junit.jupiter.api.RepeatedTest)16 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)14 ConstraintCreator (org.neo4j.graphdb.schema.ConstraintCreator)7 ArrayList (java.util.ArrayList)5 Schema (org.neo4j.graphdb.schema.Schema)5 NodePropertyExistenceConstraintDefinition (org.neo4j.kernel.impl.coreapi.schema.NodePropertyExistenceConstraintDefinition)5 RelationshipPropertyExistenceConstraintDefinition (org.neo4j.kernel.impl.coreapi.schema.RelationshipPropertyExistenceConstraintDefinition)5 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)4 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)4 UniquenessConstraintDefinition (org.neo4j.kernel.impl.coreapi.schema.UniquenessConstraintDefinition)4 Label (org.neo4j.graphdb.Label)3 NodeRepresentationTest (org.neo4j.server.rest.repr.NodeRepresentationTest)3 RelationshipRepresentationTest (org.neo4j.server.rest.repr.RelationshipRepresentationTest)3 AssertSchemaResult (apoc.result.AssertSchemaResult)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2