[#189] sdk/netmap: Refactor PlacementPolicy type

Replace alias to v2 type PlacementPolicy with v2-compatible implementation.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-05 14:08:19 +03:00 committed by Alex Vanin
parent 6a29d9c360
commit 9f19139999
5 changed files with 209 additions and 13 deletions

View file

@ -196,11 +196,7 @@ func (f *Filter) SetOperation(op Operation) {
SetOp(op.ToV2())
}
// InnerFilters returns list of inner filters.
func (f *Filter) InnerFilters() []*Filter {
fs := (*netmap.Filter)(f).
GetFilters()
func filtersFromV2(fs []*netmap.Filter) []*Filter {
res := make([]*Filter, 0, len(fs))
for i := range fs {
@ -210,14 +206,26 @@ func (f *Filter) InnerFilters() []*Filter {
return res
}
// SetInnerFilters sets list of inner filters.
func (f *Filter) SetInnerFilters(fs ...*Filter) {
// InnerFilters returns list of inner filters.
func (f *Filter) InnerFilters() []*Filter {
return filtersFromV2(
(*netmap.Filter)(f).
GetFilters(),
)
}
func filtersToV2(fs []*Filter) []*netmap.Filter {
fsV2 := make([]*netmap.Filter, 0, len(fs))
for i := range fs {
fsV2 = append(fsV2, fs[i].ToV2())
}
(*netmap.Filter)(f).
SetFilters(fsV2)
return fsV2
}
// SetInnerFilters sets list of inner filters.
func (f *Filter) SetInnerFilters(fs ...*Filter) {
(*netmap.Filter)(f).
SetFilters(filtersToV2(fs))
}

View file

@ -4,13 +4,113 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
)
// fixme: make types instead of aliases to v2 structures
type PlacementPolicy = netmap.PlacementPolicy
// PlacementPolicy represents v2-compatible placement policy.
type PlacementPolicy netmap.PlacementPolicy
// PlacementPolicyToJSON encodes PlacementPolicy to JSON format.
func PlacementPolicyToJSON(p *PlacementPolicy) ([]byte, error) {
return netmap.PlacementPolicyToJSON(p)
return netmap.PlacementPolicyToJSON(p.ToV2())
}
// PlacementPolicyFromJSON decodes PlacementPolicy from JSON format.
func PlacementPolicyFromJSON(data []byte) (*PlacementPolicy, error) {
return netmap.PlacementPolicyFromJSON(data)
p, err := netmap.PlacementPolicyFromJSON(data)
if err != nil {
return nil, err
}
return NewPlacementPolicyFromV2(p), nil
}
// NewPlacementPolicy creates and returns new PlacementPolicy instance.
func NewPlacementPolicy() *PlacementPolicy {
return NewPlacementPolicyFromV2(new(netmap.PlacementPolicy))
}
// NewPlacementPolicyFromV2 converts v2 PlacementPolicy to PlacementPolicy.
func NewPlacementPolicyFromV2(f *netmap.PlacementPolicy) *PlacementPolicy {
return (*PlacementPolicy)(f)
}
// ToV2 converts PlacementPolicy to v2 PlacementPolicy.
func (p *PlacementPolicy) ToV2() *netmap.PlacementPolicy {
return (*netmap.PlacementPolicy)(p)
}
// Replicas returns list of object replica descriptors.
func (p *PlacementPolicy) Replicas() []*Replica {
rs := (*netmap.PlacementPolicy)(p).
GetReplicas()
res := make([]*Replica, 0, len(rs))
for i := range rs {
res = append(res, NewReplicaFromV2(rs[i]))
}
return res
}
// SetReplicas sets list of object replica descriptors.
func (p *PlacementPolicy) SetReplicas(rs ...*Replica) {
rsV2 := make([]*netmap.Replica, 0, len(rs))
for i := range rs {
rsV2 = append(rsV2, rs[i].ToV2())
}
(*netmap.PlacementPolicy)(p).
SetReplicas(rsV2)
}
// ContainerBackupFactor returns container backup factor.
func (p *PlacementPolicy) ContainerBackupFactor() uint32 {
return (*netmap.PlacementPolicy)(p).
GetContainerBackupFactor()
}
// SetContainerBackupFactor sets container backup factor.
func (p *PlacementPolicy) SetContainerBackupFactor(f uint32) {
(*netmap.PlacementPolicy)(p).
SetContainerBackupFactor(f)
}
// Selector returns set of selectors to form the container's nodes subset.
func (p *PlacementPolicy) Selectors() []*Selector {
rs := (*netmap.PlacementPolicy)(p).
GetSelectors()
res := make([]*Selector, 0, len(rs))
for i := range rs {
res = append(res, NewSelectorFromV2(rs[i]))
}
return res
}
// SetSelectors sets set of selectors to form the container's nodes subset.
func (p *PlacementPolicy) SetSelectors(ss ...*Selector) {
rsV2 := make([]*netmap.Selector, 0, len(ss))
for i := range ss {
rsV2 = append(rsV2, ss[i].ToV2())
}
(*netmap.PlacementPolicy)(p).
SetSelectors(rsV2)
}
// Filters returns list of named filters to reference in selectors.
func (p *PlacementPolicy) Filters() []*Filter {
return filtersFromV2(
(*netmap.PlacementPolicy)(p).
GetFilters(),
)
}
// SetFilters sets list of named filters to reference in selectors.
func (p *PlacementPolicy) SetFilters(fs ...*Filter) {
(*netmap.PlacementPolicy)(p).
SetFilters(filtersToV2(fs))
}

69
pkg/netmap/policy_test.go Normal file
View file

@ -0,0 +1,69 @@
package netmap
import (
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
"github.com/stretchr/testify/require"
)
func TestPlacementPolicyFromV2(t *testing.T) {
pV2 := new(netmap.PlacementPolicy)
pV2.SetReplicas([]*netmap.Replica{
testReplica().ToV2(),
testReplica().ToV2(),
})
pV2.SetContainerBackupFactor(3)
pV2.SetSelectors([]*netmap.Selector{
testSelector().ToV2(),
testSelector().ToV2(),
})
pV2.SetFilters([]*netmap.Filter{
testFilter().ToV2(),
testFilter().ToV2(),
})
p := NewPlacementPolicyFromV2(pV2)
require.Equal(t, pV2, p.ToV2())
}
func TestPlacementPolicy_Replicas(t *testing.T) {
p := NewPlacementPolicy()
rs := []*Replica{testReplica(), testReplica()}
p.SetReplicas(rs...)
require.Equal(t, rs, p.Replicas())
}
func TestPlacementPolicy_ContainerBackupFactor(t *testing.T) {
p := NewPlacementPolicy()
f := uint32(3)
p.SetContainerBackupFactor(f)
require.Equal(t, f, p.ContainerBackupFactor())
}
func TestPlacementPolicy_Selectors(t *testing.T) {
p := NewPlacementPolicy()
ss := []*Selector{testSelector(), testSelector()}
p.SetSelectors(ss...)
require.Equal(t, ss, p.Selectors())
}
func TestPlacementPolicy_Filters(t *testing.T) {
p := NewPlacementPolicy()
fs := []*Filter{testFilter(), testFilter()}
p.SetFilters(fs...)
require.Equal(t, fs, p.Filters())
}

View file

@ -7,6 +7,14 @@ import (
"github.com/stretchr/testify/require"
)
func testReplica() *Replica {
r := new(Replica)
r.SetCount(3)
r.SetSelector("selector")
return r
}
func TestReplicaFromV2(t *testing.T) {
rV2 := new(netmap.Replica)
rV2.SetCount(3)

View file

@ -239,6 +239,17 @@ func TestPlacementPolicy_ProcessSelectorsInvalid(t *testing.T) {
}
}
func testSelector() *Selector {
s := new(Selector)
s.SetName("name")
s.SetCount(3)
s.SetFilter("filter")
s.SetAttribute("attribute")
s.SetClause(ClauseDistinct)
return s
}
func TestSelectorFromV2(t *testing.T) {
sV2 := new(netmap.Selector)
sV2.SetName("name")