vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletmanager/vdiff/utils.go (about) 1 /* 2 Copyright 2022 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 vdiff 18 19 import ( 20 "context" 21 "fmt" 22 "strings" 23 24 "vitess.io/vitess/go/vt/binlog/binlogplayer" 25 "vitess.io/vitess/go/vt/log" 26 27 "vitess.io/vitess/go/sqltypes" 28 29 "vitess.io/vitess/go/mysql/collations" 30 "vitess.io/vitess/go/vt/vtgate/engine" 31 ) 32 33 // newMergeSorter creates an engine.MergeSort based on the shard streamers and pk columns 34 func newMergeSorter(participants map[string]*shardStreamer, comparePKs []compareColInfo) *engine.MergeSort { 35 prims := make([]engine.StreamExecutor, 0, len(participants)) 36 for _, participant := range participants { 37 prims = append(prims, participant) 38 } 39 ob := make([]engine.OrderByParams, len(comparePKs)) 40 for i, cpk := range comparePKs { 41 weightStringCol := -1 42 // if the collation is nil or unknown, use binary collation to compare as bytes 43 if cpk.collation == nil { 44 ob[i] = engine.OrderByParams{Col: cpk.colIndex, WeightStringCol: weightStringCol, CollationID: collations.CollationBinaryID} 45 } else { 46 ob[i] = engine.OrderByParams{Col: cpk.colIndex, WeightStringCol: weightStringCol, CollationID: cpk.collation.ID()} 47 } 48 } 49 return &engine.MergeSort{ 50 Primitives: prims, 51 OrderBy: ob, 52 } 53 } 54 55 //----------------------------------------------------------------- 56 // Utility functions 57 58 func encodeString(in string) string { 59 var buf strings.Builder 60 sqltypes.NewVarChar(in).EncodeSQL(&buf) 61 return buf.String() 62 } 63 64 func pkColsToGroupByParams(pkCols []int) []*engine.GroupByParams { 65 var res []*engine.GroupByParams 66 for _, col := range pkCols { 67 res = append(res, &engine.GroupByParams{KeyCol: col, WeightStringCol: -1}) 68 } 69 return res 70 } 71 72 func insertVDiffLog(ctx context.Context, dbClient binlogplayer.DBClient, vdiffID int64, message string) { 73 query := "insert into _vt.vdiff_log(vdiff_id, message) values (%d, %s)" 74 query = fmt.Sprintf(query, vdiffID, encodeString(message)) 75 if _, err := dbClient.ExecuteFetch(query, 1); err != nil { 76 log.Error("Error inserting into _vt.vdiff_log: %v", err) 77 } 78 } 79 80 func stringListContains(lst []string, item string) bool { 81 contains := false 82 for _, t := range lst { 83 if t == item { 84 contains = true 85 break 86 } 87 } 88 return contains 89 }