You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@samza.apache.org by ga...@apache.org on 2014/03/03 23:33:58 UTC

git commit: SAMZA-110 Add a byte serde

Repository: incubator-samza
Updated Branches:
  refs/heads/master d364a5f70 -> 046ae23ba


SAMZA-110 Add a byte 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/046ae23b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-samza/tree/046ae23b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-samza/diff/046ae23b

Branch: refs/heads/master
Commit: 046ae23bab8f61cdd1e2644c6ee4f9210608e2d7
Parents: d364a5f
Author: Garry Turkington <ga...@apache.org>
Authored: Mon Mar 3 22:33:42 2014 +0000
Committer: Garry Turkington <ga...@apache.org>
Committed: Mon Mar 3 22:33:42 2014 +0000

----------------------------------------------------------------------
 .../apache/samza/serializers/ByteSerde.scala    | 35 ++++++++++++++++++
 .../samza/serializers/TestByteSerde.scala       | 37 ++++++++++++++++++++
 2 files changed, 72 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-samza/blob/046ae23b/samza-core/src/main/scala/org/apache/samza/serializers/ByteSerde.scala
----------------------------------------------------------------------
diff --git a/samza-core/src/main/scala/org/apache/samza/serializers/ByteSerde.scala b/samza-core/src/main/scala/org/apache/samza/serializers/ByteSerde.scala
new file mode 100644
index 0000000..e7ce09f
--- /dev/null
+++ b/samza-core/src/main/scala/org/apache/samza/serializers/ByteSerde.scala
@@ -0,0 +1,35 @@
+/*
+ * 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.apache.samza.config.Config
+
+/**
+ * A serializer for bytes that is effectively a no-op but can be useful for binary messages.
+ */
+class ByteSerdeFactory extends SerdeFactory[Array[Byte]] {
+  def getSerde(name: String, config: Config): Serde[Array[Byte]] = new ByteSerde
+}
+
+class ByteSerde extends Serde[Array[Byte]] {
+  def toBytes(bytes: Array[Byte]) = bytes
+
+  def fromBytes(bytes: Array[Byte]) = bytes
+}

http://git-wip-us.apache.org/repos/asf/incubator-samza/blob/046ae23b/samza-core/src/test/scala/org/apache/samza/serializers/TestByteSerde.scala
----------------------------------------------------------------------
diff --git a/samza-core/src/test/scala/org/apache/samza/serializers/TestByteSerde.scala b/samza-core/src/test/scala/org/apache/samza/serializers/TestByteSerde.scala
new file mode 100644
index 0000000..f64c263
--- /dev/null
+++ b/samza-core/src/test/scala/org/apache/samza/serializers/TestByteSerde.scala
@@ -0,0 +1,37 @@
+/*
+ * 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 TestByteSerde {
+  @Test
+  def testByteSerde {
+    val serde = new ByteSerde
+    assertEquals(null, serde.toBytes(null))
+    assertEquals(null, serde.fromBytes(null))
+
+    val testBytes = "A lazy way of creating a byte array".getBytes()
+    assertTrue(Arrays.equals(serde.toBytes(testBytes), testBytes))
+    assertTrue( Arrays.equals(serde.fromBytes(testBytes), testBytes))
+  }
+}