frostfs-node/pkg/morph/client/container/list.go
Alexander Chuprov 9b113c3156
[#1613] morph: Add tracing for morph queries to neo-go
Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
2025-02-05 16:38:20 +03:00

69 lines
1.7 KiB
Go

package container
import (
"context"
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// iterate iterates through a list of container identifiers belonging
// to the specified user of FrostFS system. The list is composed
// through Container contract call.
//
// Iterates through the identifiers of all FrostFS containers if pointer
// to user identifier is nil.
func (c *Client) iterate(ctx context.Context, idUser *user.ID, cb func(cid.ID) error) error {
var rawID []byte
if idUser != nil {
rawID = idUser.WalletBytes()
}
prm := client.TestInvokePrm{}
prm.SetMethod(listMethod)
prm.SetArgs(rawID)
res, err := c.client.TestInvoke(ctx, prm)
if err != nil {
return fmt.Errorf("test invoke (%s): %w", listMethod, err)
} else if ln := len(res); ln != 1 {
return fmt.Errorf("unexpected stack item count (%s): %d", listMethod, ln)
}
res, err = client.ArrayFromStackItem(res[0])
if err != nil {
return fmt.Errorf("get stack item array from stack item (%s): %w", listMethod, err)
}
for i := range res {
id, err := getCIDfromStackItem(res[i])
if err != nil {
return err
}
if err = cb(id); err != nil {
return err
}
}
return nil
}
func getCIDfromStackItem(item stackitem.Item) (cid.ID, error) {
rawID, err := client.BytesFromStackItem(item)
if err != nil {
return cid.ID{}, fmt.Errorf("get byte array from stack item (%s): %w", listMethod, err)
}
var id cid.ID
err = id.Decode(rawID)
if err != nil {
return cid.ID{}, fmt.Errorf("decode container ID: %w", err)
}
return id, nil
}