Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(collections): implement pagination #14468

Merged
merged 17 commits into from
Jan 6, 2023
Prev Previous commit
Next Next commit
chore: lint
  • Loading branch information
testinginprod committed Jan 3, 2023
commit d0c4809f809936c358d7a8945f1fe64f6f1eb02f
13 changes: 8 additions & 5 deletions types/query/collections_pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package query

import (
"context"
"cosmossdk.io/collections"
"fmt"

"cosmossdk.io/collections"
)

// Collection defines the minimum required API of a collection
Expand All @@ -27,7 +28,6 @@ func CollectionFilteredPaginate[K, V any, C Collection[K, V]](
ctx context.Context, coll C, pageReq *PageRequest,
predicateFunc func(key K, value V) (include bool),
) ([]collections.KeyValue[K, V], *PageResponse, error) {

if pageReq == nil {
pageReq = &PageRequest{}
}
Expand Down Expand Up @@ -57,7 +57,8 @@ func CollectionFilteredPaginate[K, V any, C Collection[K, V]](
// collFilteredPaginateNoKey applies the provided pagination on the collection when the starting key is not set.
// If predicateFunc is nil no filtering is applied.
func collFilteredPaginateNoKey[K, V any, C Collection[K, V]](ctx context.Context, coll C, reverse bool, offset, limit uint64, countTotal bool, predicateFunc func(K, V) bool) (
testinginprod marked this conversation as resolved.
Show resolved Hide resolved
[]collections.KeyValue[K, V], *PageResponse, error) {
[]collections.KeyValue[K, V], *PageResponse, error,
) {
iterator, err := getCollIter[K, V](ctx, coll, nil, reverse)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -125,7 +126,8 @@ func collFilteredPaginateNoKey[K, V any, C Collection[K, V]](ctx context.Context
func advanceIter[I interface {
Next()
Valid() bool
}](iter I, offset uint64) bool {
}](iter I, offset uint64,
) bool {
for i := uint64(0); i < offset; i++ {
if !iter.Valid() {
return false
Expand All @@ -140,7 +142,8 @@ func advanceIter[I interface {
func collFilteredPaginateByKey[K, V any, C Collection[K, V]](
testinginprod marked this conversation as resolved.
Show resolved Hide resolved
ctx context.Context, coll C,
key []byte, reverse bool, limit uint64,
predicateFunc func(K, V) bool) ([]collections.KeyValue[K, V], *PageResponse, error) {
predicateFunc func(K, V) bool,
) ([]collections.KeyValue[K, V], *PageResponse, error) {
iterator, err := getCollIter[K, V](ctx, coll, key, reverse)
if err != nil {
return nil, nil, err
Expand Down
3 changes: 2 additions & 1 deletion types/query/collections_pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package query

import (
"context"
"testing"

"cosmossdk.io/collections"
"cosmossdk.io/core/store"
db "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"
"testing"
)

func TestCollectionPagination(t *testing.T) {
Expand Down