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,
|
||||
|
||||
+17
-16
@@ -3,7 +3,6 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
@@ -17,7 +16,8 @@ var ErrMessageChatNotFound = errors.New("chat not found")
|
||||
type messageQueries interface {
|
||||
CreateMessage(ctx context.Context, arg db.CreateMessageParams) (db.Message, error)
|
||||
IsChatMember(ctx context.Context, arg db.IsChatMemberParams) (bool, error)
|
||||
ListMessages(ctx context.Context, arg db.ListMessagesParams) ([]db.Message, error)
|
||||
ListMessagesAfter(ctx context.Context, arg db.ListMessagesAfterParams) ([]db.Message, error)
|
||||
ListMessagesFirstPage(ctx context.Context, arg db.ListMessagesFirstPageParams) ([]db.Message, error)
|
||||
}
|
||||
|
||||
type MessageService struct {
|
||||
@@ -79,25 +79,26 @@ func (s *MessageService) List(
|
||||
return nil, nil, ErrMessageChatNotFound
|
||||
}
|
||||
|
||||
var afterCreatedAt *time.Time
|
||||
var afterID *uuid.UUID
|
||||
var rows []db.Message
|
||||
|
||||
if cursor != nil {
|
||||
decoded, err := decodeCursor(*cursor, chatID)
|
||||
if err != nil {
|
||||
if cursor == nil {
|
||||
rows, err = s.q.ListMessagesFirstPage(ctx, db.ListMessagesFirstPageParams{
|
||||
ChatID: chatID,
|
||||
Lim: int32(limit + 1),
|
||||
})
|
||||
} else {
|
||||
decoded, derr := decodeCursor(*cursor, chatID)
|
||||
if derr != nil {
|
||||
return nil, nil, ErrInvalidCursor
|
||||
}
|
||||
|
||||
afterCreatedAt = &decoded.CreatedAt
|
||||
afterID = &decoded.ID
|
||||
rows, err = s.q.ListMessagesAfter(ctx, db.ListMessagesAfterParams{
|
||||
ChatID: chatID,
|
||||
AfterCreatedAt: decoded.CreatedAt,
|
||||
AfterID: decoded.ID,
|
||||
Lim: int32(limit + 1),
|
||||
})
|
||||
}
|
||||
|
||||
rows, err := s.q.ListMessages(ctx, db.ListMessagesParams{
|
||||
ChatID: chatID,
|
||||
AfterCreatedAt: afterCreatedAt,
|
||||
AfterID: afterID,
|
||||
Lim: int32(limit + 1),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user