-
Notifications
You must be signed in to change notification settings - Fork 0
/
setter.go
42 lines (37 loc) · 961 Bytes
/
setter.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
package dalgo2badger
import (
"context"
"encoding/json"
"github.com/dal-go/dalgo/dal"
"github.com/dgraph-io/badger/v4"
)
func (dtb database) Set(ctx context.Context, record dal.Record) error {
return dtb.db.Update(func(txn *badger.Txn) error {
return transaction{txn: txn}.Set(ctx, record)
})
}
func (dtb database) SetMulti(ctx context.Context, records []dal.Record) error {
return dtb.db.Update(func(txn *badger.Txn) error {
return transaction{txn: txn}.SetMulti(ctx, records)
})
}
func (t transaction) Set(ctx context.Context, record dal.Record) error {
key := record.Key()
k := []byte(key.String())
record.SetError(nil)
data := record.Data()
s, err := json.Marshal(data)
if err != nil {
return err
}
err = t.txn.Set(k, s)
return err
}
func (t transaction) SetMulti(ctx context.Context, records []dal.Record) error {
for _, record := range records {
if err := t.Set(ctx, record); err != nil {
return err
}
}
return nil
}