-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmongodb.go
488 lines (410 loc) · 11.3 KB
/
mongodb.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package backends
import (
"context"
"fmt"
"log"
"reflect"
"strings"
"time"
"github.com/Microkubes/microservice-tools/config"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// MONGO_CTX_KEY is mongoDB context key
var MONGO_CTX_KEY = "MONGO_SESSION"
// MongoSession wraps a mgo.Session to embed methods in models.
type MongoSession struct {
Session *mgo.Session
repoDef RepositoryDefinition
databaseName string
collectionName string
}
// GetCollection returns the collection and a session to be closed after
func (s *MongoSession) GetCollection() (*mgo.Session, *mgo.Collection) {
session := s.Session.Copy()
c := session.DB(s.databaseName).C(s.collectionName)
return session, c
}
// MongoDBRepoBuilder builds new mongo collection.
// If it does not exist builder will create it
func MongoDBRepoBuilder(repoDef RepositoryDefinition, backend Backend) (Repository, error) {
sessionObj := backend.GetFromContext(MONGO_CTX_KEY)
if sessionObj == nil {
return nil, ErrBackendError("mongo session not configured")
}
session, ok := sessionObj.(*mgo.Session)
if !ok {
return nil, ErrBackendError("unknown session type")
}
databaseName := backend.GetConfig().DatabaseName
if databaseName == "" {
return nil, ErrBackendError("database name is missing and required")
}
collectionName := repoDef.GetName()
if collectionName == "" {
return nil, ErrBackendError("collection name is missing and required")
}
_, err := PrepareDB(
session,
databaseName,
collectionName,
repoDef.GetIndexes(),
repoDef.EnableTTL(),
repoDef.GetTTL(),
repoDef.GetTTLAttribute(),
)
if err != nil {
return nil, err
}
return &MongoSession{
Session: session,
repoDef: repoDef,
databaseName: databaseName,
collectionName: collectionName,
}, nil
}
// MongoDBBackendBuilder returns RepositoriesBackend
func MongoDBBackendBuilder(conf *config.DBInfo, manager BackendManager) (Backend, error) {
session, err := NewSession(conf.Host, conf.Username, conf.Password, conf.DatabaseName)
if err != nil {
return nil, err
}
ctx := context.WithValue(context.Background(), MONGO_CTX_KEY, session)
cleanup := func() {
session.Close()
}
return NewRepositoriesBackend(ctx, conf, MongoDBRepoBuilder, cleanup), nil
}
// NewSession returns a new Mongo Session.
func NewSession(Host string, Username string, Password string, Database string) (*mgo.Session, error) {
session, err := mgo.DialWithInfo(&mgo.DialInfo{
Addrs: []string{Host},
Username: Username,
Password: Password,
Database: Database,
Timeout: 30 * time.Second,
})
if err != nil {
return nil, err
}
// SetMode - consistency mode for the session.
session.SetMode(mgo.Monotonic, true)
return session, nil
}
// PrepareDB ensure presence of persistent and immutable data in the DB. It creates indexes
func PrepareDB(session *mgo.Session, db string, dbCollection string, indexes []Index, enableTTL bool, TTL int, TTLField string) (*mgo.Collection, error) {
collection := session.DB(db).C(dbCollection)
// Define indexes
for _, elem := range indexes {
i := elem.GetFields()
index := mgo.Index{
Key: i,
Unique: elem.Unique(),
DropDups: true,
Background: true,
Sparse: true,
}
// Create indexes
if err := collection.EnsureIndex(index); err != nil {
if qe, ok := err.(*mgo.QueryError); ok {
if qe.Code == 85 {
// IndexOptionsConflict - see here https://github.com/mongodb/mongo/blob/master/src/mongo/base/error_codes.err
// It means that there is already defined index and we try to redefine it, which is (mostly) fine.
log.Println("WARN: The index already exists and will not be updated. MongoDB error: ", err.Error())
}
} else {
log.Println("ERROR: while creating index. of type: ", reflect.TypeOf(err), " and values: ", fmt.Sprintf("%v", err))
return nil, err
}
}
}
if enableTTL == true {
if TTLField == "" {
return nil, ErrBackendError("TTL attribute is reqired when TTL is enabled")
}
if TTL == 0 {
return nil, ErrBackendError("TTL value is missing and must be greater than zero")
}
index := mgo.Index{
Key: []string{TTLField},
Unique: false,
DropDups: false,
Background: true,
Sparse: true,
ExpireAfter: time.Duration(TTL) * time.Second,
}
if err := collection.EnsureIndex(index); err != nil {
return nil, err
}
}
return collection, nil
}
// GetOne fetches only one record for given filter
func (s *MongoSession) GetOne(filter Filter, result interface{}) (interface{}, error) {
session, c := s.GetCollection()
defer session.Close()
var record map[string]interface{}
if !s.repoDef.IsCustomID() {
if err := stringToObjectID(filter); err != nil {
return nil, err
}
}
err := c.Find(filter).One(&record)
if err != nil {
if err == mgo.ErrNotFound {
return nil, err
}
return nil, err
}
if s.repoDef.IsCustomID() {
record["_id"] = record["_id"].(bson.ObjectId).Hex()
} else {
record["id"] = record["_id"].(bson.ObjectId).Hex()
}
err = MapToInterface(&record, &result)
if err != nil {
return nil, err
}
return result, nil
}
// GetAll fetches all matched records for given filter
func (s *MongoSession) GetAll(filter Filter, resultsTypeHint interface{}, order string, sorting string, limit int, offset int) (interface{}, error) {
session, c := s.GetCollection()
defer session.Close()
resultsTypeHint = AsPtr(resultsTypeHint)
results := NewSliceOfType(resultsTypeHint)
// Create a pointer to a slice value and set it to the slice
slicePointer := reflect.New(results.Type())
slicePointer.Elem().Set(results)
if !s.repoDef.IsCustomID() {
if id, ok := filter["id"]; ok {
// check if id field contains values separated by comma
if ok := strings.Contains(id.(string), ","); ok {
if err := sliceToObjectID(filter); err != nil {
return nil, ErrInvalidInput(err)
}
} else {
if err := stringToObjectID(filter); err != nil {
return nil, ErrInvalidInput(err)
}
}
}
}
mongoFilter, err := toMongoFilter(filter)
if err != nil {
return nil, ErrInvalidInput(err)
}
query := c.Find(mongoFilter)
if order != "" {
if sorting == "desc" {
order = "-" + order
}
query = query.Sort(order)
}
if offset != 0 {
query = query.Skip(offset)
}
if limit != 0 {
query = query.Limit(limit)
}
err = query.All(slicePointer.Interface())
if err != nil {
if err == mgo.ErrNotFound {
return nil, ErrNotFound(err)
}
return nil, err
}
// results is always a Slice
err = IterateOverSlice(slicePointer.Interface(), func(i int, item interface{}) error {
if item == nil {
return nil // ignore
}
itemValue := reflect.ValueOf(item)
itemType := reflect.TypeOf(item)
if itemType.Kind() == reflect.Ptr {
// item is pointer to something
itemType = itemType.Elem()
itemValue = reflect.Indirect(itemValue)
}
if itemType.Kind() == reflect.Map {
// we have a map[string]<some-type>
idValue := itemValue.MapIndex(reflect.ValueOf("_id"))
if idValue.IsValid() {
// ok,there is such value
if bsonID, ok := idValue.Interface().(bson.ObjectId); ok {
idStr := bsonID.Hex()
if s.repoDef.IsCustomID() {
// we have a custom handling on property "id", so we'll map _id => HEX(_id)
itemValue.SetMapIndex(reflect.ValueOf("_id"), reflect.ValueOf(idStr))
} else {
// no custom mapping set, so the default behaviour is to map id => HEX(_id)
itemValue.SetMapIndex(reflect.ValueOf("id"), reflect.ValueOf(idStr))
itemValue.SetMapIndex(reflect.ValueOf("_id"), reflect.Value{})
}
}
}
}
return nil
})
return slicePointer.Interface(), nil
}
// Save creates new record unless it does not exist, otherwise it updates the record
func (s *MongoSession) Save(object interface{}, filter Filter) (interface{}, error) {
session, c := s.GetCollection()
defer session.Close()
var result interface{}
payload, err := InterfaceToMap(object)
if err != nil {
return nil, err
}
if filter == nil {
id := bson.NewObjectId()
(*payload)["_id"] = id
if !s.repoDef.IsCustomID() {
delete(*payload, "id")
}
err = c.Insert(payload)
if err != nil {
if mgo.IsDup(err) {
return nil, ErrAlreadyExists("record already exists!")
}
return nil, err
}
if !s.repoDef.IsCustomID() {
(*payload)["id"] = id.Hex()
}
err = MapToInterface(payload, &object)
if err != nil {
return nil, err
}
return object, nil
}
if !s.repoDef.IsCustomID() {
if err := stringToObjectID(filter); err != nil {
return nil, ErrInvalidInput(err)
}
}
if _, ok := (*payload)["_id"]; ok {
// we can't update MongoDB's own id - it is immutable.
delete(*payload, "_id")
}
err = c.Update(filter, bson.M{"$set": payload})
if err != nil {
if err == mgo.ErrNotFound {
return nil, ErrNotFound(err)
}
if mgo.IsDup(err) {
return nil, ErrAlreadyExists("record already exists!")
}
return nil, err
}
result, err = s.GetOne(filter, object)
if err != nil {
return nil, err
}
return result, nil
}
// DeleteOne deletes only one record for given filter
func (s *MongoSession) DeleteOne(filter Filter) error {
session, c := s.GetCollection()
defer session.Close()
if !s.repoDef.IsCustomID() {
if err := stringToObjectID(filter); err != nil {
return ErrInvalidInput(err)
}
}
err := c.Remove(filter)
if err != nil {
if err == mgo.ErrNotFound {
return ErrNotFound(err)
}
return err
}
return nil
}
// DeleteAll deletes all matched records for given filter
func (s *MongoSession) DeleteAll(filter Filter) error {
session, c := s.GetCollection()
defer session.Close()
if !s.repoDef.IsCustomID() {
if err := stringToObjectID(filter); err != nil {
return ErrInvalidInput(err)
}
}
_, err := c.RemoveAll(filter)
if err != nil {
if err == mgo.ErrNotFound {
return ErrNotFound(err)
}
return err
}
return nil
}
func toMongoFilter(filter Filter) (map[string]interface{}, error) {
mgf := map[string]interface{}{}
for key, value := range filter {
if specs, ok := value.(map[string]string); ok {
if pattern, ok := specs["$pattern"]; ok {
mongoPattern := toMongoPattern(pattern)
mgf[key] = bson.M{
"$regex": mongoPattern,
}
continue
}
return nil, fmt.Errorf("unknown filter specification - supported type is $pattern")
}
// if filter key contains multiple values to search by
if val, ok := value.(string); ok {
if values := strings.Split(val, ","); len(values) > 1 {
mgf[key] = bson.M{
"$in": values,
}
continue
}
}
// if filter _id contains multiple id to search by
if val, ok := value.([]bson.ObjectId); ok {
if len(val) > 1 {
mgf[key] = bson.M{
"$in": val,
}
continue
}
}
mgf[key] = value // copy over the key=>value pairs to do exact matching
}
return mgf, nil
}
func toMongoPattern(pattern string) string {
mongoPattern := ""
prev := '\000'
for _, r := range pattern {
if r == '%' {
if prev == '%' {
mongoPattern += "%"
prev = '\000'
continue
}
prev = r
continue
}
if prev == '%' {
mongoPattern += ".*"
}
if r != '\000' {
mongoPattern += string(r)
}
prev = r
}
if prev == '%' {
// at the very end of the pattern
mongoPattern += ".*"
}
if !strings.HasPrefix(mongoPattern, ".*") {
mongoPattern = "^" + mongoPattern
}
if !strings.HasSuffix(mongoPattern, ".*") {
mongoPattern = mongoPattern + "$"
}
return mongoPattern
}