forked from vitessio/vitess
/
lookup_internal.go
199 lines (190 loc) · 5.79 KB
/
lookup_internal.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
package vindexes
import (
"bytes"
"fmt"
"strconv"
"strings"
)
// lookup implements the functions for the Lookup vindexes.
type lookup struct {
Table string `json:"table"`
From string `json:"from"`
To string `json:"to"`
sel, ver, ins, del string
isHashedIndex bool
}
func (lkp *lookup) Init(lookupQueryParams map[string]string, isHashed bool) {
table := lookupQueryParams["table"]
fromCol := lookupQueryParams["from"]
toCol := lookupQueryParams["to"]
lkp.Table = table
lkp.From = fromCol
lkp.To = toCol
lkp.sel = fmt.Sprintf("select %s from %s where %s = :%s", toCol, table, fromCol, fromCol)
lkp.ver = fmt.Sprintf("select %s from %s where %s = :%s and %s = :%s", fromCol, table, fromCol, fromCol, toCol, toCol)
lkp.ins = fmt.Sprintf("insert into %s(%s, %s) values", table, fromCol, toCol)
lkp.del = fmt.Sprintf("delete from %s where %s = :%s and %s = :%s", table, fromCol, fromCol, toCol, toCol)
lkp.isHashedIndex = isHashed
}
// MapUniqueLookup is for a Unique Vindex.
func (lkp *lookup) MapUniqueLookup(vcursor VCursor, ids []interface{}) ([][]byte, error) {
out := make([][]byte, 0, len(ids))
for _, id := range ids {
result, err := vcursor.Execute(lkp.sel, map[string]interface{}{
lkp.From: id,
})
if err != nil {
return nil, fmt.Errorf("lookup.Map: %v", err)
}
if len(result.Rows) == 0 {
out = append(out, []byte{})
continue
}
if len(result.Rows) != 1 {
return nil, fmt.Errorf("lookup.Map: unexpected multiple results from vindex %s: %v", lkp.Table, id)
}
if lkp.isHashedIndex {
num, err := getNumber(result.Rows[0][0].ToNative())
if err != nil {
return nil, fmt.Errorf("lookup.Map: %v", err)
}
out = append(out, vhash(num))
} else {
out = append(out, result.Rows[0][0].Raw())
}
}
return out, nil
}
// MapNonUniqueLookup is for a Non-Unique Vindex.
func (lkp *lookup) MapNonUniqueLookup(vcursor VCursor, ids []interface{}) ([][][]byte, error) {
out := make([][][]byte, 0, len(ids))
for _, id := range ids {
result, err := vcursor.Execute(lkp.sel, map[string]interface{}{
lkp.From: id,
})
if err != nil {
return nil, fmt.Errorf("lookup.Map: %v", err)
}
var ksids [][]byte
if lkp.isHashedIndex {
for _, row := range result.Rows {
num, err := getNumber(row[0].ToNative())
if err != nil {
return nil, fmt.Errorf("lookup.Map: %v", err)
}
ksids = append(ksids, vhash(num))
}
} else {
for _, row := range result.Rows {
ksids = append(ksids, row[0].Raw())
}
}
out = append(out, ksids)
}
return out, nil
}
// Verify returns true if ids maps to ksids.
func (lkp *lookup) Verify(vcursor VCursor, ids []interface{}, ksids [][]byte) (bool, error) {
var colBuff bytes.Buffer
var err error
if len(ids) != len(ksids) {
return false, fmt.Errorf("lookup.Verify:length of ids %v doesn't match length of ksids %v", len(ids), len(ksids))
}
val := make([]interface{}, len(ksids))
bindVars := make(map[string]interface{}, 2*len(ids))
colBuff.WriteString("(")
for rowNum, keyspaceID := range ksids {
fromStr := lkp.From + strconv.Itoa(rowNum)
toStr := lkp.To + strconv.Itoa(rowNum)
colBuff.WriteString("(")
colBuff.WriteString(lkp.From)
colBuff.WriteString("=:")
colBuff.WriteString(fromStr)
colBuff.WriteString(" and ")
colBuff.WriteString(lkp.To)
colBuff.WriteString("=:")
colBuff.WriteString(toStr)
colBuff.WriteString(")or")
if lkp.isHashedIndex {
val[rowNum], err = vunhash(keyspaceID)
if err != nil {
return false, fmt.Errorf("lookup.Verify: %v", err)
}
} else {
val[rowNum] = keyspaceID
}
bindVars[fromStr] = ids[rowNum]
bindVars[toStr] = val[rowNum]
}
lkp.ver = fmt.Sprintf("select %s from %s where %s", lkp.From, lkp.Table, strings.Trim(colBuff.String(), "or")+")")
result, err := vcursor.Execute(lkp.ver, bindVars)
if err != nil {
return false, fmt.Errorf("lookup.Verify: %v", err)
}
if len(result.Rows) != len(ids) {
return false, nil
}
return true, nil
}
// Create creates an association between ids and ksids by inserting a row in the vindex table.
func (lkp *lookup) Create(vcursor VCursor, ids []interface{}, ksids [][]byte) error {
var insBuffer bytes.Buffer
var err error
if len(ids) != len(ksids) {
return fmt.Errorf("lookup.Create:length of ids %v doesn't match length of ksids %v", len(ids), len(ksids))
}
val := make([]interface{}, len(ksids))
insBuffer.WriteString("insert into ")
insBuffer.WriteString(lkp.Table)
insBuffer.WriteString("(")
insBuffer.WriteString(lkp.From)
insBuffer.WriteString(",")
insBuffer.WriteString(lkp.To)
insBuffer.WriteString(") values")
bindVars := make(map[string]interface{}, 2*len(ids))
for rowNum, keyspaceID := range ksids {
fromStr := lkp.From + strconv.Itoa(rowNum)
toStr := lkp.To + strconv.Itoa(rowNum)
insBuffer.WriteString("(:")
insBuffer.WriteString(fromStr + ",:" + toStr)
insBuffer.WriteString("),")
if lkp.isHashedIndex {
val[rowNum], err = vunhash(keyspaceID)
if err != nil {
return fmt.Errorf("lookup.Create: %v", err)
}
} else {
val[rowNum] = keyspaceID
}
bindVars[fromStr] = ids[rowNum]
bindVars[toStr] = val[rowNum]
}
lkp.ins = strings.Trim(insBuffer.String(), ",")
if _, err := vcursor.Execute(lkp.ins, bindVars); err != nil {
return fmt.Errorf("lookup.Create: %v", err)
}
return nil
}
// Delete deletes the association between ids and ksid.
func (lkp *lookup) Delete(vcursor VCursor, ids []interface{}, ksid []byte) error {
var val interface{}
var err error
if lkp.isHashedIndex {
val, err = vunhash(ksid)
if err != nil {
return fmt.Errorf("lookup.Delete: %v", err)
}
} else {
val = ksid
}
bindvars := map[string]interface{}{
lkp.To: val,
}
for _, id := range ids {
bindvars[lkp.From] = id
if _, err := vcursor.Execute(lkp.del, bindvars); err != nil {
return fmt.Errorf("lookup.Delete: %v", err)
}
}
return nil
}