-
Notifications
You must be signed in to change notification settings - Fork 1.7k
AVRO-3126: Add support for java records #1680
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
Open
ashley-taylor
wants to merge
10
commits into
apache:main
Choose a base branch
from
ashley-taylor:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bee070b
AVRO-3126 Add support for java records
ashley-taylor 9fe3df9
Merge remote-tracking branch 'avro/main'
ashley-taylor 361a025
fixing bugs from merge
ashley-taylor 6d28a4b
working on getting tests to pass
ashley-taylor dd83eb2
unused variable from code scanning
ashley-taylor 389f134
adding some more tests to confirm matches class behavior
ashley-taylor bb94a9f
Merge remote-tracking branch 'avro/main'
ashley-taylor 0844654
cleanup tests
ashley-taylor 1896b01
missed license in refactor
ashley-taylor d0c45d9
Merge remote-tracking branch 'avro/main'
ashley-taylor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
271 changes: 271 additions & 0 deletions
271
lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectRecordEncoding.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,271 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.avro.reflect; | ||
|
|
||
| import java.io.IOException; | ||
| import java.lang.reflect.Array; | ||
| import java.lang.reflect.Constructor; | ||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.Modifier; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.apache.avro.AvroMissingFieldException; | ||
| import org.apache.avro.AvroRuntimeException; | ||
| import org.apache.avro.Schema; | ||
| import org.apache.avro.io.Decoder; | ||
| import org.apache.avro.io.Encoder; | ||
| import org.apache.avro.io.ResolvingDecoder; | ||
|
|
||
| public class ReflectRecordEncoding extends CustomEncoding<Object> { | ||
|
|
||
| private final Class<?> type; | ||
| private final List<FieldWriter> writer; | ||
| private final Constructor<?> constructor; | ||
| private List<FieldReader> reader; | ||
|
|
||
| public ReflectRecordEncoding(Class<?> type) { | ||
| this.type = type; | ||
| this.writer = null; | ||
| this.constructor = null; | ||
|
|
||
| } | ||
|
|
||
| public ReflectRecordEncoding(Class<?> type, Schema schema) { | ||
| this.type = type; | ||
| this.schema = schema; | ||
| this.writer = schema.getFields().stream().map(field -> { | ||
| try { | ||
| Field classField = type.getDeclaredField(field.name()); | ||
| classField.setAccessible(true); | ||
| AvroEncode enc = classField.getAnnotation(AvroEncode.class); | ||
| if (enc != null) | ||
| return new CustomEncodedFieldWriter(classField, enc.using().getDeclaredConstructor().newInstance()); | ||
| return new ReflectFieldWriter(classField, field.schema()); | ||
| } catch (ReflectiveOperationException e) { | ||
| throw new AvroMissingFieldException("Field does not exist", field); | ||
| } | ||
| }).collect(Collectors.toList()); | ||
|
|
||
| // order of this matches default constructor find order mapping | ||
|
|
||
| Field[] fields = type.getDeclaredFields(); | ||
|
|
||
| List<Class<?>> parameterTypes = new ArrayList<>(fields.length); | ||
|
|
||
| Map<String, Integer> offsets = new HashMap<>(); | ||
|
|
||
| // need to know offset for mapping | ||
| for (Field field : fields) { | ||
| if (Modifier.isStatic(field.getModifiers())) { | ||
| continue; | ||
| } | ||
| offsets.put(field.getName(), parameterTypes.size()); | ||
| parameterTypes.add(field.getType()); | ||
| } | ||
|
|
||
| try { | ||
| this.constructor = type.getDeclaredConstructor(parameterTypes.toArray(new Class[0])); | ||
| } catch (NoSuchMethodException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
|
|
||
| var reader = schema.getFields().stream().map(field -> { | ||
| int offset = offsets.remove(field.name()); | ||
|
|
||
| try { | ||
| Field classField = type.getDeclaredField(field.name()); | ||
| AvroEncode enc = classField.getAnnotation(AvroEncode.class); | ||
| if (enc != null) | ||
| return new CustomEncodedFieldReader(offset, enc.using().getDeclaredConstructor().newInstance(), | ||
| field.schema()); | ||
| return new ReflectFieldReader(offset, field.schema()); | ||
| } catch (ReflectiveOperationException e) { | ||
| throw new AvroRuntimeException("Could not instantiate custom Encoding"); | ||
| } | ||
| }).collect(Collectors.toList()); | ||
|
|
||
| // got a new field, set to default value | ||
| if (!offsets.isEmpty()) { | ||
| var defaultReader = new ArrayList<>(reader); | ||
|
|
||
| offsets.forEach((field, offset) -> { | ||
| try { | ||
|
|
||
| var fieldType = type.getDeclaredField(field).getType(); | ||
| if (fieldType.isPrimitive()) { | ||
| var defaultValue = Array.get(Array.newInstance(fieldType, 1), 0); | ||
| defaultReader.add(new DefaultReader(offset, defaultValue)); | ||
|
|
||
| } else { | ||
| defaultReader.add(new DefaultReader(offset, null)); | ||
| } | ||
| } catch (ReflectiveOperationException e) { | ||
| throw new AvroRuntimeException(e); | ||
| } | ||
| }); | ||
|
|
||
| reader = defaultReader; | ||
|
|
||
| } | ||
| this.reader = reader; | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public CustomEncoding<Object> setSchema(Schema schema) { | ||
| return new ReflectRecordEncoding(type, schema); | ||
| } | ||
|
|
||
| @Override | ||
| protected void write(Object datum, Encoder out) throws IOException { | ||
| throw new UnsupportedOperationException("No writer specified"); | ||
| } | ||
|
|
||
| @Override | ||
| protected void write(Object datum, Encoder out, ReflectDatumWriter writer) throws IOException { | ||
| for (FieldWriter field : this.writer) { | ||
| field.write(datum, out, writer); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected Object read(Object reuse, Decoder in) throws IOException { | ||
| throw new UnsupportedOperationException("No reader specified"); | ||
| } | ||
|
|
||
| @Override | ||
| protected Object read(Object reuse, ResolvingDecoder in, ReflectDatumReader reader) throws IOException { | ||
|
|
||
| Object[] args = new Object[this.reader.size()]; | ||
|
|
||
| for (FieldReader field : this.reader) { | ||
| field.read(in, reader, args); | ||
| } | ||
|
|
||
| try { | ||
| return this.constructor.newInstance(args); | ||
| } catch (ReflectiveOperationException e) { | ||
| throw new RuntimeException(); | ||
| } | ||
| } | ||
|
|
||
| private interface FieldWriter { | ||
| void write(Object datum, Encoder out, ReflectDatumWriter writer) throws IOException; | ||
| } | ||
|
|
||
| private static class ReflectFieldWriter implements FieldWriter { | ||
|
|
||
| private final Field field; | ||
| private final Schema schema; | ||
|
|
||
| public ReflectFieldWriter(Field field, Schema schema) { | ||
| this.field = field; | ||
| this.schema = schema; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(Object datum, Encoder out, ReflectDatumWriter writer) throws IOException { | ||
| try { | ||
| Object obj = field.get(datum); | ||
| writer.write(schema, obj, out); | ||
| } catch (ReflectiveOperationException e) { | ||
| throw new AvroRuntimeException("Could not invoke", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static class CustomEncodedFieldWriter implements FieldWriter { | ||
|
|
||
| private final Field field; | ||
| private final CustomEncoding<?> encoding; | ||
|
|
||
| public CustomEncodedFieldWriter(Field field, CustomEncoding<?> encoding) { | ||
| this.field = field; | ||
| this.encoding = encoding; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(Object datum, Encoder out, ReflectDatumWriter writer) throws IOException { | ||
| try { | ||
| Object obj = field.get(datum); | ||
| encoding.write(obj, out); | ||
| } catch (ReflectiveOperationException e) { | ||
| throw new AvroRuntimeException("Could not invoke", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private interface FieldReader { | ||
| public void read(ResolvingDecoder in, ReflectDatumReader reader, Object[] constructorArgs) throws IOException; | ||
| } | ||
|
|
||
| private static class ReflectFieldReader implements FieldReader { | ||
|
|
||
| private final int constructorOffset; | ||
| private final Schema schema; | ||
|
|
||
| public ReflectFieldReader(int constructorOffset, Schema schema) { | ||
| this.constructorOffset = constructorOffset; | ||
| this.schema = schema; | ||
| } | ||
|
|
||
| @Override | ||
| public void read(ResolvingDecoder in, ReflectDatumReader reader, Object[] constructorArgs) throws IOException { | ||
| Object obj = reader.read(null, schema, in); | ||
| constructorArgs[constructorOffset] = obj; | ||
| } | ||
| } | ||
|
|
||
| private static class DefaultReader implements FieldReader { | ||
|
|
||
| private final int constructorOffset; | ||
| private final Object defaultValue; | ||
|
|
||
| public DefaultReader(int constructorOffset, Object defaultValue) { | ||
| this.constructorOffset = constructorOffset; | ||
| this.defaultValue = defaultValue; | ||
| } | ||
|
|
||
| @Override | ||
| public void read(ResolvingDecoder in, ReflectDatumReader reader, Object[] constructorArgs) throws IOException { | ||
| constructorArgs[constructorOffset] = defaultValue; | ||
| } | ||
| } | ||
|
|
||
| private static class CustomEncodedFieldReader implements FieldReader { | ||
|
|
||
| private final int constructorOffset; | ||
| private final CustomEncoding<?> encoding; | ||
|
|
||
| public CustomEncodedFieldReader(int constructorOffset, CustomEncoding<?> encoding, Schema schema) { | ||
| this.constructorOffset = constructorOffset; | ||
| this.encoding = encoding; | ||
| encoding.setSchema(schema); | ||
| } | ||
|
|
||
| @Override | ||
| public void read(ResolvingDecoder in, ReflectDatumReader reader, Object[] constructorArgs) throws IOException { | ||
| Object obj = encoding.read(in); | ||
| constructorArgs[constructorOffset] = obj; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.