-
Notifications
You must be signed in to change notification settings - Fork 4
/
consistent_lookup.go
347 lines (307 loc) · 10.6 KB
/
consistent_lookup.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
/*
Copyright 2017 Google 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 agreedto 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 vindexes
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"gopkg.in/src-d/go-vitess.v1/sqltypes"
"gopkg.in/src-d/go-vitess.v1/vt/key"
querypb "gopkg.in/src-d/go-vitess.v1/vt/proto/query"
vtgatepb "gopkg.in/src-d/go-vitess.v1/vt/proto/vtgate"
vtrpcpb "gopkg.in/src-d/go-vitess.v1/vt/proto/vtrpc"
"gopkg.in/src-d/go-vitess.v1/vt/sqlparser"
"gopkg.in/src-d/go-vitess.v1/vt/vterrors"
)
var (
_ Vindex = (*ConsistentLookupUnique)(nil)
_ Lookup = (*ConsistentLookupUnique)(nil)
_ WantOwnerInfo = (*ConsistentLookupUnique)(nil)
_ Vindex = (*ConsistentLookup)(nil)
_ Lookup = (*ConsistentLookup)(nil)
_ WantOwnerInfo = (*ConsistentLookup)(nil)
)
func init() {
Register("consistent_lookup", NewConsistentLookup)
Register("consistent_lookup_unique", NewConsistentLookupUnique)
}
// ConsistentLookup is a non-unique lookup vindex that can stay
// consistent with respect to its owner table.
type ConsistentLookup struct {
*clCommon
}
// NewConsistentLookup creates a ConsistentLookup vindex.
// The supplied map has the following required fields:
// table: name of the backing table. It can be qualified by the keyspace.
// from: list of columns in the table that have the 'from' values of the lookup vindex.
// to: The 'to' column name of the table.
func NewConsistentLookup(name string, m map[string]string) (Vindex, error) {
clc, err := newCLCommon(name, m)
if err != nil {
return nil, err
}
return &ConsistentLookup{clCommon: clc}, nil
}
// Cost returns the cost of this vindex as 20.
func (lu *ConsistentLookup) Cost() int {
return 20
}
// IsUnique returns false since the Vindex is non unique.
func (lu *ConsistentLookup) IsUnique() bool {
return false
}
// Map can map ids to key.Destination objects.
func (lu *ConsistentLookup) Map(vcursor VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
out := make([]key.Destination, 0, len(ids))
results, err := lu.lkp.Lookup(vcursor, ids)
if err != nil {
return nil, err
}
for _, result := range results {
if len(result.Rows) == 0 {
out = append(out, key.DestinationNone{})
continue
}
ksids := make([][]byte, 0, len(result.Rows))
for _, row := range result.Rows {
ksids = append(ksids, row[0].ToBytes())
}
out = append(out, key.DestinationKeyspaceIDs(ksids))
}
return out, nil
}
//====================================================================
// ConsistentLookupUnique defines a vindex that uses a lookup table.
// The table is expected to define the id column as unique. It's
// Unique and a Lookup.
type ConsistentLookupUnique struct {
*clCommon
}
// NewConsistentLookupUnique creates a ConsistentLookupUnique vindex.
// The supplied map has the following required fields:
// table: name of the backing table. It can be qualified by the keyspace.
// from: list of columns in the table that have the 'from' values of the lookup vindex.
// to: The 'to' column name of the table.
func NewConsistentLookupUnique(name string, m map[string]string) (Vindex, error) {
clc, err := newCLCommon(name, m)
if err != nil {
return nil, err
}
return &ConsistentLookupUnique{clCommon: clc}, nil
}
// Cost returns the cost of this vindex as 10.
func (lu *ConsistentLookupUnique) Cost() int {
return 10
}
// IsUnique returns true since the Vindex is unique.
func (lu *ConsistentLookupUnique) IsUnique() bool {
return true
}
// Map can map ids to key.Destination objects.
func (lu *ConsistentLookupUnique) Map(vcursor VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
out := make([]key.Destination, 0, len(ids))
results, err := lu.lkp.Lookup(vcursor, ids)
if err != nil {
return nil, err
}
for i, result := range results {
switch len(result.Rows) {
case 0:
out = append(out, key.DestinationNone{})
case 1:
out = append(out, key.DestinationKeyspaceID(result.Rows[0][0].ToBytes()))
default:
return nil, fmt.Errorf("Lookup.Map: unexpected multiple results from vindex %s: %v", lu.lkp.Table, ids[i])
}
}
return out, nil
}
//====================================================================
// clCommon defines a vindex that uses a lookup table.
// The table is expected to define the id column as unique. It's
// Unique and a Lookup.
type clCommon struct {
name string
lkp lookupInternal
keyspace string
ownerTable string
ownerColumns []string
lockLookupQuery string
lockOwnerQuery string
insertLookupQuery string
updateLookupQuery string
}
// newCLCommon is commone code for the consistent lookup vindexes.
func newCLCommon(name string, m map[string]string) (*clCommon, error) {
lu := &clCommon{name: name}
if err := lu.lkp.Init(m, false /* autocommit */, false /* upsert */); err != nil {
return nil, err
}
return lu, nil
}
func (lu *clCommon) SetOwnerInfo(keyspace, table string, cols []sqlparser.ColIdent) error {
lu.keyspace = keyspace
lu.ownerTable = table
if len(cols) != len(lu.lkp.FromColumns) {
return fmt.Errorf("owner table column count does not match vindex %s", lu.name)
}
lu.ownerColumns = make([]string, len(cols))
for i, col := range cols {
lu.ownerColumns[i] = col.String()
}
lu.lockLookupQuery = lu.generateLockLookup()
lu.lockOwnerQuery = lu.generateLockOwner()
lu.insertLookupQuery = lu.generateInsertLookup()
lu.updateLookupQuery = lu.generateUpdateLookup()
return nil
}
// String returns the name of the vindex.
func (lu *clCommon) String() string {
return lu.name
}
// IsFunctional returns false since the Vindex is not functional.
func (lu *clCommon) IsFunctional() bool {
return false
}
// Verify returns true if ids maps to ksids.
func (lu *clCommon) Verify(vcursor VCursor, ids []sqltypes.Value, ksids [][]byte) ([]bool, error) {
return lu.lkp.Verify(vcursor, ids, ksidsToValues(ksids))
}
// Create reserves the id by inserting it into the vindex table.
func (lu *clCommon) Create(vcursor VCursor, rowsColValues [][]sqltypes.Value, ksids [][]byte, ignoreMode bool) error {
err := lu.lkp.createCustom(vcursor, rowsColValues, ksidsToValues(ksids), ignoreMode, vtgatepb.CommitOrder_PRE)
if err == nil {
return nil
}
if !strings.Contains(err.Error(), "Duplicate entry") {
return err
}
for i, row := range rowsColValues {
if err := lu.handleDup(vcursor, row, ksids[i]); err != nil {
return err
}
}
return nil
}
func (lu *clCommon) handleDup(vcursor VCursor, values []sqltypes.Value, ksid []byte) error {
bindVars := make(map[string]*querypb.BindVariable, len(values))
for colnum, val := range values {
bindVars[lu.lkp.FromColumns[colnum]] = sqltypes.ValueBindVariable(val)
}
bindVars[lu.lkp.To] = sqltypes.BytesBindVariable(ksid)
// Lock the lookup row using pre priority.
qr, err := vcursor.Execute("VindexCreate", lu.lockLookupQuery, bindVars, false /* isDML */, vtgatepb.CommitOrder_PRE)
if err != nil {
return err
}
switch len(qr.Rows) {
case 0:
if _, err := vcursor.Execute("VindexCreate", lu.insertLookupQuery, bindVars, true /* isDML */, vtgatepb.CommitOrder_PRE); err != nil {
return err
}
case 1:
existingksid := qr.Rows[0][0].ToBytes()
// Lock the target row using normal transaction priority.
qr, err = vcursor.ExecuteKeyspaceID(lu.keyspace, existingksid, lu.lockOwnerQuery, bindVars, false /* isDML */, false /* autocommit */)
if err != nil {
return err
}
if len(qr.Rows) >= 1 {
return vterrors.Errorf(vtrpcpb.Code_ALREADY_EXISTS, "duplicate entry %v", values)
}
if bytes.Equal(existingksid, ksid) {
return nil
}
if _, err := vcursor.Execute("VindexCreate", lu.updateLookupQuery, bindVars, true /* isDML */, vtgatepb.CommitOrder_PRE); err != nil {
return err
}
default:
return fmt.Errorf("unexpected rows: %v from consistent lookup vindex", qr.Rows)
}
return nil
}
// Delete deletes the entry from the vindex table.
func (lu *clCommon) Delete(vcursor VCursor, rowsColValues [][]sqltypes.Value, ksid []byte) error {
return lu.lkp.Delete(vcursor, rowsColValues, sqltypes.MakeTrusted(sqltypes.VarBinary, ksid), vtgatepb.CommitOrder_POST)
}
// Update updates the entry in the vindex table.
func (lu *clCommon) Update(vcursor VCursor, oldValues []sqltypes.Value, ksid []byte, newValues []sqltypes.Value) error {
equal := true
for i := range oldValues {
result, err := sqltypes.NullsafeCompare(oldValues[i], newValues[i])
if err != nil {
return err
}
if result != 0 {
equal = false
break
}
}
if equal {
return nil
}
if err := lu.Delete(vcursor, [][]sqltypes.Value{oldValues}, ksid); err != nil {
return err
}
return lu.Create(vcursor, [][]sqltypes.Value{newValues}, [][]byte{ksid}, false /* ignoreMode */)
}
// MarshalJSON returns a JSON representation of clCommon.
func (lu *clCommon) MarshalJSON() ([]byte, error) {
return json.Marshal(lu.lkp)
}
func (lu *clCommon) generateLockLookup() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "select %s from %s", lu.lkp.To, lu.lkp.Table)
lu.addWhere(&buf, lu.lkp.FromColumns)
fmt.Fprintf(&buf, " for update")
return buf.String()
}
func (lu *clCommon) generateLockOwner() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "select %s from %s", lu.ownerColumns[0], lu.ownerTable)
lu.addWhere(&buf, lu.ownerColumns)
// We can lock in share mode because we only want to check
// if the row exists. We still need to lock to make us wait
// in case a previous transaction is creating it.
fmt.Fprintf(&buf, " lock in share mode")
return buf.String()
}
func (lu *clCommon) generateInsertLookup() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "insert into %s(", lu.lkp.Table)
for _, col := range lu.lkp.FromColumns {
fmt.Fprintf(&buf, "%s, ", col)
}
fmt.Fprintf(&buf, "%s) values(", lu.lkp.To)
for _, col := range lu.lkp.FromColumns {
fmt.Fprintf(&buf, ":%s, ", col)
}
fmt.Fprintf(&buf, ":%s)", lu.lkp.To)
return buf.String()
}
func (lu *clCommon) generateUpdateLookup() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "update %s set %s=:%s", lu.lkp.Table, lu.lkp.To, lu.lkp.To)
lu.addWhere(&buf, lu.lkp.FromColumns)
return buf.String()
}
func (lu *clCommon) addWhere(buf *bytes.Buffer, cols []string) {
buf.WriteString(" where ")
for colIdx, column := range cols {
if colIdx != 0 {
buf.WriteString(" and ")
}
buf.WriteString(column + " = :" + lu.lkp.FromColumns[colIdx])
}
}