This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.go
150 lines (127 loc) · 3.06 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package http
import (
"github.com/keptn/go-utils/pkg/api/models"
"sync"
)
// cache is used to store key value data
type cache struct {
sync.RWMutex
cache map[string][]string
}
// NewCache creates a new cache
func NewCache() *cache {
return &cache{
cache: make(map[string][]string),
}
}
// Add adds a new element for a given key to the cache
func (c *cache) Add(key, element string) {
c.Lock()
defer c.Unlock()
eventsForTopic := c.cache[key]
for _, id := range eventsForTopic {
if id == element {
return
}
}
c.cache[key] = append(c.cache[key], element)
}
// Get returns all elements for a given key from the cache
func (c *cache) Get(key string) []string {
c.RLock()
defer c.RUnlock()
cp := make([]string, len(c.cache[key]))
copy(cp, c.cache[key])
return cp
}
// Remove removes an element for a given key from the cache
func (c *cache) Remove(key, element string) bool {
c.Lock()
defer c.Unlock()
eventsForTopic := c.cache[key]
for index, id := range eventsForTopic {
if id == element {
// found, make sure to store the result back in c.cache[key]
c.cache[key] = append(eventsForTopic[:index], eventsForTopic[index+1:]...)
return true
}
}
return false
}
// Contains checks whether the given element for a topic name is contained in the cache
func (c *cache) Contains(key, element string) bool {
c.RLock()
defer c.RUnlock()
return c.contains(key, element)
}
// Keep deletes all elements for a topic from the cache except the ones given by events
func (c *cache) Keep(key string, elements []string) {
c.Lock()
defer c.Unlock()
// keeping 0 elements, means clearing the cache
if len(elements) == 0 {
c.clear(key)
}
// convert to raw ids without duplicates
ids := dedup(elements)
// if none of the ids is known cached do nothing
if !c.containsSlice(key, ids) {
return
}
currentEventsForTopic := c.cache[key]
eventsToKeep := []string{}
for _, idOfEventToKeep := range ids {
for _, e := range currentEventsForTopic {
if idOfEventToKeep == e {
eventsToKeep = append(eventsToKeep, e)
}
}
}
c.cache[key] = eventsToKeep
}
// Lenghts returns the number of cached elements for a given topic
func (c *cache) Length(key string) int {
c.RLock()
defer c.RUnlock()
return len(c.cache[key])
}
func (c *cache) clear(key string) {
c.cache[key] = []string{}
}
func (c *cache) contains(key, element string) bool {
eventsForTopic := c.cache[key]
for _, id := range eventsForTopic {
if id == element {
return true
}
}
return false
}
func (c *cache) containsSlice(key string, elements []string) bool {
contains := false
for _, id := range elements {
if c.contains(key, id) {
contains = true
break
}
}
return contains
}
func dedup(elements []string) []string {
result := make([]string, 0, len(elements))
temp := map[string]struct{}{}
for _, el := range elements {
if _, ok := temp[el]; !ok {
temp[el] = struct{}{}
result = append(result, el)
}
}
return result
}
func ToIds(events []*models.KeptnContextExtendedCE) []string {
ids := []string{}
for _, e := range events {
ids = append(ids, e.ID)
}
return ids
}