-
Notifications
You must be signed in to change notification settings - Fork 1.7k
AVRO-3520: [java] expose read schema in custom encoding #3445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -364,20 +364,12 @@ protected ClassAccessorData computeValue(Class<?> c) { | |
|
|
||
| static class ClassAccessorData { | ||
| private final Class<?> clazz; | ||
| private final Map<String, FieldAccessor> byName = new HashMap<>(); | ||
| // getAccessorsFor replaces this map with each modification | ||
| private final Map<String, FieldAccessor> byName; | ||
| volatile Map<Schema, FieldAccessor[]> bySchema = new WeakHashMap<>(); | ||
|
|
||
| private ClassAccessorData(Class<?> c) { | ||
| clazz = c; | ||
| for (Field f : getFields(c, false)) { | ||
| if (f.isAnnotationPresent(AvroIgnore.class)) { | ||
| continue; | ||
| } | ||
| FieldAccessor accessor = ReflectionUtil.getFieldAccess().getAccessor(f); | ||
| AvroName avroname = f.getAnnotation(AvroName.class); | ||
| byName.put((avroname != null ? avroname.value() : f.getName()), accessor); | ||
| } | ||
| byName = buildByName(c, null); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -397,10 +389,12 @@ private FieldAccessor[] getAccessorsFor(Schema schema) { | |
| } | ||
|
|
||
| private FieldAccessor[] createAccessorsFor(Schema schema) { | ||
|
|
||
| var byNameSchema = buildByName(clazz, schema); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure how often
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is only called by |
||
| List<Schema.Field> avroFields = schema.getFields(); | ||
| FieldAccessor[] result = new FieldAccessor[avroFields.size()]; | ||
| for (Schema.Field avroField : schema.getFields()) { | ||
| result[avroField.pos()] = byName.get(avroField.name()); | ||
| result[avroField.pos()] = byNameSchema.get(avroField.name()); | ||
| } | ||
| return result; | ||
| } | ||
|
|
@@ -412,6 +406,25 @@ private FieldAccessor getAccessorFor(String fieldName) { | |
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static Map<String, FieldAccessor> buildByName(Class<?> c, Schema schema) { | ||
| Map<String, FieldAccessor> byName = new HashMap<>(); | ||
| for (Field f : getFields(c, false)) { | ||
| if (f.isAnnotationPresent(AvroIgnore.class)) { | ||
| continue; | ||
| } | ||
| AvroName avroname = f.getAnnotation(AvroName.class); | ||
| var name = (avroname != null ? avroname.value() : f.getName()); | ||
| Schema fieldSchema = null; | ||
| if (schema != null) { | ||
| var field = schema.getField(name); | ||
| fieldSchema = field != null ? field.schema() : null; | ||
| } | ||
| FieldAccessor accessor = ReflectionUtil.getFieldAccess().getAccessor(f, fieldSchema); | ||
| byName.put(name, accessor); | ||
| } | ||
| return byName; | ||
| } | ||
| } | ||
|
|
||
| private ClassAccessorData getClassAccessorData(Class<?> c) { | ||
|
|
@@ -1055,7 +1068,8 @@ private CustomEncodingWrapper populateEncoderCache(Schema schema) { | |
| var enc = ReflectionUtil.getAvroEncode(getClass(schema)); | ||
| if (enc != null) { | ||
| try { | ||
| return new CustomEncodingWrapper(enc.using().getDeclaredConstructor().newInstance()); | ||
| var customEncoding = enc.using().getDeclaredConstructor().newInstance(); | ||
| return new CustomEncodingWrapper(customEncoding.withSchema(schema)); | ||
| } catch (Exception e) { | ||
| throw new AvroRuntimeException("Could not instantiate custom Encoding"); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This API bothers me a bit.
Does it need to be thread-safe ? Because the overrides of this method could do something like
this.schema = schema.IMO this method should make sure somehow that it always returns a new instance of CustomEncoding that uses the provided schema.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, it is only called right after creation, so the libraries use is thread safe.
We could make it so that instead of a zero-argument constructor, it looks for a single-argument constructor and passes the schema in that way. It might be a little less discoverable as a feature, but safer.
They would need two constructors, as the zero-argument constructor will be needed to identify the schema
Alternativly perform an identity comparison after calling this method. Would mean default method would perform a reflection creation which I don't paticually like.