Skip to content

Commit

Permalink
fix: collection filtered pagination (#23002)
Browse files Browse the repository at this point in the history
(cherry picked from commit 697219c)

# Conflicts:
#	CHANGELOG.md
  • Loading branch information
akhilkumarpilli authored and mergify[bot] committed Dec 31, 2024
1 parent 84301c8 commit 387fb74
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ Ref: https://keepachangelog.com/en/1.0.0/

Every module contains its own CHANGELOG.md. Please refer to the module you are interested in.

<<<<<<< HEAD
=======
### Features

* (baseapp) [#20291](https://github.com/cosmos/cosmos-sdk/pull/20291) Simulate nested messages.
* (client/keys) [#21829](https://github.com/cosmos/cosmos-sdk/pull/21829) Add support for importing hex key using standard input.

### Improvements

### Bug Fixes

* (query) [23002](https://github.com/cosmos/cosmos-sdk/pull/23002) Fix collection filtered pagination.

### API Breaking Changes

* (x/params) [#22995](https://github.com/cosmos/cosmos-sdk/pull/22995) Remove `x/params`. Migrate to the new params system introduced in `v0.47` as demonstrated [here](https://github.com/cosmos/cosmos-sdk/blob/main/UPGRADING.md#xparams).
* (testutil) [#22392](https://github.com/cosmos/cosmos-sdk/pull/22392) Remove `testutil/network` package. Use the integration framework or systemtests framework instead.

### Deprecated

* (modules) [#22994](https://github.com/cosmos/cosmos-sdk/pull/22994) Deprecate `Invariants` and associated methods.

>>>>>>> 697219c9c (fix: collection filtered pagination (#23002))
## [v0.52.0-rc.1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.52.0-rc.1) - 2024-12-18

Every module contains its own CHANGELOG.md. Please refer to the module you are interested in.
Expand Down
52 changes: 29 additions & 23 deletions types/query/collections_pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,53 +266,59 @@ func collFilteredPaginateByKey[K, V any, C Collection[K, V], T any](
defer iterator.Close()

var (
count uint64
nextKey []byte
count uint64
nextKey []byte
transformed T
)

for ; iterator.Valid(); iterator.Next() {
// if we reached the specified limit
// then we get the next key, and we exit the iteration.
if count == limit {
concreteKey, err := iterator.Key()
if err != nil {
return nil, nil, err
}

nextKey, err = encodeCollKey[K, V](coll, concreteKey)
if err != nil {
return nil, nil, err
}
break
}

kv, err := iterator.KeyValue()
if err != nil {
return nil, nil, err
}

include := false
// if no predicate is specified then we just append the result
if predicateFunc == nil {
transformed, err := transformFunc(kv.Key, kv.Value)
transformed, err = transformFunc(kv.Key, kv.Value)
if err != nil {
return nil, nil, err
}
results = append(results, transformed)
include = true
// if predicate is applied we execute the predicate function
// and append only if predicateFunc yields true.
} else {
include, err := predicateFunc(kv.Key, kv.Value)
include, err = predicateFunc(kv.Key, kv.Value)
if err != nil {
return nil, nil, err
}
if include {
transformed, err := transformFunc(kv.Key, kv.Value)
transformed, err = transformFunc(kv.Key, kv.Value)
if err != nil {
return nil, nil, err
}
results = append(results, transformed)
}
}
count++

if include {
// if we reached the specified limit
// then we get the next key, and we exit the iteration.
if count == limit {
concreteKey, err := iterator.Key()
if err != nil {
return nil, nil, err
}

nextKey, err = encodeCollKey[K, V](coll, concreteKey)
if err != nil {
return nil, nil, err
}
break
}

results = append(results, transformed)
count++
}
}

return results, &PageResponse{
Expand Down
17 changes: 16 additions & 1 deletion types/query/collections_pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,29 @@ func TestCollectionPagination(t *testing.T) {
Limit: 3,
},
expResp: &PageResponse{
NextKey: encodeKey(5),
NextKey: encodeKey(8),
},
filter: func(key, value uint64) (bool, error) {
return key%2 == 0, nil
},
expResults: []collections.KeyValue[uint64, uint64]{
{Key: 2, Value: 2},
{Key: 4, Value: 4},
{Key: 6, Value: 6},
},
},
"filtered with key and empty next key in response": {
req: &PageRequest{
Key: encodeKey(295),
},
expResp: &PageResponse{
NextKey: nil,
},
filter: func(key, value uint64) (bool, error) {
return key%5 == 0, nil
},
expResults: []collections.KeyValue[uint64, uint64]{
{Key: 295, Value: 295},
},
},
}
Expand Down

0 comments on commit 387fb74

Please sign in to comment.