You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@samza.apache.org by cr...@apache.org on 2015/01/26 17:04:28 UTC

samza git commit: SAMZA-481; add a LongSerde

Repository: samza
Updated Branches:
  refs/heads/master 12874b46d -> b8debad38


SAMZA-481; add a LongSerde


Project: http://git-wip-us.apache.org/repos/asf/samza/repo
Commit: http://git-wip-us.apache.org/repos/asf/samza/commit/b8debad3
Tree: http://git-wip-us.apache.org/repos/asf/samza/tree/b8debad3
Diff: http://git-wip-us.apache.org/repos/asf/samza/diff/b8debad3

Branch: refs/heads/master
Commit: b8debad383316b6e4413c875491385b7d4a9bd4a
Parents: 12874b4
Author: Manikumar Reddy <ma...@gmail.com>
Authored: Mon Jan 26 08:04:22 2015 -0800
Committer: Chris Riccomini <cr...@criccomi-mn.linkedin.biz>
Committed: Mon Jan 26 08:04:22 2015 -0800

----------------------------------------------------------------------
 .../apache/samza/serializers/LongSerde.scala    | 45 ++++++++++++++++++++
 .../samza/serializers/TestLongSerde.scala       | 40 +++++++++++++++++
 2 files changed, 85 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/samza/blob/b8debad3/samza-core/src/main/scala/org/apache/samza/serializers/LongSerde.scala
----------------------------------------------------------------------
diff --git a/samza-core/src/main/scala/org/apache/samza/serializers/LongSerde.scala b/samza-core/src/main/scala/org/apache/samza/serializers/LongSerde.scala
new file mode 100644
index 0000000..41ff598
--- /dev/null
+++ b/samza-core/src/main/scala/org/apache/samza/serializers/LongSerde.scala
@@ -0,0 +1,45 @@
+/*
+ * 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 org.apache.samza.config.Config
+
+/**
+ * A serializer for longs
+ */
+class LongSerdeFactory extends SerdeFactory[java.lang.Long] {
+  def getSerde(name: String, config: Config): Serde[java.lang.Long] = new LongSerde
+}
+
+class LongSerde extends Serde[java.lang.Long] {
+  def toBytes(obj: java.lang.Long): Array[Byte] = if (obj != null) {
+    ByteBuffer.allocate(8).putLong(obj.longValue()).array
+  } else {
+    null
+  }
+
+  // big-endian by default
+  def fromBytes(bytes: Array[Byte]): java.lang.Long = if (bytes != null) {
+    ByteBuffer.wrap(bytes).getLong
+  } else {
+    null
+  }
+}

http://git-wip-us.apache.org/repos/asf/samza/blob/b8debad3/samza-core/src/test/scala/org/apache/samza/serializers/TestLongSerde.scala
----------------------------------------------------------------------
diff --git a/samza-core/src/test/scala/org/apache/samza/serializers/TestLongSerde.scala b/samza-core/src/test/scala/org/apache/samza/serializers/TestLongSerde.scala
new file mode 100644
index 0000000..77a7498
--- /dev/null
+++ b/samza-core/src/test/scala/org/apache/samza/serializers/TestLongSerde.scala
@@ -0,0 +1,40 @@
+/*
+ * 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.util.Arrays
+
+import org.junit.Assert._
+import org.junit.Test
+
+class TestLongSerde {
+  @Test
+  def testLongSerde {
+    val serde = new LongSerde
+    assertEquals(null, serde.toBytes(null))
+    assertEquals(null, serde.fromBytes(null))
+
+    val fooBar = 1234123412341234L
+    val fooBarBytes = serde.toBytes(fooBar)
+    fooBarBytes.foreach(System.err.println)
+    assertArrayEquals(Array[Byte](0, 4, 98, 109, -65, -102, 1, -14), fooBarBytes)
+    assertEquals(fooBar, serde.fromBytes(fooBarBytes))
+  }
+}