Package cascalog.kryo

Source Code of cascalog.kryo.KryoService

package cascalog.kryo;

import com.twitter.chill.KryoInstantiator;
import com.twitter.chill.KryoPool;
import com.twitter.chill.hadoop.HadoopConfig;
import com.twitter.chill.config.ConfiguredInstantiator;
import com.twitter.chill.config.ConfigurationException;

import static cascalog.Util.clojureConf;

public class KryoService {
  static int GUESS_THREADS_PER_CORE = 4;
  static int MAX_CACHED_KRYO = GUESS_THREADS_PER_CORE * Runtime.getRuntime().availableProcessors();

  static final Object mutex =  new Object();
  static KryoPool kpool = null;

  public static KryoPool defaultPool() {
    synchronized(mutex) {
      if (kpool == null) {
        try {
          KryoInstantiator kryoInst = new ConfiguredInstantiator(new HadoopConfig(clojureConf()));
          kpool = KryoPool.withByteArrayOutputStream(MAX_CACHED_KRYO, kryoInst);
        } catch (ConfigurationException cx) {
          throw new RuntimeException(cx);
        }
      }
      return kpool;
    }
  }

  public static byte[] serialize(Object obj) {
    return defaultPool().toBytesWithClass(obj);
  }

  public static Object deserialize(byte[] bytes) {
    return defaultPool().fromBytes(bytes);
  }

}
TOP

Related Classes of cascalog.kryo.KryoService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.