[#186] Refactor objectSearch, fix objectPut

Fixed panic in objectPut() if input reader is nil
Made objectSearch searching by any attribute

Signed-off-by: Angira Kekteeva <kira@nspcc.ru>
This commit is contained in:
Angira Kekteeva 2021-11-13 23:35:50 +03:00 committed by Alex Vanin
parent befe084900
commit 284a560ea6
3 changed files with 42 additions and 20 deletions

View file

@ -587,7 +587,7 @@ func (n *layer) deleteObject(ctx context.Context, bkt *data.BucketInfo, obj *Ver
obj.DeleteMarkVersion = objInfo.Version()
}
} else {
ids, err = n.objectSearch(ctx, &findParams{cid: bkt.CID, val: obj.Name})
ids, err = n.objectSearchByName(ctx, bkt.CID, obj.Name)
if err != nil {
obj.Error = err
return obj

View file

@ -22,10 +22,14 @@ import (
type (
findParams struct {
attr string
val string
cid *cid.ID
prefix string
filters []filter
cid *cid.ID
prefix string
}
filter struct {
attr string
val string
}
getParams struct {
@ -68,21 +72,29 @@ type (
}
)
func (n *layer) objectSearchByName(ctx context.Context, cid *cid.ID, filename string) ([]*object.ID, error) {
f := &findParams{
filters: []filter{{attr: object.AttributeFileName, val: filename}},
cid: cid,
prefix: "",
}
return n.objectSearch(ctx, f)
}
// objectSearch returns all available objects by search params.
func (n *layer) objectSearch(ctx context.Context, p *findParams) ([]*object.ID, error) {
var opts object.SearchFilters
opts.AddRootFilter()
if filename, err := url.QueryUnescape(p.val); err != nil {
return nil, err
} else if filename != "" {
if p.attr == "" {
opts.AddFilter(object.AttributeFileName, filename, object.MatchStringEqual)
} else {
opts.AddFilter(p.attr, filename, object.MatchStringEqual)
for _, filter := range p.filters {
if val, err := url.QueryUnescape(filter.val); err != nil {
return nil, err
} else if val != "" {
opts.AddFilter(filter.attr, val, object.MatchStringEqual)
}
}
if prefix, err := url.QueryUnescape(p.prefix); err != nil {
return nil, err
} else if prefix != "" {
@ -142,12 +154,14 @@ func (n *layer) objectPut(ctx context.Context, bkt *data.BucketInfo, p *PutObjec
idsToDeleteArr := updateCRDT2PSetHeaders(p.Header, versions, versioningEnabled)
r := p.Reader
if len(p.Header[api.ContentType]) == 0 {
d := newDetector(r)
if contentType, err := d.Detect(); err == nil {
p.Header[api.ContentType] = contentType
if r != nil {
if len(p.Header[api.ContentType]) == 0 {
d := newDetector(r)
if contentType, err := d.Detect(); err == nil {
p.Header[api.ContentType] = contentType
}
r = d.MultiReader()
}
r = d.MultiReader()
}
rawObject := formRawObject(p, bkt.CID, own, obj)
@ -307,7 +321,7 @@ func (n *layer) headLastVersionIfNotDeleted(ctx context.Context, bkt *data.Bucke
}
func (n *layer) headVersions(ctx context.Context, bkt *data.BucketInfo, objectName string) (*objectVersions, error) {
ids, err := n.objectSearch(ctx, &findParams{cid: bkt.CID, val: objectName})
ids, err := n.objectSearchByName(ctx, bkt.CID, objectName)
if err != nil {
return nil, err
}

View file

@ -44,7 +44,11 @@ func (n *layer) headSystemObject(ctx context.Context, bkt *data.BucketInfo, objN
}
func (n *layer) deleteSystemObject(ctx context.Context, bktInfo *data.BucketInfo, name string) error {
ids, err := n.objectSearch(ctx, &findParams{cid: bktInfo.CID, attr: objectSystemAttributeName, val: name})
f := &findParams{
filters: []filter{{attr: objectSystemAttributeName, val: name}},
cid: bktInfo.CID,
}
ids, err := n.objectSearch(ctx, f)
if err != nil {
return err
}
@ -166,7 +170,11 @@ func (n *layer) getCORS(ctx context.Context, bkt *data.BucketInfo, sysName strin
}
func (n *layer) headSystemVersions(ctx context.Context, bkt *data.BucketInfo, sysName string) (*objectVersions, error) {
ids, err := n.objectSearch(ctx, &findParams{cid: bkt.CID, attr: objectSystemAttributeName, val: sysName})
f := &findParams{
filters: []filter{{attr: objectSystemAttributeName, val: sysName}},
cid: bkt.CID,
}
ids, err := n.objectSearch(ctx, f)
if err != nil {
return nil, err
}