vitess.io/vitess@v0.16.2/go/vt/topo/topoproto/destination.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  	"strings"
    22  
    23  	"vitess.io/vitess/go/vt/key"
    24  	"vitess.io/vitess/go/vt/vterrors"
    25  
    26  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    27  	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
    28  )
    29  
    30  // ParseDestination parses the string representation of a Destination
    31  // of the form keyspace:shard@tablet_type. You can use a / instead of a :.
    32  func ParseDestination(targetString string, defaultTabletType topodatapb.TabletType) (string, topodatapb.TabletType, key.Destination, error) {
    33  	var dest key.Destination
    34  	var keyspace string
    35  	tabletType := defaultTabletType
    36  
    37  	last := strings.LastIndexAny(targetString, "@")
    38  	if last != -1 {
    39  		// No need to check the error. UNKNOWN will be returned on
    40  		// error and it will fail downstream.
    41  		tabletType, _ = ParseTabletType(targetString[last+1:])
    42  		targetString = targetString[:last]
    43  	}
    44  	last = strings.LastIndexAny(targetString, "/:")
    45  	if last != -1 {
    46  		dest = key.DestinationShard(targetString[last+1:])
    47  		targetString = targetString[:last]
    48  	}
    49  	// Try to parse it as a keyspace id or range
    50  	last = strings.LastIndexAny(targetString, "[")
    51  	if last != -1 {
    52  		rangeEnd := strings.LastIndexAny(targetString, "]")
    53  		if rangeEnd == -1 {
    54  			return keyspace, tabletType, dest, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "invalid key range provided. Couldn't find range end ']'")
    55  		}
    56  		rangeString := targetString[last+1 : rangeEnd]
    57  		if strings.Contains(rangeString, "-") {
    58  			// Parse as range
    59  			keyRange, err := key.ParseShardingSpec(rangeString)
    60  			if err != nil {
    61  				return keyspace, tabletType, dest, err
    62  			}
    63  			if len(keyRange) != 1 {
    64  				return keyspace, tabletType, dest, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "single keyrange expected in %s", rangeString)
    65  			}
    66  			dest = key.DestinationExactKeyRange{KeyRange: keyRange[0]}
    67  		} else {
    68  			// Parse as keyspace id
    69  			destBytes, err := hex.DecodeString(rangeString)
    70  			if err != nil {
    71  				return keyspace, tabletType, dest, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "expected valid hex in keyspace id %s", rangeString)
    72  			}
    73  			dest = key.DestinationKeyspaceID(destBytes)
    74  		}
    75  		targetString = targetString[:last]
    76  	}
    77  	keyspace = targetString
    78  	return keyspace, tabletType, dest, nil
    79  }