Skip to content

Instantly share code, notes, and snippets.

@choplin
Last active December 12, 2015 04:08
Show Gist options
  • Save choplin/4712460 to your computer and use it in GitHub Desktop.
Save choplin/4712460 to your computer and use it in GitHub Desktop.
型パラメータを使うとだめぽ
import java.io.{DataInput, DataOutput}
import org.apache.hadoop.io.{WritableComparable, WritableComparator}
import org.msgpack.ScalaMessagePack
trait MessagePackWritable extends WritableComparable[MessagePackWritable] {
type T
var instance:T
def get = instance
def getRawBytes = ScalaMessagePack.write(instance)
def write(out: DataOutput) {
val raw = ScalaMessagePack.write(instance)
assert(raw != null)
out.writeInt(raw.length)
out.write(raw, 0, raw.length)
}
def readFields(in: DataInput) {
val size = in.readInt()
if (size > 0) {
val raw = new Array[Byte](size)
in.readFully(raw, 0, size)
instance = unpack(raw)
assert(instance != null, "unpacked null")
}
}
def compareTo(other: MessagePackWritable) = {
val raw1 = ScalaMessagePack.write(instance)
val raw2 = ScalaMessagePack.write(other.instance)
WritableComparator.compareBytes(raw1, 0, raw1.length, raw2, 0, raw2.length)
}
def unpack(raw: Array[Byte]): T
}
class ListMessagePackWritable(l: List[Int]) extends MessagePackWritable {
var instance = l
type T = List[Int]
def this() = this(null)
def unpack(raw: Array[Byte]) = ScalaMessagePack.read[T](raw)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment