This repository was archived by the owner on Aug 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathAbstractGraphCommitJob.java
More file actions
143 lines (116 loc) · 5.06 KB
/
AbstractGraphCommitJob.java
File metadata and controls
143 lines (116 loc) · 5.06 KB
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
package org.lab41.dendrite.jobs;
import com.thinkaurelius.titan.core.TitanKey;
import com.thinkaurelius.titan.core.TitanLabel;
import com.thinkaurelius.titan.core.TitanProperty;
import com.thinkaurelius.titan.core.TitanTransaction;
import com.thinkaurelius.titan.graphdb.types.TypeAttribute;
import com.thinkaurelius.titan.graphdb.types.system.SystemKey;
import com.thinkaurelius.titan.graphdb.types.vertices.TitanKeyVertex;
import com.thinkaurelius.titan.graphdb.types.vertices.TitanLabelVertex;
import com.thinkaurelius.titan.graphdb.types.vertices.TitanTypeVertex;
import org.lab41.dendrite.metagraph.DendriteGraph;
import org.lab41.dendrite.metagraph.DendriteGraphTx;
import org.lab41.dendrite.metagraph.MetaGraph;
import org.lab41.dendrite.metagraph.MetaGraphTx;
import org.lab41.dendrite.metagraph.models.BranchMetadata;
import org.lab41.dendrite.metagraph.models.GraphMetadata;
import org.lab41.dendrite.metagraph.models.JobMetadata;
import org.lab41.dendrite.metagraph.models.ProjectMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class AbstractGraphCommitJob extends AbstractJob<Void> {
private Logger logger = LoggerFactory.getLogger(AbstractGraphCommitJob.class);
ProjectMetadata.Id projectId;
BranchMetadata.Id branchId;
GraphMetadata.Id srcGraphId;
GraphMetadata.Id dstGraphId;
protected AbstractGraphCommitJob(MetaGraph metaGraph,
JobMetadata.Id jobId,
ProjectMetadata.Id projectId,
BranchMetadata.Id branchId,
GraphMetadata.Id srcGraphId,
GraphMetadata.Id dstGraphId) {
super(metaGraph, jobId);
this.projectId = projectId;
this.branchId = branchId;
this.srcGraphId = srcGraphId;
this.dstGraphId = dstGraphId;
}
public ProjectMetadata.Id getProjectId() {
return projectId;
}
public BranchMetadata.Id getBranchId() {
return branchId;
}
public GraphMetadata.Id getSrcGraphId() {
return srcGraphId;
}
public GraphMetadata.Id getDstGraphId() {
return dstGraphId;
}
@Override
public Void call() {
logger.debug("Starting commit on "
+ srcGraphId
+ " to "
+ dstGraphId
+ " job " + jobId
+ " " + Thread.currentThread().getName());
setJobState(jobId, JobMetadata.RUNNING);
DendriteGraph srcGraph = metaGraph.getGraph(srcGraphId);
DendriteGraph dstGraph = metaGraph.getGraph(dstGraphId);
try {
copyIndices(srcGraph, dstGraph);
copyGraph(srcGraph, dstGraph);
// Update the branch to point the new graph.
MetaGraphTx tx = metaGraph.newTransaction();
try {
BranchMetadata branchMetadata = tx.getBranch(branchId);
branchMetadata.setGraph(tx.getGraph(dstGraph.getId()));
tx.commit();
} catch (Throwable t) {
tx.rollback();
throw t;
}
setJobState(jobId, JobMetadata.DONE);
} catch (Throwable t) {
setJobState(jobId, JobMetadata.ERROR, t.getMessage());
logger.error("error running job: " + t.getMessage());
return null;
}
logger.debug("finished job: " + jobId);
return null;
}
protected void copyIndices(DendriteGraph srcGraph, DendriteGraph dstGraph) {
// This is very much a hack, but unfortunately Titan does not yet expose a proper way to copy indices from
// one graph to another.
TitanTransaction srcTx = srcGraph.newTransaction();
DendriteGraphTx dstTx = dstGraph.newTransaction();
for(TitanKey titanKey: srcTx.getTypes(TitanKey.class)) {
if (titanKey instanceof TitanKeyVertex) {
TitanKeyVertex keyVertex = (TitanKeyVertex) titanKey;
TypeAttribute.Map definition = getDefinition(keyVertex);
if (dstTx.getType(keyVertex.getName()) == null) {
dstTx.makePropertyKey(keyVertex.getName(), definition);
}
}
}
for(TitanLabel titanLabel: srcTx.getTypes(TitanLabel.class)) {
TitanLabelVertex keyVertex = (TitanLabelVertex) titanLabel;
TypeAttribute.Map definition = getDefinition(keyVertex);
if (dstTx.getType(keyVertex.getName()) == null) {
dstTx.makeEdgeLabel(keyVertex.getName(), definition);
}
}
dstTx.commit();
srcTx.commit();
}
protected abstract void copyGraph(DendriteGraph srcGraph, DendriteGraph dstGraph);
protected TypeAttribute.Map getDefinition(TitanTypeVertex vertex) {
TypeAttribute.Map definition = new TypeAttribute.Map();
for (TitanProperty p: vertex.query().includeHidden().type(SystemKey.TypeDefinition).properties()) {
definition.add(p.getValue(TypeAttribute.class));
}
return definition;
}
}