2020-12-03 12:01:21 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2020-12-03 12:01:21 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-11-11 14:27:11 +00:00
|
|
|
// Cursor is a type for continuous object listing.
|
|
|
|
type Cursor = meta.Cursor
|
|
|
|
|
2021-01-22 13:21:45 +00:00
|
|
|
type ListContainersPrm struct{}
|
|
|
|
|
|
|
|
type ListContainersRes struct {
|
2021-05-31 11:03:17 +00:00
|
|
|
containers []*cid.ID
|
2021-01-22 13:21:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (r *ListContainersRes) Containers() []*cid.ID {
|
2021-01-22 13:21:45 +00:00
|
|
|
return r.containers
|
|
|
|
}
|
|
|
|
|
2021-10-27 13:33:26 +00:00
|
|
|
// ListWithCursorPrm contains parameters for ListWithCursor operation.
|
|
|
|
type ListWithCursorPrm struct {
|
|
|
|
count uint32
|
2021-11-11 14:27:11 +00:00
|
|
|
cursor *Cursor
|
2021-10-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListWithCursorRes contains values returned from ListWithCursor operation.
|
|
|
|
type ListWithCursorRes struct {
|
|
|
|
addrList []*object.Address
|
2021-11-11 14:27:11 +00:00
|
|
|
cursor *Cursor
|
2021-10-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:52:19 +00:00
|
|
|
// WithCount sets maximum amount of addresses that ListWithCursor should return.
|
2021-10-27 13:33:26 +00:00
|
|
|
func (p *ListWithCursorPrm) WithCount(count uint32) *ListWithCursorPrm {
|
|
|
|
p.count = count
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithCursor sets cursor for ListWithCursor operation. For initial request,
|
2021-11-12 09:52:19 +00:00
|
|
|
// ignore this param or use nil value. For consecutive requests, use value
|
2021-10-27 13:33:26 +00:00
|
|
|
// from ListWithCursorRes.
|
2021-11-11 14:27:11 +00:00
|
|
|
func (p *ListWithCursorPrm) WithCursor(cursor *Cursor) *ListWithCursorPrm {
|
2021-10-27 13:33:26 +00:00
|
|
|
p.cursor = cursor
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddressList returns addresses selected by ListWithCursor operation.
|
|
|
|
func (r ListWithCursorRes) AddressList() []*object.Address {
|
|
|
|
return r.addrList
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cursor returns cursor for consecutive listing requests.
|
2021-11-11 14:27:11 +00:00
|
|
|
func (r ListWithCursorRes) Cursor() *Cursor {
|
2021-10-27 13:33:26 +00:00
|
|
|
return r.cursor
|
|
|
|
}
|
|
|
|
|
2021-09-24 15:04:00 +00:00
|
|
|
// List returns all objects physically stored in the Shard.
|
2020-12-03 12:01:21 +00:00
|
|
|
func (s *Shard) List() (*SelectRes, error) {
|
|
|
|
lst, err := s.metaBase.Containers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't list stored containers: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res := new(SelectRes)
|
|
|
|
filters := object.NewSearchFilters()
|
2021-09-24 15:04:00 +00:00
|
|
|
filters.AddPhyFilter()
|
2020-12-03 12:01:21 +00:00
|
|
|
|
|
|
|
for i := range lst {
|
2020-12-10 13:20:38 +00:00
|
|
|
ids, err := meta.Select(s.metaBase, lst[i], filters) // consider making List in metabase
|
2020-12-03 12:01:21 +00:00
|
|
|
if err != nil {
|
|
|
|
s.log.Debug("can't select all objects",
|
|
|
|
zap.Stringer("cid", lst[i]),
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
res.addrList = append(res.addrList, ids...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2021-01-22 13:21:45 +00:00
|
|
|
|
|
|
|
func (s *Shard) ListContainers(_ *ListContainersPrm) (*ListContainersRes, error) {
|
|
|
|
containers, err := s.metaBase.Containers()
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get list of containers: %w", err)
|
2021-01-22 13:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &ListContainersRes{
|
|
|
|
containers: containers,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func ListContainers(s *Shard) ([]*cid.ID, error) {
|
2021-01-22 13:21:45 +00:00
|
|
|
res, err := s.ListContainers(&ListContainersPrm{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Containers(), nil
|
|
|
|
}
|
2021-10-27 13:33:26 +00:00
|
|
|
|
|
|
|
// ListWithCursor lists physical objects available in shard starting from
|
|
|
|
// cursor. Includes regular, tombstone and storage group objects. Does not
|
|
|
|
// include inhumed objects. Use cursor value from response for consecutive requests.
|
2021-11-12 09:52:19 +00:00
|
|
|
//
|
|
|
|
// Returns ErrEndOfListing if there are no more objects to return or count
|
|
|
|
// parameter set to zero.
|
2021-10-27 13:33:26 +00:00
|
|
|
func (s *Shard) ListWithCursor(prm *ListWithCursorPrm) (*ListWithCursorRes, error) {
|
|
|
|
metaPrm := new(meta.ListPrm).WithCount(prm.count).WithCursor(prm.cursor)
|
|
|
|
res, err := s.metaBase.ListWithCursor(metaPrm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get list of objects: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ListWithCursorRes{
|
|
|
|
addrList: res.AddressList(),
|
|
|
|
cursor: res.Cursor(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListWithCursor lists physical objects available in shard starting from
|
|
|
|
// cursor. Includes regular, tombstone and storage group objects. Does not
|
|
|
|
// include inhumed objects. Use cursor value from response for consecutive requests.
|
2021-11-12 09:52:19 +00:00
|
|
|
//
|
|
|
|
// Returns ErrEndOfListing if there are no more objects to return or count
|
|
|
|
// parameter set to zero.
|
2021-11-11 14:27:11 +00:00
|
|
|
func ListWithCursor(s *Shard, count uint32, cursor *Cursor) ([]*object.Address, *Cursor, error) {
|
2021-10-27 13:33:26 +00:00
|
|
|
prm := new(ListWithCursorPrm).WithCount(count).WithCursor(cursor)
|
|
|
|
res, err := s.ListWithCursor(prm)
|
|
|
|
if err != nil {
|
2021-11-11 14:27:11 +00:00
|
|
|
return nil, nil, err
|
2021-10-27 13:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.AddressList(), res.Cursor(), nil
|
|
|
|
}
|