[#35] policy: remove netmap/v2 dependency

Also, fix tests.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-09-13 11:04:51 +03:00 committed by Alex Vanin
parent 880f3a61e5
commit 6e3836cb1d
3 changed files with 144 additions and 165 deletions

View file

@ -5,7 +5,7 @@ import (
"fmt"
"strings"
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
)
type (
@ -38,17 +38,17 @@ type (
// ToJSON converts placement policy to JSON.
func ToJSON(np *netmap.PlacementPolicy) ([]byte, error) {
p := new(placement)
p.CBF = np.GetContainerBackupFactor()
p.Filters = make([]filter, len(np.GetFilters()))
for i, f := range np.GetFilters() {
p.CBF = np.ContainerBackupFactor()
p.Filters = make([]filter, len(np.Filters()))
for i, f := range np.Filters() {
p.Filters[i].fromNetmap(f)
}
p.Selectors = make([]selector, len(np.GetSelectors()))
for i, s := range np.GetSelectors() {
p.Selectors = make([]selector, len(np.Selectors()))
for i, s := range np.Selectors() {
p.Selectors[i].fromNetmap(s)
}
p.Replicas = make([]replica, len(np.GetReplicas()))
for i, r := range np.GetReplicas() {
p.Replicas = make([]replica, len(np.Replicas()))
for i, r := range np.Replicas() {
p.Replicas[i].fromNetmap(r)
}
return json.Marshal(p)
@ -91,10 +91,10 @@ func FromJSON(data []byte) (*netmap.PlacementPolicy, error) {
}
pp := new(netmap.PlacementPolicy)
pp.SetReplicas(rs)
pp.SetReplicas(rs...)
pp.SetContainerBackupFactor(p.CBF)
pp.SetFilters(fs)
pp.SetSelectors(ss)
pp.SetFilters(fs...)
pp.SetSelectors(ss...)
return pp, nil
}
@ -106,31 +106,30 @@ func (r *replica) toNetmap() *netmap.Replica {
}
func (r *replica) fromNetmap(nr *netmap.Replica) {
r.Count = nr.GetCount()
r.Selector = nr.GetSelector()
r.Count = nr.Count()
r.Selector = nr.Selector()
}
func (f *filter) toNetmap() (*netmap.Filter, error) {
var op netmap.Operation
switch strings.ToUpper(f.Op) {
case "EQ":
op = netmap.EQ
op = netmap.OpEQ
case "NE":
op = netmap.NE
op = netmap.OpNE
case "GT":
op = netmap.GT
op = netmap.OpGT
case "GE":
op = netmap.GE
op = netmap.OpGE
case "LT":
op = netmap.LT
op = netmap.OpLT
case "LE":
op = netmap.LE
op = netmap.OpLE
case "AND":
op = netmap.AND
op = netmap.OpAND
case "OR":
op = netmap.OR
op = netmap.OpOR
case "":
op = netmap.UnspecifiedOperation
default:
return nil, fmt.Errorf("%w: '%s'", ErrUnknownOp, f.Op)
}
@ -148,8 +147,8 @@ func (f *filter) toNetmap() (*netmap.Filter, error) {
}
nf := new(netmap.Filter)
nf.SetFilters(fs)
nf.SetOp(op)
nf.SetInnerFilters(fs...)
nf.SetOperation(op)
nf.SetName(f.Name)
nf.SetValue(f.Value)
nf.SetKey(f.Key)
@ -157,32 +156,32 @@ func (f *filter) toNetmap() (*netmap.Filter, error) {
}
func (f *filter) fromNetmap(nf *netmap.Filter) {
f.Name = nf.GetName()
f.Key = nf.GetKey()
f.Value = nf.GetValue()
switch nf.GetOp() {
case netmap.EQ:
f.Name = nf.Name()
f.Key = nf.Key()
f.Value = nf.Value()
switch nf.Operation() {
case netmap.OpEQ:
f.Op = "EQ"
case netmap.NE:
case netmap.OpNE:
f.Op = "NE"
case netmap.GT:
case netmap.OpGT:
f.Op = "GT"
case netmap.GE:
case netmap.OpGE:
f.Op = "GE"
case netmap.LT:
case netmap.OpLT:
f.Op = "LT"
case netmap.LE:
case netmap.OpLE:
f.Op = "LE"
case netmap.AND:
case netmap.OpAND:
f.Op = "AND"
case netmap.OR:
case netmap.OpOR:
f.Op = "OR"
default:
// do nothing
}
if nf.GetFilters() != nil {
f.Filters = make([]filter, len(nf.GetFilters()))
for i, sf := range nf.GetFilters() {
if nf.InnerFilters() != nil {
f.Filters = make([]filter, len(nf.InnerFilters()))
for i, sf := range nf.InnerFilters() {
f.Filters[i].fromNetmap(sf)
}
}
@ -192,9 +191,9 @@ func (s *selector) toNetmap() (*netmap.Selector, error) {
var c netmap.Clause
switch strings.ToUpper(s.Clause) {
case "SAME":
c = netmap.Same
c = netmap.ClauseSame
case "DISTINCT":
c = netmap.Distinct
c = netmap.ClauseDistinct
case "":
default:
return nil, fmt.Errorf("%w: '%s'", ErrUnknownClause, s.Clause)
@ -209,17 +208,17 @@ func (s *selector) toNetmap() (*netmap.Selector, error) {
}
func (s *selector) fromNetmap(ns *netmap.Selector) {
s.Name = ns.GetName()
s.Filter = ns.GetFilter()
s.Count = ns.GetCount()
s.Attribute = ns.GetAttribute()
switch ns.GetClause() {
case netmap.Same:
s.Name = ns.Name()
s.Filter = ns.Filter()
s.Count = ns.Count()
s.Attribute = ns.Attribute()
switch ns.Clause() {
case netmap.ClauseSame:
s.Clause = "same"
case netmap.Distinct:
case netmap.ClauseDistinct:
s.Clause = "distinct"
default:
// do nothing
}
s.Name = ns.GetName()
s.Name = ns.Name()
}

View file

@ -3,7 +3,7 @@ package policy
import (
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
"github.com/stretchr/testify/require"
)
@ -19,22 +19,21 @@ func TestToJSON(t *testing.T) {
}
t.Run("SimpleREP", func(t *testing.T) {
p := new(netmap.PlacementPolicy)
p.SetReplicas([]*netmap.Replica{newReplica("", 3)})
p.SetReplicas(newReplica("", 3))
check(t, p, `{"replicas":[{"count":3}]}`)
})
t.Run("REPWithCBF", func(t *testing.T) {
p := new(netmap.PlacementPolicy)
p.SetReplicas([]*netmap.Replica{newReplica("", 3)})
p.SetReplicas(newReplica("", 3))
p.SetContainerBackupFactor(3)
check(t, p, `{"replicas":[{"count":3}],"container_backup_factor":3}`)
})
t.Run("REPFromSelector", func(t *testing.T) {
p := new(netmap.PlacementPolicy)
p.SetReplicas([]*netmap.Replica{newReplica("Nodes", 3)})
p.SetReplicas(newReplica("Nodes", 3))
p.SetContainerBackupFactor(3)
p.SetSelectors([]*netmap.Selector{
newSelector(1, netmap.Distinct, "City", "", "Nodes"),
})
p.SetSelectors(
newSelector(1, netmap.ClauseDistinct, "City", "", "Nodes"))
check(t, p, `{
"replicas":[{"count":3,"selector":"Nodes"}],
"container_backup_factor":3,
@ -47,22 +46,21 @@ func TestToJSON(t *testing.T) {
})
t.Run("FilterOps", func(t *testing.T) {
p := new(netmap.PlacementPolicy)
p.SetReplicas([]*netmap.Replica{newReplica("Nodes", 3)})
p.SetReplicas(newReplica("Nodes", 3))
p.SetContainerBackupFactor(3)
p.SetSelectors([]*netmap.Selector{
newSelector(1, netmap.Same, "City", "Good", "Nodes"),
})
p.SetFilters([]*netmap.Filter{
newFilter("GoodRating", "Rating", "5", netmap.GE),
newFilter("Good", "", "", netmap.OR,
newFilter("GoodRating", "", "", netmap.UnspecifiedOperation),
newFilter("", "Attr1", "Val1", netmap.EQ),
newFilter("", "Attr2", "Val2", netmap.NE),
newFilter("", "", "", netmap.AND,
newFilter("", "Attr4", "2", netmap.LT),
newFilter("", "Attr5", "3", netmap.LE)),
newFilter("", "Attr3", "1", netmap.GT)),
})
p.SetSelectors(
newSelector(1, netmap.ClauseSame, "City", "Good", "Nodes"))
p.SetFilters(
newFilter("GoodRating", "Rating", "5", netmap.OpGE),
newFilter("Good", "", "", netmap.OpOR,
newFilter("GoodRating", "", "", 0),
newFilter("", "Attr1", "Val1", netmap.OpEQ),
newFilter("", "Attr2", "Val2", netmap.OpNE),
newFilter("", "", "", netmap.OpAND,
newFilter("", "Attr4", "2", netmap.OpLT),
newFilter("", "Attr5", "3", netmap.OpLE)),
newFilter("", "Attr3", "1", netmap.OpGT)),
)
check(t, p, `{
"replicas":[{"count":3,"selector":"Nodes"}],
"container_backup_factor":3,

View file

@ -6,16 +6,16 @@ import (
"math"
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
"github.com/stretchr/testify/require"
)
func TestSimple(t *testing.T) {
q := `REP 3`
expected := new(netmap.PlacementPolicy)
expected.SetFilters([]*netmap.Filter{})
expected.SetSelectors([]*netmap.Selector{})
expected.SetReplicas([]*netmap.Replica{newReplica("", 3)})
expected.SetFilters([]*netmap.Filter{}...)
expected.SetSelectors([]*netmap.Selector{}...)
expected.SetReplicas(newReplica("", 3))
r, err := Parse(q)
require.NoError(t, err)
@ -25,9 +25,9 @@ func TestSimple(t *testing.T) {
func TestSimpleWithHRWB(t *testing.T) {
q := `REP 3 CBF 4`
expected := new(netmap.PlacementPolicy)
expected.SetFilters([]*netmap.Filter{})
expected.SetSelectors([]*netmap.Selector{})
expected.SetReplicas([]*netmap.Replica{newReplica("", 3)})
expected.SetFilters([]*netmap.Filter{}...)
expected.SetSelectors([]*netmap.Selector{}...)
expected.SetReplicas(newReplica("", 3))
expected.SetContainerBackupFactor(4)
r, err := Parse(q)
@ -39,11 +39,9 @@ func TestFromSelect(t *testing.T) {
q := `REP 1 IN SPB
SELECT 1 IN City FROM * AS SPB`
expected := new(netmap.PlacementPolicy)
expected.SetFilters([]*netmap.Filter{})
expected.SetSelectors([]*netmap.Selector{
newSelector(1, netmap.UnspecifiedClause, "City", "*", "SPB"),
})
expected.SetReplicas([]*netmap.Replica{newReplica("SPB", 1)})
expected.SetFilters([]*netmap.Filter{}...)
expected.SetSelectors(newSelector(1, netmap.ClauseUnspecified, "City", "*", "SPB"))
expected.SetReplicas(newReplica("SPB", 1))
r, err := Parse(q)
require.NoError(t, err)
@ -57,9 +55,9 @@ func TestFromSelectNoAttribute(t *testing.T) {
SELECT 6 FROM *`
expected := new(netmap.PlacementPolicy)
expected.SetFilters([]*netmap.Filter{})
expected.SetSelectors([]*netmap.Selector{newSelector(6, netmap.UnspecifiedClause, "", "*", "")})
expected.SetReplicas([]*netmap.Replica{newReplica("", 2)})
expected.SetFilters([]*netmap.Filter{}...)
expected.SetSelectors(newSelector(6, netmap.ClauseUnspecified, "", "*", ""))
expected.SetReplicas(newReplica("", 2))
r, err := Parse(q)
require.NoError(t, err)
@ -72,9 +70,9 @@ func TestFromSelectNoAttribute(t *testing.T) {
FILTER StorageType EQ SSD AS F`
expected := new(netmap.PlacementPolicy)
expected.SetFilters([]*netmap.Filter{newFilter("F", "StorageType", "SSD", netmap.EQ)})
expected.SetSelectors([]*netmap.Selector{newSelector(6, netmap.UnspecifiedClause, "", "F", "")})
expected.SetReplicas([]*netmap.Replica{newReplica("", 2)})
expected.SetFilters(newFilter("F", "StorageType", "SSD", netmap.OpEQ))
expected.SetSelectors(newSelector(6, netmap.ClauseUnspecified, "", "F", ""))
expected.SetReplicas(newReplica("", 2))
r, err := Parse(q)
require.NoError(t, err)
@ -100,9 +98,9 @@ FILTER Property EQ %s AND Something NE 7 AS Filt`
r, err := Parse(q)
require.NoError(t, err)
expected := newFilter("Filt", "", "", netmap.AND,
newFilter("", "Property", s[1:len(s)-1], netmap.EQ),
newFilter("", "Something", "7", netmap.NE))
expected := newFilter("Filt", "", "", netmap.OpAND,
newFilter("", "Property", s[1:len(s)-1], netmap.OpEQ),
newFilter("", "Something", "7", netmap.OpNE))
require.EqualValues(t, []*netmap.Filter{expected}, r.Filters())
})
}
@ -114,13 +112,12 @@ SELECT 3 IN Country FROM *
SELECT 2 IN SAME City FROM *
SELECT 1 IN DISTINCT Continent FROM *`
expected := new(netmap.PlacementPolicy)
expected.SetFilters([]*netmap.Filter{})
expected.SetSelectors([]*netmap.Selector{
newSelector(3, netmap.UnspecifiedClause, "Country", "*", ""),
newSelector(2, netmap.Same, "City", "*", ""),
newSelector(1, netmap.Distinct, "Continent", "*", ""),
})
expected.SetReplicas([]*netmap.Replica{newReplica("", 4)})
expected.SetFilters([]*netmap.Filter{}...)
expected.SetSelectors(
newSelector(3, netmap.ClauseUnspecified, "Country", "*", ""),
newSelector(2, netmap.ClauseSame, "City", "*", ""),
newSelector(1, netmap.ClauseDistinct, "Continent", "*", ""))
expected.SetReplicas(newReplica("", 4))
r, err := Parse(q)
require.NoError(t, err)
@ -132,13 +129,10 @@ func TestSimpleFilter(t *testing.T) {
SELECT 1 IN City FROM Good
FILTER Rating GT 7 AS Good`
expected := new(netmap.PlacementPolicy)
expected.SetReplicas([]*netmap.Replica{newReplica("", 1)})
expected.SetSelectors([]*netmap.Selector{
newSelector(1, netmap.UnspecifiedClause, "City", "Good", ""),
})
expected.SetFilters([]*netmap.Filter{
newFilter("Good", "Rating", "7", netmap.GT),
})
expected.SetReplicas(newReplica("", 1))
expected.SetSelectors(
newSelector(1, netmap.ClauseUnspecified, "City", "Good", ""))
expected.SetFilters(newFilter("Good", "Rating", "7", netmap.OpGT))
r, err := Parse(q)
require.NoError(t, err)
@ -151,16 +145,15 @@ SELECT 2 IN City FROM Good
FILTER Country EQ "RU" AS FromRU
FILTER @FromRU AND Rating GT 7 AS Good`
expected := new(netmap.PlacementPolicy)
expected.SetReplicas([]*netmap.Replica{newReplica("", 1)})
expected.SetSelectors([]*netmap.Selector{
newSelector(2, netmap.UnspecifiedClause, "City", "Good", ""),
})
expected.SetFilters([]*netmap.Filter{
newFilter("FromRU", "Country", "RU", netmap.EQ),
newFilter("Good", "", "", netmap.AND,
newFilter("FromRU", "", "", netmap.UnspecifiedOperation),
newFilter("", "Rating", "7", netmap.GT)),
})
expected.SetReplicas(newReplica("", 1))
expected.SetSelectors(
newSelector(2, netmap.ClauseUnspecified, "City", "Good", ""))
expected.SetFilters(
newFilter("FromRU", "Country", "RU", netmap.OpEQ),
newFilter("Good", "", "", netmap.OpAND,
newFilter("FromRU", "", "", 0),
newFilter("", "Rating", "7", netmap.OpGT)),
)
r, err := Parse(q)
require.NoError(t, err)
@ -173,19 +166,16 @@ SELECT 2 IN City FROM Good
FILTER A GT 1 AND B GE 2 AND C LT 3 AND D LE 4
AND E EQ 5 AND F NE 6 AS Good`
expected := new(netmap.PlacementPolicy)
expected.SetReplicas([]*netmap.Replica{newReplica("", 1)})
expected.SetSelectors([]*netmap.Selector{
newSelector(2, netmap.UnspecifiedClause, "City", "Good", ""),
})
expected.SetFilters([]*netmap.Filter{
newFilter("Good", "", "", netmap.AND,
newFilter("", "A", "1", netmap.GT),
newFilter("", "B", "2", netmap.GE),
newFilter("", "C", "3", netmap.LT),
newFilter("", "D", "4", netmap.LE),
newFilter("", "E", "5", netmap.EQ),
newFilter("", "F", "6", netmap.NE)),
})
expected.SetReplicas(newReplica("", 1))
expected.SetSelectors(
newSelector(2, netmap.ClauseUnspecified, "City", "Good", ""))
expected.SetFilters(newFilter("Good", "", "", netmap.OpAND,
newFilter("", "A", "1", netmap.OpGT),
newFilter("", "B", "2", netmap.OpGE),
newFilter("", "C", "3", netmap.OpLT),
newFilter("", "D", "4", netmap.OpLE),
newFilter("", "E", "5", netmap.OpEQ),
newFilter("", "F", "6", netmap.OpNE)))
r, err := Parse(q)
require.NoError(t, err)
@ -197,19 +187,17 @@ func TestWithFilterPrecedence(t *testing.T) {
SELECT 1 IN City FROM SPBSSD AS SPB
FILTER City EQ "SPB" AND SSD EQ true OR City EQ "SPB" AND Rating GE 5 AS SPBSSD`
expected := new(netmap.PlacementPolicy)
expected.SetReplicas([]*netmap.Replica{newReplica("SPB", 7)})
expected.SetSelectors([]*netmap.Selector{
newSelector(1, netmap.UnspecifiedClause, "City", "SPBSSD", "SPB"),
})
expected.SetFilters([]*netmap.Filter{
newFilter("SPBSSD", "", "", netmap.OR,
newFilter("", "", "", netmap.AND,
newFilter("", "City", "SPB", netmap.EQ),
newFilter("", "SSD", "true", netmap.EQ)),
newFilter("", "", "", netmap.AND,
newFilter("", "City", "SPB", netmap.EQ),
newFilter("", "Rating", "5", netmap.GE))),
})
expected.SetReplicas(newReplica("SPB", 7))
expected.SetSelectors(
newSelector(1, netmap.ClauseUnspecified, "City", "SPBSSD", "SPB"))
expected.SetFilters(
newFilter("SPBSSD", "", "", netmap.OpOR,
newFilter("", "", "", netmap.OpAND,
newFilter("", "City", "SPB", netmap.OpEQ),
newFilter("", "SSD", "true", netmap.OpEQ)),
newFilter("", "", "", netmap.OpAND,
newFilter("", "City", "SPB", netmap.OpEQ),
newFilter("", "Rating", "5", netmap.OpGE))))
r, err := Parse(q)
require.NoError(t, err)
@ -222,18 +210,17 @@ SELECT 1 IN City FROM SPBSSD AS SPB
FILTER ( City EQ "SPB" OR SSD EQ true ) AND (City EQ "SPB" OR Rating GE 5) AS SPBSSD`
expected := new(netmap.PlacementPolicy)
expected.SetReplicas([]*netmap.Replica{newReplica("SPB", 7)})
expected.SetSelectors([]*netmap.Selector{
newSelector(1, netmap.UnspecifiedClause, "City", "SPBSSD", "SPB"),
})
expected.SetFilters([]*netmap.Filter{
newFilter("SPBSSD", "", "", netmap.AND,
newFilter("", "", "", netmap.OR,
newFilter("", "City", "SPB", netmap.EQ),
newFilter("", "SSD", "true", netmap.EQ)),
newFilter("", "", "", netmap.OR,
newFilter("", "City", "SPB", netmap.EQ),
newFilter("", "Rating", "5", netmap.GE)))})
expected.SetReplicas(newReplica("SPB", 7))
expected.SetSelectors(
newSelector(1, netmap.ClauseUnspecified, "City", "SPBSSD", "SPB"))
expected.SetFilters(
newFilter("SPBSSD", "", "", netmap.OpAND,
newFilter("", "", "", netmap.OpOR,
newFilter("", "City", "SPB", netmap.OpEQ),
newFilter("", "SSD", "true", netmap.OpEQ)),
newFilter("", "", "", netmap.OpOR,
newFilter("", "City", "SPB", netmap.OpEQ),
newFilter("", "Rating", "5", netmap.OpGE))))
r, err := Parse(q)
require.NoError(t, err)
@ -302,15 +289,10 @@ SELECT 1 FROM F AS S
FILTER "UN-LOCODE" EQ "RU LED" AS F`
expected := new(netmap.PlacementPolicy)
expected.SetReplicas([]*netmap.Replica{
newReplica("S", 1),
})
expected.SetSelectors([]*netmap.Selector{
newSelector(1, netmap.UnspecifiedClause, "", "F", "S"),
})
expected.SetFilters([]*netmap.Filter{
newFilter("F", "UN-LOCODE", "RU LED", netmap.EQ),
})
expected.SetReplicas(newReplica("S", 1))
expected.SetSelectors(
newSelector(1, netmap.ClauseUnspecified, "", "F", "S"))
expected.SetFilters(newFilter("F", "UN-LOCODE", "RU LED", netmap.OpEQ))
r, err := Parse(q)
require.NoError(t, err)
@ -322,8 +304,8 @@ func newFilter(name, key, value string, op netmap.Operation, sub ...*netmap.Filt
f.SetName(name)
f.SetKey(key)
f.SetValue(value)
f.SetOp(op)
f.SetFilters(sub)
f.SetOperation(op)
f.SetInnerFilters(sub...)
return f
}