Search in sources :

Example 1 with UniquenessConstraint

use of org.neo4j.kernel.api.constraints.UniquenessConstraint in project neo4j by neo4j.

the class BatchInserterImpl method createUniquenessConstraintRule.

private void createUniquenessConstraintRule(UniquenessConstraint constraint) {
    // TODO: Do not create duplicate index
    long indexRuleId = schemaStore.nextId();
    long constraintRuleId = schemaStore.nextId();
    int propertyKeyId = constraint.indexDescriptor().schema().getPropertyId();
    IndexRule indexRule = IndexRule.constraintIndexRule(indexRuleId, NewIndexDescriptorFactory.uniqueForLabel(constraint.label(), propertyKeyId), this.schemaIndexProviders.getDefaultProvider().getProviderDescriptor(), constraintRuleId);
    ConstraintRule constraintRule = ConstraintRule.constraintRule(constraintRuleId, ConstraintDescriptorFactory.uniqueForLabel(constraint.label(), propertyKeyId), indexRuleId);
    for (DynamicRecord record : schemaStore.allocateFrom(constraintRule)) {
        schemaStore.updateRecord(record);
    }
    schemaCache.addSchemaRule(constraintRule);
    for (DynamicRecord record : schemaStore.allocateFrom(indexRule)) {
        schemaStore.updateRecord(record);
    }
    schemaCache.addSchemaRule(indexRule);
    labelsTouched = true;
    flushStrategy.forceFlush();
}
Also used : DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) IndexRule(org.neo4j.kernel.impl.store.record.IndexRule) ConstraintRule(org.neo4j.kernel.impl.store.record.ConstraintRule) UniquenessConstraint(org.neo4j.kernel.api.constraints.UniquenessConstraint)

Example 2 with UniquenessConstraint

use of org.neo4j.kernel.api.constraints.UniquenessConstraint in project neo4j by neo4j.

the class GraphDbStructureGuide method showUniqueConstraints.

private void showUniqueConstraints(DbStructureVisitor visitor, ReadOperations read, TokenNameLookup nameLookup) {
    Iterator<ConstraintDescriptor> constraints = read.constraintsGetAll();
    while (constraints.hasNext()) {
        PropertyConstraint constraint = ConstraintBoundary.map(constraints.next());
        String userDescription = constraint.userDescription(nameLookup);
        if (constraint instanceof UniquenessConstraint) {
            visitor.visitUniqueConstraint((UniquenessConstraint) constraint, userDescription);
        } else if (constraint instanceof NodePropertyExistenceConstraint) {
            NodePropertyExistenceConstraint existenceConstraint = (NodePropertyExistenceConstraint) constraint;
            visitor.visitNodePropertyExistenceConstraint(existenceConstraint, userDescription);
        } else if (constraint instanceof RelationshipPropertyExistenceConstraint) {
            RelationshipPropertyExistenceConstraint existenceConstraint = (RelationshipPropertyExistenceConstraint) constraint;
            visitor.visitRelationshipPropertyExistenceConstraint(existenceConstraint, userDescription);
        } else {
            throw new IllegalArgumentException("Unknown constraint type: " + constraint.getClass() + ", " + "constraint: " + constraint);
        }
    }
}
Also used : PropertyConstraint(org.neo4j.kernel.api.constraints.PropertyConstraint) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor) RelationshipPropertyExistenceConstraint(org.neo4j.kernel.api.constraints.RelationshipPropertyExistenceConstraint) NodePropertyExistenceConstraint(org.neo4j.kernel.api.constraints.NodePropertyExistenceConstraint) UniquenessConstraint(org.neo4j.kernel.api.constraints.UniquenessConstraint)

Example 3 with UniquenessConstraint

use of org.neo4j.kernel.api.constraints.UniquenessConstraint in project neo4j by neo4j.

the class DbStructureCollectorTest method collectsDbStructure.

