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

     1  /*
     2  Copyright 2019 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 = (*Binary)(nil)
    30  	_ Reversible   = (*Binary)(nil)
    31  	_ Hashing      = (*Binary)(nil)
    32  )
    33  
    34  // Binary is a vindex that converts binary bits to a keyspace id.
    35  type Binary struct {
    36  	name string
    37  }
    38  
    39  // NewBinary creates a new Binary.
    40  func NewBinary(name string, _ map[string]string) (Vindex, error) {
    41  	return &Binary{name: name}, nil
    42  }
    43  
    44  // String returns the name of the vindex.
    45  func (vind *Binary) String() string {
    46  	return vind.name
    47  }
    48  
    49  // Cost returns the cost as 1.
    50  func (vind *Binary) Cost() int {
    51  	return 0
    52  }
    53  
    54  // IsUnique returns true since the Vindex is unique.
    55  func (vind *Binary) IsUnique() bool {
    56  	return true
    57  }
    58  
    59  // NeedsVCursor satisfies the Vindex interface.
    60  func (vind *Binary) NeedsVCursor() bool {
    61  	return false
    62  }
    63  
    64  // Verify returns true if ids maps to ksids.
    65  func (vind *Binary) Verify(ctx context.Context, vcursor VCursor, ids []sqltypes.Value, ksids [][]byte) ([]bool, error) {
    66  	out := make([]bool, 0, len(ids))
    67  	for i, id := range ids {
    68  		idBytes, err := vind.Hash(id)
    69  		if err != nil {
    70  			return out, err
    71  		}
    72  		out = append(out, bytes.Equal(idBytes, ksids[i]))
    73  	}
    74  	return out, nil
    75  }
    76  
    77  // Map can map ids to key.Destination objects.
    78  func (vind *Binary) Map(ctx context.Context, vcursor VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
    79  	out := make([]key.Destination, 0, len(ids))
    80  	for _, id := range ids {
    81  		idBytes, err := vind.Hash(id)
    82  		if err != nil {
    83  			return out, err
    84  		}
    85  		out = append(out, key.DestinationKeyspaceID(idBytes))
    86  	}
    87  	return out, nil
    88  }
    89  
    90  func (vind *Binary) Hash(id sqltypes.Value) ([]byte, error) {
    91  	return id.ToBytes()
    92  }
    93  
    94  // ReverseMap returns the associated ids for the ksids.
    95  func (*Binary) ReverseMap(_ VCursor, ksids [][]byte) ([]sqltypes.Value, error) {
    96  	var reverseIds = make([]sqltypes.Value, len(ksids))
    97  	for rownum, keyspaceID := range ksids {
    98  		if keyspaceID == nil {
    99  			return nil, fmt.Errorf("Binary.ReverseMap: keyspaceId is nil")
   100  		}
   101  		reverseIds[rownum] = sqltypes.MakeTrusted(sqltypes.VarBinary, keyspaceID)
   102  	}
   103  	return reverseIds, nil
   104  }
   105  
   106  func init() {
   107  	Register("binary", NewBinary)
   108  }