Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package apoc.schema;
import static org.neo4j.graphdb.Label.label;
import static org.neo4j.internal.schema.SchemaUserDescription.TOKEN_LABEL;
import static org.neo4j.internal.schema.SchemaUserDescription.TOKEN_REL_TYPE;
import apoc.result.AssertSchemaResult;
import apoc.result.IndexConstraintNodeInfo;
import apoc.result.IndexConstraintRelationshipInfo;
import apoc.util.Util;
import apoc.util.collection.Iterables;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.apache.commons.lang3.StringUtils;
import org.neo4j.common.EntityType;
import org.neo4j.common.TokenNameLookup;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.schema.ConstraintDefinition;
import org.neo4j.graphdb.schema.ConstraintType;
import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.graphdb.schema.IndexType;
import org.neo4j.graphdb.schema.Schema;
import org.neo4j.internal.kernel.api.InternalIndexState;
import org.neo4j.internal.kernel.api.PopulationProgress;
import org.neo4j.internal.kernel.api.SchemaRead;
import org.neo4j.internal.kernel.api.TokenRead;
import org.neo4j.internal.kernel.api.exceptions.LabelNotFoundKernelException;
import org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException;
import org.neo4j.internal.schema.IndexDescriptor;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.Statement;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Mode;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.NotThreadSafe;
import org.neo4j.procedure.Procedure;
import org.neo4j.procedure.UserFunction;
import org.neo4j.token.api.TokenConstants;
public class Schemas {
private static final String IDX_NOT_FOUND = "NOT_FOUND";
@Context
public Transaction tx;
@Context
public KernelTransaction ktx;
@NotThreadSafe
@Procedure(name = "apoc.schema.assert", mode = Mode.SCHEMA)
@Description("Drops all other existing indexes and constraints when `dropExisting` is `true` (default is `true`).\n"
+ "Asserts at the end of the operation that the given indexes and unique constraints are there.")
public Stream schemaAssert(
@Name("indexes") Map> indexes,
@Name("constraints") Map> constraints,
@Name(value = "dropExisting", defaultValue = "true") boolean dropExisting) {
return Stream.concat(
assertIndexes(indexes, dropExisting).stream(), assertConstraints(constraints, dropExisting).stream());
}
@NotThreadSafe
@Procedure(name = "apoc.schema.nodes", mode = Mode.SCHEMA)
@Description("Returns all indexes and constraints information for all `NODE` labels in the database.\n"
+ "It is possible to define a set of labels to include or exclude in the config parameters.")
public Stream nodes(
@Name(value = "config", defaultValue = "{}") Map config) {
return indexesAndConstraintsForNode(config);
}
@NotThreadSafe
@Procedure(name = "apoc.schema.relationships", mode = Mode.SCHEMA)
@Description("Returns the indexes and constraints information for all the relationship types in the database.\n"
+ "It is possible to define a set of relationship types to include or exclude in the config parameters.")
public Stream relationships(
@Name(value = "config", defaultValue = "{}") Map config) {
return indexesAndConstraintsForRelationships(config);
}
@NotThreadSafe
@UserFunction(name = "apoc.schema.node.indexExists")
@Description(
"Returns a `BOOLEAN` depending on whether or not an index exists for the given `NODE` label with the given property names.")
public Boolean indexExistsOnNode(
@Name("labelName") String labelName, @Name("propertyName") List propertyNames) {
return indexExists(labelName, propertyNames);
}
@NotThreadSafe
@UserFunction(value = "apoc.schema.relationship.indexExists")
@Description(
"Returns a `BOOLEAN` depending on whether or not an index exists for the given `RELATIONSHIP` type with the given property names.")
public Boolean indexExistsOnRelationship(
@Name("type") String relName, @Name("propertyName") List propertyNames) {
return indexExistsForRelationship(relName, propertyNames);
}
@NotThreadSafe
@UserFunction(name = "apoc.schema.node.constraintExists")
@Description(
"Returns a `BOOLEAN` depending on whether or not a constraint exists for the given `NODE` label with the given property names.")
public Boolean constraintExistsOnNode(
@Name("labelName") String labelName, @Name("propertyName") List propertyNames) {
return constraintsExists(labelName, propertyNames);
}
@NotThreadSafe
@UserFunction(name = "apoc.schema.relationship.constraintExists")
@Description(
"Returns a `BOOLEAN` depending on whether or not a constraint exists for the given `RELATIONSHIP` type with the given property names.")
public Boolean constraintExistsOnRelationship(
@Name("type") String type, @Name("propertyName") List propertyNames) {
return constraintsExistsForRelationship(type, propertyNames);
}
public List assertConstraints(Map> constraints0, boolean dropExisting) {
Map> constraints = copyMapOfObjects(constraints0);
List result = new ArrayList<>(constraints.size());
Schema schema = tx.schema();
for (ConstraintDefinition definition : schema.getConstraints()) {
ConstraintType constraintType = definition.getConstraintType();
String label = Util.isRelationshipCategory(constraintType)
? definition.getRelationshipType().name()
: definition.getLabel().name();
AssertSchemaResult info = new AssertSchemaResult(label, Iterables.asList(definition.getPropertyKeys()));
if (Util.constraintIsUnique(constraintType)) {
info = info.unique();
}
if (!checkIfConstraintExists(label, constraints, info)) {
if (dropExisting) {
definition.drop();
info.dropped();
}
}
result.add(info);
}
for (Map.Entry> constraint : constraints.entrySet()) {
for (Object key : constraint.getValue()) {
if (key instanceof String) {
result.add(createUniqueConstraint(schema, constraint.getKey(), key.toString()));
} else if (key instanceof List) {
result.add(createNodeKeyConstraint(constraint.getKey(), (List