Skip to content

Commit

Permalink
object: Add method to detect non-attribute filter
Browse files Browse the repository at this point in the history
Object filters may be attributed and non-attribute (for system needs).
The system filter can affect the logic of the application, so it should
be possible to check for its presence.

Add `HasNonAttributeFilter` method to `SearchFilters` that returns true
iff the instance contains non-attribute filter.

Refs #226.

Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
  • Loading branch information
cthulhu-rider committed Aug 31, 2023
1 parent f832bb5 commit 5468fbe
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
13 changes: 13 additions & 0 deletions object/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/json"
"strconv"
"strings"

v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
Expand Down Expand Up @@ -316,3 +317,15 @@ func (f *SearchFilters) AddCreationEpochFilter(m SearchMatchType, epoch uint64)
func (f *SearchFilters) AddPayloadSizeFilter(m SearchMatchType, size uint64) {
f.addFilter(m, FilterPayloadSize, staticStringer(strconv.FormatUint(size, 10)))
}

// HasNonAttributeFilter checks if SearchFilters instance contains a
// non-attribute filter.
func (f SearchFilters) HasNonAttributeFilter() bool {
for i := range f {
if strings.HasPrefix(f[i].header, v2object.ReservedFilterPrefix) {
return true
}
}

return false
}
36 changes: 36 additions & 0 deletions object/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import (

v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/nspcc-dev/neofs-sdk-go/checksum"
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
"github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
objecttest "github.com/nspcc-dev/neofs-sdk-go/object/test"
usertest "github.com/nspcc-dev/neofs-sdk-go/user/test"
versiontest "github.com/nspcc-dev/neofs-sdk-go/version/test"
"github.com/nspcc-dev/tzhash/tz"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -398,3 +403,34 @@ func TestSearchFilters_AddPayloadSizeFilter(t *testing.T) {
require.Equal(t, v2object.MatchStringEqual, fsV2[0].GetMatchType())
})
}

func TestSearchFilters_HasNonAttributeFilter(t *testing.T) {
const anyMatcher = object.MatchStringEqual
fs := new(object.SearchFilters)

require.False(t, fs.HasNonAttributeFilter())

fs.AddFilter("key", "value", anyMatcher)
require.False(t, fs.HasNonAttributeFilter())

for _, f := range []func(){
func() { fs.AddFilter("$Object:any", "", anyMatcher) },
func() { fs.AddObjectVersionFilter(anyMatcher, versiontest.Version()) },
func() { fs.AddParentIDFilter(anyMatcher, oidtest.ID()) },
func() { fs.AddObjectContainerIDFilter(anyMatcher, cidtest.ID()) },
func() { fs.AddObjectOwnerIDFilter(anyMatcher, *usertest.ID(t)) },
func() { fs.AddCreationEpochFilter(anyMatcher, rand.Uint64()) },
func() { fs.AddPayloadSizeFilter(anyMatcher, rand.Uint64()) },
func() { fs.AddPayloadHashFilter(anyMatcher, [sha256.Size]byte{1}) },
func() { fs.AddTypeFilter(anyMatcher, object.TypeTombstone) },
func() { fs.AddHomomorphicHashFilter(anyMatcher, [tz.Size]byte{1}) },
func() { fs.AddParentIDFilter(anyMatcher, oidtest.ID()) },
func() { fs.AddSplitIDFilter(anyMatcher, *objecttest.SplitID()) },
func() { fs.AddRootFilter() },
func() { fs.AddPhyFilter() },
} {
*fs = (*fs)[:0]
f()
require.True(t, fs.HasNonAttributeFilter())
}
}

0 comments on commit 5468fbe

Please sign in to comment.