def get_nodenum(self, nodestatus): """Get the count of node with given status in current replica group. Parameters: Name Type Info: nodestatus int The specified status, see Info as below. Return values: the count of node Exceptions: pysequoiadb.error.SDBTypeError 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(nodestatus, int): raise SDBTypeError("nodestatus be an instance of int") if nodestatus not in common.NODE_STATUS.available_options() : raise SDBTypeError("nodestatus invalid") try: rc, nodenum = sdb.gp_get_nodenum(self._group, nodestatus) pysequoiadb._raise_if_error("Failed to get count of node", rc) except SDBBaseError: nodenum = 0 raise return nodenum
def seek(self, seek_pos, whence = 0) : """seek in lob. Parameters: Name Type Info: seek_pos int The length to seek whence int whence of seek, it must be 0/1/2 0 means seek from begin to end of lob 1 means seek from currend position to end of lob 2 means seek from end to begin of lob Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ if not isinstance(seek_pos, int): raise SDBTypeError("seek_pos must be an instance of int") if not isinstance(whence, int): raise SDBTypeError("seek_pos must be an instance of int") if whence not in (0, 1, 2): raise InvalidParameter("value of whence is in valid", const.SDB_INVALIDARG) try: rc = sdb.lob_seek(self._handle, seek_pos, whence) pysequoiadb._raise_if_error("Failed to seek lob", rc) except SDBBaseError: raise
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 bulk_insert(self, flags, records): """Insert a bulk of record into current collection. Parameters: Name Type Info: flags int 0 or 1, see Info as below. records list/tuple The list of inserted records. Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError Info: flags : 0 or 1. 0 : stop insertting record when an error occurred 1 : continue insertting records even though error occurred """ if not isinstance(flags, int): raise SDBTypeError("flags must be an instance of int") container = [] for elem in records: if not isinstance(elem, dict): raise SDBTypeError("record must be an instance of dict") record = bson.BSON.encode(elem) container.append(record) try: rc = sdb.cl_bulk_insert(self._cl, flags, container) pysequoiadb._raise_if_error("Failed to insert records", rc) except SDBBaseError: raise
def split_async_by_percent(self, source_group_name, target_group_name, percent): """Split the specified collection from source replica group to target replica group by percent. Parameters: Name Type Info: source_group_name str The source replica group name. target_group_name str The target replica group name. percent float The split percent, Range:(0,100] Return values: task id Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ if not isinstance(source_group_name, basestring): raise SDBTypeError( "source group name must be an instance of basestring") if not isinstance(target_group_name, basestring): raise SDBTypeError( "target group name must be an instance of basestring") if not isinstance(percent, float): raise SDBTypeError("percent must be an instance of float") try: rc, task_id = sdb.cl_splite_async_by_percent( self._cl, source_group_name, target_group_name, percent) pysequoiadb._raise_if_error("Failed to split async", rc) except SDBBaseError: task_id = 0 raise return task_id
def split_by_condition(self, source_group_name, target_group_name, split_condition, split_end_condition=None): """Split the specified collection from source replica group to target replica group by range. Parameters: Name Type Info: source_group_name str The source replica group name. target_group_name str The target replica group name. split_condition dict The matching rule, return the count of all documents if None. split_end_condition dict The split end condition or None. eg: If we create a collection with the option { ShardingKey:{"age":1}, ShardingType:"Hash",Partition:2^10 }, we can fill {age:30} as the splitCondition, and fill {age:60} as the splitEndCondition. when split, the target replica group will get the records whose age's hash value are in [30,60). If splitEndCondition is null, they are in [30,max). Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ if not isinstance(source_group_name, basestring): raise SDBTypeError( "source group name must be an instance of basestring") if not isinstance(target_group_name, basestring): raise SDBTypeError( "target group name must be an instance of basestring") bson_split_condition = None if split_condition is not None: if not isinstance(split_condition, dict): raise SDBTypeError( "split condition must be an instance of dict") bson_split_condition = bson.BSON.encode(split_condition) bson_end_condition = None if split_end_condition is not None: if not isinstance(split_end_condition, dict): raise SDBTypeError( "split end condition must be an instance of dict") bson_end_condition = bson.BSON.encode(split_end_condition) try: rc = sdb.cl_split_by_condition(self._cl, source_group_name, target_group_name, bson_split_condition, bson_end_condition) pysequoiadb._raise_if_error("Failed to split", rc) except SDBBaseError: raise
def create_node(self, hostname, servicename, dbpath, config = None): """Create 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. dbpath str The database path for the node. config dict The configurations for the node. Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ if not isinstance(hostname, basestring): raise SDBTypeError("host must be an instance of basestring") if not isinstance(servicename, basestring): raise SDBTypeError("service name must be an instance of basestring") if not isinstance(dbpath, basestring): raise SDBTypeError("path must be an instance of basestring") if config is not None and not isinstance(config, dict): raise SDBTypeError("config must be an instance of dict") if config is None: config = {} try: rc = sdb.gp_create_node(self._group, hostname, servicename, dbpath, config) pysequoiadb._raise_if_error("Failed to create node", rc) except SDBBaseError: raise
def remove_node(self, hostname, servicename, config = None): """Remove 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.SDBTypeError pysequoiadb.error.SDBBaseError """ if not isinstance(hostname, basestring): raise SDBTypeError("host must be an instance of basestring") if not isinstance(servicename, basestring): raise SDBTypeError("service name must be an instance of basestring") if config is not None and not isinstance(config, dict): raise SDBTypeError("config must be an instance of dict") try: if config is not None: bson_config = bson.BSON.encode(config) rc = sdb.gp_remove_node(self._group, hostname, servicename, bson_config) else: rc = sdb.gp_remove_node(self._group, hostname, servicename) pysequoiadb._raise_if_error("Failed to remove node", rc) except SDBBaseError: raise
def attach_collection(self, cl_full_name, options): """Attach the specified collection. Parameters: Name Type Info: subcl_full_name str The name fo the subcollection. options dict he low boudary and up boudary eg: {"LowBound":{a:1},"UpBound":{a:100}} Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ if not isinstance(cl_full_name, basestring): raise SDBTypeError("full name of subcollection must be \ an instance of basestring") if not isinstance(options, dict): raise SDBTypeError("options must be an instance of basestring") bson_options = None if options is not None: bson_options = bson.BSON.encode(options) try: rc = sdb.cl_attach_collection(self._cl, cl_full_name, bson_options) pysequoiadb._raise_if_error("Failed to attach collection", rc) except SDBBaseError: raise
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.SDBTypeError pysequoiadb.error.SDBBaseError """ if not isintance(hostname, basestring): raise SDBTypeError("hostname must be an instance of basestring") if not isintance(servicename, basestring): raise SDBTypeError("servicename must be an instance of basestring") try: node = replicanode(self._client) rc = sdb.gp_get_nodebyendpoint(self._group, node._node, hostname, servicename) pysequoiadb._raise_if_error("Failed to get node", rc) except SDBBaseError: del node node = None raise return ret, node
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 delete(self, **kwargs): """Delete the matching documents in current collection. Parameters: Name Type Info: **kwargs Useful options are below - condition dict The matching rule, delete all the documents if not provided. - hint dict The hint, automatically match the optimal hint if not provided Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ bson_condition = None bson_hint = None if "condition" in kwargs: if not isinstance(kwargs.get("condition"), dict): raise SDBTypeError("condition must be an instance of dict") bson_condition = bson.BSON.encode(kwargs.get("condition")) if "hint" in kwargs: if not isinstance(kwargs.get("hint"), dict): raise SDBTypeError("hint must be an instance of dict") bson_hint = bson.BSON.encode(kwargs.get("hint")) try: rc = sdb.cl_delete(self._cl, bson_condition, bson_hint) pysequoiadb._raise_if_error("Failed to delete", rc) except SDBBaseError: raise
def get_indexes(self, idx_name=None): """Get all of or one of the indexes in current collection. Parameters: Name Type Info: idx_name str The index name, returns all of the indexes if this parameter is None. Return values: a cursor object of result Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ if idx_name is not None and not isinstance(idx_name, basestring): raise SDBTypeError("index name must be an instance of basestring") if idx_name is None: idx_name = "" try: result = cursor() rc = sdb.cl_get_index(self._cl, result._cursor, idx_name) pysequoiadb._raise_if_error("Failed to get indexes", rc) except SDBBaseError: del result result = None raise return result
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_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 create_lob(self, oid=None): """create lob. Parameters: Name Type Info: oid bson.ObjectId Specified the oid of lob to be created, if None, the oid is generated automatically Return values: a lob object Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ if oid is None: str_id = None elif isinstance(oid, bson.ObjectId): str_id = str(oid) else: raise SDBTypeError("oid must be an instance of bson.ObjectId") try: obj = lob() rc = sdb.cl_create_lob(self._cl, obj._handle, str_id) pysequoiadb._raise_if_error("Failed to create lob", rc) except SDBBaseError: raise return obj
def get_lob(self, oid): """get the specified lob. Parameters: Name Type Info: oid str/bson.ObjectId The specified oid Return values: a lob object Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ if not isinstance(oid, bson.ObjectId) and not isinstance( oid, basestring): raise SDBTypeError("oid must be bson.ObjectId or string") if isinstance(oid, bson.ObjectId): str_id = str(oid) else: str_id = oid try: obj = lob() rc = sdb.cl_get_lob(self._cl, obj._handle, str_id) pysequoiadb._raise_if_error("Failed to get specified lob", rc) except SDBBaseError: raise return obj
def get_count(self, condition=None): """Get the count of matching documents in current collection. Parameters: Name Type Info: condition dict The matching rule, return the count of all documents if None. Return values: count of result Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ bson_condition = None if condition is not None: if not isinstance(condition, dict): raise SDBTypeError("condition must be an instance of dict") bson_condition = bson.BSON.encode(condition) try: rc, count = sdb.cl_get_count(self._cl, bson_condition) pysequoiadb._raise_if_error("Failed to get count of record", rc) except SDBBaseError: count = 0 raise return count
def get_collection(self, cl_name): """Get the named collection. Parameters: Name Type Info: cl_name str The full name of the collection.. Return values: a collection object of query Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ if not isinstance(cl_name, basestring): raise SDBTypeError("collection must be an instance of basestring") try: cl = collection() rc = sdb.cs_get_collection(self._cs, cl_name, cl._cl) pysequoiadb._raise_if_error("Failed to get collection: %s" % cl_name, rc) except SDBBaseError: del cl cl = None raise return cl
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 update(self, rule, **kwargs): """Update the matching documents in current collection. Parameters: Name Type Info: rule dict The updating rule. **kwargs Useful option are below - condition dict The matching rule, update all the documents if not provided. - hint dict The hint, automatically match the optimal hint if not provided Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError Note: It won't work to update the "ShardingKey" field, but the other fields take effect. """ if not isinstance(rule, dict): raise SDBTypeError("rule must be an instance of dict") bson_rule = bson.BSON.encode(rule) bson_condition = None bson_hint = None if "condition" in kwargs: if not isinstance(kwargs.get("condition"), dict): raise SDBTypeError( "condition in kwargs must be an instance of dict") bson_condition = bson.BSON.encode(kwargs.get("condition")) if "hint" in kwargs: if not isinstance(kwargs.get("hint"), dict): raise SDBTypeError( "hint in kwargs must be an instance of dict") bson_hint = bson.BSON.encode(kwargs.get("hint")) try: rc = sdb.cl_update(self._cl, bson_rule, bson_condition, bson_hint) pysequoiadb._raise_if_error("Failed to update", rc) except SDBBaseError: raise
def create_index(self, index_def, idx_name, is_unique, is_enforced): """Create the index in current collection. Parameters: Name Type Info: index_def dict The dict object of index element. e.g. {name:1, age:-1} idx_name str The index name. is_unique bool Whether the index elements are unique or not. is_enforced bool Whether the index is enforced unique This element is meaningful when isUnique is set to true. Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ if not isinstance(index_def, dict): raise SDBTypeError("index definition must be an instance of dict") if not isinstance(idx_name, basestring): raise SDBTypeError("index name must be an instance of basestring") if not isinstance(is_unique, bool): raise SDBTypeError("is_unique must be an instance of bool") if not isinstance(is_enforced, bool): raise SDBTypeError("is_enforced must be an instance of bool") unique = 0 enforce = 0 bson_index_def = bson.BSON.encode(index_def) if is_unique: unique = 1 if is_enforced: enforced = 1 try: rc = sdb.cl_create_index(self._cl, bson_index_def, idx_name, is_unique, is_enforced) pysequoiadb._raise_if_error("Failed to create index", rc) except SDBBaseError: raise
def create_collection(self, cl_name, options = None): """create a collection using name and options. Parameters: Name Type Info: cl_name str The collection name. options dict The options for creating collection, including "ShardingKey", "ReplSize", "IsMainCL" and "Compressed" informations, no options, if None. Return values: a collection object created Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ if not isinstance(cl_name, basestring): raise SDBTypeError("collection must be an instance of basestring") bson_options = None if options is not None: if not isinstance(options, dict): raise SDBTypeError("options must be an instance of dict") bson_options = bson.BSON.encode(options) try: cl = collection() if bson_options is None: rc = sdb.cs_create_collection(self._cs, cl_name, cl._cl) else: rc = sdb.cs_create_collection_use_opt(self._cs, cl_name, bson_options, cl._cl) pysequoiadb._raise_if_error("Failed to create collection", rc) except SDBBaseError: del cl cl = None raise return cl
def aggregate(self, aggregate_options): """Execute aggregate operation in specified collection. Parameters: Name Type Info: aggregate_options list/tuple The array of dict objects. bson.SON may need if the element is order-sensitive. eg. {'$sort':bson.SON([("name",-1), ("age":1)])} it will be ordered descending by 'name' first, and be ordered ascending by 'age' Return values: a cursor object of result Exceptions: pysequoiadb.error.SDBBaseError """ if not isinstance(aggregate_options, list): raise SDBTypeError("aggregate options must be an instance of list") container = [] for option in aggregate_options: if not isinstance(option, dict): raise SDBTypeError("options must be an instance of dict") bson_option = bson.BSON.encode(option) container.append(bson_option) try: result = cursor() rc = sdb.cl_aggregate(self._cl, result._cursor, container) pysequoiadb._raise_if_error("Failed to aggregate", rc) except SDBBaseError: del result result = None raise return result
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 create_indexEE(self, cs_name, cl_name, index_def1, **kwargs): sdb = self.db if not sdb.is_valid(): print "Sdb connection is invalid" exit(1) is_unique = False is_enforced = False buffer_size = 64 index_def = eval(index_def1) #index_def = index_def1 #print 'index_def: %s' % index_def if not isinstance(index_def, list): raise SDBTypeError("Index definition must be an instance of tuple") if "is_unique" in kwargs and isinstance(kwargs.get("is_unique"), bool): is_unique = kwargs.get("is_unique") if "is_enforced" in kwargs and isinstance(kwargs.get("is_enforced"), bool): is_enforced = kwargs.get("is_enforced") if "buffer_size" in kwargs and isinstance(kwargs.get("buffer_size"), int): buffer_size = kwargs.get("buffer_size") for i in range(len(index_def)): if not isinstance(index_def[i], dict): print "Every object in index definition must be an instance of dict" print "Please check parameter: %s" % index_def[i] continue index_name = '' for key in index_def[i]: index_name = index_name + key index_name = index_name + '_' index_name = index_name + 'Idx' try: cs = sdb.get_collection_space(cs_name) cl = cs.get_collection(cl_name) cl.create_index(index_def[i], index_name, is_unique, is_enforced, buffer_size) print "Create index %s OK, full cl: %s.%s, index definition: %s" % ( index_name, cs_name, cl_name, index_def[i]) except (SDBTypeError, SDBBaseError), e: pysequoiadb._print(e)
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 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, basestring): raise SDBTypeError("collection must be an instance of basestring") try: rc = sdb.cs_drop_collection(self._cs, cl_name) pysequoiadb._raise_if_error("Failed to drop collection", rc) except SDBBaseError: raise
def drop_index(self, idx_name): """The index name. Parameters: Name Type Info: idx_name str The index name. Exceptions: pysequoiadb.error.SDBTypeError pysequoiadb.error.SDBBaseError """ if not isinstance(idx_name, basestring): raise SDBTypeError("index name must be an instance of basestring") try: rc = sdb.cl_drop_index(self._cl, idx_name) pysequoiadb._raise_if_error("Failed to drop index", rc) except SDBBaseError: raise