NebulaGraph Java Client  release-3.8
ScanVertexResult.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.storage.scan;
7 
8 import com.vesoft.nebula.DataSet;
9 import com.vesoft.nebula.client.graph.data.ValueWrapper;
10 import com.vesoft.nebula.client.storage.data.ScanStatus;
11 import com.vesoft.nebula.client.storage.data.VertexRow;
12 import com.vesoft.nebula.client.storage.data.VertexTableRow;
13 import com.vesoft.nebula.client.storage.processor.VertexProcessor;
14 import java.io.Serializable;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 
22 public class ScanVertexResult implements Serializable {
23 
24  private static final Logger LOGGER = LoggerFactory.getLogger(ScanVertexResult.class);
25  private static final long serialVersionUID = -2446261806893848521L;
26 
27  private final List<DataSet> dataSets;
28 
29  private final ScanStatus scanStatus;
30 
34  private List<VertexTableRow> vertexTableRows = new ArrayList<>();
35 
39  private List<String> propNames = new ArrayList<>();
40 
44  private List<VertexRow> verticeRows = new ArrayList<>();
45 
46  private Map<ValueWrapper, VertexRow> vidVertices = new HashMap<>();
47 
48  private String decodeType = "utf-8";
49 
50  private boolean isEmpty;
51 
52  public ScanVertexResult(List<DataSet> dataSets, ScanStatus status) {
53  this.dataSets = dataSets;
54  this.scanStatus = status;
55  this.isEmpty = isDatasetEmpty();
56  }
57 
63  public List<VertexTableRow> getVertexTableRows() {
64  if (!isEmpty && vertexTableRows.isEmpty()) {
65  constructVertexTableRow();
66  }
67  return vertexTableRows;
68  }
69 
75  public List<String> getPropNames() {
76  if (!isEmpty && propNames.isEmpty()) {
77  constructPropNames();
78  }
79  return propNames;
80  }
81 
89  if (!isEmpty && vidVertices.isEmpty()) {
90  constructVertexRow();
91  }
92  if (vidVertices.isEmpty()) {
93  return null;
94  }
95  return vidVertices.get(vid);
96  }
97 
103  public List<VertexRow> getVertices() {
104  if (!isEmpty && verticeRows.isEmpty()) {
105  constructVertexRow();
106  }
107  return verticeRows;
108  }
109 
110 
116  public Map<ValueWrapper, VertexRow> getVidVertices() {
117  if (!isEmpty && vidVertices.isEmpty()) {
118  constructVertexRow();
119  }
120  return vidVertices;
121  }
122 
123 
129  public boolean isAllSuccess() {
130  return scanStatus == ScanStatus.ALL_SUCCESS;
131  }
132 
138  public boolean isEmpty() {
139  return isEmpty;
140  }
141 
147  private boolean isDatasetEmpty() {
148  if (dataSets == null || dataSets.isEmpty()) {
149  return true;
150  }
151  for (DataSet dataSet : dataSets) {
152  if (dataSet.getRows().size() > 0) {
153  return false;
154  }
155  }
156  return true;
157  }
158 
162  private void constructVertexRow() {
163  if (isEmpty) {
164  return;
165  }
166  synchronized (this) {
167  if (vidVertices.isEmpty()) {
168  vidVertices = VertexProcessor.constructVertexRow(dataSets, decodeType);
169  verticeRows = new ArrayList<>(vidVertices.values());
170  }
171  }
172  }
173 
177  private void constructVertexTableRow() {
178  if (isEmpty) {
179  return;
180  }
181  synchronized (this) {
182  if (vertexTableRows.isEmpty()) {
183  vertexTableRows = VertexProcessor.constructVertexTableRow(dataSets, decodeType);
184  }
185  }
186  }
187 
191  private void constructPropNames() {
192  if (isEmpty) {
193  return;
194  }
195  synchronized (this) {
196  if (propNames.isEmpty()) {
197  List<byte[]> colNames = dataSets.get(0).getColumn_names();
198  for (byte[] colName : colNames) {
199  String propName = new String(colName);
200  if (!propName.contains(".")) {
201  continue;
202  }
203  propNames.add(propName.split("\\.")[1]);
204  }
205  }
206  }
207  }
208 
209 }
210 
211 
List< String > getPropNames()
get the result's property names
List< VertexTableRow > getVertexTableRows()
get vertex table rows
Map< ValueWrapper, VertexRow > getVidVertices()
get the map of vid and vertex row
List< VertexRow > getVertices()
get all vertex row
VertexRow getVertex(ValueWrapper vid)
get vertex row with id