/
mgo.go
264 lines (224 loc) · 6.93 KB
/
mgo.go
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package MgoConn
// add by stefan
import (
"github.com/Peakchen/xgameCommon/akLog"
"github.com/Peakchen/xgameCommon/RedisConn"
"github.com/Peakchen/xgameCommon/ado"
. "github.com/Peakchen/xgameCommon/public"
"fmt"
"reflect"
"sync"
"time"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)
type TAokoMgo struct {
sync.Mutex
session *mgo.Session
UserName string
Passwd string
ServiceHost string
chSessions chan *mgo.Session
PoolCnt int
server string
}
func NewMgoConn(server, Username, Passwd, Host string) *TAokoMgo {
aokomogo := &TAokoMgo{}
aokomogo.UserName = Username
aokomogo.Passwd = Passwd
aokomogo.ServiceHost = Host
aokomogo.server = server
//template set 1000 session.
aokomogo.PoolCnt = int(ado.EMgo_Thread_Cnt)
aokomogo.chSessions = make(chan *mgo.Session, aokomogo.PoolCnt)
aokomogo.NewDial()
return aokomogo
}
func (this *TAokoMgo) NewDial() {
session, err := this.NewMgoSession()
if err != nil {
akLog.FmtPrintln(err)
return
}
this.session = session
for i := 1; i <= this.PoolCnt; i++ {
this.chSessions <- session.Copy()
}
return
}
func (this *TAokoMgo) NewMgoSession() (session *mgo.Session, err error) {
MdialInfo := &mgo.DialInfo{
Addrs: []string{this.ServiceHost},
Username: this.UserName,
Password: this.Passwd,
Direct: false,
Timeout: time.Second * 10,
PoolLimit: 4096,
//ReadTimeout: time.Second * 5,
//WriteTimeout: time.Second * 10,
}
session, err = mgo.DialWithInfo(MdialInfo)
if err != nil {
err = akLog.RetError("mgo dial err: %v.\n", err)
akLog.Error("mgo dial err: %v.\n", err)
return
}
err = session.Ping()
if err != nil {
err = akLog.RetError("session ping out, err: %v.", err)
akLog.Error("session ping out, err: %v.", err)
return
}
session.SetMode(mgo.Monotonic, true)
session.SetCursorTimeout(0)
// focus on those selects.
//http://www.mongoing.com/archives/1723
Safe := &mgo.Safe{
J: true, //true:写入落到磁盘才会返回|false:不等待落到磁盘|此项保证落到磁盘
W: 1, //0:不会getLastError|1:主节点成功写入到内存|此项保证正确写入
WMode: "majority", //"majority":多节点写入|此项保证一致性|如果我们是单节点不需要这只此项
}
session.SetSafe(Safe)
//session.SetSocketTimeout(time.Duration(5 * time.Second()))
err = nil
return
}
func (this *TAokoMgo) Exit() {
if this.chSessions != nil {
close(this.chSessions)
}
}
func (this *TAokoMgo) GetMgoSession() (sess *mgo.Session, err error) {
this.Lock()
defer this.Unlock()
if this.session == nil {
err = fmt.Errorf("aoko mongo session not get invalid.")
return
}
sess = this.session
err = nil
return
}
func (this *TAokoMgo) getMgoSessionByChan() (sess *mgo.Session, err error) {
this.Lock()
defer this.Unlock()
select {
case s, _ := <-this.chSessions:
return s, nil
case <-time.After(time.Duration(1) * time.Second):
default:
}
return nil, fmt.Errorf("aoko mongo session time out and not get.")
}
func MakeMgoModel(Identify, MainModel, SubModel string) string {
return MainModel + "." + SubModel + "." + Identify
}
func (this *TAokoMgo) QueryAcc(usrName string, OutParam IDBCache) (err error, exist bool) {
condition := bson.M{OutParam.SubModel() + "." + "username": usrName}
return this.QueryByCondition(condition, OutParam)
}
func (this *TAokoMgo) QueryOne(Identify string, OutParam IDBCache) (err error, exist bool) {
condition := bson.M{"_id": Identify}
return this.QueryByCondition(condition, OutParam)
}
func (this *TAokoMgo) QueryByCondition(condition bson.M, OutParam IDBCache) (err error, exist bool) {
session, err := this.GetMgoSession()
if err != nil {
err = akLog.RetError("get sesson err: %v.", err)
return
}
s := session.Clone()
defer s.Close()
collection := s.DB(this.server).C(OutParam.MainModel())
retQuerys := collection.Find(condition)
count, ret := retQuerys.Count()
if ret != nil || count == 0 {
err = akLog.RetError("[mgo] query data err: %v, %v.", ret, count)
return
}
selectRet := retQuerys.Select(bson.M{OutParam.SubModel(): 1, "_id": 1}).Limit(1)
if selectRet == nil {
err = akLog.RetError("[mgo] selectRet invalid, submodule: %v.", OutParam.SubModel())
return
}
outval := reflect.MakeMap(reflect.MapOf(reflect.TypeOf(""), reflect.TypeOf(OutParam)))
ret = selectRet.One(outval.Interface())
if ret != nil {
err = akLog.RetError("[mgo] select one error: %v.", ret)
return
}
retIdxVal := outval.MapIndex(reflect.ValueOf(OutParam.SubModel()))
if !retIdxVal.IsValid() {
err = akLog.RetError("[mgo] outval MapIndex invalid.")
return
}
reflect.ValueOf(OutParam).Elem().Set(retIdxVal.Elem())
exist = true
return
}
func (this *TAokoMgo) QuerySome(Identify string, OutParam IDBCache) (err error) {
session, err := this.GetMgoSession()
if err != nil {
return err
}
s := session.Clone()
defer s.Close()
collection := s.DB(this.server).C(OutParam.MainModel())
err = collection.Find(bson.M{"_id": Identify}).All(&OutParam)
if err != nil {
err = fmt.Errorf("Identify: %v, MainModel: %v, SubModel: %v, err: %v.\n", Identify, OutParam.MainModel(), OutParam.SubModel(), err)
akLog.Error("[QuerySome] err: %v.\n", err)
}
return
}
func (this *TAokoMgo) InsertOne(Identify string, InParam IDBCache) (err error) {
session, err := this.GetMgoSession()
if err != nil {
return err
}
s := session.Clone()
defer s.Close()
akLog.FmtPrintf("[Insert] main: %v, sub: %v, key: %v.", InParam.MainModel(), InParam.SubModel(), InParam.Identify())
collection := s.DB(this.server).C(InParam.MainModel())
operAction := bson.M{"_id": InParam.Identify(), InParam.SubModel(): InParam}
err = collection.Insert(operAction)
if err != nil {
err = fmt.Errorf("main: %v, sub: %v, key: %v, err: %v.\n", InParam.MainModel(), InParam.SubModel(), InParam.Identify(), err)
akLog.Error("[Insert] err: %v.\n", err)
}
return
}
func (this *TAokoMgo) SaveOne(Identify string, InParam IDBCache) (err error) {
session, err := this.GetMgoSession()
if err != nil {
return err
}
redkey := MakeMgoModel(InParam.Identify(), InParam.MainModel(), InParam.SubModel())
err = Save(session, this.server, redkey, InParam)
return
}
func Save(mgosession *mgo.Session, dbserver, redkey string, data interface{}) (err error) {
s := mgosession.Clone()
defer s.Close()
key, main, sub := RedisConn.ParseRedisKey(redkey)
akLog.FmtPrintf("update origin: %v, main: %v, sub: %v, key: %v.", redkey, main, sub, key)
collection := s.DB(dbserver).C(main)
operAction := bson.M{"$set": bson.M{sub: data}}
_, err = collection.UpsertId(key, operAction)
if err != nil {
err = fmt.Errorf("main: %v, sub: %v, key: %v, err: %v.\n", main, sub, key, err)
akLog.Error("[Save] err: %v.\n", err)
}
return
}
func (this *TAokoMgo) EnsureIndex(InParam IDBCache, idxs []string) (err error) {
session, err := this.GetMgoSession()
if err != nil {
return err
}
s := session.Clone()
defer s.Close()
c := s.DB(this.server).C(InParam.SubModel())
err = c.EnsureIndex(mgo.Index{Key: idxs})
return err
}