[#223] core: Refactor object format validator

Resolve funlen linter for FormatValidator.ValidateContent method.

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-04-06 13:02:37 +03:00
parent 02831d427b
commit 6016d78a45

View file

@ -202,54 +202,78 @@ func (i ContentMeta) Objects() []oid.ID {
} }
// ValidateContent validates payload content according to the object type. // ValidateContent validates payload content according to the object type.
//
// nolint: funlen
func (v *FormatValidator) ValidateContent(o *object.Object) (ContentMeta, error) { func (v *FormatValidator) ValidateContent(o *object.Object) (ContentMeta, error) {
meta := ContentMeta{ meta := ContentMeta{
typ: o.Type(), typ: o.Type(),
} }
switch o.Type() { switch o.Type() {
case object.TypeRegular:
// ignore regular objects, they do not need payload formatting
case object.TypeTombstone: case object.TypeTombstone:
if len(o.Payload()) == 0 { if err := v.fillAndValidateTombstoneMeta(o, &meta); err != nil {
return ContentMeta{}, fmt.Errorf("(%T) empty payload in tombstone", v)
}
tombstone := object.NewTombstone()
if err := tombstone.Unmarshal(o.Payload()); err != nil {
return ContentMeta{}, fmt.Errorf("(%T) could not unmarshal tombstone content: %w", v, err)
}
// check if the tombstone has the same expiration in the body and the header
exp, err := expirationEpochAttribute(o)
if err != nil {
return ContentMeta{}, err return ContentMeta{}, err
} }
if exp != tombstone.ExpirationEpoch() {
return ContentMeta{}, errTombstoneExpiration
}
// mark all objects from the tombstone body as removed in the storage engine
_, ok := o.ContainerID()
if !ok {
return ContentMeta{}, errors.New("missing container ID")
}
idList := tombstone.Members()
meta.objs = idList
case object.TypeStorageGroup: case object.TypeStorageGroup:
if err := v.fillAndValidateStorageGroupMeta(o, &meta); err != nil {
return ContentMeta{}, err
}
case object.TypeLock:
if err := v.fillAndValidateLockMeta(o, &meta); err != nil {
return ContentMeta{}, err
}
default:
// ignore all other object types, they do not need payload formatting
}
return meta, nil
}
func (v *FormatValidator) fillAndValidateLockMeta(o *object.Object, meta *ContentMeta) error {
if len(o.Payload()) == 0 { if len(o.Payload()) == 0 {
return ContentMeta{}, fmt.Errorf("(%T) empty payload in SG", v) return errors.New("empty payload in lock")
}
if _, ok := o.ContainerID(); !ok {
return errors.New("missing container")
}
if _, ok := o.ID(); !ok {
return errors.New("missing ID")
}
// check that LOCK object has correct expiration epoch
lockExp, err := expirationEpochAttribute(o)
if err != nil {
return fmt.Errorf("lock object expiration epoch: %w", err)
}
if currEpoch := v.netState.CurrentEpoch(); lockExp < currEpoch {
return fmt.Errorf("lock object expiration: %d; current: %d", lockExp, currEpoch)
}
var lock object.Lock
if err = lock.Unmarshal(o.Payload()); err != nil {
return fmt.Errorf("decode lock payload: %w", err)
}
num := lock.NumberOfMembers()
if num == 0 {
return errors.New("missing locked members")
}
meta.objs = make([]oid.ID, num)
lock.ReadMembers(meta.objs)
return nil
}
func (v *FormatValidator) fillAndValidateStorageGroupMeta(o *object.Object, meta *ContentMeta) error {
if len(o.Payload()) == 0 {
return fmt.Errorf("(%T) empty payload in storage group", v)
} }
var sg storagegroup.StorageGroup var sg storagegroup.StorageGroup
if err := sg.Unmarshal(o.Payload()); err != nil { if err := sg.Unmarshal(o.Payload()); err != nil {
return ContentMeta{}, fmt.Errorf("(%T) could not unmarshal SG content: %w", v, err) return fmt.Errorf("(%T) could not unmarshal storage group content: %w", v, err)
} }
mm := sg.Members() mm := sg.Members()
@ -257,62 +281,48 @@ func (v *FormatValidator) ValidateContent(o *object.Object) (ContentMeta, error)
lenMM := len(mm) lenMM := len(mm)
if lenMM == 0 { if lenMM == 0 {
return ContentMeta{}, errEmptySGMembers return errEmptySGMembers
} }
uniqueFilter := make(map[oid.ID]struct{}, lenMM) uniqueFilter := make(map[oid.ID]struct{}, lenMM)
for i := 0; i < lenMM; i++ { for i := 0; i < lenMM; i++ {
if _, alreadySeen := uniqueFilter[mm[i]]; alreadySeen { if _, alreadySeen := uniqueFilter[mm[i]]; alreadySeen {
return ContentMeta{}, fmt.Errorf("storage group contains non-unique member: %s", mm[i]) return fmt.Errorf("storage group contains non-unique member: %s", mm[i])
} }
uniqueFilter[mm[i]] = struct{}{} uniqueFilter[mm[i]] = struct{}{}
} }
case object.TypeLock: return nil
}
func (v *FormatValidator) fillAndValidateTombstoneMeta(o *object.Object, meta *ContentMeta) error {
if len(o.Payload()) == 0 { if len(o.Payload()) == 0 {
return ContentMeta{}, errors.New("empty payload in lock") return fmt.Errorf("(%T) empty payload in tombstone", v)
} }
_, ok := o.ContainerID() tombstone := object.NewTombstone()
if !ok {
return ContentMeta{}, errors.New("missing container")
}
_, ok = o.ID() if err := tombstone.Unmarshal(o.Payload()); err != nil {
if !ok { return fmt.Errorf("(%T) could not unmarshal tombstone content: %w", v, err)
return ContentMeta{}, errors.New("missing ID")
} }
// check if the tombstone has the same expiration in the body and the header
// check that LOCK object has correct expiration epoch exp, err := expirationEpochAttribute(o)
lockExp, err := expirationEpochAttribute(o)
if err != nil { if err != nil {
return ContentMeta{}, fmt.Errorf("lock object expiration epoch: %w", err) return err
} }
if currEpoch := v.netState.CurrentEpoch(); lockExp < currEpoch { if exp != tombstone.ExpirationEpoch() {
return ContentMeta{}, fmt.Errorf("lock object expiration: %d; current: %d", lockExp, currEpoch) return errTombstoneExpiration
} }
var lock object.Lock // mark all objects from the tombstone body as removed in the storage engine
if _, ok := o.ContainerID(); !ok {
err = lock.Unmarshal(o.Payload()) return errors.New("missing container ID")
if err != nil {
return ContentMeta{}, fmt.Errorf("decode lock payload: %w", err)
} }
num := lock.NumberOfMembers() meta.objs = tombstone.Members()
if num == 0 { return nil
return ContentMeta{}, errors.New("missing locked members")
}
meta.objs = make([]oid.ID, num)
lock.ReadMembers(meta.objs)
default:
// ignore all other object types, they do not need payload formatting
}
return meta, nil
} }
var errExpired = errors.New("object has expired") var errExpired = errors.New("object has expired")