Skip to content

Commit

Permalink
storage/mariadb: implement fave lookup by hash_link mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed May 24, 2024
1 parent b3ac295 commit 1061bc8
Showing 1 changed file with 9 additions and 30 deletions.
39 changes: 9 additions & 30 deletions storage/mariadb/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ func (ss SongStorage) FavoriteCount(song radio.Song) (int64, error) {
handle, deferFn := ss.handle.span(op)
defer deferFn()

var query = `SELECT count(*) FROM efave WHERE isong=?;`
var query = `SELECT count(*) FROM efave JOIN esong ON esong.id = efave.isong WHERE esong.hash_link=?;`
var faveCount int64

err := sqlx.Get(handle, &faveCount, query, song.ID)
err := sqlx.Get(handle, &faveCount, query, song.HashLink)
if err != nil {
return 0, errors.E(op, err)
}
Expand Down Expand Up @@ -352,27 +352,6 @@ func (ss SongStorage) UpdateHashLink(old, new radio.SongHash) error {
}

var songFavoritesOfQuery = expand(`
SELECT
{songColumns},
{maybeTrackColumns},
{lastplayedSelect},
NOW() AS synctime
FROM
enick
JOIN
efave ON efave.inick = enick.id
JOIN
esong ON esong.id = efave.isong
(esong.hash == esong.hash_link)
LEFT JOIN
tracks ON tracks.hash = esong.hash
WHERE
enick.nick = ?
ORDER BY efave.id ASC
LIMIT ? OFFSET ?;
`)

var songFavoritesOfQuery2 = expand(`
SELECT
{songColumns},
{maybeTrackColumns},
Expand All @@ -381,21 +360,21 @@ SELECT
FROM
esong
LEFT JOIN
tracks ON tracks.hash = esong.hash_link
tracks ON tracks.hash = esong.hash
JOIN
(SELECT
esong.hash
(SELECT DISTINCT
esong.hash_link
FROM
enick
JOIN
efave ON efave.inick = enick.id
JOIN
esong ON esong.id = efave.isong
WHERE
enick.nick = ?
AND
esong.hash == esong.hash_link) AS truth
ON esong.hash_link = truth.hash;
enick.nick = ?) AS truth
ON esong.hash = truth.hash_link
ORDER BY efave.id ASC
LIMIT ? OFFSET ?;
`)

var songFavoritesOfDatabaseOnlyQuery = expand(`
Expand Down

0 comments on commit 1061bc8

Please sign in to comment.