|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.api.python |
| 19 | + |
| 20 | +import java.nio.ByteOrder |
| 21 | + |
| 22 | +import scala.collection.JavaConversions._ |
| 23 | +import scala.util.Failure |
| 24 | +import scala.util.Try |
| 25 | + |
| 26 | +import net.razorvine.pickle.{Unpickler, Pickler} |
| 27 | + |
| 28 | +import org.apache.spark.{Logging, SparkException} |
| 29 | +import org.apache.spark.rdd.RDD |
| 30 | + |
| 31 | +/** Utilities for serialization / deserialization between Python and Java, using Pickle. */ |
| 32 | +private[python] object SerDeUtil extends Logging { |
| 33 | + // Unpickle array.array generated by Python 2.6 |
| 34 | + class ArrayConstructor extends net.razorvine.pickle.objects.ArrayConstructor { |
| 35 | + // /* Description of types */ |
| 36 | + // static struct arraydescr descriptors[] = { |
| 37 | + // {'c', sizeof(char), c_getitem, c_setitem}, |
| 38 | + // {'b', sizeof(char), b_getitem, b_setitem}, |
| 39 | + // {'B', sizeof(char), BB_getitem, BB_setitem}, |
| 40 | + // #ifdef Py_USING_UNICODE |
| 41 | + // {'u', sizeof(Py_UNICODE), u_getitem, u_setitem}, |
| 42 | + // #endif |
| 43 | + // {'h', sizeof(short), h_getitem, h_setitem}, |
| 44 | + // {'H', sizeof(short), HH_getitem, HH_setitem}, |
| 45 | + // {'i', sizeof(int), i_getitem, i_setitem}, |
| 46 | + // {'I', sizeof(int), II_getitem, II_setitem}, |
| 47 | + // {'l', sizeof(long), l_getitem, l_setitem}, |
| 48 | + // {'L', sizeof(long), LL_getitem, LL_setitem}, |
| 49 | + // {'f', sizeof(float), f_getitem, f_setitem}, |
| 50 | + // {'d', sizeof(double), d_getitem, d_setitem}, |
| 51 | + // {'\0', 0, 0, 0} /* Sentinel */ |
| 52 | + // }; |
| 53 | + // TODO: support Py_UNICODE with 2 bytes |
| 54 | + // FIXME: unpickle array of float is wrong in Pyrolite, so we reverse the |
| 55 | + // machine code for float/double here to workaround it. |
| 56 | + // we should fix this after Pyrolite fix them |
| 57 | + val machineCodes: Map[Char, Int] = if (ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN)) { |
| 58 | + Map('c' -> 1, 'B' -> 0, 'b' -> 1, 'H' -> 3, 'h' -> 5, 'I' -> 7, 'i' -> 9, |
| 59 | + 'L' -> 11, 'l' -> 13, 'f' -> 14, 'd' -> 16, 'u' -> 21 |
| 60 | + ) |
| 61 | + } else { |
| 62 | + Map('c' -> 1, 'B' -> 0, 'b' -> 1, 'H' -> 2, 'h' -> 4, 'I' -> 6, 'i' -> 8, |
| 63 | + 'L' -> 10, 'l' -> 12, 'f' -> 15, 'd' -> 17, 'u' -> 20 |
| 64 | + ) |
| 65 | + } |
| 66 | + override def construct(args: Array[Object]): Object = { |
| 67 | + if (args.length == 1) { |
| 68 | + construct(args ++ Array("")) |
| 69 | + } else if (args.length == 2 && args(1).isInstanceOf[String]) { |
| 70 | + val typecode = args(0).asInstanceOf[String].charAt(0) |
| 71 | + val data: String = args(1).asInstanceOf[String] |
| 72 | + construct(typecode, machineCodes(typecode), data.getBytes("ISO-8859-1")) |
| 73 | + } else { |
| 74 | + super.construct(args) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + def initialize() = { |
| 80 | + Unpickler.registerConstructor("array", "array", new ArrayConstructor()) |
| 81 | + } |
| 82 | +} |
| 83 | + |
0 commit comments