NebulaGraph Java Client  release-3.8
Node.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.Tag;
9 import com.vesoft.nebula.Value;
10 import com.vesoft.nebula.Vertex;
11 import java.io.UnsupportedEncodingException;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Objects;
17 
18 public class Node extends BaseDataObject {
19  private final Vertex vertex;
20  private final ValueWrapper vid;
21 
22  List<String> tagNames = new ArrayList<>();
23 
29  public Node(Vertex vertex) throws UnsupportedEncodingException {
30  if (vertex == null) {
31  throw new RuntimeException("Input an null vertex object");
32  }
33  vid = new ValueWrapper(vertex.vid, getDecodeType(), getTimezoneOffset());
34  this.vertex = vertex;
35  for (Tag tag : vertex.tags) {
36  this.tagNames.add(new String(tag.name, getDecodeType()));
37  }
38  }
39 
45  public ValueWrapper getId() {
46  return vid;
47  }
48 
53  public List<String> tagNames() {
54  return tagNames;
55  }
56 
61  public List<String> labels() {
62  return tagNames;
63  }
64 
70  public boolean hasTagName(String tagName) {
71  return tagNames.contains(tagName);
72  }
73 
78  public boolean hasLabel(String tagName) {
79  return tagNames.contains(tagName);
80  }
81 
87  public List<ValueWrapper> values(String tagName) {
88  int index = tagNames.indexOf(tagName);
89  if (index < 0) {
90  throw new IllegalArgumentException(tagName + " is not found");
91  }
92  List<ValueWrapper> values = new ArrayList<>();
93  for (Value val : vertex.tags.get(index).props.values()) {
94  values.add(new ValueWrapper(val, getDecodeType(), getTimezoneOffset()));
95  }
96  return values;
97  }
98 
105  public List<String> keys(String tagName) throws UnsupportedEncodingException {
106  int index = tagNames.indexOf(tagName);
107  if (index < 0) {
108  throw new IllegalArgumentException(tagName + " is not found");
109  }
110 
111  List<String> keys = new ArrayList<>();
112  for (byte[] name : vertex.tags.get(index).props.keySet()) {
113  keys.add(new String(name, getDecodeType()));
114  }
115  return keys;
116  }
117 
124  public HashMap<String, ValueWrapper> properties(String tagName)
125  throws UnsupportedEncodingException {
126  int index = tagNames.indexOf(tagName);
127  if (index < 0) {
128  throw new IllegalArgumentException(tagName + " is not found");
129  }
130 
131  HashMap<String, ValueWrapper> properties = new HashMap();
132  for (byte[] name : vertex.tags.get(index).props.keySet()) {
133  properties.put(new String(name, getDecodeType()),
134  new ValueWrapper(vertex.tags.get(index).props.get(name),
135  getDecodeType(),
136  getTimezoneOffset()));
137  }
138  return properties;
139  }
140 
141  @Override
142  public boolean equals(Object o) {
143  if (this == o) {
144  return true;
145  }
146  if (o == null || getClass() != o.getClass()) {
147  return false;
148  }
149  Node node = (Node) o;
150  return Objects.equals(vid, node.vid);
151  }
152 
153  @Override
154  public int hashCode() {
155  return Objects.hash(vertex, vid, getDecodeType(), getTimezoneOffset(), tagNames);
156  }
157 
158  @Override
159  public String toString() {
160  try {
161  List<String> tagsStr = new ArrayList<>();
162  for (String name : tagNames()) {
163  List<String> propStrs = new ArrayList<>();
164  Map<String, ValueWrapper> props = properties(name);
165  for (String key : props.keySet()) {
166  propStrs.add(key + ": " + props.get(key).toString());
167  }
168  tagsStr.add(String.format(":%s {%s}", name, String.join(", ", propStrs)));
169  }
170  return String.format("(%s %s)", getId(), String.join(" ", tagsStr));
171  } catch (UnsupportedEncodingException e) {
172  return e.getMessage();
173  }
174  }
175 }
List< String > tagNames()
get all tag name from the node
Definition: Node.java:53
boolean hasLabel(String tagName)
Used to be compatible with older versions of interfaces.
Definition: Node.java:78
ValueWrapper getId()
get vid from the node
Definition: Node.java:45
List< ValueWrapper > values(String tagName)
get property values from the node
Definition: Node.java:87
boolean hasTagName(String tagName)
determine if node contains the given tag
Definition: Node.java:70
Node(Vertex vertex)
Node is a wrapper around the Vertex type returned by nebula-graph.
Definition: Node.java:29
List< String > labels()
Used to be compatible with older versions of interfaces.
Definition: Node.java:61
HashMap< String, ValueWrapper > properties(String tagName)
get property names and values from the node
Definition: Node.java:124
List< String > keys(String tagName)
get property names from the node
Definition: Node.java:105