mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-08 18:37:09 +02:00
* refactor(persistence): adopt generic deluan/rest repository API Pin deluan/rest to the refactor branch. REST-facing repository methods take a context and return typed values. Drop DataStore.Resource and ResourceRepository; the native API names typed repositories directly through a per-request adapter that later commits remove. * refactor(persistence): base repository helpers take a context * refactor(persistence): LibraryRepository takes a context per call * refactor(persistence): PropertyRepository takes a context per call * refactor(persistence): UserPropsRepository takes a context per call * refactor(persistence): TranscodingRepository takes a context per call * refactor(persistence): ShareRepository takes a context per call * refactor(persistence): PlayerRepository takes a context per call * refactor(persistence): RadioRepository takes a context per call * refactor(persistence): PlayQueueRepository takes a context per call * refactor(persistence): Tag and Genre repositories take a context per call * refactor(persistence): PluginRepository takes a context per call * refactor(persistence): Scrobble repositories take a context per call * refactor(persistence): FolderRepository takes a context per call * refactor(persistence): Artwork repositories take a context per call * refactor(persistence): UserRepository takes a context per call * refactor(persistence): ArtistRepository takes a context per call ReadAll no longer rewrites the shared sort mappings for the role filter; it works on a per-call copy. * test(persistence): assert artist role sort sanitization in ReadAll * refactor(persistence): AlbumRepository takes a context per call * test(persistence): pass the test context to album repository helpers * refactor(persistence): MediaFileRepository takes a context per call * refactor(persistence): Playlist repositories take a context per call * refactor(persistence): build all repositories once per store * refactor(core): REST repository wrappers are built once * refactor(persistence): repositories are stateless Remove the context field from the base repository and the per-request REST adapter. Enable the containedctx linter so no repository can hold a request context again. * chore(lint): skip containedctx in test files * refactor: share simplifications from the stateless repositories sweep Add deleteOwnedAll on sqlRepository and use it in player/share Delete to remove the duplicated bulk-delete loop; have Share.Repository() return model.ShareRepository so subsonic sharing.go drops its repeated type assertions. * chore(core): assert REST wrappers implement Persistable * chore: reformat imports * perf(persistence): build repositories on first use Each transaction store used to construct all 21 repositories up front, paying for filter and sort mapping setup the block never touched. Fields are now sync.OnceValue thunks, so a store only builds what it uses. * fix(persistence): clean plugin references per deleted user A bulk user delete that fails on a later id had already removed the earlier rows but skipped their plugin cleanup. Cleanup now runs right after each successful delete. * fix(core): unload disabled plugins even when a user delete fails A bulk delete can fail on a later id after earlier users were removed and their plugins auto-disabled. The wrapper returned before unloading, leaving those plugins running until the next successful delete or a restart. * chore(deps): pin deluan/rest to v1.0.1 Replaces the pseudo-version of the refactor branch with the tagged release. REST error messages now name the bare type (Artist, not model.Artist). * test: use the spec context instead of context.Background() Replace the context.Background()/context.TODO() calls this branch added to tests with the spec's ctx, GinkgoT().Context(), or t/b.Context(), so repository calls are bound to the running spec's lifetime. * test: declare the spec context once per Describe Set ctx from GinkgoT().Context() first in each top-level BeforeEach and reuse it, building user contexts on top of it instead of repeating inline calls.
235 lines
9.2 KiB
Go
235 lines
9.2 KiB
Go
package persistence
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
. "github.com/Masterminds/squirrel"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/utils/slice"
|
|
"github.com/pocketbase/dbx"
|
|
)
|
|
|
|
// Keeps each multi-row insert under SQLite's bind-variable limit (at most 7 vars per row).
|
|
const enqueueChunkSize = 100
|
|
|
|
// Every insert writes these, in this order; the INSERT..SELECT forms must project them to match.
|
|
// DequeueBatch also selects exactly these, to leave the drain's rows free of the trace it never reads.
|
|
var enqueueColumns = []string{"item_kind", "item_id", "image_type", "priority", "attempts", "retry_at", "enqueued_at"}
|
|
|
|
type artworkQueueRepository struct {
|
|
sqlRepository
|
|
}
|
|
|
|
func NewArtworkQueueRepository(db dbx.Builder) model.ArtworkQueueRepository {
|
|
r := &artworkQueueRepository{}
|
|
r.db = db
|
|
r.tableName = "artwork_queue"
|
|
return r
|
|
}
|
|
|
|
func (r *artworkQueueRepository) Get(ctx context.Context, kind model.Kind, id, imageType string) (*model.ArtworkQueueItem, error) {
|
|
var res model.ArtworkQueueItem
|
|
err := r.queryOne(ctx, Select("*").From(r.tableName).
|
|
Where(Eq{"item_kind": kind.Prefix(), "item_id": id, "image_type": imageType}), &res)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &res, nil
|
|
}
|
|
|
|
// Enqueue starts a fresh lifecycle: it resets enqueued_at (so a fresh request does not inherit an old
|
|
// row's spent retry budget) and clears trace (so explain does not show a prior failure at attempts 0).
|
|
func (r *artworkQueueRepository) Enqueue(ctx context.Context, items ...model.ArtworkQueueItem) error {
|
|
return r.enqueue(ctx, `ON CONFLICT (item_kind, item_id, image_type) DO UPDATE SET
|
|
priority = MAX(priority, excluded.priority), retry_at = excluded.retry_at,
|
|
attempts = 0, enqueued_at = excluded.enqueued_at, trace = '[]'`, items)
|
|
}
|
|
|
|
func (r *artworkQueueRepository) EnqueuePreservingBackoff(ctx context.Context, items ...model.ArtworkQueueItem) error {
|
|
return r.enqueue(ctx, `ON CONFLICT (item_kind, item_id, image_type) DO UPDATE SET
|
|
priority = MAX(priority, excluded.priority)`, items)
|
|
}
|
|
|
|
func (r *artworkQueueRepository) EnqueueAllMissing(ctx context.Context, kind model.Kind, priority int) (int64, error) {
|
|
entityTable, ok := artworkOwnerTables[kind]
|
|
if !ok {
|
|
return 0, fmt.Errorf("artwork queue: no entity table for kind %q", kind.Prefix())
|
|
}
|
|
now := time.Now()
|
|
return r.insertIfNotQueued(ctx, "", `SELECT ?, id, ?, ?, 0, ?, ?
|
|
FROM `+entityTable+`
|
|
WHERE id NOT IN (SELECT item_id FROM `+itemArtworkTable+` WHERE item_kind = ?)`,
|
|
kind.Prefix(), model.ImageTypePrimary, priority, now, now, kind.Prefix())
|
|
}
|
|
|
|
func (r *artworkQueueRepository) EnqueueIfMissing(ctx context.Context, items ...model.ArtworkQueueItem) error {
|
|
now := time.Now()
|
|
for chunk := range slices.Chunk(items, enqueueChunkSize) {
|
|
rows := make([]string, 0, len(chunk))
|
|
args := make([]any, 0, len(chunk)*4+2)
|
|
for _, it := range chunk {
|
|
rows = append(rows, "(?,?,?,?)")
|
|
args = append(args, it.ItemKind, it.ItemID, cmp.Or(it.ImageType, model.ImageTypePrimary), it.Priority)
|
|
}
|
|
args = append(args, now, now)
|
|
_, err := r.insertIfNotQueued(ctx,
|
|
`WITH new_items(item_kind, item_id, image_type, priority) AS (VALUES `+strings.Join(rows, ",")+`) `,
|
|
`SELECT n.item_kind, n.item_id, n.image_type, n.priority, 0, ?, ?
|
|
FROM new_items n
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM `+itemArtworkTable+` ia
|
|
WHERE ia.item_kind = n.item_kind AND ia.item_id = n.item_id AND ia.image_type = n.image_type)`,
|
|
args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DO NOTHING is deliberate: a recheck must not bump the priority or retry_at of an already-queued item.
|
|
const skipIfQueued = ` ON CONFLICT (item_kind, item_id, image_type) DO NOTHING`
|
|
|
|
// insertIfNotQueued inserts the rows selected by the given SQL, optionally prefixed by a CTE.
|
|
func (r *artworkQueueRepository) insertIfNotQueued(ctx context.Context, with, sql string, args ...any) (int64, error) {
|
|
return r.executeSQL(ctx, Expr(with+`INSERT INTO `+r.tableName+
|
|
` (`+strings.Join(enqueueColumns, ", ")+`) `+sql+skipIfQueued, args...))
|
|
}
|
|
|
|
// artworkSourceFilter selects item_artwork rows of a kind; no sources means every source, "" the
|
|
// absent state, and ArtworkSourceFailed the absent states that gave up. Several are a union, so
|
|
// asking for both absent and failed is just absent.
|
|
func artworkSourceFilter(kind model.Kind, sources []string) Sqlizer {
|
|
f := And{Eq{"item_kind": kind.Prefix()}}
|
|
if len(sources) == 0 {
|
|
return f
|
|
}
|
|
stored := slices.DeleteFunc(slices.Clone(sources), func(s string) bool { return s == model.ArtworkSourceFailed })
|
|
var match Or
|
|
if len(stored) > 0 {
|
|
match = append(match, Eq{"source": stored})
|
|
}
|
|
if len(stored) != len(sources) {
|
|
match = append(match, And{Eq{"hash": ""}, NotEq{"last_failure": ""}})
|
|
}
|
|
return append(f, match)
|
|
}
|
|
|
|
func (r *artworkQueueRepository) CountBySource(ctx context.Context, kind model.Kind, sources []string) (int64, error) {
|
|
var res struct{ Count int64 }
|
|
err := r.queryOne(ctx, Select("count(*) as count").From(itemArtworkTable).
|
|
Where(artworkSourceFilter(kind, sources)), &res)
|
|
return res.Count, err
|
|
}
|
|
|
|
func (r *artworkQueueRepository) SourcesInUse(ctx context.Context, kind model.Kind) ([]string, error) {
|
|
var res []struct{ Source string }
|
|
err := r.queryAll(ctx, Select("distinct source").From(itemArtworkTable).
|
|
Where(Eq{"item_kind": kind.Prefix()}), &res)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return slice.Map(res, func(s struct{ Source string }) string { return s.Source }), nil
|
|
}
|
|
|
|
// EnqueueBySource deliberately leaves item_artwork alone: clearing state in bulk would blank the
|
|
// library's artwork until every item is resolved again.
|
|
func (r *artworkQueueRepository) EnqueueBySource(ctx context.Context, kind model.Kind, sources []string, priority int) (int64, error) {
|
|
now := time.Now()
|
|
sel := Select("item_kind", "item_id", "image_type").
|
|
Column(Expr("?", priority)).Column("0").Column(Expr("?", now)).Column(Expr("?", now)).
|
|
From(itemArtworkTable).Where(artworkSourceFilter(kind, sources))
|
|
return r.executeSQL(ctx, Insert(r.tableName).Columns(enqueueColumns...).Select(sel).Suffix(skipIfQueued))
|
|
}
|
|
|
|
func (r *artworkQueueRepository) enqueue(ctx context.Context, conflict string, items []model.ArtworkQueueItem) error {
|
|
now := time.Now()
|
|
for chunk := range slices.Chunk(items, enqueueChunkSize) {
|
|
ins := Insert(r.tableName).Columns(enqueueColumns...)
|
|
for _, it := range chunk {
|
|
ins = ins.Values(it.ItemKind, it.ItemID, cmp.Or(it.ImageType, model.ImageTypePrimary), it.Priority, 0, now, now)
|
|
}
|
|
ins = ins.Suffix(conflict)
|
|
if _, err := r.executeSQL(ctx, ins); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *artworkQueueRepository) DequeueBatch(ctx context.Context, n int, kinds ...string) ([]model.ArtworkQueueItem, error) {
|
|
sel := Select(enqueueColumns...).From(r.tableName).
|
|
Where(LtOrEq{"retry_at": time.Now()}).
|
|
OrderBy("priority DESC", "enqueued_at ASC").
|
|
Limit(uint64(n))
|
|
if len(kinds) > 0 {
|
|
sel = sel.Where(Eq{"item_kind": kinds})
|
|
}
|
|
var res []model.ArtworkQueueItem
|
|
err := r.queryAll(ctx, sel, &res)
|
|
return res, err
|
|
}
|
|
|
|
func (r *artworkQueueRepository) MarkFailedIfUnchanged(ctx context.Context, kind, id, imageType string, seenRetryAt, retryAt time.Time, trace string) error {
|
|
upd := Update(r.tableName).
|
|
Set("attempts", Expr("attempts + 1")).
|
|
Set("retry_at", retryAt).
|
|
Set("trace", trace).
|
|
Where(Eq{"item_kind": kind, "item_id": id, "image_type": imageType, "retry_at": seenRetryAt})
|
|
_, err := r.executeSQL(ctx, upd)
|
|
return err
|
|
}
|
|
|
|
func (r *artworkQueueRepository) DeleteIfUnchanged(ctx context.Context, kind, id, imageType string, retryAt time.Time) error {
|
|
return r.delete(ctx, Eq{"item_kind": kind, "item_id": id, "image_type": imageType, "retry_at": retryAt})
|
|
}
|
|
|
|
func (r *artworkQueueRepository) PurgeDangling(ctx context.Context) (int64, error) {
|
|
return purgeDangling(ctx, r.sqlRepository)
|
|
}
|
|
|
|
// artworkQueueFilter returns no conditions for an empty filter, so an unfiltered DELETE keeps
|
|
// SQLite's truncate path. It ignores retry_at: a backing-off row is pending work too.
|
|
func artworkQueueFilter(kinds []model.Kind, priorities []int) And {
|
|
var f And
|
|
if len(kinds) > 0 {
|
|
f = append(f, Eq{"item_kind": model.KindPrefixes(kinds)})
|
|
}
|
|
if len(priorities) > 0 {
|
|
f = append(f, Eq{"priority": priorities})
|
|
}
|
|
return f
|
|
}
|
|
|
|
// CountQueued shares its filter with PurgeQueued, so a preview cannot count rows the delete misses.
|
|
func (r *artworkQueueRepository) CountQueued(ctx context.Context, kinds []model.Kind, priorities []int) ([]model.ArtworkQueueStat, error) {
|
|
sel := Select("item_kind", "priority", "count(*) as count").From(r.tableName).
|
|
GroupBy("item_kind", "priority").OrderBy("item_kind", "priority desc")
|
|
if f := artworkQueueFilter(kinds, priorities); len(f) > 0 {
|
|
sel = sel.Where(f)
|
|
}
|
|
var res []model.ArtworkQueueStat
|
|
err := r.queryAll(ctx, sel, &res)
|
|
return res, err
|
|
}
|
|
|
|
func (r *artworkQueueRepository) PurgeQueued(ctx context.Context, kinds []model.Kind, priorities []int) (int64, error) {
|
|
del := Delete(r.tableName)
|
|
if f := artworkQueueFilter(kinds, priorities); len(f) > 0 {
|
|
del = del.Where(f)
|
|
}
|
|
return r.executeSQL(ctx, del)
|
|
}
|
|
|
|
func (r *artworkQueueRepository) Count(ctx context.Context) (int64, error) {
|
|
var res struct{ Count int64 }
|
|
err := r.queryOne(ctx, Select("count(*) as count").From(r.tableName), &res)
|
|
return res.Count, err
|
|
}
|
|
|
|
var _ model.ArtworkQueueRepository = (*artworkQueueRepository)(nil)
|