-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
80 lines (68 loc) · 1.36 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package bitcask
import (
"bytes"
"encoding/binary"
"errors"
"math"
)
var ErrInvalidKeyFormat = errors.New("invalid key format includes +[],")
// Raw key:
// +key,type = value
// +name,s = "latermoon"
var (
SEP = []byte{','}
KEY = []byte{'+'} // Key Prefix
SOK = []byte{'['} // Start of Key
EOK = []byte{']'} // End of Key
)
const (
MINBYTE byte = 0
MAXBYTE byte = math.MaxUint8
)
type ElemType byte
const (
STRING ElemType = 's'
HASH ElemType = 'h'
LIST ElemType = 'l'
SORTEDSET ElemType = 'z'
NONE ElemType = '0'
)
func (e ElemType) String() string {
switch byte(e) {
case 's':
return "string"
case 'h':
return "hash"
case 'l':
return "list"
case 'z':
return "sortedset"
default:
return "none"
}
}
func rawKey(key []byte, t ElemType) []byte {
return bytes.Join([][]byte{KEY, key, SEP, []byte{byte(t)}}, nil)
}
func verifyKey(key []byte) error {
err := ErrInvalidKeyFormat
if bytes.Contains(key, SEP) {
return err
} else if bytes.Contains(key, KEY) {
return err
} else if bytes.Contains(key, SOK) {
return err
} else if bytes.Contains(key, EOK) {
return err
}
return nil
}
// itob returns an 8-byte big endian representation of v.
func itob(i int64) []byte {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, uint64(i))
return b
}
func btoi(b []byte) int64 {
return int64(binary.LittleEndian.Uint64(b))
}