| 46 | } |
| 47 | |
| 48 | func (c *Cache) addNotEmpty(group *Group) (added, removed []Config) { |
| 49 | set, ok := c.sources[group.Source] |
| 50 | if !ok { |
| 51 | set = make(map[uint64]Config) |
| 52 | c.sources[group.Source] = set |
| 53 | } |
| 54 | |
| 55 | seen := make(map[uint64]struct{}) |
| 56 | |
| 57 | for _, cfg := range group.Configs { |
| 58 | hash := cfg.Hash() |
| 59 | seen[hash] = struct{}{} |
| 60 | |
| 61 | if _, ok := set[hash]; ok { |
| 62 | continue |
| 63 | } |
| 64 | |
| 65 | set[hash] = cfg |
| 66 | if c.hashes[hash] == 0 { |
| 67 | added = append(added, cfg) |
| 68 | } |
| 69 | c.hashes[hash]++ |
| 70 | } |
| 71 | |
| 72 | if !ok { |
| 73 | return added, nil |
| 74 | } |
| 75 | |
| 76 | for hash, cfg := range set { |
| 77 | if _, ok := seen[hash]; ok { |
| 78 | continue |
| 79 | } |
| 80 | |
| 81 | delete(set, hash) |
| 82 | c.hashes[hash]-- |
| 83 | if c.hashes[hash] == 0 { |
| 84 | removed = append(removed, cfg) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if len(set) == 0 { |
| 89 | delete(c.sources, group.Source) |
| 90 | } |
| 91 | |
| 92 | return added, removed |
| 93 | } |