2021-10-27 13:14:51 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2022-06-14 16:34:23 +00:00
|
|
|
"bytes"
|
2021-11-17 11:31:31 +00:00
|
|
|
"errors"
|
2021-11-17 11:01:41 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetCount sets maximum amount of addresses that ListWithCursor should return.
|
|
|
|
func (l *ListPrm) SetCount(count uint32) {
|
2021-10-27 13:14:51 +00:00
|
|
|
l.count = int(count)
|
|
|
|
}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetCursor 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-07-12 14:59:37 +00:00
|
|
|
func (l *ListPrm) SetCursor(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 11:43:08 +00:00
|
|
|
func (db *DB) ListWithCursor(prm ListPrm) (res ListRes, err error) {
|
2022-07-21 13:26:25 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
2022-06-14 15:53:09 +00:00
|
|
|
result := make([]oid.Address, 0, prm.count)
|
|
|
|
|
2021-10-27 13:14:51 +00:00
|
|
|
err = db.boltDB.View(func(tx *bbolt.Tx) error {
|
2022-06-14 15:53:09 +00:00
|
|
|
res.addrList, res.cursor, err = db.listWithCursor(tx, result, prm.count, prm.cursor)
|
2021-10-27 13:14:51 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2022-06-14 15:53:09 +00:00
|
|
|
func (db *DB) listWithCursor(tx *bbolt.Tx, result []oid.Address, 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
|
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
|
|
|
}
|
|
|
|
|
2022-06-14 15:53:09 +00:00
|
|
|
var containerID cid.ID
|
2022-06-14 16:34:23 +00:00
|
|
|
var offset []byte
|
|
|
|
graveyardBkt := tx.Bucket(graveyardBucketName)
|
|
|
|
garbageBkt := tx.Bucket(garbageBucketName)
|
|
|
|
|
|
|
|
const idSize = 44 // size of the stringified object and container ids
|
|
|
|
var rawAddr = make([]byte, idSize*2+1)
|
2022-06-14 15:53:09 +00:00
|
|
|
|
2021-11-10 10:06:43 +00:00
|
|
|
loop:
|
|
|
|
for ; name != nil; name, _ = c.Next() {
|
2022-06-14 16:34:23 +00:00
|
|
|
b58CID, postfix := parseContainerIDWithPostfix(&containerID, name)
|
|
|
|
if b58CID == 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-06-14 16:34:23 +00:00
|
|
|
bkt := tx.Bucket(name)
|
|
|
|
if bkt != nil {
|
|
|
|
rawAddr = append(rawAddr[:0], b58CID...)
|
|
|
|
rawAddr = append(rawAddr, '/')
|
|
|
|
result, offset, cursor = selectNFromBucket(bkt, graveyardBkt, garbageBkt, rawAddr, containerID,
|
|
|
|
result, count, cursor, threshold)
|
|
|
|
}
|
2021-11-17 11:01:41 +00:00
|
|
|
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
|
|
|
|
2022-06-14 16:34:23 +00:00
|
|
|
if offset != nil {
|
|
|
|
// 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
|
|
|
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.
|
2022-06-14 16:34:23 +00:00
|
|
|
func selectNFromBucket(bkt *bbolt.Bucket, // main bucket
|
|
|
|
graveyardBkt, garbageBkt *bbolt.Bucket, // cached graveyard buckets
|
|
|
|
addrRaw []byte, // container ID prefix, optimization
|
2022-06-14 15:53:09 +00:00
|
|
|
cnt cid.ID, // container ID
|
2022-05-31 17:00:41 +00:00
|
|
|
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-06-14 16:34:23 +00:00
|
|
|
) ([]oid.Address, []byte, *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-06-14 15:53:09 +00:00
|
|
|
|
|
|
|
var obj oid.ID
|
|
|
|
if err := obj.DecodeString(string(k)); err != nil {
|
2021-11-10 10:06:43 +00:00
|
|
|
break
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
2022-06-14 15:53:09 +00:00
|
|
|
|
2021-11-17 11:01:41 +00:00
|
|
|
offset = k
|
2022-06-14 16:34:23 +00:00
|
|
|
if inGraveyardWithKey(append(addrRaw, k...), graveyardBkt, garbageBkt) > 0 {
|
2021-11-10 10:06:43 +00:00
|
|
|
continue
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
2022-06-14 15:53:09 +00:00
|
|
|
|
|
|
|
var a oid.Address
|
|
|
|
a.SetContainer(cnt)
|
|
|
|
a.SetObject(obj)
|
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
|
|
|
|
2022-06-14 16:34:23 +00:00
|
|
|
return to, offset, cursor
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
2021-11-17 11:01:41 +00:00
|
|
|
|
2022-06-14 16:34:23 +00:00
|
|
|
func parseContainerIDWithPostfix(containerID *cid.ID, name []byte) ([]byte, string) {
|
2021-11-17 11:01:41 +00:00
|
|
|
var (
|
2022-06-14 16:34:23 +00:00
|
|
|
containerIDStr = name
|
|
|
|
postfix []byte
|
2021-11-17 11:01:41 +00:00
|
|
|
)
|
|
|
|
|
2022-06-14 16:34:23 +00:00
|
|
|
ind := bytes.IndexByte(name, invalidBase58String[0])
|
2021-11-17 11:01:41 +00:00
|
|
|
if ind > 0 {
|
|
|
|
postfix = containerIDStr[ind:]
|
|
|
|
containerIDStr = containerIDStr[:ind]
|
|
|
|
}
|
|
|
|
|
2022-06-14 16:34:23 +00:00
|
|
|
if err := containerID.DecodeString(string(containerIDStr)); err != nil {
|
|
|
|
return nil, ""
|
2021-11-17 11:01:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 16:34:23 +00:00
|
|
|
return containerIDStr, string(postfix)
|
2021-11-17 11:01:41 +00:00
|
|
|
}
|