-
Notifications
You must be signed in to change notification settings - Fork 4
/
lookup_hash.go
336 lines (293 loc) · 9.93 KB
/
lookup_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
/*
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/json"
"fmt"
"gopkg.in/src-d/go-vitess.v1/sqltypes"
"gopkg.in/src-d/go-vitess.v1/vt/key"
topodatapb "gopkg.in/src-d/go-vitess.v1/vt/proto/topodata"
vtgatepb "gopkg.in/src-d/go-vitess.v1/vt/proto/vtgate"
)
var (
_ Vindex = (*LookupHash)(nil)
_ Lookup = (*LookupHash)(nil)
_ Vindex = (*LookupHashUnique)(nil)
_ Lookup = (*LookupHashUnique)(nil)
)
func init() {
Register("lookup_hash", NewLookupHash)
Register("lookup_hash_unique", NewLookupHashUnique)
}
//====================================================================
// LookupHash 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.
// Warning: This Vindex is being depcreated in favor of Lookup
type LookupHash struct {
name string
writeOnly bool
lkp lookupInternal
}
// NewLookupHash creates a LookupHash 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 NewLookupHash(name string, m map[string]string) (Vindex, error) {
lh := &LookupHash{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 *LookupHash) String() string {
return lh.name
}
// Cost returns the cost of this vindex as 20.
func (lh *LookupHash) Cost() int {
return 20
}
// IsUnique returns false since the Vindex is not unique.
func (lh *LookupHash) IsUnique() bool {
return false
}
// IsFunctional returns false since the Vindex is not functional.
func (lh *LookupHash) IsFunctional() bool {
return false
}
// Map can map ids to key.Destination objects.
func (lh *LookupHash) 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
}
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 *LookupHash) 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)
}
return lh.lkp.Verify(vcursor, ids, values)
}
// Create reserves the id by inserting it into the vindex table.
func (lh *LookupHash) 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)
}
return lh.lkp.Create(vcursor, rowsColValues, values, ignoreMode)
}
// Update updates the entry in the vindex table.
func (lh *LookupHash) 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)
}
return lh.lkp.Update(vcursor, oldValues, sqltypes.NewUint64(v), newValues)
}
// Delete deletes the entry from the vindex table.
func (lh *LookupHash) Delete(vcursor VCursor, rowsColValues [][]sqltypes.Value, ksid []byte) error {
v, err := vunhash(ksid)
if err != nil {
return fmt.Errorf("lookup.Delete.vunhash: %v", err)
}
return lh.lkp.Delete(vcursor, rowsColValues, sqltypes.NewUint64(v), vtgatepb.CommitOrder_NORMAL)
}
// MarshalJSON returns a JSON representation of LookupHash.
func (lh *LookupHash) MarshalJSON() ([]byte, error) {
return json.Marshal(lh.lkp)
}
// unhashList unhashes a list of keyspace ids into []sqltypes.Value.
func unhashList(ksids [][]byte) ([]sqltypes.Value, error) {
values := make([]sqltypes.Value, 0, len(ksids))
for _, ksid := range ksids {
v, err := vunhash(ksid)
if err != nil {
return nil, err
}
values = append(values, sqltypes.NewUint64(v))
}
return values, nil
}
//====================================================================
// LookupHashUnique 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.
// Warning: This Vindex is being depcreated in favor of LookupUnique
type LookupHashUnique struct {
name string
writeOnly bool
lkp lookupInternal
}
// NewLookupHashUnique creates a LookupHashUnique 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 NewLookupHashUnique(name string, m map[string]string) (Vindex, error) {
lhu := &LookupHashUnique{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 *LookupHashUnique) String() string {
return lhu.name
}
// Cost returns the cost of this vindex as 10.
func (lhu *LookupHashUnique) Cost() int {
return 10
}
// IsUnique returns true since the Vindex is unique.
func (lhu *LookupHashUnique) IsUnique() bool {
return true
}
// IsFunctional returns false since the Vindex is not functional.
func (lhu *LookupHashUnique) IsFunctional() bool {
return false
}
// Map can map ids to key.Destination objects.
func (lhu *LookupHashUnique) 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
}
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("LookupHash.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 *LookupHashUnique) 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)
}
return lhu.lkp.Verify(vcursor, ids, values)
}
// Create reserves the id by inserting it into the vindex table.
func (lhu *LookupHashUnique) 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)
}
return lhu.lkp.Create(vcursor, rowsColValues, values, ignoreMode)
}
// Delete deletes the entry from the vindex table.
func (lhu *LookupHashUnique) Delete(vcursor VCursor, rowsColValues [][]sqltypes.Value, ksid []byte) error {
v, err := vunhash(ksid)
if err != nil {
return fmt.Errorf("lookup.Delete.vunhash: %v", err)
}
return lhu.lkp.Delete(vcursor, rowsColValues, sqltypes.NewUint64(v), vtgatepb.CommitOrder_NORMAL)
}
// Update updates the entry in the vindex table.
func (lhu *LookupHashUnique) 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)
}
return lhu.lkp.Update(vcursor, oldValues, sqltypes.NewUint64(v), newValues)
}
// MarshalJSON returns a JSON representation of LookupHashUnique.
func (lhu *LookupHashUnique) MarshalJSON() ([]byte, error) {
return json.Marshal(lhu.lkp)
}