This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.go
84 lines (75 loc) · 1.47 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
81
82
83
84
package db
import (
"bytes"
"os"
)
func cp(bz []byte) (ret []byte) {
ret = make([]byte, len(bz))
copy(ret, bz)
return ret
}
func concat(bz1 []byte, bz2 []byte) (ret []byte) {
bz1len := len(bz1)
if bz1len == 0 {
return cp(bz2)
}
bz2len := len(bz2)
if bz2len == 0 {
return cp(bz1)
}
ret = make([]byte, bz1len+bz2len)
copy(ret, bz1)
copy(ret[bz1len:], bz2)
return ret
}
// Returns a slice of the same length (big endian)
// except incremented by one.
// Returns nil on overflow (e.g. if bz bytes are all 0xFF)
// CONTRACT: len(bz) > 0
func cpIncr(bz []byte) (ret []byte) {
if len(bz) == 0 {
panic("cpIncr expects non-zero bz length")
}
ret = cp(bz)
for i := len(bz) - 1; i >= 0; i-- {
if ret[i] < byte(0xFF) {
ret[i]++
return
}
ret[i] = byte(0x00)
if i == 0 {
// Overflow
return nil
}
}
return nil
}
// See DB interface documentation for more information.
func IsKeyInDomain(key, start, end []byte) bool {
if bytes.Compare(key, start) < 0 {
return false
}
if end != nil && bytes.Compare(end, key) <= 0 {
return false
}
return true
}
func FileExists(filePath string) bool {
_, err := os.Stat(filePath)
return !os.IsNotExist(err)
}
func PrefixToRange(prefix []byte) (start, end []byte, err error) {
if len(prefix) == 0 {
return nil, nil, errKeyEmpty
}
start = cp(prefix)
end = cpIncr(prefix)
return start, end, nil
}
// Path
func makePath(path string) error {
if len(path) == 0 {
return nil
}
return os.MkdirAll(path, 0755)
}