NebulaGraph Java Client  release-3.8
ResultSet.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.ErrorCode;
9 import com.vesoft.nebula.Row;
10 import com.vesoft.nebula.Value;
11 import com.vesoft.nebula.graph.ExecutionResponse;
12 import com.vesoft.nebula.graph.PlanDescription;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Spliterator;
17 import java.util.function.Consumer;
18 
19 public class ResultSet {
20  private final ExecutionResponse response;
21  private final List<String> columnNames = new ArrayList<>();
22  private final String decodeType = "utf-8";
23  private final int timezoneOffset;
24 
25  public static class Record implements Iterable<ValueWrapper> {
26  private final List<ValueWrapper> colValues = new ArrayList<>();
27  private List<String> columnNames = new ArrayList<>();
28 
29  public Record(List<String> columnNames, Row row, String decodeType, int timezoneOffset) {
30  if (columnNames == null) {
31  return;
32  }
33 
34  if (row == null || row.values == null) {
35  return;
36  }
37 
38  for (Value value : row.values) {
39  this.colValues.add(new ValueWrapper(value, decodeType, timezoneOffset));
40  }
41 
42  this.columnNames = columnNames;
43  }
44 
45  @Override
46  public Iterator<ValueWrapper> iterator() {
47  return this.colValues.iterator();
48  }
49 
50  @Override
51  public void forEach(Consumer<? super ValueWrapper> action) {
52  this.colValues.forEach(action);
53  }
54 
55  @Override
56  public Spliterator<ValueWrapper> spliterator() {
57  return this.colValues.spliterator();
58  }
59 
60 
61  @Override
62  public String toString() {
63  List<String> valueStr = new ArrayList<>();
64  for (ValueWrapper v : colValues) {
65  valueStr.add(v.toString());
66  }
67  return String.format("ColumnName: %s, Values: %s",
68  columnNames.toString(), valueStr.toString());
69  }
70 
76  public ValueWrapper get(int index) {
77  if (index >= columnNames.size()) {
78  throw new IllegalArgumentException(
79  String.format("Cannot get field because the key '%d' out of range", index));
80  }
81  return this.colValues.get(index);
82  }
83 
89  public ValueWrapper get(String columnName) {
90  int index = columnNames.indexOf(columnName);
91  if (index == -1) {
92  throw new IllegalArgumentException(
93  "Cannot get field because the columnName '"
94  + columnName + "' is not exists");
95  }
96  return this.colValues.get(index);
97  }
98 
103  public List<ValueWrapper> values() {
104  return colValues;
105  }
106 
111  public int size() {
112  return this.columnNames.size();
113  }
114 
120  public boolean contains(String columnName) {
121  return this.columnNames.contains(columnName);
122  }
123 
124  }
125 
126  public ResultSet(ExecutionResponse resp, int timezoneOffset) {
127  if (resp == null) {
128  throw new RuntimeException("Input an null `ExecutionResponse' object");
129  }
130  this.response = resp;
131  this.timezoneOffset = timezoneOffset;
132  if (resp.data != null) {
133  // space name's charset is 'utf-8'
134  for (byte[] column : resp.data.column_names) {
135  this.columnNames.add(new String(column));
136  }
137  }
138  }
139 
144  public boolean isSucceeded() {
145  return response.error_code == ErrorCode.SUCCEEDED;
146  }
147 
152  public boolean isEmpty() {
153  return response.data == null || response.data.rows.isEmpty();
154  }
155 
160  public int getErrorCode() {
161  return response.error_code.getValue();
162  }
163 
168  public String getSpaceName() {
169  if (response.space_name == null) {
170  return "";
171  }
172  // space name's charset is 'utf-8'
173  return new String(response.space_name);
174  }
175 
180  public String getErrorMessage() {
181  if (response.error_msg == null) {
182  return "";
183  }
184  // error message's charset is 'utf-8'
185  return new String(response.error_msg);
186  }
187 
192  public String getComment() {
193  if (response.comment == null) {
194  return "";
195  }
196  // error message's charset is 'utf-8'
197  return new String(response.comment);
198  }
199 
204  public long getLatency() {
205  return response.latency_in_us;
206  }
207 
212  public PlanDescription getPlanDesc() {
213  return response.getPlan_desc();
214  }
215 
220  public List<String> keys() {
221  return columnNames;
222  }
223 
228  public List<String> getColumnNames() {
229  return columnNames;
230  }
231 
236  public int rowsSize() {
237  if (response.data == null) {
238  return 0;
239  }
240  return response.data.rows.size();
241  }
242 
248  public Record rowValues(int index) {
249  if (response.data == null) {
250  throw new RuntimeException("Empty data");
251  }
252  if (index >= response.data.rows.size()) {
253  throw new ArrayIndexOutOfBoundsException();
254  }
255  return new Record(columnNames, response.data.rows.get(index), decodeType, timezoneOffset);
256  }
257 
263  public List<ValueWrapper> colValues(String columnName) {
264  if (response.data == null) {
265  throw new RuntimeException("Empty data");
266  }
267  int index = columnNames.indexOf(columnName);
268  if (index < 0) {
269  throw new ArrayIndexOutOfBoundsException();
270  }
271  List<ValueWrapper> values = new ArrayList<>();
272  for (int i = 0; i < response.data.rows.size(); i++) {
273  values.add(new ValueWrapper(response.data.rows.get(i).values.get(index),
274  decodeType,
275  timezoneOffset));
276  }
277  return values;
278  }
279 
284  public List<Row> getRows() {
285  if (response.data == null) {
286  throw new RuntimeException("Empty data");
287  }
288  return response.data.rows;
289  }
290 
291  @Override
292  public String toString() {
293  // When error, print the raw data directly
294  if (!isSucceeded()) {
295  return response.toString();
296  }
297  int i = 0;
298  List<String> rowStrs = new ArrayList<>();
299  while (i < rowsSize()) {
300  List<String> valueStrs = new ArrayList<>();
301  for (ValueWrapper value : rowValues(i)) {
302  valueStrs.add(value.toString());
303  }
304  rowStrs.add(String.join(",", valueStrs));
305  i++;
306  }
307  return String.format("ColumnName: %s, Rows: %s",
308  columnNames.toString(), rowStrs.toString());
309  }
310 }
Record rowValues(int index)
get row values with the row index
Definition: ResultSet.java:248
String getComment()
get the comment from the server
Definition: ResultSet.java:192
List< String > keys()
get keys of the dataset
Definition: ResultSet.java:220
List< Row > getRows()
get all rows, the value is the origin value, the string is binary
Definition: ResultSet.java:284
int getErrorCode()
get errorCode of execute result
Definition: ResultSet.java:160
PlanDescription getPlanDesc()
get the PalnDesc
Definition: ResultSet.java:212
List< ValueWrapper > colValues(String columnName)
get col values on the column key
Definition: ResultSet.java:263
String getErrorMessage()
get the error message of the execute result
Definition: ResultSet.java:180
boolean isSucceeded()
the execute result is succeeded
Definition: ResultSet.java:144
boolean isEmpty()
the result data is empty
Definition: ResultSet.java:152
String getSpaceName()
get the space name of current session
Definition: ResultSet.java:168
List< String > getColumnNames()
get column names of the dataset
Definition: ResultSet.java:228
long getLatency()
get latency of the query execute time
Definition: ResultSet.java:204