Search in sources :

Example 6 with QualifiedName

use of org.neo4j.internal.kernel.api.procs.QualifiedName in project neo4j by neo4j.

the class ProcedureCompiler method compileFunction.

List<CallableUserFunction> compileFunction(Class<?> fcnDefinition, boolean isBuiltin) throws KernelException {
    try {
        List<Method> functionMethods = Arrays.stream(fcnDefinition.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(UserFunction.class)).collect(Collectors.toList());
        if (functionMethods.isEmpty()) {
            return emptyList();
        }
        // used for proper error handling
        assertValidConstructor(fcnDefinition);
        List<CallableUserFunction> out = new ArrayList<>(functionMethods.size());
        for (Method method : functionMethods) {
            String valueName = method.getAnnotation(UserFunction.class).value();
            String definedName = method.getAnnotation(UserFunction.class).name();
            QualifiedName funcName = extractName(fcnDefinition, method, valueName, definedName);
            if (isBuiltin || config.isWhitelisted(funcName.toString())) {
                out.add(compileFunction(fcnDefinition, method, funcName));
            } else {
                log.warn(String.format("The function '%s' is not on the allowlist and won't be loaded.", funcName.toString()));
            }
        }
        out.sort(Comparator.comparing(a -> a.signature().name().toString()));
        return out;
    } catch (KernelException e) {
        throw e;
    } catch (Exception e) {
        throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, e, "Failed to compile function defined in `%s`: %s", fcnDefinition.getSimpleName(), e.getMessage());
    }
}
Also used : Mode(org.neo4j.procedure.Mode) Arrays(java.util.Arrays) Log(org.neo4j.logging.Log) UserAggregationResult(org.neo4j.procedure.UserAggregationResult) CallableUserAggregationFunction(org.neo4j.kernel.api.procedure.CallableUserAggregationFunction) Status(org.neo4j.kernel.api.exceptions.Status) UserAggregationUpdate(org.neo4j.procedure.UserAggregationUpdate) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Constructor(java.lang.reflect.Constructor) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) UserFunctionSignature(org.neo4j.internal.kernel.api.procs.UserFunctionSignature) Procedure(org.neo4j.procedure.Procedure) Method(java.lang.reflect.Method) GraphDatabaseSettings.procedure_unrestricted(org.neo4j.configuration.GraphDatabaseSettings.procedure_unrestricted) ComponentInjectionException(org.neo4j.kernel.api.exceptions.ComponentInjectionException) Collections.emptyList(java.util.Collections.emptyList) CallableUserFunction(org.neo4j.kernel.api.procedure.CallableUserFunction) FailedLoadFunction(org.neo4j.kernel.api.procedure.FailedLoadFunction) Description(org.neo4j.procedure.Description) Collectors(java.util.stream.Collectors) FieldSignature(org.neo4j.internal.kernel.api.procs.FieldSignature) Internal(org.neo4j.procedure.Internal) QualifiedName(org.neo4j.internal.kernel.api.procs.QualifiedName) FailedLoadProcedure(org.neo4j.kernel.api.procedure.FailedLoadProcedure) UserFunction(org.neo4j.procedure.UserFunction) List(java.util.List) CallableProcedure(org.neo4j.kernel.api.procedure.CallableProcedure) KernelException(org.neo4j.exceptions.KernelException) Modifier.isPublic(java.lang.reflect.Modifier.isPublic) UserAggregationFunction(org.neo4j.procedure.UserAggregationFunction) FailedLoadAggregatedFunction(org.neo4j.kernel.api.procedure.FailedLoadAggregatedFunction) ProcedureSignature(org.neo4j.internal.kernel.api.procs.ProcedureSignature) Comparator(java.util.Comparator) Admin(org.neo4j.procedure.Admin) CallableUserFunction(org.neo4j.kernel.api.procedure.CallableUserFunction) CallableUserFunction(org.neo4j.kernel.api.procedure.CallableUserFunction) UserFunction(org.neo4j.procedure.UserFunction) QualifiedName(org.neo4j.internal.kernel.api.procs.QualifiedName) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) KernelException(org.neo4j.exceptions.KernelException) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) ComponentInjectionException(org.neo4j.kernel.api.exceptions.ComponentInjectionException) KernelException(org.neo4j.exceptions.KernelException)

