From e833a7f7534f6b784de92de517ec986fad4941b6 Mon Sep 17 00:00:00 2001 From: "P. Oscar Boykin" Date: Mon, 4 Nov 2013 18:51:40 -0800 Subject: [PATCH 1/2] Adds chill-thrift --- .../twitter/chill/thrift/TBaseSerializer.java | 69 +++++++++++++++++++ project/Build.scala | 11 ++- 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 chill-thrift/src/main/java/com/twitter/chill/thrift/TBaseSerializer.java diff --git a/chill-thrift/src/main/java/com/twitter/chill/thrift/TBaseSerializer.java b/chill-thrift/src/main/java/com/twitter/chill/thrift/TBaseSerializer.java new file mode 100644 index 00000000..8ee5fac7 --- /dev/null +++ b/chill-thrift/src/main/java/com/twitter/chill/thrift/TBaseSerializer.java @@ -0,0 +1,69 @@ +/* +Copyright 2013 Twitter, 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.twitter.chill.thrift; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +import org.apache.thrift.TBase; +import org.apache.thrift.TDeserializer; +import org.apache.thrift.TException; +import org.apache.thrift.TSerializer; + +/** + * Kryo serializer for Thrift instances. + * + * Note that this class is not thread-safe. (Kryo itself is not thread + * safe, so this shouldn't be a concern.) + * + * Use this with + * addDefaultSerializer(TBase.class, TBaseSerializer.class) + * It still helps to .register your instances so the full class name + * does not need to be written. + */ +public class TBaseSerializer extends Serializer { + private final TSerializer serializer = new TSerializer(); + private final TDeserializer deserializer = new TDeserializer(); + + @Override + public void write(Kryo kryo, Output output, TBase tBase) { + try { + byte[] serThrift = serializer.serialize(tBase); + output.writeInt(serThrift.length, true); + output.writeBytes(serThrift); + } catch (TException e) { + throw new RuntimeException(e); + } + } + + @Override + public TBase read(Kryo kryo, Input input, Class tBaseClass) { + try { + TBase prototype = tBaseClass.newInstance(); + int tSize = input.readInt(true); + byte[] barr = new byte[tSize]; + input.readBytes(barr); + deserializer.deserialize(prototype, barr); + return prototype; + } catch (Exception e) { + throw new RuntimeException("Could not create " + tBaseClass, e); + } + } +} + diff --git a/project/Build.scala b/project/Build.scala index 52574233..85370005 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -97,7 +97,7 @@ object ChillBuild extends Build { * with the current. */ val unreleasedModules = Set[String]("akka") - val javaOnly = Set[String]("storm", "java", "hadoop") + val javaOnly = Set[String]("storm", "java", "hadoop", "thrift") def youngestForwardCompatible(subProj: String) = Some(subProj) @@ -170,4 +170,13 @@ object ChillBuild extends Build { "org.slf4j" % "slf4j-log4j12" % "1.6.6" % "provided" ) ).dependsOn(chillJava) + + // This can only have java deps! + lazy val chillThrift = module("thrift").settings( + crossPaths := false, + autoScalaLibrary := false, + libraryDependencies ++= Seq( + "org.apache.thrift" % "libthrift" % "0.6.1" % "provided" + ) + ) } From baf07a1f747af1c0504adfc160816c413f07fcdc Mon Sep 17 00:00:00 2001 From: "P. Oscar Boykin" Date: Mon, 4 Nov 2013 18:54:13 -0800 Subject: [PATCH 2/2] Add to aggregate target --- project/Build.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/project/Build.scala b/project/Build.scala index 85370005..53a39ccd 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -89,7 +89,8 @@ object ChillBuild extends Build { chillBijection, chillStorm, chillJava, - chillHadoop + chillHadoop, + chillThrift ) /**