2021-09-08 12:17:10 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SelectorElement struct {
|
2021-11-16 11:27:28 +01:00
|
|
|
Any bool
|
2021-09-08 12:17:10 +02:00
|
|
|
String string
|
|
|
|
Group []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (se *SelectorElement) UnmarshalJSON(input []byte) error {
|
|
|
|
if input[0] == '"' {
|
2021-11-16 11:27:28 +01:00
|
|
|
if err := json.Unmarshal(input, &se.String); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if se.String == "*" {
|
|
|
|
se.Any = true
|
|
|
|
se.String = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-09-08 12:17:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if input[0] == '[' {
|
|
|
|
return json.Unmarshal(input, &se.Group)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("the Go SelectorElement type can only be a string or an array of strings")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (se *SelectorElement) MarshalJSON() ([]byte, error) {
|
2021-11-16 11:27:28 +01:00
|
|
|
if se.Any {
|
|
|
|
return []byte("\"*\""), nil
|
|
|
|
}
|
|
|
|
|
2021-09-08 12:17:10 +02:00
|
|
|
if se.String != "" {
|
|
|
|
return json.Marshal(se.String)
|
|
|
|
}
|
|
|
|
|
|
|
|
if se.Group != nil {
|
|
|
|
return json.Marshal(se.Group)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("a Go Selector must be a non-empty string or a non-empty slice of strings")
|
|
|
|
}
|
|
|
|
|
|
|
|
type Selector []SelectorElement
|
|
|
|
|
|
|
|
func (l *level) findBuffers(selector Selector, offset int, f func(b *buffer) error) error {
|
|
|
|
l.lock.RLock()
|
|
|
|
defer l.lock.RUnlock()
|
|
|
|
|
|
|
|
if len(selector) == 0 {
|
2022-03-08 09:27:44 +01:00
|
|
|
b := l.metrics[offset]
|
|
|
|
if b != nil {
|
|
|
|
return f(b)
|
2021-09-08 12:17:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, lvl := range l.children {
|
|
|
|
err := lvl.findBuffers(nil, offset, f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sel := selector[0]
|
2021-09-13 12:25:56 +02:00
|
|
|
if len(sel.String) != 0 && l.children != nil {
|
2021-09-08 12:17:10 +02:00
|
|
|
lvl, ok := l.children[sel.String]
|
|
|
|
if ok {
|
|
|
|
err := lvl.findBuffers(selector[1:], offset, f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-13 12:25:56 +02:00
|
|
|
if sel.Group != nil && l.children != nil {
|
2021-09-08 12:17:10 +02:00
|
|
|
for _, key := range sel.Group {
|
|
|
|
lvl, ok := l.children[key]
|
|
|
|
if ok {
|
|
|
|
err := lvl.findBuffers(selector[1:], offset, f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-16 11:27:28 +01:00
|
|
|
if sel.Any && l.children != nil {
|
|
|
|
for _, lvl := range l.children {
|
|
|
|
if err := lvl.findBuffers(selector[1:], offset, f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-12-01 13:22:07 +01:00
|
|
|
return nil
|
2021-11-16 11:27:28 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 16:34:42 +01:00
|
|
|
return nil
|
2021-09-08 12:17:10 +02:00
|
|
|
}
|