Skip to content

Commit

Permalink
object: Support filters by creation epoch and payload size
Browse files Browse the repository at this point in the history
Export constants and `SearchFilters` methods related to creation epoch
and payload size.

Refs #226.

Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
  • Loading branch information
cthulhu-rider committed Sep 5, 2023
1 parent 793c8cd commit 88902f6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
12 changes: 12 additions & 0 deletions object/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ const (
FilterPayloadHomomorphicHash = v2object.FilterHeaderHomomorphicHash
FilterParentID = v2object.FilterHeaderParent
FilterSplitID = v2object.FilterHeaderSplitID
FilterCreationEpoch = v2object.FilterHeaderCreationEpoch
FilterPayloadSize = v2object.FilterHeaderPayloadLength
)

// Various filters to match certain object properties.
Expand Down Expand Up @@ -304,3 +306,13 @@ func (f *SearchFilters) AddPayloadHashFilter(m SearchMatchType, sum [sha256.Size
func (f *SearchFilters) AddHomomorphicHashFilter(m SearchMatchType, sum [tz.Size]byte) {
f.addFilter(m, FilterPayloadHomomorphicHash, staticStringer(hex.EncodeToString(sum[:])))
}

// AddCreationEpochFilter adds filter by creation epoch.
func (f *SearchFilters) AddCreationEpochFilter(m SearchMatchType, epoch uint64) {
f.addFilter(m, FilterCreationEpoch, staticStringer(strconv.FormatUint(epoch, 10)))
}

// AddPayloadSizeFilter adds filter by payload size.
func (f *SearchFilters) AddPayloadSizeFilter(m SearchMatchType, size uint64) {
f.addFilter(m, FilterPayloadSize, staticStringer(strconv.FormatUint(size, 10)))
}
51 changes: 51 additions & 0 deletions object/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"fmt"
"math/rand"
"strconv"
"testing"

v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
Expand Down Expand Up @@ -347,3 +348,53 @@ func ExampleSearchFilters_AddHomomorphicHashFilter() {
fmt.Println(hex.EncodeToString(cs.Value()))
// Output: 7e302ebb3937e810feb501965580c746048db99cebd095c3ce27022407408bf904dde8d9aa8085d2cf7202345341cc947fa9d722c6b6699760d307f653815d0c
}

func TestSearchFilters_AddCreationEpochFilter(t *testing.T) {
epoch := rand.Uint64()

fs := new(object.SearchFilters)
fs.AddCreationEpochFilter(object.MatchStringEqual, epoch)

require.Len(t, *fs, 1)

f := (*fs)[0]

require.Equal(t, object.FilterCreationEpoch, f.Header())
require.Equal(t, strconv.FormatUint(epoch, 10), f.Value())
require.Equal(t, object.MatchStringEqual, f.Operation())

t.Run("v2", func(t *testing.T) {
fsV2 := fs.ToV2()

require.Len(t, fsV2, 1)

require.Equal(t, v2object.FilterHeaderCreationEpoch, fsV2[0].GetKey())
require.Equal(t, strconv.FormatUint(epoch, 10), fsV2[0].GetValue())
require.Equal(t, v2object.MatchStringEqual, fsV2[0].GetMatchType())
})
}

func TestSearchFilters_AddPayloadSizeFilter(t *testing.T) {
size := rand.Uint64()

fs := new(object.SearchFilters)
fs.AddPayloadSizeFilter(object.MatchStringEqual, size)

require.Len(t, *fs, 1)

f := (*fs)[0]

require.Equal(t, object.FilterPayloadSize, f.Header())
require.Equal(t, strconv.FormatUint(size, 10), f.Value())
require.Equal(t, object.MatchStringEqual, f.Operation())

t.Run("v2", func(t *testing.T) {
fsV2 := fs.ToV2()

require.Len(t, fsV2, 1)

require.Equal(t, v2object.FilterHeaderPayloadLength, fsV2[0].GetKey())
require.Equal(t, strconv.FormatUint(size, 10), fsV2[0].GetValue())
require.Equal(t, v2object.MatchStringEqual, fsV2[0].GetMatchType())
})
}

0 comments on commit 88902f6

Please sign in to comment.