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 storage
|
|
|
|
|
2019-06-05 15:23:10 +01:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
)
|
2018-09-07 15:20:15 +01:00
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// CloneKey creates a copy of key.
|
2018-09-05 17:10:35 +01:00
|
|
|
func CloneKey(key Key) Key { return append(Key{}, key...) }
|
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// CloneValue creates a copy of value.
|
2018-09-05 17:10:35 +01:00
|
|
|
func CloneValue(value Value) Value { return append(Value{}, value...) }
|
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// CloneItem creates a deep copy of item.
|
2018-09-05 17:10:35 +01:00
|
|
|
func CloneItem(item ListItem) ListItem {
|
|
|
|
return ListItem{
|
|
|
|
Key: CloneKey(item.Key),
|
|
|
|
Value: CloneValue(item.Value),
|
|
|
|
IsPrefix: item.IsPrefix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// CloneItems creates a deep copy of items.
|
2018-09-05 17:10:35 +01:00
|
|
|
func CloneItems(items Items) Items {
|
|
|
|
var result = make(Items, len(items))
|
|
|
|
for i, item := range items {
|
|
|
|
result[i] = CloneItem(item)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2018-09-07 15:20:15 +01:00
|
|
|
|
2020-02-17 07:56:13 +00:00
|
|
|
// PutAll adds multiple values to the store.
|
2019-06-05 15:23:10 +01:00
|
|
|
func PutAll(ctx context.Context, store KeyValueStore, items ...ListItem) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
for _, item := range items {
|
2019-06-05 15:23:10 +01:00
|
|
|
err := store.Put(ctx, item.Key, item.Value)
|
2018-09-07 15:20:15 +01:00
|
|
|
if err != nil {
|
2021-05-14 16:05:42 +01:00
|
|
|
return fmt.Errorf("failed to put %v: %w", item, err)
|
2018-09-07 15:20:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|