use of org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor in project neo4j by neo4j.
the class UniqueDatabaseIndexPopulatorTest method sampleIncludedUpdates.
@Test
public void sampleIncludedUpdates() throws Exception {
LabelSchemaDescriptor schemaDescriptor = SchemaDescriptorFactory.forLabel(1, 1);
populator = newPopulator();
List<IndexEntryUpdate> updates = Arrays.asList(IndexEntryUpdate.add(1, schemaDescriptor, "foo"), IndexEntryUpdate.add(2, schemaDescriptor, "bar"), IndexEntryUpdate.add(3, schemaDescriptor, "baz"), IndexEntryUpdate.add(4, schemaDescriptor, "qux"));
updates.forEach(populator::includeSample);
IndexSample sample = populator.sampleResult();
assertEquals(new IndexSample(4, 4, 4), sample);
}
use of org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor in project neo4j by neo4j.
the class PropertyExistenceEnforcer method validateNode.
private void validateNode(long nodeId) throws NodePropertyExistenceException {
if (labelExistenceConstraints.isEmpty()) {
return;
}
try (Cursor<NodeItem> node = nodeCursor(nodeId)) {
if (node.next()) {
PrimitiveIntSet labelIds = node.get().labels();
propertyKeyIds.clear();
try (Cursor<PropertyItem> properties = properties(node.get())) {
while (properties.next()) {
propertyKeyIds.add(properties.get().propertyKeyId());
}
}
for (LabelSchemaDescriptor descriptor : labelExistenceConstraints) {
if (labelIds.contains(descriptor.getLabelId())) {
for (int propertyId : descriptor.getPropertyIds()) {
validateNodeProperty(nodeId, propertyId, descriptor);
}
}
}
} else {
throw new IllegalStateException(format("Node %d with changes should exist.", nodeId));
}
}
}
use of org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor in project neo4j by neo4j.
the class UniquenessConstraintCreationIT method shouldAbortConstraintCreationWhenDuplicatesExist.
@Test
public void shouldAbortConstraintCreationWhenDuplicatesExist() throws Exception {
// given
Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
// name is not unique for Foo in the existing data
int foo = statement.tokenWriteOperations().labelGetOrCreateForName("Foo");
int name = statement.tokenWriteOperations().propertyKeyGetOrCreateForName("name");
long node1 = statement.dataWriteOperations().nodeCreate();
statement.dataWriteOperations().nodeAddLabel(node1, foo);
statement.dataWriteOperations().nodeSetProperty(node1, Property.stringProperty(name, "foo"));
long node2 = statement.dataWriteOperations().nodeCreate();
statement.dataWriteOperations().nodeAddLabel(node2, foo);
statement.dataWriteOperations().nodeSetProperty(node2, Property.stringProperty(name, "foo"));
commit();
// when
LabelSchemaDescriptor descriptor = SchemaDescriptorFactory.forLabel(foo, name);
try {
SchemaWriteOperations schemaWriteOperations = schemaWriteOperationsInNewTransaction();
schemaWriteOperations.uniquePropertyConstraintCreate(descriptor);
fail("expected exception");
}// then
catch (CreateConstraintFailureException ex) {
assertEquals(ConstraintDescriptorFactory.uniqueForSchema(descriptor), ex.constraint());
Throwable cause = ex.getCause();
assertThat(cause, instanceOf(ConstraintValidationException.class));
String expectedMessage = String.format("Both Node(%d) and Node(%d) have the label `Foo` and property `name` = 'foo'", node1, node2);
String actualMessage = userMessage((ConstraintValidationException) cause);
assertEquals(expectedMessage, actualMessage);
}
}
use of org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor in project neo4j by neo4j.
the class IndexUpdaterMap method close.
@Override
public void close() throws UnderlyingStorageException {
Set<Pair<NewIndexDescriptor, UnderlyingStorageException>> exceptions = null;
for (Map.Entry<LabelSchemaDescriptor, IndexUpdater> updaterEntry : updaterMap.entrySet()) {
IndexUpdater updater = updaterEntry.getValue();
try {
updater.close();
} catch (IOException | IndexEntryConflictException e) {
if (null == exceptions) {
exceptions = new HashSet<>();
}
exceptions.add(Pair.of(NewIndexDescriptorFactory.forSchema(updaterEntry.getKey()), new UnderlyingStorageException(e)));
}
}
clear();
if (null != exceptions) {
throw new MultipleUnderlyingStorageExceptions(exceptions);
}
}
use of org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor in project neo4j by neo4j.
the class SchemaRuleSerialization method readIndexRule.
// PRIVATE
// READ INDEX
private static IndexRule readIndexRule(long id, ByteBuffer source) throws MalformedSchemaRuleException {
SchemaIndexProvider.Descriptor indexProvider = readIndexProviderDescriptor(source);
LabelSchemaDescriptor schema;
byte indexRuleType = source.get();
String name;
switch(indexRuleType) {
case GENERAL_INDEX:
schema = readLabelSchema(source);
name = readRuleName(id, IndexRule.class, source);
return IndexRule.indexRule(id, NewIndexDescriptorFactory.forSchema(schema), indexProvider, name);
case UNIQUE_INDEX:
long owningConstraint = source.getLong();
schema = readLabelSchema(source);
NewIndexDescriptor descriptor = NewIndexDescriptorFactory.uniqueForSchema(schema);
name = readRuleName(id, IndexRule.class, source);
return IndexRule.constraintIndexRule(id, descriptor, indexProvider, owningConstraint == NO_OWNING_CONSTRAINT_YET ? null : owningConstraint, name);
default:
throw new MalformedSchemaRuleException(format("Got unknown index rule type '%d'.", indexRuleType));
}
}
Aggregations