-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdynamo_repository.go
595 lines (497 loc) · 20 KB
/
dynamo_repository.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
package djoemo
import (
"context"
"errors"
"reflect"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
"github.com/guregu/dynamo"
)
// Repository facade for github.com/guregu/djoemo
type Repository struct {
dynamoClient *dynamo.DB
log logger
metrics metrics
}
// NewRepository factory method for djoemo repository
func NewRepository(dynamoClient dynamodbiface.DynamoDBAPI) RepositoryInterface {
return &Repository{
dynamoClient: dynamo.NewFromIface(dynamoClient),
log: logger{log: nopLog{}},
}
}
// WithLog enables logging; it accepts LogInterface as logger
func (repository *Repository) WithLog(log LogInterface) {
repository.log = logger{log: log}
}
// WithMetrics enables metrics; it accepts MetricsInterface as metrics publisher
func (repository *Repository) WithMetrics(metricsInterface MetricsInterface) {
repository.metrics = metrics{metrics: metricsInterface}
}
// GetItemWithContext get item; it accepts a key interface that is used to get the table name, hash key and range key if it exists;
// context which used to enable log with context; the output will be given in item
// returns true if item is found, returns false and nil if no item found, returns false and an error in case of error
func (repository *Repository) GetItemWithContext(ctx context.Context, key KeyInterface, item interface{}) (bool, error) {
if err := isValidKey(key); err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return false, err
}
// by hash
query := repository.table(key.TableName()).Get(*key.HashKeyName(), key.HashKey())
// by range
if key.RangeKeyName() != nil && key.RangeKey() != nil {
query = query.Range(*key.RangeKeyName(), dynamo.Equal, key.RangeKey())
}
err := query.OneWithContext(ctx, item)
if err != nil {
if err == dynamo.ErrNotFound {
repository.log.info(ctx, key.TableName(), ErrNoItemFound.Error())
return false, nil
}
repository.log.error(ctx, key.TableName(), err.Error())
return false, err
}
return true, nil
}
// SaveItemWithContext it accepts a key interface, that is used to get the table name; item is the item to be saved; context which used to enable log with context
// returns error in case of error
func (repository *Repository) SaveItemWithContext(ctx context.Context, key KeyInterface, item interface{}) error {
if err := isValidKey(key); err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return err
}
err := repository.table(key.TableName()).Put(item).RunWithContext(ctx)
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return err
}
err = repository.metrics.Publish(ctx, key.TableName(), MetricNameSavedItemsCount, float64(1))
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
}
return nil
}
// UpdateWithContext updates item by key; it accepts an expression (Set, SetSet, SetIfNotExists, SetExpr); key is the key to be updated;
// values contains the values that should be used in the update; context which used to enable log with context
// returns error in case of error
func (repository *Repository) UpdateWithContext(ctx context.Context, expression UpdateExpression, key KeyInterface, values map[string]interface{}) error {
if err := isValidKey(key); err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return err
}
// by hash
update := repository.table(key.TableName()).Update(*key.HashKeyName(), key.HashKey())
// by range
if key.RangeKeyName() != nil && key.RangeKey() != nil {
update = update.Range(*key.RangeKeyName(), key.RangeKey())
}
for expr, value := range values {
if expression == Add {
update.Add(expr, value)
}
if expression == Set {
update.Set(expr, value)
}
if expression == SetSet {
update.SetSet(expr, value)
}
if expression == SetIfNotExists {
update.SetIfNotExists(expr, value)
}
if expression == SetExpr {
valueSlice, err := InterfaceToArrayOfInterface(value)
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return err
}
update.SetExpr(expr, valueSlice...)
}
}
err := update.RunWithContext(ctx)
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return err
}
err = repository.metrics.Publish(ctx, key.TableName(), MetricNameUpdatedItemsCount, float64(1))
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
}
return nil
}
func (repository *Repository) prepareUpdateWithUpdateExpressions(
ctx context.Context,
key KeyInterface,
updateExpressions UpdateExpressions,
) (*dynamo.Update, error) {
if err := isValidKey(key); err != nil {
return nil, err
}
// by hash
update := repository.table(key.TableName()).Update(*key.HashKeyName(), key.HashKey())
// by range
if key.RangeKeyName() != nil && key.RangeKey() != nil {
update = update.Range(*key.RangeKeyName(), key.RangeKey())
}
for updateExpression, v := range updateExpressions {
expression := UpdateExpression(updateExpression)
for expr, value := range v {
if expression == Add {
update.Add(expr, value)
}
if expression == Set {
update.Set(expr, value)
}
if expression == SetSet {
update.SetSet(expr, value)
}
if expression == SetIfNotExists {
update.SetIfNotExists(expr, value)
}
if expression == SetExpr {
valueSlice, err := InterfaceToArrayOfInterface(value)
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return nil, err
}
update.SetExpr(expr, valueSlice...)
}
}
}
return update, nil
}
// UpdateWithUpdateExpressions updates an item with update expressions defined at field level, enabling you to set
// different update expressions for each field. The first key of the updateMap specifies the Update expression to use
// for the expressions in the map
func (repository *Repository) UpdateWithUpdateExpressions(
ctx context.Context,
key KeyInterface,
updateExpressions UpdateExpressions,
) error {
update, err := repository.prepareUpdateWithUpdateExpressions(ctx, key, updateExpressions)
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return err
}
err = update.RunWithContext(ctx)
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return err
}
err = repository.metrics.Publish(ctx, key.TableName(), MetricNameUpdatedItemsCount, float64(1))
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
}
return nil
}
// UpdateWithUpdateExpressionsAndReturnValue updates an item with update expressions defined at field level and returns
// the item, as it appears after the update, enabling you to set different update expressions for each field. The first
// key of the updateMap specifies the Update expression to use for the expressions in the map
func (repository *Repository) UpdateWithUpdateExpressionsAndReturnValue(
ctx context.Context,
key KeyInterface,
item interface{},
updateExpressions UpdateExpressions,
) error {
update, err := repository.prepareUpdateWithUpdateExpressions(ctx, key, updateExpressions)
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return err
}
err = update.ValueWithContext(ctx, item)
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return err
}
err = repository.metrics.Publish(ctx, key.TableName(), MetricNameUpdatedItemsCount, float64(1))
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
}
return nil
}
// ConditionalUpdateWithUpdateExpressionsAndReturnValue updates an item with update expressions and a condition.
// If the condition is met, the item will be updated and returned as it appears after the update.
// The first key of the updateMap specifies the Update expression to use for the expressions in the map
func (repository *Repository) ConditionalUpdateWithUpdateExpressionsAndReturnValue(
ctx context.Context,
key KeyInterface,
item interface{},
updateExpressions UpdateExpressions,
conditionExpression string,
conditionArgs ...interface{},
) (conditionMet bool, err error) {
update, err := repository.prepareUpdateWithUpdateExpressions(ctx, key, updateExpressions)
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return false, err
}
update = update.If(conditionExpression, conditionArgs...)
err = update.ValueWithContext(ctx, item)
if err != nil {
if awsError, ok := err.(awserr.Error); ok && awsError.Code() == dynamodb.ErrCodeConditionalCheckFailedException {
repository.log.info(ctx, key.TableName(), dynamodb.ErrCodeConditionalCheckFailedException)
return false, nil
}
repository.log.error(ctx, key.TableName(), err.Error())
return false, err
}
err = repository.metrics.Publish(ctx, key.TableName(), MetricNameUpdatedItemsCount, float64(1))
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
}
return true, nil
}
// DeleteItemWithContext item by its key; it accepts key of item to be deleted; context which used to enable log with context
// returns error in case of error
func (repository *Repository) DeleteItemWithContext(ctx context.Context, key KeyInterface) error {
if err := isValidKey(key); err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return err
}
// by hash
delete := repository.table(key.TableName()).Delete(*key.HashKeyName(), key.HashKey())
// by range
if key.RangeKeyName() != nil && key.RangeKey() != nil {
delete = delete.Range(*key.RangeKeyName(), key.RangeKey())
}
err := delete.RunWithContext(ctx)
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return err
}
err = repository.metrics.Publish(ctx, key.TableName(), MetricNameDeleteItemsCount, float64(1))
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
}
return nil
}
// SaveItemsWithContext batch save a slice of items by key; it accepts key of item to be saved; item to be saved; context which used to enable log with context
// returns error in case of error
func (repository *Repository) SaveItemsWithContext(ctx context.Context, key KeyInterface, items interface{}) error {
if err := isValidKey(key); err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return err
}
// by hash
batch := repository.table(key.TableName()).Batch(*key.HashKeyName())
// by hash & range
if key.RangeKeyName() != nil {
batch = repository.table(key.TableName()).Batch(*key.HashKeyName(), *key.RangeKeyName())
}
itemSlice, err := InterfaceToArrayOfInterface(items)
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return err
}
count, err := batch.Write().Put(itemSlice...).RunWithContext(ctx)
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return err
}
err = repository.metrics.Publish(ctx, key.TableName(), MetricNameSavedItemsCount, float64(count))
if err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
}
return nil
}
// DeleteItemsWithContext deletes items matching the keys; it accepts array of keys to be deleted; context which used to enable log with context
// returns error in case of error
func (repository *Repository) DeleteItemsWithContext(ctx context.Context, keys []KeyInterface) error {
if len(keys) == 0 {
return nil
}
for i := 0; i < len(keys); i++ {
if err := isValidKey(keys[i]); err != nil {
repository.log.error(ctx, keys[i].TableName(), err.Error())
return err
}
}
// by hash
batch := repository.table(keys[0].TableName()).Batch(*keys[0].HashKeyName())
// by hash & range
if keys[0].RangeKeyName() != nil {
batch = repository.table(keys[0].TableName()).Batch(*keys[0].HashKeyName(), *keys[0].RangeKeyName())
}
dynamoKeys := make([]dynamo.Keyed, len(keys))
for i := 0; i < len(keys); i++ {
dynamoKeys[i] = dynamo.Keyed(keys[i])
}
count, err := batch.Write().Delete(dynamoKeys...).RunWithContext(ctx)
if err != nil {
repository.log.error(ctx, keys[0].TableName(), err.Error())
return err
}
err = repository.metrics.Publish(ctx, keys[0].TableName(), MetricNameDeleteItemsCount, float64(count))
if err != nil {
repository.log.error(ctx, keys[0].TableName(), err.Error())
}
return nil
}
// GetItemsWithContext by key; it accepts a key interface that is used to get the table name, hash key and range key if it exists;
// context which used to enable log with context, the output will be given in items
// returns true if items are found, returns false and nil if no items found, returns false and error in case of error
func (repository *Repository) GetItemsWithContext(ctx context.Context, key KeyInterface, items interface{}) (bool, error) {
if err := isValidKey(key); err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return false, err
}
err := repository.table(key.TableName()).Get(*key.HashKeyName(), key.HashKey()).AllWithContext(ctx, items)
if err != nil {
if err == dynamo.ErrNotFound {
repository.log.info(ctx, key.TableName(), ErrNoItemFound.Error())
return false, nil
}
repository.log.error(ctx, key.TableName(), err.Error())
return false, err
}
val := reflect.ValueOf(items)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if val.Kind() == reflect.Array || val.Kind() == reflect.Slice {
if val.Len() == 0 {
return false, nil
}
}
return true, nil
}
// QueryWithContext by query; it accepts a query interface that is used to get the table name, hash key and range key with its operator if it exists;
// context which used to enable log with context, the output will be given in items
// returns error in case of error
func (repository *Repository) QueryWithContext(ctx context.Context, query QueryInterface, item interface{}) error {
if !IsPointerOFSlice(item) {
return ErrInvalidPointerSliceType
}
if err := isValidKey(query); err != nil {
repository.log.error(ctx, query.TableName(), err.Error())
return err
}
q := repository.table(query.TableName()).Get(*query.HashKeyName(), query.HashKey())
// by range
if query.RangeKeyName() != nil && query.RangeKey() != nil {
q = q.Range(*query.RangeKeyName(), dynamo.Operator(query.RangeOp()), query.RangeKey())
}
if limit := valueFromPtr(query.Limit()); limit > 0 {
q = q.Limit(limit)
}
if query.Descending() {
q = q.Order(dynamo.Descending)
}
err := q.AllWithContext(ctx, item)
if err != nil {
repository.log.error(ctx, query.TableName(), err.Error())
return err
}
return nil
}
// OptimisticLockSaveWithContext saves an item if the version attribute on the server matches the version of the object
func (repository Repository) OptimisticLockSaveWithContext(ctx context.Context, key KeyInterface, item interface{}) (bool, error) {
model, isDjoemoModel := item.(ModelInterface)
if !isDjoemoModel {
return false, errors.New("Items to use with OptimisticLock must implement the ModelInterface")
}
currentVersion := model.GetVersion()
model.IncreaseVersion()
model.InitCreatedAt()
model.InitUpdatedAt()
update := repository.table(key.TableName()).Put(item).If("attribute_not_exists(Version) OR Version = ?", currentVersion)
err := update.Run()
if err != nil {
if awserr, ok := err.(awserr.Error); ok && awserr.Code() == dynamodb.ErrCodeConditionalCheckFailedException {
repository.log.info(ctx, key.TableName(), dynamodb.ErrCodeConditionalCheckFailedException)
return false, nil
}
repository.log.error(ctx, key.TableName(), err.Error())
return false, err
}
return true, nil
}
// ConditionalUpdateWithContext updates an item when the condition is met, otherwise the update will be rejected
func (repository Repository) ConditionalUpdateWithContext(ctx context.Context, key KeyInterface, item interface{}, expression string, expressionArgs ...interface{}) (bool, error) {
update := repository.table(key.TableName()).Put(item).If(expression, expressionArgs...)
err := update.Run()
if err != nil {
if awserr, ok := err.(awserr.Error); ok && awserr.Code() == dynamodb.ErrCodeConditionalCheckFailedException {
repository.log.info(ctx, key.TableName(), dynamodb.ErrCodeConditionalCheckFailedException)
return false, nil
}
repository.log.error(ctx, key.TableName(), err.Error())
return false, err
}
return true, nil
}
// GetItem get item; it accepts a key interface that is used to get the table name, hash key and range key if it exists; the output will be given in item
// returns true if item is found, returns false and nil if no item found, returns false and an error in case of error
func (repository Repository) GetItem(key KeyInterface, item interface{}) (bool, error) {
return repository.GetItemWithContext(context.TODO(), key, item)
}
// SaveItem item; it accepts a key interface, that is used to get the table name; item is the item to be saved
// returns error in case of error
func (repository Repository) SaveItem(key KeyInterface, item interface{}) error {
return repository.SaveItemWithContext(context.TODO(), key, item)
}
// Update updates item by key; it accepts an expression (Set, SetSet, SetIfNotExists, SetExpr); key is the key to be updated;
// values contains the values that should be used in the update;
// returns error in case of error
func (repository Repository) Update(expression UpdateExpression, key KeyInterface, values map[string]interface{}) error {
return repository.UpdateWithContext(context.TODO(), expression, key, values)
}
// DeleteItem item by key; returns error in case of error
func (repository Repository) DeleteItem(key KeyInterface) error {
return repository.DeleteItemWithContext(context.TODO(), key)
}
// SaveItems batch save a slice of items by key
func (repository Repository) SaveItems(key KeyInterface, items interface{}) error {
return repository.SaveItemsWithContext(context.TODO(), key, items)
}
// DeleteItems deletes items matching the keys
func (repository Repository) DeleteItems(keys []KeyInterface) error {
return repository.DeleteItemsWithContext(context.TODO(), keys)
}
// GetItems by key; it accepts a key interface that is used to get the table name, hash key and range key if it exists; the output will be given in items
// returns true if items are found, returns false and nil if no items found, returns false and error in case of error
func (repository Repository) GetItems(key KeyInterface, items interface{}) (bool, error) {
return repository.GetItemsWithContext(context.TODO(), key, items)
}
// Query by query; it accepts a query interface that is used to get the table name, hash key and range key with its operator if it exists;
// returns error in case of error
func (repository Repository) Query(query QueryInterface, item interface{}) error {
return repository.QueryWithContext(context.TODO(), query, item)
}
// OptimisticLockSave updates an item if the version attribute on the server matches the one of the object
func (repository Repository) OptimisticLockSave(key KeyInterface, item interface{}) (bool, error) {
return repository.OptimisticLockSaveWithContext(context.TODO(), key, item)
}
// ConditionalUpdate updates an item when the condition is met, otherwise the update will be rejected
func (repository Repository) ConditionalUpdate(key KeyInterface, item interface{}, expression string, expressionArgs ...interface{}) (bool, error) {
return repository.ConditionalUpdateWithContext(context.TODO(), key, item, expression, expressionArgs)
}
// GIndex creates an index repository by name
func (repository Repository) GIndex(name string) GlobalIndexInterface {
return GlobalIndex{
name: name,
log: repository.log,
dynamoClient: repository.dynamoClient,
}
}
func (repository Repository) table(tableName string) dynamo.Table {
return repository.dynamoClient.Table(tableName)
}
// ScanIteratorWithContext returns an instance of an Iterator that provides methods for scanning tables
func (repository Repository) ScanIteratorWithContext(ctx context.Context, key KeyInterface, searchLimit int64) (IteratorInterface, error) {
if err := isValidTableName(key); err != nil {
repository.log.error(ctx, key.TableName(), err.Error())
return nil, err
}
scan := repository.table(key.TableName()).Scan()
pagingIterator := scan.Iter()
itr := &Iterator{
scan: scan,
tableName: key.TableName(),
searchLimit: searchLimit,
iterator: pagingIterator,
ctx: ctx,
}
itr.scan.SearchLimit(searchLimit)
return itr, nil
}