@Test
public void collectsDbStructure() {
    // GIVEN
    DbStructureCollector collector = new DbStructureCollector();
    collector.visitLabel(1, "Person");
    collector.visitLabel(2, "City");
    collector.visitPropertyKey(1, "name");
    collector.visitPropertyKey(2, "income");
    collector.visitRelationshipType(1, "LIVES_IN");
    collector.visitRelationshipType(2, "FRIEND");
    collector.visitUniqueIndex(NewIndexDescriptorFactory.forLabel(1, 1), ":Person(name)", 1.0d, 1L);
    collector.visitUniqueConstraint(new UniquenessConstraint(new NodePropertyDescriptor(2, 1)), ":Person(name)");
    collector.visitIndex(NewIndexDescriptorFactory.forLabel(2, 2), ":City(income)", 0.2d, 1L);
    collector.visitAllNodesCount(50);
    collector.visitNodeCount(1, "Person", 20);
    collector.visitNodeCount(2, "City", 30);
    collector.visitRelCount(1, 2, -1, "(:Person)-[:FRIEND]->()", 500);
    // WHEN
    DbStructureLookup lookup = collector.lookup();
    // THEN
    assertEquals(asList(of(1, "Person"), of(2, "City")), Iterators.asList(lookup.labels()));
    assertEquals(asList(of(1, "name"), of(2, "income")), Iterators.asList(lookup.properties()));
    assertEquals(asList(of(1, "LIVES_IN"), of(2, "FRIEND")), Iterators.asList(lookup.relationshipTypes()));
    assertEquals(asList(of("City", "name")), Iterators.asList(lookup.knownUniqueConstraints()));
    assertEquals(asList(of("Person", "name")), Iterators.asList(lookup.knownUniqueIndices()));
    assertEquals(asList(of("City", "income")), Iterators.asList(lookup.knownIndices()));
    assertEquals(50, lookup.nodesWithLabelCardinality(-1));
    assertEquals(20, lookup.nodesWithLabelCardinality(1));
    assertEquals(30, lookup.nodesWithLabelCardinality(2));
    assertEquals(500, lookup.cardinalityByLabelsAndRelationshipType(1, 2, -1));
    assertEquals(1.0d, lookup.indexSelectivity(1, 1), 0.01d);
    assertEquals(0.2d, lookup.indexSelectivity(2, 2), 0.01d);
}
Also used : NodePropertyDescriptor(org.neo4j.kernel.api.schema.NodePropertyDescriptor) UniquenessConstraint(org.neo4j.kernel.api.constraints.UniquenessConstraint) Test(org.junit.Test)

Example 4 with UniquenessConstraint

use of org.neo4j.kernel.api.constraints.UniquenessConstraint in project neo4j by neo4j.

the class DbStructureInvocationTracingAcceptanceTest method exerciseVisitor.

private void exerciseVisitor(Function<Object, DbStructureVisitor> visitor) {
    visitor.apply(null).visitLabel(0, "Person");
    visitor.apply(null).visitLabel(1, "Party");
    visitor.apply(null).visitPropertyKey(0, "name");
    visitor.apply(null).visitPropertyKey(1, "age");
    visitor.apply(null).visitRelationshipType(0, "ACCEPTS");
    visitor.apply(null).visitRelationshipType(1, "REJECTS");
    visitor.apply(null).visitIndex(NewIndexDescriptorFactory.forLabel(0, 1), ":Person(age)", 0.5d, 1L);
    visitor.apply(null).visitUniqueIndex(NewIndexDescriptorFactory.forLabel(0, 0), ":Person(name)", 0.5d, 1L);
    visitor.apply(null).visitUniqueConstraint(new UniquenessConstraint(new NodePropertyDescriptor(1, 0)), ":Party(name)");
    visitor.apply(null).visitAllNodesCount(55);
    visitor.apply(null).visitNodeCount(0, "Person", 50);
    visitor.apply(null).visitNodeCount(0, "Party", 5);
    visitor.apply(null).visitRelCount(0, 1, -1, "MATCH (:Person)-[:REJECTS]->() RETURN count(*)", 5);
}
Also used : NodePropertyDescriptor(org.neo4j.kernel.api.schema.NodePropertyDescriptor) UniquenessConstraint(org.neo4j.kernel.api.constraints.UniquenessConstraint)

Example 5 with UniquenessConstraint

use of org.neo4j.kernel.api.constraints.UniquenessConstraint in project neo4j by neo4j.

the class GraphDbStructureGuideTest method visitsUniqueConstraintsAndIndices.

@Test
public void visitsUniqueConstraintsAndIndices() throws Exception {
    DbStructureVisitor visitor = mock(DbStructureVisitor.class);
    int labelId = createLabel("Person");
    int pkId = createPropertyKey("name");
    commitAndReOpen();
    UniquenessConstraint constraint = createUniqueConstraint(labelId, pkId);
    NewIndexDescriptor descriptor = NewIndexDescriptorFactory.uniqueForLabel(labelId, pkId);
    // WHEN
    accept(visitor);
    // THEN
    verify(visitor).visitUniqueIndex(descriptor, ":Person(name)", 1.0d, 0L);
    verify(visitor).visitUniqueConstraint(constraint, "CONSTRAINT ON ( person:Person ) ASSERT person.name IS " + "UNIQUE");
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) UniquenessConstraint(org.neo4j.kernel.api.constraints.UniquenessConstraint) UniquenessConstraint(org.neo4j.kernel.api.constraints.UniquenessConstraint) Test(org.junit.Test)

Aggregations

UniquenessConstraint (org.neo4j.kernel.api.constraints.UniquenessConstraint)5 Test (org.junit.Test)2 NodePropertyDescriptor (org.neo4j.kernel.api.schema.NodePropertyDescriptor)2 NodePropertyExistenceConstraint (org.neo4j.kernel.api.constraints.NodePropertyExistenceConstraint)1 PropertyConstraint (org.neo4j.kernel.api.constraints.PropertyConstraint)1 RelationshipPropertyExistenceConstraint (org.neo4j.kernel.api.constraints.RelationshipPropertyExistenceConstraint)1 ConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)1 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)1 ConstraintRule (org.neo4j.kernel.impl.store.record.ConstraintRule)1 DynamicRecord (org.neo4j.kernel.impl.store.record.DynamicRecord)1 IndexRule (org.neo4j.kernel.impl.store.record.IndexRule)1