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-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
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.
|
2021-10-27 13:14:51 +00:00
|
|
|
func (l *ListPrm) WithCount(count uint32) *ListPrm {
|
|
|
|
l.count = int(count)
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2021-11-11 14:27:11 +00:00
|
|
|
func (l *ListPrm) WithCursor(cursor *Cursor) *ListPrm {
|
2021-10-27 13:14:51 +00:00
|
|
|
l.cursor = cursor
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListRes contains values returned from ListWithCursor operation.
|
|
|
|
type ListRes struct {
|
2022-01-26 12:11:13 +00:00
|
|
|
addrList []*addressSDK.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-01-26 12:11:13 +00:00
|
|
|
func (l ListRes) AddressList() []*addressSDK.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-01-26 12:11:13 +00:00
|
|
|
func ListWithCursor(db *DB, count uint32, cursor *Cursor) ([]*addressSDK.Address, *Cursor, error) {
|
2021-10-27 13:14:51 +00:00
|
|
|
r, err := db.ListWithCursor(new(ListPrm).WithCount(count).WithCursor(cursor))
|
|
|
|
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.
|
2021-10-27 13:14:51 +00:00
|
|
|
func (db *DB) ListWithCursor(prm *ListPrm) (res *ListRes, err error) {
|
|
|
|
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-01-26 12:11:13 +00:00
|
|
|
func (db *DB) listWithCursor(tx *bbolt.Tx, count int, cursor *Cursor) ([]*addressSDK.Address, *Cursor, error) {
|
2021-11-11 14:27:11 +00:00
|
|
|
threshold := cursor == nil // threshold is a flag to ignore cursor
|
2022-01-26 12:11:13 +00:00
|
|
|
result := make([]*addressSDK.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
|
|
|
}
|
|
|
|
|
2021-11-17 11:01:41 +00:00
|
|
|
prefix := containerID.String() + "/"
|
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
|
|
|
|
prefix string, // string of CID, optimization
|
2022-01-26 12:11:13 +00:00
|
|
|
to []*addressSDK.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-01-26 12:11:13 +00:00
|
|
|
) ([]*addressSDK.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-01-26 12:11:13 +00:00
|
|
|
a := addressSDK.NewAddress()
|
2021-11-11 14:27:11 +00:00
|
|
|
if err := a.Parse(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 (
|
|
|
|
containerID = cid.New()
|
|
|
|
containerIDStr = string(name)
|
|
|
|
postfix string
|
|
|
|
)
|
|
|
|
|
|
|
|
ind := strings.Index(string(name), invalidBase58String)
|
|
|
|
if ind > 0 {
|
|
|
|
postfix = containerIDStr[ind:]
|
|
|
|
containerIDStr = containerIDStr[:ind]
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := containerID.Parse(containerIDStr); err != nil {
|
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return containerID, postfix
|
|
|
|
}
|