vitess.io/vitess@v0.16.2/go/vt/topo/topoproto/shard.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  	"encoding/hex"
    21  	"fmt"
    22  	"html/template"
    23  	"strings"
    24  
    25  	"vitess.io/vitess/go/vt/key"
    26  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    27  )
    28  
    29  // KeyspaceShardString returns a "keyspace/shard" string taking
    30  // keyspace and shard as separate inputs.
    31  func KeyspaceShardString(keyspace, shard string) string {
    32  	return fmt.Sprintf("%v/%v", keyspace, shard)
    33  }
    34  
    35  // ParseKeyspaceShard parse a "keyspace/shard" or "keyspace:shard"
    36  // string and extract both keyspace and shard
    37  func ParseKeyspaceShard(param string) (string, string, error) {
    38  	keySpaceShard := strings.Split(param, "/")
    39  	if len(keySpaceShard) != 2 {
    40  		keySpaceShard = strings.Split(param, ":")
    41  		if len(keySpaceShard) != 2 {
    42  			return "", "", fmt.Errorf("invalid shard path: %v", param)
    43  		}
    44  	}
    45  	return keySpaceShard[0], keySpaceShard[1], nil
    46  }
    47  
    48  // SourceShardString returns a printable view of a SourceShard.
    49  func SourceShardString(source *topodatapb.Shard_SourceShard) string {
    50  	return fmt.Sprintf("SourceShard(%v,%v/%v)", source.Uid, source.Keyspace, source.Shard)
    51  }
    52  
    53  // SourceShardAsHTML returns a HTML version of the object.
    54  func SourceShardAsHTML(source *topodatapb.Shard_SourceShard) template.HTML {
    55  	result := fmt.Sprintf("<b>Uid</b>: %v</br>\n<b>Source</b>: %v/%v</br>\n", source.Uid, source.Keyspace, source.Shard)
    56  	if key.KeyRangeIsPartial(source.KeyRange) {
    57  		result += fmt.Sprintf("<b>KeyRange</b>: %v-%v</br>\n",
    58  			hex.EncodeToString(source.KeyRange.Start),
    59  			hex.EncodeToString(source.KeyRange.End))
    60  	}
    61  	if len(source.Tables) > 0 {
    62  		result += fmt.Sprintf("<b>Tables</b>: %v</br>\n",
    63  			strings.Join(source.Tables, " "))
    64  	}
    65  	return template.HTML(result)
    66  }