git.frostfs.info/TrueCloudLab/frostfs-sdk-go@v0.0.0-20241022124111-5361f0ecebd3/object/erasure_code.go (about)

     1  package object
     2  
     3  import (
     4  	"errors"
     5  
     6  	"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
     7  	refs "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
     8  	oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
     9  )
    10  
    11  // ECHeader represents erasure coding header.
    12  type ECHeader struct {
    13  	parent              oid.ID
    14  	parentSplitID       *SplitID
    15  	parentSplitParentID *oid.ID
    16  	parentAttributes    []Attribute
    17  	index               uint32
    18  	total               uint32
    19  	header              []byte
    20  	headerLength        uint32
    21  }
    22  
    23  type ECParentInfo struct {
    24  	// EC-parent's ID.
    25  	ID oid.ID
    26  
    27  	// EC-parent's split ID if the parent is a part of Split itself.
    28  	SplitID *SplitID
    29  
    30  	// EC-parent's parent split ID if the parent is a part of Split itself.
    31  	SplitParentID *oid.ID
    32  
    33  	// EC-parent's attributes.
    34  	Attributes []Attribute
    35  }
    36  
    37  // NewECHeader constructs new erasure coding header.
    38  func NewECHeader(ecParentInfo ECParentInfo, index, total uint32, header []byte, headerLength uint32) *ECHeader {
    39  	return &ECHeader{
    40  		parent:              ecParentInfo.ID,
    41  		parentSplitID:       ecParentInfo.SplitID,
    42  		parentSplitParentID: ecParentInfo.SplitParentID,
    43  		parentAttributes:    ecParentInfo.Attributes,
    44  		index:               index,
    45  		total:               total,
    46  		header:              header,
    47  		headerLength:        headerLength,
    48  	}
    49  }
    50  
    51  // WriteToV2 converts SDK structure to v2-api one.
    52  func (e *ECHeader) WriteToV2(h *object.ECHeader) {
    53  	var parent refs.ObjectID
    54  	e.parent.WriteToV2(&parent)
    55  	h.ParentSplitID = e.parentSplitID.ToV2()
    56  
    57  	if e.parentSplitParentID != nil {
    58  		parentSplitParentID := new(refs.ObjectID)
    59  		e.parentSplitParentID.WriteToV2(parentSplitParentID)
    60  		h.ParentSplitParentID = parentSplitParentID
    61  	}
    62  
    63  	h.Parent = &parent
    64  
    65  	attrs := make([]object.Attribute, len(e.parentAttributes))
    66  	for i := range e.parentAttributes {
    67  		attrs[i] = *e.parentAttributes[i].ToV2()
    68  	}
    69  	h.ParentAttributes = attrs
    70  
    71  	h.Index = e.index
    72  	h.Total = e.total
    73  	h.Header = e.header
    74  	h.HeaderLength = e.headerLength
    75  }
    76  
    77  // ReadFromV2 converts v2-api structure to SDK one.
    78  func (e *ECHeader) ReadFromV2(h *object.ECHeader) error {
    79  	if h == nil {
    80  		return nil
    81  	}
    82  	if h.Parent == nil {
    83  		return errors.New("empty parent")
    84  	}
    85  
    86  	attrs := make([]Attribute, len(h.ParentAttributes))
    87  	for i := range h.ParentAttributes {
    88  		attrs[i] = *NewAttributeFromV2(&h.ParentAttributes[i])
    89  	}
    90  	e.parentAttributes = attrs
    91  
    92  	_ = e.parent.ReadFromV2(*h.Parent)
    93  	e.parentSplitID = NewSplitIDFromV2(h.ParentSplitID)
    94  	if h.ParentSplitParentID != nil {
    95  		if e.parentSplitParentID == nil {
    96  			e.parentSplitParentID = new(oid.ID)
    97  		}
    98  		_ = e.parentSplitParentID.ReadFromV2(*h.ParentSplitParentID)
    99  	}
   100  	e.index = h.Index
   101  	e.total = h.Total
   102  	e.header = h.Header
   103  	e.headerLength = h.HeaderLength
   104  	return nil
   105  }
   106  
   107  func (o *Object) ECHeader() *ECHeader {
   108  	ec := (*object.Object)(o).GetHeader().GetEC()
   109  	if ec == nil {
   110  		return nil
   111  	}
   112  
   113  	h := new(ECHeader)
   114  	_ = h.ReadFromV2(ec)
   115  	return h
   116  }
   117  
   118  func (o *Object) SetECHeader(ec *ECHeader) {
   119  	o.setHeaderField(func(h *object.Header) {
   120  		if ec == nil {
   121  			h.SetEC(nil)
   122  			return
   123  		}
   124  
   125  		v2 := new(object.ECHeader)
   126  		ec.WriteToV2(v2)
   127  		h.SetEC(v2)
   128  	})
   129  }
   130  
   131  func (e *ECHeader) Parent() oid.ID {
   132  	return e.parent
   133  }
   134  
   135  func (e *ECHeader) SetParent(id oid.ID) {
   136  	e.parent = id
   137  }
   138  
   139  func (e *ECHeader) ParentSplitID() *SplitID {
   140  	return e.parentSplitID
   141  }
   142  
   143  func (e *ECHeader) SetParentSplitID(parentSplitID *SplitID) {
   144  	e.parentSplitID = parentSplitID
   145  }
   146  
   147  func (e *ECHeader) ParentSplitParentID() *oid.ID {
   148  	return e.parentSplitParentID
   149  }
   150  
   151  func (e *ECHeader) SetParentSplitParentID(parentSplitParentID *oid.ID) {
   152  	e.parentSplitParentID = parentSplitParentID
   153  }
   154  
   155  func (e *ECHeader) ParentAttributes() []Attribute {
   156  	return e.parentAttributes
   157  }
   158  
   159  func (e *ECHeader) SetParentAttributes(attrs []Attribute) {
   160  	e.parentAttributes = attrs
   161  }
   162  
   163  func (e *ECHeader) Index() uint32 {
   164  	return e.index
   165  }
   166  
   167  func (e *ECHeader) SetIndex(i uint32) {
   168  	e.index = i
   169  }
   170  
   171  func (e *ECHeader) Total() uint32 {
   172  	return e.total
   173  }
   174  
   175  func (e *ECHeader) SetTotal(i uint32) {
   176  	e.total = i
   177  }
   178  
   179  func (e *ECHeader) Header() []byte {
   180  	return e.header
   181  }
   182  
   183  func (e *ECHeader) SetHeader(header []byte) {
   184  	e.header = header
   185  }
   186  
   187  func (e *ECHeader) HeaderLength() uint32 {
   188  	return e.headerLength
   189  }
   190  
   191  func (e *ECHeader) SetHeaderLength(l uint32) {
   192  	e.headerLength = l
   193  }