navidrome/persistence/artist_repository.go
Deluan Quintão fcb5e1b806
fix(server): fix case-insensitive sort order and add indexes to improve performance (#3425)
* refactor(server): better sort mappings

* refactor(server): simplify GetIndex

* fix: recreate tables and indexes using proper collation

Also add tests to ensure proper collation

* chore: remove unused method

* fix: sort expressions

* fix: lint errors

* fix: cleanup
2024-10-26 14:06:34 -04:00

210 lines
5.4 KiB
Go

package persistence
import (
"cmp"
"context"
"fmt"
"net/url"
"slices"
"strings"
. "github.com/Masterminds/squirrel"
"github.com/deluan/rest"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils"
"github.com/navidrome/navidrome/utils/slice"
"github.com/pocketbase/dbx"
)
type artistRepository struct {
sqlRepository
indexGroups utils.IndexGroups
}
type dbArtist struct {
*model.Artist `structs:",flatten"`
SimilarArtists string `structs:"-" json:"similarArtists"`
}
func (a *dbArtist) PostScan() error {
if a.SimilarArtists == "" {
return nil
}
for _, s := range strings.Split(a.SimilarArtists, ";") {
fields := strings.Split(s, ":")
if len(fields) != 2 {
continue
}
name, _ := url.QueryUnescape(fields[1])
a.Artist.SimilarArtists = append(a.Artist.SimilarArtists, model.Artist{
ID: fields[0],
Name: name,
})
}
return nil
}
func (a *dbArtist) PostMapArgs(m map[string]any) error {
var sa []string
for _, s := range a.Artist.SimilarArtists {
sa = append(sa, fmt.Sprintf("%s:%s", s.ID, url.QueryEscape(s.Name)))
}
m["similar_artists"] = strings.Join(sa, ";")
return nil
}
func NewArtistRepository(ctx context.Context, db dbx.Builder) model.ArtistRepository {
r := &artistRepository{}
r.ctx = ctx
r.db = db
r.indexGroups = utils.ParseIndexGroups(conf.Server.IndexGroups)
r.tableName = "artist" // To be used by the idFilter below
r.registerModel(&model.Artist{}, map[string]filterFunc{
"id": idFilter(r.tableName),
"name": fullTextFilter,
"starred": booleanFilter,
"genre_id": eqFilter,
})
r.setSortMappings(map[string]string{
"name": "order_artist_name",
"starred_at": "starred, starred_at",
})
return r
}
func (r *artistRepository) selectArtist(options ...model.QueryOptions) SelectBuilder {
sql := r.newSelectWithAnnotation("artist.id", options...).Columns("artist.*")
return r.withGenres(sql).GroupBy("artist.id")
}
func (r *artistRepository) CountAll(options ...model.QueryOptions) (int64, error) {
sql := r.newSelectWithAnnotation("artist.id")
sql = r.withGenres(sql) // Required for filtering by genre
return r.count(sql, options...)
}
func (r *artistRepository) Exists(id string) (bool, error) {
return r.exists(Select().Where(Eq{"artist.id": id}))
}
func (r *artistRepository) Put(a *model.Artist, colsToUpdate ...string) error {
a.FullText = getFullText(a.Name, a.SortArtistName)
dba := &dbArtist{Artist: a}
_, err := r.put(dba.ID, dba, colsToUpdate...)
if err != nil {
return err
}
if a.ID == consts.VariousArtistsID {
return r.updateGenres(a.ID, nil)
}
return r.updateGenres(a.ID, a.Genres)
}
func (r *artistRepository) Get(id string) (*model.Artist, error) {
sel := r.selectArtist().Where(Eq{"artist.id": id})
var dba []dbArtist
if err := r.queryAll(sel, &dba); err != nil {
return nil, err
}
if len(dba) == 0 {
return nil, model.ErrNotFound
}
res := r.toModels(dba)
err := loadAllGenres(r, res)
return &res[0], err
}
func (r *artistRepository) GetAll(options ...model.QueryOptions) (model.Artists, error) {
sel := r.selectArtist(options...)
var dba []dbArtist
err := r.queryAll(sel, &dba)
if err != nil {
return nil, err
}
res := r.toModels(dba)
err = loadAllGenres(r, res)
return res, err
}
func (r *artistRepository) toModels(dba []dbArtist) model.Artists {
res := model.Artists{}
for i := range dba {
res = append(res, *dba[i].Artist)
}
return res
}
func (r *artistRepository) getIndexKey(a model.Artist) string {
source := a.OrderArtistName
if conf.Server.PreferSortTags {
source = cmp.Or(a.SortArtistName, a.OrderArtistName)
}
name := strings.ToLower(source)
for k, v := range r.indexGroups {
if strings.HasPrefix(name, strings.ToLower(k)) {
return v
}
}
return "#"
}
// TODO Cache the index (recalculate when there are changes to the DB)
func (r *artistRepository) GetIndex() (model.ArtistIndexes, error) {
artists, err := r.GetAll(model.QueryOptions{Sort: "name"})
if err != nil {
return nil, err
}
var result model.ArtistIndexes
for k, v := range slice.Group(artists, r.getIndexKey) {
result = append(result, model.ArtistIndex{ID: k, Artists: v})
}
slices.SortFunc(result, func(a, b model.ArtistIndex) int {
return cmp.Compare(a.ID, b.ID)
})
return result, nil
}
func (r *artistRepository) purgeEmpty() error {
del := Delete(r.tableName).Where("id not in (select distinct(album_artist_id) from album)")
c, err := r.executeSQL(del)
if err == nil {
if c > 0 {
log.Debug(r.ctx, "Purged empty artists", "totalDeleted", c)
}
}
return err
}
func (r *artistRepository) Search(q string, offset int, size int) (model.Artists, error) {
var dba []dbArtist
err := r.doSearch(q, offset, size, &dba, "name")
if err != nil {
return nil, err
}
return r.toModels(dba), nil
}
func (r *artistRepository) Count(options ...rest.QueryOptions) (int64, error) {
return r.CountAll(r.parseRestOptions(r.ctx, options...))
}
func (r *artistRepository) Read(id string) (interface{}, error) {
return r.Get(id)
}
func (r *artistRepository) ReadAll(options ...rest.QueryOptions) (interface{}, error) {
return r.GetAll(r.parseRestOptions(r.ctx, options...))
}
func (r *artistRepository) EntityName() string {
return "artist"
}
func (r *artistRepository) NewInstance() interface{} {
return &model.Artist{}
}
var _ model.ArtistRepository = (*artistRepository)(nil)
var _ model.ResourceRepository = (*artistRepository)(nil)