Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifies hbase injections with fastAttempt macro #220

Merged
merged 3 commits into from
Jun 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package com.twitter.bijection.hbase

import com.twitter.bijection.AbstractInjection
import com.twitter.bijection.Injection
import com.twitter.bijection.macros.Macros.fastAttempt
import com.twitter.bijection.Tag
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.util.Bytes
Expand Down Expand Up @@ -58,53 +59,45 @@ object HBaseInjections {
implicit lazy val string2BytesWritableInj =
new ImmutableBytesWritableInjection[String] {
override def invert(b: ImmutableBytesWritable) =
Try {
val str = Bytes.toString(b.get, b.getOffset, b.getLength)
if (str == null) sys.error("Invalid string")
else str
}
fastAttempt(b)(Bytes.toString(b.get, b.getOffset, b.getLength) match {
case null => sys.error(s"$b decoded to null, which is disallowed.")
case str => str
})
}
implicit lazy val int2BytesWritableInj =
new ImmutableBytesWritableInjection[Int] {
override def invert(b: ImmutableBytesWritable) = Try {
Bytes.toInt(b.get, b.getOffset, b.getLength)
}
override def invert(b: ImmutableBytesWritable) =
fastAttempt(b)(Bytes.toInt(b.get, b.getOffset, b.getLength))
}
implicit lazy val long2BytesWritableInj =
new ImmutableBytesWritableInjection[Long] {
override def invert(b: ImmutableBytesWritable) = Try {
Bytes.toLong(b.get, b.getOffset, b.getLength)
}
override def invert(b: ImmutableBytesWritable) =
fastAttempt(b)(Bytes.toLong(b.get, b.getOffset, b.getLength))
}
implicit lazy val double2BytesWritableInj =
new ImmutableBytesWritableInjection[Double] {
override def invert(b: ImmutableBytesWritable) = Try {
Bytes.toDouble(b.get, b.getOffset)
}
override def invert(b: ImmutableBytesWritable) =
fastAttempt(b)(Bytes.toDouble(b.get, b.getOffset))
}
implicit lazy val float2BytesWritableInj =
new ImmutableBytesWritableInjection[Float] {
override def invert(b: ImmutableBytesWritable) = Try {
Bytes.toFloat(b.get, b.getOffset)
}
override def invert(b: ImmutableBytesWritable) =
fastAttempt(b)(Bytes.toFloat(b.get, b.getOffset))
}
implicit lazy val short2BytesWritableInj =
new ImmutableBytesWritableInjection[Short] {
override def invert(b: ImmutableBytesWritable) = Try {
Bytes.toShort(b.get, b.getOffset, b.getLength)
}
override def invert(b: ImmutableBytesWritable) =
fastAttempt(b)(Bytes.toShort(b.get, b.getOffset, b.getLength))
}
implicit lazy val boolean2BytesWritableInj =
new ImmutableBytesWritableInjection[Boolean] {
override def invert(b: ImmutableBytesWritable) = Try {
Bytes.toBoolean(b.copyBytes)
}
override def invert(b: ImmutableBytesWritable) =
fastAttempt(b)(Bytes.toBoolean(b.copyBytes))
}
implicit lazy val bigDecimal2BytesWritableInj =
new ImmutableBytesWritableInjection[BigDecimal] {
override def invert(b: ImmutableBytesWritable) = Try {
Bytes.toBigDecimal(b.get, b.getOffset, b.getLength)
}
override def invert(b: ImmutableBytesWritable) =
fastAttempt(b)(Bytes.toBigDecimal(b.get, b.getOffset, b.getLength))
}
implicit lazy val bytes2BytesWritableInj: Injection[Array[Byte], ImmutableBytesWritable] =
new AbstractInjection[Array[Byte], ImmutableBytesWritable] {
Expand Down
1 change: 1 addition & 0 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ object BijectionBuild extends Build {
"org.apache.hadoop" % "hadoop-core" % "1.0.4" % "provided->default"
)
).dependsOn(bijectionCore % "test->test;compile->compile")
.dependsOn(bijectionMacros % "compile->compile")

lazy val bijectionJodaTime = module("jodatime").settings(
osgiExportAll("com.twitter.bijection.jodatime"),
Expand Down