frostfs-node/pkg/local_object_storage/metabase/select_test.go
Leonard Lyubich 7a8f322d59 [#142] metabase: Fix false-positive select
Fix a bug in the selection when an object could be added to the result after
a mismatch in the previous filter.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2020-11-03 18:42:32 +03:00

113 lines
2 KiB
Go

package meta
import (
"crypto/rand"
"testing"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/stretchr/testify/require"
)
func addNFilters(fs *objectSDK.SearchFilters, n int) {
for i := 0; i < n; i++ {
key := make([]byte, 32)
rand.Read(key)
val := make([]byte, 32)
rand.Read(val)
fs.AddFilter(string(key), string(val), objectSDK.MatchStringEqual)
}
}
func BenchmarkDB_Select(b *testing.B) {
db := newDB(b)
defer releaseDB(db)
for i := 0; i < 100; i++ {
obj := generateObject(b, testPrm{
withParent: true,
attrNum: 100,
})
require.NoError(b, db.Put(obj))
}
for _, item := range []struct {
name string
filters func(*objectSDK.SearchFilters)
}{
{
name: "empty",
filters: func(*objectSDK.SearchFilters) {
return
},
},
{
name: "1 filter",
filters: func(fs *objectSDK.SearchFilters) {
addNFilters(fs, 1)
},
},
{
name: "10 filters",
filters: func(fs *objectSDK.SearchFilters) {
addNFilters(fs, 10)
},
},
{
name: "100 filters",
filters: func(fs *objectSDK.SearchFilters) {
addNFilters(fs, 100)
},
},
{
name: "1000 filters",
filters: func(fs *objectSDK.SearchFilters) {
addNFilters(fs, 1000)
},
},
} {
b.Run(item.name, func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
fs := new(objectSDK.SearchFilters)
item.filters(fs)
b.StartTimer()
_, err := db.Select(*fs)
b.StopTimer()
require.NoError(b, err)
b.StartTimer()
}
})
}
}
func TestMismatchAfterMatch(t *testing.T) {
db := newDB(t)
defer releaseDB(db)
obj := generateObject(t, testPrm{
attrNum: 1,
})
require.NoError(t, db.Put(obj))
a := obj.GetAttributes()[0]
fs := objectSDK.SearchFilters{}
// 1st - mismatching filter
fs.AddFilter(a.GetKey(), a.GetValue()+"1", objectSDK.MatchStringEqual)
// 2nd - matching filter
fs.AddFilter(a.GetKey(), a.GetValue(), objectSDK.MatchStringEqual)
testSelect(t, db, fs)
}