This project has retired. For details please refer to its Attic page.
JythonFactoryBase 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.factories;
19  
20  import org.apache.giraph.conf.ClassConfOption;
21  import org.apache.giraph.conf.GiraphConfigurationSettable;
22  import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
23  import org.apache.giraph.conf.StrConfOption;
24  import org.apache.giraph.factories.ValueFactory;
25  import org.apache.giraph.graph.GraphType;
26  import org.apache.giraph.jython.JythonOptions;
27  import org.apache.giraph.jython.JythonUtils;
28  import org.apache.giraph.jython.wrappers.JythonWritableWrapper;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.io.Writable;
31  import org.apache.log4j.Logger;
32  import org.python.core.PyObject;
33  
34  /**
35   * Base class for Jython factories. These factories are used to create user's
36   * graph value types (IVEMM).
37   *
38   * @param <W> writable type
39   */
40  public abstract class JythonFactoryBase<W extends Writable>
41      implements ValueFactory<W>, GiraphConfigurationSettable {
42    /** Logger */
43    private static final Logger LOG = Logger.getLogger(JythonFactoryBase.class);
44  
45    /** Name of class in Jython implementing Vertex ID */
46    private String jythonClassName;
47    /** Whether the Jython type needs a wrapper */
48    private boolean useWrapper;
49  
50    /**
51     * Get the options associated with this graph type
52     *
53     * @return options
54     */
55    public abstract JythonOptions.JythonGraphTypeOptions getOptions();
56  
57    /**
58     * Get the graph type this factory is meant for
59     *
60     * @return GraphType
61     */
62    public GraphType getGraphType() {
63      return getOptions().getGraphType();
64    }
65  
66    /**
67     * The {@link org.apache.hadoop.conf.Configuration} option for setting the
68     * Jython class name in this factory implementation.
69     *
70     * @return {@link org.apache.giraph.conf.StrConfOption}
71     */
72    public StrConfOption jythonClassNameOption() {
73      return getOptions().getJythonClassNameOption();
74    }
75  
76    /**
77     * The interface class for the value type.
78     * For Vertex ID this is {@link org.apache.hadoop.io.WritableComparable}, for
79     * others this is {@link org.apache.hadoop.io.Writable}
80     *
81     * @return interface class for the value type
82     */
83    public Class<? extends Writable> writableValueClass() {
84      return getGraphType().interfaceClass();
85    }
86  
87    @Override
88    public void setConf(
89        ImmutableClassesGiraphConfiguration conf) {
90      jythonClassName = jythonClassNameOption().get(conf);
91      useWrapper = conf.getValueNeedsWrappers().get(getGraphType());
92    }
93  
94    /**
95     * Instantiate a new value Jython object.
96     *
97     * @return new value object
98     */
99    public Writable newJythonClassInstance() {
100     if (useWrapper) {
101       PyObject jythonObj = JythonUtils.newInstance(jythonClassName);
102       JythonWritableWrapper wrapper = new JythonWritableWrapper(jythonObj);
103       return wrapper;
104     } else {
105       return JythonUtils.newInstance(jythonClassName, writableValueClass());
106     }
107   }
108 
109   /**
110    * Use this factory in the {@link org.apache.hadoop.conf.Configuration}
111    *
112    * @param conf {@link org.apache.hadoop.conf.Configuration}
113    * @param jythonClassName Name of Jython class implementing value type
114    */
115   public void useThisFactory(Configuration conf, String jythonClassName) {
116     if (LOG.isInfoEnabled()) {
117       LOG.info("useThisFactory: Setting up Jython factory for " +
118           getGraphType() + " reading " + " using Jython type " +
119           jythonClassName);
120     }
121 
122     ClassConfOption factoryOption = getGraphType().factoryClassOption();
123     factoryOption.set(conf, getClass());
124 
125     jythonClassNameOption().set(conf, jythonClassName);
126   }
127 }