NebulaGraph Java Client 5.0.0
Loading...
Searching...
No Matches
Edge.java
1package com.vesoft.nebula.driver.graph.data;
2
3import static com.vesoft.nebula.driver.graph.decode.struct.SizeConstant.EDGE_TYPE_ID_SIZE;
4
5import com.vesoft.nebula.driver.graph.decode.struct.ResultGraphSchemas;
6import java.util.ArrayList;
7import java.util.List;
8import java.util.Map;
9import java.util.Objects;
10
11public class Edge extends BaseDataObject {
12
13 private final int graphId;
14 private final String graphName;
15 private final int edgeTypeId;
16 private final String edgeTypeName;
17 private final List<String> labels;
18 private final long rank;
19 private final long srcId;
20 private final long dstId;
21 private final Direction direction;
22 private final Map<String, ValueWrapper> properties;
23
24
28 public Edge(int graphId,
29 int edgeTypeId,
30 long rank,
31 long srcId,
32 long dstId,
33 Map<String, ValueWrapper> properties,
34 ResultGraphSchemas graphSchemas) {
35 this.graphId = graphId;
36 this.graphName = graphSchemas.getGraphSchema(graphId).getGraphName();
37 this.edgeTypeId = edgeTypeId;
38 int noDirectedTypeId = edgeTypeId & 0x3FFFFFFF;
39 this.edgeTypeName = graphSchemas
40 .getGraphSchema(graphId)
41 .getEdgeSchema(noDirectedTypeId)
42 .getEdgeTypeName();
43 this.labels = graphSchemas
44 .getGraphSchema(graphId)
45 .getEdgeSchema(noDirectedTypeId)
46 .getEdgeLabels();
47 this.rank = rank;
48 this.properties = properties;
49
50 int edgeTypeMoveBits = EDGE_TYPE_ID_SIZE * 8 - 2;
51 int directionBits = (edgeTypeId >> edgeTypeMoveBits) & 0x3;
52 switch (directionBits) {
53 case 0x0:
54 this.direction = Direction.OUTGOING;
55 break;
56 case 0x1:
57 this.direction = Direction.INCOMING;
58 break;
59 case 0x2:
60 case 0x3:
61 this.direction = Direction.UNDIRECTED;
62 break;
63 default:
64 this.direction = Direction.KNOWN;
65 }
66 if (this.direction == Direction.INCOMING) {
67 this.srcId = dstId;
68 this.dstId = srcId;
69 } else {
70 this.srcId = srcId;
71 this.dstId = dstId;
72 }
73 }
74
80 public String getGraph() {
81 return graphName;
82 }
83
89 public String getType() {
90 return edgeTypeName;
91 }
92
96 public int getEdgeTypeId() {
97 return edgeTypeId;
98 }
99
105 public boolean isDirected() {
106 return direction == Direction.OUTGOING || direction == Direction.INCOMING;
107 }
108
114 public List<String> getLabels() {
115 return labels;
116 }
117
123 public long getSrcId() {
124 return srcId;
125 }
126
132 public long getDstId() {
133 return dstId;
134 }
135
141 public long getRank() {
142 return rank;
143 }
144
150 public List<String> getColumnNames() {
151 return new ArrayList<>(properties.keySet());
152 }
153
159 public List<ValueWrapper> getPropertyValues() {
160 List<ValueWrapper> values = new ArrayList<>();
161 for (Map.Entry<String, ValueWrapper> kv : properties.entrySet()) {
162 values.add(kv.getValue());
163 }
164 return values;
165 }
166
172 public Map<String, ValueWrapper> getProperties() {
173 return properties;
174 }
175
176 @Override
177 public boolean equals(Object o) {
178 if (this == o) {
179 return true;
180 }
181 if (o == null || getClass() != o.getClass()) {
182 return false;
183 }
184 Edge that = (Edge) o;
185
186 return getRank() == that.getRank()
187 && ((getSrcId() == that.getSrcId() && getDstId() == that.getDstId())
188 || (getSrcId() == that.getDstId() && getDstId() == that.getSrcId()))
189 && Objects.equals(getType(), that.getType());
190 }
191
192 @Override
193 public int hashCode() {
194 return Objects.hash(graphId, edgeTypeId, rank, srcId, dstId, getDecodeType());
195 }
196
197 @Override
198 public String toString() {
199 List<String> propStrs = new ArrayList<>();
200 Map<String, ValueWrapper> props = getProperties();
201 for (String key : props.keySet()) {
202 propStrs.add(key + ":" + props.get(key).toString());
203 }
204 if (direction != Direction.UNDIRECTED) {
205 return String.format("(%d)-[%d@%s:%s{%s}]->(%d)",
206 getSrcId(),
207 getRank(),
208 getType(),
209 String.join("&", getLabels()),
210 String.join(",", propStrs),
211 getDstId());
212 } else {
213 return String.format("(%d)~[%d@%s:%s{%s}]~(%d)",
214 getSrcId(),
215 getRank(),
216 getType(),
217 String.join("&", getLabels()),
218 String.join(",", propStrs),
219 getDstId());
220 }
221
222 }
223
224 enum Direction {
225 OUTGOING, INCOMING, UNDIRECTED, KNOWN;
226 }
227}
long getRank()
get rank from the edge
Definition Edge.java:141
List< ValueWrapper > getPropertyValues()
get all property values
Definition Edge.java:159
List< String > getLabels()
get edge labels
Definition Edge.java:114
String getType()
get edge type name
Definition Edge.java:89
boolean isDirected()
if the edge is directed
Definition Edge.java:105
int getEdgeTypeId()
get edge type id
Definition Edge.java:96
List< String > getColumnNames()
get all property name
Definition Edge.java:150
Edge(int graphId, int edgeTypeId, long rank, long srcId, long dstId, Map< String, ValueWrapper > properties, ResultGraphSchemas graphSchemas)
Edge is a wrapper around the Edge type returned by nebula-graph.
Definition Edge.java:28
Map< String, ValueWrapper > getProperties()
get property names and values from the edge
Definition Edge.java:172
long getDstId()
get the dst id from the edge
Definition Edge.java:132