2021-10-27 10:00:35 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
2021-11-26 12:31:36 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
subnetid "github.com/nspcc-dev/neofs-sdk-go/subnet/id"
|
2021-10-27 10:00:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PlacementPolicy represents v2-compatible placement policy.
|
|
|
|
type PlacementPolicy netmap.PlacementPolicy
|
|
|
|
|
|
|
|
// NewPlacementPolicy creates and returns new PlacementPolicy instance.
|
|
|
|
//
|
|
|
|
// Defaults:
|
|
|
|
// - backupFactor: 0;
|
|
|
|
// - replicas nil;
|
|
|
|
// - selectors nil;
|
|
|
|
// - filters nil.
|
|
|
|
func NewPlacementPolicy() *PlacementPolicy {
|
|
|
|
return NewPlacementPolicyFromV2(new(netmap.PlacementPolicy))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPlacementPolicyFromV2 converts v2 PlacementPolicy to PlacementPolicy.
|
|
|
|
//
|
|
|
|
// Nil netmap.PlacementPolicy converts to nil.
|
|
|
|
func NewPlacementPolicyFromV2(f *netmap.PlacementPolicy) *PlacementPolicy {
|
|
|
|
return (*PlacementPolicy)(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToV2 converts PlacementPolicy to v2 PlacementPolicy.
|
|
|
|
//
|
|
|
|
// Nil PlacementPolicy converts to nil.
|
|
|
|
func (p *PlacementPolicy) ToV2() *netmap.PlacementPolicy {
|
|
|
|
return (*netmap.PlacementPolicy)(p)
|
|
|
|
}
|
|
|
|
|
2021-11-26 12:31:36 +00:00
|
|
|
// SubnetID returns subnet to select nodes from.
|
|
|
|
func (p *PlacementPolicy) SubnetID() *subnetid.ID {
|
|
|
|
return (*subnetid.ID)(
|
|
|
|
(*netmap.PlacementPolicy)(p).GetSubnetID())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSubnetID sets subnet to select nodes from.
|
|
|
|
func (p *PlacementPolicy) SetSubnetID(subnet *subnetid.ID) {
|
|
|
|
(*netmap.PlacementPolicy)(p).SetSubnetID((*refs.SubnetID)(subnet))
|
|
|
|
}
|
|
|
|
|
2021-10-27 10:00:35 +00:00
|
|
|
// Replicas returns list of object replica descriptors.
|
2022-03-11 09:13:08 +00:00
|
|
|
func (p *PlacementPolicy) Replicas() []Replica {
|
2021-10-27 10:00:35 +00:00
|
|
|
rs := (*netmap.PlacementPolicy)(p).
|
|
|
|
GetReplicas()
|
|
|
|
|
|
|
|
if rs == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:13:08 +00:00
|
|
|
res := make([]Replica, len(rs))
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
for i := range rs {
|
2022-03-11 09:13:08 +00:00
|
|
|
res[i] = *NewReplicaFromV2(&rs[i])
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetReplicas sets list of object replica descriptors.
|
2022-03-11 09:13:08 +00:00
|
|
|
func (p *PlacementPolicy) SetReplicas(rs ...Replica) {
|
|
|
|
var rsV2 []netmap.Replica
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
if rs != nil {
|
2022-03-11 09:13:08 +00:00
|
|
|
rsV2 = make([]netmap.Replica, len(rs))
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
for i := range rs {
|
2022-03-11 09:13:08 +00:00
|
|
|
rsV2[i] = *rs[i].ToV2()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(*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.
|
2022-03-11 09:13:08 +00:00
|
|
|
func (p *PlacementPolicy) Selectors() []Selector {
|
2021-10-27 10:00:35 +00:00
|
|
|
rs := (*netmap.PlacementPolicy)(p).
|
|
|
|
GetSelectors()
|
|
|
|
|
|
|
|
if rs == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:13:08 +00:00
|
|
|
res := make([]Selector, len(rs))
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
for i := range rs {
|
2022-03-11 09:13:08 +00:00
|
|
|
res[i] = *NewSelectorFromV2(&rs[i])
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSelectors sets set of selectors to form the container's nodes subset.
|
2022-03-11 09:13:08 +00:00
|
|
|
func (p *PlacementPolicy) SetSelectors(ss ...Selector) {
|
|
|
|
var ssV2 []netmap.Selector
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
if ss != nil {
|
2022-03-11 09:13:08 +00:00
|
|
|
ssV2 = make([]netmap.Selector, len(ss))
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
for i := range ss {
|
2022-03-11 09:13:08 +00:00
|
|
|
ssV2[i] = *ss[i].ToV2()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(*netmap.PlacementPolicy)(p).SetSelectors(ssV2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filters returns list of named filters to reference in selectors.
|
2022-03-11 09:13:08 +00:00
|
|
|
func (p *PlacementPolicy) Filters() []Filter {
|
2021-10-27 10:00:35 +00:00
|
|
|
return filtersFromV2(
|
|
|
|
(*netmap.PlacementPolicy)(p).
|
|
|
|
GetFilters(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFilters sets list of named filters to reference in selectors.
|
2022-03-11 09:13:08 +00:00
|
|
|
func (p *PlacementPolicy) SetFilters(fs ...Filter) {
|
2021-10-27 10:00:35 +00:00
|
|
|
(*netmap.PlacementPolicy)(p).
|
|
|
|
SetFilters(filtersToV2(fs))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal marshals PlacementPolicy into a protobuf binary form.
|
|
|
|
func (p *PlacementPolicy) Marshal() ([]byte, error) {
|
2022-05-05 08:00:13 +00:00
|
|
|
return (*netmap.PlacementPolicy)(p).StableMarshal(nil), nil
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of PlacementPolicy.
|
|
|
|
func (p *PlacementPolicy) Unmarshal(data []byte) error {
|
|
|
|
return (*netmap.PlacementPolicy)(p).Unmarshal(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes PlacementPolicy to protobuf JSON format.
|
|
|
|
func (p *PlacementPolicy) MarshalJSON() ([]byte, error) {
|
|
|
|
return (*netmap.PlacementPolicy)(p).MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes PlacementPolicy from protobuf JSON format.
|
|
|
|
func (p *PlacementPolicy) UnmarshalJSON(data []byte) error {
|
|
|
|
return (*netmap.PlacementPolicy)(p).UnmarshalJSON(data)
|
|
|
|
}
|