forked from oh-my-tidb/mok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (57 loc) · 1.57 KB
/
main.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
package main
import (
"encoding/hex"
"flag"
"fmt"
"os"
"strconv"
"strings"
"github.com/pingcap/tidb/util/codec"
)
var keyFormat = flag.String("format", "proto", "output format (go/hex/base64/proto)")
var tableID = flag.Int64("table-id", 0, "table ID")
var indexID = flag.Int64("index-id", 0, "index ID")
var rowValue = flag.String("row-value", "", "row value")
var indexValue = flag.String("index-value", "", "index value")
func main() {
flag.Parse()
if flag.NArg() == 1 { // Decode the given key.
n := N("key", []byte(flag.Arg(0)))
n.Expand().Print()
} else if flag.NArg() == 0 { // Build a key with given flags.
key := []byte{'t'}
key = codec.EncodeInt(key, *tableID)
if *tableID == 0 {
fmt.Println("table ID shouldn't be 0")
os.Exit(1)
}
if *indexID == 0 {
if *rowValue != "" {
key = append(key, []byte("_r")...)
rowValueInt, err := strconv.ParseInt(*rowValue, 10, 64)
if err != nil {
fmt.Printf("invalid row value: %s\n", *rowValue)
os.Exit(1)
}
key = codec.EncodeInt(key, rowValueInt)
}
} else {
key = append(key, []byte("_i")...)
key = codec.EncodeInt(key, *indexID)
if *indexValue != "" {
indexValueInt, err := strconv.ParseInt(*indexValue, 10, 64)
if err != nil {
fmt.Printf("invalid index value: %s\n", *indexValue)
os.Exit(1)
key = codec.EncodeInt(key, indexValueInt)
}
}
}
key = codec.EncodeBytes([]byte{}, key)
fmt.Printf("built key: %s\n", strings.ToUpper(hex.EncodeToString(key)))
} else {
fmt.Println("usage:\nmok {flags} [key]")
flag.PrintDefaults()
os.Exit(1)
}
}