NebulaGraph Python Client  release-3.8
__init__.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 import socket
10 
11 from nebula3.Exception import InValidHostname
12 from nebula3.storage import GraphStorageService
13 from nebula3.fbthrift.transport import TSocket, THeaderTransport, TTransport
14 from nebula3.fbthrift.protocol import THeaderProtocol
15 
16 
17 class GraphStorageConnection(object):
18  def __init__(self, address, timeout, meta_cache):
19  self._address_address = address
20  self._timeout_timeout = timeout
21  self._meta_cache_meta_cache = meta_cache
22  self._connection_connection = None
23  self._ip_ip = ''
24  try:
25  self._ip_ip = socket.gethostbyname(address.host)
26  if not isinstance(address.port, int):
27  raise RuntimeError('Wrong port type: {}'.format(type(address.port)))
28  except Exception:
29  raise InValidHostname(str(address.host))
30 
31  def open(self):
32  try:
33  self.closeclose()
34  s = TSocket.TSocket(self._address_address.host, self._address_address.port)
35  if self._timeout_timeout > 0:
36  s.setTimeout(self._timeout_timeout)
37 
38  buffered_transport = TTransport.TBufferedTransport(s)
39  header_transport = THeaderTransport.THeaderTransport(buffered_transport)
40  protocol = THeaderProtocol.THeaderProtocol(header_transport)
41  header_transport.open()
42 
43  self._connection_connection = GraphStorageService.Client(protocol)
44  except Exception:
45  raise
46 
47  def scan_vertex(self, req):
48  return self._connection_connection.scanVertex(req)
49 
50  def scan_edge(self, req):
51  return self._connection_connection.scanEdge(req)
52 
53  def storage_addr(self):
54  return self._address_address
55 
56  def update_leader_info(self, space_id, part_id, address):
57  self._meta_cache_meta_cache.update_storage_leader(space_id, part_id, address)
58 
59  def close(self):
60  try:
61  if self._connection_connection is not None:
62  self._connection_connection._iprot.trans.close()
63  except Exception:
64  raise
65 
66  def __del__(self):
67  self.closeclose()