2021-10-27 14:51:58 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2021-11-10 11:07:57 +00:00
|
|
|
"sort"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
objectcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard"
|
2021-10-27 14:51:58 +00:00
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ErrEndOfListing is returned from an object listing with cursor
|
|
|
|
// when the storage can't return any more objects after the provided
|
2021-11-17 11:31:31 +00:00
|
|
|
// cursor. Use nil cursor object to start listing again.
|
|
|
|
var ErrEndOfListing = shard.ErrEndOfListing
|
|
|
|
|
2021-11-11 14:27:11 +00:00
|
|
|
// Cursor is a type for continuous object listing.
|
|
|
|
type Cursor struct {
|
|
|
|
shardID string
|
|
|
|
shardCursor *shard.Cursor
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:51:58 +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 14:51:58 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// WithCount sets the maximum amount of addresses that ListWithCursor should return.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (p *ListWithCursorPrm) WithCount(count uint32) {
|
2021-10-27 14:51:58 +00:00
|
|
|
p.count = count
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// WithCursor sets a cursor for ListWithCursor operation. For initial request
|
|
|
|
// ignore this param or use nil value. For consecutive requests, use value
|
2021-10-27 14:51:58 +00:00
|
|
|
// from ListWithCursorRes.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (p *ListWithCursorPrm) WithCursor(cursor *Cursor) {
|
2021-10-27 14:51:58 +00:00
|
|
|
p.cursor = cursor
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListWithCursorRes contains values returned from ListWithCursor operation.
|
|
|
|
type ListWithCursorRes struct {
|
2022-11-12 13:48:44 +00:00
|
|
|
addrList []objectcore.AddressWithType
|
2021-11-11 14:27:11 +00:00
|
|
|
cursor *Cursor
|
2021-10-27 14:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddressList returns addresses selected by ListWithCursor operation.
|
2022-11-12 13:48:44 +00:00
|
|
|
func (l ListWithCursorRes) AddressList() []objectcore.AddressWithType {
|
2021-10-27 14:51:58 +00:00
|
|
|
return l.addrList
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cursor returns cursor for consecutive listing requests.
|
2021-11-11 14:27:11 +00:00
|
|
|
func (l ListWithCursorRes) Cursor() *Cursor {
|
2021-10-27 14:51:58 +00:00
|
|
|
return l.cursor
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ListWithCursor lists physical objects available in the engine starting
|
|
|
|
// from the cursor. It includes regular, tombstone and storage group objects.
|
|
|
|
// Does not include inhumed objects. Use cursor value from the response
|
2021-10-27 14:51:58 +00:00
|
|
|
// 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:56:59 +00:00
|
|
|
func (e *StorageEngine) ListWithCursor(prm ListWithCursorPrm) (ListWithCursorRes, error) {
|
2022-11-12 13:48:44 +00:00
|
|
|
result := make([]objectcore.AddressWithType, 0, prm.count)
|
2021-11-10 11:07:57 +00:00
|
|
|
|
|
|
|
// 1. Get available shards and sort them.
|
2021-10-27 14:51:58 +00:00
|
|
|
e.mtx.RLock()
|
2021-11-10 11:07:57 +00:00
|
|
|
shardIDs := make([]string, 0, len(e.shards))
|
|
|
|
for id := range e.shards {
|
|
|
|
shardIDs = append(shardIDs, id)
|
|
|
|
}
|
2021-10-27 14:51:58 +00:00
|
|
|
e.mtx.RUnlock()
|
|
|
|
|
2021-11-10 11:07:57 +00:00
|
|
|
if len(shardIDs) == 0 {
|
2022-05-31 11:56:59 +00:00
|
|
|
return ListWithCursorRes{}, ErrEndOfListing
|
2021-10-27 14:51:58 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 11:07:57 +00:00
|
|
|
sort.Slice(shardIDs, func(i, j int) bool {
|
|
|
|
return shardIDs[i] < shardIDs[j]
|
|
|
|
})
|
|
|
|
|
2021-11-11 14:27:11 +00:00
|
|
|
// 2. Prepare cursor object.
|
2021-11-10 11:07:57 +00:00
|
|
|
cursor := prm.cursor
|
2021-11-11 14:27:11 +00:00
|
|
|
if cursor == nil {
|
|
|
|
cursor = &Cursor{shardID: shardIDs[0]}
|
2021-11-10 11:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Iterate over available shards. Skip unavailable shards.
|
|
|
|
for i := range shardIDs {
|
|
|
|
if len(result) >= int(prm.count) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-11-11 14:27:11 +00:00
|
|
|
if shardIDs[i] < cursor.shardID {
|
2021-11-10 11:07:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
e.mtx.RLock()
|
|
|
|
shardInstance, ok := e.shards[shardIDs[i]]
|
|
|
|
e.mtx.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
count := uint32(int(prm.count) - len(result))
|
2022-05-20 18:08:59 +00:00
|
|
|
var shardPrm shard.ListWithCursorPrm
|
|
|
|
shardPrm.WithCount(count)
|
2021-11-11 14:27:11 +00:00
|
|
|
if shardIDs[i] == cursor.shardID {
|
|
|
|
shardPrm.WithCursor(cursor.shardCursor)
|
2021-11-10 11:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res, err := shardInstance.ListWithCursor(shardPrm)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, res.AddressList()...)
|
2021-11-11 14:27:11 +00:00
|
|
|
cursor.shardCursor = res.Cursor()
|
|
|
|
cursor.shardID = shardIDs[i]
|
2021-11-10 11:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(result) == 0 {
|
2022-05-31 11:56:59 +00:00
|
|
|
return ListWithCursorRes{}, ErrEndOfListing
|
2021-10-27 14:51:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 11:56:59 +00:00
|
|
|
return ListWithCursorRes{
|
2021-11-10 11:07:57 +00:00
|
|
|
addrList: result,
|
2021-11-11 14:27:11 +00:00
|
|
|
cursor: cursor,
|
2021-10-27 14:51:58 +00:00
|
|
|
}, nil
|
|
|
|
}
|