blob: e87ecd0eeb38e380dfaad773aeb1c992717570ed [file] [log] [blame]
khenaidoo5cb0d402021-12-08 14:09:16 -05001/*
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
19package resolver
20
21type 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.
30type AddressMap struct {
31 m map[string]addressMapEntryList
32}
33
34type addressMapEntryList []*addressMapEntry
35
36// NewAddressMap creates a new AddressMap.
37func 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.
43func (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.
57func (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.
66func (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.
76func (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.
92func (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.
101func (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}