redesign of updateStandaloneArtwork() - #1637
Conversation
Signed-off-by: darrell-k <darrell@darrell.org.uk>
michaelherger
left a comment
There was a problem hiding this comment.
Thanks a lot! I hope to find time to actually test this later today. All my comments are just of theoretical nature. Haven't even pulled this change yet. Bear with me.
| dir text, | ||
| path text NOT NULL, |
There was a problem hiding this comment.
What is dir vs. path? Is the latter the full path incl. filename?
There was a problem hiding this comment.
Maybe this is just my non-EN native speaker problem. But would there be names which better describe what those columns are? Even if it was file_path?
There was a problem hiding this comment.
Column name changes pushed.
| # XXX how best to delete files in non-recursive mode? | ||
| # Delete the directory itself and all children | ||
| $dbh->do("DELETE FROM scanned_files WHERE url = '${file}' OR url LIKE '${file}/%'"); | ||
| $dbh->do("DELETE FROM scanned_pics WHERE dir LIKE '${path}/%'"); |
There was a problem hiding this comment.
Is there a need for this? I believe the pictures table has a different use than the files: the latter really is there to iterate over and process all records. The former (scaned_pics) is a helper to look up things for the tracks. In my plans/ideas this will be more than just cover artwork, but eg. artist pictures too. We shouldn't delete that data before we're really done. Wouldn't we potentially need it at a later stage to look up box set artwork, too?
BTW: I first wanted to complain about the use of variables in the SQL statement, instead of using prepared statements. That's a typical target for SQL injection. A folder name of drop table <table name>; -- or similar could potentially cause harm... something we should probably clean up at some point. But please try to avoid using variables potentially containing user data as much as possible.
There was a problem hiding this comment.
Probably not. Also with the introduction of schema_scanner.sql you're also recreating scanned_files so the existing DELETE could be removed, too.
But this has prompted a thought: without the change which I assumed was temporary for debugging, to not run schema_scanner.sql unless we're in the scanner process, we'll also clear scanned_files when schema.pm is initialised in the main process. At the moment scanned_files remains populated until a full rescan. Might this affect things like autorescanning?
| filesize int(10) | ||
| filesize int(10), | ||
| coverid char(8), | ||
| status char(1) |
There was a problem hiding this comment.
please document the possible status values
| filesize int(10) | ||
| filesize int(10), | ||
| coverid char(8), | ||
| status char(1) |
There was a problem hiding this comment.
Please document the possible status values.
I also talked to my bot buddy about this, and how to potentially optimise this. A first suggestion was defining the possible values:
status TEXT NOT NULL CHECK (status IN ('D', 'E', 'N'))
(or whatever the flags!)
Not that it added much to readability or performance, but DB level validation. We probably need NULL, but you get the point.
I was also wondering about using number, and then human readable constants in code. But that would make the query definitions somewhat more cumbersome.
Anyway: if the status is well defined somewhere even I should be able to learn the few characters.
| WHERE NOT EXISTS ( | ||
| SELECT path FROM scanned_pics | ||
| WHERE scanned_pics.path = tracks.cover | ||
| ) |
There was a problem hiding this comment.
Would we need a clause here to exclude online pictures (imported from music service) from being considered deleted?
There was a problem hiding this comment.
It's there immediately below. Also excludes embedded covers (the numeric check) and includes only tracks within the currently processing base directory:
AND cover NOT LIKE 'https%'
AND CAST(CAST(cover AS INTEGER) AS TEXT) <> cover
AND url LIKE '$basedir%'
There was a problem hiding this comment.
Oh my... 🤦🏻.
Hopefully SQLite is smart enough to do these cheap checks before doing the path lookup in scanned_pics.
Why would you have to do the double casting?
There was a problem hiding this comment.
Oh my... 🤦🏻.
Hopefully SQLite is smart enough to do these cheap checks before doing the path lookup in
scanned_pics.Why would you have to do the double casting?
The is no 'is numeric' function in SQLITE. But if the value survives being cast to integer and back again, it is numeric.
| VALUES | ||
| (?, ?, ?) | ||
| (?, ?, ?, ?, ?, | ||
| CASE WHEN (SELECT COUNT(*) FROM tracks WHERE tracks.cover = ?) = 0 THEN 'N' ELSE 'E' END |
There was a problem hiding this comment.
I guess that's the reason for the new index on cover?
Doing a full count might be easier to read, but it's somewhat wasteful, as the DB would always have to count all the occurrences, even if we're only interested in the existence of at least one record.
CASE WHEN (
SELECT EXISTS (
SELECT 1 FROM tracks WHERE tracks.cover = ?
)
) THEN 'N' ELSE 'E' END;Supposedly is more efficient.
There was a problem hiding this comment.
Yes, I usually would use EXISTS, don't know what happened here. But this needs changing anyway because we need to check the image hasn't been updated with another of the same name. I'll be pushing a fix soon.
| $file, | ||
| $mtime, | ||
| $size, | ||
| substr( safe_md5_hex( $file . $mtime . $size ), 0, 8 ), |
There was a problem hiding this comment.
Could you please add another helper for this in Artwork.pm, and use it wherever we do this calculation? It's so specific and non-obvious, having an understandable function name would not only help making sure we're always doing the same thing, but also reading the code. I had to search existing code to understand what this was.
| ### I might have missed it, but I can't see where this might be called in main process async mode. | ||
| ### If it is, we'll need more work to populate scanned_pics in the main process or just keep a version of the old subroutine for that use. |
There was a problem hiding this comment.
Please don't remove this just yet... I'm a bit anxious we might be missing something. I want to double check this.
Signed-off-by: darrell-k <darrell@darrell.org.uk>
Signed-off-by: darrell-k <darrell@darrell.org.uk>
As discussed. I hope it all makes sense.
The diff generated by git for
updateStandaloneArtwork()is a bit of a mess, probably best to view the new routine as a complete replacement for the old one.This redesign enhances the new
scanned_picstable so that it can driveupdateStandaloneArtwork().coveridcolumn so that we can read it directly from the table (in the scanner process) when we need to updatetracksoralbums. In order for this to work, all externalcoveridgeneration will now use the image path, not the music file URL.statuscolumn so we can differentiate new, existing and deleted images.urlcolumn is renamed topathas it will now hold the file system path of the image, not a file:// URL. This makes things much easier.dircolumn as discussed.In performance testing, this runs faster, even though we are now calling
findStandaloneArtwork()for every track where an image change has been detected, rather than only once for each album/image group.This change enables
TitleFormatterto do its work correctly in cases when the user has specified a variable cover id which includes a "sub-album" field likediscnumberorgrouping. This means that disc or grouping-specific images can be applied to tracks using this existing mechanism when everything for the album is in the same directory.I've added some comments to new/changed code in order to aid understanding.
I'm sure at this stage there is stuff I've missed.