2021-10-27 10:00:35 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2024-06-24 11:45:59 +00:00
|
|
|
"strings"
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2024-10-07 14:20:25 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/netmap"
|
2021-10-27 10:00:35 +00:00
|
|
|
)
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// mainFilterName is a name of the filter
|
2021-10-27 10:00:35 +00:00
|
|
|
// which points to the whole netmap.
|
2022-06-07 08:25:34 +00:00
|
|
|
const mainFilterName = "*"
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2024-06-24 11:45:59 +00:00
|
|
|
const likeWildcard = "*"
|
|
|
|
|
2021-10-27 10:00:35 +00:00
|
|
|
// processFilters processes filters and returns error is any of them is invalid.
|
2022-06-07 08:25:34 +00:00
|
|
|
func (c *context) processFilters(p PlacementPolicy) error {
|
|
|
|
for i := range p.filters {
|
|
|
|
if err := c.processFilter(p.filters[i], true); err != nil {
|
|
|
|
return fmt.Errorf("process filter #%d (%s): %w", i, p.filters[i].GetName(), err)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func (c *context) processFilter(f netmap.Filter, top bool) error {
|
|
|
|
fName := f.GetName()
|
|
|
|
if fName == mainFilterName {
|
|
|
|
return fmt.Errorf("%w: '%s' is reserved", errInvalidFilterName, mainFilterName)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
if top && fName == "" {
|
|
|
|
return errUnnamedTopFilter
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
if !top && fName != "" && c.processedFilters[fName] == nil {
|
|
|
|
return errFilterNotFound
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
inner := f.GetFilters()
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
switch op := f.GetOp(); op {
|
2023-05-24 09:50:13 +00:00
|
|
|
case netmap.AND, netmap.OR, netmap.NOT:
|
2022-06-07 08:25:34 +00:00
|
|
|
for i := range inner {
|
|
|
|
if err := c.processFilter(inner[i], false); err != nil {
|
|
|
|
return fmt.Errorf("process inner filter #%d: %w", i, err)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2022-06-07 08:25:34 +00:00
|
|
|
if len(inner) != 0 {
|
|
|
|
return errNonEmptyFilters
|
|
|
|
} else if !top && fName != "" { // named reference
|
2021-10-27 10:00:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
switch op {
|
2024-06-24 11:45:59 +00:00
|
|
|
case netmap.EQ, netmap.NE, netmap.LIKE:
|
2022-06-07 08:25:34 +00:00
|
|
|
case netmap.GT, netmap.GE, netmap.LT, netmap.LE:
|
|
|
|
val := f.GetValue()
|
|
|
|
n, err := strconv.ParseUint(val, 10, 64)
|
2021-10-27 10:00:35 +00:00
|
|
|
if err != nil {
|
2022-06-07 08:25:34 +00:00
|
|
|
return fmt.Errorf("%w: '%s'", errInvalidNumber, f.GetValue())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
c.numCache[val] = n
|
2021-10-27 10:00:35 +00:00
|
|
|
default:
|
2022-06-07 08:25:34 +00:00
|
|
|
return fmt.Errorf("%w: %s", errInvalidFilterOp, op)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if top {
|
2022-06-07 08:25:34 +00:00
|
|
|
c.processedFilters[fName] = &f
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// match matches f against b. It returns no errors because
|
|
|
|
// filter should have been parsed during context creation
|
|
|
|
// and missing node properties are considered as a regular fail.
|
2022-06-07 08:25:34 +00:00
|
|
|
func (c *context) match(f *netmap.Filter, b NodeInfo) bool {
|
|
|
|
switch f.GetOp() {
|
2023-05-24 09:50:13 +00:00
|
|
|
case netmap.NOT:
|
|
|
|
inner := f.GetFilters()
|
|
|
|
fSub := &inner[0]
|
|
|
|
if name := inner[0].GetName(); name != "" {
|
|
|
|
fSub = c.processedFilters[name]
|
|
|
|
}
|
|
|
|
return !c.match(fSub, b)
|
2022-06-07 08:25:34 +00:00
|
|
|
case netmap.AND, netmap.OR:
|
|
|
|
inner := f.GetFilters()
|
|
|
|
for i := range inner {
|
|
|
|
fSub := &inner[i]
|
|
|
|
if name := inner[i].GetName(); name != "" {
|
|
|
|
fSub = c.processedFilters[name]
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
ok := c.match(fSub, b)
|
|
|
|
if ok == (f.GetOp() == netmap.OR) {
|
2021-10-27 10:00:35 +00:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return f.GetOp() == netmap.AND
|
2021-10-27 10:00:35 +00:00
|
|
|
default:
|
|
|
|
return c.matchKeyValue(f, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func (c *context) matchKeyValue(f *netmap.Filter, b NodeInfo) bool {
|
|
|
|
switch op := f.GetOp(); op {
|
|
|
|
case netmap.EQ:
|
|
|
|
return b.Attribute(f.GetKey()) == f.GetValue()
|
2024-06-24 11:45:59 +00:00
|
|
|
case netmap.LIKE:
|
|
|
|
str, prefix := strings.CutPrefix(f.GetValue(), likeWildcard)
|
|
|
|
str, suffix := strings.CutSuffix(str, likeWildcard)
|
|
|
|
if prefix && suffix {
|
|
|
|
return strings.Contains(b.Attribute(f.GetKey()), str)
|
|
|
|
}
|
|
|
|
if prefix && !suffix {
|
|
|
|
return strings.HasSuffix(b.Attribute(f.GetKey()), str)
|
|
|
|
}
|
|
|
|
if !prefix && suffix {
|
|
|
|
return strings.HasPrefix(b.Attribute(f.GetKey()), str)
|
|
|
|
}
|
|
|
|
return b.Attribute(f.GetKey()) == f.GetValue()
|
2022-06-07 08:25:34 +00:00
|
|
|
case netmap.NE:
|
|
|
|
return b.Attribute(f.GetKey()) != f.GetValue()
|
2021-10-27 10:00:35 +00:00
|
|
|
default:
|
|
|
|
var attr uint64
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
switch f.GetKey() {
|
|
|
|
case attrPrice:
|
|
|
|
attr = b.Price()
|
|
|
|
case attrCapacity:
|
2022-06-07 02:12:39 +00:00
|
|
|
attr = b.capacity()
|
2021-10-27 10:00:35 +00:00
|
|
|
default:
|
|
|
|
var err error
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
attr, err = strconv.ParseUint(b.Attribute(f.GetKey()), 10, 64)
|
2021-10-27 10:00:35 +00:00
|
|
|
if err != nil {
|
|
|
|
// Note: because filters are somewhat independent from nodes attributes,
|
|
|
|
// We don't report an error here, and fail filter instead.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
switch op {
|
|
|
|
case netmap.GT:
|
|
|
|
return attr > c.numCache[f.GetValue()]
|
|
|
|
case netmap.GE:
|
|
|
|
return attr >= c.numCache[f.GetValue()]
|
|
|
|
case netmap.LT:
|
|
|
|
return attr < c.numCache[f.GetValue()]
|
|
|
|
case netmap.LE:
|
|
|
|
return attr <= c.numCache[f.GetValue()]
|
2021-10-27 10:00:35 +00:00
|
|
|
default:
|
|
|
|
// do nothing and return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// will not happen if context was created from f (maybe panic?)
|
|
|
|
return false
|
|
|
|
}
|