1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.giraph.jython.wrappers; |
19 | |
|
20 | |
import org.apache.hadoop.io.WritableComparable; |
21 | |
import org.apache.hadoop.io.WritableUtils; |
22 | |
import org.apache.log4j.Logger; |
23 | |
import org.python.core.PyException; |
24 | |
import org.python.core.PyObject; |
25 | |
import org.python.core.PyString; |
26 | |
import org.python.modules.cPickle; |
27 | |
|
28 | |
import com.google.common.base.Preconditions; |
29 | |
|
30 | |
import java.io.DataInput; |
31 | |
import java.io.DataOutput; |
32 | |
import java.io.IOException; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class JythonWritableWrapper extends JythonWrapperBase |
39 | |
implements WritableComparable { |
40 | |
|
41 | 0 | private static final Logger LOG = |
42 | 0 | Logger.getLogger(JythonWritableWrapper.class); |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public JythonWritableWrapper(PyObject pyObject) { |
50 | 0 | super(pyObject); |
51 | 0 | } |
52 | |
|
53 | |
@Override |
54 | |
public void readFields(DataInput in) throws IOException { |
55 | 0 | String str = WritableUtils.readString(in); |
56 | 0 | PyString pyString = new PyString(str); |
57 | |
Object object; |
58 | |
try { |
59 | 0 | object = cPickle.loads(pyString); |
60 | 0 | } catch (PyException e) { |
61 | 0 | LOG.fatal("Could not deserialize Jython value from string " + str); |
62 | 0 | throw e; |
63 | 0 | } |
64 | 0 | Preconditions.checkArgument(object instanceof PyObject); |
65 | 0 | setPyObject((PyObject) object); |
66 | 0 | } |
67 | |
|
68 | |
@Override |
69 | |
public void write(DataOutput out) throws IOException { |
70 | |
PyString pyString; |
71 | |
try { |
72 | 0 | pyString = cPickle.dumps(getPyObject()); |
73 | 0 | } catch (PyException e) { |
74 | 0 | LOG.fatal("Could not serialize wrapped Jython value: " + |
75 | 0 | getPyObject().__str__()); |
76 | 0 | throw e; |
77 | 0 | } |
78 | 0 | WritableUtils.writeString(out, pyString.getString()); |
79 | 0 | } |
80 | |
|
81 | |
@Override |
82 | |
public int compareTo(Object other) { |
83 | 0 | int result = -1; |
84 | 0 | if (other instanceof PyObject) { |
85 | 0 | PyObject pyOther = (PyObject) other; |
86 | 0 | result = getPyObject().__cmp__(pyOther); |
87 | 0 | } else if (JythonWritableWrapper.class.equals(other.getClass())) { |
88 | 0 | JythonWritableWrapper wrapperOther = (JythonWritableWrapper) other; |
89 | 0 | result = getPyObject().__cmp__(wrapperOther.getPyObject()); |
90 | |
} |
91 | 0 | return result; |
92 | |
} |
93 | |
|
94 | |
@Override |
95 | |
public boolean equals(Object obj) { |
96 | 0 | if (obj == null) { |
97 | 0 | return false; |
98 | |
} |
99 | 0 | if (this == obj) { |
100 | 0 | return true; |
101 | |
} |
102 | 0 | if (obj instanceof JythonWritableWrapper) { |
103 | 0 | return compareTo(obj) == 0; |
104 | |
} |
105 | 0 | return false; |
106 | |
} |
107 | |
|
108 | |
@Override |
109 | |
public int hashCode() { |
110 | 0 | return getPyObject().__hash__().asInt(); |
111 | |
} |
112 | |
} |