-
Notifications
You must be signed in to change notification settings - Fork 3
/
collection.go
299 lines (261 loc) · 7.93 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
package keelmongo
import (
"context"
"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"
keelerrors "github.com/foomo/keel/errors"
keelpersistence "github.com/foomo/keel/persistence"
keeltime "github.com/foomo/keel/time"
)
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)
)
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()
}
}
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
}
func (c *Collection) DB() *mongo.Database {
return c.db
}
func (c *Collection) Col() *mongo.Collection {
return c.collection
}
// Get ...
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...)
}
// Exists ...
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
}
// Upsert inserts/updates with protection against dirty-writes
// requires an unique index on id AND id + version
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
}
func (c *Collection) Insert(ctx context.Context, entity Entity) error {
if _, err := c.collection.InsertOne(ctx, entity); 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
}
// Find ...
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
}
defer CloseCursor(ctx, cursor, &err)
err = cursor.All(ctx, results)
return err
}
// FindOne ...
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)
}
// FindIterate ...
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(ctx, cursor, &err)
for cursor.Next(ctx) {
if err := handler(cursor.Decode); err != nil {
return err
}
}
return err
}
// Aggregate ...
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
}
defer CloseCursor(ctx, cursor, &err)
err = cursor.All(ctx, results)
return 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(ctx, cursor, &err)
for cursor.Next(ctx) {
if err := handler(cursor.Decode); err != nil {
return err
}
}
return nil
}
// 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{})
}