def next(self, ordered=False): """Return the next document of current cursor, and move forward. Parameters: Name Type Info: ordered bool Set true if need field-ordered records, default false. Return values: a dict object of record Exceptions: pysequoiadb.error.SDBEndOfCursor pysequoiadb.error.SDBBaseError """ if not isinstance(ordered, bool): raise SDBTypeError("ordered must be an instance of bool") as_class = dict if ordered: as_class = OrderedDict rc, bson_string = sdb.cr_next(self._cursor) raise_if_error(rc, "Failed to get next record") record, size = bson._bson_to_dict(bson_string, as_class, False, bson.OLD_UUID_SUBTYPE, True) return record
def get_nodebyendpoint(self, hostname, servicename): """Get specified node from current replica group. Parameters: Name Type Info: hostname str The host name of the node. servicename str The service name of the node. Return values: a replicanode object of query Exceptions: pysequoiadb.error.SDBBaseError """ if not isinstance(hostname, str_type): raise SDBTypeError("hostname must be an instance of str_type") if not isinstance(servicename, str_type): raise SDBTypeError("servicename must be an instance of str_type") node = replicanode(self._client) try: rc = sdb.gp_get_nodebyendpoint(self._group, node._node, hostname, servicename) raise_if_error(rc, "Failed to get node") except SDBBaseError: del node raise return node
def __getattr__(self, name): """support client.cs to access to collection. eg. cc = client() cs = cc.test cl = cs.test_cl # access to collection named 'test_cl' and we should pass '__members__' and '__methods__', becasue dir(cc) will invoke __getattr__("__members__") and __getattr__("__methods__"). if success, a collection object will be returned. Exceptions: pysequoiadb.error.SDBBaseError """ if '__members__' == name or '__methods__' == name: pass else: cl = collection() try: rc = sdb.cs_get_collection(self._cs, name, cl._cl) raise_if_error(rc, "Failed to get collection: %s" % name) except SDBBaseError: del cl raise return cl
def detach_node(self, hostname, servicename, config=None): """Detach node in a given replica group. Parameters: Name Type Info: hostname str The host name for the node. servicename str The servicename for the node. config dict The configurations for the node. Exceptions: pysequoiadb.error.SDBBaseError """ if not isinstance(hostname, str_type): raise SDBTypeError("host must be an instance of str_type") if not isinstance(servicename, str_type): raise SDBTypeError("service name must be an instance of str_type") if config is not None and not isinstance(config, dict): raise SDBTypeError("config must be an instance of dict") if config is None: config = {} bson_options = bson.BSON.encode(config) rc = sdb.gp_detach_node(self._group, hostname, servicename, bson_options) raise_if_error(rc, "Failed to detach node")
def get_slave(self, *positions): """Get one of slave node of the current replica group, if no slave exists then get master. Parameters: Name Type Info: positions int The positions of nodes. Return values: a replicanode object of query Exceptions: pysequoiadb.error.SDBBaseError """ for i in range(len(positions)): if not isinstance(positions[i], int): raise SDBTypeError( "elements of positions should be instance of int") node = replicanode(self._client) try: rc = sdb.gp_get_slave(self._group, node._node, positions) raise_if_error(rc, "Failed to get slave") except SDBBaseError: del node raise return node
def get_nodenum(self, node_status): """Get the count of node with given status in current replica group. Parameters: Name Type Info: node_status int The specified status, see Info as below. Return values: the count of node Exceptions: pysequoiadb.error.SDBBaseError Info: flags : 0 or 1. 0 : count of all node 1 : count of actived node 2 : count of inactived node 3 : count of unknown node """ if not isinstance(node_status, int): raise SDBTypeError("node status be an instance of int") if node_status not in (NODE_STATUS_ALL, NODE_STATUS_ACTIVE, NODE_STATUS_INACTIVE, NODE_STATUS_UNKNOWN): raise SDBTypeError("node status invalid") rc, node_num = sdb.gp_get_nodenum(self._group, node_status) raise_if_error(rc, "Failed to get count of node") return node_num
def close(self): """close lob Exceptions: pysequoiadb.error.SDBBaseError """ rc = sdb.lob_close(self._handle) raise_if_error(rc, "Failed to close lob")
def stop(self): """Stop current replica group. Exceptions: pysequoiadb.error.SDBBaseError """ rc = sdb.gp_stop(self._group) raise_if_error(rc, "Failed to stop")
def start(self): """Start the node. Exceptions: pysequoiadb.error.SDBBaseError """ rc = sdb.nd_start(self._node) raise_if_error(rc, "Filed to start node")
def close(self): """Close the cursor's connection to database, we can't use this handle to get data again. Exceptions: pysequoiadb.error.SDBBaseError """ rc = sdb.cr_close(self._cursor) raise_if_error(rc, "Failed to close cursor")
def __del__(self): """release cursor Exceptions: pysequoiadb.error.SDBBaseError """ if self._cursor is not None: rc = sdb.release_cursor(self._cursor) raise_if_error(rc, "Failed to release cursor") self._cursor = None
def __del__(self): """delete a object existed. Exceptions: pysequoiadb.error.SDBBaseError """ if self._cs is not None: rc = sdb.release_cs(self._cs) raise_if_error(rc, "Failed to release collection space") self._cs = None
def get_create_time(self): """get create time of lob Return Values: a long int of time Exceptions: pysequoiadb.error.SDBBaseError """ rc, mms = sdb.lob_get_create_time(self._handle) raise_if_error(rc, "Failed to get create time of lob") return mms
def get_nodename(self): """Get node name of the current node. Return values: the name of node Exceptions: pysequoiadb.error.SDBBaseError """ rc, node_name = sdb.nd_get_nodename(self._node) raise_if_error(rc, "Failed to get node name") return node_name
def get_size(self): """get the size of lob. Return Values: the size of current lob Exceptions: pysequoiadb.error.SDBBaseError """ rc, size = sdb.lob_get_size(self._handle) raise_if_error(rc, "Failed to get size of lob") return size
def __del__(self): """release domain Exceptions: pysequoiadb.error.SDBBaseError """ self._domain_name = None if self._domain is not None: rc = sdb.release_domain(self._domain) raise_if_error(rc, "Failed to release domain") self._domain = None
def get_status(self): """Get status of the current node Return values: the status of node Exceptions: pysequoiadb.error.SDBBaseError """ rc, node_status = sdb.nd_get_status(self._node) raise_if_error(rc, "Failed to get node status") return node_status
def __del__(self): """release replica node Exceptions: pysequoiadb.error.SDBBaseError """ if self._node is not None: rc = sdb.release_node(self._node) raise_if_error(rc, "Failed to release node") self._node = None self._client = None
def get_oid(self): """get the oid of lob. Return Values: the oid of current lob Exceptions: pysequoiadb.error.SDBBaseError """ rc, id_str = sdb.lob_get_oid(self._handle) raise_if_error(rc, "Failed to get oid of lob") oid = bson.ObjectId(id_str) return oid
def __del__(self): """release replica group object Exceptions: pysequoiadb.error.SDBBaseError """ if self._group is not None: rc = sdb.release_group(self._group) raise_if_error(rc, "Failed to release replica group") self._group = None self._client = None
def get_collection_space_name(self): """Get the current collection space name. Return values: The name of current collection space. Exceptions: pysequoiadb.error.SDBBaseError """ rc, cs_name = sdb.cs_get_collection_space_name(self._cs) raise_if_error(rc, "Failed to get collection space name") return cs_name
def get_detail(self): """Get the detail of the replica group. Return values: a dict object of query Exceptions: pysequoiadb.error.SDBBaseError """ rc, bson_string = sdb.gp_get_detail(self._group) raise_if_error(rc, "Failed to get detail") detail, size = bson._bson_to_dict(bson_string, dict, False, bson.OLD_UUID_SUBTYPE, True) return detail
def is_catalog(self): """Test whether current replica group is catalog replica group. Return values: bool Exceptions: pysequoiadb.error.SDBBaseError """ iscatalog = False rc, is_cata = sdb.gp_is_catalog(self._group) raise_if_error(rc, "Failed to check if is catalog") if TRUE == is_cata: iscatalog = True return iscatalog
def drop_collection(self, cl_name): """Drop the specified collection in current collection space. Parameters: Name Type Info: cl_name str The collection name. Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ if not isinstance(cl_name, str_type): raise SDBTypeError("collection must be an instance of str_type") rc = sdb.cs_drop_collection(self._cs, cl_name) raise_if_error(rc, "Failed to drop collection")
def write(self, data, length): """write data into lob. Parameters: Name Type Info: data str The data to be written length int The length of data to be written Exceptions: pysequoiadb.error.SDBBaseError """ if not isinstance(data, str_type): raise SDBTypeError("data should be byte or string") rc = sdb.lob_write(self._handle, data, length) raise_if_error(rc, "Failed to write data to lob")
def lock_and_seek(self, offset, length): """lock lob data section and seek to the offset position. Parameters: Name Type Info: offset long(int in python3) The lock start position length long(int in python3) The lock length, -1 means lock from offset to the end of lob Exceptions: pysequoiadb.error.SDBBaseError """ if not isinstance(offset, (int, long_type)): raise SDBTypeError("seek_pos must be an instance of long/int") if not isinstance(length, (int, long_type)): raise SDBTypeError("seek_pos must be an instance of long/int") rc = sdb.lob_lock_and_seek(self._handle, offset, length) raise_if_error(rc, "Failed to lock lob")
def list_collection_spaces(self): """List all collection spaces in this domain. Return values: The cursor object of collection spaces. Exceptions: pysequoiadb.error.SDBBaseError """ result = cursor() try: rc = sdb.domain_list_cs(self._domain, result._cursor) raise_if_error(rc, "Failed to list collection spaces of %s" % self._domain_name) except: del result raise return result
def read(self, length): """ream data from lob. Parameters: Name Type Info: length int The length of data to be read Return Values: binary data of read Exceptions: pysequoiadb.error.SDBBaseError """ if not isinstance(length, int): raise SDBTypeError("len must be an instance of int") rc, data = sdb.lob_read(self._handle, length) raise_if_error(rc, "Failed to read data from lob") return data
def get_master(self): """Get the master node of the current replica group. Return values: a replica node object of query Exceptions: pysequoiadb.error.SDBBaseError """ node = replicanode(self._client) try: rc = sdb.gp_get_master(self._group, node._node) raise_if_error(rc, "Failed to get master") except SDBBaseError: del node raise return node
def get_slave(self): """Get one of slave node of the current replica group, if no slave exists then get master. Return values: a replicanode object of query Exceptions: pysequoiadb.error.SDBBaseError """ node = replicanode(self._client) try: rc = sdb.gp_get_slave(self._group, node._node) raise_if_error(rc, "Failed to get slave") except SDBBaseError: del node raise return node