Beispiel #1
0
 def GetCachedTables(self):
     defaultDb = self.UQueue.LoadString()
     flags = self.UQueue.LoadUInt()
     index = self.UQueue.LoadLong()
     ms = tagManagementSystem.msUnknown
     sb = CScopeUQueue()
     if len(CYourPeer.FrontCachedTables) == 0 or (
             flags & DB_CONSTS.ENABLE_TABLE_UPDATE_MESSAGES
     ) != DB_CONSTS.ENABLE_TABLE_UPDATE_MESSAGES:
         return sb.SaveInt(ms).SaveInt(0).SaveString('')
     if (flags & DB_CONSTS.ENABLE_TABLE_UPDATE_MESSAGES
         ) == DB_CONSTS.ENABLE_TABLE_UPDATE_MESSAGES:
         self.Push.Subscribe([
             DB_CONSTS.CACHE_UPDATE_CHAT_GROUP_ID,
             DB_CONSTS.STREAMING_SQL_CHAT_GROUP_ID
         ])
     sql = ''
     v = CYourPeer.FrontCachedTables
     for s in v:
         if (len(sql)) > 0:
             sql += ';'
         sql += ('SELECT * FROM ' + s)
     # use Lock and Unlock to avoid SQL stream overlap on a session within a multi-thread environment
     handler = CYourPeer.Master.Lock()
     if not handler:
         return sb.SaveInt(ms).SaveInt(-2).SaveString(
             'No connection to a master database')
     ms = handler.DBManagementSystem
     try:
         f = handler.execute(sql, lambda h, vData: self.SendRows(vData),
                             lambda h: self.SendMeta(h.ColumnInfo, index))
         # put back locked handler and its socket back into pool for reuse as soon as possible
         CYourPeer.Master.Unlock(handler)
         try:
             res = f.result(30)
             return sb.SaveInt(ms).SaveInt(res.ec).SaveString(res.em)
         except TimeoutError as ex:
             return sb.SaveInt(ms).SaveInt(-3).SaveString(str(ex))
     except (CServerError, CSocketError) as ex:
         return sb.SaveInt(ms).SaveInt(ex[0]).SaveString(ex[1])
     except Exception as ex:
         return sb.SaveInt(ms).SaveInt(-1).SaveString(str(ex))
Beispiel #2
0
    def GetCachedTables(self):
        defaultDb = self.UQueue.LoadString()
        flags = self.UQueue.LoadUInt()
        index = self.UQueue.LoadLong()
        ms = tagManagementSystem.msUnknown
        res = 0
        errMsg = ''
        redo = 1
        while redo > 0:
            redo = 0  # disable redo
            if (flags & DB_CONSTS.ENABLE_TABLE_UPDATE_MESSAGES
                ) == DB_CONSTS.ENABLE_TABLE_UPDATE_MESSAGES:
                if not self.Push.Subscribe([
                        DB_CONSTS.CACHE_UPDATE_CHAT_GROUP_ID,
                        DB_CONSTS.STREAMING_SQL_CHAT_GROUP_ID
                ]):
                    errMsg = 'Failed in subscribing for table events'  # warning message
            if len(CYourPeer.FrontCachedTables) == 0 or flags == 0:
                break
            sql = ''
            v = CYourPeer.FrontCachedTables
            for s in v:
                if (len(sql)) > 0:
                    sql += ';'
                sql += ('SELECT * FROM ' + s)
            # use Lock and Unlock to avoid SQL stream overlap on a session within a multi-thread environment
            handler = CYourPeer.Master.Lock()
            if not handler:
                res = -1
                errMsg = 'No connection to a master database'
                break
            ms = handler.DBManagementSystem
            f = UFuture()

            def ares(h, r, err, affected, fail_ok, vtId):
                res = r
                errMsg = err
                f.set(1)

            def rows(h, vData):
                self.SendRows(vData)

            def meta(h):
                self.SendMeta(h.ColumnInfo, index)

            def closed():
                res = -2
                errMsg = 'Request canceled or socket closed'
                f.set(1)

            if not handler.Execute(sql, ares, rows, meta, True, True,
                                   lambda: f.set(-2)):
                res = handler.AttachedClientSocket.ErrorCode
                errMsg = handler.AttachedClientSocket.ErrorMsg
                break

            # put back locked handler and its socket back into pool for reuse as soon as possible
            CYourPeer.Master.Unlock(handler)
            ret = f.get(25.0)
            if ret != 1:
                res = -3
                errMsg = 'Querying cached table data timeout'

        sb = CScopeUQueue()
        sb.SaveInt(ms).SaveInt(res).SaveString(errMsg)
        return sb