-
Notifications
You must be signed in to change notification settings - Fork 32
/
OrientEdge.java
169 lines (141 loc) · 6.04 KB
/
OrientEdge.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
package org.apache.tinkerpop.gremlin.orientdb;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ORecordElement;
import com.orientechnologies.orient.core.db.record.ridbag.ORidBag;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.record.impl.ODocument;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Property;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.util.StringFactory;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
public class OrientEdge extends OrientElement implements Edge {
private static final List<String> INTERNAL_FIELDS = Arrays.asList("@rid", "@class", "in", "out");
protected OIdentifiable vOut;
protected OIdentifiable vIn;
protected String label;
public OrientEdge(OrientGraph graph, OIdentifiable rawElement, final OIdentifiable out, final OIdentifiable in, final String iLabel) {
super(graph, rawElement);
vOut = checkNotNull(out);
vIn = checkNotNull(in);
label = checkNotNull(iLabel);
}
public OrientEdge(OrientGraph graph, String className, final OIdentifiable out, final OIdentifiable in, final String iLabel) {
this(graph, createRawElement(graph, className), out, in, iLabel);
}
public OrientEdge(OrientGraph graph, final OIdentifiable out, final OIdentifiable in, final String iLabel) {
this(graph, (OIdentifiable) null, out, in, iLabel);
}
public OrientEdge(OrientGraph graph, ODocument rawDocument, String label) {
this(graph, rawDocument, rawDocument.field("out", OIdentifiable.class), rawDocument.field("in", OIdentifiable.class), label);
}
public OrientEdge(OrientGraph graph, ODocument rawDocument)
{
this(graph, rawDocument, rawDocument.getClassName());
}
public static OIdentifiable getConnection(final ODocument iEdgeRecord, final Direction iDirection) {
return iEdgeRecord.rawField(iDirection == Direction.OUT ? OrientGraphUtils.CONNECTION_OUT : OrientGraphUtils.CONNECTION_IN);
}
protected static ODocument createRawElement(OrientGraph graph, String className) {
graph.createEdgeClass(className);
return new ODocument(className);
}
@Override
public Iterator<Vertex> vertices(Direction direction) {
switch (direction)
{
case OUT:
return graph.vertices(vOut.getIdentity());
case IN:
return graph.vertices(vIn.getIdentity());
case BOTH:
default:
return graph.vertices(vOut.getIdentity(), vIn.getIdentity());
}
}
public <V> Iterator<Property<V>> properties(final String... propertyKeys) {
Iterator<? extends Property<V>> properties = super.properties(propertyKeys);
return StreamUtils.asStream(properties).filter(p ->
!INTERNAL_FIELDS.contains(p.key()) ).map(p ->
(Property<V>) p
).iterator();
}
public OrientVertex getVertex(final Direction direction) {
if (direction.equals(Direction.OUT))
return new OrientVertex(graph, getOutVertex());
else if (direction.equals(Direction.IN))
return new OrientVertex(graph, getInVertex());
else
throw new IllegalArgumentException("direction " + direction + " is not supported!");
}
public void remove() {
ODocument doc = getRawDocument();
if (doc.getInternalStatus() == ORecordElement.STATUS.NOT_LOADED)
doc.load();
removeLink(Direction.IN, vIn);
removeLink(Direction.OUT, vOut);
ORID id = id();
doc.getDatabase().delete(id);
rawElement = new RemovedOIdentifiable(id);
}
@SuppressWarnings("unchecked")
private void removeLink( Direction direction, OIdentifiable rawVertex ) {
final String fieldName = OrientVertex.getConnectionFieldName(direction, this.label());
ODocument doc;
if (rawVertex instanceof ODocument) {
doc = (ODocument) rawVertex;
} else {
doc = this.getVertex(direction).getRawDocument();
}
Object found = doc.field(fieldName);
if(found instanceof ORidBag) {
ORidBag bag = (ORidBag)found;
bag.remove(this.getRawElement());
if(bag.size() == 0) doc.removeField(fieldName);
}
else if (found instanceof Collection<?>) {
((Collection<Object>) found).remove(this.getRawElement());
if(((Collection<Object>) found).isEmpty()) doc.removeField(fieldName);
} else
throw new IllegalStateException("Relationship content is invalid on field " + fieldName + ". Found: " + found);
doc.save();
}
public OIdentifiable getOutVertex() {
if (vOut != null)
// LIGHTWEIGHT EDGE
return vOut;
final ODocument doc = getRawDocument();
if (doc == null)
return null;
// if (settings != null && settings.isKeepInMemoryReferences())
// AVOID LAZY RESOLVING+SETTING OF RECORD
// return doc.rawField(OrientGraphUtils.CONNECTION_OUT);
// else
return doc.field(OrientGraphUtils.CONNECTION_OUT);
}
/**
* (Blueprints Extension) Returns the incoming vertex in form of record.
*/
public OIdentifiable getInVertex() {
if (vIn != null)
// LIGHTWEIGHT EDGE
return vIn;
final ODocument doc = getRawDocument();
if (doc == null)
return null;
// if (settings != null && settings.isKeepInMemoryReferences())
// AVOID LAZY RESOLVING+SETTING OF RECORD
// return doc.rawField(OrientGraphUtils.CONNECTION_IN);
// else
return doc.field(OrientGraphUtils.CONNECTION_IN);
}
@Override
public String toString() {
return StringFactory.edgeString(this);
}
}