This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathmttrie.go
392 lines (336 loc) · 9.91 KB
/
mttrie.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package control
import (
"fmt"
"sort"
"strings"
"github.com/intelsdi-x/snap/core"
)
/*
Given a trie like this:
root
/\
/ \
foo bar
/\ /\
/ \ / \
a b c d
The result of a collect query like so: Fetch([]string{"root", "foo"})
would return a slice of the MetricTypes found in nodes a & b.
Get collects all children of a given node and returns the values
in all leaves.
This query is needed primarily for the REST interface, where it
can be used to make efficient lookups of Metric Types in a RESTful
manner:
FETCH /metric/root/foo -> trie.Fetch([]string{"root", "foo"}) ->
[a,b]
*/
type mttNode struct {
children map[string]*mttNode
mts map[int]*metricType
}
// MTTrie struct representing the root in the trie
type MTTrie struct {
*mttNode
}
// NewMTTrie returns an empty trie
func NewMTTrie() *MTTrie {
m := &mttNode{
children: map[string]*mttNode{},
}
return &MTTrie{m}
}
// String prints out of the tr(i)e
func (m *MTTrie) String() string {
out := ""
for _, mt := range m.gatherMetricTypes() {
pluginKey := fmt.Sprintf("%s"+core.Separator+"%s"+core.Separator+"%d", mt.Plugin.TypeName(), mt.Plugin.Name(), mt.Plugin.Version())
out += fmt.Sprintf("%s => %s\n", mt.Key(), pluginKey)
}
return out
}
func (m *MTTrie) gatherMetricTypes() []metricType {
var mts []metricType
var descendants []*mttNode
for _, node := range m.children {
descendants = gatherDescendants(descendants, node)
}
for _, c := range descendants {
for _, mt := range c.mts {
mts = append(mts, *mt)
}
}
return mts
}
// DeleteByPlugin removes all metrics from the catalog if they match a loadedPlugin
func (m *MTTrie) DeleteByPlugin(cp core.CatalogedPlugin) {
for _, mt := range m.gatherMetricTypes() {
mtPluginKey := fmt.Sprintf("%s"+core.Separator+"%s"+core.Separator+"%d", mt.Plugin.TypeName(), mt.Plugin.Name(), mt.Plugin.Version())
cpKey := fmt.Sprintf("%s"+core.Separator+"%s"+core.Separator+"%d", cp.TypeName(), cp.Name(), cp.Version())
if mtPluginKey == cpKey {
// remove this metric
m.RemoveMetric(mt)
}
}
}
// RemoveMetric removes a specific metric by namespace and version from the tree
func (m *MTTrie) RemoveMetric(mt metricType) {
a, _ := m.find(mt.Namespace().Strings())
if a != nil {
for v, x := range a.mts {
if mt.Version() == x.Version() {
// delete this metric from the node
delete(a.mts, v)
}
}
}
}
// Add adds a node with the given namespace with the given MetricType
func (mtt *mttNode) Add(mt *metricType) {
ns := mt.Namespace()
node, index := mtt.walk(ns.Strings())
if index == len(ns) {
if node.mts == nil {
node.mts = make(map[int]*metricType)
}
node.mts[mt.Version()] = mt
return
}
// walk through the remaining namespace and build out the
// new branch in the trie.
for _, n := range ns[index:] {
if node.children == nil {
node.children = make(map[string]*mttNode)
}
node.children[n.Value] = &mttNode{}
node = node.children[n.Value]
}
node.mts = make(map[int]*metricType)
node.mts[mt.Version()] = mt
}
// Fetch collects all children below a given namespace
// and concatenates their metric types into a single slice
func (mtt *mttNode) Fetch(ns []string) ([]*metricType, error) {
children := mtt.fetch(ns)
var mts []*metricType
for _, child := range children {
for _, mt := range child.mts {
mts = append(mts, mt)
}
}
if len(mts) == 0 && len(ns) > 0 {
return nil, errorMetricsNotFound("/" + strings.Join(ns, "/"))
}
return mts, nil
}
// Remove removes all descendants nodes below a given namespace
func (mtt *mttNode) Remove(ns []string) error {
_, err := mtt.find(ns)
if err != nil {
return err
}
parent, err := mtt.find(ns[:len(ns)-1])
if err != nil {
return err
}
// remove node from parent
delete(parent.children, ns[len(ns)-1:][0])
return nil
}
// GetMetric works like GetMetrics, but only returns the single MT in the requested version (or in the latest if ver < 1)
// and does NOT gather the node's children.
func (mtt *mttNode) GetMetric(ns []string, ver int) (*metricType, error) {
mts, err := mtt.GetMetrics(ns, ver)
if err != nil {
return nil, err
}
// there is an expectation that only one metric should be fitted
if len(mts) > 1 {
return nil, fmt.Errorf("Incoming namespace `%s` is too ambiguous (version: %d)", "/"+strings.Join(ns, "/"), ver)
}
return mts[0], nil
}
// GetMetrics returns all MTs at the given namespace in the queried version (or in the latest if ver < 1)
// and does gather all the node's descendants if the namespace ends with an asterisk
func (mtt *mttNode) GetMetrics(ns []string, ver int) ([]*metricType, error) {
nodes := []*mttNode{}
mts := []*metricType{}
if len(ns) == 0 {
return nil, errorEmptyNamespace()
}
// search returns all of the nodes fulfilling the 'ns'
// even for some of them there is no metric (empty node.mts)
nodes = mtt.search(nodes, ns)
for _, node := range nodes {
// choose the queried version of metric types (or the latest if ver < 1)
// and concatenate them into a single slice
mt, err := getVersion(node.mts, ver)
if err != nil {
continue
}
mts = append(mts, mt)
}
if len(mts) == 0 {
return nil, errorMetricNotFound("/"+strings.Join(ns, "/"), ver)
}
return mts, nil
}
// GetVersions returns all versions of MTs below the given namespace
func (mtt *mttNode) GetVersions(ns []string) ([]*metricType, error) {
var nodes []*mttNode
var mts []*metricType
if len(ns) == 0 {
return nil, errorEmptyNamespace()
}
nodes = mtt.search(nodes, ns)
for _, node := range nodes {
// concatenates metric types in ALL versions into a single slice
for _, mt := range node.mts {
mts = append(mts, mt)
}
}
if len(mts) == 0 {
return nil, errorMetricNotFound("/" + strings.Join(ns, "/"))
}
return mts, nil
}
// fetch collects all descendants nodes below the given namespace
func (mtt *mttNode) fetch(ns []string) []*mttNode {
node, err := mtt.find(ns)
if err != nil {
return nil
}
var children []*mttNode
if node.mts != nil {
children = append(children, node)
}
if node.children != nil {
children = gatherDescendants(children, node)
}
return children
}
// walk returns the last leaf / branch present in the trie and the index in the namespace that the last node exists.
// It is useful e.g. to locate the right place to add new metric type into tree with the given namespace
func (mtt *mttNode) walk(ns []string) (*mttNode, int) {
parent := mtt
var pp *mttNode
for i, n := range ns {
if parent.children == nil {
return parent, i
}
pp = parent
parent = parent.children[n]
if parent == nil {
return pp, i
}
}
return parent, len(ns)
}
// search returns leaf nodes in the trie below the given namespace
func (mtt *mttNode) search(nodes []*mttNode, ns []string) []*mttNode {
parent := mtt
var children []*mttNode
if parent.children == nil {
return nodes
}
if len(ns) == 1 {
// the last element of ns is under searching process
switch ns[0] {
case "*":
// fetch all descendants when wildcard ends namespace
children = parent.fetch([]string{})
default:
children = parent.gatherChildren(ns[0])
}
nodes = append(nodes, children...)
return nodes
}
children = parent.gatherChildren(ns[0])
for _, child := range children {
nodes = child.search(nodes, ns[1:])
}
return nodes
}
func (mtt *mttNode) find(ns []string) (*mttNode, error) {
node, index := mtt.walk(ns)
if index != len(ns) {
return nil, errorMetricNotFound("/" + strings.Join(ns, "/"))
}
return node, nil
}
// gatherChildren returns child or children by the 'name' of a given node (direct descendant(s))
// and concatenates this direct descendant(s) into a single slice
func (mtt *mttNode) gatherChildren(name string) []*mttNode {
var children []*mttNode
switch name {
case "*":
// name of child is unspecified, so gather all children
for _, child := range mtt.children {
children = append(children, child)
}
default:
// gather a single child with specified name
child := mtt.children[name]
if child == nil {
// child with this name not exist; it might be specific instance of dynamic metric
// so, take child named with an asterisk
child = mtt.children["*"]
}
if child != nil {
children = append(children, child)
}
}
return children
}
// gatherDescendants returns all descendants of a given node
func gatherDescendants(descendants []*mttNode, node *mttNode) []*mttNode {
for _, child := range node.children {
if child.mts != nil {
descendants = append(descendants, child)
}
if child.children != nil {
descendants = gatherDescendants(descendants, child)
}
}
return descendants
}
// getVersion returns the MT in the latest version
func getLatest(mts map[int]*metricType) *metricType {
versions := []int{}
// version is a key in mts map
for ver := range mts {
// concatenates all available versions to a single slice
versions = append(versions, ver)
}
// sort and take the last element (the latest version)
sort.Ints(versions)
latestVersion := versions[len(versions)-1]
return mts[latestVersion]
}
// getVersion returns the MT in the queried version (or the latest if 'ver' < 1)
func getVersion(mts map[int]*metricType, ver int) (*metricType, error) {
if len(mts) == 0 {
return nil, errMetricNotFound
}
if ver > 0 {
// a version IS given
if mt, exist := mts[ver]; exist {
return mt, nil
}
return nil, errMetricNotFound
}
// or get the latest
return getLatest(mts), nil
}