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.
| # 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?
| ### 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.
There was a problem hiding this comment.
Please leave that code in, commented out if you want. Until we are certain.
Signed-off-by: darrell-k <darrell@darrell.org.uk>
Signed-off-by: darrell-k <darrell@darrell.org.uk>
| CREATE INDEX scannedPicDirIndex ON scanned_pics (folder); | ||
| create index scannedPicStatusidx on scanned_pics(status); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS trackscoveridx ON tracks(cover); |
There was a problem hiding this comment.
Should we add this to one of the versioned files, too? If it's only used in the scanner (for now) we can probably get away adding it to the latest existing up files, avoiding another full wipe & rescan.
There was a problem hiding this comment.
That reminds me, I changed the INSERT to check tracks using coverid rather than cover, in case the user has updated an image without changing the file name. So I don't think this index is required any more.
There was a problem hiding this comment.
You still might be using it around the update album artwork to first track coverid for remote and embedded images. query. But as that's also using album ID and coverid checks, I'm not sure it has much of an impact? Did you measure performance?
In any case: if you wanted to keep it, please name it, as it's not an index on the cover ID.
And for all index statements add a space or remove it everywhere between the table name and the index field 😉 .
There was a problem hiding this comment.
"idx" means index. I'll change it to "index"
There was a problem hiding this comment.
Hah! Right! Keep the naming convention and upper/lower/camel casing consistent to help my aging and tired eyes and brain.
Signed-off-by: darrell-k <darrell@darrell.org.uk>
…de I used Signed-off-by: darrell-k <darrell@darrell.org.uk>
Signed-off-by: darrell-k <darrell@darrell.org.uk>
Signed-off-by: darrell-k <darrell@darrell.org.uk>
|
Some rework just pushed. |
michaelherger
left a comment
There was a problem hiding this comment.
I'm sorry, another sh..load of comments. I haven't even test run this yet, but I believe there's potential for performance optimisations on the DB level.
| use Slim::Utils::OSDetect; | ||
|
|
||
| use constant MAX_RETRIES => 5; | ||
| use constant IS_SQLITE => (Slim::Utils::OSDetect->getOS()->sqlHelperClass() =~ /SQLite/ ? 1 : 0); |
| # Maybe a track instance was passed in, but no longer from updateStandaloneArtwork() which gives us | ||
| # the trackid instead, as we only need to instantiate a track if 'titleformatter' artwork naming is in use. | ||
| my $track = $trackAttributes && delete $trackAttributes->{_track}; | ||
| $track ||= Slim::Schema->find('Track', $trackAttributes->{_trackid}) if $trackAttributes->{_trackid}; |
There was a problem hiding this comment.
Can we delete the _trackid element here?
| ### 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 leave that code in, commented out if you want. Until we are certain.
| UPDATE albums | ||
| SET artwork = tracks.coverid | ||
| FROM tracks | ||
| WHERE tracks.album = albums.id | ||
| AND ( tracks.cover IS NULL OR CAST(CAST(tracks.cover AS INTEGER) AS TEXT) = tracks.cover OR tracks.cover LIKE 'https%' ) | ||
| AND ( tracks.coverid <> albums.artwork OR albums.artwork IS NULL ) | ||
| } ); |
There was a problem hiding this comment.
formatting police alert: please move one out to align with the $dbh->do()
| ### I considered adding rows to scanned_pics for these images so that they'd be processed in the loop below, but I think this is more efficient. | ||
| #there's a different syntax for MySql. |
| my $sth_update_tracks = $dbh->prepare( qq{ | ||
| UPDATE tracks | ||
| SET cover = ?, coverid = ?, cover_cached = NULL | ||
| WHERE id = ? | ||
| } ); |
There was a problem hiding this comment.
Can we optimise this? We would run an update for each track individually. But couldn't we update all tracks of an album in one update query? Hopefully (to be confirmed) the check in the loop would then no longer enter the conditional update, as the cover would already be updated on the remaining tracks of the album?
There was a problem hiding this comment.
If the user is using TitleFormatter for variable names, then they could be different covers across the album (DISC, PERFORMANCE, GROUPING...).
| if ( $track->{cover} ne $newCover ) { | ||
| my ($newCoverid) = $dbh->selectrow_array($sth_scanned_pics, undef, $newCover); | ||
| $sth_update_tracks->execute( $newCover, $newCoverid, $track->{id} ); |
There was a problem hiding this comment.
See my comment where we define $sth_update_tracks: if we updated all the tracks of the album in a single update query, wouldn't this condition skip processing all the tracks individually?
| # 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 folder LIKE '${path}%'"); |
There was a problem hiding this comment.
Is there really a need to delete this? I'd really like to use the same table for other purposes to avoid another scan for images for the contributor artwork.
There was a problem hiding this comment.
Probably not. But see my previous comment regarding scanned_files: #1637 (comment)
| Slim::Utils::SQLHelper->executeSQLFile( | ||
| $driver, $class->storage->dbh, "schema_scanner.sql" | ||
| ); | ||
| ) if main::SCANNER; ### temporary, so we keep the contents from the last scan for debugging |
There was a problem hiding this comment.
It's still proving useful for development. Also see #1637 (comment) (which I just mentioned elsewhere as well)
| $columnValueHash{cover} = $cover; | ||
| } | ||
|
|
||
| # if ( $columnValueHash{cover} =~ /^https?/ || $columnValueHash{cover} =~ /^\d+$/ ) { ###combine the regex if this works!!! |
There was a problem hiding this comment.
Obsolete idea, needs removing!
|
Just to let you know, I'm currently testing the
|
|
I'm going to resolve some of the comments in this thread, it's getting hard to follow! |
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.