Search in sources :

Example 1 with AccumulatorStateMetadata

use of com.facebook.presto.spi.function.AccumulatorStateMetadata in project presto by prestodb.

the class StateCompiler method generateStateSerializer.

public static <T> AccumulatorStateSerializer<T> generateStateSerializer(Class<T> clazz, Map<String, Type> fieldTypes, DynamicClassLoader classLoader) {
    AccumulatorStateMetadata metadata = getMetadataAnnotation(clazz);
    if (metadata != null && metadata.stateSerializerClass() != void.class) {
        try {
            return (AccumulatorStateSerializer<T>) metadata.stateSerializerClass().getConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(clazz.getSimpleName() + "Serializer"), type(Object.class), type(AccumulatorStateSerializer.class));
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    // Generate constructor
    definition.declareDefaultConstructor(a(PUBLIC));
    List<StateField> fields = enumerateFields(clazz, fieldTypes);
    generateGetSerializedType(definition, fields, callSiteBinder);
    generateSerialize(definition, callSiteBinder, clazz, fields);
    generateDeserialize(definition, callSiteBinder, clazz, fields);
    Class<? extends AccumulatorStateSerializer> serializerClass = defineClass(definition, AccumulatorStateSerializer.class, callSiteBinder.getBindings(), classLoader);
    try {
        return (AccumulatorStateSerializer<T>) serializerClass.getConstructor().newInstance();
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}
Also used : AccumulatorStateMetadata(com.facebook.presto.spi.function.AccumulatorStateMetadata) AccumulatorStateSerializer(com.facebook.presto.spi.function.AccumulatorStateSerializer) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) InvocationTargetException(java.lang.reflect.InvocationTargetException) CallSiteBinder(com.facebook.presto.bytecode.CallSiteBinder)

Example 2 with AccumulatorStateMetadata

use of com.facebook.presto.spi.function.AccumulatorStateMetadata in project presto by prestodb.

the class StateCompiler method generateStateFactory.

public static <T> AccumulatorStateFactory<T> generateStateFactory(Class<T> clazz, Map<String, Type> fieldTypes, DynamicClassLoader classLoader) {
    AccumulatorStateMetadata metadata = getMetadataAnnotation(clazz);
    if (metadata != null && metadata.stateFactoryClass() != void.class) {
        try {
            return (AccumulatorStateFactory<T>) metadata.stateFactoryClass().getConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    Class<? extends T> singleStateClass = generateSingleStateClass(clazz, fieldTypes, classLoader);
    Class<? extends T> groupedStateClass = generateGroupedStateClass(clazz, fieldTypes, classLoader);
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(clazz.getSimpleName() + "Factory"), type(Object.class), type(AccumulatorStateFactory.class));
    // Generate constructor
    definition.declareDefaultConstructor(a(PUBLIC));
    // Generate single state creation method
    definition.declareMethod(a(PUBLIC), "createSingleState", type(Object.class)).getBody().newObject(singleStateClass).dup().invokeConstructor(singleStateClass).retObject();
    // Generate grouped state creation method
    definition.declareMethod(a(PUBLIC), "createGroupedState", type(Object.class)).getBody().newObject(groupedStateClass).dup().invokeConstructor(groupedStateClass).retObject();
    // Generate getters for state class
    definition.declareMethod(a(PUBLIC), "getSingleStateClass", type(Class.class, singleStateClass)).getBody().push(singleStateClass).retObject();
    definition.declareMethod(a(PUBLIC), "getGroupedStateClass", type(Class.class, groupedStateClass)).getBody().push(groupedStateClass).retObject();
    Class<? extends AccumulatorStateFactory> factoryClass = defineClass(definition, AccumulatorStateFactory.class, classLoader);
    try {
        return (AccumulatorStateFactory<T>) factoryClass.getConstructor().newInstance();
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}
Also used : AccumulatorStateMetadata(com.facebook.presto.spi.function.AccumulatorStateMetadata) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) InvocationTargetException(java.lang.reflect.InvocationTargetException) AccumulatorStateFactory(com.facebook.presto.spi.function.AccumulatorStateFactory)

Aggregations

ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)2 AccumulatorStateMetadata (com.facebook.presto.spi.function.AccumulatorStateMetadata)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 CallSiteBinder (com.facebook.presto.bytecode.CallSiteBinder)1 AccumulatorStateFactory (com.facebook.presto.spi.function.AccumulatorStateFactory)1 AccumulatorStateSerializer (com.facebook.presto.spi.function.AccumulatorStateSerializer)1