vitess.io/vitess@v0.16.2/go/vt/vtctl/reparentutil/promotionrule/promotion_rule.go (about)

     1  /*
     2     Copyright 2014 Outbrain Inc.
     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 promotionrule
    18  
    19  import (
    20  	"fmt"
    21  )
    22  
    23  // CandidatePromotionRule describe the promotion preference/rule for an instance.
    24  // It maps to promotion_rule column in candidate_database_instance
    25  type CandidatePromotionRule string
    26  
    27  const (
    28  	Must      CandidatePromotionRule = "must"
    29  	Prefer    CandidatePromotionRule = "prefer"
    30  	Neutral   CandidatePromotionRule = "neutral"
    31  	PreferNot CandidatePromotionRule = "prefer_not"
    32  	MustNot   CandidatePromotionRule = "must_not"
    33  )
    34  
    35  var promotionRuleOrderMap = map[CandidatePromotionRule]int{
    36  	Must:      0,
    37  	Prefer:    1,
    38  	Neutral:   2,
    39  	PreferNot: 3,
    40  	MustNot:   4,
    41  }
    42  
    43  // AllPromotionRules returns all the CandidatePromotionRules in a list
    44  // sorted by their priority.
    45  func AllPromotionRules() []CandidatePromotionRule {
    46  	return []CandidatePromotionRule{Must, Prefer, Neutral, PreferNot, MustNot}
    47  }
    48  
    49  func (this *CandidatePromotionRule) BetterThan(other CandidatePromotionRule) bool {
    50  	otherOrder, ok := promotionRuleOrderMap[other]
    51  	if !ok {
    52  		return false
    53  	}
    54  	return promotionRuleOrderMap[*this] < otherOrder
    55  }
    56  
    57  // Parse returns a CandidatePromotionRule by name.
    58  // It returns an error if there is no known rule by the given name.
    59  func Parse(ruleName string) (CandidatePromotionRule, error) {
    60  	switch ruleName {
    61  	case "prefer", "neutral", "prefer_not", "must_not":
    62  		return CandidatePromotionRule(ruleName), nil
    63  	case "must":
    64  		return CandidatePromotionRule(""), fmt.Errorf("CandidatePromotionRule: %v not supported yet", ruleName)
    65  	default:
    66  		return CandidatePromotionRule(""), fmt.Errorf("Invalid CandidatePromotionRule: %v", ruleName)
    67  	}
    68  }