Search in sources :

Example 11 with LabelSchemaDescriptor

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);
}
Also used : IndexEntryUpdate(org.neo4j.kernel.api.index.IndexEntryUpdate) IndexSample(org.neo4j.storageengine.api.schema.IndexSample) LabelSchemaDescriptor(org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor) Test(org.junit.Test)

Example 12 with LabelSchemaDescriptor

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));
        }
    }
}
Also used : NodeItem(org.neo4j.storageengine.api.NodeItem) PropertyItem(org.neo4j.storageengine.api.PropertyItem) PrimitiveIntSet(org.neo4j.collection.primitive.PrimitiveIntSet) LabelSchemaDescriptor(org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor)

Example 13 with LabelSchemaDescriptor

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);
    }
}
Also used : SchemaWriteOperations(org.neo4j.kernel.api.SchemaWriteOperations) Statement(org.neo4j.kernel.api.Statement) ConstraintValidationException(org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException) LabelSchemaDescriptor(org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor) CreateConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException) Test(org.junit.Test)

Example 14 with LabelSchemaDescriptor

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);
    }
}
Also used : LabelSchemaDescriptor(org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor) IOException(java.io.IOException) UnderlyingStorageException(org.neo4j.kernel.impl.store.UnderlyingStorageException) HashMap(java.util.HashMap) Map(java.util.Map) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) IndexUpdater(org.neo4j.kernel.api.index.IndexUpdater) Pair(org.neo4j.helpers.collection.Pair) HashSet(java.util.HashSet) MultipleUnderlyingStorageExceptions(org.neo4j.kernel.impl.store.MultipleUnderlyingStorageExceptions)

Example 15 with LabelSchemaDescriptor

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));
    }
}
Also used : SchemaIndexProvider(org.neo4j.kernel.api.index.SchemaIndexProvider) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) MalformedSchemaRuleException(org.neo4j.kernel.api.exceptions.schema.MalformedSchemaRuleException) LabelSchemaDescriptor(org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor)

Aggregations

LabelSchemaDescriptor (org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor)23 Test (org.junit.Test)12 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)5 Transaction (org.neo4j.graphdb.Transaction)4 Statement (org.neo4j.kernel.api.Statement)4 IndexEntryUpdate (org.neo4j.kernel.api.index.IndexEntryUpdate)4 Node (org.neo4j.graphdb.Node)3 Pair (org.neo4j.helpers.collection.Pair)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 PrimitiveIntSet (org.neo4j.collection.primitive.PrimitiveIntSet)2 ReadOperations (org.neo4j.kernel.api.ReadOperations)2 IndexEntryConflictException (org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)2 IndexPopulator (org.neo4j.kernel.api.index.IndexPopulator)2 IndexUpdater (org.neo4j.kernel.api.index.IndexUpdater)2 NodeItem (org.neo4j.storageengine.api.NodeItem)2 IOException (java.io.IOException)1 Map (java.util.Map)1 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)1 SchemaWriteOperations (org.neo4j.kernel.api.SchemaWriteOperations)1