/
utils.go
60 lines (49 loc) · 1.51 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
package keeper
import (
"time"
"github.com/osmosis-labs/osmosis/v13/x/incentives/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// findIndex takes an array of IDs. Then return the index of a specific ID.
func findIndex(IDs []uint64, ID uint64) int {
for index, id := range IDs {
if id == ID {
return index
}
}
return -1
}
// removeValue takes an array of IDs. Then finds the index of the IDs and remove those IDs from the array.
func removeValue(IDs []uint64, ID uint64) ([]uint64, int) {
index := findIndex(IDs, ID)
if index < 0 {
return IDs, index
}
IDs[index] = IDs[len(IDs)-1] // set last element to index
return IDs[:len(IDs)-1], index
}
// getTimeKey returns the time key used when getting a set of gauges.
func getTimeKey(timestamp time.Time) []byte {
timeBz := sdk.FormatTimeBytes(timestamp)
timeBzL := len(timeBz)
prefixL := len(types.KeyPrefixTimestamp)
bz := make([]byte, prefixL+8+timeBzL)
// copy the prefix
copy(bz[:prefixL], types.KeyPrefixTimestamp)
// copy the encoded time bytes length
copy(bz[prefixL:prefixL+8], sdk.Uint64ToBigEndian(uint64(timeBzL)))
// copy the encoded time bytes
copy(bz[prefixL+8:prefixL+8+timeBzL], timeBz)
return bz
}
// combineKeys combines the byte arrays of multiple keys into a single byte array.
func combineKeys(keys ...[]byte) []byte {
combined := []byte{}
for i, key := range keys {
combined = append(combined, key...)
if i < len(keys)-1 { // not last item
combined = append(combined, types.KeyIndexSeparator...)
}
}
return combined
}