This project has retired. For details please refer to its Attic page.
JythonGiraphComputation 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;
19  
20  import org.apache.giraph.graph.AbstractComputation;
21  import org.apache.giraph.graph.GraphType;
22  import org.apache.giraph.graph.Language;
23  import org.apache.giraph.graph.Vertex;
24  import org.apache.giraph.jython.wrappers.JythonWritableWrapper;
25  import org.apache.hadoop.io.Writable;
26  import org.apache.hadoop.io.WritableComparable;
27  import org.python.core.PyObject;
28  
29  import com.google.common.base.Preconditions;
30  
31  import java.io.IOException;
32  
33  /**
34   * The {@link org.apache.giraph.graph.Computation} class for using
35   * Jython with Giraph. This class implements the Giraph necessary
36   * interfaces but it actually holds a reference to the
37   * {@link JythonComputation} which does the real work.
38   *
39   * The two classes are linked and together they allow us to coerce Jython types
40   * to Writables.
41   *
42   * @param <I> Vertex ID
43   * @param <V> Vertex value
44   * @param <E> Edge value
45   * @param <M1> Incoming message value
46   * @param <M2> Outgoing message value
47   */
48  public class JythonGiraphComputation<I extends WritableComparable,
49      V extends Writable, E extends Writable, M1 extends Writable,
50      M2 extends Writable>
51      extends AbstractComputation<I, V, E, M1, M2> {
52    /** The user's computation class */
53    private final JythonComputation jythonComputation;
54  
55    /**
56     * Constructor
57     *
58     * @param jythonComputation the user's Jython computation
59     */
60    public JythonGiraphComputation(JythonComputation jythonComputation) {
61      this.jythonComputation = jythonComputation;
62    }
63  
64    @Override public void compute(Vertex<I, V, E> vertex, Iterable<M1> messages)
65      throws IOException {
66      jythonComputation.compute(vertex, messages);
67    }
68  
69    /**
70     * Wrap a vertex id in a {@link WritableComparable} wrapper if necessary
71     *
72     * @param object data to wrap
73     * @return writable value
74     */
75    public WritableComparable wrapIdIfNecessary(Object object) {
76      return wrapIfNecessary(object, GraphType.VERTEX_ID);
77    }
78  
79    /**
80     * Wrap a user value (IVEMM) in a {@link Writable} wrapper if necessary
81     *
82     * @param object data to wrap
83     * @param graphType type of data (IVEMM)
84     * @param <W> writable type
85     * @return writable value
86     */
87    public <W extends Writable> W
88    wrapIfNecessary(Object object, GraphType graphType) {
89      if (graphType.interfaceClass().isInstance(object)) {
90        return (W) object;
91      }
92      if (getConf().getValueLanguages().get(graphType) == Language.JYTHON &&
93          getConf().getValueNeedsWrappers().get(graphType)) {
94        Preconditions.checkArgument(object instanceof PyObject);
95        return (W) new JythonWritableWrapper((PyObject) object);
96      } else {
97        return (W) object;
98      }
99    }
100 }