NebulaGraph Python Client  release-3.8
BaseResult.py
1 #!/usr/bin/env python
2 # --coding:utf-8--
3 
4 # Copyright (c) 2020 vesoft inc. All rights reserved.
5 #
6 # This source code is licensed under Apache 2.0 License.
7 
8 
9 from nebula3.common import ttypes
10 from nebula3.common.ttypes import Vertex, Tag, Edge
11 from nebula3.data.DataObject import DataSetWrapper, Node, ValueWrapper, Relationship
12 
13 
14 class VertexData(object):
15  # TODO just ignore '_vid' column
16  PROP_START_INDEX_with_vid = 2
17  PROP_START_INDEX = 1
18 
19  def __init__(self, row, col_names, decode_type='utf-8'):
20  """
21  the format is
22  '''
23  |tag_name._vid|tag_name.prop1|tag_name.prop2|
24  '''
25  """
26  if len(row.values) != len(col_names):
27  raise RuntimeError(
28  'Input row size is not equal with the col name size, {} != {}'.format(
29  row.values, col_names
30  )
31  )
32  self._row_row = row
33  self._decode_type_decode_type = decode_type
34  self._col_names_col_names = []
35  self._tag_name_tag_name = ''
36  for col_name in col_names:
37  names = col_name.split(b'.')
38  # TODO, just keep some behevior with before
39  if len(names) == 1 and names[0] == b'_vid':
40  continue
41  if len(names) != 2:
42  raise RuntimeError('Input wrong col name format of tag')
43  self._col_names_col_names.append(names[1])
44  self._tag_name_tag_name = names[0]
45 
46  def get_id(self):
47  """get vertex id, if the space vid_type is int, you can use get_id().as_int(),
48  if the space vid_type is fixed_string, you can use get_id().as_string()
49 
50  :return: ValueWrapper
51  """
52  if len(self._row_row.values) < 1:
53  raise RuntimeError(
54  'The row value is bad format, '
55  'get vertex id failed: len is {}'.format(len(self._row_row.values))
56  )
57  return ValueWrapper(self._row_row.values[0], self._decode_type_decode_type)
58 
59  def as_node(self):
60  """convert the vertex data to structure Node
61 
62  :return: Node
63  """
64  if len(self._row_row.values) < self.PROP_START_INDEXPROP_START_INDEX:
65  raise RuntimeError(
66  'The row value is bad format, '
67  'as node failed: len is {}'.format(len(self._row_row.values))
68  )
69 
70  vertex = Vertex()
71  vertex.tags = []
72  vertex.vid = self._row_row.values[0]
73  tag = Tag()
74  tag.name = self._tag_name_tag_name
75  tag.props = {}
76  index = self.PROP_START_INDEXPROP_START_INDEX
77  while index < len(self._col_names_col_names):
78  tag.props[self._col_names_col_names[index]] = self._row_row.values[index + 1]
79  index = index + 1
80  vertex.tags.append(tag)
81 
82  return Node(vertex).set_decode_type(self._decode_type_decode_type)
83 
84  def get_prop_names(self):
85  """get all prop names from the vertex data
86 
87  :return: list<string>
88  """
89  return self._col_names_col_names[self.PROP_START_INDEXPROP_START_INDEX :]
90 
91  def get_prop_values(self):
92  """get all prop values from the vertex data
93 
94  :return: list<ValueWrapper>
95  """
96  index = self.PROP_START_INDEX_with_vidPROP_START_INDEX_with_vid
97  prop_values = []
98  while index < len(self._row_row.values):
99  prop_values.append(
100  ValueWrapper(self._row_row.values[index], decode_type=self._decode_type_decode_type)
101  )
102  index = index + 1
103  return prop_values
104 
105  def __repr__(self):
106  return str(self.as_nodeas_node())
107 
108 
109 class EdgeData(object):
110  PROP_START_INDEX = 4
111 
112  def __init__(self, row, col_names, decode_type='utf-8'):
113  """
114  the format is
115  '''
116  |edge_name._src|edge_name._type|edge_name._rank|edge_name._dst|edge_name.prop1|edge_name.prop2|
117  '''
118  """
119  if len(row.values) != len(col_names):
120  raise RuntimeError(
121  'Input row size is not equal '
122  'with the col name size, {} != {}'.format(
123  len(row.values), len(col_names)
124  )
125  )
126  self._row_row = row
127  self._decode_type_decode_type = decode_type
128  self._col_names_col_names = []
129  self._edge_name_edge_name = ''
130  for col_name in col_names:
131  names = col_name.split(b'.')
132  if len(names) != 2:
133  raise RuntimeError('Input wrong col name format of edge')
134  self._col_names_col_names.append(names[1])
135  self._edge_name_edge_name = names[0]
136 
137  def get_src_id(self):
138  """get src id, if the space vid_type is int, you can use get_src_id().as_int(),
139  if the space vid_type is fixed_string, you can use get_src_id().as_string()
140 
141  :return: ValueWrapper
142  """
143  if len(self._row_row.values) < 1:
144  raise RuntimeError(
145  'The row value is bad format, '
146  'get edge src id failed: len is {}'.format(len(self._row_row.values))
147  )
148  return ValueWrapper(self._row_row.values[0], self._decode_type_decode_type)
149 
150  def get_edge_name(self):
151  """get edge name
152 
153  :return: edge name
154  """
155  return self._edge_name_edge_name.decode(self._decode_type_decode_type)
156 
157  def get_ranking(self):
158  """get edge ranking
159 
160  :return: ranking
161  """
162  if len(self._row_row.values) < 3:
163  raise RuntimeError(
164  'The row value is bad format, '
165  'get edge ranking failed: len is {}'.format(len(self._row_row.values))
166  )
167  assert self._row_row.values[2].getType() == ttypes.Value.IVAL
168  return self._row_row.values[2].get_iVal()
169 
170  def get_dst_id(self):
171  """get dst id, if the space vid_type is int, you can use get_dst_id().as_int(),
172  if the space vid_type is fixed_string, you can use get_dst_id().as_string()
173 
174  :return: ValueWrapper
175  """
176  if len(self._row_row.values) < 4:
177  raise RuntimeError(
178  'The row value is bad format, '
179  'get edge dst id failed: len is {}'.format(len(self._row_row.values))
180  )
181  return ValueWrapper(self._row_row.values[3], self._decode_type_decode_type)
182 
183  def as_relationship(self):
184  """convert the edge data to structure Relationship
185 
186  :return: Relationship
187  """
188  if len(self._row_row.values) < self.PROP_START_INDEXPROP_START_INDEX:
189  raise RuntimeError(
190  'The row value is bad format, '
191  'as relationship failed: len is {}'.format(len(self._row_row.values))
192  )
193  edge = Edge()
194  edge.src = self._row_row.values[0]
195  edge.type = self._row_row.values[1].get_iVal()
196  edge.name = self._edge_name_edge_name
197  edge.ranking = self._row_row.values[2].get_iVal()
198  edge.dst = self._row_row.values[3]
199  edge.props = {}
200  index = self.PROP_START_INDEXPROP_START_INDEX
201  while index < len(self._col_names_col_names):
202  edge.props[self._col_names_col_names[index]] = self._row_row.values[index]
203  index = index + 1
204 
205  return Relationship(edge).set_decode_type(self._decode_type_decode_type)
206 
207  def get_prop_names(self):
208  """get all prop names from the edge data
209 
210  :return: list<string>
211  """
212  return self._col_names_col_names[self.PROP_START_INDEXPROP_START_INDEX :]
213 
214  def get_prop_values(self):
215  """get all prop values from the edge data
216 
217  :return: list<ValueWrapper>
218  """
219  index = self.PROP_START_INDEXPROP_START_INDEX
220  prop_values = []
221  while index < len(self._row_row.values):
222  prop_values.append(
223  ValueWrapper(self._row_row.values[index], decode_type=self._decode_type_decode_type)
224  )
225  index = index + 1
226  return prop_values
227 
228  def __repr__(self):
229  return str(self.as_relationshipas_relationship())
230 
231 
232 class BaseResult(object):
233  def __init__(self, data_sets: list, decode_type='utf-8', is_vertex=True):
234  assert data_sets is not None
235  self.is_vertexis_vertex = is_vertex
236  self._data_sets_data_sets = data_sets
237  self._decode_type_decode_type = decode_type
238  self._pos_pos = -1
239  self._data_set_pos_data_set_pos = 0
240  self._table_pos_table_pos = -1
241  self._size_size = 0
242  for data_set in self._data_sets_data_sets:
243  self._size_size += len(data_set.rows)
244 
245  def get_data_set(self):
246  """get DataSet, it's the origin values, the string type is binary
247 
248  :return: DataSet
249  """
250  result = None
251  for data_set in self._data_sets_data_sets:
252  if result is None:
253  result = data_set
254  continue
255  if len(data_set.column_names) != len(result.column_names):
256  raise RuntimeError('Multi DataSets are different col size')
257  result.rows.extend(data_set.rows)
258 
259  return result
260 
262  """get DataSetWrapper, it's the wrapper for DataSet value, the string type is str
263 
264  :return: DataSetWrapper
265  """
266  result = None
267  for data_set in self._data_sets_data_sets:
268  if result is None:
269  result = data_set
270  continue
271  if len(data_set.column_names) != len(result.column_names):
272  raise RuntimeError('Multi DataSets are different col size')
273  result.rows.extend(data_set.rows)
274 
275  if result is None:
276  return None
277  return DataSetWrapper(result, self._decode_type_decode_type)
278 
279  def __repr__(self):
280  return str(self._data_sets_data_sets)
281 
282  def __iter__(self):
283  self._pos_pos = -1
284  return self
285 
286  def __next__(self):
287  """The VertexData or EdgeData iterator
288 
289  :return: VertexData or EdgeData Iterator
290  """
291  if len(self._data_sets_data_sets) == 0 or self._pos_pos >= self._size_size - 1:
292  raise StopIteration
293  self._pos_pos += 1
294  self._table_pos_table_pos += 1
295  if self._table_pos_table_pos >= len(self._data_sets_data_sets[self._data_set_pos_data_set_pos].rows):
296  self._table_pos_table_pos = 0
297  self._data_set_pos_data_set_pos += 1
298  col_names = self._data_sets_data_sets[self._data_set_pos_data_set_pos].column_names
299  row = self._data_sets_data_sets[self._data_set_pos_data_set_pos].rows[self._table_pos_table_pos]
300  if self.is_vertexis_vertex:
301  return VertexData(row, col_names, self._decode_type_decode_type)
302  else:
303  return EdgeData(row, col_names, self._decode_type_decode_type)
def __init__(self, row, col_names, decode_type='utf-8')
Definition: BaseResult.py:112
def __init__(self, row, col_names, decode_type='utf-8')
Definition: BaseResult.py:19