Integrate ArtistIndex logic into Artist
This commit is contained in:
@@ -2,11 +2,14 @@ package persistence
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/cloudsonic/sonic-server/conf"
|
||||
"github.com/cloudsonic/sonic-server/log"
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
"github.com/cloudsonic/sonic-server/utils"
|
||||
)
|
||||
|
||||
type Artist struct {
|
||||
@@ -17,14 +20,27 @@ type Artist struct {
|
||||
|
||||
type artistRepository struct {
|
||||
searchableRepository
|
||||
indexGroups utils.IndexGroups
|
||||
}
|
||||
|
||||
func NewArtistRepository() model.ArtistRepository {
|
||||
r := &artistRepository{}
|
||||
r.indexGroups = utils.ParseIndexGroups(conf.Sonic.IndexGroups)
|
||||
r.tableName = "artist"
|
||||
return r
|
||||
}
|
||||
|
||||
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 "#"
|
||||
}
|
||||
|
||||
func (r *artistRepository) Put(a *model.Artist) error {
|
||||
ta := Artist(*a)
|
||||
return withTx(func(o orm.Ormer) error {
|
||||
@@ -45,6 +61,34 @@ func (r *artistRepository) Get(id string) (*model.Artist, error) {
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
// TODO Cache the index (recalculate when there are changes to the DB)
|
||||
func (r *artistRepository) GetIndex() (model.ArtistIndexes, error) {
|
||||
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
|
||||
}
|
||||
|
||||
func (r *artistRepository) Refresh(ids ...string) error {
|
||||
type refreshArtist struct {
|
||||
Artist
|
||||
@@ -71,17 +115,17 @@ where f.artist_id in ('%s') group by f.artist_id order by f.id`, strings.Join(id
|
||||
|
||||
var toInsert []Artist
|
||||
var toUpdate []Artist
|
||||
for _, al := range artists {
|
||||
if al.Compilation {
|
||||
al.AlbumArtist = "Various Artists"
|
||||
for _, ar := range artists {
|
||||
if ar.Compilation {
|
||||
ar.AlbumArtist = "Various Artists"
|
||||
}
|
||||
if al.AlbumArtist != "" {
|
||||
al.Name = al.AlbumArtist
|
||||
if ar.AlbumArtist != "" {
|
||||
ar.Name = ar.AlbumArtist
|
||||
}
|
||||
if al.CurrentId != "" {
|
||||
toUpdate = append(toUpdate, al.Artist)
|
||||
if ar.CurrentId != "" {
|
||||
toUpdate = append(toUpdate, ar.Artist)
|
||||
} else {
|
||||
toInsert = append(toInsert, al.Artist)
|
||||
toInsert = append(toInsert, ar.Artist)
|
||||
}
|
||||
}
|
||||
if len(toInsert) > 0 {
|
||||
@@ -93,7 +137,7 @@ where f.artist_id in ('%s') group by f.artist_id order by f.id`, strings.Join(id
|
||||
}
|
||||
if len(toUpdate) > 0 {
|
||||
for _, al := range toUpdate {
|
||||
_, err := o.Update(&al, "name", "album_count")
|
||||
_, err := o.Update(&al)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -13,18 +13,47 @@ var _ = Describe("ArtistRepository", func() {
|
||||
repo = NewArtistRepository()
|
||||
})
|
||||
|
||||
It("saves and retrieves data", func() {
|
||||
Expect(repo.Get("1")).To(Equal(&model.Artist{ID: "1", Name: "Saara Saara", AlbumCount: 2}))
|
||||
Describe("Put/Get", func() {
|
||||
It("saves and retrieves data", func() {
|
||||
Expect(repo.Get("1")).To(Equal(&artistSaaraSaara))
|
||||
})
|
||||
|
||||
It("overrides data if ID already exists", func() {
|
||||
Expect(repo.Put(&model.Artist{ID: "1", Name: "Saara Saara is The Best!", AlbumCount: 3})).To(BeNil())
|
||||
Expect(repo.Get("1")).To(Equal(&model.Artist{ID: "1", Name: "Saara Saara is The Best!", AlbumCount: 3}))
|
||||
})
|
||||
|
||||
It("returns ErrNotFound when the ID does not exist", func() {
|
||||
_, err := repo.Get("999")
|
||||
Expect(err).To(MatchError(model.ErrNotFound))
|
||||
})
|
||||
})
|
||||
|
||||
It("overrides data if ID already exists", func() {
|
||||
Expect(repo.Put(&model.Artist{ID: "1", Name: "Saara Saara is The Best!", AlbumCount: 3})).To(BeNil())
|
||||
Expect(repo.Get("1")).To(Equal(&model.Artist{ID: "1", Name: "Saara Saara is The Best!", AlbumCount: 3}))
|
||||
})
|
||||
|
||||
It("returns ErrNotFound when the ID does not exist", func() {
|
||||
_, err := repo.Get("999")
|
||||
Expect(err).To(MatchError(model.ErrNotFound))
|
||||
Describe("GetIndex", func() {
|
||||
It("returns the index", func() {
|
||||
idx, err := repo.GetIndex()
|
||||
Expect(err).To(BeNil())
|
||||
Expect(idx).To(Equal(model.ArtistIndexes{
|
||||
{
|
||||
ID: "B",
|
||||
Artists: model.Artists{
|
||||
artistBeatles,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: "K",
|
||||
Artists: model.Artists{
|
||||
artistKraftwerk,
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: "S",
|
||||
Artists: model.Artists{
|
||||
{ID: "1", Name: "Saara Saara is The Best!", AlbumCount: 3},
|
||||
},
|
||||
},
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("PurgeInactive", func() {
|
||||
|
||||
@@ -1,145 +0,0 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/cloudsonic/sonic-server/conf"
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
"github.com/cloudsonic/sonic-server/utils"
|
||||
)
|
||||
|
||||
type ArtistInfo struct {
|
||||
ID int `orm:"pk;auto;column(id)"`
|
||||
Idx string
|
||||
ArtistID string `orm:"column(artist_id)"`
|
||||
Artist string
|
||||
AlbumCount int
|
||||
}
|
||||
|
||||
type tempIndex map[string]model.ArtistInfo
|
||||
|
||||
type artistIndexRepository struct {
|
||||
sqlRepository
|
||||
}
|
||||
|
||||
func NewArtistIndexRepository() model.ArtistIndexRepository {
|
||||
r := &artistIndexRepository{}
|
||||
r.tableName = "artist_info"
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) Put(idx *model.ArtistIndex) error {
|
||||
return withTx(func(o orm.Ormer) error {
|
||||
_, err := r.newQuery(o).Filter("idx", idx.ID).Delete()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, artist := range idx.Artists {
|
||||
a := ArtistInfo{
|
||||
Idx: idx.ID,
|
||||
ArtistID: artist.ArtistID,
|
||||
Artist: artist.Artist,
|
||||
AlbumCount: artist.AlbumCount,
|
||||
}
|
||||
err := r.insert(o, &a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) Refresh() error {
|
||||
o := Db()
|
||||
|
||||
indexGroups := utils.ParseIndexGroups(conf.Sonic.IndexGroups)
|
||||
artistIndex := make(map[string]tempIndex)
|
||||
|
||||
var artists []Artist
|
||||
_, err := o.QueryTable(&Artist{}).All(&artists)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, ar := range artists {
|
||||
r.collectIndex(indexGroups, &ar, artistIndex)
|
||||
}
|
||||
|
||||
return r.saveIndex(artistIndex)
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) collectIndex(ig utils.IndexGroups, a *Artist, artistIndex map[string]tempIndex) {
|
||||
name := a.Name
|
||||
indexName := strings.ToLower(utils.NoArticle(name))
|
||||
if indexName == "" {
|
||||
return
|
||||
}
|
||||
group := r.findGroup(ig, indexName)
|
||||
artists := artistIndex[group]
|
||||
if artists == nil {
|
||||
artists = make(tempIndex)
|
||||
artistIndex[group] = artists
|
||||
}
|
||||
artists[indexName] = model.ArtistInfo{ArtistID: a.ID, Artist: a.Name, AlbumCount: a.AlbumCount}
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) findGroup(ig utils.IndexGroups, name string) string {
|
||||
for k, v := range ig {
|
||||
key := strings.ToLower(k)
|
||||
if strings.HasPrefix(name, key) {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return "#"
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) saveIndex(artistIndex map[string]tempIndex) error {
|
||||
r.DeleteAll()
|
||||
for k, temp := range artistIndex {
|
||||
idx := &model.ArtistIndex{ID: k}
|
||||
for _, v := range temp {
|
||||
idx.Artists = append(idx.Artists, v)
|
||||
}
|
||||
err := r.Put(idx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *artistIndexRepository) GetAll() (model.ArtistIndexes, error) {
|
||||
var all []ArtistInfo
|
||||
_, err := r.newQuery(Db()).OrderBy("idx", "artist").All(&all)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fullIdx := make(map[string]*model.ArtistIndex)
|
||||
for _, a := range all {
|
||||
idx, ok := fullIdx[a.Idx]
|
||||
if !ok {
|
||||
idx = &model.ArtistIndex{ID: a.Idx}
|
||||
fullIdx[a.Idx] = idx
|
||||
}
|
||||
idx.Artists = append(idx.Artists, model.ArtistInfo{
|
||||
ArtistID: a.ArtistID,
|
||||
Artist: a.Artist,
|
||||
AlbumCount: a.AlbumCount,
|
||||
})
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
var _ model.ArtistIndexRepository = (*artistIndexRepository)(nil)
|
||||
@@ -1,40 +0,0 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Artist Index", func() {
|
||||
var repo model.ArtistIndexRepository
|
||||
|
||||
BeforeEach(func() {
|
||||
repo = NewArtistIndexRepository()
|
||||
err := repo.DeleteAll()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
|
||||
It("successfully persists data", func() {
|
||||
idx1 := model.ArtistIndex{
|
||||
ID: "D",
|
||||
Artists: model.ArtistInfos{
|
||||
{ArtistID: "4", Artist: "Doobie Brothers", AlbumCount: 2},
|
||||
{ArtistID: "3", Artist: "The Doors", AlbumCount: 5},
|
||||
},
|
||||
}
|
||||
idx2 := model.ArtistIndex{
|
||||
ID: "S",
|
||||
Artists: model.ArtistInfos{
|
||||
{ArtistID: "1", Artist: "Saara Saara", AlbumCount: 3},
|
||||
{ArtistID: "2", Artist: "Seu Jorge", AlbumCount: 1},
|
||||
},
|
||||
}
|
||||
|
||||
Expect(repo.Put(&idx1)).To(BeNil())
|
||||
Expect(repo.Put(&idx2)).To(BeNil())
|
||||
Expect(repo.GetAll()).To(Equal(model.ArtistIndexes{idx1, idx2}))
|
||||
})
|
||||
})
|
||||
@@ -1,40 +0,0 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
)
|
||||
|
||||
func CreateMockArtistIndexRepo() *MockArtistIndex {
|
||||
return &MockArtistIndex{}
|
||||
}
|
||||
|
||||
type MockArtistIndex struct {
|
||||
model.ArtistIndexRepository
|
||||
data model.ArtistIndexes
|
||||
err bool
|
||||
}
|
||||
|
||||
func (m *MockArtistIndex) SetError(err bool) {
|
||||
m.err = err
|
||||
}
|
||||
|
||||
func (m *MockArtistIndex) SetData(j string, length int) {
|
||||
m.data = make(model.ArtistIndexes, length)
|
||||
err := json.Unmarshal([]byte(j), &m.data)
|
||||
if err != nil {
|
||||
fmt.Println("ERROR: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockArtistIndex) GetAll() (model.ArtistIndexes, error) {
|
||||
if m.err {
|
||||
return nil, errors.New("Error!")
|
||||
}
|
||||
return m.data, nil
|
||||
}
|
||||
|
||||
var _ model.ArtistIndexRepository = (*MockArtistIndex)(nil)
|
||||
@@ -74,7 +74,6 @@ func initORM(dbPath string) error {
|
||||
orm.RegisterModel(new(Artist))
|
||||
orm.RegisterModel(new(Album))
|
||||
orm.RegisterModel(new(MediaFile))
|
||||
orm.RegisterModel(new(ArtistInfo))
|
||||
orm.RegisterModel(new(Checksum))
|
||||
orm.RegisterModel(new(Property))
|
||||
orm.RegisterModel(new(Playlist))
|
||||
|
||||
@@ -8,7 +8,6 @@ var Set = wire.NewSet(
|
||||
NewArtistRepository,
|
||||
NewMediaFileRepository,
|
||||
NewAlbumRepository,
|
||||
NewArtistIndexRepository,
|
||||
NewCheckSumRepository,
|
||||
NewPropertyRepository,
|
||||
NewPlaylistRepository,
|
||||
|
||||
Reference in New Issue
Block a user