-
Notifications
You must be signed in to change notification settings - Fork 0
/
emitter.go
194 lines (172 loc) · 5.52 KB
/
emitter.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
package wt_concurrency
import (
"bufio"
"fmt"
"io"
"regexp"
"strconv"
"strings"
)
// std::cout << "Line: Timestamp :read 35" << std::endl;
// std::cout << "Idx: 1" << std::endl;
// std::cout << "HasOutput: false" << std::endl;
// // canError
// {
// int errorCode =
// session_1.readAtTimestamp(35);
// if (errorCode == 0) { } else { std::cout << "Error: " << errorCode << " Str: " << wiredtiger_strerror(errorCode) << std::endl; }
// }
var lineRe = regexp.MustCompile("Line: (.+)")
var actorIdxRe = regexp.MustCompile("Idx: (\\d+)")
var hasOutputRe = regexp.MustCompile("HasOutput: (.+)")
var valRe = regexp.MustCompile("Val: (-?\\d+)")
// Error: -31808 Str: WT_PREPARE_CONFLICT: conflict with a prepared update
// Error: 22 Str: Invalid argument
var errorRe = regexp.MustCompile("Error: (-?\\d+) Str: (WT[_[A-Z]+: )?(.+)")
var numActorsRe = regexp.MustCompile("Actors: (\\d+)")
var wtErrStrRe = regexp.MustCompile("\\[1\\d{9}:\\d{0,6}.*?(WT_.*)")
type Table struct {
Actors []string
Recombined []*Recombined
Footnotes []string
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func (table *Table) Output() {
maxColSize := make([]int, len(table.Actors))
for idx, actorName := range table.Actors {
maxColSize[idx] = max(maxColSize[idx], len(actorName))
}
for _, recombined := range table.Recombined {
var act string
switch {
// Basic, no error, no output
case recombined.errCode == 0 && recombined.val == -2 && recombined.errCode == 0 && recombined.wtErrStr == "":
act = recombined.line
case recombined.errCode == 0 && recombined.val == -1:
act = fmt.Sprintf("%v (WT_NOTFOUND)", recombined.line)
case recombined.errCode == 0 && recombined.val >= 0:
act = fmt.Sprintf("%v (%v)", recombined.line, recombined.val)
case recombined.errCode != 0 && len(recombined.wtErrStr) > 0:
act = fmt.Sprintf("%v (%v %v) [%d]", recombined.line, recombined.errCode, recombined.errStr, len(table.Footnotes)+1)
table.Footnotes = append(table.Footnotes, recombined.wtErrStr)
case recombined.errCode != 0 && len(recombined.wtErrStr) == 0:
act = fmt.Sprintf("%v (%v %v)", recombined.line, recombined.errCode, recombined.errStr)
default:
panic(fmt.Sprintf("Unknown case. Rec: %+v", *recombined))
}
if recombined.actorIdx > -1 {
maxColSize[recombined.actorIdx] = max(maxColSize[recombined.actorIdx], len(act))
} else {
act = ""
}
recombined.unpaddedAct = act
}
padArray(table.Actors, maxColSize)
fmt.Printf("|%s|\n", strings.Join(table.Actors, "|"))
underRow := make([]string, len(table.Actors))
for idx, _ := range table.Actors {
// All columns are padding on the left and right by one space.
const padding = 2
underRow[idx] = strings.Repeat("-", maxColSize[idx]+2)
}
fmt.Printf("|%s|\n", strings.Join(underRow, "+"))
for _, recombined := range table.Recombined {
acts := make([]string, len(table.Actors))
if recombined.actorIdx == -1 {
padArray(acts, maxColSize)
fmt.Printf("|%s|\n", strings.Join(acts, "|"))
continue
}
acts[recombined.actorIdx] = recombined.unpaddedAct
padArray(acts, maxColSize)
fmt.Printf("|%s|\n", strings.Join(acts, "|"))
}
if len(table.Footnotes) == 0 {
return
}
fmt.Println("\nFootnotes:")
for idx, footnote := range table.Footnotes {
fmt.Printf(" [%d] %v\n", idx+1, footnote)
}
}
func pad(act string, colSize int) string {
spacesToAdd := colSize - len(act)
return fmt.Sprintf(" %v%v", act, strings.Repeat(" ", spacesToAdd))
}
func padArray(acts []string, maxColSize []int) {
for idx, act := range acts {
acts[idx] = pad(act, maxColSize[idx]+1)
}
}
type Recombined struct {
line string
actorIdx int
val int
errCode int
errStr string
wtErrStr string
unpaddedAct string
}
func NewRecombined(line string) *Recombined {
return &Recombined{line: line, actorIdx: -1, val: -2}
}
func ParseOutput(reader io.Reader) error {
scanner := bufio.NewScanner(reader)
var table Table
var recombined *Recombined
var err error
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if matches := lineRe.FindStringSubmatch(line); len(matches) > 0 {
if recombined != nil {
table.Recombined = append(table.Recombined, recombined)
}
recombined = NewRecombined(matches[1])
} else if matches := actorIdxRe.FindStringSubmatch(line); len(matches) > 0 {
recombined.actorIdx, err = strconv.Atoi(matches[1])
if err != nil {
panic(err)
}
} else if matches := hasOutputRe.FindStringSubmatch(line); len(matches) > 0 {
} else if matches := valRe.FindStringSubmatch(line); len(matches) > 0 {
recombined.val, err = strconv.Atoi(matches[1])
if err != nil {
panic(err)
}
} else if matches := errorRe.FindStringSubmatch(line); len(matches) > 0 {
recombined.errCode, err = strconv.Atoi(matches[1])
if err != nil {
panic(err)
}
recombined.errStr = strings.TrimSpace(matches[2])
if len(matches[3]) == 0 {
recombined.wtErrStr = matches[3]
} else {
recombined.errStr = matches[3]
}
} else if matches := numActorsRe.FindStringSubmatch(line); len(matches) > 0 {
var numActors int
numActors, err = strconv.Atoi(matches[1])
if err != nil {
panic(err)
}
table.Actors = make([]string, numActors)
for idx := 0; idx < numActors; idx++ {
if scanner.Scan() == false {
panic("cannot parse actors")
}
table.Actors[idx] = strings.TrimSpace(scanner.Text())
}
} else if matches := wtErrStrRe.FindStringSubmatch(line); len(matches) > 0 {
recombined.wtErrStr = matches[1]
}
}
table.Recombined = append(table.Recombined, recombined)
table.Output()
return nil
}