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);
}
}
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());
}
};
}
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");
}
}
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;
}
}
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));
}
}
}
}
Aggregations