2020-09-11 14:44:34 +00:00
|
|
|
package policy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2020-09-11 14:44:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidNumber = errors.New("policy: expected positive integer")
|
2020-09-15 14:10:38 +00:00
|
|
|
ErrUnknownClause = errors.New("policy: unknown clause")
|
2020-09-11 14:44:34 +00:00
|
|
|
ErrUnknownOp = errors.New("policy: unknown operation")
|
|
|
|
ErrUnknownFilter = errors.New("policy: filter not found")
|
|
|
|
ErrUnknownSelector = errors.New("policy: selector not found")
|
|
|
|
)
|
|
|
|
|
|
|
|
func parse(s string) (*query, error) {
|
|
|
|
q := new(query)
|
|
|
|
err := parser.Parse(strings.NewReader(s), q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return q, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses s into a placement policy.
|
|
|
|
func Parse(s string) (*netmap.PlacementPolicy, error) {
|
|
|
|
q, err := parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
seenFilters := map[string]bool{}
|
|
|
|
fs := make([]*netmap.Filter, 0, len(q.Filters))
|
|
|
|
for _, qf := range q.Filters {
|
|
|
|
f, err := filterFromOrChain(qf.Value, seenFilters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f.SetName(qf.Name)
|
|
|
|
fs = append(fs, f)
|
|
|
|
seenFilters[qf.Name] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
seenSelectors := map[string]bool{}
|
|
|
|
ss := make([]*netmap.Selector, 0, len(q.Selectors))
|
|
|
|
for _, qs := range q.Selectors {
|
2020-11-05 15:51:43 +00:00
|
|
|
if qs.Filter != netmap.MainFilterName && !seenFilters[qs.Filter] {
|
2020-09-11 14:44:34 +00:00
|
|
|
return nil, fmt.Errorf("%w: '%s'", ErrUnknownFilter, qs.Filter)
|
|
|
|
}
|
2020-11-05 15:51:43 +00:00
|
|
|
s := netmap.NewSelector()
|
2020-10-14 15:25:21 +00:00
|
|
|
switch len(qs.Bucket) {
|
|
|
|
case 1: // only bucket
|
|
|
|
s.SetAttribute(qs.Bucket[0])
|
|
|
|
case 2: // clause + bucket
|
|
|
|
s.SetClause(clauseFromString(qs.Bucket[0]))
|
|
|
|
s.SetAttribute(qs.Bucket[1])
|
2020-09-11 14:44:34 +00:00
|
|
|
}
|
|
|
|
s.SetName(qs.Name)
|
|
|
|
seenSelectors[qs.Name] = true
|
|
|
|
s.SetFilter(qs.Filter)
|
|
|
|
if qs.Count == 0 {
|
|
|
|
return nil, fmt.Errorf("%w: SELECT", ErrInvalidNumber)
|
|
|
|
}
|
|
|
|
s.SetCount(qs.Count)
|
|
|
|
ss = append(ss, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
rs := make([]*netmap.Replica, 0, len(q.Replicas))
|
|
|
|
for _, qr := range q.Replicas {
|
2020-11-05 15:51:43 +00:00
|
|
|
r := netmap.NewReplica()
|
2020-09-11 14:44:34 +00:00
|
|
|
if qr.Selector != "" {
|
|
|
|
if !seenSelectors[qr.Selector] {
|
|
|
|
return nil, fmt.Errorf("%w: '%s'", ErrUnknownSelector, qr.Selector)
|
|
|
|
}
|
|
|
|
r.SetSelector(qr.Selector)
|
|
|
|
}
|
|
|
|
if qr.Count == 0 {
|
|
|
|
return nil, fmt.Errorf("%w: REP", ErrInvalidNumber)
|
|
|
|
}
|
|
|
|
r.SetCount(uint32(qr.Count))
|
|
|
|
rs = append(rs, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := new(netmap.PlacementPolicy)
|
2020-11-05 15:51:43 +00:00
|
|
|
p.SetFilters(fs...)
|
|
|
|
p.SetSelectors(ss...)
|
|
|
|
p.SetReplicas(rs...)
|
2020-09-11 14:44:34 +00:00
|
|
|
p.SetContainerBackupFactor(q.CBF)
|
2020-11-05 15:51:43 +00:00
|
|
|
|
2020-09-11 14:44:34 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2020-10-14 15:25:21 +00:00
|
|
|
func clauseFromString(s string) netmap.Clause {
|
|
|
|
switch strings.ToUpper(s) {
|
|
|
|
case "SAME":
|
2020-11-05 15:51:43 +00:00
|
|
|
return netmap.ClauseSame
|
2020-10-14 15:25:21 +00:00
|
|
|
case "DISTINCT":
|
2020-11-05 15:51:43 +00:00
|
|
|
return netmap.ClauseDistinct
|
2020-10-14 15:25:21 +00:00
|
|
|
default:
|
2020-11-05 15:51:43 +00:00
|
|
|
return 0
|
2020-10-14 15:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 14:44:34 +00:00
|
|
|
func filterFromOrChain(expr *orChain, seen map[string]bool) (*netmap.Filter, error) {
|
|
|
|
var fs []*netmap.Filter
|
|
|
|
for _, ac := range expr.Clauses {
|
|
|
|
f, err := filterFromAndChain(ac, seen)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fs = append(fs, f)
|
|
|
|
}
|
|
|
|
if len(fs) == 1 {
|
|
|
|
return fs[0], nil
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
f := netmap.NewFilter()
|
|
|
|
f.SetOperation(netmap.OpOR)
|
|
|
|
f.SetInnerFilters(fs...)
|
2020-09-11 14:44:34 +00:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterFromAndChain(expr *andChain, seen map[string]bool) (*netmap.Filter, error) {
|
|
|
|
var fs []*netmap.Filter
|
|
|
|
for _, fe := range expr.Clauses {
|
|
|
|
var f *netmap.Filter
|
|
|
|
var err error
|
|
|
|
if fe.Expr != nil {
|
|
|
|
f, err = filterFromSimpleExpr(fe.Expr, seen)
|
|
|
|
} else {
|
2020-11-05 15:51:43 +00:00
|
|
|
f = netmap.NewFilter()
|
2020-09-11 14:44:34 +00:00
|
|
|
f.SetName(fe.Reference)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fs = append(fs, f)
|
|
|
|
}
|
|
|
|
if len(fs) == 1 {
|
|
|
|
return fs[0], nil
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
f := netmap.NewFilter()
|
|
|
|
f.SetOperation(netmap.OpAND)
|
|
|
|
f.SetInnerFilters(fs...)
|
2020-09-11 14:44:34 +00:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterFromSimpleExpr(se *simpleExpr, seen map[string]bool) (*netmap.Filter, error) {
|
2020-11-05 15:51:43 +00:00
|
|
|
f := netmap.NewFilter()
|
2020-09-11 14:44:34 +00:00
|
|
|
f.SetKey(se.Key)
|
|
|
|
switch se.Op {
|
|
|
|
case "EQ":
|
2020-11-05 15:51:43 +00:00
|
|
|
f.SetOperation(netmap.OpEQ)
|
2020-09-11 14:44:34 +00:00
|
|
|
case "NE":
|
2020-11-05 15:51:43 +00:00
|
|
|
f.SetOperation(netmap.OpNE)
|
2020-09-11 14:44:34 +00:00
|
|
|
case "GE":
|
2020-11-05 15:51:43 +00:00
|
|
|
f.SetOperation(netmap.OpGE)
|
2020-09-11 14:44:34 +00:00
|
|
|
case "GT":
|
2020-11-05 15:51:43 +00:00
|
|
|
f.SetOperation(netmap.OpGT)
|
2020-09-11 14:44:34 +00:00
|
|
|
case "LE":
|
2020-11-05 15:51:43 +00:00
|
|
|
f.SetOperation(netmap.OpLE)
|
2020-09-11 14:44:34 +00:00
|
|
|
case "LT":
|
2020-11-05 15:51:43 +00:00
|
|
|
f.SetOperation(netmap.OpLT)
|
2020-09-11 14:44:34 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("%w: '%s'", ErrUnknownOp, se.Op)
|
|
|
|
}
|
|
|
|
f.SetValue(se.Value)
|
|
|
|
return f, nil
|
|
|
|
}
|