github.com/TrueCloudLab/frostfs-api-go/v2@v2.0.0-20230228134343-196241c4e79a/container/attributes.go (about)

     1  package container
     2  
     3  // SysAttributePrefix is a prefix of key to system attribute.
     4  const SysAttributePrefix = "__NEOFS__"
     5  
     6  const (
     7  	// SysAttributeSubnet is a string ID of container's storage subnet.
     8  	SysAttributeSubnet = SysAttributePrefix + "SUBNET"
     9  
    10  	// SysAttributeName is a string of human-friendly container name registered as the domain in NNS contract.
    11  	SysAttributeName = SysAttributePrefix + "NAME"
    12  
    13  	// SysAttributeZone is a string of zone for container name.
    14  	SysAttributeZone = SysAttributePrefix + "ZONE"
    15  
    16  	// SysAttributeHomomorphicHashing is a container's homomorphic hashing state.
    17  	SysAttributeHomomorphicHashing = SysAttributePrefix + "DISABLE_HOMOMORPHIC_HASHING"
    18  )
    19  
    20  // SysAttributeZoneDefault is a default value for SysAttributeZone attribute.
    21  const SysAttributeZoneDefault = "container"
    22  
    23  const disabledHomomorphicHashingValue = "true"
    24  
    25  // HomomorphicHashingState returns container's homomorphic
    26  // hashing state:
    27  //   - true if hashing is enabled;
    28  //   - false if hashing is disabled.
    29  //
    30  // All container's attributes must be unique, otherwise behavior
    31  // is undefined.
    32  //
    33  // See also SetHomomorphicHashingState.
    34  func (c Container) HomomorphicHashingState() bool {
    35  	for i := range c.attr {
    36  		if c.attr[i].GetKey() == SysAttributeHomomorphicHashing {
    37  			return c.attr[i].GetValue() != disabledHomomorphicHashingValue
    38  		}
    39  	}
    40  
    41  	return true
    42  }
    43  
    44  // SetHomomorphicHashingState sets homomorphic hashing state for
    45  // container.
    46  //
    47  // All container's attributes must be unique, otherwise behavior
    48  // is undefined.
    49  //
    50  // See also HomomorphicHashingState.
    51  func (c *Container) SetHomomorphicHashingState(enable bool) {
    52  	for i := range c.attr {
    53  		if c.attr[i].GetKey() == SysAttributeHomomorphicHashing {
    54  			if enable {
    55  				// approach without allocation/waste
    56  				// coping works since the attributes
    57  				// order is not important
    58  				c.attr[i] = c.attr[len(c.attr)-1]
    59  				c.attr = c.attr[:len(c.attr)-1]
    60  			} else {
    61  				c.attr[i].SetValue(disabledHomomorphicHashingValue)
    62  			}
    63  
    64  			return
    65  		}
    66  	}
    67  
    68  	if !enable {
    69  		attr := Attribute{}
    70  		attr.SetKey(SysAttributeHomomorphicHashing)
    71  		attr.SetValue(disabledHomomorphicHashingValue)
    72  
    73  		c.attr = append(c.attr, attr)
    74  	}
    75  }