merged from gramsrv upstream
This commit is contained in:
parent
79c64ee916
commit
21a0856587
651 changed files with 54774 additions and 4590 deletions
|
|
@ -9,12 +9,44 @@ import (
|
|||
|
||||
// MessageBoxCounterSource 从 message_boxes durable log 恢复某 owner 的当前最大 box_id。
|
||||
type MessageBoxCounterSource struct {
|
||||
q *sqlcgen.Queries
|
||||
db sqlcgen.DBTX
|
||||
q *sqlcgen.Queries
|
||||
}
|
||||
|
||||
// NewMessageBoxCounterSource 创建 Redis BoxIDAllocator 的 PG 恢复源。
|
||||
func NewMessageBoxCounterSource(db sqlcgen.DBTX) *MessageBoxCounterSource {
|
||||
return &MessageBoxCounterSource{q: sqlcgen.New(db)}
|
||||
return &MessageBoxCounterSource{db: db, q: sqlcgen.New(db)}
|
||||
}
|
||||
|
||||
func (s *MessageBoxCounterSource) CurrentBatch(ctx context.Context, userIDs []int64) (map[int64]int, error) {
|
||||
if len(userIDs) == 0 {
|
||||
return map[int64]int{}, nil
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `
|
||||
SELECT requested.user_id, COALESCE(MAX(m.box_id), 0)::integer
|
||||
FROM unnest($1::bigint[]) AS requested(user_id)
|
||||
LEFT JOIN message_boxes m ON m.owner_user_id = requested.user_id
|
||||
GROUP BY requested.user_id`, userIDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("batch max message box id: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make(map[int64]int, len(userIDs))
|
||||
for rows.Next() {
|
||||
var userID int64
|
||||
var current int
|
||||
if err := rows.Scan(&userID, ¤t); err != nil {
|
||||
return nil, fmt.Errorf("scan batch max message box id: %w", err)
|
||||
}
|
||||
out[userID] = current
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate batch max message box id: %w", err)
|
||||
}
|
||||
if len(out) != len(userIDs) {
|
||||
return nil, fmt.Errorf("batch max message box id: returned %d of %d counters", len(out), len(userIDs))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *MessageBoxCounterSource) Current(ctx context.Context, userID int64) (int, error) {
|
||||
|
|
@ -43,22 +75,19 @@ func (s *ChannelIDCounterSource) Current(ctx context.Context, _ int64) (int, err
|
|||
return id, nil
|
||||
}
|
||||
|
||||
// SecretChatIDCounterSource 从 secret_chats 表恢复全局 secret chat id(迁移 0137)。
|
||||
type SecretChatIDCounterSource struct {
|
||||
db sqlcgen.DBTX
|
||||
}
|
||||
|
||||
// NewSecretChatIDCounterSource 创建 Redis SecretChatIDAllocator 的 PG 恢复源。
|
||||
func NewSecretChatIDCounterSource(db sqlcgen.DBTX) *SecretChatIDCounterSource {
|
||||
return &SecretChatIDCounterSource{db: db}
|
||||
}
|
||||
|
||||
func (s *SecretChatIDCounterSource) Current(ctx context.Context, _ int64) (int, error) {
|
||||
var id int
|
||||
if err := s.db.QueryRow(ctx, `SELECT COALESCE(MAX(chat_id), 0) FROM secret_chats`).Scan(&id); err != nil {
|
||||
return 0, fmt.Errorf("max secret chat id: %w", err)
|
||||
func (s *ChannelIDCounterSource) CurrentBatch(ctx context.Context, userIDs []int64) (map[int64]int, error) {
|
||||
if len(userIDs) == 0 {
|
||||
return map[int64]int{}, nil
|
||||
}
|
||||
return id, nil
|
||||
current, err := s.Current(ctx, 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make(map[int64]int, len(userIDs))
|
||||
for _, userID := range userIDs {
|
||||
out[userID] = current
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ChannelMessageIDCounterSource 从 channel_messages 恢复某 channel 的当前最大 message id。
|
||||
|
|
@ -77,3 +106,34 @@ func (s *ChannelMessageIDCounterSource) Current(ctx context.Context, channelID i
|
|||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (s *ChannelMessageIDCounterSource) CurrentBatch(ctx context.Context, channelIDs []int64) (map[int64]int, error) {
|
||||
if len(channelIDs) == 0 {
|
||||
return map[int64]int{}, nil
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `
|
||||
SELECT requested.channel_id, COALESCE(MAX(m.id), 0)::integer
|
||||
FROM unnest($1::bigint[]) AS requested(channel_id)
|
||||
LEFT JOIN channel_messages m ON m.channel_id = requested.channel_id
|
||||
GROUP BY requested.channel_id`, channelIDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("batch max channel message id: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make(map[int64]int, len(channelIDs))
|
||||
for rows.Next() {
|
||||
var channelID int64
|
||||
var current int
|
||||
if err := rows.Scan(&channelID, ¤t); err != nil {
|
||||
return nil, fmt.Errorf("scan batch max channel message id: %w", err)
|
||||
}
|
||||
out[channelID] = current
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate batch max channel message id: %w", err)
|
||||
}
|
||||
if len(out) != len(channelIDs) {
|
||||
return nil, fmt.Errorf("batch max channel message id: returned %d of %d counters", len(out), len(channelIDs))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue