-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.go
171 lines (145 loc) · 3.93 KB
/
encode.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
167
168
169
170
171
package tdb
import (
"encoding/binary"
"errors"
"fmt"
"math"
"path/filepath"
"strconv"
"strings"
"time"
)
type fileEncode uint32
func encodeFileFromUnix(unixtime uint32) fileEncode {
t := time.Unix(int64(unixtime), 0)
year, month, day := t.Date()
hour := t.Hour()
return encodeFile(year, int(month), day, hour)
}
func encodeFile(year, month, day, hour int) fileEncode {
var encoded uint32
encoded += uint32(year) * 1e5
encoded += uint32(month) * 1e3
encoded += uint32(day) * 10
if hour >= 12 {
encoded += 5
}
return fileEncode(encoded)
}
func encodeFromPath(folder, filename string) (fileEncode, error) {
var encoded fileEncode
dir := filepath.Base(folder)
if len(dir) != 6 {
return encoded, errors.New("folder is wrong to encode")
}
fName := getFileName(filename)
if len(fName) != 3 {
return encoded, errors.New("filename is wrong to encode")
}
dirV, err := strconv.Atoi(dir)
if err != nil {
return encoded, err
}
fNameV, err := strconv.Atoi(fName)
if err != nil {
return encoded, err
}
return fileEncode(dirV*1e3 + fNameV), nil
}
func (f fileEncode) year() int {
return int(uint32(f) / 1e5)
}
func (f fileEncode) month() int {
return int(uint32(f)/1e3) % 100
}
func (f fileEncode) day() int {
return int(uint32(f)/10) % 100
}
func (f fileEncode) isAM() bool {
return f%10 == 0
}
// get subFolder folder and file name
func (f fileEncode) path() (string, string) {
folder := strconv.Itoa(int(uint32(f) / 1e3))
fileName := strconv.Itoa(int(uint32(f) % 1e3))
if len(fileName) == 2 {
fileName = strings.Join([]string{"0", fileName}, "")
}
return folder, fileName
}
// get original unix time
func (f fileEncode) origin() uint32 {
fileOrigin := time.Date(f.year(), time.Month(f.month()), f.day(), 0, 0, 0, 0, time.Local).Unix()
if !f.isAM() {
fileOrigin = fileOrigin + 43200
}
return uint32(fileOrigin)
}
// implement the sort interface
type fileEncodeSlice []fileEncode
func (s fileEncodeSlice) Len() int { return len(s) }
func (s fileEncodeSlice) Less(i, j int) bool { return s[i] < s[j] }
func (s fileEncodeSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// fileRange used for query
type fileRange struct {
start fileEncode
end fileEncode
}
// create a new fileRange instance
// startUnix == 0 means from very beginning; endUnix == 0 means to very end.
func newFileRange(startUnix uint32, endUnix uint32) (*fileRange, error) {
if startUnix != 0 && endUnix != 0 && startUnix > endUnix {
return nil, ErrRange
}
one := &fileRange{
start: encodeFileFromUnix(startUnix),
end: encodeFileFromUnix(endUnix),
}
// set to 0 if startUnix == 0
if startUnix == 0 {
one.start = fileEncode(0)
}
// set to max uint32 if endUnix == 0
if endUnix == 0 {
one.end = fileEncode(math.MaxUint32)
}
return one, nil
}
// folder should be the year-month folder like "201703"
func (f *fileRange) folderInRange(folder string) (bool, error) {
v, err := strconv.Atoi(folder)
if err != nil {
return false, err
}
t := uint32(v)
if t >= uint32(f.start/1000) && t <= uint32(f.end/1000) {
return true, nil
}
return false, nil
}
// file should be the file encoded format like "201702135"
func (f *fileRange) fileInRange(file fileEncode) (bool, error) {
t := uint32(file)
if t >= uint32(f.start) && t <= uint32(f.end) {
return true, nil
}
return false, nil
}
func encodeAliasAndFile(folder string, file fileEncode) string {
return fmt.Sprintf("%s%d", filepath.Base(folder), file)
}
func encodeAction(start, last uint32) []byte {
// bytes value: first 4 bytes is uint32 for last active, last 4 bytes for uint32 start.
bs := make([]byte, 8)
// encode to bs with start timestamp
binary.LittleEndian.PutUint32(bs[4:], start)
// encode to bs with last active timestamp
binary.LittleEndian.PutUint32(bs[0:], last)
return bs
}
func decodeAction(v []byte) (uint32, uint32, error) {
if len(v) != 8 {
return 0, 0, ErrActionValue
}
return binary.LittleEndian.Uint32(v[4:]), binary.LittleEndian.Uint32(v), nil
}