Search in sources :

Example 26 with SchemaDescriptor

use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.

the class SchemaStore method buildConstraintRule.

private static SchemaRule buildConstraintRule(long id, Map<String, Value> props) throws MalformedSchemaRuleException {
    SchemaDescriptor schema = buildSchemaDescriptor(props);
    String constraintRuleType = getString(PROP_CONSTRAINT_RULE_TYPE, props);
    String name = getString(PROP_SCHEMA_RULE_NAME, props);
    OptionalLong ownedIndex = getOptionalLong(PROP_OWNED_INDEX, props);
    ConstraintDescriptor constraint;
    switch(constraintRuleType) {
        case "UNIQUE":
            constraint = ConstraintDescriptorFactory.uniqueForSchema(schema);
            if (ownedIndex.isPresent()) {
                constraint = constraint.withOwnedIndexId(ownedIndex.getAsLong());
            }
            return constraint.withId(id).withName(name);
        case "EXISTS":
            constraint = ConstraintDescriptorFactory.existsForSchema(schema);
            return constraint.withId(id).withName(name);
        case "UNIQUE_EXISTS":
            constraint = ConstraintDescriptorFactory.nodeKeyForSchema(schema);
            if (ownedIndex.isPresent()) {
                constraint = constraint.withOwnedIndexId(ownedIndex.getAsLong());
            }
            return constraint.withId(id).withName(name);
        default:
            throw new MalformedSchemaRuleException("Did not recognize constraint rule type: " + constraintRuleType);
    }
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) MalformedSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) OptionalLong(java.util.OptionalLong)

Example 27 with SchemaDescriptor

use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.

the class DbStructureCollector method lookup.

public DbStructureLookup lookup() {
    return new DbStructureLookup() {

        @Override
        public Iterator<Pair<Integer, String>> labels() {
            return labels.iterator();
        }

        @Override
        public Iterator<Pair<Integer, String>> properties() {
            return propertyKeys.iterator();
        }

        @Override
        public Iterator<Pair<Integer, String>> relationshipTypes() {
            return relationshipTypes.iterator();
        }

        @Override
        public Iterator<Pair<String[], String[]>> knownIndices() {
            return regularIndices.iterator();
        }

        @Override
        public Iterator<Pair<String[], String[]>> knownUniqueIndices() {
            return uniqueIndices.iterator();
        }

        @Override
        public Iterator<Pair<String, String[]>> knownUniqueConstraints() {
            return idsToNames(uniquenessConstraints);
        }

        @Override
        public Iterator<Pair<String, String[]>> knownNodePropertyExistenceConstraints() {
            return idsToNames(nodePropertyExistenceConstraints);
        }

        @Override
        public Iterator<Pair<String, String[]>> knownNodeKeyConstraints() {
            return idsToNames(nodeKeyConstraints);
        }

        @Override
        public long nodesAllCardinality() {
            return allNodesCount;
        }

        @Override
        public Iterator<Pair<String, String[]>> knownRelationshipPropertyExistenceConstraints() {
            return Iterators.map(relConstraint -> {
                String label = labels.byIdOrFail(relConstraint.schema().getRelTypeId());
                String[] propertyKeyNames = propertyKeys.byIdOrFail(relConstraint.schema().getPropertyIds());
                return Pair.of(label, propertyKeyNames);
            }, relPropertyExistenceConstraints.iterator());
        }

        @Override
        public long nodesWithLabelCardinality(int labelId) {
            return nodeCounts.getIfAbsent(labelId, 0L);
        }

        @Override
        public long cardinalityByLabelsAndRelationshipType(int fromLabelId, int relTypeId, int toLabelId) {
            RelSpecifier specifier = new RelSpecifier(fromLabelId, relTypeId, toLabelId);
            Long result = relCounts.get(specifier);
            return result == null ? 0L : result;
        }

        @Override
        public double indexUniqueValueSelectivity(int labelId, int... propertyKeyIds) {
            SchemaDescriptor descriptor = SchemaDescriptor.forLabel(labelId, propertyKeyIds);
            IndexStatistics result1 = regularIndices.getIndex(descriptor);
            IndexStatistics result2 = result1 == null ? uniqueIndices.getIndex(descriptor) : result1;
            return result2 == null ? Double.NaN : result2.uniqueValuesPercentage;
        }

        @Override
        public double indexPropertyExistsSelectivity(int labelId, int... propertyKeyIds) {
            SchemaDescriptor descriptor = SchemaDescriptor.forLabel(labelId, propertyKeyIds);
            IndexStatistics result1 = regularIndices.getIndex(descriptor);
            IndexStatistics result2 = result1 == null ? uniqueIndices.getIndex(descriptor) : result1;
            double indexSize = result2 == null ? Double.NaN : result2.size;
            return indexSize / nodesWithLabelCardinality(labelId);
        }

        private Iterator<Pair<String, String[]>> idsToNames(Iterable<? extends SchemaDescriptorSupplier> nodeConstraints) {
            return Iterators.map(nodeConstraint -> {
                String label = labels.byIdOrFail(nodeConstraint.schema().getLabelId());
                String[] propertyKeyNames = propertyKeys.byIdOrFail(nodeConstraint.schema().getPropertyIds());
                return Pair.of(label, propertyKeyNames);
            }, nodeConstraints.iterator());
        }
    };
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) SchemaDescriptorSupplier(org.neo4j.internal.schema.SchemaDescriptorSupplier) Pair(org.neo4j.internal.helpers.collection.Pair)

