forked from TrueCloudLab/frostfs-node
[#948] engine: Define cursor for object listing as a type
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
164cd10af8
commit
aa9ce8a853
4 changed files with 71 additions and 122 deletions
|
@ -1,18 +1,21 @@
|
|||
package meta
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
||||
core "github.com/nspcc-dev/neofs-node/pkg/core/object"
|
||||
"go.etcd.io/bbolt"
|
||||
)
|
||||
|
||||
// Cursor is a type for continuous object listing.
|
||||
type Cursor struct {
|
||||
bucket uint8
|
||||
address *object.Address
|
||||
}
|
||||
|
||||
// ListPrm contains parameters for ListWithCursor operation.
|
||||
type ListPrm struct {
|
||||
count int
|
||||
cursor string
|
||||
cursor *Cursor
|
||||
}
|
||||
|
||||
// WithCount sets maximum amount of addresses that ListWithCursor can return.
|
||||
|
@ -22,9 +25,9 @@ func (l *ListPrm) WithCount(count uint32) *ListPrm {
|
|||
}
|
||||
|
||||
// WithCursor sets cursor for ListWithCursor operation. For initial request
|
||||
// ignore this param or use empty string. For continues requests, use value
|
||||
// ignore this param or use nil value. For continues requests, use value
|
||||
// from ListRes.
|
||||
func (l *ListPrm) WithCursor(cursor string) *ListPrm {
|
||||
func (l *ListPrm) WithCursor(cursor *Cursor) *ListPrm {
|
||||
l.cursor = cursor
|
||||
return l
|
||||
}
|
||||
|
@ -32,7 +35,7 @@ func (l *ListPrm) WithCursor(cursor string) *ListPrm {
|
|||
// ListRes contains values returned from ListWithCursor operation.
|
||||
type ListRes struct {
|
||||
addrList []*object.Address
|
||||
cursor string
|
||||
cursor *Cursor
|
||||
}
|
||||
|
||||
// AddressList returns addresses selected by ListWithCursor operation.
|
||||
|
@ -41,23 +44,23 @@ func (l ListRes) AddressList() []*object.Address {
|
|||
}
|
||||
|
||||
// Cursor returns cursor for consecutive listing requests.
|
||||
func (l ListRes) Cursor() string {
|
||||
func (l ListRes) Cursor() *Cursor {
|
||||
return l.cursor
|
||||
}
|
||||
|
||||
const (
|
||||
cursorPrefixPrimary = 'p'
|
||||
cursorPrefixTombstone = 't'
|
||||
cursorPrefixSG = 's'
|
||||
cursorBucketPrimary = iota
|
||||
cursorBucketTombstone
|
||||
cursorBucketSG
|
||||
)
|
||||
|
||||
// ListWithCursor lists physical objects available in metabase. Includes regular,
|
||||
// tombstone and storage group objects. Does not include inhumed objects. Use
|
||||
// cursor value from response for consecutive requests.
|
||||
func ListWithCursor(db *DB, count uint32, cursor string) ([]*object.Address, string, error) {
|
||||
func ListWithCursor(db *DB, count uint32, cursor *Cursor) ([]*object.Address, *Cursor, error) {
|
||||
r, err := db.ListWithCursor(new(ListPrm).WithCount(count).WithCursor(cursor))
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return r.AddressList(), r.Cursor(), nil
|
||||
|
@ -76,25 +79,8 @@ func (db *DB) ListWithCursor(prm *ListPrm) (res *ListRes, err error) {
|
|||
return res, err
|
||||
}
|
||||
|
||||
func (db *DB) listWithCursor(tx *bbolt.Tx, count int, cursor string) ([]*object.Address, string, error) {
|
||||
threshold := len(cursor) == 0 // threshold is a flag to ignore cursor
|
||||
a := object.NewAddress()
|
||||
var cursorPrefix uint8
|
||||
|
||||
if !threshold { // if cursor is present, then decode it and check sanity
|
||||
cursorPrefix = cursor[0]
|
||||
switch cursorPrefix {
|
||||
case cursorPrefixPrimary, cursorPrefixSG, cursorPrefixTombstone:
|
||||
default:
|
||||
return nil, "", fmt.Errorf("invalid cursor prefix %s", string(cursorPrefix))
|
||||
}
|
||||
|
||||
cursor = cursor[1:]
|
||||
if err := a.Parse(cursor); err != nil {
|
||||
return nil, "", fmt.Errorf("invalid cursor address: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (db *DB) listWithCursor(tx *bbolt.Tx, count int, cursor *Cursor) ([]*object.Address, *Cursor, error) {
|
||||
threshold := cursor == nil // threshold is a flag to ignore cursor
|
||||
result := make([]*object.Address, 0, count)
|
||||
unique := make(map[string]struct{}) // do not parse the same containerID twice
|
||||
|
||||
|
@ -102,7 +88,7 @@ func (db *DB) listWithCursor(tx *bbolt.Tx, count int, cursor string) ([]*object.
|
|||
name, _ := c.First()
|
||||
|
||||
if !threshold {
|
||||
name, _ = c.Seek([]byte(a.ContainerID().String()))
|
||||
name, _ = c.Seek([]byte(cursor.address.ContainerID().String()))
|
||||
}
|
||||
|
||||
loop:
|
||||
|
@ -117,20 +103,20 @@ loop:
|
|||
|
||||
lookupBuckets := [...]struct {
|
||||
name []byte
|
||||
prefix uint8
|
||||
bucket uint8
|
||||
}{
|
||||
{primaryBucketName(containerID), cursorPrefixPrimary},
|
||||
{tombstoneBucketName(containerID), cursorPrefixTombstone},
|
||||
{storageGroupBucketName(containerID), cursorPrefixSG},
|
||||
{primaryBucketName(containerID), cursorBucketPrimary},
|
||||
{tombstoneBucketName(containerID), cursorBucketTombstone},
|
||||
{storageGroupBucketName(containerID), cursorBucketSG},
|
||||
}
|
||||
|
||||
for _, lb := range lookupBuckets {
|
||||
if !threshold && cursorPrefix != lb.prefix {
|
||||
continue // start from the bucket, specified in the cursor prefix
|
||||
if !threshold && cursor.bucket != lb.bucket {
|
||||
continue // start from the bucket, specified in the cursor bucket
|
||||
}
|
||||
|
||||
cursorPrefix = lb.prefix
|
||||
result, cursor = selectNFromBucket(tx, lb.name, prefix, result, count, cursor, threshold)
|
||||
cursor.bucket = lb.bucket
|
||||
if len(result) >= count {
|
||||
break loop
|
||||
}
|
||||
|
@ -142,10 +128,10 @@ loop:
|
|||
}
|
||||
|
||||
if len(result) == 0 {
|
||||
return nil, "", core.ErrEndOfListing
|
||||
return nil, nil, core.ErrEndOfListing
|
||||
}
|
||||
|
||||
return result, string(cursorPrefix) + cursor, nil
|
||||
return result, cursor, nil
|
||||
}
|
||||
|
||||
// selectNFromBucket similar to selectAllFromBucket but uses cursor to find
|
||||
|
@ -155,22 +141,24 @@ func selectNFromBucket(tx *bbolt.Tx,
|
|||
prefix string, // string of CID, optimization
|
||||
to []*object.Address, // listing result
|
||||
limit int, // stop listing at `limit` items in result
|
||||
cursor string, // start from cursor object
|
||||
cursor *Cursor, // start from cursor object
|
||||
threshold bool, // ignore cursor and start immediately
|
||||
) ([]*object.Address, string) {
|
||||
) ([]*object.Address, *Cursor) {
|
||||
bkt := tx.Bucket(name)
|
||||
if bkt == nil {
|
||||
return to, cursor
|
||||
}
|
||||
|
||||
count := len(to)
|
||||
if cursor == nil {
|
||||
cursor = new(Cursor)
|
||||
}
|
||||
|
||||
count := len(to)
|
||||
c := bkt.Cursor()
|
||||
k, _ := c.First()
|
||||
|
||||
if !threshold {
|
||||
seekKey := strings.Replace(cursor, prefix, "", 1)
|
||||
c.Seek([]byte(seekKey))
|
||||
c.Seek([]byte(cursor.address.ObjectID().String()))
|
||||
k, _ = c.Next() // we are looking for objects _after_ the cursor
|
||||
}
|
||||
|
||||
|
@ -178,19 +166,14 @@ func selectNFromBucket(tx *bbolt.Tx,
|
|||
if count >= limit {
|
||||
break
|
||||
}
|
||||
|
||||
key := prefix + string(k)
|
||||
cursor = key
|
||||
|
||||
a := object.NewAddress()
|
||||
if err := a.Parse(key); err != nil {
|
||||
if err := a.Parse(prefix + string(k)); err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
cursor.address = a
|
||||
if inGraveyard(tx, a) > 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
to = append(to, a)
|
||||
count++
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue