Skip to content

Commit

Permalink
[nspcc-dev#356] netmap: Improve docs and slightly optimize the code
Browse files Browse the repository at this point in the history
API changes:
  * Rename `Enters` method of the `NodeSubnetInfo` to `Enabled`.
  * Declare undefined behavior of `WriteSubnetInfo` with non-unique
  attributes.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
  • Loading branch information
Leonard Lyubich committed Nov 23, 2021
1 parent 8a788c1 commit 52a416b
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions netmap/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type NodeSubnetInfo struct {
id *refs.SubnetID
}

// Enters returns true iff node enters the subnet.
func (x NodeSubnetInfo) Enters() bool {
// Enabled returns true iff subnet membership is enabled for the node.
func (x NodeSubnetInfo) Enabled() bool {
return !x.exit
}

Expand All @@ -56,9 +56,11 @@ func subnetAttributeKey(id *refs.SubnetID) string {

// WriteSubnetInfo writes NodeSubnetInfo to NodeInfo via attributes. NodeInfo must not be nil.
//
// Existing subnet attributes are expected to be key-unique, otherwise undefined behavior.
//
// Does not add (removes existing) attribute if node:
// * exists non-zero subnet;
// * enters zero subnet.
// * disables non-zero subnet;
// * enables zero subnet.
//
// Attribute key is calculated from ID using format `__NEOFS__SUBNET_%s`.
// Attribute Value is:
Expand All @@ -68,7 +70,7 @@ func WriteSubnetInfo(node *NodeInfo, info NodeSubnetInfo) {
attrs := node.GetAttributes()

id := info.ID()
enters := info.Enters()
enters := info.Enabled()

// calculate attribute key
key := subnetAttributeKey(id)
Expand All @@ -77,6 +79,7 @@ func WriteSubnetInfo(node *NodeInfo, info NodeSubnetInfo) {
for i := range attrs {
if attrs[i].GetKey() == key {
attrs = append(attrs[:i], attrs[i+1:]...)
break // attributes are expected to be key-unique
}
}
} else {
Expand Down Expand Up @@ -141,7 +144,7 @@ func IterateSubnets(node *NodeInfo, f func(refs.SubnetID) error) error {

// cut subnet ID string
idTxt := strings.TrimPrefix(key, attrSubnetPrefix)
if idTxt == key {
if len(idTxt) == len(key) {
// not a subnet attribute
continue
}
Expand Down

0 comments on commit 52a416b

Please sign in to comment.