-
Notifications
You must be signed in to change notification settings - Fork 3
/
collection.go
434 lines (366 loc) · 11.8 KB
/
collection.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
package keelmongo
import (
"context"
"time"
keelerrors "github.com/foomo/keel/errors"
keelpersistence "github.com/foomo/keel/persistence"
keeltime "github.com/foomo/keel/time"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
)
type (
DecodeFn func(val interface{}) error
IterateHandlerFn func(decode DecodeFn) error
)
// Collection can only be used in the Persistor.WithCollection call.ss
type (
Collection struct {
db *mongo.Database
collection *mongo.Collection
}
CollectionOptions struct {
*options.CollectionOptions
*options.CreateIndexesOptions
Indexes []mongo.IndexModel
IndexesContext context.Context
}
CollectionOption func(*CollectionOptions)
)
// ------------------------------------------------------------------------------------------------
// ~ Options
// ------------------------------------------------------------------------------------------------
func DefaultCollectionOptions() CollectionOptions {
return CollectionOptions{
CollectionOptions: options.Collection(),
CreateIndexesOptions: options.CreateIndexes(),
IndexesContext: context.Background(),
}
}
func CollectionWithReadConcern(v *readconcern.ReadConcern) CollectionOption {
return func(o *CollectionOptions) {
o.CollectionOptions.SetReadConcern(v)
}
}
func CollectionWithWriteConcern(v *writeconcern.WriteConcern) CollectionOption {
return func(o *CollectionOptions) {
o.CollectionOptions.SetWriteConcern(v)
}
}
func CollectionWithReadPreference(v *readpref.ReadPref) CollectionOption {
return func(o *CollectionOptions) {
o.CollectionOptions.SetReadPreference(v)
}
}
func CollectionWithRegistry(v *bsoncodec.Registry) CollectionOption {
return func(o *CollectionOptions) {
o.CollectionOptions.SetRegistry(v)
}
}
func CollectionWithIndexes(v ...mongo.IndexModel) CollectionOption {
return func(o *CollectionOptions) {
o.Indexes = v
}
}
func CollectionWithIndexesMaxTime(v time.Duration) CollectionOption {
return func(o *CollectionOptions) {
o.CreateIndexesOptions.SetMaxTime(v)
}
}
func CollectionWithIndexesContext(v int32) CollectionOption {
return func(o *CollectionOptions) {
o.CreateIndexesOptions.SetCommitQuorumInt(v)
}
}
func CollectionWithIndexesQuorumMajority() CollectionOption {
return func(o *CollectionOptions) {
o.CreateIndexesOptions.SetCommitQuorumMajority()
}
}
func CollectionWithIndexesCommitQuorumString(v string) CollectionOption {
return func(o *CollectionOptions) {
o.CreateIndexesOptions.SetCommitQuorumString(v)
}
}
func CollectionWithIndexesCommitQuorumVotingMembers(v context.Context) CollectionOption {
return func(o *CollectionOptions) {
o.CreateIndexesOptions.SetCommitQuorumVotingMembers()
}
}
// ------------------------------------------------------------------------------------------------
// ~ Constructor
// ------------------------------------------------------------------------------------------------
func NewCollection(db *mongo.Database, name string, opts ...CollectionOption) (*Collection, error) {
o := DefaultCollectionOptions()
for _, opt := range opts {
opt(&o)
}
col := db.Collection(name, o.CollectionOptions)
if len(o.Indexes) > 0 {
if _, err := col.Indexes().CreateMany(o.IndexesContext, o.Indexes, o.CreateIndexesOptions); err != nil {
return nil, err
}
}
return &Collection{
db: db,
collection: col,
}, nil
}
// ------------------------------------------------------------------------------------------------
// ~ Getter
// ------------------------------------------------------------------------------------------------
func (c *Collection) DB() *mongo.Database {
return c.db
}
func (c *Collection) Col() *mongo.Collection {
return c.collection
}
// ------------------------------------------------------------------------------------------------
// ~ Public methods
// ------------------------------------------------------------------------------------------------
func (c *Collection) Get(ctx context.Context, id string, result interface{}, opts ...*options.FindOneOptions) error {
if id == "" {
return keelpersistence.ErrNotFound
}
return c.FindOne(ctx, bson.M{"id": id}, result, opts...)
}
func (c *Collection) Exists(ctx context.Context, id string) (bool, error) {
if id == "" {
return false, nil
}
ret, err := c.collection.CountDocuments(ctx, bson.M{"id": id})
return ret > 0, err
}
func (c *Collection) Upsert(ctx context.Context, id string, entity Entity) error {
if id == "" {
return errors.New("id must not be empty")
} else if entity == nil {
return errors.New("entity must not be nil")
}
if v, ok := entity.(EntityWithTimestamps); ok {
now := keeltime.Now()
if ct := v.GetCreatedAt(); ct.IsZero() {
v.SetCreatedAt(now)
}
v.SetUpdatedAt(now)
}
if v, ok := entity.(EntityWithVersion); ok {
currentVersion := v.GetVersion()
// increment version
v.IncreaseVersion()
if currentVersion == 0 {
// insert the new document
return c.Insert(ctx, entity)
} else if err := c.collection.FindOneAndUpdate(
ctx,
bson.D{{Key: "id", Value: id}, {Key: "version", Value: currentVersion}},
bson.D{{Key: "$set", Value: entity}},
options.FindOneAndUpdate().SetUpsert(false),
).Err(); errors.Is(err, mongo.ErrNoDocuments) {
return keelerrors.NewWrappedError(keelpersistence.ErrDirtyWrite, err)
} else if err != nil {
return err
}
} else if _, err := c.collection.UpdateOne(
ctx,
bson.D{{Key: "id", Value: id}},
bson.D{{Key: "$set", Value: entity}},
options.Update().SetUpsert(true),
); err != nil {
return err
}
return nil
}
// UpsertMany - NOTE: upsert many does NOT through an explicit error on dirty write so we can only assume it.
func (c *Collection) UpsertMany(ctx context.Context, entities []Entity) error {
var versionUpserts int64
var operations []mongo.WriteModel
for _, entity := range entities {
if entity == nil {
return errors.New("entity must not be nil")
} else if entity.GetID() == "" {
return errors.New("id must not be empty")
}
if v, ok := entity.(EntityWithTimestamps); ok {
now := keeltime.Now()
if ct := v.GetCreatedAt(); ct.IsZero() {
v.SetCreatedAt(now)
}
v.SetUpdatedAt(now)
}
if v, ok := entity.(EntityWithVersion); ok {
currentVersion := v.GetVersion()
// increment version
v.IncreaseVersion()
if currentVersion == 0 {
operations = append(operations,
mongo.NewInsertOneModel().SetDocument(entity),
)
} else {
versionUpserts++
operations = append(operations,
mongo.NewUpdateOneModel().
SetFilter(bson.D{{Key: "id", Value: entity.GetID()}, {Key: "version", Value: currentVersion}}).
SetUpdate(bson.D{{Key: "$set", Value: entity}}).
SetUpsert(false),
)
}
} else {
operations = append(operations,
mongo.NewUpdateOneModel().
SetFilter(bson.D{{Key: "id", Value: entity.GetID()}}).
SetUpdate(bson.D{{Key: "$set", Value: entity}}).
SetUpsert(true),
)
}
}
// Specify an option to turn the bulk insertion in order of operation
bulkOption := options.BulkWriteOptions{}
bulkOption.SetOrdered(false)
res, err := c.Col().BulkWrite(ctx, operations, &bulkOption)
if err != nil {
return err
} else if versionUpserts > 0 && (res.MatchedCount < versionUpserts || res.ModifiedCount != res.MatchedCount) {
// log.Logger().Info("missing upserts",
// zap.Int64("MatchedCount", res.MatchedCount),
// zap.Int64("InsertedCount", res.InsertedCount),
// zap.Int64("UpsertedCount", res.UpsertedCount),
// zap.Int64("ModifiedCount", res.ModifiedCount),
// zap.Any("UpsertedIDs", res.UpsertedIDs),
// zap.Any("versionUpserts", versionUpserts),
// )
return keelpersistence.ErrDirtyWrite
}
return nil
}
func (c *Collection) Insert(ctx context.Context, entity Entity) error {
if entity == nil {
return errors.New("entity must not be nil")
} else if entity.GetID() == "" {
return errors.New("id must not be empty")
}
if v, ok := entity.(EntityWithTimestamps); ok {
now := keeltime.Now()
if ct := v.GetCreatedAt(); ct.IsZero() {
v.SetCreatedAt(now)
}
v.SetUpdatedAt(now)
}
if v, ok := entity.(EntityWithVersion); ok {
// increment version
v.IncreaseVersion()
}
if _, err := c.collection.InsertOne(ctx, entity); err != nil {
return err
}
return nil
}
func (c *Collection) InsertMany(ctx context.Context, entities []Entity) error {
inserts := make([]interface{}, len(entities))
for i, entity := range entities {
if entity == nil {
return errors.New("entity must not be nil")
} else if entity.GetID() == "" {
return errors.New("id must not be empty")
}
if v, ok := entity.(EntityWithTimestamps); ok {
now := keeltime.Now()
if ct := v.GetCreatedAt(); ct.IsZero() {
v.SetCreatedAt(now)
}
v.SetUpdatedAt(now)
}
if v, ok := entity.(EntityWithVersion); ok {
// increment version
v.IncreaseVersion()
}
inserts[i] = entity
}
if _, err := c.collection.InsertMany(ctx, inserts); err != nil {
return err
}
return nil
}
func (c *Collection) Delete(ctx context.Context, id string) error {
if id == "" {
return keelpersistence.ErrNotFound
}
if err := c.collection.FindOneAndDelete(ctx, bson.M{"id": id}).Err(); errors.Is(err, mongo.ErrNoDocuments) {
return keelerrors.NewWrappedError(keelpersistence.ErrNotFound, err)
} else if err != nil {
return err
}
return nil
}
func (c *Collection) Find(ctx context.Context, filter, results interface{}, opts ...*options.FindOptions) error {
cursor, err := c.collection.Find(ctx, filter, opts...)
if errors.Is(err, mongo.ErrNoDocuments) {
return keelerrors.NewWrappedError(keelpersistence.ErrNotFound, err)
} else if err != nil {
return err
}
if err = cursor.All(ctx, results); err != nil {
return err
}
return cursor.Err()
}
func (c *Collection) FindOne(ctx context.Context, filter, result interface{}, opts ...*options.FindOneOptions) error {
res := c.collection.FindOne(ctx, filter, opts...)
if errors.Is(res.Err(), mongo.ErrNoDocuments) {
return keelerrors.NewWrappedError(keelpersistence.ErrNotFound, res.Err())
} else if res.Err() != nil {
return res.Err()
}
return res.Decode(result)
}
func (c *Collection) FindIterate(ctx context.Context, filter interface{}, handler IterateHandlerFn, opts ...*options.FindOptions) error {
cursor, err := c.collection.Find(ctx, filter, opts...)
if errors.Is(err, mongo.ErrNoDocuments) {
return keelerrors.NewWrappedError(keelpersistence.ErrNotFound, err)
} else if err != nil {
return err
}
defer CloseCursor(cursor)
for cursor.Next(ctx) {
if err := handler(cursor.Decode); err != nil {
return err
}
}
return cursor.Err()
}
func (c *Collection) Aggregate(ctx context.Context, pipeline mongo.Pipeline, results interface{}, opts ...*options.AggregateOptions) error {
cursor, err := c.collection.Aggregate(ctx, pipeline, opts...)
if err != nil {
return err
}
if err = cursor.All(ctx, results); err != nil {
return err
}
return cursor.Err()
}
func (c *Collection) AggregateIterate(ctx context.Context, pipeline mongo.Pipeline, handler IterateHandlerFn, opts ...*options.AggregateOptions) error {
cursor, err := c.collection.Aggregate(ctx, pipeline, opts...)
if err != nil {
return err
}
defer CloseCursor(cursor)
for cursor.Next(ctx) {
if err := handler(cursor.Decode); err != nil {
return err
}
}
return cursor.Err()
}
// Count returns the count of documents
func (c *Collection) Count(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error) {
return c.collection.CountDocuments(ctx, filter, opts...)
}
// CountAll returns the count of all documents
func (c *Collection) CountAll(ctx context.Context) (int64, error) {
return c.Count(ctx, bson.D{})
}