[#231] engine: Add List method

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-12-03 15:01:45 +03:00
parent a7087faa1a
commit ee1e3931a5

View file

@ -65,6 +65,44 @@ func (e *StorageEngine) Select(prm *SelectPrm) (*SelectRes, error) {
}, nil
}
// List returns `limit` available physically storage object addresses in engine.
// If limit is zero, then returns all available object addresses.
func (e *StorageEngine) List(limit uint64) (*SelectRes, error) {
addrList := make([]*object.Address, limit)
uniqueMap := make(map[string]struct{})
ln := uint64(0)
// consider iterating over shuffled shards
e.iterateOverUnsortedShards(func(sh *shard.Shard) (stop bool) {
res, err := sh.List() // consider limit result of shard iterator
if err != nil {
// TODO: smth wrong with shard, need to be processed
e.log.Warn("could not select objects from shard",
zap.Stringer("shard", sh.ID()),
zap.String("error", err.Error()),
)
} else {
for _, addr := range res.AddressList() { // save only unique values
if _, ok := uniqueMap[addr.String()]; !ok {
uniqueMap[addr.String()] = struct{}{}
addrList = append(addrList, addr)
ln++
if limit > 0 && ln >= limit {
return true
}
}
}
}
return false
})
return &SelectRes{
addrList: addrList,
}, nil
}
// Select selects objects from local storage using provided filters.
func Select(storage *StorageEngine, fs object.SearchFilters) ([]*object.Address, error) {
res, err := storage.Select(new(SelectPrm).
@ -77,7 +115,13 @@ func Select(storage *StorageEngine, fs object.SearchFilters) ([]*object.Address,
return res.AddressList(), nil
}
// SelectAll selects all objects from local storage.
func SelectAll(storage *StorageEngine) ([]*object.Address, error) {
return Select(storage, object.SearchFilters{})
// List returns `limit` available physically storage object addresses in
// engine. If limit is zero, then returns all available object addresses.
func List(storage *StorageEngine, limit uint64) ([]*object.Address, error) {
res, err := storage.List(limit)
if err != nil {
return nil, err
}
return res.AddressList(), nil
}