use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.
the class ConstraintsProcedureUtil method prettyPrint.
static String prettyPrint(ConstraintDescriptor constraintDescriptor, TokenNameLookup tokenNameLookup) {
SchemaDescriptor schema = constraintDescriptor.schema();
int[] entityTokenIds = schema.getEntityTokenIds();
if (entityTokenIds.length != 1) {
throw new IllegalArgumentException("Cannot pretty-print multi-token constraints: " + constraintDescriptor.userDescription(tokenNameLookup));
}
String entityTypeName = schema.entityType() == EntityType.NODE ? tokenNameLookup.labelGetName(entityTokenIds[0]) : tokenNameLookup.relationshipTypeGetName(entityTokenIds[0]);
entityTypeName = escapeLabelOrRelTyp(entityTypeName);
String entityName = entityTypeName.toLowerCase();
String properties = formatProperties(schema.getPropertyIds(), tokenNameLookup, entityName);
ConstraintType type = constraintDescriptor.type();
switch(type) {
case EXISTS:
switch(schema.entityType()) {
case NODE:
return "CONSTRAINT ON ( " + entityName + ":" + entityTypeName + " ) ASSERT " + properties + " IS NOT NULL";
case RELATIONSHIP:
return "CONSTRAINT ON ()-[ " + entityName + ":" + entityTypeName + " ]-() ASSERT " + properties + " IS NOT NULL";
default:
throw new IllegalStateException("Unknown schema entity type: " + schema.entityType() + ".");
}
case UNIQUE:
return "CONSTRAINT ON ( " + entityName + ":" + entityTypeName + " ) ASSERT " + properties + " IS UNIQUE";
case UNIQUE_EXISTS:
return "CONSTRAINT ON ( " + entityName + ":" + entityTypeName + " ) ASSERT " + properties + " IS NODE KEY";
default:
throw new IllegalStateException("Unknown constraint type: " + type + ".");
}
}
use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.
the class SimpleIndexPopulatorCompatibility method shouldApplyUpdatesIdempotently.
@Test
public void shouldApplyUpdatesIdempotently() throws Exception {
// GIVEN
IndexSamplingConfig indexSamplingConfig = new IndexSamplingConfig(Config.defaults());
final Value propertyValue = Values.of("value1");
withPopulator(indexProvider.getPopulator(descriptor, indexSamplingConfig, heapBufferFactory(1024), INSTANCE, tokenNameLookup), p -> {
long nodeId = 1;
// update using populator...
IndexEntryUpdate<SchemaDescriptor> update = add(nodeId, descriptor.schema(), propertyValue);
p.add(singletonList(update), NULL);
// ...is the same as update using updater
try (IndexUpdater updater = p.newPopulatingUpdater((node, propertyId, cursorContext) -> propertyValue, NULL)) {
updater.process(update);
}
});
// THEN
try (IndexAccessor accessor = indexProvider.getOnlineAccessor(descriptor, indexSamplingConfig, tokenNameLookup)) {
try (ValueIndexReader reader = accessor.newValueReader();
NodeValueIterator nodes = new NodeValueIterator()) {
int propertyKeyId = descriptor.schema().getPropertyId();
reader.query(NULL_CONTEXT, nodes, unconstrained(), PropertyIndexQuery.exact(propertyKeyId, propertyValue));
assertEquals(asSet(1L), PrimitiveLongCollections.toSet(nodes));
}
}
}
use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.
the class SchemaProcessorTest method shouldHandleCorrectDescriptorVersions.
@Test
void shouldHandleCorrectDescriptorVersions() {
List<String> callHistory = new ArrayList<>();
SchemaProcessor processor = new SchemaProcessor() {
@Override
public void processSpecific(LabelSchemaDescriptor schema) {
callHistory.add("LabelSchemaDescriptor");
}
@Override
public void processSpecific(RelationTypeSchemaDescriptor schema) {
callHistory.add("RelationTypeSchemaDescriptor");
}
@Override
public void processSpecific(SchemaDescriptor schemaDescriptor) {
callHistory.add("SchemaDescriptor");
}
};
disguisedLabel().processWith(processor);
disguisedLabel().processWith(processor);
disguisedRelType().processWith(processor);
disguisedLabel().processWith(processor);
disguisedRelType().processWith(processor);
disguisedRelType().processWith(processor);
assertThat(callHistory).containsExactly("LabelSchemaDescriptor", "LabelSchemaDescriptor", "RelationTypeSchemaDescriptor", "LabelSchemaDescriptor", "RelationTypeSchemaDescriptor", "RelationTypeSchemaDescriptor");
}
use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.
the class SchemaRuleSerialization35 method readConstraintRule.
// READ CONSTRAINT
private static ConstraintDescriptor readConstraintRule(long id, ByteBuffer source) throws MalformedSchemaRuleException {
SchemaDescriptor schema;
byte constraintRuleType = source.get();
String name;
switch(constraintRuleType) {
case EXISTS_CONSTRAINT:
schema = readSchema(source);
name = readRuleName(source).orElse(null);
return ConstraintDescriptorFactory.existsForSchema(schema).withId(id).withName(name);
case UNIQUE_CONSTRAINT:
long ownedUniqueIndex = source.getLong();
schema = readSchema(source);
UniquenessConstraintDescriptor descriptor = ConstraintDescriptorFactory.uniqueForSchema(schema);
name = readRuleName(source).orElse(null);
return descriptor.withId(id).withOwnedIndexId(ownedUniqueIndex).withName(name);
case UNIQUE_EXISTS_CONSTRAINT:
long ownedNodeKeyIndex = source.getLong();
schema = readSchema(source);
NodeKeyConstraintDescriptor nodeKeyConstraintDescriptor = ConstraintDescriptorFactory.nodeKeyForSchema(schema);
name = readRuleName(source).orElse(null);
return nodeKeyConstraintDescriptor.withId(id).withOwnedIndexId(ownedNodeKeyIndex).withName(name);
default:
throw new MalformedSchemaRuleException(format("Got unknown constraint rule type '%d'.", constraintRuleType));
}
}
use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.
the class SchemaStore method buildIndexRule.
private static SchemaRule buildIndexRule(long schemaRuleId, Map<String, Value> props) throws MalformedSchemaRuleException {
SchemaDescriptor schema = buildSchemaDescriptor(props);
String indexRuleType = getString(PROP_INDEX_RULE_TYPE, props);
boolean unique = parseIndexType(indexRuleType);
IndexPrototype prototype = unique ? IndexPrototype.uniqueForSchema(schema) : IndexPrototype.forSchema(schema);
prototype = prototype.withName(getString(PROP_SCHEMA_RULE_NAME, props));
prototype = prototype.withIndexType(getIndexType(getString(PROP_INDEX_TYPE, props)));
String providerKey = getString(PROP_INDEX_PROVIDER_NAME, props);
String providerVersion = getString(PROP_INDEX_PROVIDER_VERSION, props);
IndexProviderDescriptor providerDescriptor = new IndexProviderDescriptor(providerKey, providerVersion);
prototype = prototype.withIndexProvider(providerDescriptor);
IndexDescriptor index = prototype.materialise(schemaRuleId);
IndexConfig indexConfig = extractIndexConfig(props);
index = index.withIndexConfig(indexConfig);
if (props.containsKey(PROP_OWNING_CONSTRAINT)) {
index = index.withOwningConstraintId(getLong(PROP_OWNING_CONSTRAINT, props));
}
return index;
}
Aggregations