@@ -22,37 +22,55 @@ import (
2222)
2323
2424type ConfigManager [K comparable , T any ] struct {
25- mu sync.Mutex
26- parentRefs map [K ][]K
27- configs map [K ]T
25+ mu sync.Mutex
26+ resourceConfigKeys map [K ][]K
27+ configs map [K ]T
28+ configRefs map [K ][]K
2829}
2930
3031func NewConfigManager [K comparable , T any ]() * ConfigManager [K , T ] {
3132 return & ConfigManager [K , T ]{
32- parentRefs : make (map [K ][]K ),
33- configs : make (map [K ]T ),
33+ resourceConfigKeys : make (map [K ][]K ),
34+ configs : make (map [K ]T ),
35+ configRefs : make (map [K ][]K ),
3436 }
3537}
3638
37- func (s * ConfigManager [K , T ]) GetParentRefs (key K ) []K {
39+ func (s * ConfigManager [K , T ]) GetConfigRefs (key K ) []K {
3840 s .mu .Lock ()
3941 defer s .mu .Unlock ()
40- return s .parentRefs [key ]
42+ return s .configRefs [key ]
4143}
4244
43- func (s * ConfigManager [K , T ]) SetParentRefs (key K , refs []K ) {
45+ func (s * ConfigManager [K , T ]) GetConfigRefsByResourceKey (key K ) []K {
4446 s .mu .Lock ()
4547 defer s .mu .Unlock ()
46- s .parentRefs [key ] = refs
48+ configKeys , ok := s .resourceConfigKeys [key ]
49+ if ! ok {
50+ return nil
51+ }
52+ refs := make ([]K , 0 , len (configKeys ))
53+ for _ , k := range configKeys {
54+ if ref , ok := s .configRefs [k ]; ok {
55+ refs = append (refs , ref ... )
56+ }
57+ }
58+ return refs
59+ }
60+
61+ func (s * ConfigManager [K , T ]) SetConfigRefs (key K , refs []K ) {
62+ s .mu .Lock ()
63+ defer s .mu .Unlock ()
64+ s .configRefs [key ] = refs
4765}
4866
4967func (s * ConfigManager [K , T ]) Get (key K ) map [K ]T {
5068 s .mu .Lock ()
5169 defer s .mu .Unlock ()
5270
53- parentRefs := s .parentRefs [key ]
54- configs := make (map [K ]T , len (parentRefs ))
55- for _ , parent := range parentRefs {
71+ resourceConfigKeys := s .resourceConfigKeys [key ]
72+ configs := make (map [K ]T , len (resourceConfigKeys ))
73+ for _ , parent := range resourceConfigKeys {
5674 if cfg , ok := s .configs [parent ]; ok {
5775 configs [parent ] = cfg
5876 }
@@ -85,15 +103,15 @@ func (s *ConfigManager[K, T]) Update(
85103 defer s .mu .Unlock ()
86104
87105 parentRefSet := make (map [K ]struct {})
88- oldParentRefs := s .parentRefs [key ]
106+ oldParentRefs := s .resourceConfigKeys [key ]
89107 newRefs := make ([]K , 0 , len (mapRefs ))
90108
91109 for k , v := range mapRefs {
92110 newRefs = append (newRefs , k )
93111 s .configs [k ] = v
94112 parentRefSet [k ] = struct {}{}
95113 }
96- s .parentRefs [key ] = newRefs
114+ s .resourceConfigKeys [key ] = newRefs
97115 discard = make (map [K ]T )
98116 for _ , old := range oldParentRefs {
99117 if _ , stillUsed := parentRefSet [old ]; ! stillUsed {
@@ -115,12 +133,14 @@ func (s *ConfigManager[K, T]) Set(key K, cfg T) {
115133func (s * ConfigManager [K , T ]) Delete (key K ) {
116134 s .mu .Lock ()
117135 defer s .mu .Unlock ()
118- delete (s .parentRefs , key )
136+ delete (s .resourceConfigKeys , key )
119137 delete (s .configs , key )
138+ delete (s .configRefs , key )
120139}
121140
122141func (s * ConfigManager [K , T ]) DeleteConfig (key K ) {
123142 s .mu .Lock ()
124143 defer s .mu .Unlock ()
125- delete (s .parentRefs , key )
144+ delete (s .configs , key )
145+ delete (s .configRefs , key )
126146}
0 commit comments