Example 28 with SchemaDescriptor

use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.

the class IndexIT method shouldListMultiTokenIndexesInTheCoreAPI.

@Test
void shouldListMultiTokenIndexesInTheCoreAPI() throws Exception {
    KernelTransaction transaction = newTransaction(AUTH_DISABLED);
    long initialIndexCount = Iterators.count(transaction.schemaRead().indexesGetAll());
    SchemaDescriptor schema = SchemaDescriptor.fulltext(EntityType.NODE, new int[] { labelId, labelId2 }, new int[] { propertyKeyId });
    IndexPrototype prototype = IndexPrototype.forSchema(schema, FulltextIndexProviderFactory.DESCRIPTOR).withIndexType(IndexType.FULLTEXT).withName("multi token index");
    transaction.schemaWrite().indexCreate(prototype);
    commit();
    try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
        // then
        Set<IndexDefinition> indexes = Iterables.asSet(tx.schema().getIndexes());
        assertThat(indexes.size()).isEqualTo(initialIndexCount + 1);
        IndexDefinition index = tx.schema().getIndexByName("multi token index");
        assertThrows(IllegalStateException.class, index::getRelationshipTypes);
        assertThat(index.getLabels()).containsOnly(label(LABEL), label(LABEL2));
        assertThat(index.getPropertyKeys()).containsOnly(PROPERTY_KEY);
        assertFalse(index.isConstraintIndex(), "should not be a constraint index");
        assertTrue(index.isMultiTokenIndex(), "should be a multi-token index");
        assertFalse(index.isCompositeIndex(), "should not be a composite index");
        assertTrue(index.isNodeIndex(), "should be a node index");
        assertFalse(index.isRelationshipIndex(), "should not be a relationship index");
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) Test(org.junit.jupiter.api.Test) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)

Example 29 with SchemaDescriptor

use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.

the class IndexPopulationJobTest method indexPrototype.

