use of org.neo4j.codegen.FieldReference in project neo4j by neo4j.
the class ProcedureCompilation method compileAggregation.
/**
* Generates code for a user-defined aggregation function.
* <p>
* Given a user-defined aggregation function defined by
*
* <pre>
* class MyClass {
* {@literal @}Context
* public Log log;
*
* {@literal @}UserAggregationFunction
* public Adder create() {
* return new Adder;
* }
* }
*
* class Adder {
* private long sum;
* {@literal @}UserAggregationUpdate
* public void update(long in) {
* sum += in;
* }
* {@literal @}UserAggregationResult
* public long result() {
* return sum;
* }
* }
* }
* </pre>
* <p>
* we will generate two classe looking something like
*
* <pre>
* class Generated implements CallableUserAggregationFunction {
* public static UserFunctionSignature SIGNATURE;
* public static FieldSetter SETTER_0;
*
* public UserAggregator create(Context ctx) {
* try {
* MyClass userClass = new MyClass();
* userClass.log = (Log) SETTER_0.get(ctx);
* return new GeneratedAdder(userClass.create());
* } catch (Throwable T) {
* throw new ProcedureException([appropriate error msg], T);
* }
* }
*
* public UserFunctionSignature signature() {return SIGNATURE;}
* }
* class GeneratedAdder implements UserAggregator {
* private Adder adder;
*
* GeneratedAdder(Adder adder) {
* this.adder = adder;
* }
*
* void update(AnyValue[] in) {
* adder.update(((NumberValue) in).longValue());
* }
*
* AnyValue result() {
* return adder.result(Values.longValue(adder.result());
* }
* }
* </pre>
* <p>
* where the static fields are set once during loading via reflection.
*
* @param signature the signature of the user-defined function
* @param fieldSetters the fields to set before each call.
* @param create the method that creates the aggregator
* @param update the update method of the aggregator
* @param result the result method of the aggregator
* @return a CallableUserFunction delegating to the underlying user-defined function.
* @throws ProcedureException if something went wrong when compiling the user-defined function.
*/
static CallableUserAggregationFunction compileAggregation(UserFunctionSignature signature, List<FieldSetter> fieldSetters, Method create, Method update, Method result) throws ProcedureException {
ClassHandle handle;
try {
CodeGenerator codeGenerator = codeGenerator();
Class<?> aggregator = generateAggregator(codeGenerator, update, result, signature);
try (ClassGenerator generator = codeGenerator.generateClass(PACKAGE, className(signature), CallableUserAggregationFunction.class)) {
// static fields
FieldReference signatureField = generator.publicStaticField(typeReference(UserFunctionSignature.class), SIGNATURE_NAME);
List<FieldReference> fieldsToSet = createContextSetters(fieldSetters, generator);
// CallableUserAggregationFunction::create
try (CodeBlock method = generator.generate(AGGREGATION_CREATE)) {
method.tryCatch(body -> createAggregationBody(body, fieldSetters, fieldsToSet, create, aggregator), onError -> onError(onError, format("function `%s`", signature.name())), param(Throwable.class, "T"));
}
// CallableUserFunction::signature
try (CodeBlock method = generator.generateMethod(UserFunctionSignature.class, "signature")) {
method.returns(getStatic(signatureField));
}
handle = generator.handle();
}
Class<?> clazz = handle.loadClass();
// set all static fields
setAllStaticFields(signature, fieldSetters, create, clazz);
return (CallableUserAggregationFunction) clazz.getConstructor().newInstance();
} catch (Throwable e) {
throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, e, "Failed to compile function defined in `%s`: %s", create.getDeclaringClass().getSimpleName(), e.getMessage());
}
}
use of org.neo4j.codegen.FieldReference in project neo4j by neo4j.
the class ProcedureCompilation method generateAggregator.
private static Class<?> generateAggregator(CodeGenerator codeGenerator, Method update, Method result, UserFunctionSignature signature) {
assert update.getDeclaringClass().equals(result.getDeclaringClass());
Class<?> userAggregatorClass = update.getDeclaringClass();
ClassHandle handle;
try (ClassGenerator generator = codeGenerator.generateClass(PACKAGE, "Aggregator" + userAggregatorClass.getSimpleName() + System.nanoTime(), UserAggregator.class)) {
FieldReference aggregator = generator.field(userAggregatorClass, "aggregator");
FieldReference context = generator.field(Context.class, "ctx");
// constructor
try (CodeBlock constructor = generator.generateConstructor(param(userAggregatorClass, "aggregator"), param(Context.class, "ctx"))) {
constructor.expression(invokeSuper(OBJECT));
constructor.put(constructor.self(), aggregator, constructor.load("aggregator"));
constructor.put(constructor.self(), context, constructor.load("ctx"));
}
// update
try (CodeBlock block = generator.generate(AGGREGATION_UPDATE)) {
block.tryCatch(onSuccess -> onSuccess.expression(invoke(get(onSuccess.self(), aggregator), methodReference(update), parameters(onSuccess, update, get(onSuccess.self(), context)))), onError -> onError(onError, format("function `%s`", signature.name())), param(Throwable.class, "T"));
}
// result
try (CodeBlock block = generator.generate(AGGREGATION_RESULT)) {
block.tryCatch(onSuccess -> onSuccess.returns(toAnyValue(invoke(get(onSuccess.self(), aggregator), methodReference(result)), result.getReturnType(), get(onSuccess.self(), context))), onError -> onError(onError, format("function `%s`", signature.name())), param(Throwable.class, "T"));
}
handle = generator.handle();
}
try {
return handle.loadClass();
} catch (CompilationFailureException e) {
// We are being called from a lambda so it'll have to do with runtime exceptions here
throw new RuntimeException("Failed to generate iterator", e);
}
}
Aggregations