/
update_join.go
332 lines (268 loc) · 8.98 KB
/
update_join.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
// Copyright 2020-2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package plan
import (
"gopkg.in/src-d/go-errors.v1"
"github.com/Ciyfly/FakerMysql/sql"
)
type UpdateJoin struct {
updaters map[string]sql.RowUpdater
UnaryNode
}
// NewUpdateJoin returns an *UpdateJoin node.
func NewUpdateJoin(editorMap map[string]sql.RowUpdater, child sql.Node) *UpdateJoin {
return &UpdateJoin{
updaters: editorMap,
UnaryNode: UnaryNode{Child: child},
}
}
var _ sql.Node = (*UpdateJoin)(nil)
// String implements the sql.Node interface.
func (u *UpdateJoin) String() string {
pr := sql.NewTreePrinter()
_ = pr.WriteNode("Update Join")
_ = pr.WriteChildren(u.Child.String())
return pr.String()
}
// RowIter implements the sql.Node interface.
func (u *UpdateJoin) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
ji, err := u.Child.RowIter(ctx, row)
if err != nil {
return nil, err
}
return &updateJoinIter{
ctx: ctx,
updateSourceIter: ji,
joinSchema: u.Child.(*UpdateSource).Child.Schema(),
updaters: u.updaters,
caches: make(map[string]sql.KeyValueCache),
disposals: make(map[string]sql.DisposeFunc),
}, nil
}
// GetUpdatable returns an updateJoinTable which implements sql.UpdatableTable.
func (u *UpdateJoin) GetUpdatable() sql.UpdatableTable {
return &updatableJoinTable{
updaters: u.updaters,
joinNode: u.Child.(*UpdateSource).Child,
}
}
// WithChildren implements the sql.Node interface.
func (u *UpdateJoin) WithChildren(children ...sql.Node) (sql.Node, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(u, len(children), 1)
}
return NewUpdateJoin(u.updaters, children[0]), nil
}
// updateJoinIter wraps the child UpdateSource iter and returns join row in such a way that updates per table row are
// done once.
type updateJoinIter struct {
ctx *sql.Context
updateSourceIter sql.RowIter
joinSchema sql.Schema
updaters map[string]sql.RowUpdater
caches map[string]sql.KeyValueCache
disposals map[string]sql.DisposeFunc
}
var _ sql.RowIter = (*updateJoinIter)(nil)
func (u *updateJoinIter) Next() (sql.Row, error) {
for {
oldAndNewRow, err := u.updateSourceIter.Next()
if err != nil {
return nil, err
}
oldJoinRow, newJoinRow := oldAndNewRow[:len(oldAndNewRow)/2], oldAndNewRow[len(oldAndNewRow)/2:]
tableToOldRowMap := splitRowIntoTableRowMap(oldJoinRow, u.joinSchema)
tableToNewRowMap := splitRowIntoTableRowMap(newJoinRow, u.joinSchema)
for tableName, _ := range u.updaters {
oldTableRow := tableToOldRowMap[tableName]
cache := u.getOrCreateCache(u.ctx, tableName)
hash, err := sql.HashOf(oldTableRow)
if err != nil {
return nil, err
}
_, err = cache.Get(hash)
if errors.Is(err, sql.ErrKeyNotFound) {
cache.Put(hash, struct{}{})
continue
} else if err != nil {
return nil, err
}
// If this row for the table has already been updated we rewrite the newJoinRow counterpart to ensure that this
// returned row is not incorrectly counted by the update accumulator.
tableToNewRowMap[tableName] = oldTableRow
}
newJoinRow = recreateRowFromMap(tableToNewRowMap, u.joinSchema)
equals, err := oldJoinRow.Equals(newJoinRow, u.joinSchema)
if err != nil {
return nil, err
}
if !equals {
return append(oldJoinRow, newJoinRow...), nil
}
}
}
func (u *updateJoinIter) Close(context *sql.Context) error {
for _, disposeF := range u.disposals {
disposeF()
}
return u.updateSourceIter.Close(context)
}
func (u *updateJoinIter) getOrCreateCache(ctx *sql.Context, tableName string) sql.KeyValueCache {
potential, exists := u.caches[tableName]
if exists {
return potential
}
cache, disposal := ctx.Memory.NewHistoryCache()
u.caches[tableName] = cache
u.disposals[tableName] = disposal
return cache
}
// updatableJoinTable manages the update of multiple tables.
type updatableJoinTable struct {
updaters map[string]sql.RowUpdater
joinNode sql.Node
}
var _ sql.UpdatableTable = (*updatableJoinTable)(nil)
// Partitions implements the sql.UpdatableTable interface.
func (u *updatableJoinTable) Partitions(context *sql.Context) (sql.PartitionIter, error) {
panic("this method should not be called")
}
// PartitionsRows implements the sql.UpdatableTable interface.
func (u *updatableJoinTable) PartitionRows(context *sql.Context, partition sql.Partition) (sql.RowIter, error) {
panic("this method should not be called")
}
// Name implements the sql.UpdatableTable interface.
func (u *updatableJoinTable) Name() string {
panic("this method should not be called")
}
// String implements the sql.UpdatableTable interface.
func (u *updatableJoinTable) String() string {
panic("this method should not be called")
}
// Schema implements the sql.UpdatableTable interface.
func (u *updatableJoinTable) Schema() sql.Schema {
return u.joinNode.Schema()
}
// Updater implements the sql.UpdatableTable interface.
func (u *updatableJoinTable) Updater(ctx *sql.Context) sql.RowUpdater {
return &updatableJoinUpdater{
updaterMap: u.updaters,
schemaMap: recreateTableSchemaFromJoinSchema(u.joinNode.Schema()),
joinSchema: u.joinNode.Schema(),
}
}
// updatableJoinUpdater manages the process of taking a join row and allocating the respective updates to each updatable
// table.
type updatableJoinUpdater struct {
updaterMap map[string]sql.RowUpdater
schemaMap map[string]sql.Schema
joinSchema sql.Schema
}
var _ sql.RowUpdater = (*updatableJoinUpdater)(nil)
// StatementBegin implements the sql.TableEditor interface.
func (u *updatableJoinUpdater) StatementBegin(ctx *sql.Context) {
for _, v := range u.updaterMap {
v.StatementBegin(ctx)
}
}
// DiscardChanges implements the sql.TableEditor interface.
func (u *updatableJoinUpdater) DiscardChanges(ctx *sql.Context, errorEncountered error) error {
for _, v := range u.updaterMap {
err := v.DiscardChanges(ctx, errorEncountered)
if err != nil {
return err
}
}
return nil
}
// StatementComplete implements the sql.TableEditor interface.
func (u *updatableJoinUpdater) StatementComplete(ctx *sql.Context) error {
for _, v := range u.updaterMap {
err := v.StatementComplete(ctx)
if err != nil {
return err
}
}
return nil
}
// Update implements the sql.RowUpdater interface.
func (u *updatableJoinUpdater) Update(ctx *sql.Context, old sql.Row, new sql.Row) error {
tableToOldRowMap := splitRowIntoTableRowMap(old, u.joinSchema)
tableToNewRowMap := splitRowIntoTableRowMap(new, u.joinSchema)
for tableName, updater := range u.updaterMap {
oldRow := tableToOldRowMap[tableName]
newRow := tableToNewRowMap[tableName]
schema := u.schemaMap[tableName]
eq, err := oldRow.Equals(newRow, schema)
if err != nil {
return err
}
if !eq {
err = updater.Update(ctx, oldRow, newRow)
}
if err != nil {
return err
}
}
return nil
}
// Close implements the sql.RowUpdater interface.
func (u *updatableJoinUpdater) Close(ctx *sql.Context) error {
for _, updater := range u.updaterMap {
err := updater.Close(ctx)
if err != nil {
return err
}
}
return nil
}
// splitRowIntoTableRowMap takes a join table row and breaks into a map of tables and their respective row.
func splitRowIntoTableRowMap(row sql.Row, joinSchema sql.Schema) map[string]sql.Row {
ret := make(map[string]sql.Row)
if len(joinSchema) == 0 {
return ret
}
currentTable := joinSchema[0].Source
currentRow := sql.Row{row[0]}
for i := 1; i < len(joinSchema); i++ {
c := joinSchema[i]
if c.Source != currentTable {
ret[currentTable] = currentRow
currentTable = c.Source
currentRow = sql.Row{row[i]}
} else {
currentTable = c.Source
currentRow = append(currentRow, row[i])
}
}
ret[currentTable] = currentRow
return ret
}
// recreateRowFromMap takes a join schema and row map and recreates the original join row.
func recreateRowFromMap(rowMap map[string]sql.Row, joinSchema sql.Schema) sql.Row {
var ret sql.Row
if len(joinSchema) == 0 {
return ret
}
currentTable := joinSchema[0].Source
ret = append(ret, rowMap[currentTable]...)
for i := 1; i < len(joinSchema); i++ {
c := joinSchema[i]
if c.Source != currentTable {
ret = append(ret, rowMap[c.Source]...)
currentTable = c.Source
}
}
return ret
}