forked from src-d/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.go
273 lines (224 loc) · 6.25 KB
/
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
package plan
import (
"io"
"github.com/src-d/go-mysql-server/sql"
)
// QueryProcess represents a running query process node. It will use a callback
// to notify when it has finished running.
type QueryProcess struct {
UnaryNode
Notify NotifyFunc
}
// NotifyFunc is a function to notify about some event.
type NotifyFunc func()
// NewQueryProcess creates a new QueryProcess node.
func NewQueryProcess(node sql.Node, notify NotifyFunc) *QueryProcess {
return &QueryProcess{UnaryNode{Child: node}, notify}
}
// WithChildren implements the Node interface.
func (p *QueryProcess) WithChildren(children ...sql.Node) (sql.Node, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(p, len(children), 1)
}
return NewQueryProcess(children[0], p.Notify), nil
}
// RowIter implements the sql.Node interface.
func (p *QueryProcess) RowIter(ctx *sql.Context) (sql.RowIter, error) {
iter, err := p.Child.RowIter(ctx)
if err != nil {
return nil, err
}
return &trackedRowIter{iter: iter, onDone: p.Notify}, nil
}
func (p *QueryProcess) String() string { return p.Child.String() }
// ProcessIndexableTable is a wrapper for sql.Tables inside a query process
// that support indexing.
// It notifies the process manager about the status of a query when a
// partition is processed.
type ProcessIndexableTable struct {
sql.IndexableTable
OnPartitionDone NamedNotifyFunc
OnPartitionStart NamedNotifyFunc
OnRowNext NamedNotifyFunc
}
// NewProcessIndexableTable returns a new ProcessIndexableTable.
func NewProcessIndexableTable(t sql.IndexableTable, onPartitionDone, onPartitionStart, OnRowNext NamedNotifyFunc) *ProcessIndexableTable {
return &ProcessIndexableTable{t, onPartitionDone, onPartitionStart, OnRowNext}
}
// Underlying implements sql.TableWrapper interface.
func (t *ProcessIndexableTable) Underlying() sql.Table {
return t.IndexableTable
}
// IndexKeyValues implements the sql.IndexableTable interface.
func (t *ProcessIndexableTable) IndexKeyValues(
ctx *sql.Context,
columns []string,
) (sql.PartitionIndexKeyValueIter, error) {
iter, err := t.IndexableTable.IndexKeyValues(ctx, columns)
if err != nil {
return nil, err
}
return &trackedPartitionIndexKeyValueIter{iter, t.OnPartitionDone, t.OnPartitionStart, t.OnRowNext}, nil
}
// PartitionRows implements the sql.Table interface.
func (t *ProcessIndexableTable) PartitionRows(ctx *sql.Context, p sql.Partition) (sql.RowIter, error) {
iter, err := t.IndexableTable.PartitionRows(ctx, p)
if err != nil {
return nil, err
}
partitionName := partitionName(p)
if t.OnPartitionStart != nil {
t.OnPartitionStart(partitionName)
}
var onDone NotifyFunc
if t.OnPartitionDone != nil {
onDone = func() {
t.OnPartitionDone(partitionName)
}
}
var onNext NotifyFunc
if t.OnRowNext != nil {
onNext = func() {
t.OnRowNext(partitionName)
}
}
return &trackedRowIter{iter: iter, onNext: onNext, onDone: onDone}, nil
}
var _ sql.IndexableTable = (*ProcessIndexableTable)(nil)
// NamedNotifyFunc is a function to notify about some event with a string argument.
type NamedNotifyFunc func(name string)
// ProcessTable is a wrapper for sql.Tables inside a query process. It
// notifies the process manager about the status of a query when a partition
// is processed.
type ProcessTable struct {
sql.Table
OnPartitionDone NamedNotifyFunc
OnPartitionStart NamedNotifyFunc
OnRowNext NamedNotifyFunc
}
// NewProcessTable returns a new ProcessTable.
func NewProcessTable(t sql.Table, onPartitionDone, onPartitionStart, OnRowNext NamedNotifyFunc) *ProcessTable {
return &ProcessTable{t, onPartitionDone, onPartitionStart, OnRowNext}
}
// Underlying implements sql.TableWrapper interface.
func (t *ProcessTable) Underlying() sql.Table {
return t.Table
}
// PartitionRows implements the sql.Table interface.
func (t *ProcessTable) PartitionRows(ctx *sql.Context, p sql.Partition) (sql.RowIter, error) {
iter, err := t.Table.PartitionRows(ctx, p)
if err != nil {
return nil, err
}
partitionName := partitionName(p)
if t.OnPartitionStart != nil {
t.OnPartitionStart(partitionName)
}
var onDone NotifyFunc
if t.OnPartitionDone != nil {
onDone = func() {
t.OnPartitionDone(partitionName)
}
}
var onNext NotifyFunc
if t.OnRowNext != nil {
onNext = func() {
t.OnRowNext(partitionName)
}
}
return &trackedRowIter{iter: iter, onNext: onNext, onDone: onDone}, nil
}
type trackedRowIter struct {
iter sql.RowIter
onDone NotifyFunc
onNext NotifyFunc
}
func (i *trackedRowIter) done() {
if i.onDone != nil {
i.onDone()
i.onDone = nil
}
}
func (i *trackedRowIter) Next() (sql.Row, error) {
row, err := i.iter.Next()
if err != nil {
if err == io.EOF {
i.done()
}
return nil, err
}
if i.onNext != nil {
i.onNext()
}
return row, nil
}
func (i *trackedRowIter) Close() error {
i.done()
return i.iter.Close()
}
type trackedPartitionIndexKeyValueIter struct {
sql.PartitionIndexKeyValueIter
OnPartitionDone NamedNotifyFunc
OnPartitionStart NamedNotifyFunc
OnRowNext NamedNotifyFunc
}
func (i *trackedPartitionIndexKeyValueIter) Next() (sql.Partition, sql.IndexKeyValueIter, error) {
p, iter, err := i.PartitionIndexKeyValueIter.Next()
if err != nil {
return nil, nil, err
}
partitionName := partitionName(p)
if i.OnPartitionStart != nil {
i.OnPartitionStart(partitionName)
}
var onDone NotifyFunc
if i.OnPartitionDone != nil {
onDone = func() {
i.OnPartitionDone(partitionName)
}
}
var onNext NotifyFunc
if i.OnRowNext != nil {
onNext = func() {
i.OnRowNext(partitionName)
}
}
return p, &trackedIndexKeyValueIter{iter, onDone, onNext}, nil
}
type trackedIndexKeyValueIter struct {
iter sql.IndexKeyValueIter
onDone NotifyFunc
onNext NotifyFunc
}
func (i *trackedIndexKeyValueIter) done() {
if i.onDone != nil {
i.onDone()
i.onDone = nil
}
}
func (i *trackedIndexKeyValueIter) Close() (err error) {
i.done()
if i.iter != nil {
err = i.iter.Close()
}
return err
}
func (i *trackedIndexKeyValueIter) Next() ([]interface{}, []byte, error) {
v, k, err := i.iter.Next()
if err != nil {
if err == io.EOF {
i.done()
}
return nil, nil, err
}
if i.onNext != nil {
i.onNext()
}
return v, k, nil
}
func partitionName(p sql.Partition) string {
if n, ok := p.(sql.Nameable); ok {
return n.Name()
}
return string(p.Key())
}