@@ -24,33 +24,54 @@ import (
24
24
"sync"
25
25
)
26
26
27
- type MySQLLogLoader struct {}
27
+ type (
28
+ MySQLLogLoader struct {}
29
+
30
+ mysqlLogReaderState struct {
31
+ prevQuery string
32
+ lineNumber int
33
+ queryStart int
34
+ scanner * bufio.Scanner
35
+ err error
36
+ closed bool
37
+ mu sync.Mutex
38
+ reg * regexp.Regexp
39
+ fd * os.File
40
+ }
41
+
42
+ statefulLoader interface {
43
+ next () (Query , bool )
44
+ close () error
45
+ }
46
+
47
+ errLoader struct {
48
+ err error
49
+ }
50
+ )
51
+
52
+ var _ statefulLoader = (* errLoader )(nil )
53
+
54
+ func (e * errLoader ) close () error {
55
+ return e .err
56
+ }
57
+
58
+ func (e * errLoader ) next () (Query , bool ) {
59
+ return Query {}, false
60
+ }
28
61
29
62
func (MySQLLogLoader ) Load (fileName string ) ([]Query , error ) {
30
- next , stop := MySQLLogLoader {}.Loadit (fileName )
63
+ loader := MySQLLogLoader {}.Loadit (fileName )
31
64
32
65
var queries []Query
33
66
for {
34
- query , ok := next ()
67
+ query , ok := loader . next ()
35
68
if ! ok {
36
69
break
37
70
}
38
71
queries = append (queries , query )
39
72
}
40
73
41
- return queries , stop ()
42
- }
43
-
44
- type mysqlLogReaderState struct {
45
- prevQuery string
46
- lineNumber int
47
- queryStart int
48
- scanner * bufio.Scanner
49
- err error
50
- closed bool
51
- mu sync.Mutex
52
- reg * regexp.Regexp
53
- fd * os.File
74
+ return queries , loader .close ()
54
75
}
55
76
56
77
func (s * mysqlLogReaderState ) next () (Query , bool ) {
@@ -132,29 +153,19 @@ func (s *mysqlLogReaderState) close() error {
132
153
return s .err
133
154
}
134
155
135
- func (MySQLLogLoader ) Loadit (fileName string ) ( next func () ( Query , bool ), stop func () error ) {
156
+ func (MySQLLogLoader ) Loadit (fileName string ) statefulLoader {
136
157
reg := regexp .MustCompile (`^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z)\s+(\d+)\s+(\w+)\s+(.*)` )
137
158
138
159
fd , err := os .OpenFile (fileName , os .O_RDONLY , 0 )
139
160
if err != nil {
140
- return errFail ( err )
161
+ return & errLoader { err }
141
162
}
142
163
143
164
scanner := bufio .NewScanner (fd )
144
165
145
- s := & mysqlLogReaderState {
166
+ return & mysqlLogReaderState {
146
167
scanner : scanner ,
147
168
reg : reg ,
148
169
fd : fd ,
149
170
}
150
-
151
- return s .next , s .close
152
- }
153
-
154
- func errFail (err error ) (next func () (Query , bool ), stop func () error ) {
155
- return func () (Query , bool ) {
156
- return Query {}, false
157
- }, func () error {
158
- return err
159
- }
160
171
}
0 commit comments