1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
36
37
38
39
40 public abstract class JythonFactoryBase<W extends Writable>
41 implements ValueFactory<W>, GiraphConfigurationSettable {
42
43 private static final Logger LOG = Logger.getLogger(JythonFactoryBase.class);
44
45
46 private String jythonClassName;
47
48 private boolean useWrapper;
49
50
51
52
53
54
55 public abstract JythonOptions.JythonGraphTypeOptions getOptions();
56
57
58
59
60
61
62 public GraphType getGraphType() {
63 return getOptions().getGraphType();
64 }
65
66
67
68
69
70
71
72 public StrConfOption jythonClassNameOption() {
73 return getOptions().getJythonClassNameOption();
74 }
75
76
77
78
79
80
81
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
96
97
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
111
112
113
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 }