vitess.io/vitess@v0.16.2/go/vt/vtorc/inst/instance_key_map.go (about)

     1  /*
     2     Copyright 2015 Shlomi Noach, courtesy Booking.com
     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 inst
    18  
    19  import (
    20  	"encoding/json"
    21  	"sort"
    22  	"strings"
    23  )
    24  
    25  // InstanceKeyMap is a convenience struct for listing InstanceKey-s
    26  type InstanceKeyMap map[InstanceKey]bool
    27  
    28  func NewInstanceKeyMap() *InstanceKeyMap {
    29  	return &InstanceKeyMap{}
    30  }
    31  
    32  // AddKey adds a single key to this map
    33  func (instanceKeyMap *InstanceKeyMap) AddKey(key InstanceKey) {
    34  	(*instanceKeyMap)[key] = true
    35  }
    36  
    37  // AddKeys adds all given keys to this map
    38  func (instanceKeyMap *InstanceKeyMap) AddKeys(keys []InstanceKey) {
    39  	for _, key := range keys {
    40  		instanceKeyMap.AddKey(key)
    41  	}
    42  }
    43  
    44  // AddInstances adds keys of all given instances to this map
    45  func (instanceKeyMap *InstanceKeyMap) AddInstances(instances [](*Instance)) {
    46  	for _, instance := range instances {
    47  		instanceKeyMap.AddKey(instance.Key)
    48  	}
    49  }
    50  
    51  // HasKey checks if given key is within the map
    52  func (instanceKeyMap *InstanceKeyMap) HasKey(key InstanceKey) bool {
    53  	_, ok := (*instanceKeyMap)[key]
    54  	return ok
    55  }
    56  
    57  // GetInstanceKeys returns keys in this map in the form of an array
    58  func (instanceKeyMap *InstanceKeyMap) GetInstanceKeys() []InstanceKey {
    59  	res := []InstanceKey{}
    60  	for key := range *instanceKeyMap {
    61  		res = append(res, key)
    62  	}
    63  	sort.Slice(res, func(i, j int) bool {
    64  		return res[i].Hostname < res[j].Hostname || res[i].Hostname == res[j].Hostname && res[i].Port < res[j].Port
    65  	})
    66  	return res
    67  }
    68  
    69  // Intersect returns a keymap which is the intersection of this and another map
    70  func (instanceKeyMap *InstanceKeyMap) Intersect(other *InstanceKeyMap) *InstanceKeyMap {
    71  	intersected := NewInstanceKeyMap()
    72  	for key := range *other {
    73  		if instanceKeyMap.HasKey(key) {
    74  			intersected.AddKey(key)
    75  		}
    76  	}
    77  	return intersected
    78  }
    79  
    80  // MarshalJSON will marshal this map as JSON
    81  func (instanceKeyMap InstanceKeyMap) MarshalJSON() ([]byte, error) {
    82  	return json.Marshal(instanceKeyMap.GetInstanceKeys())
    83  }
    84  
    85  // UnmarshalJSON reds this object from JSON
    86  func (instanceKeyMap *InstanceKeyMap) UnmarshalJSON(b []byte) error {
    87  	var keys []InstanceKey
    88  	if err := json.Unmarshal(b, &keys); err != nil {
    89  		return err
    90  	}
    91  	*instanceKeyMap = make(InstanceKeyMap)
    92  	for _, key := range keys {
    93  		instanceKeyMap.AddKey(key)
    94  	}
    95  	return nil
    96  }
    97  
    98  // ToJSON will marshal this map as JSON
    99  func (instanceKeyMap *InstanceKeyMap) ToJSON() (string, error) {
   100  	bytes, err := instanceKeyMap.MarshalJSON()
   101  	return string(bytes), err
   102  }
   103  
   104  // ToJSONString will marshal this map as JSON
   105  func (instanceKeyMap *InstanceKeyMap) ToJSONString() string {
   106  	s, _ := instanceKeyMap.ToJSON()
   107  	return s
   108  }
   109  
   110  // ToCommaDelimitedList will export this map in comma delimited format
   111  func (instanceKeyMap *InstanceKeyMap) ToCommaDelimitedList() string {
   112  	keyDisplays := []string{}
   113  	for key := range *instanceKeyMap {
   114  		keyDisplays = append(keyDisplays, key.DisplayString())
   115  	}
   116  	return strings.Join(keyDisplays, ",")
   117  }
   118  
   119  // ReadJSON unmarshalls a json into this map
   120  func (instanceKeyMap *InstanceKeyMap) ReadJSON(jsonString string) error {
   121  	var keys []InstanceKey
   122  	err := json.Unmarshal([]byte(jsonString), &keys)
   123  	if err != nil {
   124  		return err
   125  	}
   126  	instanceKeyMap.AddKeys(keys)
   127  	return err
   128  }
   129  
   130  // ReadJSON unmarshalls a json into this map
   131  func (instanceKeyMap *InstanceKeyMap) ReadCommaDelimitedList(list string) error {
   132  	tokens := strings.Split(list, ",")
   133  	for _, token := range tokens {
   134  		key, err := ParseResolveInstanceKey(token)
   135  		if err != nil {
   136  			return err
   137  		}
   138  		instanceKeyMap.AddKey(*key)
   139  	}
   140  	return nil
   141  }