forked from TrueCloudLab/frostfs-sdk-go
[TrueCloudLab/hrw#2] sdk-go: Optimize node hash
Compute node hash by node initialization Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
parent
2cbc585edd
commit
e9c1a2ab2b
1 changed files with 7 additions and 1 deletions
|
@ -25,7 +25,8 @@ import (
|
||||||
//
|
//
|
||||||
// Instances can be created using built-in var declaration.
|
// Instances can be created using built-in var declaration.
|
||||||
type NodeInfo struct {
|
type NodeInfo struct {
|
||||||
m netmap.NodeInfo
|
m netmap.NodeInfo
|
||||||
|
hash uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
// reads NodeInfo from netmap.NodeInfo message. If checkFieldPresence is set,
|
// reads NodeInfo from netmap.NodeInfo message. If checkFieldPresence is set,
|
||||||
|
@ -86,6 +87,7 @@ func (x *NodeInfo) readFromV2(m netmap.NodeInfo, checkFieldPresence bool) error
|
||||||
}
|
}
|
||||||
|
|
||||||
x.m = m
|
x.m = m
|
||||||
|
x.hash = hrw.Hash(binPublicKey)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -167,6 +169,7 @@ func (x *NodeInfo) UnmarshalJSON(data []byte) error {
|
||||||
// See also PublicKey.
|
// See also PublicKey.
|
||||||
func (x *NodeInfo) SetPublicKey(key []byte) {
|
func (x *NodeInfo) SetPublicKey(key []byte) {
|
||||||
x.m.SetPublicKey(key)
|
x.m.SetPublicKey(key)
|
||||||
|
x.hash = hrw.Hash(x.m.GetPublicKey())
|
||||||
}
|
}
|
||||||
|
|
||||||
// PublicKey returns value set using SetPublicKey.
|
// PublicKey returns value set using SetPublicKey.
|
||||||
|
@ -233,6 +236,9 @@ var _ hrw.Hasher = NodeInfo{}
|
||||||
// Hash is needed to support weighted HRW therefore sort function sorts nodes
|
// Hash is needed to support weighted HRW therefore sort function sorts nodes
|
||||||
// based on their public key. Hash isn't expected to be used directly.
|
// based on their public key. Hash isn't expected to be used directly.
|
||||||
func (x NodeInfo) Hash() uint64 {
|
func (x NodeInfo) Hash() uint64 {
|
||||||
|
if x.hash != 0 {
|
||||||
|
return x.hash
|
||||||
|
}
|
||||||
return hrw.Hash(x.m.GetPublicKey())
|
return hrw.Hash(x.m.GetPublicKey())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue