/
cache.go
223 lines (200 loc) · 5.35 KB
/
cache.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package intent
import (
"fmt"
"sync"
"github.com/Juniper/asf/pkg/models"
"github.com/sirupsen/logrus"
)
// Cache stores intents.
type Cache struct {
*intentStore
}
// Query finds an intent in cache.
type Query struct {
load func(intents) Intent
description string
}
// String returns the description of a Query.
func (q Query) String() string {
return q.description
}
// ByUUID returns a query to find an intent by UUID.
func ByUUID(uuid string) Query {
return Query{
load: func(is intents) Intent {
return is.uuidToIntent[uuid]
},
description: fmt.Sprintf("by UUID: %v", uuid),
}
}
// ByFQName returns a query to find an intent by FQName.
func ByFQName(fqName []string) Query {
return Query{
load: func(is intents) Intent {
return is.uuidToIntent[is.fqNameToUUID[fqNameKey(fqName)]]
},
description: fmt.Sprintf("by FQName: %v", fqName),
}
}
// NewCache creates new cache for intents.
func NewCache() *Cache {
return &Cache{
intentStore: newIntentStore(),
}
}
// Load loads intent from cache.
func (c *Cache) Load(kind string, q Query) Intent {
logrus.WithFields(logrus.Fields{"kind": kind, "query": q}).Debug("Loading from cache")
return c.intentStore.load(kind, q)
}
// Store puts intent into cache.
func (c *Cache) Store(i Intent) {
logrus.WithFields(logrus.Fields{"kind": i.Kind(), "uuid": i.GetUUID()}).Debug("Storing in cache")
c.intentStore.store(i.Kind(), i)
}
// Delete deletes intent from cache. It accepts kebab-case or CamelCase type name.
func (c *Cache) Delete(kind string, q Query) {
logrus.WithFields(logrus.Fields{"kind": kind, "query": q}).Debug("Deleting from cache")
c.intentStore.delete(kind, q)
}
type intentStore struct {
typeNameToIntents map[string]intents
uuidToType map[string]string
sync.RWMutex
}
type intents struct {
fqNameToUUID map[string]string
uuidToIntent map[string]Intent
}
func newIntentStore() *intentStore {
return &intentStore{
typeNameToIntents: make(map[string]intents),
uuidToType: make(map[string]string),
}
}
func (s *intentStore) load(typeName string, q Query) Intent {
s.RLock()
defer s.RUnlock()
return s.loadInternal(typeName, q)
}
func (s *intentStore) delete(typeName string, q Query) {
s.Lock()
defer s.Unlock()
s.removeDependencies(typeName, q)
s.deleteInternal(typeName, q)
}
func (s *intentStore) store(typeName string, i Intent) {
s.Lock()
defer s.Unlock()
backRefs := i.GetBackReferences()
children := i.GetChildren()
s.removeDependencies(typeName, ByUUID(i.GetUUID()))
s.storeInternal(i)
s.addDependencies(i, backRefs, children)
}
func (s *intentStore) loadInternal(typeName string, q Query) Intent {
is, ok := s.typeNameToIntents[typeName]
if !ok {
return nil
}
return q.load(is)
}
func (s *intentStore) deleteInternal(typeName string, q Query) {
is, ok := s.typeNameToIntents[typeName]
if !ok {
return
}
intent := q.load(is)
if intent == nil {
return
}
delete(is.fqNameToUUID, fqNameKey(intent.GetFQName()))
delete(is.uuidToIntent, intent.GetUUID())
delete(s.uuidToType, intent.GetUUID())
}
func (s *intentStore) storeInternal(i Intent) {
is, ok := s.typeNameToIntents[i.Kind()]
if !ok {
is = intents{
fqNameToUUID: map[string]string{},
uuidToIntent: map[string]Intent{},
}
s.typeNameToIntents[i.Kind()] = is
}
is.uuidToIntent[i.GetUUID()] = i
is.fqNameToUUID[fqNameKey(i.GetFQName())] = i.GetUUID()
s.uuidToType[i.GetUUID()] = i.Kind()
}
func (s *intentStore) addDependencies(i Intent, backRefs, children []models.Object) {
for _, backRef := range backRefs {
i.AddBackReference(backRef)
}
for _, child := range children {
i.AddChild(child)
}
for _, ref := range i.GetReferences() {
t, ok := s.uuidToType[ref.GetUUID()]
if !ok {
continue
}
dependentIntent := s.loadInternal(t, ByUUID(ref.GetUUID()))
if dependentIntent != nil {
i.AddDependentIntent(dependentIntent)
dependentIntent.AddBackReference(i.GetObject())
dependentIntent.AddDependentIntent(i)
s.storeInternal(dependentIntent)
}
}
if i.GetParentUUID() != "" {
t, ok := s.uuidToType[i.GetParentUUID()]
if !ok {
return
}
dependentIntent := s.loadInternal(t, ByUUID(i.GetParentUUID()))
if dependentIntent != nil {
i.AddDependentIntent(dependentIntent)
dependentIntent.AddChild(i.GetObject())
dependentIntent.AddDependentIntent(i)
s.storeInternal(dependentIntent)
}
}
}
func (s *intentStore) removeDependencies(typeName string, q Query) {
i := s.loadInternal(typeName, q)
if i == nil {
return
}
for _, backRef := range i.GetObject().GetBackReferences() {
i.RemoveBackReference(backRef)
}
for _, child := range i.GetObject().GetChildren() {
i.RemoveChild(child)
}
for _, ref := range i.GetObject().GetReferences() {
t, ok := s.uuidToType[ref.GetUUID()]
if !ok {
continue
}
dependentIntent := s.loadInternal(t, ByUUID(ref.GetUUID()))
if dependentIntent != nil {
i.RemoveDependentIntent(dependentIntent)
dependentIntent.RemoveBackReference(i.GetObject())
dependentIntent.RemoveDependentIntent(i)
}
}
if i.GetParentUUID() != "" {
t, ok := s.uuidToType[i.GetParentUUID()]
if !ok {
return
}
dependentIntent := s.loadInternal(t, ByUUID(i.GetParentUUID()))
if dependentIntent != nil {
i.RemoveDependentIntent(dependentIntent)
dependentIntent.RemoveChild(i.GetObject())
dependentIntent.RemoveDependentIntent(i)
}
}
}
func fqNameKey(fqName []string) string {
return models.FQNameToString(fqName)
}