navidrome/tests/mock_radio_repository.go
Deluan 9ce51cf575 perf(artwork): fetch only IDs for backfill enumeration
Backfill enumerated every album, artist, playlist and radio via GetAll
and mapped out just the ID. GetAll materializes full entities (library
joins, participant/stats/tags JSON, annotation, artwork hydration), so on
a large library it loaded tens of thousands of heavy structs only to read
one field each — spiking transient RSS to ~1GB during the one-time
upgrade backfill, a memory risk on small NAS/Pi hardware.

Add GetAllIDs to the album, artist, playlist and radio repositories: it
reuses each repo's base row-set filter (library visibility, artist
content join, playlist userFilter) but projects only id, skipping the
heavy columns and post-processing. A per-repo parity test asserts
GetAllIDs returns exactly the same id set as GetAll.

Verified on a 727MB / 29k-artist production DB copy: peak RSS during
backfill dropped from ~1012MB to ~89MB, file descriptors flat, same
36,138 items enqueued.
2026-07-23 13:20:22 -04:00

98 lines
1.8 KiB
Go

package tests
import (
"errors"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/model/id"
)
type MockedRadioRepo struct {
model.RadioRepository
Data map[string]*model.Radio
All model.Radios
Err bool
Options model.QueryOptions
}
func CreateMockedRadioRepo() *MockedRadioRepo {
return &MockedRadioRepo{}
}
func (m *MockedRadioRepo) SetError(err bool) {
m.Err = err
}
func (m *MockedRadioRepo) CountAll(options ...model.QueryOptions) (int64, error) {
if m.Err {
return 0, errors.New("error")
}
return int64(len(m.Data)), nil
}
func (m *MockedRadioRepo) Delete(id string) error {
if m.Err {
return errors.New("Error!")
}
_, found := m.Data[id]
if !found {
return errors.New("not found")
}
delete(m.Data, id)
return nil
}
func (m *MockedRadioRepo) Exists(id string) (bool, error) {
if m.Err {
return false, errors.New("Error!")
}
_, found := m.Data[id]
return found, nil
}
func (m *MockedRadioRepo) Get(id string) (*model.Radio, error) {
if m.Err {
return nil, errors.New("Error!")
}
if d, ok := m.Data[id]; ok {
return d, nil
}
return nil, model.ErrNotFound
}
func (m *MockedRadioRepo) GetAll(qo ...model.QueryOptions) (model.Radios, error) {
if len(qo) > 0 {
m.Options = qo[0]
}
if m.Err {
return nil, errors.New("Error!")
}
return m.All, nil
}
func (m *MockedRadioRepo) GetAllIDs(qo ...model.QueryOptions) ([]string, error) {
all, err := m.GetAll(qo...)
if err != nil {
return nil, err
}
ids := make([]string, len(all))
for i, r := range all {
ids[i] = r.ID
}
return ids, nil
}
func (m *MockedRadioRepo) Put(radio *model.Radio, _ ...string) error {
if m.Err {
return errors.New("error")
}
if radio.ID == "" {
radio.ID = id.NewRandom()
}
m.Data[radio.ID] = radio
return nil
}