forked from mtusi2026/mtusi2026lab4
192 lines
7.4 KiB
Go
192 lines
7.4 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/charawein/mtusi2026lab4/internal/domain"
|
|
"github.com/charawein/mtusi2026lab4/internal/repository"
|
|
)
|
|
|
|
type AttachmentRepository struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewAttachmentRepository(pool *pgxpool.Pool) *AttachmentRepository {
|
|
return &AttachmentRepository{pool: pool}
|
|
}
|
|
|
|
const attachmentColumns = `id, chat_id, owner_id, filename, object_key, status,
|
|
COALESCE(content_type, ''), COALESCE(size_bytes, 0), COALESCE(sha256, ''), created_at, updated_at`
|
|
|
|
func scanAttachment(row pgx.Row) (*domain.Attachment, error) {
|
|
var a domain.Attachment
|
|
err := row.Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ObjectKey, &a.Status,
|
|
&a.ContentType, &a.SizeBytes, &a.SHA256, &a.CreatedAt, &a.UpdatedAt)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, repository.ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &a, nil
|
|
}
|
|
|
|
// CreateIntent записывает намерение загрузки (status=uploading) ДО записи в S3.
|
|
func (r *AttachmentRepository) CreateIntent(ctx context.Context, a *domain.Attachment) error {
|
|
const q = `
|
|
INSERT INTO attachments (id, chat_id, owner_id, filename, object_key, status, created_at, updated_at)
|
|
VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5, 'uploading', $6, $6)
|
|
`
|
|
if _, err := r.pool.Exec(ctx, q, a.ID, a.ChatID, a.OwnerID, a.Filename, a.ObjectKey, a.CreatedAt); err != nil {
|
|
return fmt.Errorf("insert attachment intent: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// MarkReady переводит намерение в ready после успешного и проверенного PUT.
|
|
// Если строки uploading уже нет (её убрал reconciler) — ErrNotFound.
|
|
func (r *AttachmentRepository) MarkReady(ctx context.Context, a *domain.Attachment) error {
|
|
const q = `
|
|
UPDATE attachments
|
|
SET status = 'ready', content_type = $2, size_bytes = $3, sha256 = $4, updated_at = $5
|
|
WHERE id = $1::uuid AND status = 'uploading'
|
|
`
|
|
tag, err := r.pool.Exec(ctx, q, a.ID, a.ContentType, a.SizeBytes, a.SHA256, a.UpdatedAt)
|
|
if err != nil {
|
|
return fmt.Errorf("mark attachment ready: %w", err)
|
|
}
|
|
if tag.RowsAffected() != 1 {
|
|
return repository.ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteIntent убирает намерение, если загрузка не удалась (компенсация).
|
|
func (r *AttachmentRepository) DeleteIntent(ctx context.Context, id string) error {
|
|
const q = `DELETE FROM attachments WHERE id = $1::uuid AND status = 'uploading'`
|
|
if _, err := r.pool.Exec(ctx, q, id); err != nil {
|
|
return fmt.Errorf("delete attachment intent: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetForMember возвращает вложение, если пользователь — участник его чата.
|
|
// Чужое и несуществующее вложение неразличимы (ErrNotFound).
|
|
func (r *AttachmentRepository) GetForMember(ctx context.Context, id, userID string) (*domain.Attachment, error) {
|
|
q := `SELECT ` + attachmentColumns + `
|
|
FROM attachments a
|
|
WHERE a.id = $1::uuid
|
|
AND EXISTS (SELECT 1 FROM chat_members m WHERE m.chat_id = a.chat_id AND m.user_id = $2::uuid)`
|
|
a, err := scanAttachment(r.pool.QueryRow(ctx, q, id, userID))
|
|
if err != nil && !errors.Is(err, repository.ErrNotFound) {
|
|
return nil, fmt.Errorf("select attachment: %w", err)
|
|
}
|
|
return a, err
|
|
}
|
|
|
|
// --- для reconciler ---
|
|
|
|
// StreamByKey отдаёт все строки в байтовом порядке object_key (как ListObjectsV2).
|
|
func (r *AttachmentRepository) StreamByKey(ctx context.Context, prefix string, fn func(*domain.Attachment) error) error {
|
|
q := `SELECT ` + attachmentColumns + `
|
|
FROM attachments
|
|
WHERE starts_with(object_key, $1)
|
|
ORDER BY object_key COLLATE "C"`
|
|
rows, err := r.pool.Query(ctx, q, prefix)
|
|
if err != nil {
|
|
return fmt.Errorf("stream attachments: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
a, err := scanAttachment(rows)
|
|
if err != nil {
|
|
return fmt.Errorf("scan attachment: %w", err)
|
|
}
|
|
if err := fn(a); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return rows.Err()
|
|
}
|
|
|
|
// HasKey — есть ли строка с этим object_key (повторная проверка перед удалением объекта).
|
|
func (r *AttachmentRepository) HasKey(ctx context.Context, key string) (bool, error) {
|
|
var ok bool
|
|
err := r.pool.QueryRow(ctx, `SELECT EXISTS (SELECT 1 FROM attachments WHERE object_key = $1)`, key).Scan(&ok)
|
|
return ok, err
|
|
}
|
|
|
|
// DeleteStaleIntent удаляет намерение, только если оно всё ещё uploading и старше cutoff.
|
|
func (r *AttachmentRepository) DeleteStaleIntent(ctx context.Context, id string, cutoff time.Time) (bool, error) {
|
|
const q = `DELETE FROM attachments WHERE id = $1::uuid AND status = 'uploading' AND updated_at < $2`
|
|
tag, err := r.pool.Exec(ctx, q, id, cutoff)
|
|
return tag.RowsAffected() == 1, err
|
|
}
|
|
|
|
// SetStatus меняет статус ready↔missing (карантин и восстановление).
|
|
func (r *AttachmentRepository) SetStatus(ctx context.Context, id, from, to string) (bool, error) {
|
|
const q = `UPDATE attachments SET status = $3, updated_at = now() WHERE id = $1::uuid AND status = $2`
|
|
tag, err := r.pool.Exec(ctx, q, id, from, to)
|
|
return tag.RowsAffected() == 1, err
|
|
}
|
|
|
|
// DeleteUnreferencedMissing удаляет запись карантина старше cutoff, если на неё
|
|
// не ссылается ни одно сообщение. Блокировка строки (FOR UPDATE) конфликтует с
|
|
// FOR SHARE при создании сообщения, поэтому гонки «удалили, пока прикрепляли» нет.
|
|
func (r *AttachmentRepository) DeleteUnreferencedMissing(ctx context.Context, id string, cutoff time.Time) (bool, error) {
|
|
tx, err := r.pool.Begin(ctx)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer func() { _ = tx.Rollback(ctx) }()
|
|
var locked string
|
|
err = tx.QueryRow(ctx, `SELECT id FROM attachments WHERE id = $1::uuid AND status = 'missing' AND updated_at < $2 FOR UPDATE`, id, cutoff).Scan(&locked)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return false, nil
|
|
}
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
var referenced bool
|
|
if err := tx.QueryRow(ctx, `SELECT EXISTS (SELECT 1 FROM message_attachments WHERE attachment_id = $1::uuid)`, id).Scan(&referenced); err != nil {
|
|
return false, err
|
|
}
|
|
if referenced {
|
|
return false, nil
|
|
}
|
|
if _, err := tx.Exec(ctx, `DELETE FROM attachments WHERE id = $1::uuid`, id); err != nil {
|
|
return false, err
|
|
}
|
|
return true, tx.Commit(ctx)
|
|
}
|
|
|
|
// IsReferenced — используется ли вложение каким-либо сообщением.
|
|
func (r *AttachmentRepository) IsReferenced(ctx context.Context, id string) (bool, error) {
|
|
var ok bool
|
|
err := r.pool.QueryRow(ctx, `SELECT EXISTS (SELECT 1 FROM message_attachments WHERE attachment_id = $1::uuid)`, id).Scan(&ok)
|
|
return ok, err
|
|
}
|
|
|
|
// TryLock — advisory lock, чтобы одновременно работал один reconciler.
|
|
func (r *AttachmentRepository) TryLock(ctx context.Context) (func(), bool, error) {
|
|
conn, err := r.pool.Acquire(ctx)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
var ok bool
|
|
if err := conn.QueryRow(ctx, `SELECT pg_try_advisory_lock(4242004)`).Scan(&ok); err != nil || !ok {
|
|
conn.Release()
|
|
return nil, false, err
|
|
}
|
|
return func() {
|
|
_, _ = conn.Exec(context.Background(), `SELECT pg_advisory_unlock(4242004)`)
|
|
conn.Release()
|
|
}, true, nil
|
|
}
|