forked from 2892931976/binlog_inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollback_process.go
360 lines (304 loc) · 8.71 KB
/
rollback_process.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package main
import (
"io"
"os"
"strings"
"sync"
)
func ReverseFileGo(rollbackFileChan chan map[string]string, bytesCntFiles map[string][][]int, keepTrx bool, wg *sync.WaitGroup) {
defer wg.Done()
for arr := range rollbackFileChan {
//ReverseFileToNewFile(arr["tmp"], arr["rollback"], batchLines)
//ReverseFileToNewFileOneByOneLineAndKeepTrx(arr["tmp"], arr["rollback"])
ReverseFileToNewFileOneByOneLineAndKeepTrxBatchRead(arr["tmp"], arr["rollback"], bytesCntFiles[arr["tmp"]], keepTrx)
err := os.Remove(arr["tmp"])
if err != nil {
CheckErr(err, "fail to remove file "+arr["tmp"], ERR_FILE_REMOVE, false)
}
}
}
func ReverseFileToNewFileOneByOneLineAndKeepTrxBatchRead(srcFile string, destFile string, trxPoses [][]int, keepTrx bool) error {
srcFH, err := os.Open(srcFile)
if err != nil {
CheckErr(err, "fail to open file "+srcFile, ERR_FILE_OPEN, false)
return err
}
defer srcFH.Close()
destFH, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
CheckErr(err, "fail to open file "+destFile, ERR_FILE_OPEN, false)
return err
}
defer destFH.Close()
srcInfo, err := srcFH.Stat()
if err != nil {
CheckErr(err, "fail to stat file "+srcFile, ERR_FILE_READ, false)
return err
}
srcSize := srcInfo.Size() //int64
var readByteCntTotal int64 = 0
var bufStr string
var LineSep string = "\n"
//var ifCommit bool = true
_, err = srcFH.Seek(0, os.SEEK_END)
if err != nil {
CheckErr(err, "fail to seek file "+srcFile, ERR_FILE_SEEK, false)
return err
}
var lastTrxIdx int = 0
for batchIdx := len(trxPoses) - 1; batchIdx >= 0; batchIdx-- {
startPos, err := srcFH.Seek(-int64(trxPoses[batchIdx][0]), os.SEEK_CUR)
if err != nil {
CheckErr(err, "fail to seek file "+srcFile, ERR_FILE_SEEK, false)
return err
}
var buf []byte = make([]byte, trxPoses[batchIdx][0])
//_, err = srcFH.Read(buf)
_, err = io.ReadFull(srcFH, buf)
if err != nil {
CheckErr(err, "fail to read file "+srcFile, ERR_FILE_READ, false)
return err
}
readByteCntTotal += int64(trxPoses[batchIdx][0])
bufStr = string(buf)
strArr := strings.Split(bufStr, LineSep)
var strArrStrs []string = make([]string, len(strArr))
for ji, ai := 0, len(strArr)-1; ai >= 0; ai-- {
if strArr[ai] == "" {
continue
}
/*
if trxPoses[batchIdx][1] == 1 {
if strArr[ai] == "commit" {
ifCommit = true
if batchIdx == 0 && ai == 0 {
strArrStrs[ji] = "" // "commit" is written as the first line in the tmp file, so we skip it
} else {
strArrStrs[ji] = "begin"
}
} else if strArr[ai] == "rollback" {
ifCommit = false
strArrStrs[ji] = "begin"
} else if strArr[ai] == "begin" {
if ifCommit {
strArrStrs[ji] = "commit"
} else {
strArrStrs[ji] = "rollback"
}
ifCommit = true // default is commit
}
} else {
strArrStrs[ji] = strArr[ai]
}
*/
strArrStrs[ji] = strArr[ai]
ji++
}
if keepTrx && lastTrxIdx != trxPoses[batchIdx][1] {
destFH.WriteString("commit\nbegin\n")
}
lastTrxIdx = trxPoses[batchIdx][1]
_, err = destFH.WriteString(strings.Join(strArrStrs, LineSep))
if err != nil {
CheckErr(err, "fail to write file "+destFile, ERR_FILE_WRITE, false)
return err
}
if readByteCntTotal == srcSize || startPos == 0 {
break // finishing reading
}
if batchIdx > 0 {
_, err := srcFH.Seek(-int64(trxPoses[batchIdx][0]), os.SEEK_CUR)
if err != nil {
CheckErr(err, "fail to seek file "+srcFile, ERR_FILE_SEEK, false)
return err
}
}
}
if keepTrx {
destFH.WriteString("commit\n")
}
return nil
}
func ReverseFileToNewFileOneByOneLineAndKeepTrx(srcFile string, destFile string) error {
srcFH, err := os.Open(srcFile)
if err != nil {
CheckErr(err, "fail to open file "+srcFile, ERR_FILE_OPEN, false)
return err
}
defer srcFH.Close()
destFH, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
CheckErr(err, "fail to open file "+destFile, ERR_FILE_OPEN, false)
return err
}
defer destFH.Close()
srcInfo, err := srcFH.Stat()
if err != nil {
CheckErr(err, "fail to stat file "+srcFile, ERR_FILE_READ, false)
return err
}
srcSize := srcInfo.Size() //int64
var offset int64 = -1
var readByteCntTotal int64 = 0
var buf []byte = make([]byte, 1)
var bufStr string
var LineSep string = "\n"
var printLineStr string
var byteCntBeforePrint int64 = 0
var ifCommit bool = true
_, err = srcFH.Seek(0, os.SEEK_END)
if err != nil {
CheckErr(err, "fail to seek file "+srcFile, ERR_FILE_SEEK, false)
return err
}
for {
startPos, err := srcFH.Seek(offset, os.SEEK_CUR)
if err != nil {
CheckErr(err, "fail to seek file "+srcFile, ERR_FILE_SEEK, false)
return err
}
_, err = srcFH.Read(buf)
if err != nil {
CheckErr(err, "fail to read file "+srcFile, ERR_FILE_READ, false)
return err
}
readByteCntTotal++
byteCntBeforePrint++
bufStr = string(buf)
if bufStr == LineSep && readByteCntTotal == 1 {
_, err := srcFH.Seek(offset, os.SEEK_CUR)
if err != nil {
CheckErr(err, "fail to seek file "+srcFile, ERR_FILE_SEEK, false)
return err
}
continue // if the end of file is EOL, skip it
}
// read a line or we alread finish reading the whole file
if bufStr == LineSep || readByteCntTotal == srcSize || startPos == 0 {
printLine := make([]byte, byteCntBeforePrint-1)
_, err := io.ReadFull(srcFH, printLine)
if err != nil {
CheckErr(err, "fail to read file "+srcFile, ERR_FILE_READ, false)
return err
}
printLineStr = string(printLine)
if printLineStr == "commit\n" {
ifCommit = true
printLineStr = "begin\n"
} else if printLineStr == "rollback\n" {
ifCommit = false
printLineStr = "begin\n"
} else if printLineStr == "begin\n" {
if ifCommit {
printLineStr = "commit\n"
} else {
printLineStr = "rollback\n"
}
ifCommit = true // default is commit
}
_, err = destFH.WriteString(printLineStr)
if err != nil {
CheckErr(err, "fail to write file "+destFile, ERR_FILE_WRITE, false)
return err
}
if readByteCntTotal == srcSize || startPos == 0 {
break
}
_, err = srcFH.Seek(-byteCntBeforePrint, os.SEEK_CUR)
if err != nil {
CheckErr(err, "fail to seek file "+srcFile, ERR_FILE_SEEK, false)
return err
}
byteCntBeforePrint = 1
} else {
_, err := srcFH.Seek(offset, os.SEEK_CUR)
if err != nil {
CheckErr(err, "fail to seek file "+srcFile, ERR_FILE_SEEK, false)
return err
}
}
}
return nil
}
func ReverseFileToNewFile(srcFile string, destFile string, batchLines int) error {
srcFH, err := os.Open(srcFile)
if err != nil {
CheckErr(err, "fail to open file "+srcFile, ERR_FILE_OPEN, false)
return err
}
defer srcFH.Close()
destFH, err := os.Open(destFile)
if err != nil {
CheckErr(err, "fail to open file "+destFile, ERR_FILE_OPEN, false)
return err
}
defer destFH.Close()
srcInfo, err := srcFH.Stat()
if err != nil {
CheckErr(err, "fail to stat file "+srcFile, ERR_FILE_READ, false)
return err
}
srcSize := srcInfo.Size() //int64
var offset int64 = -1
var readByteCntTotal int64 = 0
var buf []byte = make([]byte, 1)
var LineSep string = "\n"
var lineCnt int = 0
var byteCntBeforePrint int64 = 0
_, err = srcFH.Seek(0, os.SEEK_END)
if err != nil {
CheckErr(err, "fail to seek file "+srcFile, ERR_FILE_SEEK, false)
return err
}
for {
startPos, err := srcFH.Seek(offset, os.SEEK_CUR)
if err != nil {
CheckErr(err, "fail to seek file "+srcFile, ERR_FILE_SEEK, false)
return err
}
_, err = srcFH.Read(buf)
if err != nil {
CheckErr(err, "fail to read file "+srcFile, ERR_FILE_READ, false)
return err
}
if string(buf) == LineSep {
// if the last byte is EOL, donnot count
if readByteCntTotal != 0 {
lineCnt++
}
}
readByteCntTotal++
byteCntBeforePrint++
// read enough lines or we alread finish reading the whole file
if lineCnt == batchLines || readByteCntTotal == srcSize || startPos == 0 {
printLines := make([]byte, byteCntBeforePrint-1)
_, err := io.ReadFull(srcFH, printLines)
if err != nil {
CheckErr(err, "fail to read file "+srcFile, ERR_FILE_READ, false)
return err
}
_, err = destFH.Write(printLines)
if err != nil {
CheckErr(err, "fail to write file "+destFile, ERR_FILE_WRITE, false)
return err
}
if readByteCntTotal == srcSize || startPos == 0 {
break
}
_, err = srcFH.Seek(-byteCntBeforePrint, os.SEEK_CUR)
if err != nil {
CheckErr(err, "fail to seek file "+srcFile, ERR_FILE_SEEK, false)
return err
}
lineCnt = 0
byteCntBeforePrint = 1
} else {
_, err := srcFH.Seek(offset, os.SEEK_CUR)
if err != nil {
CheckErr(err, "fail to seek file "+srcFile, ERR_FILE_SEEK, false)
return err
}
}
}
return nil
}