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 2013/10/14 21:18:36 UTC

git commit: SAMZA-56; add an integer serde.

Updated Branches:
  refs/heads/master e949c810b -> 0c3a3ac7b


SAMZA-56; add an integer serde.


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

Branch: refs/heads/master
Commit: 0c3a3ac7bee94d969fcfb184fab1513ab7c6f602
Parents: e949c81
Author: Chris Riccomini <cr...@criccomi-mn.linkedin.biz>
Authored: Mon Oct 14 12:18:27 2013 -0700
Committer: Chris Riccomini <cr...@criccomi-mn.linkedin.biz>
Committed: Mon Oct 14 12:18:27 2013 -0700

----------------------------------------------------------------------
 .../apache/samza/serializers/IntegerSerde.scala | 45 ++++++++++++++++++++
 .../samza/serializers/TestIntegerSerde.scala    | 39 +++++++++++++++++
 2 files changed, 84 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-samza/blob/0c3a3ac7/samza-core/src/main/scala/org/apache/samza/serializers/IntegerSerde.scala
----------------------------------------------------------------------
diff --git a/samza-core/src/main/scala/org/apache/samza/serializers/IntegerSerde.scala b/samza-core/src/main/scala/org/apache/samza/serializers/IntegerSerde.scala
new file mode 100644
index 0000000..566fefa
--- /dev/null
+++ b/samza-core/src/main/scala/org/apache/samza/serializers/IntegerSerde.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 integers
+ */
+class IntegerSerdeFactory extends SerdeFactory[Integer] {
+  def getSerde(name: String, config: Config): Serde[Integer] = new IntegerSerde
+}
+
+class IntegerSerde extends Serde[Integer] {
+  def toBytes(obj: Integer): Array[Byte] = if (obj != null) {
+    ByteBuffer.allocate(4).putInt(obj).array
+  } else {
+    null
+  }
+
+  // big-endian by default
+  def fromBytes(bytes: Array[Byte]): Integer = if (bytes != null) {
+    ByteBuffer.wrap(bytes).getInt
+  } else {
+    null
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-samza/blob/0c3a3ac7/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
new file mode 100644
index 0000000..45a2b04
--- /dev/null
+++ b/samza-core/src/test/scala/org/apache/samza/serializers/TestIntegerSerde.scala
@@ -0,0 +1,39 @@
+/*
+ * 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 org.junit.Assert._
+import org.junit.Test
+import java.util.Arrays
+
+class TestIntegerSerde {
+  @Test
+  def testIntegerSerde {
+    val serde = new IntegerSerde
+    assertEquals(null, serde.toBytes(null))
+    assertEquals(null, serde.fromBytes(null))
+
+    val fooBar = 37
+    val fooBarBytes = serde.toBytes(fooBar)
+    fooBarBytes.foreach(System.err.println)
+    assertTrue(Arrays.equals(Array[Byte](0, 0, 0, 37), fooBarBytes))
+    assertEquals(fooBar, serde.fromBytes(fooBarBytes))
+  }
+}
\ No newline at end of file