2020-09-17 09:20:35 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
2021-02-09 22:32:13 +00:00
|
|
|
"encoding/json"
|
2020-10-27 17:39:41 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
2021-05-31 12:09:04 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2020-10-27 17:39:41 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2020-09-17 09:20:35 +00:00
|
|
|
v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SearchMatchType indicates match operation on specified header.
|
|
|
|
type SearchMatchType uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
MatchUnknown SearchMatchType = iota
|
|
|
|
MatchStringEqual
|
2021-02-01 17:57:18 +00:00
|
|
|
MatchStringNotEqual
|
|
|
|
MatchNotPresent
|
2021-08-16 11:59:21 +00:00
|
|
|
MatchCommonPrefix
|
2020-09-17 09:20:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (m SearchMatchType) ToV2() v2object.MatchType {
|
|
|
|
switch m {
|
|
|
|
case MatchStringEqual:
|
|
|
|
return v2object.MatchStringEqual
|
2021-02-01 17:57:18 +00:00
|
|
|
case MatchStringNotEqual:
|
|
|
|
return v2object.MatchStringNotEqual
|
|
|
|
case MatchNotPresent:
|
|
|
|
return v2object.MatchNotPresent
|
2021-08-16 11:59:21 +00:00
|
|
|
case MatchCommonPrefix:
|
|
|
|
return v2object.MatchCommonPrefix
|
2020-09-17 09:20:35 +00:00
|
|
|
default:
|
|
|
|
return v2object.MatchUnknown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SearchMatchFromV2(t v2object.MatchType) (m SearchMatchType) {
|
|
|
|
switch t {
|
|
|
|
case v2object.MatchStringEqual:
|
|
|
|
m = MatchStringEqual
|
2021-02-01 17:57:18 +00:00
|
|
|
case v2object.MatchStringNotEqual:
|
|
|
|
m = MatchStringNotEqual
|
|
|
|
case v2object.MatchNotPresent:
|
|
|
|
m = MatchNotPresent
|
2021-08-16 11:59:21 +00:00
|
|
|
case v2object.MatchCommonPrefix:
|
|
|
|
m = MatchCommonPrefix
|
2020-09-17 09:20:35 +00:00
|
|
|
default:
|
|
|
|
m = MatchUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2021-06-18 12:27:01 +00:00
|
|
|
// String returns string representation of SearchMatchType.
|
|
|
|
//
|
|
|
|
// String mapping:
|
|
|
|
// * MatchStringEqual: STRING_EQUAL;
|
|
|
|
// * MatchStringNotEqual: STRING_NOT_EQUAL;
|
|
|
|
// * MatchNotPresent: NOT_PRESENT;
|
2021-08-16 11:59:21 +00:00
|
|
|
// * MatchCommonPrefix: COMMON_PREFIX;
|
2021-06-18 12:27:01 +00:00
|
|
|
// * MatchUnknown, default: MATCH_TYPE_UNSPECIFIED.
|
|
|
|
func (m SearchMatchType) String() string {
|
|
|
|
return m.ToV2().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromString parses SearchMatchType from a string representation.
|
|
|
|
// It is a reverse action to String().
|
|
|
|
//
|
|
|
|
// Returns true if s was parsed successfully.
|
|
|
|
func (m *SearchMatchType) FromString(s string) bool {
|
|
|
|
var g v2object.MatchType
|
|
|
|
|
|
|
|
ok := g.FromString(s)
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
*m = SearchMatchFromV2(g)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2020-09-17 09:20:35 +00:00
|
|
|
type SearchFilter struct {
|
2020-10-27 17:39:41 +00:00
|
|
|
header filterKey
|
|
|
|
value fmt.Stringer
|
2020-09-17 09:20:35 +00:00
|
|
|
op SearchMatchType
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:39:41 +00:00
|
|
|
type staticStringer string
|
|
|
|
|
|
|
|
type filterKey struct {
|
|
|
|
typ filterKeyType
|
|
|
|
|
|
|
|
str string
|
|
|
|
}
|
|
|
|
|
|
|
|
// enumeration of reserved filter keys.
|
|
|
|
type filterKeyType int
|
|
|
|
|
2020-10-27 18:11:58 +00:00
|
|
|
type boolStringer bool
|
|
|
|
|
2020-09-17 09:20:35 +00:00
|
|
|
type SearchFilters []SearchFilter
|
|
|
|
|
2020-10-27 17:39:41 +00:00
|
|
|
const (
|
|
|
|
_ filterKeyType = iota
|
|
|
|
fKeyVersion
|
2020-11-10 10:43:25 +00:00
|
|
|
fKeyObjectID
|
2020-10-27 17:39:41 +00:00
|
|
|
fKeyContainerID
|
|
|
|
fKeyOwnerID
|
|
|
|
fKeyCreationEpoch
|
|
|
|
fKeyPayloadLength
|
|
|
|
fKeyPayloadHash
|
|
|
|
fKeyType
|
|
|
|
fKeyHomomorphicHash
|
|
|
|
fKeyParent
|
2020-11-25 16:32:52 +00:00
|
|
|
fKeySplitID
|
2020-10-27 18:11:58 +00:00
|
|
|
fKeyPropRoot
|
2020-11-09 12:29:26 +00:00
|
|
|
fKeyPropPhy
|
2020-10-27 17:39:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (k filterKey) String() string {
|
|
|
|
switch k.typ {
|
|
|
|
default:
|
|
|
|
return k.str
|
|
|
|
case fKeyVersion:
|
|
|
|
return v2object.FilterHeaderVersion
|
2020-11-10 10:43:25 +00:00
|
|
|
case fKeyObjectID:
|
|
|
|
return v2object.FilterHeaderObjectID
|
2020-10-27 17:39:41 +00:00
|
|
|
case fKeyContainerID:
|
|
|
|
return v2object.FilterHeaderContainerID
|
|
|
|
case fKeyOwnerID:
|
|
|
|
return v2object.FilterHeaderOwnerID
|
|
|
|
case fKeyCreationEpoch:
|
|
|
|
return v2object.FilterHeaderCreationEpoch
|
|
|
|
case fKeyPayloadLength:
|
|
|
|
return v2object.FilterHeaderPayloadLength
|
|
|
|
case fKeyPayloadHash:
|
|
|
|
return v2object.FilterHeaderPayloadHash
|
|
|
|
case fKeyType:
|
|
|
|
return v2object.FilterHeaderObjectType
|
|
|
|
case fKeyHomomorphicHash:
|
|
|
|
return v2object.FilterHeaderHomomorphicHash
|
|
|
|
case fKeyParent:
|
|
|
|
return v2object.FilterHeaderParent
|
2020-11-25 16:32:52 +00:00
|
|
|
case fKeySplitID:
|
|
|
|
return v2object.FilterHeaderSplitID
|
2020-10-27 18:11:58 +00:00
|
|
|
case fKeyPropRoot:
|
|
|
|
return v2object.FilterPropertyRoot
|
2020-11-09 12:29:26 +00:00
|
|
|
case fKeyPropPhy:
|
|
|
|
return v2object.FilterPropertyPhy
|
2020-10-27 17:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s staticStringer) String() string {
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
2020-10-27 18:11:58 +00:00
|
|
|
func (s boolStringer) String() string {
|
|
|
|
if s {
|
|
|
|
return v2object.BooleanPropertyValueTrue
|
|
|
|
}
|
|
|
|
|
|
|
|
return v2object.BooleanPropertyValueFalse
|
|
|
|
}
|
|
|
|
|
2020-09-17 09:20:35 +00:00
|
|
|
func (f *SearchFilter) Header() string {
|
2020-10-27 17:39:41 +00:00
|
|
|
return f.header.String()
|
2020-09-17 09:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilter) Value() string {
|
2020-10-27 17:39:41 +00:00
|
|
|
return f.value.String()
|
2020-09-17 09:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilter) Operation() SearchMatchType {
|
|
|
|
return f.op
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSearchFilters() SearchFilters {
|
|
|
|
return SearchFilters{}
|
|
|
|
}
|
|
|
|
|
2020-09-17 10:00:27 +00:00
|
|
|
func NewSearchFiltersFromV2(v2 []*v2object.SearchFilter) SearchFilters {
|
2020-09-17 09:20:35 +00:00
|
|
|
filters := make(SearchFilters, 0, len(v2))
|
2020-11-16 14:54:43 +00:00
|
|
|
|
2020-09-17 09:20:35 +00:00
|
|
|
for i := range v2 {
|
2020-09-17 10:00:27 +00:00
|
|
|
if v2[i] == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:39:41 +00:00
|
|
|
filters.AddFilter(
|
|
|
|
v2[i].GetKey(),
|
|
|
|
v2[i].GetValue(),
|
|
|
|
SearchMatchFromV2(v2[i].GetMatchType()),
|
|
|
|
)
|
2020-09-17 09:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return filters
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:39:41 +00:00
|
|
|
func (f *SearchFilters) addFilter(op SearchMatchType, keyTyp filterKeyType, key string, val fmt.Stringer) {
|
2020-09-17 09:20:35 +00:00
|
|
|
if *f == nil {
|
|
|
|
*f = make(SearchFilters, 0, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
*f = append(*f, SearchFilter{
|
2020-10-27 17:39:41 +00:00
|
|
|
header: filterKey{
|
|
|
|
typ: keyTyp,
|
|
|
|
str: key,
|
|
|
|
},
|
|
|
|
value: val,
|
|
|
|
op: op,
|
2020-09-17 09:20:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:39:41 +00:00
|
|
|
func (f *SearchFilters) AddFilter(header, value string, op SearchMatchType) {
|
|
|
|
f.addFilter(op, 0, header, staticStringer(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilters) addReservedFilter(op SearchMatchType, keyTyp filterKeyType, val fmt.Stringer) {
|
|
|
|
f.addFilter(op, keyTyp, "", val)
|
|
|
|
}
|
|
|
|
|
2020-11-09 12:41:40 +00:00
|
|
|
// addFlagFilters adds filters that works like flags: they don't need to have
|
|
|
|
// specific match type or value. They processed by NeoFS nodes by the fact
|
|
|
|
// of presence in search query. E.g.: PHY, ROOT.
|
|
|
|
func (f *SearchFilters) addFlagFilter(keyTyp filterKeyType) {
|
|
|
|
f.addFilter(MatchUnknown, keyTyp, "", staticStringer(""))
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:39:41 +00:00
|
|
|
func (f *SearchFilters) AddObjectVersionFilter(op SearchMatchType, v *pkg.Version) {
|
|
|
|
f.addReservedFilter(op, fKeyVersion, v)
|
|
|
|
}
|
|
|
|
|
2021-05-31 12:09:04 +00:00
|
|
|
func (f *SearchFilters) AddObjectContainerIDFilter(m SearchMatchType, id *cid.ID) {
|
2020-10-27 17:39:41 +00:00
|
|
|
f.addReservedFilter(m, fKeyContainerID, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilters) AddObjectOwnerIDFilter(m SearchMatchType, id *owner.ID) {
|
|
|
|
f.addReservedFilter(m, fKeyOwnerID, id)
|
|
|
|
}
|
|
|
|
|
2020-09-17 10:00:27 +00:00
|
|
|
func (f SearchFilters) ToV2() []*v2object.SearchFilter {
|
|
|
|
result := make([]*v2object.SearchFilter, 0, len(f))
|
2020-11-16 14:54:43 +00:00
|
|
|
|
2020-09-17 09:20:35 +00:00
|
|
|
for i := range f {
|
2020-09-17 10:00:27 +00:00
|
|
|
v2 := new(v2object.SearchFilter)
|
2020-10-27 17:39:41 +00:00
|
|
|
v2.SetKey(f[i].header.String())
|
|
|
|
v2.SetValue(f[i].value.String())
|
2020-09-17 09:20:35 +00:00
|
|
|
v2.SetMatchType(f[i].op.ToV2())
|
|
|
|
|
|
|
|
result = append(result, v2)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2020-09-30 15:54:07 +00:00
|
|
|
|
2020-11-09 12:41:40 +00:00
|
|
|
func (f *SearchFilters) addRootFilter() {
|
|
|
|
f.addFlagFilter(fKeyPropRoot)
|
2020-09-30 15:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilters) AddRootFilter() {
|
2020-11-09 12:41:40 +00:00
|
|
|
f.addRootFilter()
|
2020-09-30 15:54:07 +00:00
|
|
|
}
|
2020-09-30 15:57:39 +00:00
|
|
|
|
2020-11-09 12:41:40 +00:00
|
|
|
func (f *SearchFilters) addPhyFilter() {
|
|
|
|
f.addFlagFilter(fKeyPropPhy)
|
2020-09-30 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 12:29:26 +00:00
|
|
|
func (f *SearchFilters) AddPhyFilter() {
|
2020-11-09 12:41:40 +00:00
|
|
|
f.addPhyFilter()
|
2020-10-27 18:11:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 16:32:52 +00:00
|
|
|
// AddParentIDFilter adds filter by parent identifier.
|
2020-11-05 16:31:23 +00:00
|
|
|
func (f *SearchFilters) AddParentIDFilter(m SearchMatchType, id *ID) {
|
|
|
|
f.addReservedFilter(m, fKeyParent, id)
|
|
|
|
}
|
2020-11-20 06:55:15 +00:00
|
|
|
|
|
|
|
// AddObjectIDFilter adds filter by object identifier.
|
|
|
|
func (f *SearchFilters) AddObjectIDFilter(m SearchMatchType, id *ID) {
|
|
|
|
f.addReservedFilter(m, fKeyObjectID, id)
|
|
|
|
}
|
2020-11-25 16:32:52 +00:00
|
|
|
|
|
|
|
func (f *SearchFilters) AddSplitIDFilter(m SearchMatchType, id *SplitID) {
|
|
|
|
f.addReservedFilter(m, fKeySplitID, id)
|
|
|
|
}
|
2020-12-17 15:48:09 +00:00
|
|
|
|
|
|
|
// AddTypeFilter adds filter by object type.
|
|
|
|
func (f *SearchFilters) AddTypeFilter(m SearchMatchType, typ Type) {
|
|
|
|
f.addReservedFilter(m, fKeyType, typ)
|
|
|
|
}
|
2021-02-09 22:32:13 +00:00
|
|
|
|
|
|
|
// MarshalJSON encodes SearchFilters to protobuf JSON format.
|
|
|
|
func (f *SearchFilters) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(f.ToV2())
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes SearchFilters from protobuf JSON format.
|
|
|
|
func (f *SearchFilters) UnmarshalJSON(data []byte) error {
|
|
|
|
var fsV2 []*v2object.SearchFilter
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &fsV2); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*f = NewSearchFiltersFromV2(fsV2)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|