2021-10-27 10:00:35 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Filter represents v2-compatible netmap filter.
|
|
|
|
type Filter netmap.Filter
|
|
|
|
|
|
|
|
// MainFilterName is a name of the filter
|
|
|
|
// which points to the whole netmap.
|
|
|
|
const MainFilterName = "*"
|
|
|
|
|
|
|
|
// applyFilter applies named filter to b.
|
2022-05-20 12:45:12 +00:00
|
|
|
func (c *context) applyFilter(name string, b *Node) bool {
|
2021-10-27 10:00:35 +00:00
|
|
|
return name == MainFilterName || c.match(c.Filters[name], b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// processFilters processes filters and returns error is any of them is invalid.
|
2022-05-20 12:45:12 +00:00
|
|
|
func (c *context) processFilters(p *PlacementPolicy) error {
|
2022-03-11 09:13:08 +00:00
|
|
|
filters := p.Filters()
|
|
|
|
for i := range filters {
|
|
|
|
if err := c.processFilter(&filters[i], true); err != nil {
|
2021-10-27 10:00:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-20 12:45:12 +00:00
|
|
|
func (c *context) processFilter(f *Filter, top bool) error {
|
2021-10-27 10:00:35 +00:00
|
|
|
if f == nil {
|
|
|
|
return fmt.Errorf("%w: FILTER", ErrMissingField)
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Name() == MainFilterName {
|
|
|
|
return fmt.Errorf("%w: '*' is reserved", ErrInvalidFilterName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if top && f.Name() == "" {
|
|
|
|
return ErrUnnamedTopFilter
|
|
|
|
}
|
|
|
|
|
|
|
|
if !top && f.Name() != "" && c.Filters[f.Name()] == nil {
|
|
|
|
return fmt.Errorf("%w: '%s'", ErrFilterNotFound, f.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
switch f.Operation() {
|
|
|
|
case OpAND, OpOR:
|
|
|
|
for _, flt := range f.InnerFilters() {
|
2022-03-11 09:13:08 +00:00
|
|
|
if err := c.processFilter(&flt, false); err != nil {
|
2021-10-27 10:00:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if len(f.InnerFilters()) != 0 {
|
|
|
|
return ErrNonEmptyFilters
|
|
|
|
} else if !top && f.Name() != "" { // named reference
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch f.Operation() {
|
|
|
|
case OpEQ, OpNE:
|
|
|
|
case OpGT, OpGE, OpLT, OpLE:
|
|
|
|
n, err := strconv.ParseUint(f.Value(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: '%s'", ErrInvalidNumber, f.Value())
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:13:08 +00:00
|
|
|
c.numCache[f.Value()] = n
|
2021-10-27 10:00:35 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("%w: %s", ErrInvalidFilterOp, f.Operation())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if top {
|
|
|
|
c.Filters[f.Name()] = f
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-20 12:45:12 +00:00
|
|
|
func (c *context) match(f *Filter, b *Node) bool {
|
2021-10-27 10:00:35 +00:00
|
|
|
switch f.Operation() {
|
|
|
|
case OpAND, OpOR:
|
|
|
|
for _, lf := range f.InnerFilters() {
|
|
|
|
if lf.Name() != "" {
|
2022-03-11 09:13:08 +00:00
|
|
|
lf = *c.Filters[lf.Name()]
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 09:13:08 +00:00
|
|
|
ok := c.match(&lf, b)
|
2021-10-27 10:00:35 +00:00
|
|
|
if ok == (f.Operation() == OpOR) {
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Operation() == OpAND
|
|
|
|
default:
|
|
|
|
return c.matchKeyValue(f, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 12:45:12 +00:00
|
|
|
func (c *context) matchKeyValue(f *Filter, b *Node) bool {
|
2021-10-27 10:00:35 +00:00
|
|
|
switch f.Operation() {
|
|
|
|
case OpEQ:
|
|
|
|
return b.Attribute(f.Key()) == f.Value()
|
|
|
|
case OpNE:
|
|
|
|
return b.Attribute(f.Key()) != f.Value()
|
|
|
|
default:
|
|
|
|
var attr uint64
|
|
|
|
|
|
|
|
switch f.Key() {
|
|
|
|
case AttrPrice:
|
|
|
|
attr = b.Price
|
|
|
|
case AttrCapacity:
|
|
|
|
attr = b.Capacity
|
|
|
|
default:
|
|
|
|
var err error
|
|
|
|
|
|
|
|
attr, err = strconv.ParseUint(b.Attribute(f.Key()), 10, 64)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch f.Operation() {
|
|
|
|
case OpGT:
|
2022-03-11 09:13:08 +00:00
|
|
|
return attr > c.numCache[f.Value()]
|
2021-10-27 10:00:35 +00:00
|
|
|
case OpGE:
|
2022-03-11 09:13:08 +00:00
|
|
|
return attr >= c.numCache[f.Value()]
|
2021-10-27 10:00:35 +00:00
|
|
|
case OpLT:
|
2022-03-11 09:13:08 +00:00
|
|
|
return attr < c.numCache[f.Value()]
|
2021-10-27 10:00:35 +00:00
|
|
|
case OpLE:
|
2022-03-11 09:13:08 +00:00
|
|
|
return attr <= c.numCache[f.Value()]
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFilter creates and returns new Filter instance.
|
|
|
|
//
|
|
|
|
// Defaults:
|
|
|
|
// - name: "";
|
|
|
|
// - key: "";
|
|
|
|
// - value: "";
|
|
|
|
// - operation: 0;
|
|
|
|
// - filters: nil.
|
|
|
|
func NewFilter() *Filter {
|
|
|
|
return NewFilterFromV2(new(netmap.Filter))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFilterFromV2 converts v2 Filter to Filter.
|
|
|
|
//
|
|
|
|
// Nil netmap.Filter converts to nil.
|
|
|
|
func NewFilterFromV2(f *netmap.Filter) *Filter {
|
|
|
|
return (*Filter)(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToV2 converts Filter to v2 Filter.
|
|
|
|
//
|
|
|
|
// Nil Filter converts to nil.
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:13:08 +00:00
|
|
|
func filtersFromV2(fs []netmap.Filter) []Filter {
|
2021-10-27 10:00:35 +00:00
|
|
|
if fs == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:13:08 +00:00
|
|
|
res := make([]Filter, len(fs))
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
for i := range fs {
|
2022-03-11 09:13:08 +00:00
|
|
|
res[i] = *NewFilterFromV2(&fs[i])
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// InnerFilters returns list of inner filters.
|
2022-03-11 09:13:08 +00:00
|
|
|
func (f *Filter) InnerFilters() []Filter {
|
2021-10-27 10:00:35 +00:00
|
|
|
return filtersFromV2((*netmap.Filter)(f).GetFilters())
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:13:08 +00:00
|
|
|
func filtersToV2(fs []Filter) (fsV2 []netmap.Filter) {
|
2021-10-27 10:00:35 +00:00
|
|
|
if fs != nil {
|
2022-03-11 09:13:08 +00:00
|
|
|
fsV2 = make([]netmap.Filter, len(fs))
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
for i := range fs {
|
2022-03-11 09:13:08 +00:00
|
|
|
fsV2[i] = *fs[i].ToV2()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetInnerFilters sets list of inner filters.
|
2022-03-11 09:13:08 +00:00
|
|
|
func (f *Filter) SetInnerFilters(fs ...Filter) {
|
2021-10-27 10:00:35 +00:00
|
|
|
(*netmap.Filter)(f).
|
|
|
|
SetFilters(filtersToV2(fs))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal marshals Filter into a protobuf binary form.
|
|
|
|
func (f *Filter) Marshal() ([]byte, error) {
|
2022-05-30 19:05:35 +00:00
|
|
|
return (*netmap.Filter)(f).StableMarshal(nil), nil
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|