Add method to find all ID's for the nice names

Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
Evgeniy Kulikov 2020-08-20 22:33:59 +03:00
parent a3c95cffb1
commit 21591ebe65

View file

@ -120,13 +120,10 @@ func (n *layer) objectSearchContainer(ctx context.Context, cid refs.CID) ([]refs
return result, nil return result, nil
} }
// objectFindID returns object id (uuid) based on it's nice name in s3. If // objectFindIDs returns object id's (uuid) based on they nice name in s3. If
// nice name is uuid compatible, then function returns it. // nice name is uuid compatible, then function returns it.
func (n *layer) objectFindID(ctx context.Context, cid refs.CID, name string, put bool) (refs.ObjectID, error) { func (n *layer) objectFindIDs(ctx context.Context, cid refs.CID, name string) ([]refs.ObjectID, error) {
var ( var q query.Query
id refs.ObjectID
q query.Query
)
q.Filters = append(q.Filters, query.Filter{ q.Filters = append(q.Filters, query.Filter{
Type: query.Filter_Exact, Type: query.Filter_Exact,
@ -140,12 +137,12 @@ func (n *layer) objectFindID(ctx context.Context, cid refs.CID, name string, put
queryBinary, err := q.Marshal() queryBinary, err := q.Marshal()
if err != nil { if err != nil {
return id, err return nil, err
} }
conn, err := n.cli.GetConnection(ctx) conn, err := n.cli.GetConnection(ctx)
if err != nil { if err != nil {
return id, err return nil, err
} }
token, err := n.cli.SessionToken(ctx, &pool.SessionParams{ token, err := n.cli.SessionToken(ctx, &pool.SessionParams{
@ -154,7 +151,7 @@ func (n *layer) objectFindID(ctx context.Context, cid refs.CID, name string, put
Verb: service.Token_Info_Search, Verb: service.Token_Info_Search,
}) })
if err != nil { if err != nil {
return id, err return nil, err
} }
req := new(object.SearchRequest) req := new(object.SearchRequest)
@ -167,7 +164,7 @@ func (n *layer) objectFindID(ctx context.Context, cid refs.CID, name string, put
err = service.SignRequestData(n.key, req) err = service.SignRequestData(n.key, req)
if err != nil { if err != nil {
return id, err return nil, err
} }
// todo: think about timeout // todo: think about timeout
@ -176,7 +173,7 @@ func (n *layer) objectFindID(ctx context.Context, cid refs.CID, name string, put
searchClient, err := object.NewServiceClient(conn).Search(ctx, req) searchClient, err := object.NewServiceClient(conn).Search(ctx, req)
if err != nil { if err != nil {
return id, err return nil, err
} }
var response []refs.Address var response []refs.Address
@ -188,18 +185,33 @@ func (n *layer) objectFindID(ctx context.Context, cid refs.CID, name string, put
break break
} }
return id, errors.New("search command received error") return nil, errors.New("search command received error")
} }
response = append(response, resp.Addresses...) response = append(response, resp.Addresses...)
} }
switch ln := len(response); { switch ln := len(response); {
case ln > 1: case ln > 0:
return id, errors.New("several objects with the same name found") result := make([]refs.ObjectID, 0, len(response))
case ln == 1: for i := range response {
return response[0].ObjectID, nil result = append(result, response[i].ObjectID)
}
return result, nil
default: default:
return nil, errors.New("object not found")
}
}
// objectFindID returns object id (uuid) based on it's nice name in s3. If
// nice name is uuid compatible, then function returns it.
func (n *layer) objectFindID(ctx context.Context, cid refs.CID, name string, put bool) (refs.ObjectID, error) {
var id refs.ObjectID
if result, err := n.objectFindIDs(ctx, cid, name); err != nil {
return id, err
} else if ln := len(result); ln == 0 {
// Minio lists all objects with and without nice names. All objects // Minio lists all objects with and without nice names. All objects
// without nice name still have "name" in terms of minio - uuid encoded // without nice name still have "name" in terms of minio - uuid encoded
// into string. There is a tricky case when user upload object // into string. There is a tricky case when user upload object
@ -215,7 +227,11 @@ func (n *layer) objectFindID(ctx context.Context, cid refs.CID, name string, put
} }
} }
return id, errors.New("object not found") return id, errors.New("object not found")
} else if ln == 1 {
return result[0], nil
} }
return id, errors.New("several objects with the same name found")
} }
// objectHead returns all object's headers. // objectHead returns all object's headers.