vitess.io/vitess@v0.16.2/go/vt/topo/topoproto/srvkeyspace.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 topoproto
    18  
    19  import (
    20  	"bytes"
    21  	"sort"
    22  
    23  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    24  )
    25  
    26  // ShardReferenceArray is used for sorting ShardReference arrays
    27  type ShardReferenceArray []*topodatapb.ShardReference
    28  
    29  // Len implements sort.Interface
    30  func (sra ShardReferenceArray) Len() int { return len(sra) }
    31  
    32  // Len implements sort.Interface
    33  func (sra ShardReferenceArray) Less(i, j int) bool {
    34  	if sra[i].KeyRange == nil || len(sra[i].KeyRange.Start) == 0 {
    35  		return true
    36  	}
    37  	if sra[j].KeyRange == nil || len(sra[j].KeyRange.Start) == 0 {
    38  		return false
    39  	}
    40  	return bytes.Compare(sra[i].KeyRange.Start, sra[j].KeyRange.Start) < 0
    41  }
    42  
    43  // Len implements sort.Interface
    44  func (sra ShardReferenceArray) Swap(i, j int) {
    45  	sra[i], sra[j] = sra[j], sra[i]
    46  }
    47  
    48  // Sort will sort the list according to KeyRange.Start
    49  func (sra ShardReferenceArray) Sort() { sort.Sort(sra) }
    50  
    51  // SrvKeyspaceGetPartition returns a Partition for the given tablet type,
    52  // or nil if it's not there.
    53  func SrvKeyspaceGetPartition(sk *topodatapb.SrvKeyspace, tabletType topodatapb.TabletType) *topodatapb.SrvKeyspace_KeyspacePartition {
    54  	for _, p := range sk.Partitions {
    55  		if p.ServedType == tabletType {
    56  			return p
    57  		}
    58  	}
    59  	return nil
    60  }