package placement import ( "errors" "strings" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap" ) const ( attrPrefix = "$attribute:" ) type Metric interface { CalculateValue(*netmap.NodeInfo, *netmap.NodeInfo) []byte } func ValidateMetric(raw string) error { if strings.HasPrefix(raw, attrPrefix) { return nil } return errors.New("unsupported priority metric") } func ParseMetric(raw string) Metric { if attr, found := strings.CutPrefix(raw, attrPrefix); found { return NewAttributeMetric(attr) } return nil } // attributeMetric describes priority metric based on attribute. type attributeMetric struct { attribute string } // CalculateValue return [0] if from and to contains attribute attributeMetric.attribute and // the value of attribute is the same. In other case return [1]. func (am *attributeMetric) CalculateValue(from *netmap.NodeInfo, to *netmap.NodeInfo) []byte { fromAttr := from.Attribute(am.attribute) toAttr := to.Attribute(am.attribute) if len(fromAttr) > 0 && len(toAttr) > 0 && fromAttr == toAttr { return []byte{0} } return []byte{1} } func NewAttributeMetric(raw string) Metric { attr, _ := strings.CutPrefix(raw, attrPrefix) return &attributeMetric{attribute: attr} }