Skip to content

Commit

Permalink
Perf optimization and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
frankgrimes97 committed Jun 22, 2020
1 parent 494c81b commit b7f6ed3
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,31 @@ public GenericData.Record read(Map<String, Object> json, Schema schema) {
}
}

private GenericData.Record readRecord(Map<String, Object> json, Schema schema, Deque<String> path) {
final GenericRecordBuilder record = new GenericRecordBuilder(schema);
final Map<String,Field> fieldAliasMap = new HashMap<>(1);
private static Map<String, Field> getFieldAliasMap(final Schema schema) {
final Map<String, Field> fieldAliasMap = new HashMap<>(1);
for (Field field: schema.getFields()) {
for (String alias: field.aliases()) {
fieldAliasMap.put(alias, field);
}
}
return fieldAliasMap;
}

private GenericData.Record readRecord(Map<String, Object> json, Schema schema, Deque<String> path) {
final GenericRecordBuilder record = new GenericRecordBuilder(schema);
json.entrySet().forEach(entry -> {
final String entryKey = entry.getKey();
final Field field = schema.getField(entryKey);
if (field != null) {
record.set(field, read(field, field.schema(), entry.getValue(), path, false));
} else if (fieldAliasMap.containsKey(entryKey)) {
final Field aliasedField = fieldAliasMap.get(entryKey);
record.set(aliasedField, read(aliasedField, aliasedField.schema(), entry.getValue(), path, false));
} else if (unknownFieldListener != null) {
unknownFieldListener.onUnknownField(entry.getKey(), entry.getValue(), PathsPrinter.print(path, entry.getKey()));
} else {
final Map<String, Field> fieldAliasMap = getFieldAliasMap(schema);
if (fieldAliasMap.containsKey(entryKey)) {
final Field aliasedField = fieldAliasMap.get(entryKey);
record.set(aliasedField, read(aliasedField, aliasedField.schema(), entry.getValue(), path, false));
} else if (unknownFieldListener != null) {
unknownFieldListener.onUnknownField(entry.getKey(), entry.getValue(), PathsPrinter.print(path, entry.getKey()));
}
}
});
return record.build();
Expand Down

0 comments on commit b7f6ed3

Please sign in to comment.