2020-11-25 09:53:17 +00:00
|
|
|
package blobstor
|
|
|
|
|
|
|
|
import (
|
2021-09-13 11:51:17 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-11-25 09:53:17 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza"
|
2022-01-20 15:53:29 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/fstree"
|
2021-11-10 07:08:33 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2020-11-25 09:53:17 +00:00
|
|
|
)
|
|
|
|
|
2021-09-13 11:51:17 +00:00
|
|
|
// IterationElement represents a unit of elements through which Iterate operation passes.
|
|
|
|
type IterationElement struct {
|
|
|
|
data []byte
|
|
|
|
|
|
|
|
blzID *blobovnicza.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectData returns stored object in a binary representation.
|
|
|
|
func (x IterationElement) ObjectData() []byte {
|
|
|
|
return x.data
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlobovniczaID returns identifier of Blobovnicza in which object is stored.
|
|
|
|
// Returns nil if object isn't in Blobovnicza.
|
|
|
|
func (x IterationElement) BlobovniczaID() *blobovnicza.ID {
|
|
|
|
return x.blzID
|
|
|
|
}
|
|
|
|
|
|
|
|
// IterationHandler is a generic processor of IterationElement.
|
|
|
|
type IterationHandler func(IterationElement) error
|
2020-11-25 09:53:17 +00:00
|
|
|
|
|
|
|
// IteratePrm groups the parameters of Iterate operation.
|
|
|
|
type IteratePrm struct {
|
2022-01-20 16:32:49 +00:00
|
|
|
handler IterationHandler
|
|
|
|
ignoreErrors bool
|
2020-11-25 09:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IterateRes groups resulting values of Iterate operation.
|
|
|
|
type IterateRes struct{}
|
|
|
|
|
|
|
|
// SetIterationHandler sets the action to be performed on each iteration.
|
|
|
|
func (i *IteratePrm) SetIterationHandler(h IterationHandler) {
|
|
|
|
i.handler = h
|
|
|
|
}
|
|
|
|
|
2022-01-20 16:32:49 +00:00
|
|
|
// IgnoreErrors sets the flag signifying whether errors should be ignored.
|
|
|
|
func (i *IteratePrm) IgnoreErrors() {
|
|
|
|
i.ignoreErrors = true
|
|
|
|
}
|
|
|
|
|
2020-11-25 09:53:17 +00:00
|
|
|
// Iterate traverses the storage over the stored objects and calls the handler
|
|
|
|
// on each element.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely iterate over the storage.
|
|
|
|
//
|
2021-09-13 11:51:17 +00:00
|
|
|
// If handler returns an error, method wraps and returns it immediately.
|
|
|
|
func (b *BlobStor) Iterate(prm IteratePrm) (*IterateRes, error) {
|
|
|
|
var elem IterationElement
|
|
|
|
|
2022-01-20 16:32:49 +00:00
|
|
|
err := b.blobovniczas.iterateBlobovniczas(prm.ignoreErrors, func(p string, blz *blobovnicza.Blobovnicza) error {
|
2021-09-13 11:51:17 +00:00
|
|
|
err := blobovnicza.IterateObjects(blz, func(data []byte) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// decompress the data
|
|
|
|
elem.data, err = b.decompressor(data)
|
|
|
|
if err != nil {
|
2022-01-20 16:32:49 +00:00
|
|
|
if prm.ignoreErrors {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-13 11:51:17 +00:00
|
|
|
return fmt.Errorf("could not decompress object data: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
elem.blzID = blobovnicza.NewIDFromBytes([]byte(p))
|
|
|
|
|
|
|
|
return prm.handler(elem)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("blobovnicza iterator failure %s: %w", p, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("blobovniczas iterator failure: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
elem.blzID = nil
|
|
|
|
|
2022-01-20 15:53:29 +00:00
|
|
|
err = b.fsTree.Iterate(new(fstree.IterationPrm).WithHandler(func(_ *objectSDK.Address, data []byte) error {
|
2021-09-13 11:51:17 +00:00
|
|
|
// decompress the data
|
|
|
|
elem.data, err = b.decompressor(data)
|
|
|
|
if err != nil {
|
2022-01-20 16:32:49 +00:00
|
|
|
if prm.ignoreErrors {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-13 11:51:17 +00:00
|
|
|
return fmt.Errorf("could not decompress object data: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return prm.handler(elem)
|
2022-01-20 16:32:49 +00:00
|
|
|
}).WithIgnoreErrors(prm.ignoreErrors))
|
|
|
|
|
2021-09-13 11:51:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("fs tree iterator failure: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return new(IterateRes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IterateBinaryObjects is a helper function which iterates over BlobStor and passes binary objects to f.
|
|
|
|
func IterateBinaryObjects(blz *BlobStor, f func(data []byte, blzID *blobovnicza.ID) error) error {
|
|
|
|
var prm IteratePrm
|
|
|
|
|
|
|
|
prm.SetIterationHandler(func(elem IterationElement) error {
|
|
|
|
return f(elem.ObjectData(), elem.BlobovniczaID())
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err := blz.Iterate(prm)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// IterateObjects is a helper function which iterates over BlobStor and passes decoded objects to f.
|
|
|
|
func IterateObjects(blz *BlobStor, f func(obj *object.Object, blzID *blobovnicza.ID) error) error {
|
|
|
|
var obj *object.Object
|
|
|
|
|
|
|
|
return IterateBinaryObjects(blz, func(data []byte, blzID *blobovnicza.ID) error {
|
|
|
|
if obj == nil {
|
|
|
|
obj = object.New()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := obj.Unmarshal(data); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal the object: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f(obj, blzID)
|
|
|
|
})
|
2020-11-25 09:53:17 +00:00
|
|
|
}
|