2020-09-17 09:20:35 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
2020-10-27 17:39:41 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
|
|
|
"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
|
|
|
|
)
|
|
|
|
|
|
|
|
func (m SearchMatchType) ToV2() v2object.MatchType {
|
|
|
|
switch m {
|
|
|
|
case MatchStringEqual:
|
|
|
|
return v2object.MatchStringEqual
|
|
|
|
default:
|
|
|
|
return v2object.MatchUnknown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SearchMatchFromV2(t v2object.MatchType) (m SearchMatchType) {
|
|
|
|
switch t {
|
|
|
|
case v2object.MatchStringEqual:
|
|
|
|
m = MatchStringEqual
|
|
|
|
default:
|
|
|
|
m = MatchUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-17 09:20:35 +00:00
|
|
|
type SearchFilters []SearchFilter
|
|
|
|
|
2020-10-27 17:39:41 +00:00
|
|
|
const (
|
|
|
|
_ filterKeyType = iota
|
|
|
|
fKeyVersion
|
|
|
|
fKeyContainerID
|
|
|
|
fKeyOwnerID
|
|
|
|
fKeyCreationEpoch
|
|
|
|
fKeyPayloadLength
|
|
|
|
fKeyPayloadHash
|
|
|
|
fKeyType
|
|
|
|
fKeyHomomorphicHash
|
|
|
|
fKeyParent
|
|
|
|
)
|
|
|
|
|
|
|
|
func (k filterKey) String() string {
|
|
|
|
switch k.typ {
|
|
|
|
default:
|
|
|
|
return k.str
|
|
|
|
case fKeyVersion:
|
|
|
|
return v2object.FilterHeaderVersion
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s staticStringer) String() string {
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilters) AddObjectVersionFilter(op SearchMatchType, v *pkg.Version) {
|
|
|
|
f.addReservedFilter(op, fKeyVersion, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilters) AddObjectContainerIDFilter(m SearchMatchType, id *container.ID) {
|
|
|
|
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-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
|
|
|
|
|
|
|
func (f *SearchFilters) addRootFilter(val string) {
|
|
|
|
f.AddFilter(KeyRoot, val, MatchStringEqual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilters) AddRootFilter() {
|
|
|
|
f.addRootFilter(ValRoot)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilters) AddNonRootFilter() {
|
|
|
|
f.addRootFilter(ValNonRoot)
|
|
|
|
}
|
2020-09-30 15:57:39 +00:00
|
|
|
|
|
|
|
func (f *SearchFilters) addLeafFilter(val string) {
|
|
|
|
f.AddFilter(KeyLeaf, val, MatchStringEqual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilters) AddLeafFilter() {
|
|
|
|
f.addLeafFilter(ValLeaf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilters) AddNonLeafFilter() {
|
|
|
|
f.addLeafFilter(ValNonLeaf)
|
|
|
|
}
|