github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/resolver/map.go (about)

     1  /*
     2   *
     3   * Copyright 2021 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package resolver
    20  
    21  type addressMapEntry struct {
    22  	addr  Address
    23  	value interface{}
    24  }
    25  
    26  // AddressMap is a map of addresses to arbitrary values taking into account
    27  // Attributes.  BalancerAttributes are ignored, as are Metadata and Type.
    28  // Multiple accesses may not be performed concurrently.  Must be created via
    29  // NewAddressMap; do not construct directly.
    30  type AddressMap struct {
    31  	m map[string]addressMapEntryList
    32  }
    33  
    34  type addressMapEntryList []*addressMapEntry
    35  
    36  // NewAddressMap creates a new AddressMap.
    37  func NewAddressMap() *AddressMap {
    38  	return &AddressMap{m: make(map[string]addressMapEntryList)}
    39  }
    40  
    41  // find returns the index of addr in the addressMapEntry slice, or -1 if not
    42  // present.
    43  func (l addressMapEntryList) find(addr Address) int {
    44  	if len(l) == 0 {
    45  		return -1
    46  	}
    47  	for i, entry := range l {
    48  		if entry.addr.ServerName == addr.ServerName &&
    49  			entry.addr.Attributes.Equal(addr.Attributes) {
    50  			return i
    51  		}
    52  	}
    53  	return -1
    54  }
    55  
    56  // Get returns the value for the address in the map, if present.
    57  func (a *AddressMap) Get(addr Address) (value interface{}, ok bool) {
    58  	entryList := a.m[addr.Addr]
    59  	if entry := entryList.find(addr); entry != -1 {
    60  		return entryList[entry].value, true
    61  	}
    62  	return nil, false
    63  }
    64  
    65  // Set updates or adds the value to the address in the map.
    66  func (a *AddressMap) Set(addr Address, value interface{}) {
    67  	entryList := a.m[addr.Addr]
    68  	if entry := entryList.find(addr); entry != -1 {
    69  		a.m[addr.Addr][entry].value = value
    70  		return
    71  	}
    72  	a.m[addr.Addr] = append(a.m[addr.Addr], &addressMapEntry{addr: addr, value: value})
    73  }
    74  
    75  // Delete removes addr from the map.
    76  func (a *AddressMap) Delete(addr Address) {
    77  	entryList := a.m[addr.Addr]
    78  	entry := entryList.find(addr)
    79  	if entry == -1 {
    80  		return
    81  	}
    82  	if len(entryList) == 1 {
    83  		entryList = nil
    84  	} else {
    85  		copy(entryList[entry:], entryList[entry+1:])
    86  		entryList = entryList[:len(entryList)-1]
    87  	}
    88  	a.m[addr.Addr] = entryList
    89  }
    90  
    91  // Len returns the number of entries in the map.
    92  func (a *AddressMap) Len() int {
    93  	ret := 0
    94  	for _, entryList := range a.m {
    95  		ret += len(entryList)
    96  	}
    97  	return ret
    98  }
    99  
   100  // Keys returns a slice of all current map keys.
   101  func (a *AddressMap) Keys() []Address {
   102  	ret := make([]Address, 0, a.Len())
   103  	for _, entryList := range a.m {
   104  		for _, entry := range entryList {
   105  			ret = append(ret, entry.addr)
   106  		}
   107  	}
   108  	return ret
   109  }