vitess.io/vitess@v0.16.2/go/vt/vttest/shard_name.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 vttest
    18  
    19  import (
    20  	"fmt"
    21  )
    22  
    23  // GetShardName returns an appropriate shard name, as a string.
    24  // A single shard name is simply 0; otherwise it will attempt to split up 0x100
    25  // into multiple shards.  For example, in a two sharded keyspace, shard 0 is
    26  // -80, shard 1 is 80-.  This function currently only applies to sharding setups
    27  // where the shard count is 256 or less, and all shards are equal width.
    28  func GetShardName(shard, total int) string {
    29  	width := 0x100 / total
    30  	switch {
    31  	case total == 1:
    32  		return "0"
    33  	case shard == 0:
    34  		return fmt.Sprintf("-%02x", width)
    35  	case shard == total-1:
    36  		return fmt.Sprintf("%02x-", shard*width)
    37  	default:
    38  		return fmt.Sprintf("%02x-%02x", shard*width, (shard+1)*width)
    39  	}
    40  }
    41  
    42  // GetShardNames creates a slice of shard names for N shards
    43  func GetShardNames(total int) (names []string) {
    44  	for i := 0; i < total; i++ {
    45  		names = append(names, GetShardName(i, total))
    46  	}
    47  	return
    48  }