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

     1  package status
     2  
     3  const sectionBitSize = 10
     4  
     5  // InSections checks if the Code is in [i,j] section list.
     6  func (x Code) InSections(i, j uint32) bool {
     7  	return uint32(x) >= i<<sectionBitSize && uint32(x) < (j+1)<<sectionBitSize
     8  }
     9  
    10  // LocalizeSection localizes the Code to the sec-th section.
    11  //
    12  // Does not make sense if the Code is outside the section.
    13  func (x *Code) LocalizeSection(sec uint32) {
    14  	*x = *x - Code(sec<<sectionBitSize)
    15  }
    16  
    17  // GlobalizeSection globalizes the Code of the sec-th section.
    18  //
    19  // Does not make sense if the Code is outside the section.
    20  func (x *Code) GlobalizeSection(sec uint32) {
    21  	*x = *x + Code(sec<<sectionBitSize)
    22  }
    23  
    24  // IsInSection returns true if the Code belongs to sec-th section.
    25  func IsInSection(code Code, sec uint32) bool {
    26  	return code.InSections(sec, sec)
    27  }
    28  
    29  const successSections = 1
    30  
    31  // IsSuccess checks if the Code is a success code.
    32  func IsSuccess(c Code) bool {
    33  	return c.InSections(0, successSections-1)
    34  }
    35  
    36  // LocalizeSuccess localizes the Code to the success section.
    37  func LocalizeSuccess(c *Code) {
    38  	c.LocalizeSection(0)
    39  }
    40  
    41  // GlobalizeSuccess globalizes the Code to the success section.
    42  func GlobalizeSuccess(c *Code) {
    43  	c.GlobalizeSection(0)
    44  }
    45  
    46  func sectionAfterSuccess(sec uint32) uint32 {
    47  	return successSections + sec
    48  }
    49  
    50  // Success codes.
    51  const (
    52  	// OK is a local status Code value for default success.
    53  	OK Code = iota
    54  )
    55  
    56  // Common failure codes.
    57  const (
    58  	// Internal is a local Code value for INTERNAL failure status.
    59  	Internal Code = iota
    60  	// WrongMagicNumber is a local Code value for WRONG_MAGIC_NUMBER failure status.
    61  	WrongMagicNumber
    62  	// SignatureVerificationFail is a local Code value for SIGNATURE_VERIFICATION_FAIL status.
    63  	SignatureVerificationFail
    64  	// NodeUnderMaintenance is a local Code value for NODE_UNDER_MAINTENANCE status.
    65  	NodeUnderMaintenance
    66  )
    67  
    68  const (
    69  	_ = iota - 1
    70  	sectionCommon
    71  )
    72  
    73  // IsCommonFail checks if the Code is a common failure code.
    74  func IsCommonFail(c Code) bool {
    75  	return IsInSection(c, sectionAfterSuccess(sectionCommon))
    76  }
    77  
    78  // LocalizeCommonFail localizes the Code to the common fail section.
    79  func LocalizeCommonFail(c *Code) {
    80  	c.LocalizeSection(sectionAfterSuccess(sectionCommon))
    81  }
    82  
    83  // GlobalizeCommonFail globalizes the Code to the common fail section.
    84  func GlobalizeCommonFail(c *Code) {
    85  	c.GlobalizeSection(sectionAfterSuccess(sectionCommon))
    86  }
    87  
    88  // LocalizeIfInSection checks if passed global status.Code belongs to the section and:
    89  //
    90  //	then localizes the code and returns true,
    91  //	else leaves the code unchanged and returns false.
    92  //
    93  // Arg must not be nil.
    94  func LocalizeIfInSection(c *Code, sec uint32) bool {
    95  	if IsInSection(*c, sec) {
    96  		c.LocalizeSection(sec)
    97  		return true
    98  	}
    99  
   100  	return false
   101  }