private IndexPrototype indexPrototype(Label label, String propertyKey, boolean constraint) throws KernelException {
    try (KernelTransaction tx = kernel.beginTransaction(IMPLICIT, LoginContext.AUTH_DISABLED)) {
        int labelId = tx.tokenWrite().labelGetOrCreateForName(label.name());
        int propertyKeyId = tx.tokenWrite().propertyKeyGetOrCreateForName(propertyKey);
        SchemaDescriptor schema = SchemaDescriptor.forLabel(labelId, propertyKeyId);
        IndexPrototype descriptor = constraint ? IndexPrototype.uniqueForSchema(schema, PROVIDER_DESCRIPTOR) : IndexPrototype.forSchema(schema, PROVIDER_DESCRIPTOR);
        tx.commit();
        return descriptor;
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) IndexPrototype(org.neo4j.internal.schema.IndexPrototype)

Example 30 with SchemaDescriptor

use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.

the class NodeChecker method checkIndexVsNodes.

private void checkIndexVsNodes(LongRange range, IndexDescriptor descriptor, boolean lastRange) throws Exception {
    CacheAccess.Client client = context.cacheAccess.client();
    IndexAccessor accessor = context.indexAccessors.accessorFor(descriptor);
    RelationshipCounter.NodeLabelsLookup nodeLabelsLookup = observedCounts.nodeLabelsLookup();
    SchemaDescriptor schema = descriptor.schema();
    PropertySchemaType propertySchemaType = schema.propertySchemaType();
    long[] indexEntityTokenIds = toLongArray(schema.getEntityTokenIds());
    indexEntityTokenIds = sortAndDeduplicate(indexEntityTokenIds);
    try (var cursorContext = new CursorContext(context.pageCacheTracer.createPageCursorTracer(NODE_INDEXES_CHECKER_TAG));
        var allEntriesReader = accessor.newAllEntriesValueReader(range.from(), lastRange ? Long.MAX_VALUE : range.to(), cursorContext)) {
        for (long entityId : allEntriesReader) {
            try {
                boolean entityExists = client.getBooleanFromCache(entityId, CacheSlots.NodeLink.SLOT_IN_USE);
                if (!entityExists) {
                    reporter.forIndexEntry(new IndexEntry(descriptor, context.tokenNameLookup, entityId)).nodeNotInUse(recordLoader.node(entityId, cursorContext));
                } else {
                    long[] entityTokenIds = nodeLabelsLookup.nodeLabels(entityId);
                    compareTwoSortedLongArrays(propertySchemaType, entityTokenIds, indexEntityTokenIds, indexLabel -> reporter.forIndexEntry(new IndexEntry(descriptor, context.tokenNameLookup, entityId)).nodeDoesNotHaveExpectedLabel(recordLoader.node(entityId, cursorContext), indexLabel), storeLabel -> {
                    /*here we're only interested in what the the index has that the store doesn't have*/
                    });
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                // OK so apparently the index has a node way outside node highId
                reporter.forIndexEntry(new IndexEntry(descriptor, context.tokenNameLookup, entityId)).nodeNotInUse(recordLoader.node(entityId, cursorContext));
            }
        }
    }
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) IndexAccessor(org.neo4j.kernel.api.index.IndexAccessor) CacheAccess(org.neo4j.consistency.checking.cache.CacheAccess) IndexEntry(org.neo4j.consistency.store.synthetic.IndexEntry) CursorContext(org.neo4j.io.pagecache.context.CursorContext) PropertySchemaType(org.neo4j.internal.schema.PropertySchemaType) RelationshipCounter(org.neo4j.internal.recordstorage.RelationshipCounter)

Aggregations

SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)58 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)27 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)23 Test (org.junit.jupiter.api.Test)18 IndexPrototype (org.neo4j.internal.schema.IndexPrototype)18 RelationTypeSchemaDescriptor (org.neo4j.internal.schema.RelationTypeSchemaDescriptor)18 Value (org.neo4j.values.storable.Value)11 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)10 ArrayList (java.util.ArrayList)6 KernelTransactionImplementation (org.neo4j.kernel.impl.api.KernelTransactionImplementation)6 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)5 IndexConfig (org.neo4j.internal.schema.IndexConfig)5 MemoryTracker (org.neo4j.memory.MemoryTracker)5 KernelException (org.neo4j.exceptions.KernelException)4 UnspecifiedKernelException (org.neo4j.exceptions.UnspecifiedKernelException)4 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)4 IndexNotApplicableKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException)4 SchemaKernelException (org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException)4 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)4 IndexProviderDescriptor (org.neo4j.internal.schema.IndexProviderDescriptor)4