From f78c19156915b290859a0d84b8c42e22b0f3dd24 Mon Sep 17 00:00:00 2001 From: Ben Pence Date: Tue, 2 Jun 2015 14:08:10 -0700 Subject: [PATCH 1/3] Simplifies hbase injections with fastAttempt macro --- .../bijection/hbase/HBaseInjections.scala | 45 ++++++++----------- project/Build.scala | 1 + 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/bijection-hbase/src/main/scala/com/twitter/bijection/hbase/HBaseInjections.scala b/bijection-hbase/src/main/scala/com/twitter/bijection/hbase/HBaseInjections.scala index cabf8653e..6fe062dff 100644 --- a/bijection-hbase/src/main/scala/com/twitter/bijection/hbase/HBaseInjections.scala +++ b/bijection-hbase/src/main/scala/com/twitter/bijection/hbase/HBaseInjections.scala @@ -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 @@ -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("Could not read string from: $b") + 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] { diff --git a/project/Build.scala b/project/Build.scala index c991ab66b..e529bf946 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -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 % "test->test;compile->compile") lazy val bijectionJodaTime = module("jodatime").settings( osgiExportAll("com.twitter.bijection.jodatime"), From fb91a69c80b4b6761e0a9391fc686f4bbd578608 Mon Sep 17 00:00:00 2001 From: Ben Pence Date: Wed, 3 Jun 2015 22:12:50 -0700 Subject: [PATCH 2/3] Removes hbase dependency on macros test target --- project/Build.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Build.scala b/project/Build.scala index e529bf946..6a03da161 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -262,7 +262,7 @@ object BijectionBuild extends Build { "org.apache.hadoop" % "hadoop-core" % "1.0.4" % "provided->default" ) ).dependsOn(bijectionCore % "test->test;compile->compile") - .dependsOn(bijectionMacros % "test->test;compile->compile") + .dependsOn(bijectionMacros % "compile->compile") lazy val bijectionJodaTime = module("jodatime").settings( osgiExportAll("com.twitter.bijection.jodatime"), From 9289d5f5f0972fe488d63697c4f086ac120a29cd Mon Sep 17 00:00:00 2001 From: Ben Pence Date: Wed, 3 Jun 2015 22:15:22 -0700 Subject: [PATCH 3/3] Fixes hbase injection error message --- .../scala/com/twitter/bijection/hbase/HBaseInjections.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bijection-hbase/src/main/scala/com/twitter/bijection/hbase/HBaseInjections.scala b/bijection-hbase/src/main/scala/com/twitter/bijection/hbase/HBaseInjections.scala index 6fe062dff..e872f8099 100644 --- a/bijection-hbase/src/main/scala/com/twitter/bijection/hbase/HBaseInjections.scala +++ b/bijection-hbase/src/main/scala/com/twitter/bijection/hbase/HBaseInjections.scala @@ -60,7 +60,7 @@ object HBaseInjections { new ImmutableBytesWritableInjection[String] { override def invert(b: ImmutableBytesWritable) = fastAttempt(b)(Bytes.toString(b.get, b.getOffset, b.getLength) match { - case null => sys.error("Could not read string from: $b") + case null => sys.error(s"$b decoded to null, which is disallowed.") case str => str }) }