예제 #1
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
예제 #2
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
예제 #3
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
예제 #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
예제 #5
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
예제 #6
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
예제 #7
0
    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
예제 #8
0
   def list_replica_groups(self):
      """List all replica groups of current database.

      Return values:
         a cursor object of replication groups.
      Exceptions:
         pysequoiadb.error.SDBBaseError
      """
      try:
         result = cursor()
         rc = sdb.sdb_list_replica_groups(self._client, result._cursor)
         pysequoiadb._raise_if_error("Failed to list replica groups", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      return result
예제 #9
0
   def list_collections(self):
      """List all collections in current database.

      Return values:
         a cursor object of collection.
      Exceptions:
         pysequoiadb.error.SDBBaseError
      """
      try:
         result = cursor()
         rc = sdb.sdb_list_collections(self._client, result._cursor)
         pysequoiadb._raise_if_error("Failed to list collections", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      return result
예제 #10
0
   def list_lobs(self):
      """list all lobs.

      Parameters:
         Name     Type                 Info:

      Return values:
         a cursor object of query
      Exceptions:
         pysequoiadb.error.SDBBaseError
      """
      try:
         result = cursor()
         rc = sdb.cl_list_lobs(self._cl, result._cursor)
         pysequoiadb._raise_if_error("Failed to list lobs", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      return result
예제 #11
0
    def list_lobs(self):
        """list all lobs.

      Parameters:
         Name     Type                 Info:

      Return values:
         a cursor object of query
      Exceptions:
         pysequoiadb.error.SDBBaseError
      """
        try:
            result = cursor()
            rc = sdb.cl_list_lobs(self._cl, result._cursor)
            pysequoiadb._raise_if_error("Failed to list lobs", rc)
        except SDBBaseError:
            del result
            result = None
            raise

        return result
예제 #12
0
   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
예제 #13
0
    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
예제 #14
0
    def explain(self, **kwargs):
        """Get the matching documents in current collection.

      Parameters:
         Name              Type     Info:
         **kwargs                   Useful options are below
         - condition       dict     The matching rule, update all the
                                          documents if not provided.
         - selected        dict     The selective rule, return the whole
                                          document if not provided.
         - order_by        dict     The ordered rule, result set is unordered
                                          if not provided.
         - hint            dict     The hint, automatically match the optimal
                                          hint if not provided.
         - num_to_skip     long     Skip the first numToSkip documents,
                                          default is 0L.
         - num_to_return   long     Only return numToReturn documents,
                                          default is -1L for returning
                                          all results.
         - flag            int      
         - options         json     
      Return values:
         a cursor object of query
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """

        bson_condition = None
        bson_selector = None
        bson_order_by = None
        bson_hint = None
        bson_options = None

        num_to_skip = 0L
        num_to_return = -1L
        flag = 0

        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 "selector" in kwargs:
            if not isinstance(kwargs.get("selector"), dict):
                raise SDBTypeError("selector 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 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 must be an instance of dict")
            bson_hint = bson.BSON.encode(kwargs.get("hint"))
        if "num_to_skip" in kwargs:
            if not isinstance(kwargs.get("num_to_skip"), long):
                raise SDBTypeError("num_to_skip must be an instance of long")
            else:
                num_to_skip = kwargs.get("num_to_skip")
        if "num_to_return" in kwargs:
            if not isinstance(kwargs.get("num_to_return"), long):
                raise SDBTypeError("num_to_return must be an instance of long")
            else:
                num_to_return = kwargs.get("num_to_return")
        if "flag" in kwargs:
            if not isinstance(kwargs.get("flag"), int):
                raise SDBTypeError("flag must be an instance of int")
            else:
                num_to_return = kwargs.get("flag")
        if "options" in kwargs:
            if not isinstance(kwargs.get("options"), dict):
                raise SDBTypeError("options must be an instance of dict")
            bson_options = bson.BSON.encode(kwargs.get("options"))

        try:
            result = cursor()
            rc = sdb.cl_explain(self._cl, result._cursor, bson_condition,
                                bson_selector, bson_order_by, bson_hint,
                                num_to_skip, num_to_return, flag, bson_options)
            pysequoiadb._raise_if_error("Failed to explain", rc)
        except SDBBaseError:
            del result
            result = None
            raise

        return result
예제 #15
0
   def query_one(self, **kwargs):
      """Get one matching documents in current collection.

      Parameters:
         Name              Type     Info:
         **kwargs                   Useful options are below
         - condition       dict     The matching rule, update all the
                                          documents if not provided.
         - selected        dict     The selective rule, return the whole
                                          document if not provided.
         - order_by        dict     The ordered rule, result set is unordered
                                          if not provided.
         - hint            dict     The hint, automatically match the optimal
                                          hint if not provided.
         - num_to_skip     long     Skip the first numToSkip documents,
                                          default is 0L.
      Return values:
         a record of json/dict
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
      bson_condition = None
      bson_selector = None
      bson_order_by = None
      bson_hint = None
      
      num_to_skip = 0L

      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 "selector" in kwargs:
         if not isinstance(kwargs.get("selector"), dict):
            raise SDBTypeError("selector 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 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 must be an instance of dict")
         bson_hint = bson.BSON.encode(kwargs.get("hint"))
      if "num_to_skip" in kwargs:
         if not isinstance(kwargs.get("num_to_skip"), long):
            raise SDBTypeError("num_to_skip must be an instance of long")
         else:
            num_to_skip = kwargs.get("num_to_skip")

      try:
         result = cursor()
         rc = sdb.cl_query(self._cl, result._cursor,
                          bson_condition, bson_selector,
                          bson_order_by, bson_hint,
                          num_to_skip, 1L)
         pysequoiadb._raise_if_error("Failed to query one", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      try:
         record = result.next()
      except SDBEndOfCursor :
         record = None
      except SDBBaseError:
         raise

      del result
      result = None

      return record
예제 #16
0
   def get_query_meta(self, **kwargs):
      """Get the index blocks' or data blocks' infomations for concurrent query.

      Parameters:
         Name              Type     Info:
         **kwargs                   Useful options are below
         - condition       dict     The matching rule, return the whole range
                                          of index blocks if not provided.
                                          eg:{"age":{"$gt":25},"age":{"$lt":75}}.
         - order_by        dict     The ordered rule, result set is unordered
                                          if not provided.bson.SON may need if it is
                                          order-sensitive.
         - hint            dict     One of the indexs in current collection,
                                          using default index to query if not
                                          provided.
                                          eg:{"":"ageIndex"}.
         - num_to_skip     long     Skip the first num_to_skip documents,
                                          default is 0L.
         - num_to_return   long     Only return num_to_return documents,
                                          default is -1L for returning all results.
      Return values:
         a cursor object of query
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
      num_to_skip = 0L
      num_to_return = -1L

      if "num_to_skip" in kwargs:
         if not isinstance(kwargs.get("num_to_skip"), long):
            raise SDBTypeError("number to skip must be an instance of long")
         else:
            num_to_skip = kwargs.get("num_to_skip")
      if "num_to_return" in kwargs:
         if not isinstance(kwargs.get("num_to_return"), long):
            raise SDBTypeError("number to return must be an instance of long")
         else:
            num_to_return = kwargs.get("num_to_return")

      bson_condition = None
      bson_order_by = 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 "order_by" in kwargs:
         if not isinstance(kwargs.get("order_by"), dict):
            raise SDBTypeError("order_by 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 must be an instance of dict")
         bson_hint = bson.BSON.encode(kwargs.get("hint"))

      try:
         result = cursor()
         rc = sdb.cl_get_query_meta(self._cl, result._cursor, condition,
                                order_by, hint, num_to_skip, num_to_return)
         pysequoiadb._raise_if_error("Failed to query meta", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      return result
예제 #17
0
   def get_list(self, list_type, **kwargs):
      """Get the informations of specified type.

      Parameters:
         Name        Type     Info:
         list_type   int      Type of list option, see Info as below.
         **kwargs             Useful options are below
         - condition dict     The matching rule, match all the documents
                                    if None.
         - selector  dict     The selective rule, return the whole
                                    documents if None.
         - order_by  dict     The ordered rule, never sort if None.
      Return values:
         a cursor object of query
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      Info:
         list type:
                0          : Get all contexts list
                1          : Get contexts list for the current session
                2          : Get all sessions list
                3          : Get the current session
                4          : Get all collections list
                5          : Get all collecion spaces' list
                6          : Get storage units list
                7          : Get replicaGroup list ( only applicable in sharding env )
                8          : Get store procedure list
                9          : Get domains list
                10         : Get tasks list
                11         : Get collection space list in domain
                12         : Get collection list in domain
      """
      if not isinstance(list_type, int):
         raise SDBTypeError("list type must be an instance of int")
      if list_type < 0 or list_type > 12:
         raise SDBTypeError("list type value %d is not defined" %
                                     list_type)

      bson_condition = None
      bson_selector = None
      bson_order_by = None

      if "condition" in kwargs:
         bson_condition = bson.BSON.encode(kwargs.get("condition"))
      if "selector" in kwargs:
         bson_selector = bson.BSON.encode(kwargs.get("selector"))
      if "order_by" in kwargs:
         bson_order_by = bson.BSON.encode(kwargs.get("order_by"))

      try:
         result = cursor()
         rc = sdb.sdb_get_list(self._client, result._cursor, list_type,
                                 bson_condition, bson_selector, bson_order_by)
         pysequoiadb._raise_if_error("Failed to get list", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      return result
예제 #18
0
   def get_snapshot(self, snap_type, **kwargs):
      """Get the snapshots of specified type.

      Parameters:
         Name           Type  Info:
         snap_typr      str   The type of snapshot, see Info as below
         **kwargs             Useful options are below
         - condition    dict  The matching rule, match all the documents
                                    if not provided.
         - selector     dict  The selective rule, return the whole
                                    document if not provided.
         - order_by     dict  The ordered rule, result set is unordered
                                    if not provided.
      Return values:
         a cursor object of query
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      Info:
        snapshot type:
                  0     : Get all contexts' snapshot
                  1     : Get the current context's snapshot
                  2     : Get all sessions' snapshot
                  3     : Get the current session's snapshot
                  4     : Get the collections' snapshot
                  5     : Get the collection spaces' snapshot
                  6     : Get database's snapshot
                  7     : Get system's snapshot
                  8     : Get catalog's snapshot
      """
      if not isinstance(snap_type, int):
         raise SDBTypeError("snap type must be an instance of int")
      if snap_type < 0 or snap_type > 8:
         raise SDBTypeError("snap_type value is invalid")

      bson_condition = None
      bson_selector = None
      bson_order_by = 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 "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"))

      try:
         result = cursor()
         rc = sdb.sdb_get_snapshot(self._client, result._cursor, snap_type,
                                     bson_condition, bson_selector, bson_order_by)
         pysequoiadb._raise_if_error("Failed to get snapshot", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      return result
예제 #19
0
   def list_backup(self, options, **kwargs):
      """List the backups.

      Parameters:
         Name        Type     Info:
         options     dict     Contains configuration infomations for remove
                                     backups, list all the backups in the
                                     default backup path if None. 
                                     The "options" contains 3 options as below. 
                                     All the elements in options are optional. 
                                     eg:
                                     { "GroupName":["rgame1", "rgName2"], 
                                       "Path":"/opt/sequoiadb/backup",
                                       "Name":"backupName" }
                                     See Info as below.
         **kwargs             Useful option arw 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.
      Return values:
         a cursor object of backup list
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      Info:
         GroupName   :  Assign the backups of specifed replica groups to be list.
         Path        :  Assign the backups in specifed path to be list,
                              if not assign, use the backup path asigned in the
                              configuration file.
         Name        :  Assign the backups with specifed name to be list.
      """

      bson_condition = None
      bson_selector = None
      bson_order_by = None

      if not isinstance(options, dict):
         raise SDBTypeError("options in kwargs must be an instance of dict")

      bson_options = bson.BSON.encode(options)
      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 "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"))

      try:
         result = cursor()
         rc = sdb.sdb_list_backup(self._client, result._cursor, bson_options,
                                   bson_condition, bson_selector, bson_order_by)
         pysequoiadb._raise_if_error("Failed to list backup", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      return result
예제 #20
0
    def get_query_meta(self, **kwargs):
        """Get the index blocks' or data blocks' infomations for concurrent query.

      Parameters:
         Name              Type     Info:
         **kwargs                   Useful options are below
         - condition       dict     The matching rule, return the whole range
                                          of index blocks if not provided.
                                          eg:{"age":{"$gt":25},"age":{"$lt":75}}.
         - order_by        dict     The ordered rule, result set is unordered
                                          if not provided.bson.SON may need if it is
                                          order-sensitive.
         - hint            dict     One of the indexs in current collection,
                                          using default index to query if not
                                          provided.
                                          eg:{"":"ageIndex"}.
         - num_to_skip     long     Skip the first num_to_skip documents,
                                          default is 0L.
         - num_to_return   long     Only return num_to_return documents,
                                          default is -1L for returning all results.
      Return values:
         a cursor object of query
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
        num_to_skip = 0L
        num_to_return = -1L

        if "num_to_skip" in kwargs:
            if not isinstance(kwargs.get("num_to_skip"), long):
                raise SDBTypeError(
                    "number to skip must be an instance of long")
            else:
                num_to_skip = kwargs.get("num_to_skip")
        if "num_to_return" in kwargs:
            if not isinstance(kwargs.get("num_to_return"), long):
                raise SDBTypeError(
                    "number to return must be an instance of long")
            else:
                num_to_return = kwargs.get("num_to_return")

        bson_condition = None
        bson_order_by = 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 "order_by" in kwargs:
            if not isinstance(kwargs.get("order_by"), dict):
                raise SDBTypeError("order_by 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 must be an instance of dict")
            bson_hint = bson.BSON.encode(kwargs.get("hint"))

        try:
            result = cursor()
            rc = sdb.cl_get_query_meta(self._cl, result._cursor, condition,
                                       order_by, hint, num_to_skip,
                                       num_to_return)
            pysequoiadb._raise_if_error("Failed to query meta", rc)
        except SDBBaseError:
            del result
            result = None
            raise

        return result
예제 #21
0
   def query_and_remove(self, condition = None, selector = None, order_by = None, hint = None, num_to_skip = 0, num_to_return = -1):
      """Get the matching documents in current collection and remove.

      Parameters:
         Name            Type     Info:
         condition       dict     The matching rule, update all the
                                          documents if not provided.
         selector        dict     The selective rule, return the whole
                                          document if not provided.
         order_by        dict     The ordered rule, result set is unordered
                                          if not provided.
         hint            dict     The hint, automatically match the optimal
                                          hint if not provided.
         num_to_skip     long     Skip the first numToSkip documents,
                                          default is 0L.
         num_to_return   long     Only return numToReturn documents,
                                          default is -1L for returning
                                          all results.
      Return values:
         a cursor object of query
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """

      bson_condition = None
      bson_selector = None
      bson_order_by = None
      bson_hint = None

      if condition != None:
         if not isinstance(condition, dict):
            raise SDBTypeError("condition must be an instance of dict")
         bson_condition = bson.BSON.encode(condition)
      if selector != None:
         if not isinstance(selector, dict):
            raise SDBTypeError("selector must be an instance of dict")
         bson_selector = bson.BSON.encode(selector)
      if order_by != None:
         if not isinstance(order_by, dict):
            raise SDBTypeError("order_by must be an instance of dict")
         bson_order_by = bson.BSON.encode(order_by)
      if hint != None:
         if not isinstance(hint, dict):
            raise SDBTypeError("hint must be an instance of dict")
         bson_hint = bson.BSON.encode(hint)
      if num_to_skip != None:
         if not isinstance(num_to_skip, int):
            raise SDBTypeError("num_to_skip must be an instance of int")
      if num_to_return != None:
         if not isinstance(num_to_return, int):
            raise SDBTypeError("num_to_return must be an instance of int")

      try:
         result = cursor()
         rc = sdb.cl_query_and_remove(self._cl, result._cursor,
                                      bson_condition, bson_selector,
                                      bson_order_by, bson_hint,
                                      num_to_skip, num_to_return)
         pysequoiadb._raise_if_error("Failed to query", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      return result
예제 #22
0
    def query_one(self, **kwargs):
        """Get one matching documents in current collection.

      Parameters:
         Name              Type     Info:
         **kwargs                   Useful options are below
         - condition       dict     The matching rule, update all the
                                          documents if not provided.
         - selected        dict     The selective rule, return the whole
                                          document if not provided.
         - order_by        dict     The ordered rule, result set is unordered
                                          if not provided.
         - hint            dict     The hint, automatically match the optimal
                                          hint if not provided.
         - num_to_skip     long     Skip the first numToSkip documents,
                                          default is 0L.
      Return values:
         a record of json/dict
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """
        bson_condition = None
        bson_selector = None
        bson_order_by = None
        bson_hint = None

        num_to_skip = 0L

        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 "selector" in kwargs:
            if not isinstance(kwargs.get("selector"), dict):
                raise SDBTypeError("selector 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 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 must be an instance of dict")
            bson_hint = bson.BSON.encode(kwargs.get("hint"))
        if "num_to_skip" in kwargs:
            if not isinstance(kwargs.get("num_to_skip"), long):
                raise SDBTypeError("num_to_skip must be an instance of long")
            else:
                num_to_skip = kwargs.get("num_to_skip")

        try:
            result = cursor()
            rc = sdb.cl_query(self._cl, result._cursor, bson_condition,
                              bson_selector, bson_order_by, bson_hint,
                              num_to_skip, 1L)
            pysequoiadb._raise_if_error("Failed to query one", rc)
        except SDBBaseError:
            del result
            result = None
            raise

        try:
            record = result.next()
        except SDBEndOfCursor:
            record = None
        except SDBBaseError:
            raise

        del result
        result = None

        return record
예제 #23
0
   def explain(self, **kwargs):
      """Get the matching documents in current collection.

      Parameters:
         Name              Type     Info:
         **kwargs                   Useful options are below
         - condition       dict     The matching rule, update all the
                                          documents if not provided.
         - selected        dict     The selective rule, return the whole
                                          document if not provided.
         - order_by        dict     The ordered rule, result set is unordered
                                          if not provided.
         - hint            dict     The hint, automatically match the optimal
                                          hint if not provided.
         - num_to_skip     long     Skip the first numToSkip documents,
                                          default is 0L.
         - num_to_return   long     Only return numToReturn documents,
                                          default is -1L for returning
                                          all results.
         - flag            int      
         - options         json     
      Return values:
         a cursor object of query
      Exceptions:
         pysequoiadb.error.SDBTypeError
         pysequoiadb.error.SDBBaseError
      """

      bson_condition = None
      bson_selector = None
      bson_order_by = None
      bson_hint = None
      bson_options = None
      
      num_to_skip = 0L
      num_to_return = -1L
      flag = 0

      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 "selector" in kwargs:
         if not isinstance(kwargs.get("selector"), dict):
            raise SDBTypeError("selector 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 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 must be an instance of dict")
         bson_hint = bson.BSON.encode(kwargs.get("hint"))
      if "num_to_skip" in kwargs:
         if not isinstance(kwargs.get("num_to_skip"), long):
            raise SDBTypeError("num_to_skip must be an instance of long")
         else:
            num_to_skip = kwargs.get("num_to_skip")
      if "num_to_return" in kwargs:
         if not isinstance(kwargs.get("num_to_return"), long):
            raise SDBTypeError("num_to_return must be an instance of long")
         else:
            num_to_return = kwargs.get("num_to_return")
      if "flag" in kwargs:
         if not isinstance(kwargs.get("flag"), int):
            raise SDBTypeError("flag must be an instance of int")
         else:
            num_to_return = kwargs.get("flag")
      if "options" in kwargs:
         if not isinstance(kwargs.get("options"), dict):
            raise SDBTypeError("options must be an instance of dict")
         bson_options = bson.BSON.encode(kwargs.get("options"))

      try:
         result = cursor()
         rc = sdb.cl_explain(self._cl, result._cursor,
                             bson_condition, bson_selector,
                             bson_order_by, bson_hint,
                             num_to_skip, num_to_return,
                             flag, bson_options)
         pysequoiadb._raise_if_error("Failed to explain", rc)
      except SDBBaseError:
         del result
         result = None
         raise

      return result