Example 7 with QualifiedName

use of org.neo4j.internal.kernel.api.procs.QualifiedName in project neo4j by neo4j.

the class ProcedureCompiler method compileAggregationFunction.

List<CallableUserAggregationFunction> compileAggregationFunction(Class<?> fcnDefinition) throws KernelException {
    try {
        List<Method> methods = Arrays.stream(fcnDefinition.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(UserAggregationFunction.class)).collect(Collectors.toList());
        if (methods.isEmpty()) {
            return emptyList();
        }
        assertValidConstructor(fcnDefinition);
        List<CallableUserAggregationFunction> out = new ArrayList<>(methods.size());
        for (Method method : methods) {
            String valueName = method.getAnnotation(UserAggregationFunction.class).value();
            String definedName = method.getAnnotation(UserAggregationFunction.class).name();
            QualifiedName funcName = extractName(fcnDefinition, method, valueName, definedName);
            if (config.isWhitelisted(funcName.toString())) {
                out.add(compileAggregationFunction(fcnDefinition, method, funcName));
            } else {
                log.warn(String.format("The function '%s' is not on the allowlist and won't be loaded.", funcName.toString()));
            }
        }
        out.sort(Comparator.comparing(a -> a.signature().name().toString()));
        return out;
    } catch (KernelException e) {
        throw e;
    } catch (Exception e) {
        throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, e, "Failed to compile function defined in `%s`: %s", fcnDefinition.getSimpleName(), e.getMessage());
    }
}
Also used : Mode(org.neo4j.procedure.Mode) Arrays(java.util.Arrays) Log(org.neo4j.logging.Log) UserAggregationResult(org.neo4j.procedure.UserAggregationResult) CallableUserAggregationFunction(org.neo4j.kernel.api.procedure.CallableUserAggregationFunction) Status(org.neo4j.kernel.api.exceptions.Status) UserAggregationUpdate(org.neo4j.procedure.UserAggregationUpdate) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Constructor(java.lang.reflect.Constructor) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) UserFunctionSignature(org.neo4j.internal.kernel.api.procs.UserFunctionSignature) Procedure(org.neo4j.procedure.Procedure) Method(java.lang.reflect.Method) GraphDatabaseSettings.procedure_unrestricted(org.neo4j.configuration.GraphDatabaseSettings.procedure_unrestricted) ComponentInjectionException(org.neo4j.kernel.api.exceptions.ComponentInjectionException) Collections.emptyList(java.util.Collections.emptyList) CallableUserFunction(org.neo4j.kernel.api.procedure.CallableUserFunction) FailedLoadFunction(org.neo4j.kernel.api.procedure.FailedLoadFunction) Description(org.neo4j.procedure.Description) Collectors(java.util.stream.Collectors) FieldSignature(org.neo4j.internal.kernel.api.procs.FieldSignature) Internal(org.neo4j.procedure.Internal) QualifiedName(org.neo4j.internal.kernel.api.procs.QualifiedName) FailedLoadProcedure(org.neo4j.kernel.api.procedure.FailedLoadProcedure) UserFunction(org.neo4j.procedure.UserFunction) List(java.util.List) CallableProcedure(org.neo4j.kernel.api.procedure.CallableProcedure) KernelException(org.neo4j.exceptions.KernelException) Modifier.isPublic(java.lang.reflect.Modifier.isPublic) UserAggregationFunction(org.neo4j.procedure.UserAggregationFunction) FailedLoadAggregatedFunction(org.neo4j.kernel.api.procedure.FailedLoadAggregatedFunction) ProcedureSignature(org.neo4j.internal.kernel.api.procs.ProcedureSignature) Comparator(java.util.Comparator) Admin(org.neo4j.procedure.Admin) CallableUserAggregationFunction(org.neo4j.kernel.api.procedure.CallableUserAggregationFunction) UserAggregationFunction(org.neo4j.procedure.UserAggregationFunction) CallableUserAggregationFunction(org.neo4j.kernel.api.procedure.CallableUserAggregationFunction) QualifiedName(org.neo4j.internal.kernel.api.procs.QualifiedName) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) KernelException(org.neo4j.exceptions.KernelException) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) ComponentInjectionException(org.neo4j.kernel.api.exceptions.ComponentInjectionException) KernelException(org.neo4j.exceptions.KernelException)

