-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_hash.go
49 lines (42 loc) · 941 Bytes
/
encode_hash.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
package main
import "github.com/gushitong/rekkles/ut"
type HashEncoder struct {
key []byte
}
func (h HashEncoder) Encode(field []byte) []byte {
return ut.ConcatBytearray(
[]byte{(byte)(SymbolHash)},
[]byte{uint8(len(h.key))},
h.key,
field,
)
}
func (h HashEncoder) Prefix() []byte {
return ut.ConcatBytearray(
[]byte{(byte)(SymbolHash)},
[]byte{uint8(len(h.key))},
h.key,
)
}
func (h HashEncoder) QueueKey() []byte {
return ut.ConcatBytearray(
[]byte{(byte)(SymbolQueue)},
h.key,
)
}
func (h HashEncoder) Decode(hashKey []byte) ([]byte, error) {
if hashKey[0] != (byte)(SymbolHash) {
return nil, ErrCorruptedHashKey
}
lenKey := int(hashKey[1])
if len(hashKey) <= lenKey+2 {
return nil, ErrCorruptedHashKey
}
return hashKey[lenKey+2:], nil
}
func NewHashEncoder(key []byte) (*HashEncoder, error) {
if len(key) > MaxKeySize {
return nil, ErrKeySizeExceeded
}
return &HashEncoder{key: key}, nil
}