From 32219bb294be6590103b5845507e5baf7bc3c170 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 10 Nov 2020 15:49:40 +0300 Subject: [PATCH] [#160] Remove query match function This function duplicates query processing that is done in meta-storage now. Signed-off-by: Alex Vanin --- pkg/services/object/search/query/query.go | 2 - pkg/services/object/search/query/v1/v1.go | 51 -------- .../object/search/query/v1/v1_test.go | 118 ------------------ 3 files changed, 171 deletions(-) delete mode 100644 pkg/services/object/search/query/v1/v1_test.go diff --git a/pkg/services/object/search/query/query.go b/pkg/services/object/search/query/query.go index 1efebd55c4..7c8dcc7ea9 100644 --- a/pkg/services/object/search/query/query.go +++ b/pkg/services/object/search/query/query.go @@ -2,10 +2,8 @@ package query import ( objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" - "github.com/nspcc-dev/neofs-node/pkg/core/object" ) type Query interface { ToSearchFilters() objectSDK.SearchFilters - Match(*object.Object, func(*objectSDK.ID)) } diff --git a/pkg/services/object/search/query/v1/v1.go b/pkg/services/object/search/query/v1/v1.go index cd136e0614..b045986a95 100644 --- a/pkg/services/object/search/query/v1/v1.go +++ b/pkg/services/object/search/query/v1/v1.go @@ -4,8 +4,6 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg/container" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/pkg/owner" - v2object "github.com/nspcc-dev/neofs-api-go/v2/object" - "github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/services/object/search/query" ) @@ -31,55 +29,6 @@ func ownerIDValue(id *owner.ID) string { return id.String() } -func (q *Query) Match(obj *object.Object, handler func(*objectSDK.ID)) { - for par := (*object.Object)(nil); obj != nil; par, obj = obj, obj.GetParent() { - match := true - - for i := 0; match && i < len(q.filters); i++ { - switch typ := q.filters[i].Operation(); typ { - default: - match = false - case objectSDK.MatchStringEqual: - switch key := q.filters[i].Header(); key { - default: - match = headerEqual(obj, key, q.filters[i].Value()) - case v2object.FilterPropertyRoot: - match = (q.filters[i].Value() == v2object.BooleanPropertyValueTrue) == (!obj.HasParent() && - obj.GetType() == objectSDK.TypeRegular) - case v2object.FilterPropertyLeaf: - match = (q.filters[i].Value() == v2object.BooleanPropertyValueTrue) == (par == nil) - } - } - } - - if match { - handler(obj.GetID()) - } - } -} - -func headerEqual(obj *object.Object, key, value string) bool { - switch key { - default: - for _, attr := range obj.GetAttributes() { - if attr.GetKey() == key && attr.GetValue() == value { - return true - } - } - - return false - case v2object.FilterHeaderContainerID: - return value == cidValue(obj.GetContainerID()) - case v2object.FilterHeaderOwnerID: - return value == ownerIDValue(obj.GetOwnerID()) - case v2object.FilterPropertyChildfree: - return (value == v2object.BooleanPropertyValueTrue) == (len(obj.GetChildren()) == 0) - case v2object.FilterHeaderParent: - return idValue(obj.GetParentID()) == value - // TODO: add other headers - } -} - func (q *Query) ToSearchFilters() objectSDK.SearchFilters { return q.filters } diff --git a/pkg/services/object/search/query/v1/v1_test.go b/pkg/services/object/search/query/v1/v1_test.go deleted file mode 100644 index ab3c4d5f20..0000000000 --- a/pkg/services/object/search/query/v1/v1_test.go +++ /dev/null @@ -1,118 +0,0 @@ -package query - -import ( - "crypto/rand" - "crypto/sha256" - "testing" - - "github.com/nspcc-dev/neofs-api-go/pkg/container" - objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" - "github.com/nspcc-dev/neofs-api-go/pkg/owner" - "github.com/nspcc-dev/neofs-node/pkg/core/object" - "github.com/nspcc-dev/neofs-node/pkg/services/object/search/query" - "github.com/stretchr/testify/require" -) - -func testID(t *testing.T) *objectSDK.ID { - cs := [sha256.Size]byte{} - - _, err := rand.Read(cs[:]) - require.NoError(t, err) - - id := objectSDK.NewID() - id.SetSHA256(cs) - - return id -} - -func testCID(t *testing.T) *container.ID { - cs := [sha256.Size]byte{} - - _, err := rand.Read(cs[:]) - require.NoError(t, err) - - id := container.NewID() - id.SetSHA256(cs) - - return id -} - -func testOwnerID(t *testing.T) *owner.ID { - w := new(owner.NEO3Wallet) - - _, err := rand.Read(w.Bytes()) - require.NoError(t, err) - - id := owner.NewID() - id.SetNeo3Wallet(w) - - return id -} - -func matchesQuery(q query.Query, obj *object.Object) (res bool) { - q.Match(obj, func(id *objectSDK.ID) { - res = id.Equal(obj.GetID()) - }) - - return -} - -func TestQ_Match(t *testing.T) { - t.Run("container identifier equal", func(t *testing.T) { - obj := object.NewRaw() - - id := testCID(t) - obj.SetContainerID(id) - - fs := objectSDK.SearchFilters{} - fs.AddObjectContainerIDFilter(objectSDK.MatchStringEqual, id) - - q := New(fs) - - require.True(t, matchesQuery(q, obj.Object())) - - obj.SetContainerID(testCID(t)) - - require.False(t, matchesQuery(q, obj.Object())) - }) - - t.Run("owner identifier equal", func(t *testing.T) { - obj := object.NewRaw() - - id := testOwnerID(t) - obj.SetOwnerID(id) - - fs := objectSDK.SearchFilters{} - fs.AddObjectOwnerIDFilter(objectSDK.MatchStringEqual, id) - - q := New(fs) - - require.True(t, matchesQuery(q, obj.Object())) - - obj.SetOwnerID(testOwnerID(t)) - - require.False(t, matchesQuery(q, obj.Object())) - }) - - t.Run("attribute equal", func(t *testing.T) { - obj := object.NewRaw() - - k, v := "key", "val" - a := new(objectSDK.Attribute) - a.SetKey(k) - a.SetValue(v) - - obj.SetAttributes(a) - - fs := objectSDK.SearchFilters{} - fs.AddFilter(k, v, objectSDK.MatchStringEqual) - - q := New(fs) - - require.True(t, matchesQuery(q, obj.Object())) - - a.SetKey(k + "1") - - require.False(t, matchesQuery(q, obj.Object())) - }) -}