vitess.io/vitess@v0.16.2/go/vt/vtgate/vindexes/null.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 23 "vitess.io/vitess/go/sqltypes" 24 "vitess.io/vitess/go/vt/key" 25 ) 26 27 var ( 28 _ Vindex = (*Null)(nil) 29 nullksid = []byte{0} 30 ) 31 32 // Null defines a vindex that always return 0. It's Unique and 33 // Functional. 34 // This is useful for rows that always go into the first shard. 35 // This Vindex can be used for validating an unsharded->sharded transition. 36 // Unlike other vindexes, this one will work even for NULL input values. This 37 // will allow you to keep MySQL auto-inc columns unchanged. 38 type Null struct { 39 name string 40 } 41 42 // NewNull creates a new Null. 43 func NewNull(name string, m map[string]string) (Vindex, error) { 44 return &Null{name: name}, nil 45 } 46 47 // String returns the name of the vindex. 48 func (vind *Null) String() string { 49 return vind.name 50 } 51 52 // Cost returns the cost of this index as 100. 53 func (vind *Null) Cost() int { 54 return 100 55 } 56 57 // IsUnique returns true since the Vindex is unique. 58 func (vind *Null) IsUnique() bool { 59 return true 60 } 61 62 // NeedsVCursor satisfies the Vindex interface. 63 func (vind *Null) NeedsVCursor() bool { 64 return false 65 } 66 67 // Map can map ids to key.Destination objects. 68 func (vind *Null) Map(ctx context.Context, vcursor VCursor, ids []sqltypes.Value) ([]key.Destination, error) { 69 out := make([]key.Destination, 0, len(ids)) 70 for i := 0; i < len(ids); i++ { 71 out = append(out, key.DestinationKeyspaceID(nullksid)) 72 } 73 return out, nil 74 } 75 76 // Verify returns true if ids maps to ksids. 77 func (vind *Null) Verify(ctx context.Context, vcursor VCursor, ids []sqltypes.Value, ksids [][]byte) ([]bool, error) { 78 out := make([]bool, len(ids)) 79 for i := range ids { 80 out[i] = bytes.Equal(nullksid, ksids[i]) 81 } 82 return out, nil 83 } 84 85 func init() { 86 Register("null", NewNull) 87 }