2021-11-08 10:04:45 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-02-16 14:48:53 +00:00
|
|
|
"strconv"
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
v2object "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/version"
|
2021-11-08 10:04:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SearchMatchType indicates match operation on specified header.
|
|
|
|
type SearchMatchType uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
MatchUnknown SearchMatchType = iota
|
|
|
|
MatchStringEqual
|
|
|
|
MatchStringNotEqual
|
|
|
|
MatchNotPresent
|
|
|
|
MatchCommonPrefix
|
|
|
|
)
|
|
|
|
|
|
|
|
func (m SearchMatchType) ToV2() v2object.MatchType {
|
|
|
|
switch m {
|
|
|
|
case MatchStringEqual:
|
|
|
|
return v2object.MatchStringEqual
|
|
|
|
case MatchStringNotEqual:
|
|
|
|
return v2object.MatchStringNotEqual
|
|
|
|
case MatchNotPresent:
|
|
|
|
return v2object.MatchNotPresent
|
|
|
|
case MatchCommonPrefix:
|
|
|
|
return v2object.MatchCommonPrefix
|
|
|
|
default:
|
|
|
|
return v2object.MatchUnknown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SearchMatchFromV2(t v2object.MatchType) (m SearchMatchType) {
|
|
|
|
switch t {
|
|
|
|
case v2object.MatchStringEqual:
|
|
|
|
m = MatchStringEqual
|
|
|
|
case v2object.MatchStringNotEqual:
|
|
|
|
m = MatchStringNotEqual
|
|
|
|
case v2object.MatchNotPresent:
|
|
|
|
m = MatchNotPresent
|
|
|
|
case v2object.MatchCommonPrefix:
|
|
|
|
m = MatchCommonPrefix
|
|
|
|
default:
|
|
|
|
m = MatchUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns string representation of SearchMatchType.
|
|
|
|
//
|
|
|
|
// String mapping:
|
2022-08-24 14:17:40 +00:00
|
|
|
// - MatchStringEqual: STRING_EQUAL;
|
|
|
|
// - MatchStringNotEqual: STRING_NOT_EQUAL;
|
|
|
|
// - MatchNotPresent: NOT_PRESENT;
|
|
|
|
// - MatchCommonPrefix: COMMON_PREFIX;
|
|
|
|
// - MatchUnknown, default: MATCH_TYPE_UNSPECIFIED.
|
2021-11-08 10:04:45 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-31 06:55:08 +00:00
|
|
|
type stringEncoder interface {
|
|
|
|
EncodeToString() string
|
|
|
|
}
|
|
|
|
|
2021-11-08 10:04:45 +00:00
|
|
|
type SearchFilter struct {
|
|
|
|
header filterKey
|
2022-05-31 06:55:08 +00:00
|
|
|
value stringEncoder
|
2021-11-08 10:04:45 +00:00
|
|
|
op SearchMatchType
|
|
|
|
}
|
|
|
|
|
|
|
|
type staticStringer string
|
|
|
|
|
|
|
|
type filterKey struct {
|
|
|
|
typ filterKeyType
|
|
|
|
|
|
|
|
str string
|
|
|
|
}
|
|
|
|
|
|
|
|
// enumeration of reserved filter keys.
|
|
|
|
type filterKeyType int
|
|
|
|
|
|
|
|
type SearchFilters []SearchFilter
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ filterKeyType = iota
|
|
|
|
fKeyVersion
|
|
|
|
fKeyObjectID
|
|
|
|
fKeyContainerID
|
|
|
|
fKeyOwnerID
|
|
|
|
fKeyCreationEpoch
|
|
|
|
fKeyPayloadLength
|
|
|
|
fKeyPayloadHash
|
|
|
|
fKeyType
|
|
|
|
fKeyHomomorphicHash
|
|
|
|
fKeyParent
|
|
|
|
fKeySplitID
|
|
|
|
fKeyPropRoot
|
|
|
|
fKeyPropPhy
|
|
|
|
)
|
|
|
|
|
|
|
|
func (k filterKey) String() string {
|
|
|
|
switch k.typ {
|
|
|
|
default:
|
|
|
|
return k.str
|
|
|
|
case fKeyVersion:
|
|
|
|
return v2object.FilterHeaderVersion
|
|
|
|
case fKeyObjectID:
|
|
|
|
return v2object.FilterHeaderObjectID
|
|
|
|
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
|
|
|
|
case fKeySplitID:
|
|
|
|
return v2object.FilterHeaderSplitID
|
|
|
|
case fKeyPropRoot:
|
|
|
|
return v2object.FilterPropertyRoot
|
|
|
|
case fKeyPropPhy:
|
|
|
|
return v2object.FilterPropertyPhy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 06:55:08 +00:00
|
|
|
func (s staticStringer) EncodeToString() string {
|
2021-11-08 10:04:45 +00:00
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilter) Header() string {
|
|
|
|
return f.header.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilter) Value() string {
|
2022-05-31 06:55:08 +00:00
|
|
|
return f.value.EncodeToString()
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilter) Operation() SearchMatchType {
|
|
|
|
return f.op
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSearchFilters() SearchFilters {
|
|
|
|
return SearchFilters{}
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:00:54 +00:00
|
|
|
func NewSearchFiltersFromV2(v2 []v2object.SearchFilter) SearchFilters {
|
2021-11-08 10:04:45 +00:00
|
|
|
filters := make(SearchFilters, 0, len(v2))
|
|
|
|
|
|
|
|
for i := range v2 {
|
|
|
|
filters.AddFilter(
|
|
|
|
v2[i].GetKey(),
|
|
|
|
v2[i].GetValue(),
|
|
|
|
SearchMatchFromV2(v2[i].GetMatchType()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return filters
|
|
|
|
}
|
|
|
|
|
2022-05-31 06:55:08 +00:00
|
|
|
func (f *SearchFilters) addFilter(op SearchMatchType, keyTyp filterKeyType, key string, val stringEncoder) {
|
2021-11-08 10:04:45 +00:00
|
|
|
if *f == nil {
|
|
|
|
*f = make(SearchFilters, 0, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
*f = append(*f, SearchFilter{
|
|
|
|
header: filterKey{
|
|
|
|
typ: keyTyp,
|
|
|
|
str: key,
|
|
|
|
},
|
|
|
|
value: val,
|
|
|
|
op: op,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilters) AddFilter(header, value string, op SearchMatchType) {
|
|
|
|
f.addFilter(op, 0, header, staticStringer(value))
|
|
|
|
}
|
|
|
|
|
2022-05-31 06:55:08 +00:00
|
|
|
func (f *SearchFilters) addReservedFilter(op SearchMatchType, keyTyp filterKeyType, val stringEncoder) {
|
2021-11-08 10:04:45 +00:00
|
|
|
f.addFilter(op, keyTyp, "", val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// addFlagFilters adds filters that works like flags: they don't need to have
|
2022-12-29 10:46:18 +00:00
|
|
|
// specific match type or value. They processed by FrostFS nodes by the fact
|
2021-11-08 10:04:45 +00:00
|
|
|
// of presence in search query. E.g.: PHY, ROOT.
|
|
|
|
func (f *SearchFilters) addFlagFilter(keyTyp filterKeyType) {
|
|
|
|
f.addFilter(MatchUnknown, keyTyp, "", staticStringer(""))
|
|
|
|
}
|
|
|
|
|
2022-05-31 06:55:08 +00:00
|
|
|
func (f *SearchFilters) AddObjectVersionFilter(op SearchMatchType, v version.Version) {
|
|
|
|
f.addReservedFilter(op, fKeyVersion, staticStringer(version.EncodeToString(v)))
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
func (f *SearchFilters) AddObjectContainerIDFilter(m SearchMatchType, id cid.ID) {
|
2021-11-08 10:04:45 +00:00
|
|
|
f.addReservedFilter(m, fKeyContainerID, id)
|
|
|
|
}
|
|
|
|
|
2022-05-31 06:55:08 +00:00
|
|
|
func (f *SearchFilters) AddObjectOwnerIDFilter(m SearchMatchType, id user.ID) {
|
2021-11-08 10:04:45 +00:00
|
|
|
f.addReservedFilter(m, fKeyOwnerID, id)
|
|
|
|
}
|
|
|
|
|
2022-02-16 14:48:53 +00:00
|
|
|
func (f *SearchFilters) AddNotificationEpochFilter(epoch uint64) {
|
|
|
|
f.addFilter(MatchStringEqual, 0, v2object.SysAttributeTickEpoch, staticStringer(strconv.FormatUint(epoch, 10)))
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:00:54 +00:00
|
|
|
func (f SearchFilters) ToV2() []v2object.SearchFilter {
|
|
|
|
result := make([]v2object.SearchFilter, len(f))
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
for i := range f {
|
2022-03-11 09:00:54 +00:00
|
|
|
result[i].SetKey(f[i].header.String())
|
2022-05-31 06:55:08 +00:00
|
|
|
result[i].SetValue(f[i].value.EncodeToString())
|
2022-03-11 09:00:54 +00:00
|
|
|
result[i].SetMatchType(f[i].op.ToV2())
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilters) addRootFilter() {
|
|
|
|
f.addFlagFilter(fKeyPropRoot)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilters) AddRootFilter() {
|
|
|
|
f.addRootFilter()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilters) addPhyFilter() {
|
|
|
|
f.addFlagFilter(fKeyPropPhy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilters) AddPhyFilter() {
|
|
|
|
f.addPhyFilter()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddParentIDFilter adds filter by parent identifier.
|
2022-04-11 16:25:14 +00:00
|
|
|
func (f *SearchFilters) AddParentIDFilter(m SearchMatchType, id oid.ID) {
|
2021-11-08 10:04:45 +00:00
|
|
|
f.addReservedFilter(m, fKeyParent, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddObjectIDFilter adds filter by object identifier.
|
2022-04-11 16:25:14 +00:00
|
|
|
func (f *SearchFilters) AddObjectIDFilter(m SearchMatchType, id oid.ID) {
|
2021-11-08 10:04:45 +00:00
|
|
|
f.addReservedFilter(m, fKeyObjectID, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SearchFilters) AddSplitIDFilter(m SearchMatchType, id *SplitID) {
|
2022-05-31 06:55:08 +00:00
|
|
|
f.addReservedFilter(m, fKeySplitID, staticStringer(id.String()))
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddTypeFilter adds filter by object type.
|
|
|
|
func (f *SearchFilters) AddTypeFilter(m SearchMatchType, typ Type) {
|
2022-05-31 06:55:08 +00:00
|
|
|
f.addReservedFilter(m, fKeyType, staticStringer(typ.String()))
|
2021-11-08 10:04:45 +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 {
|
2022-03-11 09:00:54 +00:00
|
|
|
var fsV2 []v2object.SearchFilter
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &fsV2); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*f = NewSearchFiltersFromV2(fsV2)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|