2021-10-27 13:14:51 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2021-11-17 11:31:31 +00:00
|
|
|
"errors"
|
2021-11-17 11:01:41 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-10-27 13:14:51 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2021-11-17 11:31:31 +00:00
|
|
|
// ErrEndOfListing is returned from object listing with cursor
|
|
|
|
// when storage can't return any more objects after provided
|
|
|
|
// cursor. Use nil cursor object to start listing again.
|
|
|
|
var ErrEndOfListing = errors.New("end of object listing")
|
|
|
|
|
2021-11-11 14:27:11 +00:00
|
|
|
// Cursor is a type for continuous object listing.
|
|
|
|
type Cursor struct {
|
2021-11-17 11:01:41 +00:00
|
|
|
bucketName []byte
|
|
|
|
inBucketOffset []byte
|
2021-11-11 14:27:11 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 13:14:51 +00:00
|
|
|
// ListPrm contains parameters for ListWithCursor operation.
|
|
|
|
type ListPrm struct {
|
|
|
|
count int
|
2021-11-11 14:27:11 +00:00
|
|
|
cursor *Cursor
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:52:19 +00:00
|
|
|
// WithCount sets maximum amount of addresses that ListWithCursor should return.
|
2022-05-20 16:48:14 +00:00
|
|
|
func (l *ListPrm) WithCount(count uint32) {
|
2021-10-27 13:14:51 +00:00
|
|
|
l.count = int(count)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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:14:51 +00:00
|
|
|
// from ListRes.
|
2022-05-20 16:48:14 +00:00
|
|
|
func (l *ListPrm) WithCursor(cursor *Cursor) {
|
2021-10-27 13:14:51 +00:00
|
|
|
l.cursor = cursor
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListRes contains values returned from ListWithCursor operation.
|
|
|
|
type ListRes struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addrList []oid.Address
|
2021-11-11 14:27:11 +00:00
|
|
|
cursor *Cursor
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddressList returns addresses selected by ListWithCursor operation.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (l ListRes) AddressList() []oid.Address {
|
2021-10-27 13:14:51 +00:00
|
|
|
return l.addrList
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cursor returns cursor for consecutive listing requests.
|
2021-11-11 14:27:11 +00:00
|
|
|
func (l ListRes) Cursor() *Cursor {
|
2021-10-27 13:14:51 +00:00
|
|
|
return l.cursor
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:52:19 +00:00
|
|
|
// ListWithCursor lists physical objects available in metabase starting from
|
2022-02-15 12:51:56 +00:00
|
|
|
// cursor. Includes objects of all types. 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.
|
2022-05-31 17:00:41 +00:00
|
|
|
func ListWithCursor(db *DB, count uint32, cursor *Cursor) ([]oid.Address, *Cursor, error) {
|
2022-05-20 16:48:14 +00:00
|
|
|
var listPrm ListPrm
|
|
|
|
listPrm.WithCount(count)
|
|
|
|
listPrm.WithCursor(cursor)
|
|
|
|
|
|
|
|
r, err := db.ListWithCursor(listPrm)
|
2021-10-27 13:14:51 +00:00
|
|
|
if err != nil {
|
2021-11-11 14:27:11 +00:00
|
|
|
return nil, nil, err
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r.AddressList(), r.Cursor(), nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:52:19 +00:00
|
|
|
// ListWithCursor lists physical objects available in metabase starting from
|
2022-02-15 12:51:56 +00:00
|
|
|
// cursor. Includes objects of all types. 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.
|
2022-05-20 16:48:14 +00:00
|
|
|
func (db *DB) ListWithCursor(prm ListPrm) (res *ListRes, err error) {
|
2021-10-27 13:14:51 +00:00
|
|
|
err = db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
res = new(ListRes)
|
|
|
|
res.addrList, res.cursor, err = db.listWithCursor(tx, prm.count, prm.cursor)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (db *DB) listWithCursor(tx *bbolt.Tx, count int, cursor *Cursor) ([]oid.Address, *Cursor, error) {
|
2021-11-11 14:27:11 +00:00
|
|
|
threshold := cursor == nil // threshold is a flag to ignore cursor
|
2022-05-31 17:00:41 +00:00
|
|
|
result := make([]oid.Address, 0, count)
|
2021-11-17 11:01:41 +00:00
|
|
|
var bucketName []byte
|
2021-10-27 13:14:51 +00:00
|
|
|
|
2021-11-10 10:06:43 +00:00
|
|
|
c := tx.Cursor()
|
|
|
|
name, _ := c.First()
|
|
|
|
|
|
|
|
if !threshold {
|
2021-11-17 11:01:41 +00:00
|
|
|
name, _ = c.Seek(cursor.bucketName)
|
2021-11-10 10:06:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loop:
|
|
|
|
for ; name != nil; name, _ = c.Next() {
|
2021-11-17 11:01:41 +00:00
|
|
|
containerID, postfix := parseContainerIDWithPostfix(name)
|
2021-10-27 13:14:51 +00:00
|
|
|
if containerID == nil {
|
2021-11-10 10:06:43 +00:00
|
|
|
continue
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 11:01:41 +00:00
|
|
|
switch postfix {
|
2022-02-15 12:51:56 +00:00
|
|
|
case
|
|
|
|
"",
|
|
|
|
storageGroupPostfix,
|
|
|
|
bucketNameSuffixLockers,
|
|
|
|
tombstonePostfix:
|
2021-11-17 11:01:41 +00:00
|
|
|
default:
|
|
|
|
continue
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
prefix := containerID.EncodeToString() + "/"
|
2021-10-27 13:14:51 +00:00
|
|
|
|
2021-11-17 11:01:41 +00:00
|
|
|
result, cursor = selectNFromBucket(tx, name, prefix, result, count, cursor, threshold)
|
|
|
|
bucketName = name
|
|
|
|
if len(result) >= count {
|
|
|
|
break loop
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
2021-11-17 11:01:41 +00:00
|
|
|
|
|
|
|
// set threshold flag after first `selectNFromBucket` invocation
|
|
|
|
// first invocation must look for cursor object
|
|
|
|
threshold = true
|
2021-11-10 10:06:43 +00:00
|
|
|
}
|
2021-10-27 13:14:51 +00:00
|
|
|
|
|
|
|
if len(result) == 0 {
|
2021-11-17 11:31:31 +00:00
|
|
|
return nil, nil, ErrEndOfListing
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 11:01:41 +00:00
|
|
|
// new slice is much faster but less memory efficient
|
|
|
|
// we need to copy, because bucketName exists during bbolt tx
|
|
|
|
cursor.bucketName = make([]byte, len(bucketName))
|
|
|
|
copy(cursor.bucketName, bucketName)
|
|
|
|
|
2021-11-11 14:27:11 +00:00
|
|
|
return result, cursor, nil
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// selectNFromBucket similar to selectAllFromBucket but uses cursor to find
|
|
|
|
// object to start selecting from. Ignores inhumed objects.
|
|
|
|
func selectNFromBucket(tx *bbolt.Tx,
|
|
|
|
name []byte, // bucket name
|
2022-05-31 17:00:41 +00:00
|
|
|
prefix string, // string of container ID, optimization
|
|
|
|
to []oid.Address, // listing result
|
2021-10-27 13:14:51 +00:00
|
|
|
limit int, // stop listing at `limit` items in result
|
2021-11-11 14:27:11 +00:00
|
|
|
cursor *Cursor, // start from cursor object
|
2021-10-27 13:14:51 +00:00
|
|
|
threshold bool, // ignore cursor and start immediately
|
2022-05-31 17:00:41 +00:00
|
|
|
) ([]oid.Address, *Cursor) {
|
2021-10-27 13:14:51 +00:00
|
|
|
bkt := tx.Bucket(name)
|
|
|
|
if bkt == nil {
|
|
|
|
return to, cursor
|
|
|
|
}
|
|
|
|
|
2021-11-11 14:27:11 +00:00
|
|
|
if cursor == nil {
|
|
|
|
cursor = new(Cursor)
|
|
|
|
}
|
2021-10-27 13:14:51 +00:00
|
|
|
|
2021-11-11 14:27:11 +00:00
|
|
|
count := len(to)
|
2021-11-10 10:06:43 +00:00
|
|
|
c := bkt.Cursor()
|
|
|
|
k, _ := c.First()
|
2021-10-27 13:14:51 +00:00
|
|
|
|
2021-11-17 11:01:41 +00:00
|
|
|
offset := cursor.inBucketOffset
|
|
|
|
|
2021-11-10 10:06:43 +00:00
|
|
|
if !threshold {
|
2021-11-17 11:01:41 +00:00
|
|
|
c.Seek(offset)
|
2021-11-10 10:06:43 +00:00
|
|
|
k, _ = c.Next() // we are looking for objects _after_ the cursor
|
|
|
|
}
|
2021-10-27 13:14:51 +00:00
|
|
|
|
2021-11-10 10:06:43 +00:00
|
|
|
for ; k != nil; k, _ = c.Next() {
|
|
|
|
if count >= limit {
|
|
|
|
break
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
2022-05-31 17:00:41 +00:00
|
|
|
var a oid.Address
|
|
|
|
if err := a.DecodeString(prefix + string(k)); err != nil {
|
2021-11-10 10:06:43 +00:00
|
|
|
break
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
2021-11-17 11:01:41 +00:00
|
|
|
offset = k
|
2021-10-27 13:14:51 +00:00
|
|
|
if inGraveyard(tx, a) > 0 {
|
2021-11-10 10:06:43 +00:00
|
|
|
continue
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
to = append(to, a)
|
|
|
|
count++
|
2021-11-10 10:06:43 +00:00
|
|
|
}
|
2021-10-27 13:14:51 +00:00
|
|
|
|
2021-11-17 11:01:41 +00:00
|
|
|
// new slice is much faster but less memory efficient
|
|
|
|
// we need to copy, because offset exists during bbolt tx
|
|
|
|
cursor.inBucketOffset = make([]byte, len(offset))
|
|
|
|
copy(cursor.inBucketOffset, offset)
|
|
|
|
|
2021-10-27 13:14:51 +00:00
|
|
|
return to, cursor
|
|
|
|
}
|
2021-11-17 11:01:41 +00:00
|
|
|
|
|
|
|
func parseContainerIDWithPostfix(name []byte) (*cid.ID, string) {
|
|
|
|
var (
|
2022-05-12 16:37:46 +00:00
|
|
|
containerID cid.ID
|
2021-11-17 11:01:41 +00:00
|
|
|
containerIDStr = string(name)
|
|
|
|
postfix string
|
|
|
|
)
|
|
|
|
|
|
|
|
ind := strings.Index(string(name), invalidBase58String)
|
|
|
|
if ind > 0 {
|
|
|
|
postfix = containerIDStr[ind:]
|
|
|
|
containerIDStr = containerIDStr[:ind]
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
if err := containerID.DecodeString(containerIDStr); err != nil {
|
2021-11-17 11:01:41 +00:00
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
return &containerID, postfix
|
2021-11-17 11:01:41 +00:00
|
|
|
}
|