Example 8 with QualifiedName

use of org.neo4j.internal.kernel.api.procs.QualifiedName in project neo4j by neo4j.

the class ProcedureHolder method name2Id.

private Integer name2Id(QualifiedName name) {
    Integer id = nameToId.get(name);
    if (id == null) {
        // Did not find it in the case sensitive lookup - let's check for case insensitive objects
        QualifiedName lowerCaseName = toLowerCaseName(name);
        id = caseInsensitiveName2Id.get(lowerCaseName);
    }
    return id;
}
Also used : QualifiedName(org.neo4j.internal.kernel.api.procs.QualifiedName)

Example 9 with QualifiedName

use of org.neo4j.internal.kernel.api.procs.QualifiedName in project neo4j by neo4j.

the class ProcedureRegistry method register.

/**
 * Register a new function.
 *
 * @param function the function.
 */
public void register(CallableUserAggregationFunction function, boolean overrideCurrentImplementation, boolean builtIn) throws ProcedureException {
    UserFunctionSignature signature = function.signature();
    QualifiedName name = signature.name();
    if (functions.get(name) != null) {
        throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Unable to register aggregation function, because the name `%s` is already in use as a function.", name);
    }
    CallableUserAggregationFunction oldImplementation = aggregationFunctions.get(name);
    if (oldImplementation == null) {
        aggregationFunctions.put(name, function, signature.caseInsensitive());
    } else {
        if (overrideCurrentImplementation) {
            aggregationFunctions.put(name, function, signature.caseInsensitive());
        } else {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Unable to register aggregation function, because the name `%s` is already in use.", name);
        }
    }
    if (builtIn) {
        builtInAggregatingFunctionIds.add(aggregationFunctions.idOf(name));
    }
}
Also used : CallableUserAggregationFunction(org.neo4j.kernel.api.procedure.CallableUserAggregationFunction) QualifiedName(org.neo4j.internal.kernel.api.procs.QualifiedName) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) UserFunctionSignature(org.neo4j.internal.kernel.api.procs.UserFunctionSignature)

Example 10 with QualifiedName

use of org.neo4j.internal.kernel.api.procs.QualifiedName 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)

Aggregations

QualifiedName (org.neo4j.internal.kernel.api.procs.QualifiedName)23 ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)14 Test (org.junit.jupiter.api.Test)13 Arrays (java.util.Arrays)7 List (java.util.List)7 Collectors (java.util.stream.Collectors)7 KernelException (org.neo4j.exceptions.KernelException)7 ProcedureSignature (org.neo4j.internal.kernel.api.procs.ProcedureSignature)6 Config (org.neo4j.configuration.Config)5 ArrayUtils.toArray (org.apache.commons.lang3.ArrayUtils.toArray)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 UserFunctionSignature (org.neo4j.internal.kernel.api.procs.UserFunctionSignature)4 CallableProcedure (org.neo4j.kernel.api.procedure.CallableProcedure)4 CallableUserAggregationFunction (org.neo4j.kernel.api.procedure.CallableUserAggregationFunction)4 KernelIntegrationTest (org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)4 Constructor (java.lang.reflect.Constructor)3 Method (java.lang.reflect.Method)3 Modifier.isPublic (java.lang.reflect.Modifier.isPublic)3 ArrayList (java.util.ArrayList)3 Collections.emptyList (java.util.Collections.emptyList)3