[#160] Remove query match function

This function duplicates query processing that
is done in meta-storage now.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-11-10 15:49:40 +03:00 committed by Alex Vanin
parent 03fed8ca59
commit 32219bb294
3 changed files with 0 additions and 171 deletions

View file

@ -2,10 +2,8 @@ package query
import ( import (
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-node/pkg/core/object"
) )
type Query interface { type Query interface {
ToSearchFilters() objectSDK.SearchFilters ToSearchFilters() objectSDK.SearchFilters
Match(*object.Object, func(*objectSDK.ID))
} }

View file

@ -4,8 +4,6 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg/container" "github.com/nspcc-dev/neofs-api-go/pkg/container"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/owner" "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" "github.com/nspcc-dev/neofs-node/pkg/services/object/search/query"
) )
@ -31,55 +29,6 @@ func ownerIDValue(id *owner.ID) string {
return 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 { func (q *Query) ToSearchFilters() objectSDK.SearchFilters {
return q.filters return q.filters
} }

View file

@ -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()))
})
}