[#344] netmap: Add method Clone
Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
This commit is contained in:
parent
69b0711d12
commit
224e921447
6 changed files with 113 additions and 0 deletions
|
@ -1,6 +1,9 @@
|
||||||
package netmap
|
package netmap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/session"
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/session"
|
||||||
)
|
)
|
||||||
|
@ -382,6 +385,15 @@ func (a *Attribute) SetParents(parent []string) {
|
||||||
a.parents = parent
|
a.parents = parent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clone returns a copy of Attribute.
|
||||||
|
func (a *Attribute) Clone() *Attribute {
|
||||||
|
return &Attribute{
|
||||||
|
parents: slices.Clone(a.parents),
|
||||||
|
value: a.value,
|
||||||
|
key: a.key,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (ni *NodeInfo) GetPublicKey() []byte {
|
func (ni *NodeInfo) GetPublicKey() []byte {
|
||||||
if ni != nil {
|
if ni != nil {
|
||||||
return ni.publicKey
|
return ni.publicKey
|
||||||
|
@ -465,6 +477,19 @@ func (ni *NodeInfo) SetState(state NodeState) {
|
||||||
ni.state = state
|
ni.state = state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clone returns a copy of NodeInfo.
|
||||||
|
func (ni *NodeInfo) Clone() *NodeInfo {
|
||||||
|
dst := NodeInfo{
|
||||||
|
addresses: slices.Clone(ni.addresses),
|
||||||
|
publicKey: bytes.Clone(ni.publicKey),
|
||||||
|
state: ni.state,
|
||||||
|
}
|
||||||
|
for _, v := range ni.attributes {
|
||||||
|
dst.attributes = append(dst.attributes, *v.Clone())
|
||||||
|
}
|
||||||
|
return &dst
|
||||||
|
}
|
||||||
|
|
||||||
func (l *LocalNodeInfoResponseBody) GetVersion() *refs.Version {
|
func (l *LocalNodeInfoResponseBody) GetVersion() *refs.Version {
|
||||||
if l != nil {
|
if l != nil {
|
||||||
return l.version
|
return l.version
|
||||||
|
|
48
api/netmap/types_test.go
Normal file
48
api/netmap/types_test.go
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
package netmap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"slices"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNodeInfo_Clone(t *testing.T) {
|
||||||
|
var ni NodeInfo
|
||||||
|
ni.publicKey = []byte{2}
|
||||||
|
attr := Attribute{
|
||||||
|
key: "key",
|
||||||
|
value: "value",
|
||||||
|
parents: []string{"parent", "parent2"},
|
||||||
|
}
|
||||||
|
ni.attributes = []Attribute{attr}
|
||||||
|
ni.addresses = []string{"5", "6"}
|
||||||
|
|
||||||
|
c := ni.Clone()
|
||||||
|
|
||||||
|
require.True(t, c != &ni)
|
||||||
|
require.True(t, bytes.Equal(c.publicKey, ni.publicKey))
|
||||||
|
require.True(t, &(c.publicKey[0]) != &(ni.publicKey[0]))
|
||||||
|
require.True(t, &(c.attributes[0]) != &(ni.attributes[0]))
|
||||||
|
require.True(t, slices.Compare(c.addresses, ni.addresses) == 0)
|
||||||
|
require.True(t, &(c.addresses[0]) != &(ni.addresses[0]))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAttribute_Clone(t *testing.T) {
|
||||||
|
attr := Attribute{
|
||||||
|
key: "key",
|
||||||
|
value: "value",
|
||||||
|
parents: []string{"parent1", "parent2"},
|
||||||
|
}
|
||||||
|
|
||||||
|
c := attr.Clone()
|
||||||
|
|
||||||
|
require.True(t, c != &attr)
|
||||||
|
require.True(t, c.key == attr.key)
|
||||||
|
require.True(t, &(c.key) != &(attr.key))
|
||||||
|
require.True(t, &(c.value) != &(attr.value))
|
||||||
|
require.True(t, c.value == attr.value)
|
||||||
|
require.True(t, &(c.parents[0]) != &(attr.parents[0]))
|
||||||
|
require.True(t, slices.Compare(c.parents, attr.parents) == 0)
|
||||||
|
}
|
|
@ -96,6 +96,17 @@ func (m NetMap) Epoch() uint64 {
|
||||||
return m.epoch
|
return m.epoch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clone returns a copy of NetMap.
|
||||||
|
func (x *NetMap) Clone() *NetMap {
|
||||||
|
dst := NetMap{
|
||||||
|
epoch: x.epoch,
|
||||||
|
}
|
||||||
|
for _, node := range x.nodes {
|
||||||
|
dst.nodes = append(dst.nodes, *node.Clone())
|
||||||
|
}
|
||||||
|
return &dst
|
||||||
|
}
|
||||||
|
|
||||||
// nodes is a slice of NodeInfo instances needed for HRW sorting.
|
// nodes is a slice of NodeInfo instances needed for HRW sorting.
|
||||||
type nodes []NodeInfo
|
type nodes []NodeInfo
|
||||||
|
|
||||||
|
|
|
@ -45,3 +45,15 @@ func TestNetMap_SetEpoch(t *testing.T) {
|
||||||
|
|
||||||
require.EqualValues(t, e, m.Epoch())
|
require.EqualValues(t, e, m.Epoch())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNetMap_Clone(t *testing.T) {
|
||||||
|
nm := new(netmap.NetMap)
|
||||||
|
nm.SetEpoch(1)
|
||||||
|
var ni netmap.NodeInfo
|
||||||
|
nm.SetNodes([]netmap.NodeInfo{ni})
|
||||||
|
|
||||||
|
c := nm.Clone()
|
||||||
|
|
||||||
|
require.True(t, c != nm)
|
||||||
|
require.True(t, &(c.Nodes()[0]) != &(nm.Nodes()[0]))
|
||||||
|
}
|
||||||
|
|
|
@ -563,6 +563,14 @@ func (x *NodeInfo) SetStatus(state NodeState) {
|
||||||
x.m.SetState(netmap.NodeState(state))
|
x.m.SetState(netmap.NodeState(state))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clone returns a copy of NodeInfo.
|
||||||
|
func (ni *NodeInfo) Clone() *NodeInfo {
|
||||||
|
return &NodeInfo{
|
||||||
|
hash: ni.hash,
|
||||||
|
m: *ni.m.Clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// String implements fmt.Stringer.
|
// String implements fmt.Stringer.
|
||||||
//
|
//
|
||||||
// String is designed to be human-readable, and its format MAY differ between
|
// String is designed to be human-readable, and its format MAY differ between
|
||||||
|
|
|
@ -108,3 +108,12 @@ func TestNodeInfo_ExternalAddr(t *testing.T) {
|
||||||
n.SetExternalAddresses(addr[1:]...)
|
n.SetExternalAddresses(addr[1:]...)
|
||||||
require.Equal(t, addr[1:], n.ExternalAddresses())
|
require.Equal(t, addr[1:], n.ExternalAddresses())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNodeInfo_Clone(t *testing.T) {
|
||||||
|
var ni NodeInfo
|
||||||
|
ni.SetPublicKey([]byte{2, 3})
|
||||||
|
|
||||||
|
c := ni.Clone()
|
||||||
|
require.True(t, c != &ni)
|
||||||
|
require.True(t, &(c.PublicKey()[0]) != &(ni.PublicKey()[0]))
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue