vitess.io/vitess@v0.16.2/go/vt/vtgate/vindexes/unicodeloosexxhash.go (about)

     1  /*
     2  Copyright 2020 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package vindexes
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"fmt"
    23  
    24  	"vitess.io/vitess/go/sqltypes"
    25  	"vitess.io/vitess/go/vt/key"
    26  )
    27  
    28  var (
    29  	_ SingleColumn = (*UnicodeLooseXXHash)(nil)
    30  	_ Hashing      = (*UnicodeLooseXXHash)(nil)
    31  )
    32  
    33  // UnicodeLooseXXHash is a vindex that normalizes and hashes unicode strings
    34  // to a keyspace id. It conservatively converts the string to its base
    35  // characters before hashing. This is also known as UCA level 1.
    36  // Ref: http://www.unicode.org/reports/tr10/#Multi_Level_Comparison.
    37  // This is compatible with MySQL's utf8_unicode_ci collation.
    38  type UnicodeLooseXXHash struct {
    39  	name string
    40  }
    41  
    42  // NewUnicodeLooseXXHash creates a new UnicodeLooseXXHash struct.
    43  func NewUnicodeLooseXXHash(name string, _ map[string]string) (Vindex, error) {
    44  	return &UnicodeLooseXXHash{name: name}, nil
    45  }
    46  
    47  // String returns the name of the vindex.
    48  func (vind *UnicodeLooseXXHash) String() string {
    49  	return vind.name
    50  }
    51  
    52  // Cost returns the cost as 1.
    53  func (vind *UnicodeLooseXXHash) Cost() int {
    54  	return 1
    55  }
    56  
    57  // IsUnique returns true since the Vindex is unique.
    58  func (vind *UnicodeLooseXXHash) IsUnique() bool {
    59  	return true
    60  }
    61  
    62  // NeedsVCursor satisfies the Vindex interface.
    63  func (vind *UnicodeLooseXXHash) NeedsVCursor() bool {
    64  	return false
    65  }
    66  
    67  // Verify returns true if ids maps to ksids.
    68  func (vind *UnicodeLooseXXHash) Verify(ctx context.Context, vcursor VCursor, ids []sqltypes.Value, ksids [][]byte) ([]bool, error) {
    69  	out := make([]bool, 0, len(ids))
    70  	for i, id := range ids {
    71  		data, err := vind.Hash(id)
    72  		if err != nil {
    73  			return nil, fmt.Errorf("UnicodeLooseXXHash.Verify: %v", err)
    74  		}
    75  		out = append(out, bytes.Equal(data, ksids[i]))
    76  	}
    77  	return out, nil
    78  }
    79  
    80  // Map can map ids to key.Destination objects.
    81  func (vind *UnicodeLooseXXHash) Map(ctx context.Context, vcursor VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
    82  	out := make([]key.Destination, 0, len(ids))
    83  	for _, id := range ids {
    84  		data, err := vind.Hash(id)
    85  		if err != nil {
    86  			return nil, fmt.Errorf("UnicodeLooseXXHash.Map: %v", err)
    87  		}
    88  		out = append(out, key.DestinationKeyspaceID(data))
    89  	}
    90  	return out, nil
    91  }
    92  
    93  func (vind *UnicodeLooseXXHash) Hash(id sqltypes.Value) ([]byte, error) {
    94  	return unicodeHash(vXXHash, id)
    95  }
    96  
    97  func init() {
    98  	Register("unicode_loose_xxhash", NewUnicodeLooseXXHash)
    99  }