Skip to content

Commit

Permalink
Export time id paginator (#1526)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrel-b authored Jun 19, 2024
1 parent f36573f commit b48e258
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 52 deletions.
12 changes: 6 additions & 6 deletions publicapi/community.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (api CommunityAPI) PaginateHoldersByCommunityID(ctx context.Context, commun
return nil, PageInfo{}, err
}

queryFunc := func(params timeIDPagingParams) ([]db.User, error) {
queryFunc := func(params TimeIDPagingParams) ([]db.User, error) {
return api.loaders.PaginateHoldersByCommunityID.Load(db.PaginateHoldersByCommunityIDParams{
CommunityID: communityID,
Limit: sql.NullInt32{Int32: int32(params.Limit), Valid: true},
Expand All @@ -112,7 +112,7 @@ func (api CommunityAPI) PaginateHoldersByCommunityID(ctx context.Context, commun
return u.CreatedAt, u.ID, nil
}

paginator := timeIDPaginator[db.User]{
paginator := TimeIDPaginator[db.User]{
QueryFunc: queryFunc,
CursorFunc: cursorFunc,
CountFunc: countFunc,
Expand All @@ -133,7 +133,7 @@ func (api CommunityAPI) PaginatePostsByCommunityID(ctx context.Context, communit
return nil, PageInfo{}, err
}

timeFunc := func(params timeIDPagingParams) ([]db.Post, error) {
timeFunc := func(params TimeIDPagingParams) ([]db.Post, error) {
return api.loaders.PaginatePostsByCommunityID.Load(db.PaginatePostsByCommunityIDParams{
CommunityID: communityID,
Limit: params.Limit,
Expand All @@ -154,7 +154,7 @@ func (api CommunityAPI) PaginatePostsByCommunityID(ctx context.Context, communit
return p.CreatedAt, p.ID, nil
}

paginator := timeIDPaginator[db.Post]{
paginator := TimeIDPaginator[db.Post]{
QueryFunc: timeFunc,
CursorFunc: timeCursorFunc,
CountFunc: countFunc,
Expand All @@ -175,7 +175,7 @@ func (api CommunityAPI) PaginateTokensByCommunityID(ctx context.Context, communi
return nil, PageInfo{}, err
}

queryFunc := func(params timeIDPagingParams) ([]db.Token, error) {
queryFunc := func(params TimeIDPagingParams) ([]db.Token, error) {
results, err := api.loaders.PaginateTokensByCommunityID.Load(db.PaginateTokensByCommunityIDParams{
CommunityID: communityID,
Limit: params.Limit,
Expand All @@ -197,7 +197,7 @@ func (api CommunityAPI) PaginateTokensByCommunityID(ctx context.Context, communi
return t.CreatedAt, t.ID, nil
}

paginator := timeIDPaginator[db.Token]{
paginator := TimeIDPaginator[db.Token]{
QueryFunc: queryFunc,
CursorFunc: cursorFunc,
CountFunc: countFunc,
Expand Down
12 changes: 6 additions & 6 deletions publicapi/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (api ContractAPI) GetChildContractsByParentID(ctx context.Context, contract
return nil, PageInfo{}, err
}

queryFunc := func(params timeIDPagingParams) ([]db.Contract, error) {
queryFunc := func(params TimeIDPagingParams) ([]db.Contract, error) {
return api.loaders.GetChildContractsByParentIDBatchPaginate.Load(db.GetChildContractsByParentIDBatchPaginateParams{
ParentID: contractID,
CurBeforeTime: params.CursorBeforeTime,
Expand All @@ -91,7 +91,7 @@ func (api ContractAPI) GetChildContractsByParentID(ctx context.Context, contract
return c.CreatedAt, c.ID, nil
}

paginator := timeIDPaginator[db.Contract]{
paginator := TimeIDPaginator[db.Contract]{
QueryFunc: queryFunc,
CursorFunc: cursorFunc,
}
Expand Down Expand Up @@ -218,7 +218,7 @@ func (api ContractAPI) GetCommunityPostsByContractID(ctx context.Context, contra
return nil, PageInfo{}, err
}

timeFunc := func(params timeIDPagingParams) ([]db.Post, error) {
timeFunc := func(params TimeIDPagingParams) ([]db.Post, error) {
return api.loaders.PaginatePostsByContractID.Load(db.PaginatePostsByContractIDParams{
ContractID: contractID,
Limit: params.Limit,
Expand All @@ -239,7 +239,7 @@ func (api ContractAPI) GetCommunityPostsByContractID(ctx context.Context, contra
return p.CreatedAt, p.ID, nil
}

paginator := timeIDPaginator[db.Post]{
paginator := TimeIDPaginator[db.Post]{
QueryFunc: timeFunc,
CursorFunc: timeCursorFunc,
CountFunc: countFunc,
Expand All @@ -262,7 +262,7 @@ func (api ContractAPI) GetCommunityPostsByContractIDAndProjectID(ctx context.Con
return nil, PageInfo{}, err
}

timeFunc := func(params timeIDPagingParams) ([]db.Post, error) {
timeFunc := func(params TimeIDPagingParams) ([]db.Post, error) {
return api.queries.PaginatePostsByContractIDAndProjectID(ctx, db.PaginatePostsByContractIDAndProjectIDParams{
ContractID: contractID,
ProjectIDInt: int32(projectID),
Expand All @@ -284,7 +284,7 @@ func (api ContractAPI) GetCommunityPostsByContractIDAndProjectID(ctx context.Con
return p.CreatedAt, p.ID, nil
}

paginator := timeIDPaginator[db.Post]{
paginator := TimeIDPaginator[db.Post]{
QueryFunc: timeFunc,
CursorFunc: timeCursorFunc,
CountFunc: countFunc,
Expand Down
12 changes: 6 additions & 6 deletions publicapi/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func (api FeedAPI) PersonalFeed(ctx context.Context, before *string, after *stri
return nil, PageInfo{}, err
}

queryFunc := func(params timeIDPagingParams) ([]interface{}, error) {
queryFunc := func(params TimeIDPagingParams) ([]interface{}, error) {
keys, err := api.queries.PaginatePersonalFeedByUserID(ctx, db.PaginatePersonalFeedByUserIDParams{
Follower: userID,
Limit: params.Limit,
Expand All @@ -436,7 +436,7 @@ func (api FeedAPI) PersonalFeed(ctx context.Context, before *string, after *stri
return feedEntityToTypedType(ctx, api.loaders, keys)
}

paginator := timeIDPaginator[any]{
paginator := TimeIDPaginator[any]{
QueryFunc: queryFunc,
CursorFunc: feedCursor,
}
Expand All @@ -456,7 +456,7 @@ func (api FeedAPI) UserFeed(ctx context.Context, userID persist.DBID, before *st
return nil, PageInfo{}, err
}

queryFunc := func(params timeIDPagingParams) ([]db.Post, error) {
queryFunc := func(params TimeIDPagingParams) ([]db.Post, error) {
return api.queries.PaginatePostsByUserID(ctx, db.PaginatePostsByUserIDParams{
UserID: userID,
Limit: params.Limit,
Expand All @@ -477,7 +477,7 @@ func (api FeedAPI) UserFeed(ctx context.Context, userID persist.DBID, before *st
return p.CreatedAt, p.ID, nil
}

paginator := timeIDPaginator[db.Post]{
paginator := TimeIDPaginator[db.Post]{
QueryFunc: queryFunc,
CursorFunc: cursorFunc,
CountFunc: countFunc,
Expand All @@ -493,7 +493,7 @@ func (api FeedAPI) GlobalFeed(ctx context.Context, before *string, after *string
}
viewerID, _ := getAuthenticatedUserID(ctx)

queryFunc := func(params timeIDPagingParams) ([]any, error) {
queryFunc := func(params TimeIDPagingParams) ([]any, error) {
keys, err := api.queries.PaginateGlobalFeed(ctx, db.PaginateGlobalFeedParams{
Limit: params.Limit,
CurBeforeTime: params.CursorBeforeTime,
Expand All @@ -511,7 +511,7 @@ func (api FeedAPI) GlobalFeed(ctx context.Context, before *string, after *string
return feedEntityToTypedType(ctx, api.loaders, keys)
}

paginator := timeIDPaginator[any]{
paginator := TimeIDPaginator[any]{
QueryFunc: queryFunc,
CursorFunc: feedCursor,
}
Expand Down
28 changes: 14 additions & 14 deletions publicapi/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (api InteractionAPI) PaginateAdmiresByFeedEventID(ctx context.Context, feed
return nil, PageInfo{}, err
}

queryFunc := func(params timeIDPagingParams) ([]db.Admire, error) {
queryFunc := func(params TimeIDPagingParams) ([]db.Admire, error) {
return api.loaders.PaginateAdmiresByFeedEventIDBatch.Load(db.PaginateAdmiresByFeedEventIDBatchParams{
FeedEventID: feedEventID,
Limit: params.Limit,
Expand All @@ -307,7 +307,7 @@ func (api InteractionAPI) PaginateAdmiresByFeedEventID(ctx context.Context, feed
return a.CreatedAt, a.ID, nil
}

paginator := timeIDPaginator[db.Admire]{
paginator := TimeIDPaginator[db.Admire]{
QueryFunc: queryFunc,
CursorFunc: cursorFunc,
CountFunc: countFunc,
Expand All @@ -328,7 +328,7 @@ func (api InteractionAPI) PaginateAdmiresByCommentID(ctx context.Context, commen
return nil, PageInfo{}, err
}

queryFunc := func(params timeIDPagingParams) ([]db.Admire, error) {
queryFunc := func(params TimeIDPagingParams) ([]db.Admire, error) {
return api.loaders.PaginateAdmiresByCommentIDBatch.Load(db.PaginateAdmiresByCommentIDBatchParams{
CommentID: commentID,
Limit: params.Limit,
Expand All @@ -349,7 +349,7 @@ func (api InteractionAPI) PaginateAdmiresByCommentID(ctx context.Context, commen
return a.CreatedAt, a.ID, nil
}

paginator := timeIDPaginator[db.Admire]{
paginator := TimeIDPaginator[db.Admire]{
QueryFunc: queryFunc,
CursorFunc: cursorFunc,
CountFunc: countFunc,
Expand All @@ -370,7 +370,7 @@ func (api InteractionAPI) PaginateCommentsByFeedEventID(ctx context.Context, fee
return nil, PageInfo{}, err
}

queryFunc := func(params timeIDPagingParams) ([]db.Comment, error) {
queryFunc := func(params TimeIDPagingParams) ([]db.Comment, error) {
return api.loaders.PaginateCommentsByFeedEventIDBatch.Load(db.PaginateCommentsByFeedEventIDBatchParams{
FeedEventID: feedEventID,
Limit: params.Limit,
Expand All @@ -391,7 +391,7 @@ func (api InteractionAPI) PaginateCommentsByFeedEventID(ctx context.Context, fee
return c.CreatedAt, c.ID, nil
}

paginator := timeIDPaginator[db.Comment]{
paginator := TimeIDPaginator[db.Comment]{
QueryFunc: queryFunc,
CursorFunc: cursorFunc,
CountFunc: countFunc,
Expand All @@ -412,7 +412,7 @@ func (api InteractionAPI) PaginateRepliesByCommentID(ctx context.Context, commen
return nil, PageInfo{}, err
}

queryFunc := func(params timeIDPagingParams) ([]db.Comment, error) {
queryFunc := func(params TimeIDPagingParams) ([]db.Comment, error) {
return api.loaders.PaginateRepliesByCommentIDBatch.Load(db.PaginateRepliesByCommentIDBatchParams{
CommentID: commentID,
Limit: params.Limit,
Expand All @@ -433,7 +433,7 @@ func (api InteractionAPI) PaginateRepliesByCommentID(ctx context.Context, commen
return c.CreatedAt, c.ID, nil
}

paginator := timeIDPaginator[db.Comment]{
paginator := TimeIDPaginator[db.Comment]{
QueryFunc: queryFunc,
CursorFunc: cursorFunc,
CountFunc: countFunc,
Expand Down Expand Up @@ -487,7 +487,7 @@ func (api InteractionAPI) PaginateAdmiresByPostID(ctx context.Context, postID pe
return nil, PageInfo{}, err
}

queryFunc := func(params timeIDPagingParams) ([]db.Admire, error) {
queryFunc := func(params TimeIDPagingParams) ([]db.Admire, error) {
return api.loaders.PaginateAdmiresByPostIDBatch.Load(db.PaginateAdmiresByPostIDBatchParams{
PostID: postID,
Limit: params.Limit,
Expand All @@ -508,7 +508,7 @@ func (api InteractionAPI) PaginateAdmiresByPostID(ctx context.Context, postID pe
return a.CreatedAt, a.ID, nil
}

paginator := timeIDPaginator[db.Admire]{
paginator := TimeIDPaginator[db.Admire]{
QueryFunc: queryFunc,
CursorFunc: cursorFunc,
CountFunc: countFunc,
Expand Down Expand Up @@ -538,7 +538,7 @@ func (api InteractionAPI) PaginateAdmiresByTokenID(ctx context.Context, tokenID
}
onlyForActor := actorID != ""

queryFunc := func(params timeIDPagingParams) ([]db.Admire, error) {
queryFunc := func(params TimeIDPagingParams) ([]db.Admire, error) {
return api.loaders.PaginateAdmiresByTokenIDBatch.Load(db.PaginateAdmiresByTokenIDBatchParams{
TokenID: tokenID,
Limit: params.Limit,
Expand All @@ -561,7 +561,7 @@ func (api InteractionAPI) PaginateAdmiresByTokenID(ctx context.Context, tokenID
return a.CreatedAt, a.ID, nil
}

paginator := timeIDPaginator[db.Admire]{
paginator := TimeIDPaginator[db.Admire]{
QueryFunc: queryFunc,
CursorFunc: cursorFunc,
CountFunc: countFunc,
Expand All @@ -582,7 +582,7 @@ func (api InteractionAPI) PaginateCommentsByPostID(ctx context.Context, postID p
return nil, PageInfo{}, err
}

queryFunc := func(params timeIDPagingParams) ([]db.Comment, error) {
queryFunc := func(params TimeIDPagingParams) ([]db.Comment, error) {
return api.loaders.PaginateCommentsByPostIDBatch.Load(db.PaginateCommentsByPostIDBatchParams{
PostID: postID,
Limit: params.Limit,
Expand All @@ -603,7 +603,7 @@ func (api InteractionAPI) PaginateCommentsByPostID(ctx context.Context, postID p
return c.CreatedAt, c.ID, nil
}

paginator := timeIDPaginator[db.Comment]{
paginator := TimeIDPaginator[db.Comment]{
QueryFunc: queryFunc,
CursorFunc: cursorFunc,
CountFunc: countFunc,
Expand Down
4 changes: 2 additions & 2 deletions publicapi/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (api NotificationsAPI) GetViewerNotifications(ctx context.Context, before,
return nil, PageInfo{}, 0, err
}

queryFunc := func(params timeIDPagingParams) ([]db.Notification, error) {
queryFunc := func(params TimeIDPagingParams) ([]db.Notification, error) {
return api.loaders.GetUserNotificationsBatch.Load(db.GetUserNotificationsBatchParams{
OwnerID: userID,
Limit: params.Limit,
Expand All @@ -55,7 +55,7 @@ func (api NotificationsAPI) GetViewerNotifications(ctx context.Context, before,
return n.CreatedAt, n.ID, nil
}

paginator := timeIDPaginator[db.Notification]{
paginator := TimeIDPaginator[db.Notification]{
QueryFunc: queryFunc,
CursorFunc: cursorFunc,
CountFunc: countFunc,
Expand Down
20 changes: 10 additions & 10 deletions publicapi/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func applyCursors[Node any, K cursor](allEdges []Node, cF cursorable[Node, K], b
}

// keysetPaginator is the base keyset pagination struct. You probably don't want to use this directly;
// use a cursor-specific helper like timeIDPaginator.
// use a cursor-specific helper like TimeIDPaginator.
// For reasons to favor keyset pagination, see: https://www.citusdata.com/blog/2016/03/30/five-ways-to-paginate/
type keysetPaginator[Node any, Cur cursor] struct {
// QueryFunc returns paginated results for the given paging parameters
Expand Down Expand Up @@ -212,13 +212,13 @@ func (p *keysetPaginator[Node, Cur]) paginate(before *string, after *string, fir
return pageFrom(results, p.CountFunc, p.Cursorable, before, after, first, last)
}

// timeIDPaginator paginates results using a cursor with a time.Time and a persist.DBID.
// TimeIDPaginator paginates results using a cursor with a time.Time and a persist.DBID.
// By using the combination of a timestamp and a unique DBID for our ORDER BY clause,
// we can achieve fast keyset pagination results while avoiding edge cases when multiple
// rows have the same timestamp.
type timeIDPaginator[Node any] struct {
type TimeIDPaginator[Node any] struct {
// QueryFunc returns paginated results for the given paging parameters
QueryFunc func(params timeIDPagingParams) ([]Node, error)
QueryFunc func(params TimeIDPagingParams) ([]Node, error)

// CursorFunc returns a time and DBID that will be encoded into a cursor string
CursorFunc func(node Node) (time.Time, persist.DBID, error)
Expand All @@ -228,8 +228,8 @@ type timeIDPaginator[Node any] struct {
CountFunc func() (count int, err error)
}

// timeIDPagingParams are the parameters used to paginate with a time+DBID cursor
type timeIDPagingParams struct {
// TimeIDPagingParams are the parameters used to paginate with a time+DBID cursor
type TimeIDPagingParams struct {
Limit int32
CursorBeforeTime time.Time
CursorBeforeID persist.DBID
Expand All @@ -245,7 +245,7 @@ func withDefaultCursorTime(before, after time.Time) func(beforeCur, afterCur *ti
}
}

func (p *timeIDPaginator[Node]) paginate(before *string, after *string, first *int, last *int, opts ...func(beforeCur, afterCur *timeIDCursor)) ([]Node, PageInfo, error) {
func (p *TimeIDPaginator[Node]) paginate(before *string, after *string, first *int, last *int, opts ...func(beforeCur, afterCur *timeIDCursor)) ([]Node, PageInfo, error) {
queryFunc := func(limit int32, pagingForward bool) ([]Node, error) {
beforeCur := cursors.NewTimeIDCursor()
beforeCur.Time = defaultCursorBeforeTime
Expand All @@ -270,7 +270,7 @@ func (p *timeIDPaginator[Node]) paginate(before *string, after *string, first *i
}
}

queryParams := timeIDPagingParams{
queryParams := TimeIDPagingParams{
Limit: limit,
CursorBeforeTime: beforeCur.Time,
CursorBeforeID: beforeCur.ID,
Expand Down Expand Up @@ -368,7 +368,7 @@ func (p *boolIDFloatIDPaginator[Node]) paginate(before *string, after *string, f
return paginator.paginate(before, after, first, last)
}

type sharedFollowersPaginator[Node any] struct{ timeIDPaginator[Node] }
type sharedFollowersPaginator[Node any] struct{ TimeIDPaginator[Node] }

func (p *sharedFollowersPaginator[Node]) paginate(before *string, after *string, first *int, last *int) ([]Node, PageInfo, error) {
queryFunc := func(limit int32, pagingForward bool) ([]Node, error) {
Expand All @@ -393,7 +393,7 @@ func (p *sharedFollowersPaginator[Node]) paginate(before *string, after *string,
}
}

queryParams := timeIDPagingParams{
queryParams := TimeIDPagingParams{
Limit: limit,
CursorBeforeTime: beforeCur.Time,
CursorBeforeID: beforeCur.ID,
Expand Down
4 changes: 2 additions & 2 deletions publicapi/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (api TokenAPI) GetTokensBookmarkedByUserId(ctx context.Context, userID pers
if err := validatePaginationParams(api.validator, first, last); err != nil {
return nil, PageInfo{}, err
}
queryFunc := func(params timeIDPagingParams) ([]db.PaginateTokensAdmiredByUserIDBatchRow, error) {
queryFunc := func(params TimeIDPagingParams) ([]db.PaginateTokensAdmiredByUserIDBatchRow, error) {
return api.loaders.PaginateTokensAdmiredByUserIDBatch.Load(db.PaginateTokensAdmiredByUserIDBatchParams{
UserID: userID,
CurBeforeTime: params.CursorBeforeTime,
Expand All @@ -156,7 +156,7 @@ func (api TokenAPI) GetTokensBookmarkedByUserId(ctx context.Context, userID pers
return t.Admire.CreatedAt, t.Admire.ID, nil
}

paginator := timeIDPaginator[db.PaginateTokensAdmiredByUserIDBatchRow]{
paginator := TimeIDPaginator[db.PaginateTokensAdmiredByUserIDBatchRow]{
QueryFunc: queryFunc,
CursorFunc: cursorFunc,
CountFunc: countFunc,
Expand Down
Loading

0 comments on commit b48e258

Please sign in to comment.