Last active
January 3, 2016 12:19
-
-
Save kmussel/8461886 to your computer and use it in GitHub Desktop.
blueprints Add Edge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Object createLink(final ODocument iFromVertex, | |
final OIdentifiable iTo, final String iFieldName) { | |
final Object out; | |
Object found = iFromVertex.field(iFieldName); | |
if (found == null) | |
// CREATE ONLY ONE LINK | |
out = iTo; | |
else if (found instanceof OIdentifiable) { | |
if (found.equals(iTo)) | |
// SAME LINK, SKIP IT | |
return found; | |
// DOUBLE: SCALE UP THE LINK INTO A COLLECTION | |
out = new OMVRBTreeRIDSet(iFromVertex); | |
((OMVRBTreeRIDSet) out).add((OIdentifiable) found); | |
((OMVRBTreeRIDSet) out).add(iTo); | |
} else if (found instanceof OMVRBTreeRIDSet) { | |
// ADD THE LINK TO THE COLLECTION | |
out = null; | |
((OMVRBTreeRIDSet) found).add(iTo); | |
} else if (found instanceof Collection<?>) { | |
// CONVERT IT IN SET | |
out = new OMVRBTreeRIDSet(((Collection<?>) found).size()); | |
((OMVRBTreeRIDSet) out) | |
.addAll((Collection<? extends OIdentifiable>) found); | |
((OMVRBTreeRIDSet) out).add(iTo); | |
} else | |
throw new IllegalStateException( | |
"Relationship content is invalid on field " + iFieldName | |
+ ". Found: " + found); | |
if (out != null) | |
// OVERWRITE IT | |
iFromVertex.field(iFieldName, out); | |
return out; | |
} | |
public OrientEdge addEdge(String label, final OrientVertex inVertex, | |
final String iClassName, final String iClusterName, | |
final Object... fields) { | |
if (inVertex == null) | |
throw new IllegalArgumentException("destination vertex is null"); | |
graph.autoStartTransaction(); | |
// TEMPORARY STATIC LOCK TO AVOID MT PROBLEMS AGAINST OMVRBTreeRID | |
OMVRBTreeRID.getLock().acquireExclusiveLock(); | |
try { | |
final ODocument outDocument = getRecord(); | |
final ODocument inDocument = ((OrientVertex) inVertex).getRecord(); | |
final OrientEdge edge; | |
OIdentifiable to; | |
OIdentifiable from; | |
label = OrientBaseGraph.encodeClassName(label); | |
if (label == null && iClassName != null) | |
// RETRO-COMPATIBILITY WITH THE SYNTAX CLASS:<CLASS-NAME> | |
label = OrientBaseGraph.encodeClassName(iClassName); | |
final boolean useVertexFieldsForEdgeLabels = graph | |
.isUseVertexFieldsForEdgeLabels(); | |
final String outFieldName = getConnectionFieldName(Direction.OUT, | |
label, useVertexFieldsForEdgeLabels); | |
final String inFieldName = getConnectionFieldName(Direction.IN, | |
label, useVertexFieldsForEdgeLabels); | |
// since the label for the edge can potentially get re-assigned | |
// before being pushed into the OrientEdge, the | |
// null check has to go here. | |
if (label == null) | |
throw ExceptionFactory.edgeLabelCanNotBeNull(); | |
if (canCreateDynamicEdge(outDocument, inDocument, outFieldName, | |
inFieldName, fields, label)) { | |
// CREATE A LIGHTWEIGHT DYNAMIC EDGE | |
from = rawElement; | |
to = inDocument; | |
edge = new OrientEdge(graph, from, to, label); | |
} else { | |
// CREATE THE EDGE DOCUMENT TO STORE FIELDS TOO | |
edge = new OrientEdge(graph, label, fields); | |
if (graph.isKeepInMemoryReferences()) | |
edge.getRecord().fields(OrientBaseGraph.CONNECTION_OUT, | |
rawElement.getIdentity(), | |
OrientBaseGraph.CONNECTION_IN, | |
inDocument.getIdentity()); | |
else | |
edge.getRecord().fields(OrientBaseGraph.CONNECTION_OUT, | |
rawElement, OrientBaseGraph.CONNECTION_IN, | |
inDocument); | |
from = (OIdentifiable) edge.getRecord(); | |
to = (OIdentifiable) edge.getRecord(); | |
} | |
if (graph.isKeepInMemoryReferences()) { | |
// USES REFERENCES INSTEAD OF DOCUMENTS | |
from = from.getIdentity(); | |
to = to.getIdentity(); | |
} | |
// OUT-VERTEX ---> IN-VERTEX/EDGE | |
createLink(outDocument, to, outFieldName); | |
// IN-VERTEX ---> OUT-VERTEX/EDGE | |
createLink(inDocument, from, inFieldName); | |
edge.save(iClusterName); | |
inDocument.save(); | |
outDocument.save(); | |
return edge; | |
} finally { | |
OMVRBTreeRID.getLock().releaseExclusiveLock(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment