com.adgear.anoa.read.AvroDecoders Maven / Gradle / Ivy
Show all versions of anoa-core Show documentation
package com.adgear.anoa.read;
import com.adgear.anoa.Anoa;
import com.adgear.anoa.AnoaHandler;
import com.fasterxml.jackson.core.JsonParser;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.generic.IndexedRecord;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.JsonDecoder;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificRecord;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Utility class for generating functions for deserializing Avro records. Unless specified
* otherwise, the functions should not be deemed thread-safe.
*/
public class AvroDecoders {
protected AvroDecoders() {
}
static protected Supplier ofNullable(Supplier supplier) {
return (supplier != null) ? supplier : () -> ((R) null);
}
/**
* @param schema Avro record schema
* @param supplier when not null, used for record instantiation
* @return A function which deserializes an Avro record from its binary encoding
*/
static public /*@NonNull*/ Function binary(
/*@NonNull*/ Schema schema,
/*@Nullable*/ Supplier supplier) {
return binary(new GenericDatumReader<>(schema), ofNullable(supplier));
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param schema Avro record schema
* @param supplier when not null, used for record instantiation
* @param Metadata type
* @return A function which deserializes an Avro record from its binary encoding
*/
static public /*@NonNull*/ Function, Anoa> binary(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Schema schema,
/*@Nullable*/ Supplier supplier) {
return binary(anoaHandler, new GenericDatumReader<>(schema), ofNullable(supplier));
}
/**
* @param writer Avro schema with which the record was originally serialized
* @param reader Avro schema to use for deserialization
* @param supplier when not null, used for record instantiation
* @return A function which deserializes an Avro record from its binary encoding
*/
static public /*@NonNull*/ Function binary(
/*@NonNull*/ Schema writer,
/*@NonNull*/ Schema reader,
/*@Nullable*/ Supplier supplier) {
return binary(new GenericDatumReader<>(writer, reader), ofNullable(supplier));
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param writer Avro schema with which the record was originally serialized
* @param reader Avro schema to use for deserialization
* @param supplier when not null, used for record instantiation
* @param Metadata type
* @return A function which deserializes an Avro record from its binary encoding
*/
static public /*@NonNull*/ Function, Anoa> binary(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Schema writer,
/*@NonNull*/ Schema reader,
/*@Nullable*/ Supplier supplier) {
return binary(anoaHandler, new GenericDatumReader<>(writer, reader), ofNullable(supplier));
}
/**
* @param recordClass Avro SpecificRecord class object
* @param supplier when not null, used for record instantiation
* @param Avro SpecificRecord record type
* @return A function which deserializes an Avro record from its binary encoding
*/
static public /*@NonNull*/ Function binary(
/*@NonNull*/ Class recordClass,
/*@Nullable*/ Supplier supplier) {
return binary(new SpecificDatumReader<>(recordClass), ofNullable(supplier));
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Avro SpecificRecord class object
* @param supplier when not null, used for record instantiation
* @param Avro SpecificRecord record type
* @param Metadata type
* @return A function which deserializes an Avro record from its binary encoding
*/
static public /*@NonNull*/ Function, Anoa> binary(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Class recordClass,
/*@Nullable*/ Supplier supplier) {
return binary(anoaHandler, new SpecificDatumReader<>(recordClass), ofNullable(supplier));
}
static protected class BinaryDecoderWrapper {
protected BinaryDecoder decoder = null;
protected BinaryDecoder getDecoder(byte[] bytes) {
decoder = DecoderFactory.get().binaryDecoder(bytes, decoder);
return decoder;
}
}
static /*@NonNull*/ Function binary(
/*@NonNull*/ GenericDatumReader reader,
/*@NonNull*/ Supplier supplier) {
BinaryDecoderWrapper decoderWrapper = new BinaryDecoderWrapper();
return (byte[] in) -> {
try {
return reader.read(supplier.get(), decoderWrapper.getDecoder(in));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
}
static /*@NonNull*/ Function, Anoa> binary(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ GenericDatumReader reader,
/*@NonNull*/ Supplier supplier) {
BinaryDecoderWrapper decoderWrapper = new BinaryDecoderWrapper();
return anoaHandler.functionChecked(
(byte[] in) -> reader.read(supplier.get(), decoderWrapper.getDecoder(in)));
}
/**
* @param schema Avro record schema
* @param supplier when not null, used for record instantiation
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public /*@NonNull*/ Function json(
/*@NonNull*/ Schema schema,
/*@Nullable*/ Supplier supplier) {
return json(new GenericDatumReader<>(schema), ofNullable(supplier));
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param schema Avro record schema
* @param supplier when not null, used for record instantiation
* @param Metadata type
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public /*@NonNull*/ Function, Anoa> json(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Schema schema,
/*@Nullable*/ Supplier supplier) {
return json(anoaHandler, new GenericDatumReader<>(schema), ofNullable(supplier));
}
/**
* @param writer Avro schema with which the record was originally serialized
* @param reader Avro schema to use for deserialization
* @param supplier when not null, used for record instantiation
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public /*@NonNull*/ Function json(
/*@NonNull*/ Schema writer,
/*@NonNull*/ Schema reader,
/*@Nullable*/ Supplier supplier) {
return json(new GenericDatumReader<>(writer, reader), ofNullable(supplier));
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param writer Avro schema with which the record was originally serialized
* @param reader Avro schema to use for deserialization
* @param supplier when not null, used for record instantiation
* @param Metadata type
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public /*@NonNull*/ Function, Anoa> json(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Schema writer,
/*@NonNull*/ Schema reader,
/*@Nullable*/ Supplier supplier) {
return json(anoaHandler, new GenericDatumReader<>(writer, reader), ofNullable(supplier));
}
/**
* @param recordClass Avro SpecificRecord class object
* @param supplier when not null, used for record instantiation
* @param Avro SpecificRecord record type
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public /*@NonNull*/ Function json(
/*@NonNull*/ Class recordClass,
/*@Nullable*/ Supplier supplier) {
return json(new SpecificDatumReader<>(recordClass), ofNullable(supplier));
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Avro SpecificRecord class object
* @param supplier when not null, used for record instantiation
* @param Avro SpecificRecord record type
* @param Metadata type
* @return A function which deserializes an Avro record from its JSON encoding
*/
static public /*@NonNull*/ Function, Anoa> json(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Class recordClass,
/*@Nullable*/ Supplier supplier) {
return json(anoaHandler, new SpecificDatumReader<>(recordClass), ofNullable(supplier));
}
static protected JsonDecoder createJsonDecoder(Schema schema) {
try {
return DecoderFactory.get().jsonDecoder(schema, "");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
static /*@NonNull*/ Function json(
/*@NonNull*/ GenericDatumReader reader,
/*@NonNull*/ Supplier supplier) {
final JsonDecoder jsonDecoder = createJsonDecoder(reader.getSchema());
return (String in) -> {
try {
jsonDecoder.configure(in);
return reader.read(supplier.get(), jsonDecoder);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
}
static /*@NonNull*/ Function, Anoa> json(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ GenericDatumReader reader,
/*@NonNull*/ Supplier supplier) {
final JsonDecoder jsonDecoder = createJsonDecoder(reader.getSchema());
return anoaHandler.functionChecked((String in) -> {
jsonDecoder.configure(in);
return reader.read(supplier.get(), jsonDecoder);
});
}
/**
* @param schema Avro record schema
* @param strict enable strict type checking
* @param Jackson JsonParser type
* @return A function which reads an Avro record from a JsonParser, in its 'natural' encoding
*/
static public
/*@NonNull*/ Function
jackson(
/*@NonNull*/ Schema schema,
boolean strict) {
final AvroReader reader = new AvroReader.GenericReader(schema);
return (P jp) -> reader.read(jp, strict);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param schema Avro record Schema
* @param strict enable strict type checking
* @param Jackson JsonParser type
* @param Metadata type
* @return A function which reads an Avro record from a JsonParser, in its 'natural' encoding
*/
static public
/*@NonNull*/ Function, Anoa> jackson(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Schema schema,
boolean strict) {
final AvroReader reader = new AvroReader.GenericReader(schema);
return anoaHandler.functionChecked((P jp) -> reader.readChecked(jp, strict));
}
/**
* @param recordClass Avro SpecificRecord class object
* @param strict enable strict type checking
* @param Jackson JsonParser type
* @param Avro SpecificRecord record type
* @return A function which reads an Avro record from a JsonParser, in its 'natural' encoding
*/
static public /*@NonNull*/ Function
jackson(
/*@NonNull*/ Class recordClass,
boolean strict) {
final AvroReader reader = new AvroReader.SpecificReader<>(recordClass);
return (P jp) -> reader.read(jp, strict);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Avro SpecificRecord class object
* @param strict enable strict type checking
* @param Jackson JsonParser type
* @param Avro SpecificRecord record type
* @param Metadata type
* @return A function which reads an Avro record from a JsonParser, in its 'natural' encoding
*/
static public
/*@NonNull*/ Function, Anoa> jackson(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Class recordClass,
boolean strict) {
final AvroReader reader = new AvroReader.SpecificReader<>(recordClass);
return anoaHandler.functionChecked((P jp) -> reader.readChecked(jp, strict));
}
}