Skip to content

Commit 0317f77

Browse files
committed
libnetwork/internal/setmatrix: make keys generic
Make the SetMatrix key's type generic so that e.g. netip.Addr values can be used as matrix keys. Signed-off-by: Cory Snider <[email protected]>
1 parent 7ea613d commit 0317f77

6 files changed

Lines changed: 24 additions & 24 deletions

File tree

libnetwork/drivers/overlay/peerdb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (p *peerEntryDB) UnMarshalDB() peerEntry {
6262

6363
type peerMap struct {
6464
// set of peerEntry, note the values have to be objects and not pointers to maintain the proper equality checks
65-
mp setmatrix.SetMatrix[peerEntryDB]
65+
mp setmatrix.SetMatrix[string, peerEntryDB]
6666
sync.Mutex
6767
}
6868

libnetwork/internal/setmatrix/setmatrix.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import (
1313
// The zero value is an empty set matrix ready to use.
1414
//
1515
// SetMatrix values are safe for concurrent use.
16-
type SetMatrix[T comparable] struct {
17-
matrix map[string]mapset.Set[T]
16+
type SetMatrix[K, V comparable] struct {
17+
matrix map[K]mapset.Set[V]
1818

1919
mu sync.Mutex
2020
}
2121

2222
// Get returns the members of the set for a specific key as a slice.
23-
func (s *SetMatrix[T]) Get(key string) ([]T, bool) {
23+
func (s *SetMatrix[K, V]) Get(key K) ([]V, bool) {
2424
s.mu.Lock()
2525
defer s.mu.Unlock()
2626
set, ok := s.matrix[key]
@@ -31,7 +31,7 @@ func (s *SetMatrix[T]) Get(key string) ([]T, bool) {
3131
}
3232

3333
// Contains is used to verify if an element is in a set for a specific key.
34-
func (s *SetMatrix[T]) Contains(key string, value T) (containsElement, setExists bool) {
34+
func (s *SetMatrix[K, V]) Contains(key K, value V) (containsElement, setExists bool) {
3535
s.mu.Lock()
3636
defer s.mu.Unlock()
3737
set, ok := s.matrix[key]
@@ -43,13 +43,13 @@ func (s *SetMatrix[T]) Contains(key string, value T) (containsElement, setExists
4343

4444
// Insert inserts the value in the set of a key and returns whether the value is
4545
// inserted (was not already in the set) and the number of elements in the set.
46-
func (s *SetMatrix[T]) Insert(key string, value T) (inserted bool, cardinality int) {
46+
func (s *SetMatrix[K, V]) Insert(key K, value V) (inserted bool, cardinality int) {
4747
s.mu.Lock()
4848
defer s.mu.Unlock()
4949
set, ok := s.matrix[key]
5050
if !ok {
5151
if s.matrix == nil {
52-
s.matrix = make(map[string]mapset.Set[T])
52+
s.matrix = make(map[K]mapset.Set[V])
5353
}
5454
s.matrix[key] = mapset.NewThreadUnsafeSet(value)
5555
return true, 1
@@ -59,7 +59,7 @@ func (s *SetMatrix[T]) Insert(key string, value T) (inserted bool, cardinality i
5959
}
6060

6161
// Remove removes the value in the set for a specific key.
62-
func (s *SetMatrix[T]) Remove(key string, value T) (removed bool, cardinality int) {
62+
func (s *SetMatrix[K, V]) Remove(key K, value V) (removed bool, cardinality int) {
6363
s.mu.Lock()
6464
defer s.mu.Unlock()
6565
set, ok := s.matrix[key]
@@ -80,7 +80,7 @@ func (s *SetMatrix[T]) Remove(key string, value T) (removed bool, cardinality in
8080
}
8181

8282
// Cardinality returns the number of elements in the set for a key.
83-
func (s *SetMatrix[T]) Cardinality(key string) (cardinality int, ok bool) {
83+
func (s *SetMatrix[K, V]) Cardinality(key K) (cardinality int, ok bool) {
8484
s.mu.Lock()
8585
defer s.mu.Unlock()
8686
set, ok := s.matrix[key]
@@ -93,7 +93,7 @@ func (s *SetMatrix[T]) Cardinality(key string) (cardinality int, ok bool) {
9393

9494
// String returns the string version of the set.
9595
// The empty string is returned if there is no set for key.
96-
func (s *SetMatrix[T]) String(key string) (v string, ok bool) {
96+
func (s *SetMatrix[K, V]) String(key K) (v string, ok bool) {
9797
s.mu.Lock()
9898
defer s.mu.Unlock()
9999
set, ok := s.matrix[key]
@@ -104,10 +104,10 @@ func (s *SetMatrix[T]) String(key string) (v string, ok bool) {
104104
}
105105

106106
// Keys returns all the keys in the map.
107-
func (s *SetMatrix[T]) Keys() []string {
107+
func (s *SetMatrix[K, V]) Keys() []K {
108108
s.mu.Lock()
109109
defer s.mu.Unlock()
110-
keys := make([]string, 0, len(s.matrix))
110+
keys := make([]K, 0, len(s.matrix))
111111
for k := range s.matrix {
112112
keys = append(keys, k)
113113
}

libnetwork/internal/setmatrix/setmatrix_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func TestSetSerialInsertDelete(t *testing.T) {
12-
var s SetMatrix[string]
12+
var s SetMatrix[string, string]
1313

1414
b, i := s.Insert("a", "1")
1515
if !b || i != 1 {
@@ -135,7 +135,7 @@ func TestSetSerialInsertDelete(t *testing.T) {
135135
}
136136
}
137137

138-
func insertDeleteRotuine(ctx context.Context, endCh chan int, s *SetMatrix[string], key, value string) {
138+
func insertDeleteRotuine(ctx context.Context, endCh chan int, s *SetMatrix[string, string], key, value string) {
139139
for {
140140
select {
141141
case <-ctx.Done():
@@ -158,7 +158,7 @@ func insertDeleteRotuine(ctx context.Context, endCh chan int, s *SetMatrix[strin
158158
}
159159

160160
func TestSetParallelInsertDelete(t *testing.T) {
161-
var s SetMatrix[string]
161+
var s SetMatrix[string, string]
162162
parallelRoutines := 6
163163
endCh := make(chan int)
164164
// Let the routines running and competing for 10s

libnetwork/libnetwork_internal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func getSvcRecords(t *testing.T, n *Network, key string) (addrs []netip.Addr, fo
457457
sr, ok := n.ctrlr.svcRecords[n.id]
458458
assert.Assert(t, ok)
459459

460-
lookup := func(svcMap *setmatrix.SetMatrix[svcMapEntry]) bool {
460+
lookup := func(svcMap *setmatrix.SetMatrix[string, svcMapEntry]) bool {
461461
mapEntryList, ok := svcMap.Get(key)
462462
if !ok {
463463
return false

libnetwork/network.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ type svcMapEntry struct {
5757
}
5858

5959
type svcInfo struct {
60-
svcMap setmatrix.SetMatrix[svcMapEntry]
61-
svcIPv6Map setmatrix.SetMatrix[svcMapEntry]
62-
ipMap setmatrix.SetMatrix[ipInfo]
60+
svcMap setmatrix.SetMatrix[string, svcMapEntry]
61+
svcIPv6Map setmatrix.SetMatrix[string, svcMapEntry]
62+
ipMap setmatrix.SetMatrix[string, ipInfo]
6363
service map[string][]servicePorts
6464
}
6565

@@ -1370,23 +1370,23 @@ func (n *Network) updateSvcRecord(ctx context.Context, ep *Endpoint, isAdd bool)
13701370
}
13711371
}
13721372

1373-
func addIPToName(ipMap *setmatrix.SetMatrix[ipInfo], name, serviceID string, ip net.IP) {
1373+
func addIPToName(ipMap *setmatrix.SetMatrix[string, ipInfo], name, serviceID string, ip net.IP) {
13741374
reverseIP := netutils.ReverseIP(ip.String())
13751375
ipMap.Insert(reverseIP, ipInfo{
13761376
name: name,
13771377
serviceID: serviceID,
13781378
})
13791379
}
13801380

1381-
func delIPToName(ipMap *setmatrix.SetMatrix[ipInfo], name, serviceID string, ip net.IP) {
1381+
func delIPToName(ipMap *setmatrix.SetMatrix[string, ipInfo], name, serviceID string, ip net.IP) {
13821382
reverseIP := netutils.ReverseIP(ip.String())
13831383
ipMap.Remove(reverseIP, ipInfo{
13841384
name: name,
13851385
serviceID: serviceID,
13861386
})
13871387
}
13881388

1389-
func addNameToIP(svcMap *setmatrix.SetMatrix[svcMapEntry], name, serviceID string, epIP net.IP) {
1389+
func addNameToIP(svcMap *setmatrix.SetMatrix[string, svcMapEntry], name, serviceID string, epIP net.IP) {
13901390
// Since DNS name resolution is case-insensitive, Use the lower-case form
13911391
// of the name as the key into svcMap
13921392
lowerCaseName := strings.ToLower(name)
@@ -1396,7 +1396,7 @@ func addNameToIP(svcMap *setmatrix.SetMatrix[svcMapEntry], name, serviceID strin
13961396
})
13971397
}
13981398

1399-
func delNameToIP(svcMap *setmatrix.SetMatrix[svcMapEntry], name, serviceID string, epIP net.IP) {
1399+
func delNameToIP(svcMap *setmatrix.SetMatrix[string, svcMapEntry], name, serviceID string, epIP net.IP) {
14001400
lowerCaseName := strings.ToLower(name)
14011401
svcMap.Remove(lowerCaseName, svcMapEntry{
14021402
ip: epIP.String(),

libnetwork/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type service struct {
5757
// associated with it. At stable state the endpoint ID expected is 1
5858
// but during transition and service change it is possible to have
5959
// temporary more than 1
60-
ipToEndpoint setmatrix.SetMatrix[string]
60+
ipToEndpoint setmatrix.SetMatrix[string, string]
6161

6262
deleted bool
6363

0 commit comments

Comments
 (0)