Github user andrewor14 commented on a diff in the pull request:
https://github.com/apache/spark/pull/5096#discussion_r28013923
--- Diff: core/src/main/scala/org/apache/spark/api/r/SerDe.scala ---
@@ -0,0 +1,341 @@
+/*
+ * 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.spark.api.r
+
+import java.io.{DataInputStream, DataOutputStream}
+import java.sql.{Date, Time}
+
+import scala.collection.JavaConversions._
+
+/**
+ * Utility functions to serialize, deserialize objects to / from R
+ */
+private[spark] object SerDe {
+
+ // Type mapping from R to Java
+ //
+ // NULL -> void
+ // integer -> Int
+ // character -> String
+ // logical -> Boolean
+ // double, numeric -> Double
+ // raw -> Array[Byte]
+ // Date -> Date
+ // POSIXlt/POSIXct -> Time
+ //
+ // list[T] -> Array[T], where T is one of above mentioned types
+ // environment -> Map[String, T], where T is a native type
+ // jobj -> Object, where jobj is an object created in the backend
+
+ def readObjectType(dis: DataInputStream): Char = {
+ dis.readByte().toChar
+ }
+
+ def readObject(dis: DataInputStream): Object = {
+ val dataType = readObjectType(dis)
+ readTypedObject(dis, dataType)
+ }
+
+ def readTypedObject(
+ dis: DataInputStream,
+ dataType: Char): Object = {
+ dataType match {
+ case 'n' => null
+ case 'i' => new java.lang.Integer(readInt(dis))
+ case 'd' => new java.lang.Double(readDouble(dis))
+ case 'b' => new java.lang.Boolean(readBoolean(dis))
+ case 'c' => readString(dis)
+ case 'e' => readMap(dis)
+ case 'r' => readBytes(dis)
+ case 'l' => readList(dis)
+ case 'D' => readDate(dis)
+ case 't' => readTime(dis)
+ case 'j' => JVMObjectTracker.getObject(readString(dis))
+ case _ => throw new IllegalArgumentException(s"Invalid type
$dataType")
+ }
+ }
+
+ def readBytes(in: DataInputStream): Array[Byte] = {
+ val len = readInt(in)
+ val out = new Array[Byte](len)
+ val bytesRead = in.readFully(out)
+ out
+ }
+
+ def readInt(in: DataInputStream): Int = {
+ in.readInt()
+ }
+
+ def readDouble(in: DataInputStream): Double = {
+ in.readDouble()
+ }
+
+ def readString(in: DataInputStream): String = {
+ val len = in.readInt()
+ val asciiBytes = new Array[Byte](len)
+ in.readFully(asciiBytes)
+ assert(asciiBytes(len - 1) == 0)
+ val str = new String(asciiBytes.dropRight(1).map(_.toChar))
+ str
+ }
+
+ def readBoolean(in: DataInputStream): Boolean = {
+ val intVal = in.readInt()
+ if (intVal == 0) false else true
--- End diff --
can be `intVal != 0`
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]