This is an automated email from the ASF dual-hosted git repository.
chl501 pushed a commit to branch componentization
in repository https://gitbox.apache.org/repos/asf/hama.git
The following commit(s) were added to refs/heads/componentization by this push:
new 036be18 Add serialization trait.
036be18 is described below
commit 036be18898cf5c0035b6346a4fd2b2e767dfb728
Author: Chiahung Lin <[email protected]>
AuthorDate: Sun May 19 16:29:17 2019 +0200
Add serialization trait.
---
build.sbt | 15 +++---
build.sbt => io/build.sbt | 26 +---------
.../scala/org/apache/hama/io/serialization.scala | 56 ++++++++++++++++++++++
3 files changed, 63 insertions(+), 34 deletions(-)
diff --git a/build.sbt b/build.sbt
index 1f27989..21f4f87 100644
--- a/build.sbt
+++ b/build.sbt
@@ -27,16 +27,13 @@ lazy val common = Seq (
)
)
-lazy val commons = (project in file("commons.v2")).settings (
- common
-)
+lazy val commons = (project in file("commons.v2")).settings(common)
-lazy val bsp = (project in file("bsp")).settings (
- common
-)
+lazy val bsp = (project in file("bsp")).settings(common)
+
+lazy val io = (project in file("io")).settings(common)
-lazy val membership = (project in file("membership")).settings (
- common
-) .dependsOn(commons)
+lazy val membership = (project in file("membership")).settings(common).
+ dependsOn(commons)
lazy val core = (project in file(".")).aggregate(commons, bsp, membership)
diff --git a/build.sbt b/io/build.sbt
similarity index 54%
copy from build.sbt
copy to io/build.sbt
index 1f27989..0f1604e 100644
--- a/build.sbt
+++ b/io/build.sbt
@@ -15,28 +15,4 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-lazy val common = Seq (
- version := "0.0.1",
- organization := "org.apache.hama",
- scalaVersion := "2.12.6",
- scalacOptions := Seq ("-deprecation", "-Ypartial-unification"),
- libraryDependencies ++= Seq (
- "org.typelevel" %% "cats-core" % "1.6.0",
- "org.scalaz" %% "scalaz-zio" % "1.0-RC4",
- "org.scalatest" %% "scalatest" % "3.0.5" % "test"
- )
-)
-
-lazy val commons = (project in file("commons.v2")).settings (
- common
-)
-
-lazy val bsp = (project in file("bsp")).settings (
- common
-)
-
-lazy val membership = (project in file("membership")).settings (
- common
-) .dependsOn(commons)
-
-lazy val core = (project in file(".")).aggregate(commons, bsp, membership)
+name := "io"
diff --git a/io/src/main/scala/org/apache/hama/io/serialization.scala
b/io/src/main/scala/org/apache/hama/io/serialization.scala
new file mode 100644
index 0000000..6426b14
--- /dev/null
+++ b/io/src/main/scala/org/apache/hama/io/serialization.scala
@@ -0,0 +1,56 @@
+/**
+ * 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.hama.io
+
+import java.io.DataInput
+import java.io.DataOutput
+import scala.util.Failure
+import scala.util.Success
+import scala.util.Try
+
+trait serialization[T] {
+
+ def read(input: DataInput, clazz: Class[T]): T
+
+ def write(value: T): Either[Throwable, Array[Byte]]
+
+}
+object serialization {
+
+ def apply[T](implicit instance: T) = instance
+
+ implicit val int = new serialization[Int] {
+
+ override def read(input: DataInput, clazz: Class[Int]): Int =
+ input.readInt
+
+ override def write(value: Int): Either[Throwable, Array[Byte]] = Try {
+ val byteStream = new ByteArrayOutputStream
+ val out = new DataOutputStream(byteSteram)
+ out.writeInt(value)
+ (out, byteStream)
+ } match {
+ case Failure(ex) => Left(ex)
+ case Success((out, stream)) =>
+ try {} finally { out.close }
+ Right(stream.toByteArray)
+ }
+
+ }
+
+}