2022-06-01 12:42:28 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
oidSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-06-01 12:42:28 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
searchFilters []string
|
|
|
|
|
|
|
|
objectSearchCmd = &cobra.Command{
|
|
|
|
Use: "search",
|
|
|
|
Short: "Search object",
|
|
|
|
Long: "Search object",
|
|
|
|
Run: searchObject,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func initObjectSearchCmd() {
|
|
|
|
commonflags.Init(objectSearchCmd)
|
2022-10-20 09:40:33 +00:00
|
|
|
initFlagSession(objectSearchCmd, "SEARCH")
|
2022-06-01 12:42:28 +00:00
|
|
|
|
|
|
|
flags := objectSearchCmd.Flags()
|
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
flags.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage)
|
|
|
|
_ = objectSearchCmd.MarkFlagRequired(commonflags.CIDFlag)
|
2022-06-01 12:42:28 +00:00
|
|
|
|
|
|
|
flags.StringSliceVarP(&searchFilters, "filters", "f", nil,
|
|
|
|
"Repeated filter expressions or files with protobuf JSON")
|
|
|
|
|
|
|
|
flags.Bool("root", false, "Search for user objects")
|
|
|
|
flags.Bool("phy", false, "Search physically stored objects")
|
2022-10-18 11:43:04 +00:00
|
|
|
flags.String(commonflags.OIDFlag, "", "Search object by identifier")
|
2022-06-01 12:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func searchObject(cmd *cobra.Command, _ []string) {
|
|
|
|
var cnr cid.ID
|
|
|
|
readCID(cmd, &cnr)
|
|
|
|
|
|
|
|
sf, err := parseSearchFilters(cmd)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", err)
|
2022-06-01 12:42:28 +00:00
|
|
|
|
|
|
|
pk := key.GetOrGenerate(cmd)
|
|
|
|
|
2022-10-10 18:31:19 +00:00
|
|
|
cli := internalclient.GetSDKClientByFlag(cmd, pk, commonflags.RPC)
|
|
|
|
|
2022-06-01 12:42:28 +00:00
|
|
|
var prm internalclient.SearchObjectsPrm
|
2022-10-10 18:31:19 +00:00
|
|
|
prm.SetClient(cli)
|
2022-06-01 12:42:28 +00:00
|
|
|
Prepare(cmd, &prm)
|
2022-10-10 18:31:19 +00:00
|
|
|
readSessionGlobal(cmd, &prm, pk, cnr)
|
2022-06-01 12:42:28 +00:00
|
|
|
prm.SetContainerID(cnr)
|
|
|
|
prm.SetFilters(sf)
|
|
|
|
|
2023-05-24 13:51:57 +00:00
|
|
|
res, err := internalclient.SearchObjects(cmd.Context(), prm)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "rpc error: %w", err)
|
2022-06-01 12:42:28 +00:00
|
|
|
|
|
|
|
ids := res.IDList()
|
|
|
|
|
|
|
|
cmd.Printf("Found %d objects.\n", len(ids))
|
|
|
|
for i := range ids {
|
|
|
|
cmd.Println(ids[i].String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
var searchUnaryOpVocabulary = map[string]objectSDK.SearchMatchType{
|
|
|
|
"NOPRESENT": objectSDK.MatchNotPresent,
|
2022-06-01 12:42:28 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
var searchBinaryOpVocabulary = map[string]objectSDK.SearchMatchType{
|
|
|
|
"EQ": objectSDK.MatchStringEqual,
|
|
|
|
"NE": objectSDK.MatchStringNotEqual,
|
|
|
|
"COMMON_PREFIX": objectSDK.MatchCommonPrefix,
|
2022-06-01 12:42:28 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
func parseSearchFilters(cmd *cobra.Command) (objectSDK.SearchFilters, error) {
|
|
|
|
var fs objectSDK.SearchFilters
|
2022-06-01 12:42:28 +00:00
|
|
|
|
|
|
|
for i := range searchFilters {
|
|
|
|
words := strings.Fields(searchFilters[i])
|
|
|
|
|
|
|
|
switch len(words) {
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid field number: %d", len(words))
|
|
|
|
case 1:
|
|
|
|
data, err := os.ReadFile(words[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not read attributes filter from file: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
subFs := objectSDK.NewSearchFilters()
|
2022-06-01 12:42:28 +00:00
|
|
|
|
|
|
|
if err := subFs.UnmarshalJSON(data); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not unmarshal attributes filter from file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fs = append(fs, subFs...)
|
|
|
|
case 2:
|
|
|
|
m, ok := searchUnaryOpVocabulary[words[1]]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unsupported unary op: %s", words[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.AddFilter(words[0], "", m)
|
|
|
|
case 3:
|
|
|
|
m, ok := searchBinaryOpVocabulary[words[1]]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unsupported binary op: %s", words[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.AddFilter(words[0], words[2], m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
root, _ := cmd.Flags().GetBool("root")
|
|
|
|
if root {
|
|
|
|
fs.AddRootFilter()
|
|
|
|
}
|
|
|
|
|
|
|
|
phy, _ := cmd.Flags().GetBool("phy")
|
|
|
|
if phy {
|
|
|
|
fs.AddPhyFilter()
|
|
|
|
}
|
|
|
|
|
2022-10-18 11:43:04 +00:00
|
|
|
oid, _ := cmd.Flags().GetString(commonflags.OIDFlag)
|
2022-06-01 12:42:28 +00:00
|
|
|
if oid != "" {
|
|
|
|
var id oidSDK.ID
|
|
|
|
if err := id.DecodeString(oid); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse object ID: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
fs.AddObjectIDFilter(objectSDK.MatchStringEqual, id)
|
2022-06-01 12:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fs, nil
|
|
|
|
}
|