-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_helpers.go
69 lines (59 loc) · 1.27 KB
/
test_helpers.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
package badgerutils
import (
"fmt"
"strings"
"github.com/dgraph-io/badger"
)
type sampleRecord struct {
Key string
Value string
}
func csvToKeyValue(line string) (*KeyValue, error) {
kv := strings.Split(line, ":")
if len(kv) < 2 {
return nil, fmt.Errorf("%v has less than 2 kv", line)
}
return &KeyValue{
Key: []byte(kv[0]),
Value: []byte(kv[1]),
}, nil
}
func readDB(dir string) ([]sampleRecord, error) {
db, err := openDB(dir)
if err != nil {
return nil, err
}
defer db.Close()
chkv, cherr := make(chan KeyValue), make(chan error)
go func(chan KeyValue, chan error) {
err := db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
key := item.Key()
value, err := item.Value()
if err != nil {
return err
}
kv := KeyValue{Key: key, Value: value}
chkv <- kv
}
close(chkv)
return nil
})
cherr <- err
}(chkv, cherr)
sampleRecords := make([]sampleRecord, 0)
for kv := range chkv {
sampleRecords = append(sampleRecords, sampleRecord{
Key: string(kv.Key),
Value: string(kv.Value),
})
}
if err := <-cherr; err != nil {
return nil, err
}
return sampleRecords, nil
}