This project has retired. For details please refer to its
Attic page.
JythonWritableWrapper xref
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 private static final Logger LOG =
42 Logger.getLogger(JythonWritableWrapper.class);
43
44
45
46
47
48
49 public JythonWritableWrapper(PyObject pyObject) {
50 super(pyObject);
51 }
52
53 @Override
54 public void readFields(DataInput in) throws IOException {
55 String str = WritableUtils.readString(in);
56 PyString pyString = new PyString(str);
57 Object object;
58 try {
59 object = cPickle.loads(pyString);
60 } catch (PyException e) {
61 LOG.fatal("Could not deserialize Jython value from string " + str);
62 throw e;
63 }
64 Preconditions.checkArgument(object instanceof PyObject);
65 setPyObject((PyObject) object);
66 }
67
68 @Override
69 public void write(DataOutput out) throws IOException {
70 PyString pyString;
71 try {
72 pyString = cPickle.dumps(getPyObject());
73 } catch (PyException e) {
74 LOG.fatal("Could not serialize wrapped Jython value: " +
75 getPyObject().__str__());
76 throw e;
77 }
78 WritableUtils.writeString(out, pyString.getString());
79 }
80
81 @Override
82 public int compareTo(Object other) {
83 int result = -1;
84 if (other instanceof PyObject) {
85 PyObject pyOther = (PyObject) other;
86 result = getPyObject().__cmp__(pyOther);
87 } else if (JythonWritableWrapper.class.equals(other.getClass())) {
88 JythonWritableWrapper wrapperOther = (JythonWritableWrapper) other;
89 result = getPyObject().__cmp__(wrapperOther.getPyObject());
90 }
91 return result;
92 }
93
94 @Override
95 public boolean equals(Object obj) {
96 if (obj == null) {
97 return false;
98 }
99 if (this == obj) {
100 return true;
101 }
102 if (obj instanceof JythonWritableWrapper) {
103 return compareTo(obj) == 0;
104 }
105 return false;
106 }
107
108 @Override
109 public int hashCode() {
110 return getPyObject().__hash__().asInt();
111 }
112 }