navidrome/persistence/artist_repository.go

187 lines
4.1 KiB
Go
Raw Normal View History

package persistence
2020-01-12 23:32:06 +01:00
import (
2020-01-16 22:53:48 +01:00
"fmt"
"sort"
2020-01-16 22:53:48 +01:00
"strings"
2020-01-12 23:32:06 +01:00
"github.com/astaxie/beego/orm"
"github.com/cloudsonic/sonic-server/conf"
2020-01-16 22:53:48 +01:00
"github.com/cloudsonic/sonic-server/log"
2020-01-15 04:22:34 +01:00
"github.com/cloudsonic/sonic-server/model"
"github.com/cloudsonic/sonic-server/utils"
2020-01-12 23:32:06 +01:00
)
2020-01-18 03:03:54 +01:00
type artist struct {
2020-01-12 23:32:06 +01:00
ID string `orm:"pk;column(id)"`
Name string `orm:"index"`
AlbumCount int `orm:"column(album_count)"`
}
type artistRepository struct {
searchableRepository
indexGroups utils.IndexGroups
2020-01-12 23:32:06 +01:00
}
2020-01-15 04:22:34 +01:00
func NewArtistRepository() model.ArtistRepository {
2020-01-12 23:32:06 +01:00
r := &artistRepository{}
r.indexGroups = utils.ParseIndexGroups(conf.Sonic.IndexGroups)
2020-01-13 06:04:11 +01:00
r.tableName = "artist"
2020-01-12 23:32:06 +01:00
return r
}
2020-01-18 03:03:54 +01:00
func (r *artistRepository) getIndexKey(a *artist) string {
name := strings.ToLower(utils.NoArticle(a.Name))
for k, v := range r.indexGroups {
key := strings.ToLower(k)
if strings.HasPrefix(name, key) {
return v
}
}
return "#"
}
2020-01-15 04:22:34 +01:00
func (r *artistRepository) Put(a *model.Artist) error {
2020-01-18 03:03:54 +01:00
ta := artist(*a)
return withTx(func(o orm.Ormer) error {
return r.put(o, a.ID, a.Name, &ta)
})
2020-01-12 23:32:06 +01:00
}
2020-01-15 04:22:34 +01:00
func (r *artistRepository) Get(id string) (*model.Artist, error) {
2020-01-18 03:03:54 +01:00
ta := artist{ID: id}
2020-01-12 23:32:06 +01:00
err := Db().Read(&ta)
if err == orm.ErrNoRows {
2020-01-15 04:22:34 +01:00
return nil, model.ErrNotFound
2020-01-12 23:32:06 +01:00
}
if err != nil {
return nil, err
}
2020-01-15 04:22:34 +01:00
a := model.Artist(ta)
2020-01-12 23:32:06 +01:00
return &a, nil
}
// TODO Cache the index (recalculate when there are changes to the DB)
func (r *artistRepository) GetIndex() (model.ArtistIndexes, error) {
2020-01-18 03:03:54 +01:00
var all []artist
_, err := r.newQuery(Db()).OrderBy("name").All(&all)
if err != nil {
return nil, err
}
fullIdx := make(map[string]*model.ArtistIndex)
for _, a := range all {
ax := r.getIndexKey(&a)
idx, ok := fullIdx[ax]
if !ok {
idx = &model.ArtistIndex{ID: ax}
fullIdx[ax] = idx
}
idx.Artists = append(idx.Artists, model.Artist(a))
}
var result model.ArtistIndexes
for _, idx := range fullIdx {
result = append(result, *idx)
}
sort.Slice(result, func(i, j int) bool {
return result[i].ID < result[j].ID
})
return result, nil
}
2020-01-16 22:53:48 +01:00
func (r *artistRepository) Refresh(ids ...string) error {
type refreshArtist struct {
2020-01-18 03:03:54 +01:00
artist
2020-01-16 22:53:48 +01:00
CurrentId string
AlbumArtist string
Compilation bool
}
var artists []refreshArtist
o := Db()
sql := fmt.Sprintf(`
select f.artist_id as id,
f.artist as name,
f.album_artist,
f.compilation,
count(*) as album_count,
a.id as current_id
from album f
left outer join artist a on f.artist_id = a.id
where f.artist_id in ('%s') group by f.artist_id order by f.id`, strings.Join(ids, "','"))
_, err := o.Raw(sql).QueryRows(&artists)
if err != nil {
return err
}
2020-01-18 03:03:54 +01:00
var toInsert []artist
var toUpdate []artist
for _, ar := range artists {
if ar.Compilation {
ar.AlbumArtist = "Various Artists"
2020-01-16 22:53:48 +01:00
}
if ar.AlbumArtist != "" {
ar.Name = ar.AlbumArtist
2020-01-16 22:53:48 +01:00
}
if ar.CurrentId != "" {
2020-01-18 03:03:54 +01:00
toUpdate = append(toUpdate, ar.artist)
2020-01-16 22:53:48 +01:00
} else {
2020-01-18 03:03:54 +01:00
toInsert = append(toInsert, ar.artist)
2020-01-16 22:53:48 +01:00
}
err := r.addToIndex(o, r.tableName, ar.ID, ar.Name)
if err != nil {
return err
}
2020-01-16 22:53:48 +01:00
}
if len(toInsert) > 0 {
n, err := o.InsertMulti(100, toInsert)
if err != nil {
return err
}
log.Debug("Inserted new artists", "num", n)
}
if len(toUpdate) > 0 {
for _, al := range toUpdate {
_, err := o.Update(&al)
2020-01-16 22:53:48 +01:00
if err != nil {
return err
}
}
log.Debug("Updated artists", "num", len(toUpdate))
}
return err
}
2020-01-15 04:22:34 +01:00
func (r *artistRepository) PurgeInactive(activeList model.Artists) error {
return withTx(func(o orm.Ormer) error {
_, err := r.purgeInactive(o, activeList, func(item interface{}) string {
2020-01-15 04:22:34 +01:00
return item.(model.Artist).ID
})
return err
2020-01-12 23:32:06 +01:00
})
}
2020-01-15 04:22:34 +01:00
func (r *artistRepository) Search(q string, offset int, size int) (model.Artists, error) {
if len(q) <= 2 {
return nil, nil
}
2020-01-18 03:03:54 +01:00
var results []artist
err := r.doSearch(r.tableName, q, offset, size, &results, "name")
if err != nil {
return nil, err
}
return r.toArtists(results), nil
}
2020-01-18 03:03:54 +01:00
func (r *artistRepository) toArtists(all []artist) model.Artists {
2020-01-15 04:22:34 +01:00
result := make(model.Artists, len(all))
for i, a := range all {
2020-01-15 04:22:34 +01:00
result[i] = model.Artist(a)
}
return result
}
2020-01-15 04:22:34 +01:00
var _ model.ArtistRepository = (*artistRepository)(nil)
2020-01-18 03:03:54 +01:00
var _ = model.Artist(artist{})