forked from mtusi2026/mtusi2026lab2
perf(pagination): split list queries into first-page and after-cursor variants
This commit is contained in:
+46
-33
@@ -5,7 +5,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
@@ -27,7 +26,8 @@ type chatQueries interface {
|
||||
AddChatMembers(ctx context.Context, arg db.AddChatMembersParams) error
|
||||
CreateChat(ctx context.Context, arg db.CreateChatParams) (db.Chat, error)
|
||||
GetChat(ctx context.Context, arg db.GetChatParams) (db.GetChatRow, error)
|
||||
ListChats(ctx context.Context, arg db.ListChatsParams) ([]db.ListChatsRow, error)
|
||||
ListChatsAfter(ctx context.Context, arg db.ListChatsAfterParams) ([]db.ListChatsAfterRow, error)
|
||||
ListChatsFirstPage(ctx context.Context, arg db.ListChatsFirstPageParams) ([]db.ListChatsFirstPageRow, error)
|
||||
}
|
||||
|
||||
type txBeginner interface {
|
||||
@@ -142,48 +142,61 @@ func (s *ChatService) List(
|
||||
limit int,
|
||||
cursor *string,
|
||||
) ([]model.Chat, *string, error) {
|
||||
var afterCreatedAt *time.Time
|
||||
var afterID *uuid.UUID
|
||||
var chats []model.Chat
|
||||
|
||||
if cursor != nil {
|
||||
decoded, err := decodeCursor(*cursor, uuid.Nil)
|
||||
if cursor == nil {
|
||||
rows, err := s.q.ListChatsFirstPage(ctx, db.ListChatsFirstPageParams{
|
||||
UserID: userID,
|
||||
Lim: int32(limit + 1),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
chats = make([]model.Chat, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
chats = append(chats, model.Chat{
|
||||
ID: r.ID,
|
||||
Title: r.Title,
|
||||
CreatedAt: r.CreatedAt,
|
||||
MemberIDs: r.MemberIds,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
decoded, derr := decodeCursor(*cursor, uuid.Nil)
|
||||
if derr != nil {
|
||||
return nil, nil, ErrInvalidCursor
|
||||
}
|
||||
|
||||
afterCreatedAt = &decoded.CreatedAt
|
||||
afterID = &decoded.ID
|
||||
}
|
||||
|
||||
rows, err := s.q.ListChats(ctx, db.ListChatsParams{
|
||||
UserID: userID,
|
||||
AfterCreatedAt: afterCreatedAt,
|
||||
AfterID: afterID,
|
||||
Lim: int32(limit + 1),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
hasMore := len(rows) > limit
|
||||
if hasMore {
|
||||
rows = rows[:limit]
|
||||
}
|
||||
|
||||
chats := make([]model.Chat, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
chats = append(chats, model.Chat{
|
||||
ID: r.ID,
|
||||
Title: r.Title,
|
||||
CreatedAt: r.CreatedAt,
|
||||
MemberIDs: r.MemberIds,
|
||||
rows, err := s.q.ListChatsAfter(ctx, db.ListChatsAfterParams{
|
||||
UserID: userID,
|
||||
AfterCreatedAt: decoded.CreatedAt,
|
||||
AfterID: decoded.ID,
|
||||
Lim: int32(limit + 1),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
chats = make([]model.Chat, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
chats = append(chats, model.Chat{
|
||||
ID: r.ID,
|
||||
Title: r.Title,
|
||||
CreatedAt: r.CreatedAt,
|
||||
MemberIDs: r.MemberIds,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
hasMore := len(chats) > limit
|
||||
if hasMore {
|
||||
chats = chats[:limit]
|
||||
}
|
||||
|
||||
var next *string
|
||||
if hasMore {
|
||||
last := chats[len(chats)-1]
|
||||
|
||||
c := encodeCursor(Cursor{
|
||||
CreatedAt: last.CreatedAt,
|
||||
ID: last.ID,
|
||||
|
||||
Reference in New Issue
Block a user