All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adgear.anoa.read.ThriftDecoders Maven / Gradle / Ivy

Go to download

Core classes for Anoa library, which aims to be a safe, convenient and fast record de/serialization wrapper for the Avro, Thrift and Jackson libraries, using the functional idioms of Java 8. The anoa-core module tries to keep upstream dependencies to a minimum.

There is a newer version: 3.1.2
Show newest version
package com.adgear.anoa.read;

import com.adgear.anoa.Anoa;
import com.adgear.anoa.AnoaHandler;
import com.fasterxml.jackson.core.JsonParser;

import org.apache.thrift.TBase;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TJSONProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TMemoryInputTransport;
import org.apache.thrift.transport.TTransport;

import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Utility class for generating functions for deserializing Thrift records. Unless specified
 * otherwise, the functions should not be deemed thread-safe.
 */
public class ThriftDecoders {

  protected ThriftDecoders() {
  }

  /** 
   * @param supplier provides the returned Thrift record instances
   * @param  Thrift record type 
   * @return A function which deserializes a Thrift record from its compact binary encoding
   */
  static public  /*@NonNull*/ Function compact(
      /*@NonNull*/ Supplier supplier) {
    return fn(supplier, TCompactProtocol::new);
  }

  /**
   * @param anoaHandler {@code AnoaHandler} instance to use for exception handling
   * @param supplier provides the returned Thrift record instances
   * @param  Thrift record type
   * @param  Metadata type 
   * @return A function which deserializes a Thrift record from its compact binary encoding 
   */
  static public 
  /*@NonNull*/ Function, Anoa> compact(
      /*@NonNull*/ AnoaHandler anoaHandler,
      /*@NonNull*/ Supplier supplier) {
    return fn(anoaHandler, supplier, TCompactProtocol::new);
  }

  /**
   * @param supplier provides the returned Thrift record instances
   * @param  Thrift record type
   * @return A function which deserializes a Thrift record from its standard binary encoding
   */
  static public  /*@NonNull*/ Function binary(
      /*@NonNull*/ Supplier supplier) {
    return fn(supplier, TBinaryProtocol::new);
  }

  /**
   * @param anoaHandler {@code AnoaHandler} instance to use for exception handling
   * @param supplier provides the returned Thrift record instances
   * @param  Thrift record type
   * @param  Metadata type
   * @return A function which deserializes a Thrift record from its standard binary encoding
   */
  static public 
  /*@NonNull*/ Function, Anoa> binary(
      /*@NonNull*/ AnoaHandler anoaHandler,
      /*@NonNull*/ Supplier supplier) {
    return fn(anoaHandler, supplier, TBinaryProtocol::new);
  }

  /**
   * @param supplier provides the returned Thrift record instances
   * @param  Thrift record type
   * @return A function which deserializes a Thrift record from its Thrift JSON encoding
   */
  static public  /*@NonNull*/ Function json(
      /*@NonNull*/ Supplier supplier) {
    return fn(supplier, TJSONProtocol::new);
  }

  /**
   * @param anoaHandler {@code AnoaHandler} instance to use for exception handling
   * @param supplier provides the returned Thrift record instances
   * @param  Thrift record type
   * @param  Metadata type
   * @return A function which deserializes a Thrift record from its Thrift JSON encoding
   */
  static public 
  /*@NonNull*/ Function, Anoa> json(
      /*@NonNull*/ AnoaHandler anoaHandler,
      /*@NonNull*/ Supplier supplier) {
    return fn(anoaHandler, supplier, TJSONProtocol::new);
  }

  static  /*@NonNull*/ Function fn(
      /*@NonNull*/ Supplier supplier,
      /*@NonNull*/ Function protocolFactory) {
    final TMemoryInputTransport tTransport = new TMemoryInputTransport();
    final TProtocol tProtocol = protocolFactory.apply(tTransport);
    final LookAheadIterator lookAheadIterator =
        LookAheadIteratorFactory.thrift(tProtocol, supplier);
    return (byte[] bytes) -> {
      lookAheadIterator.reset(null);
      tTransport.reset(bytes);
      return lookAheadIterator.next();
    };
  }

  static  /*@NonNull*/ Function, Anoa> fn(
      /*@NonNull*/ AnoaHandler anoaHandler,
      /*@NonNull*/ Supplier supplier,
      /*@NonNull*/ Function protocolFactory) {
    final TMemoryInputTransport tTransport = new TMemoryInputTransport();
    final TProtocol tProtocol = protocolFactory.apply(tTransport);
    final LookAheadIterator> lookAheadIterator =
        LookAheadIteratorFactory.thrift(anoaHandler, tProtocol, supplier);
    return (Anoa bytesWrapped) -> bytesWrapped.flatMap(bytes -> {
      lookAheadIterator.reset(null);
      tTransport.reset(bytesWrapped.get());
      return lookAheadIterator.next();
    });
  }

  /** 
   * @param recordClass Thrift record class object
   * @param strict enable strict type checking
   * @param 

Jackson JsonParser type * @param Thrift record type * @return A function which reads a Thrift record from a JsonParser, in its 'natural' encoding. */ static public

/*@NonNull*/ Function jackson( /*@NonNull*/ Class recordClass, boolean strict) { final AbstractReader reader = new ThriftReader<>(recordClass); return (P jp) -> reader.read(jp, strict); } /** * @param anoaHandler {@code AnoaHandler} instance to use for exception handling * @param recordClass Thrift record class object * @param strict enable strict type checking * @param

Jackson JsonParser type * @param Thrift record type * @param Metadata type * @return A function which reads a Thrift record from a JsonParser, in its 'natural' encoding. */ static public

/*@NonNull*/ Function, Anoa> jackson( /*@NonNull*/ AnoaHandler anoaHandler, /*@NonNull*/ Class recordClass, boolean strict) { final AbstractReader reader = new ThriftReader<>(recordClass); return anoaHandler.functionChecked((P jp) -> (T) reader.readChecked(jp, strict)); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy