-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
unicode.go
93 lines (77 loc) · 2.82 KB
/
unicode.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
/*
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"
"fmt"
"sync"
"unicode/utf8"
"vitess.io/vitess/go/sqltypes"
"golang.org/x/text/collate"
"golang.org/x/text/language"
)
// Shared functions for Unicode string normalization
// for Vindexes.
func unicodeHash(hashFunc func([]byte) []byte, key sqltypes.Value) ([]byte, error) {
collator := collatorPool.Get().(*pooledCollator)
defer collatorPool.Put(collator)
keyBytes, err := key.ToBytes()
if err != nil {
return nil, err
}
norm, err := normalize(collator.col, collator.buf, keyBytes)
if err != nil {
return nil, err
}
return hashFunc(norm), nil
}
func normalize(col *collate.Collator, buf *collate.Buffer, in []byte) ([]byte, error) {
// We cannot pass invalid UTF-8 to the collator.
if !utf8.Valid(in) {
return nil, fmt.Errorf("cannot normalize string containing invalid UTF-8: %q", string(in))
}
// Ref: http://dev.mysql.com/doc/refman/5.6/en/char.html.
// Trailing spaces are ignored by MySQL.
in = bytes.TrimRight(in, " ")
// We use the collation key which can be used to
// perform lexical comparisons.
return col.Key(buf, in), nil
}
// pooledCollator pairs a Collator and a Buffer.
// These pairs are pooled to avoid reallocating for every request,
// which would otherwise be required because they can't be used concurrently.
//
// Note that you must ensure no active references into the buffer remain
// before you return this pair back to the pool.
// That is, either do your processing on the result first, or make a copy.
type pooledCollator struct {
col *collate.Collator
buf *collate.Buffer
}
var collatorPool = sync.Pool{New: newPooledCollator}
func newPooledCollator() any {
// Ref: http://www.unicode.org/reports/tr10/#Introduction.
// Unicode seems to define a universal (or default) order.
// But various locales have conflicting order,
// which they have the right to override.
// Unfortunately, the Go library requires you to specify a locale.
// So, I chose English assuming that it won't override
// the Unicode universal order. But I couldn't find an easy
// way to verify this.
// Also, the locale differences are not an issue for level 1,
// because the conservative comparison makes them all equal.
return &pooledCollator{
col: collate.New(language.English, collate.Loose),
buf: new(collate.Buffer),
}
}