Skip to content

Commit

Permalink
object: Support numerical matchers in search queries (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-khimov authored Feb 20, 2024
2 parents 18171f2 + e3b565d commit 24254bf
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/mr-tron/base58 v1.2.0
github.com/nspcc-dev/hrw/v2 v2.0.0-20231115095647-bf62f4ad0a43
github.com/nspcc-dev/neo-go v0.102.0
github.com/nspcc-dev/neofs-api-go/v2 v2.14.1-0.20240125143754-70b1ffbd8141
github.com/nspcc-dev/neofs-api-go/v2 v2.14.1-0.20240213170208-cfca09b5acbe
github.com/nspcc-dev/tzhash v1.7.1
github.com/stretchr/testify v1.8.4
github.com/testcontainers/testcontainers-go v0.24.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ github.com/nspcc-dev/hrw/v2 v2.0.0-20231115095647-bf62f4ad0a43 h1:zXkCRGTHqhkBRJ
github.com/nspcc-dev/hrw/v2 v2.0.0-20231115095647-bf62f4ad0a43/go.mod h1:BGU4YsuoFXjQddsCfUXpq5uNr2A8W4PrWbiljdD/TpU=
github.com/nspcc-dev/neo-go v0.102.0 h1:O2Gt4JPOWmp0c+PnPWwd2wPI74BKSwkaNCEyvyQTWJw=
github.com/nspcc-dev/neo-go v0.102.0/go.mod h1:QXxpZxJT2KedwM0Nlj8UO0/fZN2WIe4h/i03uBHKbnc=
github.com/nspcc-dev/neofs-api-go/v2 v2.14.1-0.20240125143754-70b1ffbd8141 h1:rf8Zbn8tWBhAmIYPZeMwtyrZkUrOlUhlJ9W4LzAvRk4=
github.com/nspcc-dev/neofs-api-go/v2 v2.14.1-0.20240125143754-70b1ffbd8141/go.mod h1:eaffSBIGhXUIMYvRBGXmlgQRLyyCWlzOft9jGYlqwrw=
github.com/nspcc-dev/neofs-api-go/v2 v2.14.1-0.20240213170208-cfca09b5acbe h1:Hoq88+PWS6tNnX4Y0jxE0C8wvxPI8UlVnCs2ZJDEy4Y=
github.com/nspcc-dev/neofs-api-go/v2 v2.14.1-0.20240213170208-cfca09b5acbe/go.mod h1:eaffSBIGhXUIMYvRBGXmlgQRLyyCWlzOft9jGYlqwrw=
github.com/nspcc-dev/neofs-crypto v0.4.0 h1:5LlrUAM5O0k1+sH/sktBtrgfWtq1pgpDs09fZo+KYi4=
github.com/nspcc-dev/neofs-crypto v0.4.0/go.mod h1:6XJ8kbXgOfevbI2WMruOtI+qUJXNwSGM/E9eClXxPHs=
github.com/nspcc-dev/rfc6979 v0.2.0 h1:3e1WNxrN60/6N0DW7+UYisLeZJyfqZTNOjeV/toYvOE=
Expand Down
70 changes: 50 additions & 20 deletions object/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,46 @@ const (
MatchStringNotEqual
MatchNotPresent
MatchCommonPrefix
MatchNumGT
MatchNumGE
MatchNumLT
MatchNumLE
)

// ToV2 converts [SearchMatchType] to v2 [v2object.MatchType] enum value.
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
case
MatchStringEqual,
MatchStringNotEqual,
MatchNotPresent,
MatchCommonPrefix,
MatchNumGT,
MatchNumGE,
MatchNumLT,
MatchNumLE:
return v2object.MatchType(m)
default:
return v2object.MatchUnknown
}
}

