-
Notifications
You must be signed in to change notification settings - Fork 59
Pagination keyset2 #956
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
Open
HayimShaul
wants to merge
31
commits into
main
Choose a base branch
from
pagination-keyset2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+802
−96
Open
Pagination keyset2 #956
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
2da0386
Keyset pagination
alexandrosfilios ce55e74
Merge branch 'pagination-keyset2' of https://github.com/hyperledger-l…
e475ba7
passing missing context
fbbd5e6
bug fixes in keyset pagination
8de3c0c
Some info
alexandrosfilios 17f61d7
bug fix: keyset pagination handling lastId
7f43183
keyset pagination tests
f25ac45
bug fixes
9b5c53c
bug fix: dont let pagination add fieldName to select if it is already…
36ae33f
improved implementation
2da4b91
support asterix
61408de
fix keyset pagination with int id
20aa429
Keyset pagination
alexandrosfilios 74c1d9c
bug fixes in keyset pagination
bbb88ba
Some info
alexandrosfilios db748b3
bug fix: keyset pagination handling lastId
7a259ac
keyset pagination tests
b55bfb2
bug fixes
70d032a
bug fix: dont let pagination add fieldName to select if it is already…
526150e
improved implementation
479a450
support asterix
38c05b0
fix keyset pagination with int id
11e4de3
Merge branch 'pagination-keyset2' of https://github.com/hyperledger-l…
65c23d9
fix some broken tests
1356ea6
bug fixes
1fe61b8
fixup!
alexandrosfilios 2ac819c
debugging ... still not working
9452a81
pagination test with database works
af0c0dc
use switch instead of ifs
c86cf34
don't keep firstIdOffset and lastIdOffset in keyset pagination
aa71b57
typos and making sure a field added to sql query is unique
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
platform/view/services/db/driver/sql/query/pagination/keyset.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
|
||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package pagination | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/hyperledger-labs/fabric-smart-client/platform/common/driver" | ||
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/sql/query/common" | ||
) | ||
|
||
// PropertyName is the name of the field in the struct that is returned from the database | ||
// V is the type of the field | ||
type PropertyName[V comparable] string | ||
|
||
// ExtractField extracts the field from the given value | ||
func (p PropertyName[V]) ExtractField(v any) V { | ||
return reflect.ValueOf(v).FieldByName(string(p)).Interface().(V) | ||
} | ||
|
||
type keyset[I comparable, V any] struct { | ||
offset int | ||
pageSize int | ||
sqlIdName common.FieldName | ||
idGetter func(V) I | ||
// the first and last id values in the page | ||
firstId, lastId I | ||
} | ||
|
||
// KeysetWithField creates a keyset pagination where the id has field name idFieldName | ||
func KeysetWithField[I comparable](offset int, pageSize int, sqlIdName common.FieldName, idFieldName PropertyName[I]) (*keyset[I, any], error) { | ||
if strings.ToUpper(string(idFieldName[0])) != string(idFieldName[0]) { | ||
return nil, fmt.Errorf("must use exported field") | ||
} | ||
return Keyset(offset, pageSize, sqlIdName, idFieldName.ExtractField) | ||
} | ||
|
||
type id[I comparable] interface { | ||
Id() I | ||
} | ||
|
||
// KeysetWithId creates a keyset pagination where the result object implements id[I] | ||
func KeysetWithId[I comparable, V id[I]](offset int, pageSize int, sqlIdName common.FieldName) (*keyset[I, V], error) { | ||
return Keyset[I, V](offset, pageSize, sqlIdName, func(v V) I { return v.Id() }) | ||
} | ||
|
||
// Keyset creates a keyset pagination | ||
func Keyset[I comparable, V any](offset int, pageSize int, sqlIdName common.FieldName, idGetter func(V) I) (*keyset[I, V], error) { | ||
if offset < 0 { | ||
return nil, fmt.Errorf("offset must be greater than zero. Offset: %d", offset) | ||
} | ||
if pageSize < 0 { | ||
return nil, fmt.Errorf("page size must be greater than zero. pageSize: %d", pageSize) | ||
} | ||
return &keyset[I, V]{ | ||
offset: offset, | ||
pageSize: pageSize, | ||
sqlIdName: sqlIdName, | ||
idGetter: idGetter, | ||
firstId: nilElement[I](), | ||
lastId: nilElement[I](), | ||
}, nil | ||
} | ||
|
||
func nilElement[I any]() I { | ||
var zero I | ||
switch any(zero).(type) { | ||
case int: | ||
return any(-1).(I) | ||
case string: | ||
return any("").(I) | ||
default: | ||
panic("unsupported type") | ||
} | ||
} | ||
|
||
func (p *keyset[I, V]) nilElement() I { | ||
return nilElement[I]() | ||
} | ||
|
||
func (p *keyset[I, V]) GoToOffset(offset int) (driver.Pagination, error) { | ||
if offset < 0 { | ||
return nil, fmt.Errorf("Offset must be greater than zero. pageSize: %d", pageSize) | ||
} | ||
if offset == p.offset+p.pageSize { | ||
return &keyset[I, V]{ | ||
offset: offset, | ||
pageSize: p.pageSize, | ||
sqlIdName: p.sqlIdName, | ||
idGetter: p.idGetter, | ||
firstId: p.lastId, | ||
lastId: p.nilElement(), | ||
}, nil | ||
} | ||
return &keyset[I, V]{ | ||
offset: offset, | ||
pageSize: p.pageSize, | ||
sqlIdName: p.sqlIdName, | ||
idGetter: p.idGetter, | ||
firstId: p.nilElement(), | ||
lastId: p.nilElement(), | ||
}, nil | ||
} | ||
|
||
func (p *keyset[I, V]) GoToPage(pageNum int) (driver.Pagination, error) { | ||
return p.GoToOffset(pageNum * p.pageSize) | ||
} | ||
|
||
func (p *keyset[I, V]) GoForward(numOfpages int) (driver.Pagination, error) { | ||
return p.GoToOffset(p.offset + (numOfpages * p.pageSize)) | ||
} | ||
|
||
func (p *keyset[I, V]) GoBack(numOfpages int) (driver.Pagination, error) { | ||
return p.GoForward(-1 * numOfpages) | ||
} | ||
|
||
func (p *keyset[I, V]) Prev() (driver.Pagination, error) { return p.GoBack(1) } | ||
|
||
func (p *keyset[I, V]) Next() (driver.Pagination, error) { return p.GoForward(1) } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, here you are handling the case where we call
Next()
twice (without having fetched the db results first). And I think this is the reason you have to keep offsetoflastid and offsetoffirstid. However, this is not a use case for us. When we call next, we would first fetch the results and then proceed to the next (as all paginations work in UIs). So, instead of taking care of this case for optimized performance, I would just fall back to an offset pagination (you add the new offset to the existing offset and drop the firstId). It may have worse performance, but it is simpler and it is just a fallback for a case that will practically never occur, just so we are protected from unexpected behaviors.