hxp
4 天以前 388823edfe6308cba6f76ca6dc4f20022c5cb2be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/python
# -*- coding: GBK -*-
#
##@package E:/GameSVN/TeamTool/PythonScribe/PyModule/Coupon/CouponDB.py
# @todo: 
#
# @author: Alee
# @date 2017-6-2 ÏÂÎç05:21:48
# @version 1.0
#
# @note: 
#
#---------------------------------------------------------------------
#-------------------------------------------------------------------------------
#¼ì²é²¢¸üÐÂË÷Òý
#-------------------------------------------------------------------------------
from lib import ReadConfig
from lib import DBController
from lib import mylog
import os
 
g_dbController = None
 
CouponWXColName = "tagCouponWXSC"   # Î¢ÐÅÉ̳Ç
 
CouponBatchColName = "tagCouponBatch"
CouponCodeColName = "tagCouponCode"
ModuleName = "CouponDB"
 
Def_ConfigPath = os.getcwd()
 
 
def InitDB():
    global g_dbController
    
    if not g_dbController:
        IP = ReadConfig.ReadConfig(Def_ConfigPath + "\\config.ini").GetValue("MongoDB", "IP")
        Port = ReadConfig.ReadConfig(Def_ConfigPath + "\\config.ini").GetInt("MongoDB", "Port")
        User = ReadConfig.ReadConfig(Def_ConfigPath + "\\config.ini").GetValue("MongoDB", "User")
        Pwd = ReadConfig.ReadConfig(Def_ConfigPath + "\\config.ini").GetValue("MongoDB", "Pwd")
        DBName = ReadConfig.ReadConfig(Def_ConfigPath + "\\config.ini").GetValue("MongoDB", "DBName")
        g_dbController = DBController.DBController(IP, Port, DBName, User, Pwd, None)
    
        if g_dbController.connected:
            #Ö»ÔÚÁ´½ÓDB¼ì²éÒ»´Î
            result = CheckAndUpdateIndexOnDb(g_dbController.db)
            if not result:
                mylog.debug('Ë÷ÒýÓÐÎÊÌâ')
                return False
        mylog.debug('»ñȡеÄÊý¾Ý¿âÁ´½Ó')
        
    if not g_dbController.connected:
        mylog.debug('ÎÞ·¨Á´½ÓÊý¾Ý¿â')
        return False
    
    return True
 
def GetDBEventCon():
    global g_dbController
    if not InitDB():
        return None
    return g_dbController
 
 
 
 
 
def ReadIndexInfo():
    indexInfoDict = {
 
    CouponBatchColName:
    {
        'CouponBatch_1':
        {
            'unique':True,
            'key':[('couponid', 1)]    #Ë÷ÒýµÄ×ÖµäºÍµÝÔö(1)»òµÝ¼õ(-1)
         }
     },
 
    }
    return indexInfoDict
 
def CompareIndex(curIndexDict, expectIndexDict):
    #±È½Ïunique
    curUnique = curIndexDict.get('unique', None)
    expectUnique = expectIndexDict.get('unique', None)
    if curUnique is None:
        if expectUnique is None:
            pass
        elif expectUnique:
#            print '#debug 1'
            return False
        else:
            pass
    else:
        if expectUnique is None:
            if (not curUnique):
                return True
#            print '#debug 2'
            return False
        if curUnique and (not expectUnique):
#            print '#debug 3'
            return False
        if (not curUnique) and expectUnique:
#            print '#debug 4'
            return False
    #±È½Ïkeylist
    curKeyList = curIndexDict.get('key', None)
    expectKeyList = expectIndexDict.get('key', None)
    if curKeyList is None:
        if expectKeyList is None:
            pass
        else:
#            print '#debug 5'
            return False
    else:
        if expectKeyList is None:
#            print '#debug 6'
            return False
        else:
            for pair in curKeyList:
                if pair not in expectKeyList:
#                    print '#debug 7'
                    return False
            for pair in expectKeyList:
                if pair not in curKeyList:
#                    print '#debug 8'
                    return False
    return True
 
def CheckIndexes(db, colName, expectIndexInfo):
    col = db[colName]
 
    '''¼ì²é±íÖÐÊÇ·ñÓжàÓàµÄË÷Òý'''
    indexDict = col.index_information()
    for k, v in indexDict.items():
        if k == '_id_':
            continue
        if k not in expectIndexInfo:
            return False
        if not CompareIndex(v, expectIndexInfo[k]):
            return False
    return True
 
def AddIndexes(db, colName, expectIndexes):
    '''Ìí¼ÓË÷Òý'''
    col = db[colName]
    '''¸ø±íÌí¼ÓË÷Òý'''
    curIndexesDict = col.index_information()
    for k, v in expectIndexes.items():
        if k not in curIndexesDict:
            expectIndexInfo = expectIndexes[k]
            isUnique = expectIndexInfo.get('unique', False)
            try:
                col.create_index(expectIndexInfo['key'], name = k, unique = isUnique)
            except Exception, e:
                return False
    return True     
 
def CheckAndUpdateIndexOnDb(db):
    '''¶ÔÓÚË÷ÒýÎļþÖеÄÿ¸ö±í£¬Êý¾Ý¿âÖиñíÓеÄË÷ÒýÐÅÏ¢£¬Ë÷ÒýÎļþÖбØÐëÓв¢ÇÒÆ¥ÅäuniqueºÍ(key, direction)list'''
    indexInfoDict = ReadIndexInfo()
    for k, v in indexInfoDict.items():
        colName = k
        colIndexDict = v
        if not CheckIndexes(db, colName, colIndexDict):
            return False
        if not AddIndexes(db, colName, colIndexDict):
            return False
    return True