Search in sources :

Example 36 with ProcedureException

use of org.neo4j.internal.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class IndexProcedures method createIndex.

private Stream<BuiltInProcedures.SchemaIndexInfo> createIndex(String name, List<String> labels, List<String> properties, IndexProviderDescriptor indexProviderDescriptor, Map<String, Object> configMap, String statusMessage, IndexCreator indexCreator) throws ProcedureException {
    IndexConfig indexConfig = IndexSettingUtil.toIndexConfigFromStringObjectMap(configMap);
    assertSingleLabel(labels);
    assertValidIndexProvider(indexProviderDescriptor);
    int labelId = getOrCreateLabelId(labels.get(0));
    int[] propertyKeyIds = getOrCreatePropertyIds(properties);
    try {
        SchemaWrite schemaWrite = ktx.schemaWrite();
        LabelSchemaDescriptor labelSchemaDescriptor = SchemaDescriptor.forLabel(labelId, propertyKeyIds);
        indexCreator.create(schemaWrite, name, labelSchemaDescriptor, indexProviderDescriptor, indexConfig);
        return Stream.of(new BuiltInProcedures.SchemaIndexInfo(name, labels, properties, indexProviderDescriptor.name(), statusMessage));
    } catch (KernelException e) {
        throw new ProcedureException(e.status(), e, e.getMessage());
    }
}
Also used : IndexConfig(org.neo4j.internal.schema.IndexConfig) SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) KernelException(org.neo4j.exceptions.KernelException)

Example 37 with ProcedureException

use of org.neo4j.internal.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class ProcedureRegistry method register.

/**
 * Register a new procedure.
 *
 * @param proc the procedure.
 */
public void register(CallableProcedure proc, boolean overrideCurrentImplementation) throws ProcedureException {
    ProcedureSignature signature = proc.signature();
    QualifiedName name = signature.name();
    String descriptiveName = signature.toString();
    validateSignature(descriptiveName, signature.inputSignature(), "input");
    validateSignature(descriptiveName, signature.outputSignature(), "output");
    if (!signature.isVoid() && signature.outputSignature().isEmpty()) {
        throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Procedures with zero output fields must be declared as VOID");
    }
    CallableProcedure oldImplementation = procedures.get(name);
    if (oldImplementation == null) {
        procedures.put(name, proc, signature.caseInsensitive());
    } else {
        if (overrideCurrentImplementation) {
            procedures.put(name, proc, signature.caseInsensitive());
        } else {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Unable to register procedure, because the name `%s` is already in use.", name);
        }
    }
}
Also used : ProcedureSignature(org.neo4j.internal.kernel.api.procs.ProcedureSignature) QualifiedName(org.neo4j.internal.kernel.api.procs.QualifiedName) CallableProcedure(org.neo4j.kernel.api.procedure.CallableProcedure) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException)

Example 38 with ProcedureException

use of org.neo4j.internal.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class FieldInjections method setters.

/**
 * For each annotated field in the provided class, creates a `FieldSetter`.
 * @param cls The class where injection should happen.
 * @return A list of `FieldSetters`
 * @throws ProcedureException if the type of the injected field does not match what has been registered.
 */
List<FieldSetter> setters(Class<?> cls) throws ProcedureException {
    List<FieldSetter> setters = new LinkedList<>();
    Class<?> currentClass = cls;
    do {
        for (Field field : currentClass.getDeclaredFields()) {
            // ignore synthetic fields
            if (field.isSynthetic()) {
                continue;
            }
            if (Modifier.isStatic(field.getModifiers())) {
                if (field.isAnnotationPresent(Context.class)) {
                    throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "The field `%s` in the class named `%s` is annotated as a @Context field,%n" + "but it is static. @Context fields must be public, non-final and non-static,%n" + "because they are reset each time a procedure is invoked.", field.getName(), cls.getSimpleName());
                }
                continue;
            }
            assertValidForInjection(cls, field);
            setters.add(createInjector(cls, field));
        }
    } while ((currentClass = currentClass.getSuperclass()) != null);
    return setters;
}
Also used : Field(java.lang.reflect.Field) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) LinkedList(java.util.LinkedList)

Example 39 with ProcedureException

use of org.neo4j.internal.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class BoltKeepAliveSchedulingIT method installSleepProcedure.

private static void installSleepProcedure(GraphDatabaseService db) throws ProcedureException {
    GraphDatabaseAPI dbApi = (GraphDatabaseAPI) db;
    dbApi.getDependencyResolver().resolveDependency(GlobalProcedures.class).register(new CallableProcedure.BasicProcedure(procedureSignature("boltissue", "sleep").out(ProcedureSignature.VOID).build()) {

        @Override
        public RawIterator<AnyValue[], ProcedureException> apply(Context context, AnyValue[] objects, ResourceTracker resourceTracker) throws ProcedureException {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new ProcedureException(Status.General.UnknownError, e, "Interrupted");
            }
            return RawIterator.empty();
        }
    });
}
Also used : Context(org.neo4j.kernel.api.procedure.Context) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) ResourceTracker(org.neo4j.kernel.api.ResourceTracker) AnyValue(org.neo4j.values.AnyValue) CallableProcedure(org.neo4j.kernel.api.procedure.CallableProcedure) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) RawIterator(org.neo4j.collection.RawIterator) GlobalProcedures(org.neo4j.kernel.api.procedure.GlobalProcedures)

