-
Notifications
You must be signed in to change notification settings - Fork 402
/
utils.go
104 lines (89 loc) · 2.75 KB
/
utils.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package testsuite
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"storj.io/common/testcontext"
"storj.io/storj/storage"
)
func newItem(key, value string, isPrefix bool) storage.ListItem {
return storage.ListItem{
Key: storage.Key(key),
Value: storage.Value(value),
IsPrefix: isPrefix,
}
}
func cleanupItems(t testing.TB, ctx *testcontext.Context, store storage.KeyValueStore, items storage.Items) {
bulkDeleter, ok := store.(BulkDeleter)
if ok {
err := bulkDeleter.BulkDelete(ctx, items)
if err != nil {
t.Fatalf("could not do bulk cleanup of items: %v", err)
}
} else {
for _, item := range items {
_ = store.Delete(ctx, item.Key)
}
}
}
// BulkImporter identifies KV storage facilities that can do bulk importing of items more
// efficiently than inserting one-by-one.
type BulkImporter interface {
BulkImport(context.Context, storage.Iterator) error
}
// BulkDeleter identifies KV storage facilities that can delete multiple items efficiently.
type BulkDeleter interface {
BulkDelete(context.Context, storage.Items) error
}
// BulkCleaner identifies KV storage facilities that can delete all items efficiently.
type BulkCleaner interface {
BulkDeleteAll(ctx context.Context) error
}
type iterationTest struct {
Name string
Options storage.IterateOptions
Expected storage.Items
}
func testIterations(t *testing.T, ctx *testcontext.Context, store storage.KeyValueStore, tests []iterationTest) {
t.Helper()
for _, test := range tests {
items, err := iterateItems(ctx, store, test.Options, -1)
if err != nil {
t.Errorf("%s: %v", test.Name, err)
continue
}
if diff := cmp.Diff(test.Expected, items, cmpopts.EquateEmpty()); diff != "" {
t.Errorf("%s: (-want +got)\n%s", test.Name, diff)
}
}
}
func isEmptyKVStore(tb testing.TB, ctx *testcontext.Context, store storage.KeyValueStore) bool {
tb.Helper()
keys, err := store.List(ctx, storage.Key(""), 1)
if err != nil {
tb.Fatalf("Failed to check if KeyValueStore is empty: %v", err)
}
return len(keys) == 0
}
type collector struct {
Items storage.Items
Limit int
}
func (collect *collector) include(ctx context.Context, it storage.Iterator) error {
var item storage.ListItem
for (collect.Limit < 0 || len(collect.Items) < collect.Limit) && it.Next(ctx, &item) {
collect.Items = append(collect.Items, storage.CloneItem(item))
}
return nil
}
func iterateItems(ctx *testcontext.Context, store storage.KeyValueStore, opts storage.IterateOptions, limit int) (storage.Items, error) {
collect := &collector{Limit: limit}
err := store.Iterate(ctx, opts, collect.include)
if err != nil {
return nil, err
}
return collect.Items, nil
}