Skip to content

Commit

Permalink
Improve Avro support in native applications
Browse files Browse the repository at this point in the history
  • Loading branch information
cescoffier authored and xumk committed Oct 14, 2020
1 parent b1e19a0 commit f4ddc1a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Collection;

import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.specific.AvroGenerated;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
Expand All @@ -24,14 +23,12 @@ public void build(CombinedIndexBuildItem indexBuildItem,
BuildProducer<NativeImageConfigBuildItem> conf) {

NativeImageConfigBuildItem.Builder builder = NativeImageConfigBuildItem.builder();
builder.addRuntimeInitializedClass(GenericDatumReader.class.getName());

Collection<AnnotationInstance> annotations = indexBuildItem.getIndex()
.getAnnotations(DotName.createSimple(AvroGenerated.class.getName()));
for (AnnotationInstance annotation : annotations) {
if (annotation.target().kind() == AnnotationTarget.Kind.CLASS) {
String className = annotation.target().asClass().name().toString();
builder.addRuntimeInitializedClass(className);
reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, true, className));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.function.Function;

import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.IndexedRecord;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.ResolvingDecoder;
import org.apache.avro.util.WeakIdentityHashMap;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.Inject;
Expand Down Expand Up @@ -79,5 +85,45 @@ public Target_org_apache_avro_reflect_ReflectData_ClassAccessorData(Class<?> c)

}

@TargetClass(className = "org.apache.avro.generic.GenericDatumReader")
final class Target_org_apache_avro_generic_GenericDatumReader {

@Alias
private GenericData data;
@Alias
private Schema actual;
@Alias
private Schema expected;
@Alias
private DatumReader<?> fastDatumReader;
@Alias
private ResolvingDecoder creatorResolver;
@Alias
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset)
private Thread creator;

@Alias
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.FromAlias)
private static ThreadLocal<Map<Schema, Map<Schema, ResolvingDecoder>>> RESOLVER_CACHE = ThreadLocal.withInitial(
WeakIdentityHashMap::new);
@Alias
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset)
private Map<Schema, Class> stringClassCache;
@Alias
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset)
private Map<Class, Constructor> stringCtorCache;

@Substitute
protected Target_org_apache_avro_generic_GenericDatumReader(GenericData data) {
this.fastDatumReader = null;
this.creatorResolver = null;
this.stringClassCache = new IdentityHashMap();
this.stringCtorCache = new HashMap();
this.data = data;
this.creator = Thread.currentThread();
}

}

class AvroSubstitutions {
}

0 comments on commit f4ddc1a

Please sign in to comment.