-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.go
166 lines (138 loc) · 3.1 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package bitcask
import (
"crypto/sha1"
"encoding/binary"
"hash/crc32"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"golang.org/x/sys/unix"
)
func PathExists(path string) bool {
_, err := os.Stat(path)
if err != nil && os.IsNotExist(err) {
return false
}
return true
}
func ComputeCRC32(data []byte) uint32 {
const castagnoliPoly = 0x82f63b78
table := crc32.MakeTable(castagnoliPoly)
checksum := crc32.Checksum(data, table)
return (checksum>>15 | checksum<<17) + 0xa282ead8
}
// pread does not modify the file pointer, so it has no effect on append write
func PreadFull(fd int, buf []byte, offset int64) error {
totalRead, expectRead := 0, len(buf)
for totalRead < expectRead {
// pread syscall try to read data with the specific buffer length
n, err := unix.Pread(fd, buf[totalRead:], offset+int64(totalRead))
if err != nil {
if err == io.EOF {
break
}
return err
}
totalRead += n
}
return nil
}
// return 0, 0 for all exceptions
func DecodeUvarint(data []byte) (uint64, int) {
v, size := binary.Uvarint(data)
if size <= 0 {
return 0, 0
}
return v, size
}
type Runners struct {
functors []func()
committed bool
}
func NewRunner() *Runners {
return &Runners{
committed: true,
}
}
func NewReverseRunner() *Runners {
return &Runners{
committed: false,
}
}
func (r *Runners) Post(f func()) {
r.functors = append(r.functors, f)
}
func (r *Runners) Do() {
if !r.committed {
return
}
for idx := range r.functors {
r.functors[idx]()
}
}
func (r *Runners) Rollback() {
r.committed = false
}
func (r *Runners) Commit() {
r.committed = true
}
func ParseFilename(name string) (fileType int, fid uint64, err error) {
ext := filepath.Ext(name)
switch ext {
case WalFileSuffix:
fid, err := strconv.Atoi(name[:len(name)-len(ext)])
return WalFileType, uint64(fid), err
case HintFileSuffix:
fid, err := strconv.Atoi(name[:len(name)-len(ext)])
return HintFileType, uint64(fid), err
case MergeFileSuffix:
fid, err := strconv.Atoi(name[:len(name)-len(ext)])
return MergeFileType, uint64(fid), err
case TmpFileSuffix:
fid, err := strconv.Atoi(name[:len(name)-len(ext)])
return TmpFileType, uint64(fid), err
}
if name == CurrentFile {
return CurrentFileType, 0, nil
}
if name == LockFile {
return LockFileType, 0, nil
}
if strings.HasPrefix(name, ManifestFilePrefix) {
fid, err := strconv.Atoi(name[len(ManifestFilePrefix)+1:])
return ManifestFileType, uint64(fid), err
}
return UnknownFileType, 0, nil
}
// namespace is fixed size
func MergedKey(ns, key []byte) []byte {
mergedKey := make([]byte, len(ns)+len(key))
copy(mergedKey, ns)
copy(mergedKey[len(ns):], key)
return mergedKey
}
func GenSha1NS(ns string) []byte {
hash := sha1.Sum([]byte(ns))
return hash[:]
}
func GenSha1Etag(data []byte) []byte {
hash := sha1.Sum(data)
return hash[:]
}
func Gen1KBytes() []byte {
buf := make([]byte, 1024)
for i := 0; i < 128; i++ {
copy(buf[i*8:], []byte("01234567"))
}
return buf
}
func GenNKBytes(n int) []byte {
bytes1KB := Gen1KBytes()
buf := make([]byte, 1024*n)
for i := 0; i < n; i++ {
copy(buf[i*1024:], bytes1KB)
}
return buf
}