-
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 2 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 |
|---|---|---|
|
|
@@ -48,4 +48,16 @@ protected Schema getSchema() { | |
| return schema; | ||
| } | ||
|
|
||
| /** | ||
| * Receives the schema, giving the concrete encoder implementation an | ||
| * opportunity to detect schema changes and behave accordingly. Useful for | ||
| * maintaining backwards compatibility. | ||
| * | ||
| * @param schema the schema detected during read. | ||
| * @return custom encoding to be used. | ||
| */ | ||
| public CustomEncoding<T> withSchema(Schema 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. This API bothers me a bit.
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. Currently, it is only called right after creation, so the libraries use is thread safe. Alternativly perform an identity comparison after calling this method. Would mean default method would perform a reflection creation which I don't paticually like. |
||
| return this; | ||
| } | ||
|
|
||
| } | ||
| 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 customEncodingClass = enc.using().getDeclaredConstructor().newInstance(); | ||
|
||
| return new CustomEncodingWrapper(customEncodingClass.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.
#withSchema()is used also at https://github.com/apache/avro/pull/3445/files#diff-87216da701d0b2e3135497e3a9ffc4065a56d31142729d9867e0ca2e7dd2b2d1R1072 where the schema iswrite. The javadoc seems not correct by sayingreadThere 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.
updated comment