Search in sources :

Example 1 with CallableUserFunction

use of org.neo4j.kernel.api.proc.CallableUserFunction in project neo4j by neo4j.

the class ProcedureRegistry method register.

/**
     * Register a new function.
     *
     * @param function the function.
     */
public void register(CallableUserFunction function, boolean overrideCurrentImplementation) throws ProcedureException {
    UserFunctionSignature signature = function.signature();
    QualifiedName name = signature.name();
    CallableUserFunction oldImplementation = functions.get(name);
    if (oldImplementation == null) {
        functions.put(name, function);
    } else {
        if (overrideCurrentImplementation) {
            functions.put(name, function);
        } else {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Unable to register function, because the name `%s` is already in use.", name);
        }
    }
}
Also used : CallableUserFunction(org.neo4j.kernel.api.proc.CallableUserFunction) QualifiedName(org.neo4j.kernel.api.proc.QualifiedName) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) UserFunctionSignature(org.neo4j.kernel.api.proc.UserFunctionSignature)

Example 2 with CallableUserFunction

use of org.neo4j.kernel.api.proc.CallableUserFunction 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) throws ProcedureException {
    UserFunctionSignature signature = function.signature();
    QualifiedName name = signature.name();
    CallableUserFunction oldImplementation = functions.get(name);
    if (oldImplementation == null) {
        aggregationFunctions.put(name, function);
    } else {
        if (overrideCurrentImplementation) {
            aggregationFunctions.put(name, function);
        } else {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Unable to register aggregation function, because the name `%s` is already in use.", name);
        }
    }
}
Also used : CallableUserFunction(org.neo4j.kernel.api.proc.CallableUserFunction) QualifiedName(org.neo4j.kernel.api.proc.QualifiedName) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) UserFunctionSignature(org.neo4j.kernel.api.proc.UserFunctionSignature)

Example 3 with CallableUserFunction

use of org.neo4j.kernel.api.proc.CallableUserFunction in project neo4j by neo4j.

the class Procedures method start.

@Override
public void start() throws Throwable {
    ProcedureJarLoader loader = new ProcedureJarLoader(compiler, log);
    ProcedureJarLoader.Callables callables = loader.loadProceduresFromDir(pluginDir);
    for (CallableProcedure procedure : callables.procedures()) {
        register(procedure);
    }
    for (CallableUserFunction function : callables.functions()) {
        register(function);
    }
    for (CallableUserAggregationFunction function : callables.aggregationFunctions()) {
        register(function);
    }
    // And register built-in procedures
    builtin.accept(this);
}
Also used : CallableUserFunction(org.neo4j.kernel.api.proc.CallableUserFunction) CallableUserAggregationFunction(org.neo4j.kernel.api.proc.CallableUserAggregationFunction) CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure)

Example 4 with CallableUserFunction

use of org.neo4j.kernel.api.proc.CallableUserFunction in project neo4j by neo4j.

the class ReflectiveUserFunctionTest method shouldInjectLogging.

@Test
public void shouldInjectLogging() throws KernelException {
    // Given
    Log log = spy(Log.class);
    components.register(Log.class, (ctx) -> log);
    CallableUserFunction function = procedureCompiler.compileFunction(LoggingFunction.class).get(0);
    // When
    function.apply(new BasicContext(), new Object[0]);
    // Then
    verify(log).debug("1");
    verify(log).info("2");
    verify(log).warn("3");
    verify(log).error("4");
}
Also used : CallableUserFunction(org.neo4j.kernel.api.proc.CallableUserFunction) Log(org.neo4j.logging.Log) NullLog(org.neo4j.logging.NullLog) BasicContext(org.neo4j.kernel.api.proc.BasicContext) Test(org.junit.Test)

Example 5 with CallableUserFunction

use of org.neo4j.kernel.api.proc.CallableUserFunction in project neo4j by neo4j.

the class ReflectiveUserFunctionTest method shouldSupportFunctionDeprecation.

@Test
public void shouldSupportFunctionDeprecation() throws Throwable {
    // Given
    Log log = mock(Log.class);
    ReflectiveProcedureCompiler procedureCompiler = new ReflectiveProcedureCompiler(new TypeMappers(), components, new ComponentRegistry(), log, ProcedureConfig.DEFAULT);
    // When
    List<CallableUserFunction> funcs = procedureCompiler.compileFunction(FunctionWithDeprecation.class);
    // Then
    verify(log).warn("Use of @UserFunction(deprecatedBy) without @Deprecated in org.neo4j.kernel.impl.proc.badFunc");
    verifyNoMoreInteractions(log);
    for (CallableUserFunction func : funcs) {
        String name = func.signature().name().name();
        func.apply(new BasicContext(), new Object[0]);
        switch(name) {
            case "newFunc":
                assertFalse("Should not be deprecated", func.signature().deprecated().isPresent());
                break;
            case "oldFunc":
            case "badFunc":
                assertTrue("Should be deprecated", func.signature().deprecated().isPresent());
                assertThat(func.signature().deprecated().get(), equalTo("newFunc"));
                break;
            default:
                fail("Unexpected function: " + name);
        }
    }
}
Also used : CallableUserFunction(org.neo4j.kernel.api.proc.CallableUserFunction) Log(org.neo4j.logging.Log) NullLog(org.neo4j.logging.NullLog) BasicContext(org.neo4j.kernel.api.proc.BasicContext) Test(org.junit.Test)

Aggregations

CallableUserFunction (org.neo4j.kernel.api.proc.CallableUserFunction)17 Test (org.junit.Test)12 BasicContext (org.neo4j.kernel.api.proc.BasicContext)9 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)5 UserFunctionSignature (org.neo4j.kernel.api.proc.UserFunctionSignature)5 Log (org.neo4j.logging.Log)5 NullLog (org.neo4j.logging.NullLog)4 CallableProcedure (org.neo4j.kernel.api.proc.CallableProcedure)3 QualifiedName (org.neo4j.kernel.api.proc.QualifiedName)3 MethodHandle (java.lang.invoke.MethodHandle)2 ComponentInjectionException (org.neo4j.kernel.api.exceptions.ComponentInjectionException)2 CallableUserAggregationFunction (org.neo4j.kernel.api.proc.CallableUserAggregationFunction)2 FailedLoadFunction (org.neo4j.kernel.api.proc.FailedLoadFunction)2 FieldSignature (org.neo4j.kernel.api.proc.FieldSignature)2 ProcedureSignature (org.neo4j.kernel.api.proc.ProcedureSignature)2 UserFunction (org.neo4j.procedure.UserFunction)2 MethodHandles (java.lang.invoke.MethodHandles)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 URL (java.net.URL)1