Skip to content

Commit

Permalink
Merge pull request #156 from twitter/chill-thrift
Browse files Browse the repository at this point in the history
Adds chill-thrift
  • Loading branch information
ianoc committed Nov 5, 2013
2 parents bdca40b + 80c1f2e commit faaa890
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
@@ -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<TBase> {
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<TBase> 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);
}
}
}

12 changes: 11 additions & 1 deletion project/Build.scala
Expand Up @@ -90,6 +90,7 @@ object ChillBuild extends Build {
chillStorm,
chillJava,
chillHadoop,
chillThrift,
chillAkka
)

Expand All @@ -98,7 +99,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)
Expand Down Expand Up @@ -182,4 +183,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"
)
)
}

0 comments on commit faaa890

Please sign in to comment.