-
Notifications
You must be signed in to change notification settings - Fork 6
/
keyValStorage.go
50 lines (40 loc) · 1.09 KB
/
keyValStorage.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
package keyValStorage
import (
"bytes"
"github.com/ElrondNetwork/elrond-go-core/core"
)
// KeyValStorage holds a key and an associated value
type keyValStorage struct {
key []byte
value []byte
}
// NewKeyValStorage creates a new key-value storage
func NewKeyValStorage(key []byte, val []byte) *keyValStorage {
return &keyValStorage{
key: key,
value: val,
}
}
// Key returns the key in the key-value storage
func (k *keyValStorage) Key() []byte {
return k.key
}
// Value returns the value in the key-value storage
func (k *keyValStorage) Value() []byte {
return k.value
}
// ValueWithoutSuffix will try to trim the suffix from the current data field
func (k *keyValStorage) ValueWithoutSuffix(suffix []byte) ([]byte, error) {
if len(suffix) == 0 {
return k.value, nil
}
lenValue := len(k.value)
lenSuffix := len(suffix)
position := bytes.Index(k.value, suffix)
if position != lenValue-lenSuffix || position < 0 {
return nil, core.ErrSuffixNotPresentOrInIncorrectPosition
}
newData := make([]byte, lenValue-lenSuffix)
copy(newData, k.value[:position])
return newData, nil
}