-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
133 lines (106 loc) · 2.46 KB
/
parser.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
package genlog
import (
"bufio"
"bytes"
"io"
"regexp"
"strings"
)
// https://github.com/mysql/mysql-server/blob/5.6/sql/log.cc#L1897
var reMySQL56 = regexp.MustCompile(`(?s)^(\d{6}\s+\d{1,2}:\d{2}:\d{2}|\t)\s+(\d+)\s+([^\t]+)\t(.*)`)
// https://github.com/mysql/mysql-server/blob/5.7/sql/log.cc#L783
// NOTE: In Aurora MySQL 5.7, there may be no space between "Time" and "ID"
var reMySQL57 = regexp.MustCompile(`(?s)^([^\sZ]+Z)\s*(\d+)\s+([^\t]+)\t(.*)`)
// https://github.com/mysql/mysql-server/blob/5.6/sql/log.cc#L1676
// https://github.com/mysql/mysql-server/blob/5.7/sql/log.cc#L696
var reIgnore = regexp.MustCompile(
`^(?:` + strings.Join([]string{
`\S+, Version: \S+ (.+). started with:`,
`Tcp port: \d+ Unix socket: \S+`,
`Time Id Command Argument`,
}, "|") + `)$`)
type Block struct {
Time string
Id string
Command string
Argument string
}
func newBlock(tm, id, cmd, arg string) (*Block, *strings.Builder) {
block := &Block{
Time: tm,
Id: id,
Command: cmd,
}
argBldr := &strings.Builder{}
argBldr.WriteString(arg)
return block, argBldr
}
func callBack(block *Block, argBldr *strings.Builder, cb func(block *Block)) {
arg := strings.TrimRight(argBldr.String(), "\n")
block.Argument = arg
cb(block)
}
func Parse(file io.Reader, cb func(block *Block)) error {
reader := bufio.NewReader(file)
var block *Block
var argBldr *strings.Builder
var prevTm string
var re *regexp.Regexp
cpTm := false
for {
rawLine, err := readLine(reader)
if err == io.EOF {
break
} else if err != nil {
return err
}
line := string(rawLine)
if reIgnore.MatchString(line) {
continue
}
line += "\n"
if re == nil {
if reMySQL56.MatchString(line) {
re = reMySQL56
cpTm = true
} else if reMySQL57.MatchString(line) {
re = reMySQL57
} else {
continue
}
}
if m := re.FindStringSubmatch(line); m != nil {
tm := m[1]
if cpTm {
if tm == "\t" {
tm = prevTm
} else {
prevTm = tm
}
}
if block != nil {
callBack(block, argBldr, cb)
}
block, argBldr = newBlock(tm, m[2], m[3], m[4])
} else if block != nil {
argBldr.WriteString(line)
}
}
if block != nil {
callBack(block, argBldr, cb)
}
return nil
}
func readLine(r *bufio.Reader) ([]byte, error) {
var buf bytes.Buffer
for {
line, isPrefix, err := r.ReadLine()
n := len(line)
if n > 0 {
buf.Write(line)
}
if !isPrefix || err != nil {
return buf.Bytes(), err
}
}
}