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

com.twitter.heron.streamlet.impl.KryoSerializer Maven / Gradle / Ivy

There is a newer version: 0.17.8
Show newest version
//  Copyright 2017 Twitter. All rights reserved.
//
//  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.twitter.heron.streamlet.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.CollectionSerializer;
import com.esotericsoftware.kryo.serializers.DefaultSerializers;
import com.esotericsoftware.kryo.serializers.MapSerializer;

import com.twitter.heron.api.serializer.IPluggableSerializer;

/**
 * KryoSerializer is a wrapper around Heron's IPluggableSerializer.
 * Streamlet based topologies turning on kryo serialization are based off of it.
 */
public class KryoSerializer implements IPluggableSerializer {
  private Kryo kryo;
  private Output kryoOut;
  private Input kryoIn;

  /**
   * A quick utility function that determines whether kryo has been linked
   * with the streamlet binary
   */
  public static void checkForKryo() {
    Kryo k = new Kryo();
  }

  @Override
  public void initialize(Map config) {
    kryo = getKryo();
    kryoOut = new Output(2000, 2000000000);
    kryoIn = new Input(1);
  }

  @Override
  public byte[] serialize(Object object) {
    kryoOut.clear();
    kryo.writeClassAndObject(kryoOut, object);
    return kryoOut.toBytes();
  }

  @Override
  public Object deserialize(byte[] input) {
    kryoIn.setBuffer(input);
    return kryo.readClassAndObject(kryoIn);
  }

  private Kryo getKryo() {
    Kryo k = new Kryo();
    k.setRegistrationRequired(false);
    k.setReferences(false);
    k.register(byte[].class);
    k.register(ArrayList.class, new ArrayListSerializer());
    k.register(HashMap.class, new HashMapSerializer());
    k.register(HashSet.class, new HashSetSerializer());
    k.register(BigInteger.class, new DefaultSerializers.BigIntegerSerializer());
    return k;
  }

  private class ArrayListSerializer extends CollectionSerializer {
    @Override
    @SuppressWarnings("rawtypes") // extending Kryo class that uses raw types
    public Collection create(Kryo k, Input input, Class type) {
      return new ArrayList();
    }
  }

  private class HashMapSerializer extends MapSerializer {
    @Override
    @SuppressWarnings("rawtypes") // extending kryo class signature that takes Map
    public Map create(Kryo k, Input input, Class type) {
      return new HashMap<>();
    }
  }

  private class HashSetSerializer extends CollectionSerializer {
    @Override
    @SuppressWarnings("rawtypes") // extending Kryo class that uses raw types
    public Collection create(Kryo k, Input input, Class type) {
      return new HashSet();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy