-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmgojuice.go
368 lines (302 loc) · 9.7 KB
/
mgojuice.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package mgojuice
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/golang/glog"
"github.com/kelseyhightower/envconfig"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
const (
// MasterSession provides direct access to master database.
MasterSession = "master"
// MonotonicSession provides reads to slaves.
MonotonicSession = "monotonic"
)
var (
// Reference to the singleton.
singleton mongoManager
)
type (
// mongoConfiguration contains settings for initialization.
mongoConfiguration struct {
Hosts string
Database string
UserName string
Password string
}
// mongoManager contains dial and session information.
mongoSession struct {
mongoDBDialInfo *mgo.DialInfo
mongoSession *mgo.Session
}
// mongoManager manages a map of session.
mongoManager struct {
sessions map[string]mongoSession
}
// DBCall defines a type of function that can be used
// to excecute code against MongoDB.
DBCall func(*mgo.Collection) error
DBUpdateCall func(*mgo.Collection) (*mgo.ChangeInfo, error)
)
// Startup brings the manager to a running state.
func Startup() error {
// If the system has already been started ignore the call.
if singleton.sessions != nil {
return nil
}
if glog.V(2) {
glog.Info("Application Startup!")
}
// Pull in the configuration.
var config mongoConfiguration
if err := envconfig.Process("mongodb", &config); err != nil {
glog.Fatalf("Error[%s] while fetching ENV variables on Startup", err)
return err
}
// Create the Mongo Manager.
singleton = mongoManager{
sessions: make(map[string]mongoSession),
}
// Log the mongodb connection straps.
if glog.V(2) {
glog.Infof("Startup - MongoDB : Hosts[%s]", config.Hosts)
glog.Infof("Startup - MongoDB : Database[%s]", config.Database)
glog.Infof("Startup - MongoDB : Username[%s]", config.UserName)
}
hosts := strings.Split(config.Hosts, ",")
// Create the strong session.
if err := CreateSession("strong", MasterSession, hosts, config.Database, config.UserName, config.Password); err != nil {
glog.Fatalf("Error[%s] while creating a 'strong' Mongodb session", err)
return err
}
// Create the monotonic session.
if err := CreateSession("monotonic", MonotonicSession, hosts, config.Database, config.UserName, config.Password); err != nil {
glog.Fatalf("Error[%s] while creating a 'monotonic' Mongodb session", err)
return err
}
if glog.V(2) {
glog.Info("Startup completed!")
}
return nil
}
// Shutdown systematically brings the manager down gracefully.
func Shutdown() error {
if glog.V(2) {
glog.Info("Shutdown starting ..")
}
// Close the databases
for _, session := range singleton.sessions {
CloseSession(session.mongoSession)
}
if glog.V(2) {
glog.Info("Shutdown completed")
}
return nil
}
// CreateSession creates a connection pool for use.
func CreateSession(mode string, sessionName string, hosts []string, databaseName string, username string, password string) error {
if glog.V(2) {
glog.Infof("CreateSession - Mode[%s] SessionName[%s] Hosts[%s] DatabaseName[%s] Username[%s]", mode, sessionName, hosts, databaseName, username)
}
// Create the database object
mongoSession := mongoSession{
mongoDBDialInfo: &mgo.DialInfo{
Addrs: hosts,
Timeout: 60 * time.Second,
Database: databaseName,
Username: username,
Password: password,
},
}
// Establish the master session.
var err error
mongoSession.mongoSession, err = mgo.DialWithInfo(mongoSession.mongoDBDialInfo)
if err != nil {
if glog.V(2) {
glog.Infof("CreateSession error [%s]", err)
}
return err
}
switch mode {
case "strong":
// Reads and writes will always be made to the master server using a
// unique connection so that reads and writes are fully consistent,
// ordered, and observing the most up-to-date data.
// http://godoc.org/github.com/finapps/mgo#Session.SetMode
mongoSession.mongoSession.SetMode(mgo.Strong, true)
break
case "monotonic":
// Reads may not be entirely up-to-date, but they will always see the
// history of changes moving forward, the data read will be consistent
// across sequential queries in the same session, and modifications made
// within the session will be observed in following queries (read-your-writes).
// http://godoc.org/github.com/finapps/mgo#Session.SetMode
mongoSession.mongoSession.SetMode(mgo.Monotonic, true)
}
// Have the session check for errors.
// http://godoc.org/github.com/finapps/mgo#Session.SetSafe
mongoSession.mongoSession.SetSafe(&mgo.Safe{})
// Add the database to the map.
singleton.sessions[sessionName] = mongoSession
glog.Info(mongoSession.mongoSession.BuildInfo())
if glog.V(2) {
glog.Info("CreateSession completed")
}
return nil
}
// CopyMasterSession makes a copy of the master session for client use.
func CopyMasterSession() (*mgo.Session, error) {
return CopySession(MasterSession)
}
// CopyMonotonicSession makes a copy of the monotonic session for client use.
func CopyMonotonicSession() (*mgo.Session, error) {
return CopySession(MonotonicSession)
}
// CopySession makes a copy of the specified session for client use.
func CopySession(useSession string) (*mgo.Session, error) {
if glog.V(2) {
glog.Infof("CopySession UseSession[%s]", useSession)
}
// Find the session object.
session := singleton.sessions[useSession]
if session.mongoSession == nil {
err := fmt.Errorf("Unable To Locate Session[%s]", useSession)
glog.Fatalf("Error[%s] while CopySession", err)
return nil, err
}
// Copy the master session.
mongoSession := session.mongoSession.Copy()
if glog.V(2) {
glog.Info("CopySession completed")
}
return mongoSession, nil
}
// CloneMasterSession makes a clone of the master session for client use.
func CloneMasterSession() (*mgo.Session, error) {
return CloneSession(MasterSession)
}
// CloneMonotonicSession makes a clone of the monotinic session for client use.
func CloneMonotonicSession() (*mgo.Session, error) {
return CloneSession(MonotonicSession)
}
// CloneSession makes a clone of the specified session for client use.
func CloneSession(useSession string) (*mgo.Session, error) {
if glog.V(2) {
glog.Info("CloneSession started: UseSession[%s]", useSession)
}
// Find the session object.
session := singleton.sessions[useSession]
if session.mongoSession == nil {
err := fmt.Errorf("Unable To Locate Session[%s]", useSession)
glog.Fatalf("Error[%s] while CloneSession", err)
return nil, err
}
// Clone the master session.
mongoSession := session.mongoSession.Clone()
if glog.V(2) {
glog.Info("CloneSession completed")
}
return mongoSession, nil
}
// CloseSession puts the connection back into the pool.
func CloseSession(mongoSession *mgo.Session) {
if glog.V(2) {
glog.Info("CloseSession started")
}
mongoSession.Close()
if glog.V(2) {
glog.Info("CloseSession completed")
}
}
// GetDatabase returns a reference to the specified database.
func GetDatabase(mongoSession *mgo.Session, useDatabase string) *mgo.Database {
return mongoSession.DB(useDatabase)
}
// GetCollection returns a reference to a collection for the specified database and collection name.
func GetCollection(mongoSession *mgo.Session, useDatabase string, useCollection string) *mgo.Collection {
return mongoSession.DB(useDatabase).C(useCollection)
}
// CollectionExists returns true if the collection name exists in the specified database.
func CollectionExists(mongoSession *mgo.Session, useDatabase string, useCollection string) bool {
database := mongoSession.DB(useDatabase)
collections, err := database.CollectionNames()
if err != nil {
return false
}
for _, collection := range collections {
if collection == useCollection {
return true
}
}
return false
}
// ToString converts the quer map to a string.
func ToString(queryMap interface{}) string {
json, err := json.Marshal(queryMap)
if err != nil {
return ""
}
return string(json)
}
// ToStringD converts bson.D to a string.
func ToStringD(queryMap bson.D) string {
json, err := json.Marshal(queryMap)
if err != nil {
return ""
}
return string(json)
}
// Execute the MongoDB literal function.
func Execute(sessionName string, collectionName string, dbCall DBCall) error {
mongoSession, err := PrepareMongoSession(sessionName)
if err != nil {
return err
}
defer CloseSession(mongoSession)
if glog.V(2) {
glog.Infof("Execute: Database[%s] Collection[%s]", mongoSession.DB("").Name, collectionName)
}
/* Capture the specified collection.
DB returns a value representing the named database. If name is empty, the database name provided
in the dialed URL is used instead. If that is also empty, "test" is used as a fallback in a way equivalent to the mongo shell.*/
collection := GetCollection(mongoSession, mongoSession.DB("").Name, collectionName)
// Execute the MongoDB call.
err = dbCall(collection)
if err != nil {
glog.Error(err)
return err
}
if glog.V(2) {
glog.Info("Execute completed")
}
return nil
}
// Execute Mongos find and modify method
func FindAndModify(sessionName string, collectionName string, dbCall DBUpdateCall) error {
mongoSession, err := PrepareMongoSession(sessionName)
if err != nil {
return err
}
defer CloseSession(mongoSession)
if glog.V(2) {
glog.Infof("Execute: Database[%s] Collection[%s]", mongoSession.DB("").Name, collectionName)
}
/* Capture the specified collection.
DB returns a value representing the named database. If name is empty, the database name provided
in the dialed URL is used instead. If that is also empty, "test" is used as a fallback in a way equivalent to the mongo shell.*/
collection := GetCollection(mongoSession, mongoSession.DB("").Name, collectionName)
// Execute the MongoDB call.
info, err := dbCall(collection)
if err != nil {
glog.Error(err)
return err
}
if glog.V(2) {
glog.Info(info)
glog.Info("Execute completed")
}
return nil
}