Skip to content

Commit

Permalink
feat: add query.GenericFilteredPaginated (backport cosmos#12253) (cos…
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored and JeancarloBarrios committed Sep 28, 2024
1 parent b622ac7 commit 4c4f657
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
steps:
- uses: actions/setup-go@v2.1.4
with:
go-version: 1.17
go-version: 1.18
- uses: technote-space/get-diff-action@v5
id: git_diff
with:
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/sims.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.17
go-version: 1.18
- name: Display go version
run: go version
- run: make build
Expand All @@ -32,11 +32,11 @@ jobs:
steps:
- uses: actions/setup-go@v2.1.3
with:
go-version: 1.17
go-version: 1.18
- name: Display go version
run: go version
- name: Install runsim
run: export GO111MODULE="on" && go get github.com/cosmos/tools/cmd/runsim@v1.0.0
run: export GO111MODULE="on" && go install github.com/cosmos/tools/cmd/runsim@v1.0.0
- uses: actions/cache@v2.1.6
with:
path: ~/go/bin
Expand All @@ -49,7 +49,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2.1.3
with:
go-version: 1.17
go-version: 1.18
- name: Display go version
run: go version
- uses: technote-space/get-diff-action@v4
Expand All @@ -76,7 +76,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.17
go-version: 1.18
- name: Display go version
run: go version
- uses: technote-space/get-diff-action@v4
Expand Down Expand Up @@ -104,7 +104,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2.1.3
with:
go-version: 1.17
go-version: 1.18
- name: Display go version
run: go version
- uses: technote-space/get-diff-action@v4
Expand All @@ -123,7 +123,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.17
go-version: 1.18
- name: Display go version
run: go version
- uses: technote-space/get-diff-action@v4
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.17
go-version: 1.18
- name: Display go version
run: go version
- name: install tparse
Expand All @@ -42,7 +42,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.17
go-version: 1.18
- uses: technote-space/get-diff-action@v4
id: git_diff
with:
Expand Down Expand Up @@ -73,7 +73,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.17
go-version: 1.18
- name: Display go version
run: go version
- uses: technote-space/get-diff-action@v4
Expand Down Expand Up @@ -133,7 +133,7 @@ jobs:
steps:
- uses: actions/checkout@v4
with:
go-version: 1.17
go-version: 1.18
- uses: technote-space/get-diff-action@v4
with:
PATTERNS: |
Expand Down Expand Up @@ -525,7 +525,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.17
go-version: 1.18
- uses: technote-space/get-diff-action@v4
with:
PATTERNS: |
Expand Down Expand Up @@ -914,7 +914,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.17
go-version: 1.18
- uses: technote-space/get-diff-action@v4
id: git_diff
with:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

* (simapp) [#12314](https://github.com/cosmos/cosmos-sdk/pull/12314) Increase `DefaultGenTxGas` from `1000000` to `10000000`
* [#12371](https://github.com/cosmos/cosmos-sdk/pull/12371) Update min required Golang version to 1.18.

### Bug Fixes

* [\#12317](https://github.com/cosmos/cosmos-sdk/pull/12317) Rename `edit-validator` command's `--moniker` flag to `--new-moniker`
* (x/upgrade) [#12264](https://github.com/cosmos/cosmos-sdk/pull/12264) Fix `GetLastCompleteUpgrade` to properly return the latest upgrade.
* (x/crisis) [#12208](https://github.com/cosmos/cosmos-sdk/pull/12208) Fix progress index of crisis invariant assertion logs.

### Features

* (query) [#12253](https://github.com/cosmos/cosmos-sdk/pull/12253) Add `GenericFilteredPaginate` to the `query` package to improve UX.

## [v0.45.5](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.45.5) - 2022-06-09

### Improvements
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The Cosmos SDK is a framework for building blockchain applications. [Tendermint
**WARNING**: The Cosmos SDK has mostly stabilized, but we are still making some
breaking changes.

**Note**: Requires [Go 1.17+](https://golang.org/dl/)
**Note**: Requires [Go 1.18+](https://golang.org/dl/)

## Quick Start

Expand Down
2 changes: 1 addition & 1 deletion contrib/images/simd-dlv/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.17-alpine AS build
FROM golang:1.18-alpine AS build
RUN apk add build-base git linux-headers libc-dev
RUN go install github.com/go-delve/delve/cmd/dlv@latest
WORKDIR /work
Expand Down
2 changes: 1 addition & 1 deletion contrib/images/simd-env/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.17-alpine AS build
FROM golang:1.18-alpine AS build
RUN apk add build-base git linux-headers
WORKDIR /work
COPY go.mod go.sum /work/
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go 1.17
go 1.18

module github.com/cosmos/cosmos-sdk

Expand Down
161 changes: 151 additions & 10 deletions types/query/filtered_pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package query
import (
"errors"

"github.com/cosmos/gogoproto/proto"

"cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/types"
)

// FilteredPaginate does pagination of all the results in the PrefixStore based on the
Expand Down Expand Up @@ -35,8 +32,18 @@ func FilteredPaginate(
err error
)

iterator := getIterator(prefixStore, pageRequest.Key, pageRequest.Reverse)
defer iterator.Close()
// count total results when the limit is zero/not supplied
countTotal = true
}

if len(key) != 0 {
iterator := getIterator(prefixStore, key, reverse)
defer iterator.Close()

var (
numHits uint64
nextKey []byte
)

if len(pageRequest.Key) != 0 {
accumulateFn := func(_ uint64) bool { return true }
Expand Down Expand Up @@ -76,10 +83,10 @@ func FilteredPaginate(
}
}

res := &PageResponse{NextKey: nextKey}
if pageRequest.CountTotal {
res.Total = numHits
}
var (
numHits uint64
nextKey []byte
)

return res, nil
}
Expand Down Expand Up @@ -209,3 +216,137 @@ func GenericFilteredPaginate[T, F proto.Message](

return results, res, nil
}

// GenericFilteredPaginate does pagination of all the results in the PrefixStore based on the
// provided PageRequest. `onResult` should be used to filter or transform the results.
// `c` is a constructor function that needs to return a new instance of the type T (this is to
// workaround some generic pitfalls in which we can't instantiate a T struct inside the function).
// If key is provided, the pagination uses the optimized querying.
// If offset is used, the pagination uses lazy filtering i.e., searches through all the records.
// The resulting slice (of type F) can be of a different type than the one being iterated through
// (type T), so it's possible to do any necessary transformation inside the onResult function.
func GenericFilteredPaginate[T codec.ProtoMarshaler, F codec.ProtoMarshaler](
cdc codec.BinaryCodec,
prefixStore types.KVStore,
pageRequest *PageRequest,
onResult func(key []byte, value T) (F, error),
constructor func() T,
) ([]F, *PageResponse, error) {
// if the PageRequest is nil, use default PageRequest
if pageRequest == nil {
pageRequest = &PageRequest{}
}

offset := pageRequest.Offset
key := pageRequest.Key
limit := pageRequest.Limit
countTotal := pageRequest.CountTotal
reverse := pageRequest.Reverse
results := []F{}

if offset > 0 && key != nil {
return results, nil, fmt.Errorf("invalid request, either offset or key is expected, got both")
}

if limit == 0 {
limit = DefaultLimit

// count total results when the limit is zero/not supplied
countTotal = true
}

if len(key) != 0 {
iterator := getIterator(prefixStore, key, reverse)
defer iterator.Close()

var (
numHits uint64
nextKey []byte
)

for ; iterator.Valid(); iterator.Next() {
if numHits == limit {
nextKey = iterator.Key()
break
}

if iterator.Error() != nil {
return nil, nil, iterator.Error()
}

protoMsg := constructor()

err := cdc.Unmarshal(iterator.Value(), protoMsg)
if err != nil {
return nil, nil, err
}

val, err := onResult(iterator.Key(), protoMsg)
if err != nil {
return nil, nil, err
}

if val.Size() != 0 {
results = append(results, val)
numHits++
}
}

return results, &PageResponse{
NextKey: nextKey,
}, nil
}

iterator := getIterator(prefixStore, nil, reverse)
defer iterator.Close()

end := offset + limit

var (
numHits uint64
nextKey []byte
)

for ; iterator.Valid(); iterator.Next() {
if iterator.Error() != nil {
return nil, nil, iterator.Error()
}

protoMsg := constructor()

err := cdc.Unmarshal(iterator.Value(), protoMsg)
if err != nil {
return nil, nil, err
}

val, err := onResult(iterator.Key(), protoMsg)
if err != nil {
return nil, nil, err
}

if val.Size() != 0 {
// Previously this was the "accumulate" flag
if numHits >= offset && numHits < end {
results = append(results, val)
}
numHits++
}

if numHits == end+1 {
if nextKey == nil {
nextKey = iterator.Key()
}

if !countTotal {
break
}
}
}

res := &PageResponse{NextKey: nextKey}
if countTotal {
res.Total = numHits
}

return results, res, nil
}
Loading

0 comments on commit 4c4f657

Please sign in to comment.