Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2019 Expedia, Inc.
*
* Licensed 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
*
* http://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 com.expediagroup.rhapsody.kafka.avro.serde;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.function.Supplier;
import org.apache.avro.Schema;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.reflect.ReflectData;
import org.apache.avro.reflect.ReflectDatumReader;
import org.apache.avro.specific.SpecificData;
import com.expediagroup.rhapsody.util.ConfigLoading;
public class ReflectDecoderAvroDeserializer extends LoadingAvroDeserializer {
public static final String REFLECT_ALLOW_NULL_PROPERTY = "reflect.allow.null";
private boolean reflectAllowNull = false;
@Override
public void configure(Map configs, boolean isKey) {
super.configure(configs, isKey);
this.reflectAllowNull = ConfigLoading.load(configs, REFLECT_ALLOW_NULL_PROPERTY, Boolean::valueOf, reflectAllowNull);
}
@Override
protected Schema loadTypeSchema(Type dataType) {
return getReflectData().getSchema(dataType);
}
@Override
protected T deserializeNonNullWithSchemas(Schema writerSchema, Schema readerSchema, ByteBuffer dataBuffer) throws IOException {
return createDatumReader(writerSchema, readerSchema, getReflectData())
.read(null, DecoderFactory.get().binaryDecoder(dataBuffer.array(), dataBuffer.arrayOffset() + dataBuffer.position(), dataBuffer.remaining(), null));
}
protected DatumReader createDatumReader(Schema writerSchema, Schema readerSchema, ReflectData reflectData) {
return new ReflectDecoderDatumReader<>(writerSchema, readerSchema, reflectData);
}
private ReflectData getReflectData() {
return reflectAllowNull ? ReflectData.AllowNull.get() : ReflectData.get();
}
/**
* At one point, Avro did not know how to handle the deserialization of certain abstract types,
* like Sets and SortedSets. This DatumReader addresses types we wanted to support at ToW.
* Clients that need to support other types should be able to follow a similar pattern
*/
protected static class ReflectDecoderDatumReader extends ReflectDatumReader {
private static final Map> CREATORS_BY_CLASS_NAME = createCreatorsByClassName();
public ReflectDecoderDatumReader(Schema writer, Schema reader, ReflectData data) {
super(writer, reader, data);
}
@Override
protected Object newArray(Object old, int size, Schema schema) {
Supplier> creator = CREATORS_BY_CLASS_NAME.get(schema.getProp(SpecificData.CLASS_PROP));
return old == null && creator != null ? creator.get() : super.newArray(old, size, schema);
}
private static Map> createCreatorsByClassName() {
Map> creatorsByClassName = new HashMap<>();
creatorsByClassName.put(Set.class.getName(), HashSet::new);
creatorsByClassName.put(SortedSet.class.getName(), TreeSet::new);
return creatorsByClassName;
}
}
}