Skip to content

Commit

Permalink
Refactor cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrel-b committed Aug 21, 2023
1 parent d61f60b commit accd0a4
Show file tree
Hide file tree
Showing 7 changed files with 607 additions and 578 deletions.
17 changes: 7 additions & 10 deletions publicapi/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,16 @@ func (api CollectionAPI) GetTopCollectionsForCommunity(ctx context.Context, chai
}

var collectionIDs []persist.DBID
var cursor positionCursor
var paginator positionPaginator

// If a cursor is provided, we can skip querying the cache
if before != nil {
if _, collectionIDs, err = paginator.decodeCursor(*before); err != nil {
if err = cursor.Unpack(*before); err != nil {
return nil, pageInfo, err
}
} else if after != nil {
if _, collectionIDs, err = paginator.decodeCursor(*after); err != nil {
if err = cursor.Unpack(*after); err != nil {
return nil, pageInfo, err
}
} else {
Expand All @@ -143,19 +144,15 @@ func (api CollectionAPI) GetTopCollectionsForCommunity(ctx context.Context, chai
}

paginator.QueryFunc = func(params positionPagingParams) ([]any, error) {
cIDs, _ := util.Map(collectionIDs, func(id persist.DBID) (string, error) {
return id.String(), nil
})
cIDs := util.MapWithoutError(collectionIDs, func(id persist.DBID) string { return id.String() })
c, err := api.queries.GetVisibleCollectionsByIDsPaginate(ctx, db.GetVisibleCollectionsByIDsPaginateParams{
CollectionIds: cIDs,
CurBeforePos: params.CursorBeforePos,
CurAfterPos: params.CursorAfterPos,
PagingForward: params.PagingForward,
Limit: params.Limit,
})
a, _ := util.Map(c, func(c db.Collection) (any, error) {
return c, nil
})
a := util.MapWithoutError(c, func(c db.Collection) any { return c })
return a, err
}

Expand All @@ -164,8 +161,8 @@ func (api CollectionAPI) GetTopCollectionsForCommunity(ctx context.Context, chai
posLookup[id] = i + 1 // Postgres uses 1-based indexing
}

paginator.CursorFunc = func(node any) (int, []persist.DBID, error) {
return posLookup[node.(db.Collection).ID], collectionIDs, nil
paginator.CursorFunc = func(node any) (int64, []persist.DBID, error) {
return int64(posLookup[node.(db.Collection).ID]), collectionIDs, nil
}

// The collections are sorted by ascending rank so we need to switch the cursor positions
Expand Down
Loading

0 comments on commit accd0a4

Please sign in to comment.