/
unicodeloosexxhash.go
109 lines (91 loc) · 3.07 KB
/
unicodeloosexxhash.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
/*
Copyright 2020 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"
"context"
"fmt"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
)
var (
_ SingleColumn = (*UnicodeLooseXXHash)(nil)
_ Hashing = (*UnicodeLooseXXHash)(nil)
_ ParamValidating = (*UnicodeLooseXXHash)(nil)
)
// UnicodeLooseXXHash is a vindex that normalizes and hashes unicode strings
// to a keyspace id. It conservatively converts the string to its base
// characters before hashing. This is also known as UCA level 1.
// Ref: http://www.unicode.org/reports/tr10/#Multi_Level_Comparison.
// This is compatible with MySQL's utf8_unicode_ci collation.
type UnicodeLooseXXHash struct {
name string
unknownParams []string
}
// newUnicodeLooseXXHash creates a new UnicodeLooseXXHash struct.
func newUnicodeLooseXXHash(name string, m map[string]string) (Vindex, error) {
return &UnicodeLooseXXHash{
name: name,
unknownParams: FindUnknownParams(m, nil),
}, nil
}
// String returns the name of the vindex.
func (vind *UnicodeLooseXXHash) String() string {
return vind.name
}
// Cost returns the cost as 1.
func (vind *UnicodeLooseXXHash) Cost() int {
return 1
}
// IsUnique returns true since the Vindex is unique.
func (vind *UnicodeLooseXXHash) IsUnique() bool {
return true
}
// NeedsVCursor satisfies the Vindex interface.
func (vind *UnicodeLooseXXHash) NeedsVCursor() bool {
return false
}
// Verify returns true if ids maps to ksids.
func (vind *UnicodeLooseXXHash) Verify(ctx context.Context, vcursor VCursor, ids []sqltypes.Value, ksids [][]byte) ([]bool, error) {
out := make([]bool, 0, len(ids))
for i, id := range ids {
data, err := vind.Hash(id)
if err != nil {
return nil, fmt.Errorf("UnicodeLooseXXHash.Verify: %v", err)
}
out = append(out, bytes.Equal(data, ksids[i]))
}
return out, nil
}
// Map can map ids to key.Destination objects.
func (vind *UnicodeLooseXXHash) Map(ctx context.Context, vcursor VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
out := make([]key.Destination, 0, len(ids))
for _, id := range ids {
data, err := vind.Hash(id)
if err != nil {
return nil, fmt.Errorf("UnicodeLooseXXHash.Map: %v", err)
}
out = append(out, key.DestinationKeyspaceID(data))
}
return out, nil
}
func (vind *UnicodeLooseXXHash) Hash(id sqltypes.Value) ([]byte, error) {
return unicodeHash(&collateXX, id)
}
// UnknownParams implements the ParamValidating interface.
func (vind *UnicodeLooseXXHash) UnknownParams() []string {
return vind.unknownParams
}
func init() {
Register("unicode_loose_xxhash", newUnicodeLooseXXHash)
}