Add denormalized list of artist_ids to album, to speed-up artist's albums queries

This will be removed once we have a proper many-to-many relationship between album and artist
This commit is contained in:
Deluan
2020-12-13 14:05:48 -05:00
parent f86bc070de
commit 4f90fa9924
7 changed files with 139 additions and 58 deletions
+7 -8
View File
@@ -2,6 +2,7 @@ package persistence
import (
"context"
"fmt"
"os"
"path/filepath"
"sort"
@@ -63,13 +64,7 @@ func yearFilter(field string, value interface{}) Sqlizer {
}
func artistFilter(field string, value interface{}) Sqlizer {
return exists("media_file", And{
ConcatExpr("album_id=album.id"),
Or{
Eq{"artist_id": value},
Eq{"album_artist_id": value},
},
})
return Like{"all_artist_ids": fmt.Sprintf("%%%s%%", value)}
}
func (r *albumRepository) CountAll(options ...model.QueryOptions) (int64, error) {
@@ -153,6 +148,7 @@ func (r *albumRepository) refresh(ids ...string) error {
model.Album
CurrentId string
SongArtists string
SongArtistIds string
Years string
DiscSubtitles string
Comments string
@@ -167,7 +163,9 @@ func (r *albumRepository) refresh(ids ...string) error {
f.catalog_num, f.compilation, f.genre, max(f.year) as max_year, sum(f.duration) as duration,
count(f.id) as song_count, a.id as current_id,
group_concat(f.disc_subtitle, ' ') as disc_subtitles,
group_concat(f.artist, ' ') as song_artists, group_concat(f.year, ' ') as years,
group_concat(f.artist, ' ') as song_artists,
group_concat(f.artist_id, ' ') as song_artist_ids,
group_concat(f.year, ' ') as years,
sum(f.size) as size`).
From("media_file f").
LeftJoin("album a on f.album_id = a.id").
@@ -222,6 +220,7 @@ func (r *albumRepository) refresh(ids ...string) error {
toInsert++
al.CreatedAt = time.Now()
}
al.AllArtistIDs = utils.SanitizeStrings(al.SongArtistIds, al.AlbumArtistID, al.ArtistID)
al.FullText = getFullText(al.Name, al.Artist, al.AlbumArtist, al.SongArtists,
al.SortAlbumName, al.SortArtistName, al.SortAlbumArtistName, al.DiscSubtitles)
_, err := r.put(al.ID, al.Album)
+3 -27
View File
@@ -1,42 +1,18 @@
package persistence
import (
"regexp"
"sort"
"strings"
. "github.com/Masterminds/squirrel"
"github.com/deluan/navidrome/conf"
"github.com/kennygrant/sanitize"
"github.com/deluan/navidrome/utils"
)
var quotesRegex = regexp.MustCompile("[“”‘’'\"\\[\\(\\{\\]\\)\\}]")
func getFullText(text ...string) string {
fullText := sanitizeStrings(text...)
fullText := utils.SanitizeStrings(text...)
return " " + fullText
}
func sanitizeStrings(text ...string) string {
sanitizedText := strings.Builder{}
for _, txt := range text {
sanitizedText.WriteString(strings.TrimSpace(sanitize.Accents(strings.ToLower(txt))) + " ")
}
words := make(map[string]struct{})
for _, w := range strings.Fields(sanitizedText.String()) {
words[w] = struct{}{}
}
var fullText []string
for w := range words {
w = quotesRegex.ReplaceAllString(w, "")
if w != "" {
fullText = append(fullText, w)
}
}
sort.Strings(fullText)
return strings.Join(fullText, " ")
}
func (r sqlRepository) doSearch(q string, offset, size int, results interface{}, orderBys ...string) error {
q = strings.TrimSpace(q)
q = strings.TrimSuffix(q, "*")
@@ -59,7 +35,7 @@ func fullTextExpr(value string) Sqlizer {
if !conf.Server.SearchFullString {
sep = " "
}
q := sanitizeStrings(value)
q := utils.SanitizeStrings(value)
parts := strings.Split(q, " ")
filters := And{}
for _, part := range parts {
+2 -22
View File
@@ -7,28 +7,8 @@ import (
var _ = Describe("sqlRepository", func() {
Describe("getFullText", func() {
It("returns all lowercase chars", func() {
Expect(getFullText("Some Text")).To(Equal(" some text"))
})
It("removes accents", func() {
Expect(getFullText("Quintão")).To(Equal(" quintao"))
})
It("remove extra spaces", func() {
Expect(getFullText(" some text ")).To(Equal(" some text"))
})
It("remove duplicated words", func() {
Expect(getFullText("legião urbana urbana legiÃo")).To(Equal(" legiao urbana"))
})
It("remove symbols", func() {
Expect(getFullText("Toms Diner ' “40” A")).To(Equal(" 40 a diner toms"))
})
It("remove opening brackets", func() {
Expect(getFullText("[Five Years]")).To(Equal(" five years"))
It("prefixes with a space", func() {
Expect(getFullText("legiao urbana")).To(Equal(" legiao urbana"))
})
})
})