-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
numeric_static_map.go
148 lines (126 loc) · 3.43 KB
/
numeric_static_map.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
/*
Copyright 2019 The Vitess Authors.
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 (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"io/ioutil"
"strconv"
"vitess.io/vitess/go/vt/vtgate/evalengine"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
)
var (
_ SingleColumn = (*NumericStaticMap)(nil)
)
// NumericLookupTable stores the mapping of keys.
type NumericLookupTable map[uint64]uint64
// NumericStaticMap is similar to vindex Numeric but first attempts a lookup via
// a JSON file.
type NumericStaticMap struct {
name string
lookup NumericLookupTable
}
func init() {
Register("numeric_static_map", NewNumericStaticMap)
}
// NewNumericStaticMap creates a NumericStaticMap vindex.
func NewNumericStaticMap(name string, params map[string]string) (Vindex, error) {
jsonPath, ok := params["json_path"]
if !ok {
return nil, errors.New("NumericStaticMap: Could not find `json_path` param in vschema")
}
lt, err := loadNumericLookupTable(jsonPath)
if err != nil {
return nil, err
}
return &NumericStaticMap{
name: name,
lookup: lt,
}, nil
}
// String returns the name of the vindex.
func (vind *NumericStaticMap) String() string {
return vind.name
}
// Cost returns the cost of this vindex as 1.
func (*NumericStaticMap) Cost() int {
return 1
}
// IsUnique returns true since the Vindex is unique.
func (vind *NumericStaticMap) IsUnique() bool {
return true
}
// NeedsVCursor satisfies the Vindex interface.
func (vind *NumericStaticMap) NeedsVCursor() bool {
return false
}
// Verify returns true if ids and ksids match.
func (vind *NumericStaticMap) Verify(_ VCursor, ids []sqltypes.Value, ksids [][]byte) ([]bool, error) {
out := make([]bool, len(ids))
for i := range ids {
var keybytes [8]byte
num, err := evalengine.ToUint64(ids[i])
if err != nil {
return nil, err
}
lookupNum, ok := vind.lookup[num]
if ok {
num = lookupNum
}
binary.BigEndian.PutUint64(keybytes[:], num)
out[i] = bytes.Equal(keybytes[:], ksids[i])
}
return out, nil
}
// Map can map ids to key.Destination objects.
func (vind *NumericStaticMap) Map(cursor VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
out := make([]key.Destination, 0, len(ids))
for _, id := range ids {
num, err := evalengine.ToUint64(id)
if err != nil {
out = append(out, key.DestinationNone{})
continue
}
lookupNum, ok := vind.lookup[num]
if ok {
num = lookupNum
}
var keybytes [8]byte
binary.BigEndian.PutUint64(keybytes[:], num)
out = append(out, key.DestinationKeyspaceID(keybytes[:]))
}
return out, nil
}
func loadNumericLookupTable(path string) (NumericLookupTable, error) {
var m map[string]uint64
lt := make(map[uint64]uint64)
data, err := ioutil.ReadFile(path)
if err != nil {
return lt, err
}
err = json.Unmarshal(data, &m)
if err != nil {
return lt, err
}
for k, v := range m {
newK, err := strconv.ParseUint(k, 10, 64)
if err != nil {
return lt, err
}
lt[newK] = v
}
return lt, nil
}