2020-09-22 06:51:47 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2020-10-29 14:57:07 +00:00
|
|
|
v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2020-09-22 06:51:47 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/search/query"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Query struct {
|
2020-10-01 12:12:39 +00:00
|
|
|
filters objectSDK.SearchFilters
|
2020-09-22 06:51:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 12:12:39 +00:00
|
|
|
func New(filters objectSDK.SearchFilters) query.Query {
|
2020-09-22 06:51:47 +00:00
|
|
|
return &Query{
|
|
|
|
filters: filters,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func idValue(id *objectSDK.ID) string {
|
2020-10-21 15:49:07 +00:00
|
|
|
return id.String()
|
2020-09-22 06:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func cidValue(id *container.ID) string {
|
2020-10-21 15:49:07 +00:00
|
|
|
return id.String()
|
2020-09-22 06:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ownerIDValue(id *owner.ID) string {
|
2020-10-21 15:49:07 +00:00
|
|
|
return id.String()
|
2020-09-22 06:51:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 12:12:39 +00:00
|
|
|
func (q *Query) Match(obj *object.Object, handler func(*objectSDK.ID)) {
|
|
|
|
for par := (*object.Object)(nil); obj != nil; par, obj = obj, obj.GetParent() {
|
|
|
|
match := true
|
|
|
|
|
|
|
|
for i := 0; match && i < len(q.filters); i++ {
|
|
|
|
switch typ := q.filters[i].Operation(); typ {
|
|
|
|
default:
|
|
|
|
match = false
|
|
|
|
case objectSDK.MatchStringEqual:
|
|
|
|
switch key := q.filters[i].Header(); key {
|
|
|
|
default:
|
|
|
|
match = headerEqual(obj, key, q.filters[i].Value())
|
2020-10-29 14:57:07 +00:00
|
|
|
case v2object.FilterPropertyRoot:
|
|
|
|
match = (q.filters[i].Value() == v2object.BooleanPropertyValueTrue) == (!obj.HasParent() &&
|
2020-10-16 10:36:00 +00:00
|
|
|
obj.GetType() == objectSDK.TypeRegular)
|
2020-10-29 14:57:07 +00:00
|
|
|
case v2object.FilterPropertyLeaf:
|
|
|
|
match = (q.filters[i].Value() == v2object.BooleanPropertyValueTrue) == (par == nil)
|
2020-10-01 12:12:39 +00:00
|
|
|
}
|
2020-09-22 06:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 12:12:39 +00:00
|
|
|
if match {
|
|
|
|
handler(obj.GetID())
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 06:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func headerEqual(obj *object.Object, key, value string) bool {
|
|
|
|
switch key {
|
|
|
|
default:
|
|
|
|
for _, attr := range obj.GetAttributes() {
|
|
|
|
if attr.GetKey() == key && attr.GetValue() == value {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2020-10-29 14:57:07 +00:00
|
|
|
case v2object.FilterHeaderContainerID:
|
2020-09-22 06:51:47 +00:00
|
|
|
return value == cidValue(obj.GetContainerID())
|
2020-10-29 14:57:07 +00:00
|
|
|
case v2object.FilterHeaderOwnerID:
|
2020-09-22 06:51:47 +00:00
|
|
|
return value == ownerIDValue(obj.GetOwnerID())
|
2020-10-29 14:57:07 +00:00
|
|
|
case v2object.FilterPropertyChildfree:
|
|
|
|
return (value == v2object.BooleanPropertyValueTrue) == (len(obj.GetChildren()) == 0)
|
|
|
|
case v2object.FilterHeaderParent:
|
2020-10-01 11:42:17 +00:00
|
|
|
return idValue(obj.GetParentID()) == value
|
2020-09-22 06:51:47 +00:00
|
|
|
// TODO: add other headers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Query) ToSearchFilters() objectSDK.SearchFilters {
|
2020-10-01 12:12:39 +00:00
|
|
|
return q.filters
|
2020-09-22 06:51:47 +00:00
|
|
|
}
|