-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
mongo.go
195 lines (161 loc) · 3.97 KB
/
mongo.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
package mongo
import (
"crypto/tls"
"net"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"github.com/tidepool-org/platform/errors"
"github.com/tidepool-org/platform/log"
)
// TODO: Consider SetStats, GetStats
// TODO: Consider SetDebug and SetLogger
// TODO: Consider findAndModify via Query.Apply
type Status struct {
State string
BuildInfo *mgo.BuildInfo
LiveServers []string
Mode mgo.Mode
Safe *mgo.Safe
Ping string
}
func NewStore(cfg *Config, lgr log.Logger) (*Store, error) {
if cfg == nil {
return nil, errors.New("config is missing")
}
if lgr == nil {
return nil, errors.New("logger is missing")
}
if err := cfg.Validate(); err != nil {
return nil, errors.Wrap(err, "config is invalid")
}
loggerFields := map[string]interface{}{
"database": cfg.Database,
"collectionPrefix": cfg.CollectionPrefix,
}
lgr = lgr.WithFields(loggerFields)
dialInfo := mgo.DialInfo{}
dialInfo.Addrs = cfg.Addresses
if cfg.TLS {
dialInfo.DialServer = func(serverAddr *mgo.ServerAddr) (net.Conn, error) {
return tls.Dial("tcp", serverAddr.String(), &tls.Config{InsecureSkipVerify: true}) // TODO: Secure this connection
}
}
dialInfo.Database = cfg.Database
if cfg.Username != nil {
dialInfo.Username = *cfg.Username
}
if cfg.Password != nil {
dialInfo.Password = *cfg.Password
}
dialInfo.Timeout = cfg.Timeout
lgr.WithField("config", cfg).Debug("Dialing Mongo database")
session, err := mgo.DialWithInfo(&dialInfo)
if err != nil {
return nil, errors.Wrap(err, "unable to dial database")
}
lgr.Debug("Verifying Mongo build version is supported")
buildInfo, err := session.BuildInfo()
if err != nil {
session.Close()
return nil, errors.Wrap(err, "unable to determine build info")
}
if !buildInfo.VersionAtLeast(3) {
session.Close()
return nil, errors.Newf("unsupported mongo build version %q", buildInfo.Version)
}
lgr.Debug("Setting Mongo consistency mode to Strong")
session.SetMode(mgo.Strong, true)
// TODO: Do we need to set Safe so we get write > 1?
return &Store{
Config: cfg,
Session: session,
}, nil
}
type Store struct {
Config *Config
Session *mgo.Session
}
func (s *Store) IsClosed() bool {
return s.Session == nil
}
func (s *Store) Close() {
if s.Session != nil {
s.Session.Close()
s.Session = nil
}
}
func (s *Store) Status() interface{} {
status := &Status{
State: "CLOSED",
Ping: "FAILED",
}
if !s.IsClosed() {
status.State = "OPEN"
if buildInfo, err := s.Session.BuildInfo(); err == nil {
status.BuildInfo = &buildInfo
}
status.LiveServers = s.Session.LiveServers()
status.Mode = s.Session.Mode()
status.Safe = s.Session.Safe()
if s.Session.Ping() == nil {
status.Ping = "OK"
}
}
return status
}
func (s *Store) NewSession(collection string) *Session {
return &Session{
sourceSession: s.Session,
database: s.Config.Database,
collection: s.Config.CollectionPrefix + collection,
}
}
type Session struct {
sourceSession *mgo.Session
targetSession *mgo.Session
database string
collection string
}
func (s *Session) IsClosed() bool {
return s.sourceSession == nil
}
func (s *Session) Close() {
if s.targetSession != nil {
s.targetSession.Close()
s.targetSession = nil
}
s.sourceSession = nil
}
func (s *Session) EnsureIndexes() error {
return nil
}
func (s *Session) EnsureAllIndexes(indexes []mgo.Index) error {
for _, index := range indexes {
if err := s.C().EnsureIndex(index); err != nil {
return errors.Wrapf(err, "unable to ensure index with key %v", index.Key)
}
}
return nil
}
func (s *Session) C() *mgo.Collection {
if s.IsClosed() {
return nil
}
if s.targetSession == nil {
s.targetSession = s.sourceSession.Copy()
}
return s.targetSession.DB(s.database).C(s.collection)
}
func (s *Session) ConstructUpdate(set bson.M, unset bson.M) bson.M {
update := bson.M{}
if len(set) != 0 {
update["$set"] = set
}
if len(unset) != 0 {
update["$unset"] = unset
}
if len(update) != 0 {
return update
}
return nil
}