/
region_experimental.go
137 lines (118 loc) · 3.66 KB
/
region_experimental.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
/*
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"
"fmt"
"vitess.io/vitess/go/vt/vtgate/evalengine"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
)
var (
_ MultiColumn = (*RegionExperimental)(nil)
)
func init() {
Register("region_experimental", NewRegionExperimental)
}
// RegionExperimental is a multi-column unique vindex. The first column is prefixed
// to the hash of the second column to produce the keyspace id.
// RegionExperimental can be used for geo-partitioning because the first column can denote a region,
// and its value will dictate the shard for that region.
type RegionExperimental struct {
name string
regionBytes int
}
// NewRegionExperimental creates a RegionExperimental vindex.
// The supplied map requires all the fields of "consistent_lookup_unique".
// Additionally, it requires a region_bytes argument whose value can be "1", or "2".
func NewRegionExperimental(name string, m map[string]string) (Vindex, error) {
rbs, ok := m["region_bytes"]
if !ok {
return nil, fmt.Errorf("region_experimental missing region_bytes param")
}
var rb int
switch rbs {
case "1":
rb = 1
case "2":
rb = 2
default:
return nil, fmt.Errorf("region_bits must be 1 or 2: %v", rbs)
}
return &RegionExperimental{
name: name,
regionBytes: rb,
}, nil
}
// String returns the name of the vindex.
func (ge *RegionExperimental) String() string {
return ge.name
}
// Cost returns the cost of this index as 1.
func (ge *RegionExperimental) Cost() int {
return 1
}
// IsUnique returns true since the Vindex is unique.
func (ge *RegionExperimental) IsUnique() bool {
return true
}
// NeedsVCursor satisfies the Vindex interface.
func (ge *RegionExperimental) NeedsVCursor() bool {
return false
}
// Map satisfies MultiColumn.
func (ge *RegionExperimental) Map(vcursor VCursor, rowsColValues [][]sqltypes.Value) ([]key.Destination, error) {
destinations := make([]key.Destination, 0, len(rowsColValues))
for _, row := range rowsColValues {
if len(row) != 2 {
destinations = append(destinations, key.DestinationNone{})
continue
}
// Compute region prefix.
rn, err := evalengine.ToUint64(row[0])
if err != nil {
destinations = append(destinations, key.DestinationNone{})
continue
}
r := make([]byte, 2, 2+8)
binary.BigEndian.PutUint16(r, uint16(rn))
// Compute hash.
hn, err := evalengine.ToUint64(row[1])
if err != nil {
destinations = append(destinations, key.DestinationNone{})
continue
}
h := vhash(hn)
// Concatenate and add to destinations.
if ge.regionBytes == 1 {
r = r[1:]
}
dest := append(r, h...)
destinations = append(destinations, key.DestinationKeyspaceID(dest))
}
return destinations, nil
}
// Verify satisfies MultiColumn.
func (ge *RegionExperimental) Verify(vcursor VCursor, rowsColValues [][]sqltypes.Value, ksids [][]byte) ([]bool, error) {
result := make([]bool, len(rowsColValues))
destinations, _ := ge.Map(vcursor, rowsColValues)
for i, dest := range destinations {
destksid, ok := dest.(key.DestinationKeyspaceID)
if !ok {
continue
}
result[i] = bytes.Equal([]byte(destksid), ksids[i])
}
return result, nil
}