forked from mtusi2026/mtusi2026lab6
243 lines
10 KiB
Go
243 lines
10 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/charawein/mtusi2026lab6/internal/domain"
|
|
"github.com/charawein/mtusi2026lab6/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,
|
|
COALESCE(error_code, ''), pipeline_version, COALESCE(staging_key, '')`
|
|
|
|
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,
|
|
&a.ErrorCode, &a.PipelineVersion, &a.StagingKey)
|
|
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, staging_key, status, created_at, updated_at)
|
|
VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5, $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
|
|
}
|
|
|
|
// MarkQueued — точка 202. В ОДНОЙ транзакции: метаданные проверенного оригинала
|
|
// и status=queued, логическое задание (jobs) и сообщение для broker (outbox).
|
|
// После COMMIT работа не теряется: даже если broker недоступен или relay упал,
|
|
// outbox будет опубликован позже. Если строки uploading уже нет — ErrNotFound.
|
|
func (r *AttachmentRepository) MarkQueued(ctx context.Context, a *domain.Attachment, pipelineVersion, maxAttempts int) (string, error) {
|
|
tx, err := r.pool.Begin(ctx)
|
|
if err != nil {
|
|
return "", fmt.Errorf("begin tx: %w", err)
|
|
}
|
|
defer func() { _ = tx.Rollback(ctx) }()
|
|
const q = `
|
|
UPDATE attachments
|
|
SET status = 'queued', content_type = $2, size_bytes = $3, sha256 = $4, updated_at = $5
|
|
WHERE id = $1::uuid AND status = 'uploading'
|
|
`
|
|
tag, err := tx.Exec(ctx, q, a.ID, a.ContentType, a.SizeBytes, a.SHA256, a.UpdatedAt)
|
|
if err != nil {
|
|
return "", fmt.Errorf("mark attachment queued: %w", err)
|
|
}
|
|
if tag.RowsAffected() != 1 {
|
|
return "", repository.ErrNotFound
|
|
}
|
|
jobID, err := EnqueueJobTx(ctx, tx, a.ID, pipelineVersion, maxAttempts, "upload")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return "", fmt.Errorf("commit tx: %w", err)
|
|
}
|
|
return jobID, nil
|
|
}
|
|
|
|
// Variants — варианты указанной версии pipeline.
|
|
func (r *AttachmentRepository) Variants(ctx context.Context, attachmentID string, version int) ([]domain.Variant, error) {
|
|
const q = `
|
|
SELECT name, content_type, size_bytes, width, height, sha256, object_key, pipeline_version
|
|
FROM attachment_variants
|
|
WHERE attachment_id = $1::uuid AND pipeline_version = $2
|
|
ORDER BY CASE name WHEN 'thumbnail' THEN 1 WHEN 'crop' THEN 2 ELSE 3 END
|
|
`
|
|
rows, err := r.pool.Query(ctx, q, attachmentID, version)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("select variants: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
out := []domain.Variant{}
|
|
for rows.Next() {
|
|
var v domain.Variant
|
|
if err := rows.Scan(&v.Name, &v.ContentType, &v.SizeBytes, &v.Width, &v.Height, &v.SHA256, &v.ObjectKey, &v.PipelineVersion); err != nil {
|
|
return nil, fmt.Errorf("scan variant: %w", err)
|
|
}
|
|
out = append(out, v)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// 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 отдаёт все известные БД ключи объектов в байтовом порядке (как
|
|
// ListObjectsV2): оригинал вложения (object_key), отдельный staging-объект и
|
|
// объекты вариантов. Для staging и вариантов Status — «staging»/«variant», чтобы
|
|
// reconciler не путал их с оригиналом.
|
|
func (r *AttachmentRepository) StreamByKey(ctx context.Context, prefix string, fn func(*domain.Attachment) error) error {
|
|
const q = `
|
|
SELECT key, id::text, status, size_bytes, updated_at FROM (
|
|
SELECT object_key AS key, id, status, COALESCE(size_bytes, 0) AS size_bytes, updated_at FROM attachments
|
|
UNION ALL
|
|
SELECT staging_key, id, 'staging', COALESCE(size_bytes, 0), updated_at FROM attachments
|
|
WHERE staging_key IS NOT NULL AND staging_key <> object_key
|
|
UNION ALL
|
|
SELECT object_key, attachment_id, 'variant', size_bytes, created_at FROM attachment_variants
|
|
) k
|
|
WHERE starts_with(key, $1)
|
|
ORDER BY key COLLATE "C"`
|
|
rows, err := r.pool.Query(ctx, q, prefix)
|
|
if err != nil {
|
|
return fmt.Errorf("stream object keys: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var a domain.Attachment
|
|
if err := rows.Scan(&a.ObjectKey, &a.ID, &a.Status, &a.SizeBytes, &a.UpdatedAt); err != nil {
|
|
return fmt.Errorf("scan object key: %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 OR staging_key = $1)
|
|
OR EXISTS (SELECT 1 FROM attachment_variants 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
|
|
}
|