// SearchMatchFromV2 converts v2 [v2object.MatchType] to [SearchMatchType] enum value.
func SearchMatchFromV2(t v2object.MatchType) (m SearchMatchType) {
func SearchMatchFromV2(t v2object.MatchType) SearchMatchType {
switch t {
case v2object.MatchStringEqual:
m = MatchStringEqual
case v2object.MatchStringNotEqual:
m = MatchStringNotEqual
case v2object.MatchNotPresent:
m = MatchNotPresent
case v2object.MatchCommonPrefix:
m = MatchCommonPrefix
case
v2object.MatchStringEqual,
v2object.MatchStringNotEqual,
v2object.MatchNotPresent,
v2object.MatchCommonPrefix,
v2object.MatchNumGT,
v2object.MatchNumGE,
v2object.MatchNumLT,
v2object.MatchNumLE:
return SearchMatchType(t)
default:
m = MatchUnknown
return MatchUnknown
}

return m
}

// EncodeToString returns string representation of [SearchMatchType].
Expand All @@ -67,6 +73,10 @@ func SearchMatchFromV2(t v2object.MatchType) (m SearchMatchType) {
// - [MatchStringNotEqual]: STRING_NOT_EQUAL;
// - [MatchNotPresent]: NOT_PRESENT;
// - [MatchCommonPrefix]: COMMON_PREFIX;
// - [MatchNumGT], default: NUM_GT;
// - [MatchNumGE], default: NUM_GE;
// - [MatchNumLT], default: NUM_LT;
// - [MatchNumLE], default: NUM_LE;
// - [MatchUnknown], default: MATCH_TYPE_UNSPECIFIED.
func (m SearchMatchType) EncodeToString() string {
return m.ToV2().String()
Expand Down Expand Up @@ -200,6 +210,8 @@ func (f *SearchFilters) addFilter(op SearchMatchType, key string, val stringEnco
}

// AddFilter adds a filter to group by simple plain parameters.
//
// If op is numeric (like [MatchNumGT]), value must be a base-10 integer.
func (f *SearchFilters) AddFilter(key, value string, op SearchMatchType) {
f.addFilter(op, key, staticStringer(value))
}
Expand All @@ -212,16 +224,22 @@ func (f *SearchFilters) addFlagFilter(key string) {
}

// AddObjectVersionFilter adds a filter by version.
//
// The op must not be numeric (like [MatchNumGT]).
func (f *SearchFilters) AddObjectVersionFilter(op SearchMatchType, v version.Version) {
f.addFilter(op, FilterVersion, staticStringer(version.EncodeToString(v)))
}

// AddObjectContainerIDFilter adds a filter by container id.
//
// The m must not be numeric (like [MatchNumGT]).
func (f *SearchFilters) AddObjectContainerIDFilter(m SearchMatchType, id cid.ID) {
f.addFilter(m, FilterContainerID, id)
}

// AddObjectOwnerIDFilter adds a filter by object owner id.
//
// The m must not be numeric (like [MatchNumGT]).
func (f *SearchFilters) AddObjectOwnerIDFilter(m SearchMatchType, id user.ID) {
f.addFilter(m, FilterOwnerID, id)
}
Expand Down Expand Up @@ -263,21 +281,29 @@ func (f *SearchFilters) AddPhyFilter() {
}

// AddParentIDFilter adds filter by parent identifier.
//
// The m must not be numeric (like [MatchNumGT]).
func (f *SearchFilters) AddParentIDFilter(m SearchMatchType, id oid.ID) {
f.addFilter(m, FilterParentID, id)
}

// AddObjectIDFilter adds filter by object identifier.
//
// The m must not be numeric (like [MatchNumGT]).
func (f *SearchFilters) AddObjectIDFilter(m SearchMatchType, id oid.ID) {
f.addFilter(m, FilterID, id)
}

// AddSplitIDFilter adds filter by split ID.
//
// The m must not be numeric (like [MatchNumGT]).
func (f *SearchFilters) AddSplitIDFilter(m SearchMatchType, id SplitID) {
f.addFilter(m, FilterSplitID, staticStringer(id.String()))
}

// AddTypeFilter adds filter by object type.
//
// The m must not be numeric (like [MatchNumGT]).
func (f *SearchFilters) AddTypeFilter(m SearchMatchType, typ Type) {
f.addFilter(m, FilterType, staticStringer(typ.EncodeToString()))
}
Expand Down Expand Up @@ -305,11 +331,15 @@ func (f *SearchFilters) UnmarshalJSON(data []byte) error {
}

// AddPayloadHashFilter adds filter by payload hash.
//
// The m must not be numeric (like [MatchNumGT]).
func (f *SearchFilters) AddPayloadHashFilter(m SearchMatchType, sum [sha256.Size]byte) {
f.addFilter(m, FilterPayloadChecksum, staticStringer(hex.EncodeToString(sum[:])))
}

// AddHomomorphicHashFilter adds filter by homomorphic hash.
//
// The m must not be numeric (like [MatchNumGT]).
func (f *SearchFilters) AddHomomorphicHashFilter(m SearchMatchType, sum [tz.Size]byte) {
f.addFilter(m, FilterPayloadHomomorphicHash, staticStringer(hex.EncodeToString(sum[:])))
}
Expand Down
8 changes: 8 additions & 0 deletions object/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ var eqV2Matches = map[object.SearchMatchType]v2object.MatchType{
object.MatchStringNotEqual: v2object.MatchStringNotEqual,
object.MatchNotPresent: v2object.MatchNotPresent,
object.MatchCommonPrefix: v2object.MatchCommonPrefix,
object.MatchNumGT: v2object.MatchNumGT,
object.MatchNumGE: v2object.MatchNumGE,
object.MatchNumLT: v2object.MatchNumLT,
object.MatchNumLE: v2object.MatchNumLE,
}

func TestMatch(t *testing.T) {
Expand Down Expand Up @@ -265,6 +269,10 @@ func TestSearchMatchType_String(t *testing.T) {
{val: toPtr(object.MatchStringNotEqual), str: "STRING_NOT_EQUAL"},
{val: toPtr(object.MatchNotPresent), str: "NOT_PRESENT"},
{val: toPtr(object.MatchUnknown), str: "MATCH_TYPE_UNSPECIFIED"},
{val: toPtr(object.MatchNumGT), str: "NUM_GT"},
{val: toPtr(object.MatchNumGE), str: "NUM_GE"},
{val: toPtr(object.MatchNumLT), str: "NUM_LT"},
{val: toPtr(object.MatchNumLE), str: "NUM_LE"},
})
}

Expand Down

0 comments on commit 24254bf

Please sign in to comment.