This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
/
MarvelGraphFactory.java
337 lines (294 loc) · 13.4 KB
/
MarvelGraphFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazon.titan.example;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import au.com.bytecode.opencsv.CSVReader;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricRegistry;
import com.thinkaurelius.titan.core.Multiplicity;
import com.thinkaurelius.titan.core.PropertyKey;
import com.thinkaurelius.titan.core.TitanGraph;
import com.thinkaurelius.titan.core.schema.TitanManagement;
import com.thinkaurelius.titan.util.stats.MetricManager;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Vertex;
/**
*
* @author Matthew Sowders
* @author Alexander Patrikalakis
*
*/
public class MarvelGraphFactory {
private static final SecureRandom RANDOM = new SecureRandom();
private static final int BATCH_SIZE = 10;
private static final Logger LOG = LoggerFactory.getLogger(MarvelGraphFactory.class);
public static final String APPEARED = "appeared";
public static final String COMIC_BOOK = "comic-book";
public static final String CHARACTER = "character";
public static final String WEAPON = "weapon";
public static final MetricRegistry REGISTRY = MetricManager.INSTANCE.getRegistry();
public static final ConsoleReporter REPORTER = ConsoleReporter.forRegistry(REGISTRY).build();
private static final String TIMER_LINE = "MarvelGraphFactory.line";
private static final String TIMER_CREATE = "MarvelGraphFactory.create_";
private static final String COUNTER_GET = "MarvelGraphFactory.get_";
private static final String[] WEAPONS = { "claws", "ring", "shield", "robotic suit", "cards", "surf board", "glider", "gun", "swords", "lasso" };
private static final AtomicInteger COMPLETED_TASK_COUNT = new AtomicInteger(0);
private static final int POOL_SIZE = 10;
public static void load(final TitanGraph graph, final int rowsToLoad, final boolean report) throws Exception {
TitanManagement mgmt = graph.openManagement();
if (mgmt.getGraphIndex(CHARACTER) == null) {
final PropertyKey characterKey = mgmt.makePropertyKey(CHARACTER).dataType(String.class).make();
mgmt.buildIndex(CHARACTER, Vertex.class).addKey(characterKey).unique().buildCompositeIndex();
}
if (mgmt.getGraphIndex(COMIC_BOOK) == null) {
final PropertyKey comicBookKey = mgmt.makePropertyKey(COMIC_BOOK).dataType(String.class).make();
mgmt.buildIndex(COMIC_BOOK, Vertex.class).addKey(comicBookKey).unique().buildCompositeIndex();
mgmt.makePropertyKey(WEAPON).dataType(String.class).make();
mgmt.makeEdgeLabel(APPEARED).multiplicity(Multiplicity.MULTI).make();
}
mgmt.commit();
ClassLoader classLoader = MarvelGraphFactory.class.getClassLoader();
URL resource = classLoader.getResource("META-INF/marvel.csv");
int line = 0;
Map<String, Set<String>> comicToCharacter = new HashMap<>();
Map<String, Set<String>> characterToComic = new HashMap<>();
Set<String> characters = new HashSet<>();
BlockingQueue<Runnable> creationQueue = new LinkedBlockingQueue<>();
try (CSVReader reader = new CSVReader(new InputStreamReader(resource.openStream()))) {
String[] nextLine;
while ((nextLine = reader.readNext()) != null && line < rowsToLoad) {
line++;
String comicBook = nextLine[1];
String[] characterNames = nextLine[0].split("/");
if (!comicToCharacter.containsKey(comicBook)) {
comicToCharacter.put(comicBook, new HashSet<String>());
}
List<String> comicCharacters = Arrays.asList(characterNames);
comicToCharacter.get(comicBook).addAll(comicCharacters);
characters.addAll(comicCharacters);
}
}
for (String character : characters) {
creationQueue.add(new CharacterCreationCommand(graph, character));
}
BlockingQueue<Runnable> appearedQueue = new LinkedBlockingQueue<>();
for (String comicBook : comicToCharacter.keySet()) {
creationQueue.add(new ComicBookCreationCommand(graph, comicBook));
Set<String> comicCharacters = comicToCharacter.get(comicBook);
for (String character : comicCharacters) {
AppearedCommand lineCommand = new AppearedCommand(graph, new Appeared(character, comicBook));
appearedQueue.add(lineCommand);
if (!characterToComic.containsKey(character)) {
characterToComic.put(character, new HashSet<String>());
}
characterToComic.get(character).add(comicBook);
}
REGISTRY.histogram("histogram.comic-to-character").update(comicCharacters.size());
}
int maxAppearances = 0;
String maxCharacter = "";
for (String character : characterToComic.keySet()) {
Set<String> comicBookSet = characterToComic.get(character);
int numberOfAppearances = comicBookSet.size();
REGISTRY.histogram("histogram.character-to-comic").update(numberOfAppearances);
if (numberOfAppearances > maxAppearances) {
maxCharacter = character;
maxAppearances = numberOfAppearances;
}
}
LOG.info("Character {} has most appearances at {}", maxCharacter, maxAppearances);
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
for (int i = 0; i < POOL_SIZE; i++) {
executor.execute(new BatchCommand(graph, creationQueue));
}
executor.shutdown();
while (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
LOG.info("Awaiting:" + creationQueue.size());
if(report) {
REPORTER.report();
}
}
executor = Executors.newSingleThreadExecutor();
executor.execute(new BatchCommand(graph, appearedQueue));
executor.shutdown();
while (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
LOG.info("Awaiting:" + appearedQueue.size());
if(report) {
REPORTER.report();
}
}
LOG.info("MarvelGraphFactory.load complete");
}
public static class Appeared {
final String character;
final String comicBook;
public Appeared(String character, String comicBook) {
this.character = character;
this.comicBook = comicBook;
}
public String getCharacter() {
return character;
}
public String getComicBook() {
return comicBook;
}
}
public static class BatchCommand implements Runnable {
final TitanGraph graph;
final BlockingQueue<Runnable> commands;
public BatchCommand(TitanGraph graph, BlockingQueue<Runnable> commands) {
this.commands = commands;
this.graph = graph;
}
@Override
public void run() {
int i = 0;
Runnable command = null;
while ((command = commands.poll()) != null) {
try {
command.run();
} catch (Throwable e) {
Throwable rootCause = ExceptionUtils.getRootCause(e);
String rootCauseMessage = null == rootCause ? "" : rootCause.getMessage();
LOG.error("Error processing comic book {} {}", e.getMessage(), rootCauseMessage, e);
}
if (i++ % BATCH_SIZE == 0) {
try {
graph.tx().commit();
} catch (Throwable e) {
LOG.error("error processing commit {} {}", e.getMessage(), ExceptionUtils.getRootCause(e).getMessage());
}
}
}
try {
graph.tx().commit();
} catch (Throwable e) {
LOG.error("error processing commit {} {}", e.getMessage(), ExceptionUtils.getRootCause(e).getMessage());
}
}
}
public static class ComicBookCreationCommand implements Runnable {
final String comicBook;
final TitanGraph graph;
public ComicBookCreationCommand(final TitanGraph graph, final String comicBook) {
this.comicBook = comicBook;
this.graph = graph;
}
@Override
public void run() {
createComicBook(graph, comicBook);
}
private static Vertex createComicBook(TitanGraph graph, String value) {
long start = System.currentTimeMillis();
Vertex vertex = graph.addVertex();
vertex.property(COMIC_BOOK, value);
REGISTRY.counter(COUNTER_GET + COMIC_BOOK).inc();
long end = System.currentTimeMillis();
long time = end - start;
REGISTRY.timer(TIMER_CREATE + COMIC_BOOK).update(time, TimeUnit.MILLISECONDS);
return vertex;
}
}
public static class CharacterCreationCommand implements Runnable {
final String character;
final TitanGraph graph;
public CharacterCreationCommand(final TitanGraph graph, final String character) {
this.character = character;
this.graph = graph;
}
@Override
public void run() {
createCharacter(graph, character);
}
private static Vertex createCharacter(TitanGraph graph, String value) {
long start = System.currentTimeMillis();
Vertex vertex = graph.addVertex();
vertex.property(CHARACTER, value);
// only sets weapon on character vertex on initial creation.
vertex.property(WEAPON, WEAPONS[RANDOM.nextInt(WEAPONS.length)]);
REGISTRY.counter(COUNTER_GET + CHARACTER).inc();
long end = System.currentTimeMillis();
long time = end - start;
REGISTRY.timer(TIMER_CREATE + CHARACTER).update(time, TimeUnit.MILLISECONDS);
return vertex;
}
}
public static class AppearedCommand implements Runnable {
final TitanGraph graph;
final Appeared appeared;
public AppearedCommand(final TitanGraph graph, Appeared appeared) {
this.graph = graph;
this.appeared = appeared;
}
@Override
public void run() {
try {
processLine(graph, appeared);
} catch (Throwable e) {
Throwable rootCause = ExceptionUtils.getRootCause(e);
String rootCauseMessage = null == rootCause ? "" : rootCause.getMessage();
LOG.error("Error processing line {} {}", e.getMessage(), rootCauseMessage, e);
} finally {
COMPLETED_TASK_COUNT.incrementAndGet();
}
}
}
protected static void processLine(final TitanGraph graph, Appeared appeared) {
long start = System.currentTimeMillis();
process(graph, appeared);
long end = System.currentTimeMillis();
long time = end - start;
REGISTRY.timer(TIMER_LINE).update(time, TimeUnit.MILLISECONDS);
}
private static void process(TitanGraph graph, Appeared appeared) {
Vertex comicBookVertex = get(graph, COMIC_BOOK, appeared.getComicBook());
if (null == comicBookVertex) {
REGISTRY.counter("error.missingComicBook." + appeared.getComicBook()).inc();
comicBookVertex = graph.addVertex();
comicBookVertex.property(COMIC_BOOK, appeared.getComicBook());
}
Vertex characterVertex = get(graph, CHARACTER, appeared.getCharacter());
if (null == characterVertex) {
REGISTRY.counter("error.missingCharacter." + appeared.getCharacter()).inc();
characterVertex = graph.addVertex();
characterVertex.property(CHARACTER, appeared.getCharacter());
characterVertex.property(WEAPON, WEAPONS[RANDOM.nextInt(WEAPONS.length)]);
}
characterVertex.addEdge(APPEARED, comicBookVertex);
}
private static Vertex get(TitanGraph graph, String key, String value) {
final GraphTraversalSource g = graph.traversal();
final Iterator<Vertex> it = g.V().has(key, value);
return it.hasNext() ? it.next() : null;
}
}