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;
}
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;
}
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;
}
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);
}
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());
}
}
Aggregations