use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.
the class SchemaImpl method getIndexReference.
private static IndexDescriptor getIndexReference(SchemaRead schemaRead, TokenRead tokenRead, IndexDefinitionImpl index) throws SchemaRuleException {
// Use the precise embedded index reference when available.
IndexDescriptor reference = index.getIndexReference();
if (reference != null) {
return reference;
}
// Otherwise attempt to reverse engineer the schema that will let us look up the real IndexReference.
int[] propertyKeyIds = resolveAndValidatePropertyKeys(tokenRead, index.getPropertyKeysArrayShared());
SchemaDescriptor schema;
if (index.isNodeIndex()) {
int[] labelIds = resolveAndValidateTokens("Label", index.getLabelArrayShared(), Label::name, tokenRead::nodeLabel);
if (index.isMultiTokenIndex()) {
schema = fulltext(EntityType.NODE, labelIds, propertyKeyIds);
} else if (index.getIndexType() == IndexType.LOOKUP) {
schema = forAnyEntityTokens(EntityType.NODE);
} else {
schema = forLabel(labelIds[0], propertyKeyIds);
}
} else if (index.isRelationshipIndex()) {
int[] relTypes = resolveAndValidateTokens("Relationship type", index.getRelationshipTypesArrayShared(), RelationshipType::name, tokenRead::relationshipType);
if (index.isMultiTokenIndex()) {
schema = fulltext(EntityType.RELATIONSHIP, relTypes, propertyKeyIds);
} else if (index.getIndexType() == IndexType.LOOKUP) {
schema = forAnyEntityTokens(EntityType.RELATIONSHIP);
} else {
schema = forRelType(relTypes[0], propertyKeyIds);
}
} else {
throw new IllegalArgumentException("The given index is neither a node index, nor a relationship index: " + index + ".");
}
Iterator<IndexDescriptor> iterator = schemaRead.index(schema);
if (!iterator.hasNext()) {
throw new SchemaRuleNotFoundException(schema, tokenRead);
}
reference = iterator.next();
if (iterator.hasNext()) {
throw new DuplicateSchemaRuleException(schema, tokenRead);
}
return reference;
}
use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.
the class EntityUpdates method gatherUpdatesForPotentials.
@SuppressWarnings("ConstantConditions")
private <INDEX_KEY extends SchemaDescriptorSupplier> Iterable<IndexEntryUpdate<INDEX_KEY>> gatherUpdatesForPotentials(Iterable<INDEX_KEY> potentiallyRelevant) {
List<IndexEntryUpdate<INDEX_KEY>> indexUpdates = new ArrayList<>();
for (INDEX_KEY indexKey : potentiallyRelevant) {
SchemaDescriptor schema = indexKey.schema();
boolean relevantBefore = relevantBefore(schema);
boolean relevantAfter = relevantAfter(schema);
int[] propertyIds = schema.getPropertyIds();
if (relevantBefore && !relevantAfter) {
indexUpdates.add(IndexEntryUpdate.remove(entityId, indexKey, valuesBefore(propertyIds)));
} else if (!relevantBefore && relevantAfter) {
indexUpdates.add(IndexEntryUpdate.add(entityId, indexKey, valuesAfter(propertyIds)));
} else if (relevantBefore && relevantAfter) {
if (valuesChanged(propertyIds, schema.propertySchemaType())) {
indexUpdates.add(IndexEntryUpdate.change(entityId, indexKey, valuesBefore(propertyIds), valuesAfter(propertyIds)));
}
}
}
return indexUpdates;
}
use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.
the class IndexIT method shouldListCompositeIndexesInTheCoreAPI.
@Test
void shouldListCompositeIndexesInTheCoreAPI() throws Exception {
KernelTransaction transaction = newTransaction(AUTH_DISABLED);
long initialIndexCount = Iterators.count(transaction.schemaRead().indexesGetAll());
SchemaDescriptor schema = SchemaDescriptor.forLabel(labelId, propertyKeyId, propertyKeyId2);
transaction.schemaWrite().indexCreate(schema, "my index");
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("my index");
assertThrows(IllegalStateException.class, index::getRelationshipTypes);
assertThat(index.getLabels()).containsOnly(label(LABEL));
assertThat(index.getPropertyKeys()).containsOnly(PROPERTY_KEY, PROPERTY_KEY2);
assertFalse(index.isConstraintIndex(), "should not be a constraint index");
assertFalse(index.isMultiTokenIndex(), "should not be a multi-token index");
assertTrue(index.isCompositeIndex(), "should 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 IndexIT method shouldListCompositeMultiTokenRelationshipIndexesInTheCoreAPI.
@Test
void shouldListCompositeMultiTokenRelationshipIndexesInTheCoreAPI() throws Exception {
KernelTransaction transaction = newTransaction(AUTH_DISABLED);
long initialIndexCount = Iterators.count(transaction.schemaRead().indexesGetAll());
SchemaDescriptor schema = SchemaDescriptor.fulltext(EntityType.RELATIONSHIP, new int[] { relType, relType2 }, new int[] { propertyKeyId, propertyKeyId2 });
IndexPrototype prototype = IndexPrototype.forSchema(schema, FulltextIndexProviderFactory.DESCRIPTOR).withIndexType(IndexType.FULLTEXT).withName("index name");
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("index name");
assertThrows(IllegalStateException.class, index::getLabels);
assertThat(index.getRelationshipTypes()).containsOnly(withName(REL_TYPE), withName(REL_TYPE2));
assertThat(index.getPropertyKeys()).containsOnly(PROPERTY_KEY, PROPERTY_KEY2);
assertFalse(index.isConstraintIndex(), "should not be a constraint index");
assertTrue(index.isMultiTokenIndex(), "should be a multi-token index");
assertTrue(index.isCompositeIndex(), "should be a composite index");
assertFalse(index.isNodeIndex(), "should not be a node index");
assertTrue(index.isRelationshipIndex(), "should be a relationship index");
}
}
use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.
the class IndexIT method shouldListRelationshipIndexesInTheCoreAPI.
@Test
void shouldListRelationshipIndexesInTheCoreAPI() throws Exception {
KernelTransaction transaction = newTransaction(AUTH_DISABLED);
long initialIndexCount = Iterators.count(transaction.schemaRead().indexesGetAll());
SchemaDescriptor schema = SchemaDescriptor.forRelType(relType, propertyKeyId);
transaction.schemaWrite().indexCreate(schema, "my index");
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("my index");
assertThrows(IllegalStateException.class, index::getLabels);
assertThat(index.getRelationshipTypes()).containsOnly(withName(REL_TYPE));
assertThat(index.getPropertyKeys()).containsOnly(PROPERTY_KEY);
assertFalse(index.isConstraintIndex(), "should not be a constraint index");
assertFalse(index.isMultiTokenIndex(), "should not be a multi-token index");
assertFalse(index.isCompositeIndex(), "should not be a composite index");
assertFalse(index.isNodeIndex(), "should not be a node index");
assertTrue(index.isRelationshipIndex(), "should be a relationship index");
}
}
Aggregations