1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */18package org.apache.giraph.jython.factories;
1920import org.apache.giraph.conf.ClassConfOption;
21import org.apache.giraph.conf.GiraphConfigurationSettable;
22import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
23import org.apache.giraph.conf.StrConfOption;
24import org.apache.giraph.factories.ValueFactory;
25import org.apache.giraph.graph.GraphType;
26import org.apache.giraph.jython.JythonOptions;
27import org.apache.giraph.jython.JythonUtils;
28import org.apache.giraph.jython.wrappers.JythonWritableWrapper;
29import org.apache.hadoop.conf.Configuration;
30import org.apache.hadoop.io.Writable;
31import org.apache.log4j.Logger;
32import org.python.core.PyObject;
3334/**35 * Base class for Jython factories. These factories are used to create user's36 * graph value types (IVEMM).37 *38 * @param <W> writable type39 */40publicabstractclass JythonFactoryBase<W extends Writable>
41implements ValueFactory<W>, GiraphConfigurationSettable {
42/** Logger */43privatestaticfinal Logger LOG = Logger.getLogger(JythonFactoryBase.class);
4445/** Name of class in Jython implementing Vertex ID */46private String jythonClassName;
47/** Whether the Jython type needs a wrapper */48privateboolean useWrapper;
4950/**51 * Get the options associated with this graph type52 *53 * @return options54 */55publicabstract JythonOptions.JythonGraphTypeOptions getOptions();
5657/**58 * Get the graph type this factory is meant for59 *60 * @return GraphType61 */62publicGraphType getGraphType() {
63return getOptions().getGraphType();
64 }
6566/**67 * The {@link org.apache.hadoop.conf.Configuration} option for setting the68 * Jython class name in this factory implementation.69 *70 * @return {@link org.apache.giraph.conf.StrConfOption}71 */72publicStrConfOption jythonClassNameOption() {
73return getOptions().getJythonClassNameOption();
74 }
7576/**77 * The interface class for the value type.78 * For Vertex ID this is {@link org.apache.hadoop.io.WritableComparable}, for79 * others this is {@link org.apache.hadoop.io.Writable}80 *81 * @return interface class for the value type82 */83public Class<? extends Writable> writableValueClass() {
84return getGraphType().interfaceClass();
85 }
8687 @Override
88publicvoid setConf(
89ImmutableClassesGiraphConfiguration conf) {
90 jythonClassName = jythonClassNameOption().get(conf);
91 useWrapper = conf.getValueNeedsWrappers().get(getGraphType());
92 }
9394/**95 * Instantiate a new value Jython object.96 *97 * @return new value object98 */99public Writable newJythonClassInstance() {
100if (useWrapper) {
101 PyObject jythonObj = JythonUtils.newInstance(jythonClassName);
102JythonWritableWrapper wrapper = newJythonWritableWrapper(jythonObj);
103return wrapper;
104 } else {
105return JythonUtils.newInstance(jythonClassName, writableValueClass());
106 }
107 }
108109/**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 type114 */115publicvoid useThisFactory(Configuration conf, String jythonClassName) {
116if (LOG.isInfoEnabled()) {
117 LOG.info("useThisFactory: Setting up Jython factory for " +
118 getGraphType() + " reading " + " using Jython type " +
119 jythonClassName);
120 }
121122ClassConfOption factoryOption = getGraphType().factoryClassOption();
123 factoryOption.set(conf, getClass());
124125 jythonClassNameOption().set(conf, jythonClassName);
126 }
127 }