forked from bigdatagenomics/adam
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d16cd03
commit 2f05f3e
Showing
7 changed files
with
346 additions
and
399 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
addSbtPlugin("org.hammerlab.sbt" % "base" % "4.6.2-SNAPSHOT") | ||
addSbtPlugin("org.hammerlab.sbt" % "base" % "4.6.2") |
637 changes: 263 additions & 374 deletions
637
src/main/scala/org/bdgenomics/adam/serialization/ADAMKryoRegistrator.scala
Large diffs are not rendered by default.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
src/main/scala/org/bdgenomics/adam/serialization/AvroSerializer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.bdgenomics.adam.serialization | ||
|
||
import com.esotericsoftware.kryo.io.{ Input, Output } | ||
import com.esotericsoftware.kryo.{ Kryo, Serializer } | ||
import it.unimi.dsi.fastutil.io.{ FastByteArrayInputStream, FastByteArrayOutputStream } | ||
import org.apache.avro.io.{ BinaryDecoder, BinaryEncoder, DecoderFactory, EncoderFactory } | ||
import org.apache.avro.specific.{ SpecificDatumReader, SpecificDatumWriter, SpecificRecord } | ||
|
||
import scala.reflect.ClassTag | ||
// NOTE: This class is not thread-safe; however, Spark guarantees that only a single thread will access it. | ||
class AvroSerializer[T <: SpecificRecord: ClassTag] extends Serializer[T] { | ||
val reader = new SpecificDatumReader[T](scala.reflect.classTag[T].runtimeClass.asInstanceOf[Class[T]]) | ||
val writer = new SpecificDatumWriter[T](scala.reflect.classTag[T].runtimeClass.asInstanceOf[Class[T]]) | ||
var in = InputStreamWithDecoder(1024) | ||
val outstream = new FastByteArrayOutputStream() | ||
val encoder = EncoderFactory.get().directBinaryEncoder(outstream, null.asInstanceOf[BinaryEncoder]) | ||
|
||
setAcceptsNull(false) | ||
|
||
def write(kryo: Kryo, kryoOut: Output, record: T) = { | ||
outstream.reset() | ||
writer.write(record, encoder) | ||
kryoOut.writeInt(outstream.array.length, true) | ||
kryoOut.write(outstream.array) | ||
} | ||
|
||
def read(kryo: Kryo, kryoIn: Input, klazz: Class[T]): T = this.synchronized { | ||
val len = kryoIn.readInt(true) | ||
if (len > in.size) { | ||
in = InputStreamWithDecoder(len + 1024) | ||
} | ||
in.stream.reset() | ||
// Read Kryo bytes into input buffer | ||
kryoIn.readBytes(in.buffer, 0, len) | ||
// Read the Avro object from the buffer | ||
reader.read(null.asInstanceOf[T], in.decoder) | ||
} | ||
} | ||
|
||
case class InputStreamWithDecoder(size: Int) { | ||
val buffer = new Array[Byte](size) | ||
val stream = new FastByteArrayInputStream(buffer) | ||
val decoder = DecoderFactory.get().directBinaryDecoder(stream, null.asInstanceOf[BinaryDecoder]) | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/scala/org/bdgenomics/adam/serialization/WritableSerializer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.bdgenomics.adam.serialization | ||
|
||
import com.esotericsoftware.kryo.io.{ Input, KryoDataInput, KryoDataOutput, Output } | ||
import com.esotericsoftware.kryo.{ Kryo, Serializer } | ||
import org.apache.hadoop.io.Writable | ||
|
||
/** | ||
* A Kryo serializer for Hadoop writables. | ||
* | ||
* Lifted from the Apache Spark user email list | ||
* (http://apache-spark-user-list.1001560.n3.nabble.com/Hadoop-Writable-and-Spark-serialization-td5721.html) | ||
* which indicates that it was originally copied from Shark itself, back when | ||
* Spark 0.9 was the state of the art. | ||
* | ||
* @tparam T The class to serialize, which implements the Writable interface. | ||
*/ | ||
class WritableSerializer[T <: Writable] extends Serializer[T] { | ||
override def write(kryo: Kryo, output: Output, writable: T) { | ||
writable.write(new KryoDataOutput(output)) | ||
} | ||
|
||
override def read(kryo: Kryo, input: Input, cls: java.lang.Class[T]): T = { | ||
val writable = cls.newInstance() | ||
writable.readFields(new KryoDataInput(input)) | ||
writable | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters