Search in sources :

Example 11 with SchemaDescriptor

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

the class SchemaMatcher method onMatchingSchema.

/**
 * Iterate over some schema suppliers, and invoke a callback for every supplier that matches the entity. To match the
 * entity E the supplier must supply a {@link SchemaDescriptor} D, such that E has values for all the properties of D.
 * The supplied schemas are all assumed to match E on token (label or relationship).
 * <p>
 * To avoid unnecessary store lookups, this implementation only gets propertyKeyIds for the entity if some
 * descriptor has a valid token.
 *
 * @param <SUPPLIER> the type to match. Must implement SchemaDescriptorSupplier
 * @param <EXCEPTION> The type of exception that can be thrown when taking the action
 * @param schemaSuppliers The suppliers to match
 * @param specialPropertyId This property id will always count as a match for the descriptor, regardless of
 * whether the entity has this property or not
 * @param callback The action to take on match
 * @throws EXCEPTION This exception is propagated from the action
 */
static <SUPPLIER extends SchemaDescriptorSupplier, EXCEPTION extends Exception> void onMatchingSchema(Iterator<SUPPLIER> schemaSuppliers, int specialPropertyId, int[] existingPropertyIds, ThrowingConsumer<SUPPLIER, EXCEPTION> callback) throws EXCEPTION {
    while (schemaSuppliers.hasNext()) {
        SUPPLIER schemaSupplier = schemaSuppliers.next();
        SchemaDescriptor schema = schemaSupplier.schema();
        if (entityHasSchemaProperties(existingPropertyIds, schema.getPropertyIds(), specialPropertyId)) {
            callback.accept(schemaSupplier);
        }
    }
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor)

Example 12 with SchemaDescriptor

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

the class SchemaComplianceChecker method checkCorrectlyIndexed.

<ENTITY extends PrimitiveRecord> void checkCorrectlyIndexed(ENTITY entity, long[] entityTokens, IntObjectMap<Value> values, Function<ENTITY, ConsistencyReport.PrimitiveConsistencyReport> reportSupplier) {
    for (IndexDescriptor indexRule : indexes) {
        SchemaDescriptor schema = indexRule.schema();
        Value[] valueArray = RecordLoading.entityIntersectionWithSchema(entityTokens, values, schema);
        if (valueArray == null) {
            continue;
        }
        var reader = indexReaders.reader(indexRule);
        if (indexRule.isUnique()) {
            verifyIndexedUniquely(entity, valueArray, indexRule, reader, reportSupplier);
        } else {
            long count = reader.countIndexedEntities(entity.getId(), cursorContext, schema.getPropertyIds(), valueArray);
            reportIncorrectIndexCount(entity, valueArray, indexRule, count, reportSupplier);
        }
    }
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) Value(org.neo4j.values.storable.Value) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Example 13 with SchemaDescriptor

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

the class UniquePropertyValueValidationException method getUserMessage.

@Override
public String getUserMessage(TokenNameLookup tokenNameLookup) {
    SchemaDescriptor schema = constraint.schema();
    StringBuilder message = new StringBuilder();
    for (Iterator<IndexEntryConflictException> iterator = conflicts.iterator(); iterator.hasNext(); ) {
        IndexEntryConflictException conflict = iterator.next();
        message.append(conflict.evidenceMessage(tokenNameLookup, schema));
        if (iterator.hasNext()) {
            message.append(System.lineSeparator());
        }
    }
    return message.toString();
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)

Example 14 with SchemaDescriptor

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

the class TxStateTest method addNodesToIndex.

private IndexUpdater addNodesToIndex(final IndexDescriptor descriptor) {
    return new IndexUpdater() {

        @Override
        public void withDefaultStringProperties(long... nodeIds) {
            Collection<Pair<Long, String>> entries = new ArrayList<>(nodeIds.length);
            for (long nodeId : nodeIds) {
                entries.add(of(nodeId, "value" + nodeId));
            }
            withProperties(entries);
        }

        private <T> void withProperties(Collection<Pair<Long, T>> nodesWithValues) {
            SchemaDescriptor schema = descriptor.schema();
            int[] labelIds = schema.getEntityTokenIds();
            int[] propertyKeyIds = schema.getPropertyIds();
            assertEquals(1, labelIds.length);
            assertEquals(1, propertyKeyIds.length);
            for (Pair<Long, T> entry : nodesWithValues) {
                long nodeId = entry.first();
                state.nodeDoCreate(nodeId);
                state.nodeDoAddLabel(labelIds[0], nodeId);
                Value valueAfter = Values.of(entry.other());
                state.nodeDoAddProperty(nodeId, propertyKeyIds[0], valueAfter);
                state.indexDoUpdateEntry(schema, nodeId, null, ValueTuple.of(valueAfter));
            }
        }
    };
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) ArrayList(java.util.ArrayList) Value(org.neo4j.values.storable.Value) Values.stringValue(org.neo4j.values.storable.Values.stringValue) Collection(java.util.Collection) Pair(org.neo4j.internal.helpers.collection.Pair)