Example 40 with ProcedureException

use of org.neo4j.internal.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class CommunityProcedureITBase method listProcedures.

@Test
void listProcedures() throws Throwable {
    // When
    ProcedureHandle procedures = procs().procedureGet(procedureName("dbms", "procedures"));
    RawIterator<AnyValue[], ProcedureException> stream = procs().procedureCallRead(procedures.id(), new AnyValue[0], ProcedureCallContext.EMPTY);
    // Then
    List<AnyValue[]> actual = asList(stream);
    List<Object[]> expected = getExpectedCommunityProcs();
    Map<String, AnyValue[]> resultMap = actual.stream().collect(toMap(row -> ((StringValue) row[0]).stringValue(), Function.identity()));
    Map<String, Object[]> expectedMap = expected.stream().collect(toMap(row -> ((StringValue) row[0]).stringValue(), Function.identity()));
    assertThat(resultMap.keySet(), containsInAnyOrder(expectedMap.keySet().toArray()));
    for (String procName : resultMap.keySet()) {
        AnyValue[] actualArray = resultMap.get(procName);
        Object[] expectedArray = expectedMap.get(procName);
        assertNotNull(expectedArray, "Got an unexpected entry for " + procName + " =>\n" + printElementsOfArray(actualArray));
        assertEquals(expectedArray.length, actualArray.length, "Count of columns for " + procName + " does not match");
        for (int i = 1; i < actualArray.length; i++) {
            Matcher matcher;
            if (expectedArray[i] instanceof TextArray) {
                // this has to be a list of roles, we ignore those in community and expect a null here
                matcher = equalTo(NO_VALUE);
            } else if (expectedArray[i] instanceof Matcher) {
                matcher = (Matcher) expectedArray[i];
            } else {
                matcher = equalTo(expectedArray[i]);
            }
            assertThat("Column " + i + " for " + procName + " does not match", actualArray[i], matcher);
        }
    }
    commit();
}
Also used : AnyValue(org.neo4j.values.AnyValue) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) NO_VALUE(org.neo4j.values.storable.Values.NO_VALUE) Arrays(java.util.Arrays) TextArray(org.neo4j.values.storable.TextArray) RawIterator(org.neo4j.collection.RawIterator) Iterators.asList(org.neo4j.internal.helpers.collection.Iterators.asList) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) ProcedureSignature.procedureName(org.neo4j.internal.kernel.api.procs.ProcedureSignature.procedureName) Function(java.util.function.Function) Collectors.joining(java.util.stream.Collectors.joining) Test(org.junit.jupiter.api.Test) List(java.util.List) ProcedureCallContext(org.neo4j.internal.kernel.api.procs.ProcedureCallContext) StringValue(org.neo4j.values.storable.StringValue) Collectors.toMap(java.util.stream.Collectors.toMap) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) ProcedureHandle(org.neo4j.internal.kernel.api.procs.ProcedureHandle) Matcher(org.hamcrest.Matcher) Map(java.util.Map) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Matcher(org.hamcrest.Matcher) ProcedureHandle(org.neo4j.internal.kernel.api.procs.ProcedureHandle) AnyValue(org.neo4j.values.AnyValue) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) StringValue(org.neo4j.values.storable.StringValue) TextArray(org.neo4j.values.storable.TextArray) Test(org.junit.jupiter.api.Test)

Aggregations

ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)124 Test (org.junit.jupiter.api.Test)95 CallableProcedure (org.neo4j.kernel.api.procedure.CallableProcedure)21 AnyValue (org.neo4j.values.AnyValue)19 QualifiedName (org.neo4j.internal.kernel.api.procs.QualifiedName)14 KernelException (org.neo4j.exceptions.KernelException)10 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)10 KernelIntegrationTest (org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)9 Arrays (java.util.Arrays)8 List (java.util.List)8 RawIterator (org.neo4j.collection.RawIterator)8 UserFunctionSignature (org.neo4j.internal.kernel.api.procs.UserFunctionSignature)8 CallableUserFunction (org.neo4j.kernel.api.procedure.CallableUserFunction)8 Collectors (java.util.stream.Collectors)7 ProcedureSignature (org.neo4j.internal.kernel.api.procs.ProcedureSignature)7 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)7 Method (java.lang.reflect.Method)6 ArrayList (java.util.ArrayList)6 FieldSignature (org.neo4j.internal.kernel.api.procs.FieldSignature)6 CallableUserAggregationFunction (org.neo4j.kernel.api.procedure.CallableUserAggregationFunction)6