-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.go
96 lines (90 loc) · 2.18 KB
/
updater.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
package dalgo2badger
import (
"context"
"encoding/json"
"fmt"
"github.com/dal-go/dalgo/dal"
"github.com/dgraph-io/badger/v4"
)
func (dtb database) Update(
ctx context.Context,
key *dal.Key,
updates []dal.Update,
preconditions ...dal.Precondition,
) error {
return dtb.db.Update(func(txn *badger.Txn) error {
return transaction{txn: txn}.Update(ctx, key, updates, preconditions...)
})
}
func (dtb database) UpdateMulti(
ctx context.Context,
keys []*dal.Key,
updates []dal.Update,
preconditions ...dal.Precondition,
) error {
return dtb.db.Update(func(txn *badger.Txn) error {
tx := transaction{txn: txn}
return tx.UpdateMulti(ctx, keys, updates, preconditions...)
})
}
func (t transaction) Update(
ctx context.Context,
key *dal.Key,
updates []dal.Update,
preconditions ...dal.Precondition,
) error {
// we need the t.update() method as it is reused in UpdateMulti()
return t.update(ctx, key, updates, preconditions...)
}
func (t transaction) UpdateMulti(
ctx context.Context,
keys []*dal.Key,
updates []dal.Update,
preconditions ...dal.Precondition,
) error {
for i, key := range keys {
if err := t.update(ctx, key, updates, preconditions...); err != nil {
if err == dal.ErrRecordNotFound {
continue
}
return fmt.Errorf("failed to update record %d of %d: %w", i+1, len(keys), err)
}
}
return nil
}
func (t transaction) update(
_ context.Context,
key *dal.Key,
updates []dal.Update,
preconditions ...dal.Precondition,
) error {
k := []byte(key.String())
item, err := t.txn.Get(k)
if err != nil {
if err == badger.ErrKeyNotFound {
return dal.ErrRecordNotFound
}
return err
}
data := make(map[string]interface{})
err = item.Value(func(val []byte) error {
if err = json.Unmarshal(val, &data); err != nil {
return fmt.Errorf("failed to unmarshal data as JSON object: %v", err)
}
return nil
})
if err != nil {
return fmt.Errorf("failed to unmarshal data as JSON object: %v", err)
}
for _, u := range updates {
data[u.Field] = u.Value
}
var b []byte
if b, err = json.Marshal(data); err != nil {
return fmt.Errorf("failed to marshal data as JSON object: %v", err)
}
if err = t.txn.Set(k, b); err != nil {
return err
}
return nil
}