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