set the size of indexes

This commit is contained in:
sentriz
2019-07-02 21:19:25 +01:00
parent e517c7538c
commit 3d68e15241
3 changed files with 19 additions and 15 deletions

View File

@@ -29,10 +29,11 @@ func (c *Controller) GetIndexes(w http.ResponseWriter, r *http.Request) {
Where("albums.parent_id = 1").
Group("albums.id").
Find(&folders)
indexMap := make(map[string]*subsonic.Index)
indexes := []*subsonic.Index{}
// [a-z#] -> 27
indexMap := make(map[string]*subsonic.Index, 27)
resp := make([]*subsonic.Index, 0, 27)
for _, folder := range folders {
i := indexOf(folder.IndexRightPath())
i := lowerUDecOrHash(folder.IndexRightPath())
index, ok := indexMap[i]
if !ok {
index = &subsonic.Index{
@@ -40,18 +41,18 @@ func (c *Controller) GetIndexes(w http.ResponseWriter, r *http.Request) {
Artists: []*subsonic.Artist{},
}
indexMap[i] = index
indexes = append(indexes, index)
resp = append(resp, index)
}
index.Artists = append(index.Artists,
newArtistByFolder(folder))
}
sort.Slice(indexes, func(i, j int) bool {
return indexes[i].Name < indexes[j].Name
sort.Slice(resp, func(i, j int) bool {
return resp[i].Name < resp[j].Name
})
sub := subsonic.NewResponse()
sub.Indexes = &subsonic.Indexes{
LastModified: 0,
Index: indexes,
Index: resp,
}
respond(w, r, sub)
}

View File

@@ -22,10 +22,11 @@ func (c *Controller) GetArtists(w http.ResponseWriter, r *http.Request) {
`).
Group("artists.id").
Find(&artists)
indexMap := make(map[string]*subsonic.Index)
indexes := &subsonic.Artists{}
// [a-z#] -> 27
indexMap := make(map[string]*subsonic.Index, 27)
resp := make([]*subsonic.Index, 0, 27)
for _, artist := range artists {
i := indexOf(artist.IndexName())
i := lowerUDecOrHash(artist.IndexName())
index, ok := indexMap[i]
if !ok {
index = &subsonic.Index{
@@ -33,16 +34,18 @@ func (c *Controller) GetArtists(w http.ResponseWriter, r *http.Request) {
Artists: []*subsonic.Artist{},
}
indexMap[i] = index
indexes.List = append(indexes.List, index)
resp = append(resp, index)
}
index.Artists = append(index.Artists,
newArtistByTags(artist))
}
sort.Slice(indexes.List, func(i, j int) bool {
return indexes.List[i].Name < indexes.List[j].Name
sort.Slice(resp, func(i, j int) bool {
return resp[i].Name < resp[j].Name
})
sub := subsonic.NewResponse()
sub.Artists = indexes
sub.Artists = &subsonic.Artists{
List: resp,
}
respond(w, r, sub)
}

View File

@@ -17,7 +17,7 @@ import (
"github.com/sentriz/gonic/server/subsonic"
)
func indexOf(in string) string {
func lowerUDecOrHash(in string) string {
lower := unicode.ToLower(rune(in[0]))
if !unicode.IsLetter(lower) {
return "#"