Skip to content

Commit

Permalink
Add a OrderedSerialization.viaTransform with no dependencies, and a B…
Browse files Browse the repository at this point in the history
…ijectedOrderedSerialization in scalding core
  • Loading branch information
ianoc committed Jun 18, 2015
1 parent e2da143 commit a7c8c25
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright 2014 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.scalding

import com.twitter.scalding.serialization.OrderedSerialization
import com.twitter.bijection.Bijection

object BijectedOrderedSerialization {
implicit def fromBijection[T, U](implicit bij: Bijection[T, U], ordSer: OrderedSerialization[U]) =
OrderedSerialization.viaTransform[T, U](bij.apply(_), bij.invert(_))
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ object OrderedSerialization {
case NonFatal(e) => CompareFailure(e)
}

def viaTransform[T, U](
packFn: T => U,
unpackFn: U => T)(implicit otherOrdSer: OrderedSerialization[U]): OrderedSerialization[T] =
new OrderedSerialization[T] {

override def hash(t: T) = otherOrdSer.hash(packFn(t))

override def compareBinary(a: java.io.InputStream, b: java.io.InputStream): OrderedSerialization.Result =
otherOrdSer.compareBinary(a, b)

override def compare(x: T, y: T) =
otherOrdSer.compare(packFn(x), packFn(y))

override def read(in: InputStream): Try[T] =
otherOrdSer.read(in).map(unpackFn)

override def write(out: OutputStream, t: T): Try[Unit] =
otherOrdSer.write(out, packFn(t))

override def staticSize: Option[Int] = otherOrdSer.staticSize

override def dynamicSize(t: T): Option[Int] = otherOrdSer.dynamicSize(packFn(t))
}

/**
* The the serialized comparison matches the unserialized comparison
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ object SerializationProperties extends Properties("SerializationProperties") {

implicit val stringOrdSer: OrderedSerialization[String] = new StringOrderedSerialization

class IntWrapperClass(val x: Int)

implicit val myIntWrapperOrdSer: OrderedSerialization[IntWrapperClass] =
OrderedSerialization.viaTransform[IntWrapperClass, Int](_.x, new IntWrapperClass(_))

implicit val arbIntWrapperClass: Arbitrary[IntWrapperClass] =
Arbitrary(implicitly[Arbitrary[Int]].arbitrary.map(new IntWrapperClass(_)))

implicit def tuple[A: OrderedSerialization, B: OrderedSerialization]: OrderedSerialization[(A, B)] =
new OrderedSerialization2[A, B](implicitly, implicitly)

Expand Down Expand Up @@ -119,11 +127,15 @@ object SerializationProperties extends Properties("SerializationProperties") {
property("sequences compare well [(String, String)]") = serializeSequenceCompare[(String, String)]
property("sequences equiv well [(String, String)]") = serializeSequenceEquiv[(String, String)]

property("sequences compare well [IntWrapperClass]") = serializeSequenceCompare[IntWrapperClass]
property("sequences equiv well [IntWrapperClass]") = serializeSequenceEquiv[IntWrapperClass]

// Test the independent, non-sequenced, laws as well
include(LawTester("Int Ordered", OrderedSerialization.allLaws[Int]))
include(LawTester("(Int, Int) Ordered", OrderedSerialization.allLaws[(Int, Int)]))
include(LawTester("String Ordered", OrderedSerialization.allLaws[String]))
include(LawTester("(String, Int) Ordered", OrderedSerialization.allLaws[(String, Int)]))
include(LawTester("(Int, String) Ordered", OrderedSerialization.allLaws[(Int, String)]))
include(LawTester("(String, String) Ordered", OrderedSerialization.allLaws[(String, String)]))
include(LawTester("IntWrapperClass Ordered", OrderedSerialization.allLaws[IntWrapperClass]))
}

0 comments on commit a7c8c25

Please sign in to comment.