This project has retired. For details please refer to its Attic page.
JythonWritableWrapper xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Wraps a Jython object, adding {@link WritableComparable} interface.
36   * Used for graph types (IVEMM) that are in pure Jython.
37   */
38  public class JythonWritableWrapper extends JythonWrapperBase
39      implements WritableComparable {
40    /** Logger */
41    private static final Logger LOG =
42        Logger.getLogger(JythonWritableWrapper.class);
43  
44    /**
45     * Constructor
46     *
47     * @param pyObject jython object to wrap
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 }