Search in sources :

Example 1 with ClassGenerator

use of com.facebook.presto.bytecode.ClassGenerator in project ignite-3 by apache.

the class AsmMarshallerGenerator method create.

/**
 * {@inheritDoc}
 */
@Override
public <K, V> KvMarshaller<K, V> create(SchemaDescriptor schema, Mapper<K> keyMapper, Mapper<V> valueMapper) {
    final String className = MARSHALLER_CLASS_NAME_PREFIX + schema.version();
    Class<K> keyClass = keyMapper.targetType();
    Class<V> valClass = valueMapper.targetType();
    final StringWriter writer = new StringWriter();
    try {
        // Generate Marshaller code.
        long generation = System.nanoTime();
        final ClassDefinition classDef = generateMarshallerClass(className, schema, keyClass, valClass);
        long compilationTime = System.nanoTime();
        generation = compilationTime - generation;
        final ClassGenerator generator = ClassGenerator.classGenerator(getClassLoader());
        if (dumpCode) {
            generator.outputTo(writer).fakeLineNumbers(true).runAsmVerifier(true).dumpRawBytecode(true);
        }
        final Class<? extends KvMarshaller> aClass = generator.defineClass(classDef, KvMarshaller.class);
        compilationTime = System.nanoTime() - compilationTime;
        if (LOG.isTraceEnabled()) {
            LOG.trace("ASM marshaller created: codeGenStage={}us, compileStage={}us. Code: {}", TimeUnit.NANOSECONDS.toMicros(generation), TimeUnit.NANOSECONDS.toMicros(compilationTime), writer);
        } else if (LOG.isDebugEnabled()) {
            LOG.debug("ASM marshaller created: codeGenStage={}us, compileStage={}us.", TimeUnit.NANOSECONDS.toMicros(generation), TimeUnit.NANOSECONDS.toMicros(compilationTime));
        }
        // noinspection unchecked
        return aClass.getDeclaredConstructor(SchemaDescriptor.class, ObjectFactory.class, ObjectFactory.class).newInstance(schema, MarshallerUtil.factoryForClass(keyClass), MarshallerUtil.factoryForClass(valClass));
    } catch (Exception | LinkageError e) {
        throw new IllegalArgumentException("Failed to create marshaller for key-value pair: schemaVer=" + schema.version() + ", keyClass=" + keyClass.getSimpleName() + ", valueClass=" + valClass.getSimpleName(), e);
    }
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) MarshallerException(org.apache.ignite.internal.schema.marshaller.MarshallerException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) ClassGenerator(com.facebook.presto.bytecode.ClassGenerator) StringWriter(java.io.StringWriter) ObjectFactory(org.apache.ignite.internal.util.ObjectFactory)

Aggregations

ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)1 ClassGenerator (com.facebook.presto.bytecode.ClassGenerator)1 StringWriter (java.io.StringWriter)1 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)1 MarshallerException (org.apache.ignite.internal.schema.marshaller.MarshallerException)1 ObjectFactory (org.apache.ignite.internal.util.ObjectFactory)1 IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)1