2020-09-05 10:37:47 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
|
|
|
)
|
|
|
|
|
2020-11-05 10:02:31 +00:00
|
|
|
// Filter represents v2-compatible netmap filter.
|
|
|
|
type Filter netmap.Filter
|
|
|
|
|
2020-09-05 10:37:47 +00:00
|
|
|
// MainFilterName is a name of the filter
|
|
|
|
// which points to the whole netmap.
|
|
|
|
const MainFilterName = "*"
|
|
|
|
|
|
|
|
// applyFilter applies named filter to b.
|
|
|
|
func (c *Context) applyFilter(name string, b *Node) bool {
|
|
|
|
return name == MainFilterName || c.match(c.Filters[name], b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// processFilters processes filters and returns error is any of them is invalid.
|
2020-11-05 12:45:06 +00:00
|
|
|
func (c *Context) processFilters(p *PlacementPolicy) error {
|
|
|
|
for _, f := range p.Filters() {
|
2020-09-05 10:37:47 +00:00
|
|
|
if err := c.processFilter(f, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-11-16 14:52:15 +00:00
|
|
|
|
2020-09-05 10:37:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-05 12:45:06 +00:00
|
|
|
func (c *Context) processFilter(f *Filter, top bool) error {
|
2020-09-05 10:37:47 +00:00
|
|
|
if f == nil {
|
|
|
|
return fmt.Errorf("%w: FILTER", ErrMissingField)
|
|
|
|
}
|
2020-11-16 14:52:15 +00:00
|
|
|
|
2020-11-05 12:45:06 +00:00
|
|
|
if f.Name() == MainFilterName {
|
2020-09-05 10:37:47 +00:00
|
|
|
return fmt.Errorf("%w: '*' is reserved", ErrInvalidFilterName)
|
|
|
|
}
|
2020-11-16 14:52:15 +00:00
|
|
|
|
2020-11-05 12:45:06 +00:00
|
|
|
if top && f.Name() == "" {
|
2020-09-05 10:37:47 +00:00
|
|
|
return ErrUnnamedTopFilter
|
|
|
|
}
|
2020-11-16 14:52:15 +00:00
|
|
|
|
2020-11-05 12:45:06 +00:00
|
|
|
if !top && f.Name() != "" && c.Filters[f.Name()] == nil {
|
|
|
|
return fmt.Errorf("%w: '%s'", ErrFilterNotFound, f.Name())
|
2020-09-05 10:37:47 +00:00
|
|
|
}
|
2020-11-16 14:52:15 +00:00
|
|
|
|
2020-11-05 12:45:06 +00:00
|
|
|
switch f.Operation() {
|
|
|
|
case OpAND, OpOR:
|
|
|
|
for _, flt := range f.InnerFilters() {
|
2020-09-05 10:37:47 +00:00
|
|
|
if err := c.processFilter(flt, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2020-11-05 12:45:06 +00:00
|
|
|
if len(f.InnerFilters()) != 0 {
|
2020-09-05 10:37:47 +00:00
|
|
|
return ErrNonEmptyFilters
|
2020-11-05 12:45:06 +00:00
|
|
|
} else if !top && f.Name() != "" { // named reference
|
2020-09-05 10:37:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-16 14:52:15 +00:00
|
|
|
|
2020-11-05 12:45:06 +00:00
|
|
|
switch f.Operation() {
|
|
|
|
case OpEQ, OpNE:
|
|
|
|
case OpGT, OpGE, OpLT, OpLE:
|
|
|
|
n, err := strconv.ParseUint(f.Value(), 10, 64)
|
2020-09-05 10:37:47 +00:00
|
|
|
if err != nil {
|
2020-11-05 12:45:06 +00:00
|
|
|
return fmt.Errorf("%w: '%s'", ErrInvalidNumber, f.Value())
|
2020-09-05 10:37:47 +00:00
|
|
|
}
|
2020-11-16 14:52:15 +00:00
|
|
|
|
2020-09-05 10:37:47 +00:00
|
|
|
c.numCache[f] = n
|
|
|
|
default:
|
2020-11-05 12:45:06 +00:00
|
|
|
return fmt.Errorf("%w: %s", ErrInvalidFilterOp, f.Operation())
|
2020-09-05 10:37:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-16 14:52:15 +00:00
|
|
|
|
2020-09-05 10:37:47 +00:00
|
|
|
if top {
|
2020-11-05 12:45:06 +00:00
|
|
|
c.Filters[f.Name()] = f
|
2020-09-05 10:37:47 +00:00
|
|
|
}
|
2020-11-16 14:52:15 +00:00
|
|
|
|
2020-09-05 10:37:47 +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.
|
2020-11-05 12:45:06 +00:00
|
|
|
func (c *Context) match(f *Filter, b *Node) bool {
|
|
|
|
switch f.Operation() {
|
|
|
|
case OpAND, OpOR:
|
|
|
|
for _, lf := range f.InnerFilters() {
|
|
|
|
if lf.Name() != "" {
|
|
|
|
lf = c.Filters[lf.Name()]
|
2020-09-05 10:37:47 +00:00
|
|
|
}
|
2020-11-16 14:52:15 +00:00
|
|
|
|
2020-09-05 10:37:47 +00:00
|
|
|
ok := c.match(lf, b)
|
2020-11-05 12:45:06 +00:00
|
|
|
if ok == (f.Operation() == OpOR) {
|
2020-09-05 10:37:47 +00:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
}
|
2020-11-16 14:52:15 +00:00
|
|
|
|
2020-11-05 12:45:06 +00:00
|
|
|
return f.Operation() == OpAND
|
2020-09-05 10:37:47 +00:00
|
|
|
default:
|
|
|
|
return c.matchKeyValue(f, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 12:45:06 +00:00
|
|
|
func (c *Context) matchKeyValue(f *Filter, b *Node) bool {
|
|
|
|
switch f.Operation() {
|
|
|
|
case OpEQ:
|
|
|
|
return b.Attribute(f.Key()) == f.Value()
|
|
|
|
case OpNE:
|
|
|
|
return b.Attribute(f.Key()) != f.Value()
|
2020-09-05 10:37:47 +00:00
|
|
|
default:
|
|
|
|
var attr uint64
|
2020-11-16 14:52:15 +00:00
|
|
|
|
2020-11-05 12:45:06 +00:00
|
|
|
switch f.Key() {
|
2021-02-10 18:51:16 +00:00
|
|
|
case AttrPrice:
|
2020-09-05 10:37:47 +00:00
|
|
|
attr = b.Price
|
2021-02-10 18:51:16 +00:00
|
|
|
case AttrCapacity:
|
2020-09-05 10:37:47 +00:00
|
|
|
attr = b.Capacity
|
|
|
|
default:
|
|
|
|
var err error
|
2020-11-16 14:52:15 +00:00
|
|
|
|
2020-11-05 12:45:06 +00:00
|
|
|
attr, err = strconv.ParseUint(b.Attribute(f.Key()), 10, 64)
|
2020-09-05 10:37:47 +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
|
|
|
|
}
|
|
|
|
}
|
2020-11-16 14:52:15 +00:00
|
|
|
|
2020-11-05 12:45:06 +00:00
|
|
|
switch f.Operation() {
|
|
|
|
case OpGT:
|
2020-09-05 10:37:47 +00:00
|
|
|
return attr > c.numCache[f]
|
2020-11-05 12:45:06 +00:00
|
|
|
case OpGE:
|
2020-09-05 10:37:47 +00:00
|
|
|
return attr >= c.numCache[f]
|
2020-11-05 12:45:06 +00:00
|
|
|
case OpLT:
|
2020-09-05 10:37:47 +00:00
|
|
|
return attr < c.numCache[f]
|
2020-11-05 12:45:06 +00:00
|
|
|
case OpLE:
|
2020-09-05 10:37:47 +00:00
|
|
|
return attr <= c.numCache[f]
|
2021-01-11 16:31:23 +00:00
|
|
|
default:
|
|
|
|
// do nothing and return false
|
2020-09-05 10:37:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// will not happen if context was created from f (maybe panic?)
|
|
|
|
return false
|
|
|
|
}
|
2020-11-05 10:02:31 +00:00
|
|
|
|
|
|
|
// NewFilter creates and returns new Filter instance.
|
|
|
|
func NewFilter() *Filter {
|
|
|
|
return NewFilterFromV2(new(netmap.Filter))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFilterFromV2 converts v2 Filter to Filter.
|
|
|
|
func NewFilterFromV2(f *netmap.Filter) *Filter {
|
|
|
|
return (*Filter)(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToV2 converts Filter to v2 Filter.
|
|
|
|
func (f *Filter) ToV2() *netmap.Filter {
|
|
|
|
return (*netmap.Filter)(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key returns key to filter.
|
|
|
|
func (f *Filter) Key() string {
|
|
|
|
return (*netmap.Filter)(f).
|
|
|
|
GetKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetKey sets key to filter.
|
|
|
|
func (f *Filter) SetKey(key string) {
|
|
|
|
(*netmap.Filter)(f).
|
|
|
|
SetKey(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns value to match.
|
|
|
|
func (f *Filter) Value() string {
|
|
|
|
return (*netmap.Filter)(f).
|
|
|
|
GetValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetValue sets value to match.
|
|
|
|
func (f *Filter) SetValue(val string) {
|
|
|
|
(*netmap.Filter)(f).
|
|
|
|
SetValue(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns filter name.
|
|
|
|
func (f *Filter) Name() string {
|
|
|
|
return (*netmap.Filter)(f).
|
|
|
|
GetName()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetName sets filter name.
|
|
|
|
func (f *Filter) SetName(name string) {
|
|
|
|
(*netmap.Filter)(f).
|
|
|
|
SetName(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Operation returns filtering operation.
|
|
|
|
func (f *Filter) Operation() Operation {
|
|
|
|
return OperationFromV2(
|
|
|
|
(*netmap.Filter)(f).
|
|
|
|
GetOp(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOperation sets filtering operation.
|
|
|
|
func (f *Filter) SetOperation(op Operation) {
|
|
|
|
(*netmap.Filter)(f).
|
|
|
|
SetOp(op.ToV2())
|
|
|
|
}
|
|
|
|
|
2020-11-05 11:08:19 +00:00
|
|
|
func filtersFromV2(fs []*netmap.Filter) []*Filter {
|
2020-11-05 10:02:31 +00:00
|
|
|
res := make([]*Filter, 0, len(fs))
|
|
|
|
|
|
|
|
for i := range fs {
|
|
|
|
res = append(res, NewFilterFromV2(fs[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-11-05 11:08:19 +00:00
|
|
|
// InnerFilters returns list of inner filters.
|
|
|
|
func (f *Filter) InnerFilters() []*Filter {
|
|
|
|
return filtersFromV2(
|
|
|
|
(*netmap.Filter)(f).
|
|
|
|
GetFilters(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:58:37 +00:00
|
|
|
func filtersToV2(fs []*Filter) (fsV2 []*netmap.Filter) {
|
|
|
|
if fs != nil {
|
|
|
|
fsV2 = make([]*netmap.Filter, 0, len(fs))
|
2020-11-05 10:02:31 +00:00
|
|
|
|
2021-03-12 12:58:37 +00:00
|
|
|
for i := range fs {
|
|
|
|
fsV2 = append(fsV2, fs[i].ToV2())
|
|
|
|
}
|
2020-11-05 10:02:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 12:58:37 +00:00
|
|
|
return
|
2020-11-05 11:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetInnerFilters sets list of inner filters.
|
|
|
|
func (f *Filter) SetInnerFilters(fs ...*Filter) {
|
2020-11-05 10:02:31 +00:00
|
|
|
(*netmap.Filter)(f).
|
2020-11-05 11:08:19 +00:00
|
|
|
SetFilters(filtersToV2(fs))
|
2020-11-05 10:02:31 +00:00
|
|
|
}
|
2020-11-13 08:45:59 +00:00
|
|
|
|
|
|
|
// Marshal marshals Filter into a protobuf binary form.
|
|
|
|
//
|
|
|
|
// Buffer is allocated when the argument is empty.
|
|
|
|
// Otherwise, the first buffer is used.
|
|
|
|
func (f *Filter) Marshal(b ...[]byte) ([]byte, error) {
|
|
|
|
var buf []byte
|
|
|
|
if len(b) > 0 {
|
|
|
|
buf = b[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*netmap.Filter)(f).StableMarshal(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of Filter.
|
|
|
|
func (f *Filter) Unmarshal(data []byte) error {
|
|
|
|
return (*netmap.Filter)(f).
|
|
|
|
Unmarshal(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes Filter to protobuf JSON format.
|
|
|
|
func (f *Filter) MarshalJSON() ([]byte, error) {
|
|
|
|
return (*netmap.Filter)(f).
|
|
|
|
MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes Filter from protobuf JSON format.
|
|
|
|
func (f *Filter) UnmarshalJSON(data []byte) error {
|
|
|
|
return (*netmap.Filter)(f).
|
|
|
|
UnmarshalJSON(data)
|
|
|
|
}
|