forked from oh-my-tidb/mok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.go
204 lines (188 loc) · 3.96 KB
/
rules.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"encoding/base64"
"encoding/hex"
"net/url"
"strconv"
"strings"
"github.com/pingcap/tidb/util/codec"
)
type Rule func(*Node) *Variant
var rules []Rule
func init() {
rules = []Rule{
DecodeHex,
DecodeComparableKey,
DecodeRocksDBKey,
DecodeTablePrefix,
DecodeTableRow,
DecodeTableIndex,
DecodeIndexValues,
DecodeLiteral,
DecodeBase64,
DecodeIntegerBytes,
DecodeURLEscaped,
}
}
func DecodeHex(n *Node) *Variant {
if n.typ != "key" {
return nil
}
decoded, err := hex.DecodeString(string(n.val))
if err != nil {
return nil
}
return &Variant{
method: "decode hex key",
children: []*Node{N("key", decoded)},
}
}
func DecodeComparableKey(n *Node) *Variant {
if n.typ != "key" {
return nil
}
b, decoded, err := codec.DecodeBytes(n.val, nil)
if err != nil {
return nil
}
children := []*Node{N("key", decoded)}
switch len(b) {
case 0:
case 8:
children = append(children, N("ts", b))
default:
return nil
}
return &Variant{
method: "decode mvcc key",
children: children,
}
}
func DecodeRocksDBKey(n *Node) *Variant {
if n.typ != "key" {
return nil
}
if len(n.val) > 0 && n.val[0] == 'z' {
return &Variant{
method: "decode rocksdb data key",
children: []*Node{N("key", n.val[1:])},
}
}
return nil
}
func DecodeTablePrefix(n *Node) *Variant {
if n.typ == "key" && len(n.val) >= 9 && n.val[0] == 't' {
return &Variant{
method: "table prefix",
children: []*Node{N("table_id", n.val[1:])},
}
}
return nil
}
func DecodeTableRow(n *Node) *Variant {
if n.typ == "key" && len(n.val) >= 19 && n.val[0] == 't' && n.val[9] == '_' && n.val[10] == 'r' {
handleTyp := "index_values"
if remain, _, err := codec.DecodeInt(n.val[11:]); err == nil && len(remain) == 0 {
handleTyp = "row_id"
}
return &Variant{
method: "table row key",
children: []*Node{N("table_id", n.val[1:9]), N(handleTyp, n.val[11:])},
}
}
return nil
}
func DecodeTableIndex(n *Node) *Variant {
if n.typ == "key" && len(n.val) >= 19 && n.val[0] == 't' && n.val[9] == '_' && n.val[10] == 'i' {
return &Variant{
method: "table index key",
children: []*Node{N("table_id", n.val[1:9]), N("index_id", n.val[11:19]), N("index_values", n.val[19:])},
}
}
return nil
}
func DecodeIndexValues(n *Node) *Variant {
if n.typ != "index_values" {
return nil
}
var children []*Node
for key := n.val; len(key) > 0; {
remain, _, e := codec.DecodeOne(key)
if e != nil {
children = append(children, N("key", key))
break
} else {
children = append(children, N("index_value", key[:len(key)-len(remain)]))
}
key = remain
}
return &Variant{
method: "decode index values",
children: children,
}
}
func DecodeLiteral(n *Node) *Variant {
if n.typ != "key" {
return nil
}
s, err := decodeKey(string(n.val))
if err != nil {
return nil
}
if s == string(n.val) {
return nil
}
return &Variant{
method: "decode go literal key",
children: []*Node{N("key", []byte(s))},
}
}
func DecodeBase64(n *Node) *Variant {
if n.typ != "key" {
return nil
}
s, err := base64.StdEncoding.DecodeString(string(n.val))
if err != nil {
return nil
}
child := N("key", []byte(s))
child.Expand()
if len(child.variants) == 0 {
return nil
}
return &Variant{
method: "decode base64 key",
children: []*Node{N("key", []byte(s))},
}
}
func DecodeIntegerBytes(n *Node) *Variant {
if n.typ != "key" {
return nil
}
fields := strings.Fields(strings.ReplaceAll(strings.Trim(string(n.val), "[]"), ",", ""))
var b []byte
for _, f := range fields {
c, err := strconv.ParseInt(f, 10, 9)
if err != nil {
return nil
}
b = append(b, byte(c))
}
return &Variant{
method: "decode integer bytes",
children: []*Node{N("key", b)},
}
}
func DecodeURLEscaped(n *Node) *Variant {
if n.typ != "key" {
return nil
}
s, err := url.PathUnescape(string(n.val))
if err != nil || s == string(n.val) {
return nil
}
return &Variant{
method: "decode url encoded",
children: []*Node{N("key", []byte(s))},
}
}