NebulaGraph Python Client  release-3.4
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_values(self):
85  """get all prop values from the vertex data
86 
87  :return: list<ValueWrapper>
88  """
89  index = self.PROP_START_INDEX_with_vidPROP_START_INDEX_with_vid
90  prop_values = []
91  while index < len(self._row_row.values):
92  prop_values.append(
93  ValueWrapper(self._row_row.values[index], decode_type=self._decode_type_decode_type)
94  )
95  index = index + 1
96  return prop_values
97 
98  def __repr__(self):
99  return str(self.as_nodeas_node())
100 
101 
102 class EdgeData(object):
103  PROP_START_INDEX = 4
104 
105  def __init__(self, row, col_names, decode_type='utf-8'):
106  """
107  the format is
108  '''
109  |edge_name._src|edge_name._type|edge_name._rank|edge_name._dst|edge_name.prop1|edge_name.prop2|
110  '''
111  """
112  if len(row.values) != len(col_names):
113  raise RuntimeError(
114  'Input row size is not equal '
115  'with the col name size, {} != {}'.format(
116  len(row.values), len(col_names)
117  )
118  )
119  self._row_row = row
120  self._decode_type_decode_type = decode_type
121  self._col_names_col_names = []
122  self._edge_name_edge_name = ''
123  for col_name in col_names:
124  names = col_name.split(b'.')
125  if len(names) != 2:
126  raise RuntimeError('Input wrong col name format of edge')
127  self._col_names_col_names.append(names[1])
128  self._edge_name_edge_name = names[0]
129 
130  def get_src_id(self):
131  """get src id, if the space vid_type is int, you can use get_src_id().as_int(),
132  if the space vid_type is fixed_string, you can use get_src_id().as_string()
133 
134  :return: ValueWrapper
135  """
136  if len(self._row_row.values) < 1:
137  raise RuntimeError(
138  'The row value is bad format, '
139  'get edge src id failed: len is {}'.format(len(self._row_row.values))
140  )
141  return ValueWrapper(self._row_row.values[0], self._decode_type_decode_type)
142 
143  def get_edge_name(self):
144  """get edge name
145 
146  :return: edge name
147  """
148  return self._edge_name_edge_name.decode(self._decode_type_decode_type)
149 
150  def get_ranking(self):
151  """get edge ranking
152 
153  :return: ranking
154  """
155  if len(self._row_row.values) < 3:
156  raise RuntimeError(
157  'The row value is bad format, '
158  'get edge ranking failed: len is {}'.format(len(self._row_row.values))
159  )
160  assert self._row_row.values[2].getType() == ttypes.Value.IVAL
161  return self._row_row.values[2].get_iVal()
162 
163  def get_dst_id(self):
164  """get dst id, if the space vid_type is int, you can use get_dst_id().as_int(),
165  if the space vid_type is fixed_string, you can use get_dst_id().as_string()
166 
167  :return: ValueWrapper
168  """
169  if len(self._row_row.values) < 4:
170  raise RuntimeError(
171  'The row value is bad format, '
172  'get edge dst id failed: len is {}'.format(len(self._row_row.values))
173  )
174  return ValueWrapper(self._row_row.values[3], self._decode_type_decode_type)
175 
176  def as_relationship(self):
177  """convert the edge data to structure Relationship
178 
179  :return: Relationship
180  """
181  if len(self._row_row.values) < self.PROP_START_INDEXPROP_START_INDEX:
182  raise RuntimeError(
183  'The row value is bad format, '
184  'as relationship failed: len is {}'.format(len(self._row_row.values))
185  )
186  edge = Edge()
187  edge.src = self._row_row.values[0]
188  edge.type = self._row_row.values[1].get_iVal()
189  edge.name = self._edge_name_edge_name
190  edge.ranking = self._row_row.values[2].get_iVal()
191  edge.dst = self._row_row.values[3]
192  edge.props = {}
193  index = self.PROP_START_INDEXPROP_START_INDEX
194  while index < len(self._col_names_col_names):
195  edge.props[self._col_names_col_names[index]] = self._row_row.values[index]
196  index = index + 1
197 
198  return Relationship(edge).set_decode_type(self._decode_type_decode_type)
199 
200  def get_prop_values(self):
201  """get all prop values from the edge data
202 
203  :return: list<ValueWrapper>
204  """
205  index = self.PROP_START_INDEXPROP_START_INDEX
206  prop_values = []
207  while index < len(self._row_row.values):
208  prop_values.append(
209  ValueWrapper(self._row_row.values[index], decode_type=self._decode_type_decode_type)
210  )
211  index = index + 1
212  return prop_values
213 
214  def __repr__(self):
215  return str(self.as_relationshipas_relationship())
216 
217 
218 class BaseResult(object):
219  def __init__(self, data_sets: list, decode_type='utf-8', is_vertex=True):
220  assert data_sets is not None
221  self.is_vertexis_vertex = is_vertex
222  self._data_sets_data_sets = data_sets
223  self._decode_type_decode_type = decode_type
224  self._pos_pos = -1
225  self._data_set_pos_data_set_pos = 0
226  self._table_pos_table_pos = -1
227  self._size_size = 0
228  for data_set in self._data_sets_data_sets:
229  self._size_size += len(data_set.rows)
230 
231  def get_data_set(self):
232  """get DataSet, it's the origin values, the string type is binary
233 
234  :return: DataSet
235  """
236  result = None
237  for data_set in self._data_sets_data_sets:
238  if result is None:
239  result = data_set
240  continue
241  if len(data_set.column_names) != len(result.column_names):
242  raise RuntimeError('Multi DataSets are different col size')
243  result.rows.extend(data_set.rows)
244 
245  return result
246 
248  """get DataSetWrapper, it's the wrapper for DataSet value, the string type is str
249 
250  :return: DataSetWrapper
251  """
252  result = None
253  for data_set in self._data_sets_data_sets:
254  if result is None:
255  result = data_set
256  continue
257  if len(data_set.column_names) != len(result.column_names):
258  raise RuntimeError('Multi DataSets are different col size')
259  result.rows.extend(data_set.rows)
260 
261  if result is None:
262  return None
263  return DataSetWrapper(result, self._decode_type_decode_type)
264 
265  def __repr__(self):
266  return str(self._data_sets_data_sets)
267 
268  def __iter__(self):
269  self._pos_pos = -1
270  return self
271 
272  def __next__(self):
273  """The VertexData or EdgeData iterator
274 
275  :return: VertexData or EdgeData Iterator
276  """
277  if len(self._data_sets_data_sets) == 0 or self._pos_pos >= self._size_size - 1:
278  raise StopIteration
279  self._pos_pos += 1
280  self._table_pos_table_pos += 1
281  if self._table_pos_table_pos >= len(self._data_sets_data_sets[self._data_set_pos_data_set_pos].rows):
282  self._table_pos_table_pos = 0
283  self._data_set_pos_data_set_pos += 1
284  col_names = self._data_sets_data_sets[self._data_set_pos_data_set_pos].column_names
285  row = self._data_sets_data_sets[self._data_set_pos_data_set_pos].rows[self._table_pos_table_pos]
286  if self.is_vertexis_vertex:
287  return VertexData(row, col_names, self._decode_type_decode_type)
288  else:
289  return EdgeData(row, col_names, self._decode_type_decode_type)
def __init__(self, row, col_names, decode_type='utf-8')
Definition: BaseResult.py:105
def __init__(self, row, col_names, decode_type='utf-8')
Definition: BaseResult.py:19