use of org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor in project neo4j by neo4j.
the class ConstraintEnforcingEntityOperations method nodeSetProperty.
@Override
public Property nodeSetProperty(KernelStatement state, long nodeId, DefinedProperty property) throws EntityNotFoundException, AutoIndexingKernelException, InvalidTransactionTypeKernelException, ConstraintValidationException {
try (Cursor<NodeItem> cursor = nodeCursorById(state, nodeId)) {
NodeItem node = cursor.get();
Iterator<ConstraintDescriptor> constraints = getConstraintsInvolvingProperty(state, property.propertyKeyId());
Iterator<UniquenessConstraintDescriptor> uniquenessConstraints = new CastingIterator<>(constraints, UniquenessConstraintDescriptor.class);
nodeSchemaMatcher.onMatchingSchema(state, uniquenessConstraints, node, property.propertyKeyId(), constraint -> {
validateNoExistingNodeWithExactValues(state, constraint, getAllPropertyValues(state, constraint.schema(), node, property), node.id());
});
}
return entityWriteOperations.nodeSetProperty(state, nodeId, property);
}
use of org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor in project neo4j by neo4j.
the class SchemaProcedure method buildSchemaGraph.
public GraphResult buildSchemaGraph() {
final Map<String, NodeImpl> nodes = new HashMap<>();
final Map<String, Set<RelationshipImpl>> relationships = new HashMap<>();
try (Statement statement = kernelTransaction.acquireStatement()) {
ReadOperations readOperations = statement.readOperations();
StatementTokenNameLookup statementTokenNameLookup = new StatementTokenNameLookup(readOperations);
try (Transaction transaction = graphDatabaseAPI.beginTx()) {
// add all labelsInDatabase
ResourceIterator<Label> labelsInDatabase = graphDatabaseAPI.getAllLabelsInUse().iterator();
while (labelsInDatabase.hasNext()) {
Label label = labelsInDatabase.next();
Map<String, Object> properties = new HashMap<>();
Iterator<NewIndexDescriptor> indexDescriptorIterator = readOperations.indexesGetForLabel(readOperations.labelGetForName(label.name()));
ArrayList<String> indexes = new ArrayList<>();
while (indexDescriptorIterator.hasNext()) {
NewIndexDescriptor index = indexDescriptorIterator.next();
String[] propertyNames = PropertyNameUtils.getPropertyKeys(statementTokenNameLookup, index.schema().getPropertyIds());
indexes.add(String.join(",", propertyNames));
}
properties.put("indexes", indexes);
Iterator<ConstraintDescriptor> nodePropertyConstraintIterator = readOperations.constraintsGetForLabel(readOperations.labelGetForName(label.name()));
ArrayList<String> constraints = new ArrayList<>();
while (nodePropertyConstraintIterator.hasNext()) {
PropertyConstraint constraint = ConstraintBoundary.map(nodePropertyConstraintIterator.next());
constraints.add(constraint.userDescription(statementTokenNameLookup));
}
properties.put("constraints", constraints);
getOrCreateLabel(label.name(), properties, nodes);
}
//add all relationships
Iterator<RelationshipType> relationshipTypeIterator = graphDatabaseAPI.getAllRelationshipTypesInUse().iterator();
while (relationshipTypeIterator.hasNext()) {
RelationshipType relationshipType = relationshipTypeIterator.next();
String relationshipTypeGetName = relationshipType.name();
int relId = readOperations.relationshipTypeGetForName(relationshipTypeGetName);
ResourceIterator<Label> labelsInUse = graphDatabaseAPI.getAllLabelsInUse().iterator();
List<NodeImpl> startNodes = new LinkedList<>();
List<NodeImpl> endNodes = new LinkedList<>();
while (labelsInUse.hasNext()) {
Label labelToken = labelsInUse.next();
String labelName = labelToken.name();
Map<String, Object> properties = new HashMap<>();
NodeImpl node = getOrCreateLabel(labelName, properties, nodes);
int labelId = readOperations.labelGetForName(labelName);
if (readOperations.countsForRelationship(labelId, relId, ReadOperations.ANY_LABEL) > 0) {
startNodes.add(node);
}
if (readOperations.countsForRelationship(ReadOperations.ANY_LABEL, relId, labelId) > 0) {
endNodes.add(node);
}
}
for (NodeImpl startNode : startNodes) {
for (NodeImpl endNode : endNodes) {
RelationshipImpl relationship = addRelationship(startNode, endNode, relationshipTypeGetName, relationships);
}
}
}
transaction.success();
return getGraphResult(nodes, relationships);
}
}
}
use of org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor in project neo4j by neo4j.
the class StateHandlingStatementOperations method uniquePropertyConstraintCreate.
@Override
public UniquenessConstraintDescriptor uniquePropertyConstraintCreate(KernelStatement state, LabelSchemaDescriptor descriptor) throws CreateConstraintFailureException {
UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema(descriptor);
try {
if (state.hasTxStateWithChanges() && // ..., DROP, *CREATE*
state.txState().indexDoUnRemove(constraint.ownedIndexDescriptor())) {
// creation is undoing a drop
if (// CREATE, ..., DROP, *CREATE*
!state.txState().constraintDoUnRemove(constraint)) {
// ... the drop we are undoing did itself undo a prior create...
state.txState().constraintDoAdd(constraint, state.txState().indexCreatedForConstraint(constraint));
}
} else // *CREATE*
{
// create from scratch
Iterator<ConstraintDescriptor> it = storeLayer.constraintsGetForSchema(descriptor);
while (it.hasNext()) {
if (it.next().equals(constraint)) {
return constraint;
}
}
long indexId = constraintIndexCreator.createUniquenessConstraintIndex(state, this, descriptor);
state.txState().constraintDoAdd(constraint, indexId);
}
return constraint;
} catch (UniquePropertyValueValidationException | DropIndexFailureException | TransactionFailureException e) {
throw new CreateConstraintFailureException(constraint, e);
}
}
use of org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor in project neo4j by neo4j.
the class SchemaImpl method asConstraintDefinitions.
private Iterable<ConstraintDefinition> asConstraintDefinitions(Iterator<? extends ConstraintDescriptor> constraints, ReadOperations readOperations) {
// Intentionally create an eager list so that used statement can be closed
List<ConstraintDefinition> definitions = new ArrayList<>();
while (constraints.hasNext()) {
ConstraintDescriptor constraint = constraints.next();
definitions.add(asConstraintDefinition(ConstraintBoundary.map(constraint), readOperations));
}
return definitions;
}
use of org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor in project neo4j by neo4j.
the class SchemaRuleSerialization method lengthOf.
/**
* Compute the byte size needed to serialize the provided ConstraintRule using serialize.
* @param constraintRule the ConstraintRule
* @return the byte size of ConstraintRule
*/
public static int lengthOf(ConstraintRule constraintRule) {
// legacy label or relType id
int length = 4;
// schema rule type
length += 1;
// constraint type
length += 1;
ConstraintDescriptor constraintDescriptor = constraintRule.getConstraintDescriptor();
if (constraintDescriptor.type() == ConstraintDescriptor.Type.UNIQUE) {
// owned index id
length += 8;
}
length += constraintDescriptor.schema().computeWith(schemaSizeComputer);
length += UTF8.computeRequiredByteBufferSize(constraintRule.getName());
return length;
}
Aggregations