-
Notifications
You must be signed in to change notification settings - Fork 402
/
util.go
58 lines (48 loc) · 1.28 KB
/
util.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package storage
import (
"context"
"fmt"
)
// NextKey returns the successive key.
func NextKey(key Key) Key {
return append(CloneKey(key), 0)
}
// AfterPrefix returns the key after prefix.
func AfterPrefix(key Key) Key {
after := CloneKey(key)
after[len(after)-1]++
return after
}
// CloneKey creates a copy of key.
func CloneKey(key Key) Key { return append(Key{}, key...) }
// CloneValue creates a copy of value.
func CloneValue(value Value) Value { return append(Value{}, value...) }
// CloneItem creates a deep copy of item.
func CloneItem(item ListItem) ListItem {
return ListItem{
Key: CloneKey(item.Key),
Value: CloneValue(item.Value),
IsPrefix: item.IsPrefix,
}
}
// CloneItems creates a deep copy of items.
func CloneItems(items Items) Items {
var result = make(Items, len(items))
for i, item := range items {
result[i] = CloneItem(item)
}
return result
}
// PutAll adds multiple values to the store.
func PutAll(ctx context.Context, store KeyValueStore, items ...ListItem) (err error) {
defer mon.Task()(&ctx)(&err)
for _, item := range items {
err := store.Put(ctx, item.Key, item.Value)
if err != nil {
return fmt.Errorf("failed to put %v: %w", item, err)
}
}
return nil
}