NebulaGraph Java Client  release-3.8
Relationship.java
1 /* Copyright (c) 2020 vesoft inc. All rights reserved.
2  *
3  * This source code is licensed under Apache 2.0 License.
4  */
5 
6 package com.vesoft.nebula.client.graph.data;
7 
8 import com.vesoft.nebula.Edge;
9 import com.vesoft.nebula.Value;
10 import java.io.UnsupportedEncodingException;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Objects;
16 
17 public class Relationship extends BaseDataObject {
18  private final Edge edge;
19 
24  public Relationship(Edge edge) {
25  if (edge == null) {
26  throw new RuntimeException("Input an null edge object");
27  }
28  this.edge = edge;
29  }
30 
36  public ValueWrapper srcId() {
37  return edge.type > 0 ? new ValueWrapper(edge.src, getDecodeType(), getTimezoneOffset())
38  : new ValueWrapper(edge.dst, getDecodeType(), getTimezoneOffset());
39  }
40 
46  public ValueWrapper dstId() {
47  return edge.type > 0 ? new ValueWrapper(edge.dst, getDecodeType(), getTimezoneOffset())
48  : new ValueWrapper(edge.src, getDecodeType(), getTimezoneOffset());
49  }
50 
55  public String edgeName() {
56  return new String(edge.name);
57  }
58 
63  public long ranking() {
64  return edge.ranking;
65  }
66 
72  public List<String> keys() throws UnsupportedEncodingException {
73  List<String> propNames = new ArrayList<>();
74  for (byte[] name : edge.props.keySet()) {
75  propNames.add(new String(name, getDecodeType()));
76  }
77  return propNames;
78  }
79 
84  public List<ValueWrapper> values() {
85  List<ValueWrapper> propVals = new ArrayList<>();
86  for (Value val : edge.props.values()) {
87  propVals.add(new ValueWrapper(val, getDecodeType(), getTimezoneOffset()));
88  }
89  return propVals;
90  }
91 
96  public HashMap<String, ValueWrapper> properties() throws UnsupportedEncodingException {
97  HashMap<String, ValueWrapper> properties = new HashMap<>();
98  for (byte[] key : edge.props.keySet()) {
99  properties.put(new String(key, getDecodeType()),
100  new ValueWrapper(edge.props.get(key), getDecodeType(), getTimezoneOffset()));
101  }
102  return properties;
103  }
104 
105  @Override
106  public boolean equals(Object o) {
107  if (this == o) {
108  return true;
109  }
110  if (o == null || getClass() != o.getClass()) {
111  return false;
112  }
113  Relationship that = (Relationship) o;
114  return ranking() == that.ranking()
115  && Objects.equals(srcId(), that.srcId())
116  && Objects.equals(dstId(), that.dstId())
117  && Objects.equals(edgeName(), that.edgeName());
118  }
119 
120  @Override
121  public int hashCode() {
122  return Objects.hash(edge, getDecodeType(), getTimezoneOffset());
123  }
124 
125  @Override
126  public String toString() {
127  try {
128  List<String> propStrs = new ArrayList<>();
129  Map<String, ValueWrapper> props = properties();
130  for (String key : props.keySet()) {
131  propStrs.add(key + ": " + props.get(key).toString());
132  }
133  return String.format("(%s)-[:%s@%d{%s}]->(%s)",
134  srcId(), edgeName(), ranking(), String.join(", ", propStrs), dstId());
135  } catch (UnsupportedEncodingException e) {
136  return e.getMessage();
137  }
138  }
139 }
List< ValueWrapper > values()
get property values from the relationship
ValueWrapper dstId()
get the dst id from the relationship
List< String > keys()
get all property name from the relationship
Relationship(Edge edge)
Relationship is a wrapper around the Edge type returned by nebula-graph.
long ranking()
get ranking from the relationship
HashMap< String, ValueWrapper > properties()
get property names and values from the relationship
String edgeName()
get edge name from the relationship
ValueWrapper srcId()
get the src id from the relationship