| #!/usr/bin/python  | 
| # -*- coding: GBK -*-  | 
| #-------------------------------------------------------------------------------  | 
| # @copyright: Copyright (c) 2004, 2010 Xiamen Wangyou Network Technology Co.,Ltd., All rights reserved.  | 
| #-------------------------------------------------------------------------------  | 
| #  | 
|   | 
| import sys  | 
| import binascii  | 
| import base64  | 
| try:  | 
|     import threading  | 
| except ImportError:  | 
|     import dummy_threading as threading       | 
| import CommFunc      | 
| import MongoDBConfig    | 
| #from Common import (CommFuncEx, CommFunc, mylog)  | 
| #from DBCommon import (CommonDefine, )  | 
| import traceback  | 
|   | 
| PLAYERID_FEED = 10000  | 
| PLAYERID_STEP = 1  | 
|   | 
| SIDErrorCnt = 0  | 
| MAGIC_SID = 2266520  | 
|   | 
| SIDErrorCounterLock = threading.Lock()  | 
|   | 
| ADOExceptionCnt = 0  | 
| AdoExceptionCounterLock = threading.Lock()  | 
|   | 
| #ÐèÒª¶àÏ̱߳£»¤  | 
| def addADOExceptionCount():  | 
|     global ADOExceptionCnt  | 
|     global AdoExceptionCounterLock  | 
|     AdoExceptionCounterLock.acquire()  | 
|     ADOExceptionCnt += 1  | 
|     AdoExceptionCounterLock.release()  | 
|     return ADOExceptionCnt  | 
|   | 
| def getADOExceptionCount():  | 
|     return ADOExceptionCnt  | 
|           | 
| def fix_outgoingText(text):  | 
|     if isinstance(text, unicode):  | 
|         if MongoDBConfig.USER_DB_IsBase64:  | 
|              return base64.b64decode(text)  | 
|         return CommFunc.UnicodeToEncoding(MongoDBConfig.USER_DB_ENCODING, text)[1]  | 
|     #²»ÊÇunicode,²»´¦Àí  | 
|     return text  | 
|   | 
| def fix_incomingText(text):  | 
|     if isinstance(text, str):  | 
|         if MongoDBConfig.USER_DB_IsBase64:  | 
|             return base64.b64encode(text)  | 
|         return CommFunc.EncodingToUnicode(MongoDBConfig.USER_DB_ENCODING, text)[1]  | 
|     #²»ÊÇ×Ö·û´®£¬²»´¦Àí  | 
|     return text  | 
|   | 
| def fix_incoming(dictObj):  | 
|     for k, v in dictObj.iteritems():  | 
|         if isinstance(v, str):  | 
|             result, convertText = CommFuncEx.EncodingToUnicode(CommonDefine.ENCODING, v)  | 
|             if result:  | 
|                 dictObj[k] = convertText  | 
|   | 
| def fix_outgoing(dictObj):  | 
|     for k, v in dictObj.iteritems():  | 
|         if isinstance(v, unicode):  | 
|             result, convertText = CommFuncEx.UnicodeToEncoding(CommonDefine.ENCODING, v)  | 
|             if result:  | 
|                 dictObj[k] = convertText  | 
|   | 
| def addSIDErrorCnt():  | 
|     global SIDErrorCnt  | 
|     global SIDErrorCounterLock  | 
|     SIDErrorCounterLock.acquire()  | 
|     SIDErrorCnt += 1  | 
|     SIDErrorCounterLock.release()  | 
|     return SIDErrorCnt  | 
|   | 
| def getSIDErrorCnt():  | 
|     global SIDErrorCnt  | 
|     return SIDErrorCnt  | 
|    | 
| def makeSID(data):  | 
| #    mylog.debug('makeSID(%s) = %s'%(data, binascii.crc32(data)))  | 
|     return binascii.crc32(data)  | 
|   | 
| def checkSID(data, expectSID):  | 
|     global MAGIC_SID  | 
|     if expectSID == MAGIC_SID:  | 
|         return True  | 
|     calcSID = makeSID(data)  | 
|     if expectSID == calcSID:  | 
|         return True  | 
| #    mylog.debug('expectSID = %s calcSID = %s'%(expectSID, calcSID))  | 
|     return False  | 
|   | 
| SPLITCHAR = '\t'  | 
|   | 
| def sysDBLoadFromFile(className, fileName):  | 
|     data = ''  | 
|     try:  | 
|         fileObj = open(fileName, 'rb', 0)  | 
|         mylog.info('Loading %s...'%fileName)  | 
|         try:  | 
|             rowObj = className()  | 
|             rowCnt = 0  | 
|             for line in fileObj:  | 
|                 rowCnt += 1  | 
|                 if rowCnt == 1:  | 
|                     continue    #skip header  | 
| #                logging.getLogger('sysDBLoadFromFile').debug('line %d = %s'%(rowCnt,line))  | 
|                 rowObj.readLine(line)  | 
|                 data += rowObj.getBuffer()  | 
|         except IOError,(readErrNo, readStdErr):  | 
|             mylog.error('Read %s failed! errno = %s stdErr = %s'%(fileName, readErrNo, readStdErr))  | 
|             data = ''  | 
|             data = CommFunc.WriteDWORD(data, 0)  | 
|             return data  | 
|         except:  | 
|             mylog.error('Exception = %s'%sys.exc_info()[0])  | 
|             mylog.error(traceback.format_exc())   | 
|             data = ''  | 
|             data = CommFunc.WriteDWORD(data, 0)  | 
|             return data  | 
|         else:  | 
|             fileObj.close()  | 
| #            logging.getLogger('sysDBLoadFromFile').debug('data = %s'%binascii.b2a_hex(data))  | 
|             mylog.info('Load %s cnt = %s'%(fileName, rowCnt - 1))  | 
|               | 
|             buf = ''  | 
|             buf = CommFunc.WriteDWORD(buf, rowCnt - 1)  | 
|             buf = CommFunc.WriteString(buf, len(data), data)  | 
|             return buf  | 
|     except IOError, (errno, stderr):  | 
|         mylog.error('Can not open %s!errno = %s stderr = %s'%(fileName, errno, stderr))  | 
|       | 
|     data = ''  | 
|     data = CommFunc.WriteDWORD(data, 0)  | 
|     return data  | 
|           | 
| def SysDBUpdate(className, collection, fileName):  | 
|     collection.drop()  | 
|     try:  | 
|         fileObj = open(fileName,'rb', 0)  | 
|         try:  | 
|             rowObj = className()  | 
|             row = 0  | 
|             for line in fileObj:  | 
|                 row += 1  | 
|                 if row == 1:  | 
|                     continue    #skip header  | 
| #                print 'line_%d=%s'%(Col,line)  | 
|                 rowObj.readLine(line)  | 
|                 rowObj.adoInsert(collection)  | 
|         except IOError,(readErrNo, readStdErr):  | 
|             mylog.error('Read %s failed!errno = %s stderr = %s'%(fileName, readErrNo, readStdErr))  | 
|         finally:  | 
|             fileObj.close()  | 
|     except IOError,(errno, stderr):  | 
|         mylog.error('Can not open %s! errno = %s stderr = %s'%(fileName, errno, stderr))  | 
|           | 
| def seq(db, collectionName, fieldName, feed, increment):  | 
|     collection = db['%s_seq'%collectionName]  | 
|     resultObj = collection.find_and_modify(query={'_id':fieldName}, update={'$inc':{'seq':increment}}, new=True)  | 
|     if resultObj:  | 
|         result = resultObj['seq']  | 
|     else:  | 
|         resultObj = collection.find_and_modify(query={'_id':fieldName}, update={'$set':{'seq':feed}}, new=True,  | 
|                                                upsert=True)  | 
|         if resultObj:  | 
|             result = resultObj['seq']  | 
|         else:  | 
|             mylog.error('seq failed!')  | 
|             return 0  | 
|     return result  | 
|           | 
|   | 
|   |