Example 15 with SchemaDescriptor

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

the class BuiltInProceduresTest method setup.

@BeforeEach
void setup() throws Exception {
    procs.registerComponent(KernelTransaction.class, ctx -> ctx.internalTransaction().kernelTransaction(), false);
    procs.registerComponent(DependencyResolver.class, Context::dependencyResolver, false);
    procs.registerComponent(GraphDatabaseAPI.class, Context::graphDatabaseAPI, false);
    procs.registerComponent(Transaction.class, Context::internalTransaction, true);
    procs.registerComponent(SecurityContext.class, Context::securityContext, true);
    procs.registerComponent(ProcedureCallContext.class, Context::procedureCallContext, true);
    procs.registerComponent(SystemGraphComponents.class, ctx -> systemGraphComponents, false);
    procs.registerComponent(Log.class, ctx -> log, false);
    procs.registerType(Node.class, NTNode);
    procs.registerType(Relationship.class, NTRelationship);
    procs.registerType(Path.class, NTPath);
    new SpecialBuiltInProcedures("1.3.37", Edition.COMMUNITY.toString()).accept(procs);
    procs.registerProcedure(BuiltInProcedures.class);
    procs.registerProcedure(BuiltInDbmsProcedures.class);
    when(transaction.kernelTransaction()).thenReturn(tx);
    when(tx.tokenRead()).thenReturn(tokens);
    when(tx.dataRead()).thenReturn(read);
    when(tx.schemaRead()).thenReturn(schemaRead);
    when(tx.securityContext()).thenReturn(SecurityContext.AUTH_DISABLED);
    when(callContext.isCalledFromCypher()).thenReturn(false);
    when(schemaRead.snapshot()).thenReturn(schemaReadCore);
    when(tokens.propertyKeyGetAllTokens()).thenAnswer(asTokens(propKeys));
    when(tokens.labelsGetAllTokens()).thenAnswer(asTokens(labels));
    when(tokens.relationshipTypesGetAllTokens()).thenAnswer(asTokens(relTypes));
    when(schemaReadCore.indexesGetAll()).thenAnswer(i -> Iterators.concat(indexes.iterator(), uniqueIndexes.iterator()));
    when(schemaReadCore.index(any(SchemaDescriptor.class))).thenAnswer((Answer<IndexDescriptor>) invocationOnMock -> {
        SchemaDescriptor schema = invocationOnMock.getArgument(0);
        return getIndexReference(schema);
    });
    when(schemaReadCore.constraintsGetAll()).thenAnswer(i -> constraints.iterator());
    when(tokens.propertyKeyName(anyInt())).thenAnswer(invocation -> propKeys.get(invocation.getArgument(0)));
    when(tokens.nodeLabelName(anyInt())).thenAnswer(invocation -> labels.get(invocation.getArgument(0)));
    when(tokens.relationshipTypeName(anyInt())).thenAnswer(invocation -> relTypes.get(invocation.getArgument(0)));
    when(tokens.propertyKeyGetName(anyInt())).thenAnswer(invocation -> propKeys.get(invocation.getArgument(0)));
    when(tokens.labelGetName(anyInt())).thenAnswer(invocation -> labels.get(invocation.getArgument(0)));
    when(tokens.relationshipTypeGetName(anyInt())).thenAnswer(invocation -> relTypes.get(invocation.getArgument(0)));
    when(tokens.entityTokensGetNames(any(), any())).then(invocation -> {
        EntityType type = invocation.getArgument(0);
        int[] ids = invocation.getArgument(1);
        Map<Integer, String> mapping = type == EntityType.NODE ? labels : relTypes;
        return Arrays.stream(ids).mapToObj(mapping::get).toArray(String[]::new);
    });
    when(schemaReadCore.constraintsGetForRelationshipType(anyInt())).thenReturn(emptyIterator());
    when(schemaReadCore.indexesGetForLabel(anyInt())).thenReturn(emptyIterator());
    when(schemaReadCore.indexesGetForRelationshipType(anyInt())).thenReturn(emptyIterator());
    when(schemaReadCore.constraintsGetForLabel(anyInt())).thenReturn(emptyIterator());
    when(read.countsForNode(anyInt())).thenReturn(1L);
    when(read.countsForRelationship(anyInt(), anyInt(), anyInt())).thenReturn(1L);
    when(schemaReadCore.indexGetState(any(IndexDescriptor.class))).thenReturn(InternalIndexState.ONLINE);
}
Also used : SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) BasicContext.buildContext(org.neo4j.kernel.api.procedure.BasicContext.buildContext) Context(org.neo4j.kernel.api.procedure.Context) ProcedureCallContext(org.neo4j.internal.kernel.api.procs.ProcedureCallContext) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) SystemGraphComponent(org.neo4j.dbms.database.SystemGraphComponent) MapUtil(org.neo4j.internal.helpers.collection.MapUtil) Log(org.neo4j.logging.Log) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EMPTY(org.neo4j.kernel.api.index.IndexProvider.EMPTY) Config(org.neo4j.configuration.Config) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Value(org.neo4j.values.storable.Value) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) Statement(org.neo4j.kernel.api.Statement) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Collections.singletonList(java.util.Collections.singletonList) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) EMPTY_RESOURCE_TRACKER(org.neo4j.kernel.api.ResourceTracker.EMPTY_RESOURCE_TRACKER) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) Map(java.util.Map) SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) Transaction(org.neo4j.graphdb.Transaction) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) PopulationProgress(org.neo4j.internal.kernel.api.PopulationProgress) SettingValueParsers(org.neo4j.configuration.SettingValueParsers) TextValue(org.neo4j.values.storable.TextValue) Collections.emptyIterator(java.util.Collections.emptyIterator) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Test(org.junit.jupiter.api.Test) Path(org.neo4j.graphdb.Path) List(java.util.List) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) EntityType(org.neo4j.common.EntityType) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Mockito.mock(org.mockito.Mockito.mock) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AnyValue(org.neo4j.values.AnyValue) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) NodeExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor) SettingImpl(org.neo4j.configuration.SettingImpl) InternalIndexState(org.neo4j.internal.kernel.api.InternalIndexState) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) TokenRead(org.neo4j.internal.kernel.api.TokenRead) HashMap(java.util.HashMap) NTNode(org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTNode) Node(org.neo4j.graphdb.Node) ConstraintDescriptorFactory(org.neo4j.internal.schema.constraints.ConstraintDescriptorFactory) ArrayList(java.util.ArrayList) GlobalProcedures(org.neo4j.kernel.api.procedure.GlobalProcedures) Values(org.neo4j.values.storable.Values) DefaultValueMapper(org.neo4j.kernel.impl.util.DefaultValueMapper) Answer(org.mockito.stubbing.Answer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SchemaDescriptor.forLabel(org.neo4j.internal.schema.SchemaDescriptor.forLabel) SystemGraphComponents(org.neo4j.dbms.database.SystemGraphComponents) TestSystemGraphComponent(org.neo4j.dbms.database.TestSystemGraphComponent) DependencyResolver(org.neo4j.common.DependencyResolver) IndexConfig(org.neo4j.internal.schema.IndexConfig) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) IntSupplier(java.util.function.IntSupplier) ValueUtils(org.neo4j.kernel.impl.util.ValueUtils) Iterator(java.util.Iterator) Read(org.neo4j.internal.kernel.api.Read) Iterators(org.neo4j.internal.helpers.collection.Iterators) Edition(org.neo4j.common.Edition) Setting(org.neo4j.graphdb.config.Setting) BasicContext.buildContext(org.neo4j.kernel.api.procedure.BasicContext.buildContext) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) Mockito.when(org.mockito.Mockito.when) Context(org.neo4j.kernel.api.procedure.Context) NTPath(org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTPath) GlobalProceduresRegistry(org.neo4j.procedure.impl.GlobalProceduresRegistry) ProcedureCallContext(org.neo4j.internal.kernel.api.procs.ProcedureCallContext) Relationship(org.neo4j.graphdb.Relationship) NTRelationship(org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTRelationship) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NamedToken(org.neo4j.token.api.NamedToken) ProcedureSignature(org.neo4j.internal.kernel.api.procs.ProcedureSignature) EntityType(org.neo4j.common.EntityType) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) BeforeEach(org.junit.jupiter.api.BeforeEach)

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