-
Notifications
You must be signed in to change notification settings - Fork 36
/
path_builder.go
189 lines (173 loc) · 5.02 KB
/
path_builder.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// Copyright (C) 2014 Moriyoshi Koizumi
// Copyright (C) 2014 Treasure Data, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
package fluentd_forwarder
import (
"errors"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
type JournalFileType rune
const (
Head = JournalFileType('b')
Rest = JournalFileType('q')
)
type JournalPathInfo struct {
Key string
Type JournalFileType
VariablePortion string
TSuffix string
Timestamp int64 // elapsed time in msec since epoch
UniqueId []byte
}
var NilJournalPathInfo = JournalPathInfo{"", 0, "", "", 0, nil}
var pathRegexp, _ = regexp.Compile(fmt.Sprintf("^(.*)[._](%c|%c)([0-9a-fA-F]{1,32})$", Head, Rest))
func encodeKey(key string) string {
keyLen := len(key)
retval := make([]byte, keyLen*2)
i := 0
for j := 0; j < keyLen; j += 1 {
c := key[j]
if c == '-' || c == '_' || c == '.' || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') {
cap_ := cap(retval)
if i >= cap_ {
newRetval := make([]byte, cap_+len(key))
copy(newRetval, retval)
retval = newRetval
}
retval[i] = c
i += 1
} else {
cap_ := cap(retval)
if i+3 > cap_ {
newSize := cap_
for {
newSize += len(key)
if i+3 <= newSize {
break
}
if newSize < cap_ {
// unlikely
panic("?")
}
}
newRetval := make([]byte, newSize)
copy(newRetval, retval)
retval = newRetval
}
retval[i] = '%'
retval[i+1] = "0123456789abcdef"[(c>>4)&15]
retval[i+2] = "0123456789abcdef"[c&15]
i += 3
}
}
return string(retval[0:i])
}
func decodeKey(encoded string) (string, error) {
return url.QueryUnescape(encoded)
}
func convertTSuffixToUniqueId(tSuffix string) ([]byte, error) {
tSuffixLen := len(tSuffix)
buf := make([]byte, tSuffixLen)
for i := 0; i < tSuffixLen; i += 2 {
ii, err := strconv.ParseUint(tSuffix[i:i+2], 16, 8)
if err != nil {
return nil, err
}
buf[i/2] = byte(ii)
buf[(i+tSuffixLen)/2] = byte(ii)
}
return buf, nil
}
func convertTSuffixToUnixNano(tSuffix string) (int64, error) {
t, err := strconv.ParseInt(tSuffix, 16, 64)
return t >> 12, err
}
func IsValidJournalPathInfo(info JournalPathInfo) bool {
return len(info.Key) > 0 && info.Type != 0
}
func BuildJournalPathWithTSuffix(key string, bq JournalFileType, tSuffix string) string {
encodedKey := encodeKey(key)
return fmt.Sprintf(
"%s.%c%s",
encodedKey,
rune(bq),
tSuffix,
)
}
func BuildJournalPath(key string, bq JournalFileType, time_ time.Time, randValue int64) JournalPathInfo {
timestamp := time_.UnixNano()
t := (timestamp/1000)<<12 | (randValue & 0xfff)
tSuffix := strconv.FormatInt(t, 16)
if pad := 16 - len(tSuffix); pad > 0 {
// unlikely
tSuffix = strings.Repeat("0", pad) + tSuffix
}
uniqueId, err := convertTSuffixToUniqueId(tSuffix)
if err != nil {
panic("WTF? " + err.Error())
} // should never happen
return JournalPathInfo{
Key: key,
Type: bq,
VariablePortion: BuildJournalPathWithTSuffix(key, bq, tSuffix),
TSuffix: tSuffix,
Timestamp: timestamp,
UniqueId: uniqueId,
}
}
func firstRune(s string) rune {
for _, r := range s {
return r
}
return -1 // in case the string is empty
}
func DecodeJournalPath(variablePortion string) (JournalPathInfo, error) {
m := pathRegexp.FindStringSubmatch(variablePortion)
if m == nil {
return NilJournalPathInfo, errors.New("malformed path string")
}
key, err := decodeKey(m[1])
if err != nil {
return NilJournalPathInfo, errors.New("malformed path string")
}
uniqueId, err := convertTSuffixToUniqueId(m[3])
if err != nil {
return NilJournalPathInfo, errors.New("malformed path string")
}
timestamp, err := convertTSuffixToUnixNano(m[3])
if err != nil {
return NilJournalPathInfo, errors.New("malformed path string")
}
return JournalPathInfo{
Key: key,
Type: JournalFileType(firstRune(m[2])),
VariablePortion: variablePortion,
TSuffix: m[3],
Timestamp: timestamp,
UniqueId: uniqueId,
}, nil
}