Esempio n. 1
0
   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
Esempio n. 2
0
   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
Esempio n. 3
0
   def create_replica_cata_group(self, host, service, path, configure):
      """Create a catalog replica group.

      Parameters:
         Name         Type     Info:
         host         str      The hostname for the catalog replica group.
         service      str      The servicename for the catalog replica group.
         path         str      The path for the catalog replica group.
         configure    dict     The configurations for the catalog replica group.
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
      if not isinstance(host, basestring):
         raise SDBTypeError("host must be an instance of basestring")
      if not isinstance(service, basestring):
         raise SDBTypeError("service name must be an instance of basestring")
      if not isinstance(path, basestring):
         raise SDBTypeError("path must be an instance of basestring")
      if not isinstance(configure, dict):
         raise SDBTypeError("configure must be an instance of dict")

      bson_configure = bson.BSON.encode(configure)

      try:
         rc = sdb.sdb_create_replica_cata_group(self._client, host, service,
                                                  path, bson_configure)
         pysequoiadb._raise_if_error("Failed to create replica cate group", rc)
      except SDBBaseError:
         raise
Esempio n. 4
0
    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
Esempio n. 5
0
   def exec_sql(self, sql):
      """Executing SQL command.

      Parameters:
         Name         Type     Info:
         sql          str      The SQL command.
      Return values:
         a cursor object of matching documents.
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
      if not isinstance(sql, basestring):
         raise SDBTypeError("sql must be an instance of basestring")

      try:
         result = cursor()
         rc = sdb.sdb_exec_sql(self._client, sql, result._cursor)
         pysequoiadb._raise_if_error("Failed to execute sql command", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      return result
Esempio n. 6
0
   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
Esempio n. 7
0
    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
Esempio n. 8
0
   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:
         try:
            cl = collection()
            rc = sdb.cs_get_collection(self._cs, name, cl._cl)
            pysequoiadb._raise_if_error("Failed to get collection: %s" %
                                        name, rc)
         except SDBBaseError:
            del cl;
            cl = None
            raise

         return cl
Esempio n. 9
0
    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
Esempio n. 10
0
   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
Esempio n. 11
0
   def create_replica_group(self, group_name):
      """Create the specified replica group.

      Parameters:
         Name        Type     Info:
         group_name  str      The name of replica group to be created.
      Return values:
         the replicagroup object created.
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
      if not isinstance(group_name, basestring):
         raise SDBTypeError("group name must be an instance of basestring")

      try:
         replica_group = replicagroup(self._client)
         rc = sdb.sdb_create_replica_group(self._client, group_name,
                                             replica_group._group)
         pysequoiadb._raise_if_error("Failed to create replica group", rc)
      except SDBBaseError:
         del replica_group
         replica_group = None
         raise

      return replica_group
Esempio n. 12
0
   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
Esempio n. 13
0
   def eval_procedure(self, name):
      """Eval a func.
      
      Parameters:
         Name         Type     Info:
         name         str      The name of store procedure.
      Return values:
         cursor object of current eval.
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
      if not isinstance(name, basestring):
         raise SDBTypeError("code must be an instance of basestring")

      try:
         result = cursor()
         rc = sdb.sdb_eval_JS(self._client, result._cursor, name)
         pysequoiadb._raise_if_error("Failed to eval procedure", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      return result
Esempio n. 14
0
    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
Esempio n. 15
0
   def get_nodebyname(self,nodename):
      """Get specified node from current replica group.
      
      Parameters:
         Name         Type     Info:
         nodename     str      The host name of the node.
      Return values:
         a replicanode object of query
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
      if not isintance(nodename, basestring):
         raise SDBTypeError("nodename must be an instance of basestring")

      try:
         node = replicanode(self._client)
         rc = sdb.gp_get_nodebyname(self._group, node._node, nodename)
         pysequoiadb._raise_if_error("Failed to get node", rc)
      except SDBBaseError:
         del node
         node = None
         raise

      return node
Esempio n. 16
0
    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
Esempio n. 17
0
   def get_replica_group_by_name(self, group_name):
      """Get the specified replica group of specified group name.

      Parameters:
         Name         Type     Info:
         group_name   str      The name of replica group.
      Return values:
         the replicagroup object of query.
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
      if not isinstance(group_name, basestring):
         raise SDBTypeError("group name must be an instance of basestring")

      try:
         result = replicagroup(self._client)
         rc = sdb.sdb_get_replica_group_by_name(self._client, group_name,
                                                  result._group)
         pysequoiadb._raise_if_error("Failed to get specified group", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      return result
Esempio n. 18
0
   def get_replica_group_by_id(self, id):
      """Get the specified replica group of specified group id.

      Parameters:
         Name       Type     Info:
         id         str      The id of replica group.
      Return values:
         the replicagroup object of query.
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
      if not isinstance(id, int):
         raise SDBTypeError("group id must be an instance of int")

      try:
         result = replicagroup(self._client)
         rc = sdb.sdb_get_replica_group_by_id(self._client, id, result._group)
         pysequoiadb._raise_if_error("Failed to get specified group", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      return result
Esempio n. 19
0
   def get_collection_space(self, cs_name):
      """Get the specified collection space.

      Parameters:
         Name         Type     Info:
         cs_name      str      The name of collection space.
      Return values:
         a collection space object of query.
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
      if not isinstance(cs_name, basestring):
         raise SDBTypeError("name of collection space must be an instance of basestring")

      try:
         cs = collectionspace()
         rc = sdb.sdb_get_collection_space(self._client, cs_name, cs._cs)
         pysequoiadb._raise_if_error("Failed to get collection space", rc)
      except SDBBaseError:
         del cs
         cs = None
         raise

      return cs
Esempio n. 20
0
   def get_collection(self, cl_full_name):
      """Get the specified collection.

      Parameters:
         Name         Type     Info:
         cl_full_name str      The full name of collection
      Return values:
         a collection object of query.
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
      if not isinstance(cl_full_name, basestring):
         raise SDBTypeError("full name of collection must be an instance of basestring")
      if '.' not in cl_full_name:
         raise SDBTypeError("Full name must included '.'")

      try:
         cl = collection()
         rc = sdb.sdb_get_collection(self._client, cl_full_name, cl._cl)
         pysequoiadb._raise_if_error("Failed to get collection", rc)
      except SDBBaseError:
         del cl
         cl = None
         raise

      return cl
Esempio n. 21
0
   def __getattr__(self, name):
      """support client.cs to access to collection space.

         eg.
         cc = client()
         cs = cc.test # access to collection space named 'test'

         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, or None.
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
      if '__members__' == name or '__methods__' == name:
         pass
      else:
         try:
            cs = collectionspace()
            rc = sdb.sdb_get_collection_space(self._client, name, cs._cs)
            pysequoiadb._raise_if_error("Failed to get collection space: %s" %
                                        name, rc)
         except SDBBaseError:
            del cs;
            cs = None
            raise

         return cs
Esempio n. 22
0
    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
Esempio n. 23
0
    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
Esempio n. 24
0
   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
Esempio n. 25
0
   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
Esempio n. 26
0
    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
Esempio n. 27
0
   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
Esempio n. 28
0
   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
Esempio n. 29
0
   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
Esempio n. 30
0
   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
Esempio n. 31
0
   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
Esempio n. 32
0
    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
Esempio n. 33
0
   def cancel_task(self, task_id, is_async):
      """Cancel the specified task.

      Parameters:
         Name         Type     Info:
         task_id      long     The task id to be canceled.
         is_async     bool     The operation "cancel task" is async or not,
                                     "True" for async, "False" for sync.
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
      if not isinstance(task_id, long):
         raise SDBTypeError("task id must be an instance of list")

      async = 0
      if isinstance(is_async, bool):
         if is_async:
            async = 1
      else:
         raise SDBTypeError("size of tasks must be an instance of int")

      try:
         rc = sdb.sdb_cancel_task(self._client, task_id, async)
         pysequoiadb._raise_if_error("Failed to cancel task", rc)
      except SDBBaseError:
         raise
Esempio n. 34
0
   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
Esempio n. 35
0
   def list_procedures(self, condition):
      """List store procedures.

      Parameters:
         Name         Type     Info:
         condition    dict     The condition of list.
      Return values:
         an cursor object of result
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
      if not isinstance(condition, dict):
         raise SDBTypeError("condition must be an instance of dict")

      bson_condition = bson.BSON.encode(condition)
      try:
         result = cursor()
         rc = sdb.sdb_list_procedures(self._client, result._cursor,
                                        bson_condition)
         pysequoiadb._raise_if_error("Failed to list procedures", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      return result
Esempio n. 36
0
   def split_async_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).
      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")

      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, task_id = sdb.cl_split_async_by_condition(self._cl,
                                                      source_group_name,
                                                      target_group_name,
                                                      bson_split_condition,
                                                      bson_end_condition)
         pysequoiadb._raise_if_error("Failed to split async", rc)

      except SDBBaseError:
         task_id = 0
         raise

      return task_id
Esempio n. 37
0
 def __del__(self) :
    if self._handle is not None:
       try:
          rc = sdb.release_lob(self._handle)
          pysequoiadb._raise_if_error("Failed to release lob", rc)
       except SDBBaseError:
          raise
       self._handle = None
Esempio n. 38
0
 def __del__(self):
    if self._dc is not None:
       try:
          rc = sdb.release_dc(self._cursor)
          pysequoiadb._raise_if_error("Failed to release data center", rc)
       except SDBBaseError:
          raise
       self._dc = None
Esempio n. 39
0
   def list_task(self, **kwargs):
      """List the tasks.

      Parameters:
         Name           Type     Info:
         **kwargs                Useful options are below
         - condition    dict     The matching rule, return all the documents
                                       if None.
         - selector     dict     The selective rule, return the whole
                                       document if None.
         - order_by     dict     The ordered rule, never sort if None.
                                       bson.SON may need if it is order-sensitive.
                                       eg.
                                       bson.SON([("name",-1), ("age":1)]) it will
                                       be ordered descending by 'name' first, and
                                       be ordered ascending by 'age'
         - hint         dict     The hint, automatically match the optimal
                                       hint if None.
      Return values:
         a cursor object of task list
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
      bson_condition = None
      bson_selector = None
      bson_order_by = None
      bson_hint = None

      if "condition" in kwargs:
         if not isinstance(kwargs.get("condition", dict)):
            raise SDBTypeError("consition in kwargs must be an instance of dict")
         bson_condition = bson.BSON.encode(kwargs.get("condition"))
      if "selector" in kwargs:
         if not isinstance(kwargs.get("selector", dict)):
            raise SDBTypeError("selector in kwargs must be an instance of dict")
         bson_selector = bson.BSON.encode(kwargs.get("selector"))
      if "order_by" in kwargs:
         if not isinstance(kwargs.get("order_by", dict)):
            raise SDBTypeError("order_by in kwargs must be an instance of dict")
         bson_order_by = bson.BSON.encode(kwargs.get("order_by"))
      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:
         result = cursor()
         rc = sdb.sdb_list_task(self._client, result._cursor, bson_condition,
                                  bson_selector, bson_order_by, bson_hint)
         pysequoiadb._raise_if_error("Failed to list tasks", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      return result
Esempio n. 40
0
 def __del__(self):
     if self._dc is not None:
         try:
             rc = sdb.release_dc(self._cursor)
             pysequoiadb._raise_if_error("Failed to release data center",
                                         rc)
         except SDBBaseError:
             raise
         self._dc = None
Esempio n. 41
0
    def deactivate(self):
        """Deactive data center

      Exceptions:
         pysequoiadb.error.SDBBaseError
      """
        try:
            rc = sdb.dc_deactivate(self._dc)
            pysequoiadb._raise_if_error("Failed to make data center deactive")
        except SDBBaseError:
            raise
Esempio n. 42
0
 def connect(self):
     """Connect to the current node.
   
   Exceptions:
      pysequoiadb.error.SDBBaseError
   """
     try:
         rc = sdb.nd_connect(self._node, self._client)
         pysequoiadb._raise_if_error("Failed to connect", rc)
     except SDBBaseError:
         raise
Esempio n. 43
0
    def remove_image(self):
        """Remove an image

      Exceptions:
         pysequoiadb.error.SDBBaseError
      """
        try:
            rc = sdb.dc_remove_image(self._dc)
            pysequoiadb._raise_if_error("Failed to remove data center", rc)
        except SDBBaseError:
            raise
Esempio n. 44
0
 def stop(self):
    """Stop current replica group.
    
    Exceptions:
       pysequoiadb.error.SDBBaseError
    """
    try:
       rc = sdb.gp_stop(self._group)
       pysequoiadb._raise_if_error("Failed to stop", rc)
    except SDBBaseError:
       raise
Esempio n. 45
0
 def start(self):
     """Start the node.
   
   Exceptions:
      pysequoiadb.error.SDBBaseError
   """
     try:
         rc = sdb.nd_start(self._node)
         pysequoiadb._raise_if_error("Filed to start node", rc)
     except SDBBaseError:
         raise
Esempio n. 46
0
   def close(self):
      """close lob

      Exceptions:
         pysequoiadb.error.SDBBaseError
      """
      try:
         rc = sdb.lob_close(self._handle)
         pysequoiadb._raise_if_error("Failed to close lob", rc)
      except SDBBaseError:
         raise
Esempio n. 47
0
    def disable_image(self):
        """Disable image in data center

      Exceptions:
         pysequoiadb.error.SDBBaseError
      """
        try:
            rc = sdb.dc_disable_image(self._dc)
            pysequoiadb._raise_if_error("Failed to disable image", rc)
        except SDBBaseError:
            raise
Esempio n. 48
0
    def disabel_read_only(self):
        """Disable data center to be read-only

      Exceptions:
         pysequoiadb.error.SDBBaseError
      """
        try:
            rc = sdb.dc_disable_read_only(self._dc)
            pysequoiadb._raise_if_error(
                "Failed to disable data center read-only", rc)
        except SDBBaseError:
            raise
Esempio n. 49
0
    def close(self):
        """Close the cursor's connection to database, we can't use this handle to
         get data again.

      Exceptions:
         pysequoiadb.error.SDBBaseError
      """
        try:
            rc = sdb.cr_close(self._cursor)
            pysequoiadb._raise_if_error("Failed to close cursor", rc)
        except SDBBaseError:
            raise
Esempio n. 50
0
    def activate(self):
        """Active data center

      Exceptions:
         pysequoiadb.error.SDBBaseError
      """
        try:
            rc = sdb.dc_activate(self._dc)
            pysequoiadb._raise_if_error("Failed to make date center activate",
                                        rc)
        except SDBBaseError:
            raise
Esempio n. 51
0
    def __del__(self):
        """release cursor

      Exceptions:
         pysequoiadb.error.SDBBaseError
      """
        if self._cursor is not None:
            try:
                rc = sdb.release_cursor(self._cursor)
                pysequoiadb._raise_if_error("Failed to release cursor", rc)
            except SDBBaseError:
                raise
            self._cursor = None
Esempio n. 52
0
   def get_size(self) :
      """get the size of lob.

      Return Values:
         the size of current lob
      Exceptions:
         pysequoiadb.error.SDBBaseError
      """
      try:
         rc, size = sdb.lob_get_size(self._handle)
         pysequoiadb._raise_if_error("Failed to get size of lob", rc)
      except SDBBaseError:
         raise
      return size