2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-09-05 17:10:35 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package testsuite
|
|
|
|
|
|
|
|
import (
|
2019-06-05 15:23:10 +01:00
|
|
|
"context"
|
2018-09-05 17:10:35 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2018-10-16 12:43:44 +01:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/testcontext"
|
2018-10-16 12:43:44 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-09-05 17:10:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func newItem(key, value string, isPrefix bool) storage.ListItem {
|
|
|
|
return storage.ListItem{
|
|
|
|
Key: storage.Key(key),
|
|
|
|
Value: storage.Value(value),
|
|
|
|
IsPrefix: isPrefix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 19:06:22 +01:00
|
|
|
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)
|
|
|
|
}
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 19:06:22 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-09-05 17:10:35 +01:00
|
|
|
type iterationTest struct {
|
|
|
|
Name string
|
|
|
|
Options storage.IterateOptions
|
|
|
|
Expected storage.Items
|
|
|
|
}
|
|
|
|
|
2019-09-24 19:06:22 +01:00
|
|
|
func testIterations(t *testing.T, ctx *testcontext.Context, store storage.KeyValueStore, tests []iterationTest) {
|
2018-09-05 17:10:35 +01:00
|
|
|
t.Helper()
|
|
|
|
for _, test := range tests {
|
2019-09-24 19:06:22 +01:00
|
|
|
items, err := iterateItems(ctx, store, test.Options, -1)
|
2018-09-05 17:10:35 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: %v", test.Name, err)
|
|
|
|
continue
|
|
|
|
}
|
2018-10-25 18:11:28 +01:00
|
|
|
if diff := cmp.Diff(test.Expected, items, cmpopts.EquateEmpty()); diff != "" {
|
2018-09-05 17:10:35 +01:00
|
|
|
t.Errorf("%s: (-want +got)\n%s", test.Name, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 19:06:22 +01:00
|
|
|
func isEmptyKVStore(tb testing.TB, ctx *testcontext.Context, store storage.KeyValueStore) bool {
|
2018-10-25 18:11:28 +01:00
|
|
|
tb.Helper()
|
2019-06-05 15:23:10 +01:00
|
|
|
keys, err := store.List(ctx, storage.Key(""), 1)
|
2018-10-25 18:11:28 +01:00
|
|
|
if err != nil {
|
|
|
|
tb.Fatalf("Failed to check if KeyValueStore is empty: %v", err)
|
|
|
|
}
|
|
|
|
return len(keys) == 0
|
|
|
|
}
|
|
|
|
|
2018-09-05 17:10:35 +01:00
|
|
|
type collector struct {
|
|
|
|
Items storage.Items
|
2018-10-25 18:11:28 +01:00
|
|
|
Limit int
|
2018-09-05 17:10:35 +01:00
|
|
|
}
|
|
|
|
|
2019-06-05 15:23:10 +01:00
|
|
|
func (collect *collector) include(ctx context.Context, it storage.Iterator) error {
|
2018-09-05 17:10:35 +01:00
|
|
|
var item storage.ListItem
|
2019-06-05 15:23:10 +01:00
|
|
|
for (collect.Limit < 0 || len(collect.Items) < collect.Limit) && it.Next(ctx, &item) {
|
2018-09-05 17:10:35 +01:00
|
|
|
collect.Items = append(collect.Items, storage.CloneItem(item))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-25 18:11:28 +01:00
|
|
|
|
2019-09-24 19:06:22 +01:00
|
|
|
func iterateItems(ctx *testcontext.Context, store storage.KeyValueStore, opts storage.IterateOptions, limit int) (storage.Items, error) {
|
2018-10-25 18:11:28 +01:00
|
|
|
collect := &collector{Limit: limit}
|
2019-06-05 15:23:10 +01:00
|
|
|
err := store.Iterate(ctx, opts, collect.include)
|
2018-10-25 18:11:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return collect.Items, nil
|
|
|
|
}
|