SAMZA-636; add a uuid serde
Project: http://git-wip-us.apache.org/repos/asf/samza/repo Commit: http://git-wip-us.apache.org/repos/asf/samza/commit/edc2c78f Tree: http://git-wip-us.apache.org/repos/asf/samza/tree/edc2c78f Diff: http://git-wip-us.apache.org/repos/asf/samza/diff/edc2c78f Branch: refs/heads/samza-sql Commit: edc2c78f8759bb38dae1c2e0472f1123456f30c5 Parents: b276afe Author: Mohamed Mahmoud (El-Geish) <[email protected]> Authored: Tue Apr 7 16:18:35 2015 -0700 Committer: Chris Riccomini <[email protected]> Committed: Tue Apr 7 16:18:35 2015 -0700 ---------------------------------------------------------------------- .../apache/samza/serializers/StringSerde.scala | 1 - .../apache/samza/serializers/UUIDSerde.scala | 47 +++++++++++++++++ .../samza/serializers/TestIntegerSerde.scala | 3 -- .../samza/serializers/TestUUIDSerde.scala | 53 ++++++++++++++++++++ 4 files changed, 100 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/samza/blob/edc2c78f/samza-core/src/main/scala/org/apache/samza/serializers/StringSerde.scala ---------------------------------------------------------------------- diff --git a/samza-core/src/main/scala/org/apache/samza/serializers/StringSerde.scala b/samza-core/src/main/scala/org/apache/samza/serializers/StringSerde.scala index 7b4b23d..6e5f260 100644 --- a/samza-core/src/main/scala/org/apache/samza/serializers/StringSerde.scala +++ b/samza-core/src/main/scala/org/apache/samza/serializers/StringSerde.scala @@ -19,7 +19,6 @@ package org.apache.samza.serializers -import java.nio.ByteBuffer import org.apache.samza.config.Config /** http://git-wip-us.apache.org/repos/asf/samza/blob/edc2c78f/samza-core/src/main/scala/org/apache/samza/serializers/UUIDSerde.scala ---------------------------------------------------------------------- diff --git a/samza-core/src/main/scala/org/apache/samza/serializers/UUIDSerde.scala b/samza-core/src/main/scala/org/apache/samza/serializers/UUIDSerde.scala new file mode 100644 index 0000000..88d4327 --- /dev/null +++ b/samza-core/src/main/scala/org/apache/samza/serializers/UUIDSerde.scala @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.samza.serializers + +import java.nio.ByteBuffer +import java.util.UUID + +import org.apache.samza.config.Config + +/** + * A serializer for UUID + */ +class UUIDSerdeFactory extends SerdeFactory[UUID] { + def getSerde(name: String, config: Config): Serde[UUID] = new UUIDSerde +} + +class UUIDSerde() extends Serde[UUID] { + def toBytes(obj: UUID): Array[Byte] = if (obj != null) { + ByteBuffer.allocate(16).putLong(obj.getMostSignificantBits).putLong(obj.getLeastSignificantBits).array + } else { + null + } + + def fromBytes(bytes: Array[Byte]): UUID = if (bytes != null) { + val buffer = ByteBuffer.wrap(bytes) + new UUID(buffer.getLong, buffer.getLong) + } else { + null + } +} http://git-wip-us.apache.org/repos/asf/samza/blob/edc2c78f/samza-core/src/test/scala/org/apache/samza/serializers/TestIntegerSerde.scala ---------------------------------------------------------------------- diff --git a/samza-core/src/test/scala/org/apache/samza/serializers/TestIntegerSerde.scala b/samza-core/src/test/scala/org/apache/samza/serializers/TestIntegerSerde.scala index ad646d7..a3e7e1f 100644 --- a/samza-core/src/test/scala/org/apache/samza/serializers/TestIntegerSerde.scala +++ b/samza-core/src/test/scala/org/apache/samza/serializers/TestIntegerSerde.scala @@ -19,8 +19,6 @@ package org.apache.samza.serializers -import java.util.Arrays - import org.junit.Assert._ import org.junit.Test @@ -33,7 +31,6 @@ class TestIntegerSerde { val fooBar = 37 val fooBarBytes = serde.toBytes(fooBar) - fooBarBytes.foreach(System.err.println) assertArrayEquals(Array[Byte](0, 0, 0, 37), fooBarBytes) assertEquals(fooBar, serde.fromBytes(fooBarBytes)) } http://git-wip-us.apache.org/repos/asf/samza/blob/edc2c78f/samza-core/src/test/scala/org/apache/samza/serializers/TestUUIDSerde.scala ---------------------------------------------------------------------- diff --git a/samza-core/src/test/scala/org/apache/samza/serializers/TestUUIDSerde.scala b/samza-core/src/test/scala/org/apache/samza/serializers/TestUUIDSerde.scala new file mode 100644 index 0000000..04ddcdb --- /dev/null +++ b/samza-core/src/test/scala/org/apache/samza/serializers/TestUUIDSerde.scala @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.samza.serializers + +import java.nio.BufferUnderflowException +import java.util.UUID + +import org.junit.Assert._ +import org.junit.Test + +class TestUUIDSerde { + private val serde = new UUIDSerde + + @Test + def testUUIDSerde { + val uuid = new UUID(13, 42) + val bytes = serde.toBytes(uuid) + assertArrayEquals(Array[Byte](0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 42), bytes) + assertEquals(uuid, serde.fromBytes(bytes)) + } + + @Test + def testToBytesWhenNull { + assertEquals(null, serde.toBytes(null)) + } + + @Test + def testFromBytesWhenNull { + assertEquals(null, serde.fromBytes(null)) + } + + @Test(expected = classOf[BufferUnderflowException]) + def testFromBytesWhenInvalid { + assertEquals(null, serde.fromBytes(Array[Byte](0))) + } +}
