github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/master/shardddl/info.go (about) 1 // Copyright 2020 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package shardddl 15 16 import ( 17 "sort" 18 19 "github.com/pingcap/tiflow/dm/pkg/shardddl/pessimism" 20 ) 21 22 // PessimismInfoSlice attaches the methods of Interface to []pessimism.Info, 23 // sorting in increasing order according to `Source` field. 24 type PessimismInfoSlice []pessimism.Info 25 26 // Len implements Sorter.Len. 27 func (p PessimismInfoSlice) Len() int { 28 return len(p) 29 } 30 31 // Less implements Sorter.Less. 32 func (p PessimismInfoSlice) Less(i, j int) bool { 33 return p[i].Source < p[j].Source 34 } 35 36 // Swap implements Sorter.Swap. 37 func (p PessimismInfoSlice) Swap(i, j int) { 38 p[i], p[j] = p[j], p[i] 39 } 40 41 // pessimismInfoMapToSlice converts a `map[string]pessimism.Info` to `[]pessimism.Info` in increasing order according to `Source` field. 42 func pessimismInfoMapToSlice(ifm map[string]pessimism.Info) []pessimism.Info { 43 ret := make(PessimismInfoSlice, 0, len(ifm)) 44 for _, info := range ifm { 45 ret = append(ret, info) 46 } 47 sort.Sort(ret) 48 return ret 49 }