forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lookup_unicodeloosemd5_hash.go
404 lines (358 loc) · 12.2 KB
/
lookup_unicodeloosemd5_hash.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
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 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 vindexes
import (
"encoding/binary"
"encoding/json"
"fmt"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)
var (
_ Vindex = (*LookupUnicodeLooseMD5Hash)(nil)
_ Lookup = (*LookupUnicodeLooseMD5Hash)(nil)
_ Vindex = (*LookupUnicodeLooseMD5HashUnique)(nil)
_ Lookup = (*LookupUnicodeLooseMD5HashUnique)(nil)
)
func init() {
Register("lookup_unicodeloosemd5_hash", NewLookupUnicodeLooseMD5Hash)
Register("lookup_unicodeloosemd5_hash_unique", NewLookupUnicodeLooseMD5HashUnique)
}
//====================================================================
// LookupUnicodeLooseMD5Hash defines a vindex that uses a lookup table.
// The table is expected to define the id column as unique. It's
// NonUnique and a Lookup and stores the from value in a hashed form.
// Warning: This Vindex is being depcreated in favor of Lookup
type LookupUnicodeLooseMD5Hash struct {
name string
writeOnly bool
lkp lookupInternal
}
// NewLookupUnicodeLooseMD5Hash creates a LookupUnicodeLooseMD5Hash 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.
//
// The following fields are optional:
// autocommit: setting this to "true" will cause inserts to upsert and deletes to be ignored.
// write_only: in this mode, Map functions return the full keyrange causing a full scatter.
func NewLookupUnicodeLooseMD5Hash(name string, m map[string]string) (Vindex, error) {
lh := &LookupUnicodeLooseMD5Hash{name: name}
autocommit, err := boolFromMap(m, "autocommit")
if err != nil {
return nil, err
}
lh.writeOnly, err = boolFromMap(m, "write_only")
if err != nil {
return nil, err
}
// if autocommit is on for non-unique lookup, upsert should also be on.
if err := lh.lkp.Init(m, autocommit, autocommit /* upsert */); err != nil {
return nil, err
}
return lh, nil
}
// String returns the name of the vindex.
func (lh *LookupUnicodeLooseMD5Hash) String() string {
return lh.name
}
// Cost returns the cost of this vindex as 20.
func (lh *LookupUnicodeLooseMD5Hash) Cost() int {
return 20
}
// IsUnique returns false since the Vindex is not unique.
func (lh *LookupUnicodeLooseMD5Hash) IsUnique() bool {
return false
}
// IsFunctional returns false since the Vindex is not functional.
func (lh *LookupUnicodeLooseMD5Hash) IsFunctional() bool {
return false
}
// Map can map ids to key.Destination objects.
func (lh *LookupUnicodeLooseMD5Hash) Map(vcursor VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
out := make([]key.Destination, 0, len(ids))
if lh.writeOnly {
for range ids {
out = append(out, key.DestinationKeyRange{KeyRange: &topodatapb.KeyRange{}})
}
return out, nil
}
ids, err := convertIds(ids)
if err != nil {
return nil, err
}
results, err := lh.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 {
num, err := sqltypes.ToUint64(row[0])
if err != nil {
// A failure to convert is equivalent to not being
// able to map.
continue
}
ksids = append(ksids, vhash(num))
}
out = append(out, key.DestinationKeyspaceIDs(ksids))
}
return out, nil
}
// Verify returns true if ids maps to ksids.
func (lh *LookupUnicodeLooseMD5Hash) Verify(vcursor VCursor, ids []sqltypes.Value, ksids [][]byte) ([]bool, error) {
if lh.writeOnly {
out := make([]bool, len(ids))
for i := range ids {
out[i] = true
}
return out, nil
}
values, err := unhashList(ksids)
if err != nil {
return nil, fmt.Errorf("lookup.Verify.vunhash: %v", err)
}
ids, err = convertIds(ids)
if err != nil {
return nil, fmt.Errorf("lookup.Verify.vunhash: %v", err)
}
return lh.lkp.Verify(vcursor, ids, values)
}
// Create reserves the id by inserting it into the vindex table.
func (lh *LookupUnicodeLooseMD5Hash) Create(vcursor VCursor, rowsColValues [][]sqltypes.Value, ksids [][]byte, ignoreMode bool) error {
values, err := unhashList(ksids)
if err != nil {
return fmt.Errorf("lookup.Create.vunhash: %v", err)
}
rowsColValues, err = convertRows(rowsColValues)
if err != nil {
return fmt.Errorf("lookup.Create.convert: %v", err)
}
return lh.lkp.Create(vcursor, rowsColValues, values, ignoreMode)
}
// Update updates the entry in the vindex table.
func (lh *LookupUnicodeLooseMD5Hash) Update(vcursor VCursor, oldValues []sqltypes.Value, ksid []byte, newValues []sqltypes.Value) error {
v, err := vunhash(ksid)
if err != nil {
return fmt.Errorf("lookup.Update.vunhash: %v", err)
}
newValues, err = convertIds(newValues)
if err != nil {
return fmt.Errorf("lookup.Update.convert: %v", err)
}
oldValues, err = convertIds(oldValues)
if err != nil {
return fmt.Errorf("lookup.Update.convert: %v", err)
}
return lh.lkp.Update(vcursor, oldValues, sqltypes.NewUint64(v), newValues)
}
// Delete deletes the entry from the vindex table.
func (lh *LookupUnicodeLooseMD5Hash) Delete(vcursor VCursor, rowsColValues [][]sqltypes.Value, ksid []byte) error {
v, err := vunhash(ksid)
if err != nil {
return fmt.Errorf("lookup.Delete.vunhash: %v", err)
}
rowsColValues, err = convertRows(rowsColValues)
if err != nil {
return fmt.Errorf("lookup.Delete.convert: %v", err)
}
return lh.lkp.Delete(vcursor, rowsColValues, sqltypes.NewUint64(v))
}
// MarshalJSON returns a JSON representation of LookupHash.
func (lh *LookupUnicodeLooseMD5Hash) MarshalJSON() ([]byte, error) {
return json.Marshal(lh.lkp)
}
//====================================================================
// LookupUnicodeLooseMD5HashUnique 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 and will store the from value in a hashed format.
// Warning: This Vindex is being depcreated in favor of LookupUnique
type LookupUnicodeLooseMD5HashUnique struct {
name string
writeOnly bool
lkp lookupInternal
}
// NewLookupUnicodeLooseMD5HashUnique creates a LookupUnicodeLooseMD5HashUnique 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.
//
// The following fields are optional:
// autocommit: setting this to "true" will cause deletes to be ignored.
// write_only: in this mode, Map functions return the full keyrange causing a full scatter.
func NewLookupUnicodeLooseMD5HashUnique(name string, m map[string]string) (Vindex, error) {
lhu := &LookupUnicodeLooseMD5HashUnique{name: name}
autocommit, err := boolFromMap(m, "autocommit")
if err != nil {
return nil, err
}
lhu.writeOnly, err = boolFromMap(m, "write_only")
if err != nil {
return nil, err
}
// Don't allow upserts for unique vindexes.
if err := lhu.lkp.Init(m, autocommit, false /* upsert */); err != nil {
return nil, err
}
return lhu, nil
}
// String returns the name of the vindex.
func (lhu *LookupUnicodeLooseMD5HashUnique) String() string {
return lhu.name
}
// Cost returns the cost of this vindex as 10.
func (lhu *LookupUnicodeLooseMD5HashUnique) Cost() int {
return 10
}
// IsUnique returns true since the Vindex is unique.
func (lhu *LookupUnicodeLooseMD5HashUnique) IsUnique() bool {
return true
}
// IsFunctional returns false since the Vindex is not functional.
func (lhu *LookupUnicodeLooseMD5HashUnique) IsFunctional() bool {
return false
}
// Map can map ids to key.Destination objects.
func (lhu *LookupUnicodeLooseMD5HashUnique) Map(vcursor VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
out := make([]key.Destination, 0, len(ids))
if lhu.writeOnly {
for range ids {
out = append(out, key.DestinationKeyRange{KeyRange: &topodatapb.KeyRange{}})
}
return out, nil
}
ids, err := convertIds(ids)
if err != nil {
return nil, err
}
results, err := lhu.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:
num, err := sqltypes.ToUint64(result.Rows[0][0])
if err != nil {
out = append(out, key.DestinationNone{})
continue
}
out = append(out, key.DestinationKeyspaceID(vhash(num)))
default:
return nil, fmt.Errorf("LookupUnicodeLooseMD5HashUnique.Map: unexpected multiple results from vindex %s: %v", lhu.lkp.Table, ids[i])
}
}
return out, nil
}
// Verify returns true if ids maps to ksids.
func (lhu *LookupUnicodeLooseMD5HashUnique) Verify(vcursor VCursor, ids []sqltypes.Value, ksids [][]byte) ([]bool, error) {
if lhu.writeOnly {
out := make([]bool, len(ids))
for i := range ids {
out[i] = true
}
return out, nil
}
values, err := unhashList(ksids)
if err != nil {
return nil, fmt.Errorf("lookup.Verify.vunhash: %v", err)
}
ids, err = convertIds(ids)
if err != nil {
return nil, fmt.Errorf("lookup.Verify.vunhash: %v", err)
}
return lhu.lkp.Verify(vcursor, ids, values)
}
// Create reserves the id by inserting it into the vindex table.
func (lhu *LookupUnicodeLooseMD5HashUnique) Create(vcursor VCursor, rowsColValues [][]sqltypes.Value, ksids [][]byte, ignoreMode bool) error {
values, err := unhashList(ksids)
if err != nil {
return fmt.Errorf("lookup.Create.vunhash: %v", err)
}
rowsColValues, err = convertRows(rowsColValues)
if err != nil {
return fmt.Errorf("lookup.Create.convert: %v", err)
}
return lhu.lkp.Create(vcursor, rowsColValues, values, ignoreMode)
}
// Delete deletes the entry from the vindex table.
func (lhu *LookupUnicodeLooseMD5HashUnique) Delete(vcursor VCursor, rowsColValues [][]sqltypes.Value, ksid []byte) error {
v, err := vunhash(ksid)
if err != nil {
return fmt.Errorf("lookup.Delete.vunhash: %v", err)
}
rowsColValues, err = convertRows(rowsColValues)
if err != nil {
return fmt.Errorf("lookup.Delete.convert: %v", err)
}
return lhu.lkp.Delete(vcursor, rowsColValues, sqltypes.NewUint64(v))
}
// Update updates the entry in the vindex table.
func (lhu *LookupUnicodeLooseMD5HashUnique) Update(vcursor VCursor, oldValues []sqltypes.Value, ksid []byte, newValues []sqltypes.Value) error {
v, err := vunhash(ksid)
if err != nil {
return fmt.Errorf("lookup.Update.vunhash: %v", err)
}
newValues, err = convertIds(newValues)
if err != nil {
return fmt.Errorf("lookup.Update.convert: %v", err)
}
oldValues, err = convertIds(oldValues)
if err != nil {
return fmt.Errorf("lookup.Update.convert: %v", err)
}
return lhu.lkp.Update(vcursor, oldValues, sqltypes.NewUint64(v), newValues)
}
// MarshalJSON returns a JSON representation of LookupHashUnique.
func (lhu *LookupUnicodeLooseMD5HashUnique) MarshalJSON() ([]byte, error) {
return json.Marshal(lhu.lkp)
}
func unicodeHashValue(value sqltypes.Value) (sqltypes.Value, error) {
hash, err := unicodeHash(value)
if err != nil {
return sqltypes.NULL, err
}
return sqltypes.NewUint64(binary.BigEndian.Uint64(hash[:8])), nil
}
func convertIds(ids []sqltypes.Value) ([]sqltypes.Value, error) {
converted := make([]sqltypes.Value, 0, len(ids))
for _, id := range ids {
idVal, err := unicodeHashValue(id)
if err != nil {
return nil, err
}
converted = append(converted, idVal)
}
return converted, nil
}
func convertRows(rows [][]sqltypes.Value) ([][]sqltypes.Value, error) {
converted := make([][]sqltypes.Value, 0, len(rows))
for _, row := range rows {
row, err := convertIds(row)
if err != nil {
return nil, err
}
converted = append(converted, row)
}
return converted, nil
}