Ticket #3041: mythmusic_scanner.diff

File mythmusic_scanner.diff, 25.1 KB (added by stuartm, 17 years ago)

ID caching and a couple of other tweaks

  • mythmusic/mythmusic/metadata.cpp

     
    169169    if (m_genre == "")
    170170        m_genre = QObject::tr("Unknown Genre");
    171171
    172     // Don't update the database if a song with the exact same
    173     // metadata is already there
    174172    MSqlQuery query(MSqlQuery::InitCon());
    175     query.prepare("SELECT music_songs.filename "
    176                   "FROM music_songs "
    177                   "LEFT JOIN music_artists ON music_songs.artist_id=music_artists.artist_id "
    178                   "LEFT JOIN music_albums ON music_songs.album_id=music_albums.album_id "
    179                   "LEFT JOIN music_artists AS music_comp_artists ON music_albums.artist_id=music_comp_artists.artist_id "
    180                   "LEFT JOIN music_genres ON music_songs.genre_id=music_genres.genre_id "
    181                   "WHERE music_artists.artist_name = :ARTIST"
    182                   " AND music_comp_artists.artist_name = :COMPILATION_ARTIST"
    183                   " AND music_albums.album_name = :ALBUM"
    184                   " AND music_songs.name = :TITLE"
    185                   " AND music_genres.genre = :GENRE"
    186                   " AND music_songs.year = :YEAR"
    187                   " AND music_songs.track = :TRACKNUM"
    188                   " AND music_songs.length = :LENGTH"
    189                   " AND music_songs.format = :FORMAT ;");
    190173
    191     query.bindValue(":ARTIST", m_artist.utf8());
    192     query.bindValue(":COMPILATION_ARTIST", m_compilation_artist.utf8());
    193     query.bindValue(":ALBUM", m_album.utf8());
    194     query.bindValue(":TITLE", m_title.utf8());
    195     query.bindValue(":GENRE", m_genre.utf8());
    196     query.bindValue(":YEAR", m_year);
    197     query.bindValue(":TRACKNUM", m_tracknum);
    198     query.bindValue(":LENGTH", m_length);
    199     query.bindValue(":FORMAT", m_format);
    200 
    201     if (query.exec() && query.size() > 0 )
     174    if (m_directoryid < 0)
    202175    {
    203         if (m_id > 0)
    204         {
    205             query.prepare("UPDATE music_songs SET "
    206                 "date_modified = :DATE_MOD WHERE filename = :FILENAME;");
    207             query.bindValue(":DATE_MOD", QDateTime::currentDateTime());
    208             query.bindValue(":FILENAME", sqlfilename.utf8());
    209             if (!query.exec() || !query.isActive())
    210             {
    211                 MythContext::DBError("update modification date", query);
    212             }
    213         }
    214 
    215         return;
    216     }
    217 
    218     // Load the directory id
    219     unsigned int directoryId;
    220     query.prepare("SELECT directory_id FROM music_directories "
    221                   "WHERE path = :DIRECTORY ;");
    222     query.bindValue(":DIRECTORY", sqldir.utf8());
    223 
    224     if (!query.exec() || !query.isActive())
    225     {
    226         MythContext::DBError("music select directory id", query);
    227         return;
    228     }
    229     if (query.next())
    230     {
    231         directoryId = query.value(0).toInt();
    232     }
    233     else
    234     {
    235         query.prepare("INSERT INTO music_directories (path) VALUES (:DIRECTORY);");
     176        // Load the directory id
     177        query.prepare("SELECT directory_id FROM music_directories "
     178                    "WHERE path = :DIRECTORY ;");
    236179        query.bindValue(":DIRECTORY", sqldir.utf8());
    237180
    238         if (!query.exec() || !query.isActive() || query.numRowsAffected() <= 0)
     181        if (!query.exec() || !query.isActive())
    239182        {
    240             MythContext::DBError("music insert directory", query);
     183            MythContext::DBError("music select directory id", query);
    241184            return;
    242185        }
    243         directoryId = query.lastInsertId().toInt();
     186        if (query.next())
     187        {
     188            m_directoryid = query.value(0).toInt();
     189        }
     190        else
     191        {
     192            query.prepare("INSERT INTO music_directories (path) VALUES (:DIRECTORY);");
     193            query.bindValue(":DIRECTORY", sqldir.utf8());
     194
     195            if (!query.exec() || !query.isActive() || query.numRowsAffected() <= 0)
     196            {
     197                MythContext::DBError("music insert directory", query);
     198                return;
     199            }
     200            m_directoryid = query.lastInsertId().toInt();
     201        }
    244202    }
    245203
    246     // Load the artist id or insert it and get the id
    247     unsigned int artistId;
    248     query.prepare("SELECT artist_id FROM music_artists "
    249                   "WHERE artist_name = :ARTIST ;");
    250     query.bindValue(":ARTIST", m_artist.utf8());
    251 
    252     if (!query.exec() || !query.isActive())
     204    if (m_artistid < 0)
    253205    {
    254         MythContext::DBError("music select artist id", query);
    255         return;
    256     }
    257     if (query.next())
    258     {
    259         artistId = query.value(0).toInt();
    260     }
    261     else
    262     {
    263         query.prepare("INSERT INTO music_artists (artist_name) VALUES (:ARTIST);");
     206        // Load the artist id
     207        query.prepare("SELECT artist_id FROM music_artists "
     208                    "WHERE artist_name = :ARTIST ;");
    264209        query.bindValue(":ARTIST", m_artist.utf8());
    265210
    266         if (!query.exec() || !query.isActive() || query.numRowsAffected() <= 0)
     211        if (!query.exec() || !query.isActive())
    267212        {
    268             MythContext::DBError("music insert artist", query);
     213            MythContext::DBError("music select artist id", query);
    269214            return;
    270215        }
    271         artistId = query.lastInsertId().toInt();
     216        if (query.next())
     217        {
     218            m_artistid = query.value(0).toInt();
     219        }
     220        else
     221        {
     222            query.prepare("INSERT INTO music_artists (artist_name) VALUES (:ARTIST);");
     223            query.bindValue(":ARTIST", m_artist.utf8());
     224
     225            if (!query.exec() || !query.isActive() || query.numRowsAffected() <= 0)
     226            {
     227                MythContext::DBError("music insert artist", query);
     228                return;
     229            }
     230            m_artistid = query.lastInsertId().toInt();
     231        }
    272232    }
    273233
    274234    // Compilation Artist
    275     unsigned int compilationArtistId;
    276235    if (m_artist == m_compilation_artist)
    277236    {
    278         compilationArtistId = artistId;
     237        m_compartistid = m_artistid;
    279238    }
    280239    else
    281240    {
     
    289248        }
    290249        if (query.next())
    291250        {
    292             compilationArtistId = query.value(0).toInt();
     251            m_compartistid = query.value(0).toInt();
    293252        }
    294253        else
    295254        {
     
    301260                MythContext::DBError("music insert compilation artist", query);
    302261                return;
    303262            }
    304             compilationArtistId = query.lastInsertId().toInt();
     263            m_compartistid = query.lastInsertId().toInt();
    305264        }
    306265    }
    307266
    308267    // Album
    309     unsigned int albumId;
    310     query.prepare("SELECT album_id FROM music_albums "
    311                   "WHERE artist_id = :COMP_ARTIST_ID "
    312                   " AND album_name = :ALBUM ;");
    313     query.bindValue(":COMP_ARTIST_ID", compilationArtistId);
    314     query.bindValue(":ALBUM", m_album.utf8());
    315     if (!query.exec() || !query.isActive())
     268    if (m_albumid < 0)
    316269    {
    317         MythContext::DBError("music select album id", query);
    318         return;
    319     }
    320     if (query.next())
    321     {
    322         albumId = query.value(0).toInt();
    323     }
    324     else
    325     {
    326         query.prepare("INSERT INTO music_albums (artist_id, album_name, compilation, year) VALUES (:COMP_ARTIST_ID, :ALBUM, :COMPILATION, :YEAR);");
    327         query.bindValue(":COMP_ARTIST_ID", compilationArtistId);
     270        query.prepare("SELECT album_id FROM music_albums "
     271                    "WHERE artist_id = :COMP_ARTIST_ID "
     272                    " AND album_name = :ALBUM ;");
     273        query.bindValue(":COMP_ARTIST_ID", m_compartistid);
    328274        query.bindValue(":ALBUM", m_album.utf8());
    329         query.bindValue(":COMPILATION", m_compilation);
    330         query.bindValue(":YEAR", m_year);
    331 
    332         if (!query.exec() || !query.isActive() || query.numRowsAffected() <= 0)
     275        if (!query.exec() || !query.isActive())
    333276        {
    334             MythContext::DBError("music insert album", query);
     277            MythContext::DBError("music select album id", query);
    335278            return;
    336279        }
    337         albumId = query.lastInsertId().toInt();
     280        if (query.next())
     281        {
     282            m_albumid = query.value(0).toInt();
     283        }
     284        else
     285        {
     286            query.prepare("INSERT INTO music_albums (artist_id, album_name, compilation, year) VALUES (:COMP_ARTIST_ID, :ALBUM, :COMPILATION, :YEAR);");
     287            query.bindValue(":COMP_ARTIST_ID", m_compartistid);
     288            query.bindValue(":ALBUM", m_album.utf8());
     289            query.bindValue(":COMPILATION", m_compilation);
     290            query.bindValue(":YEAR", m_year);
     291
     292            if (!query.exec() || !query.isActive() || query.numRowsAffected() <= 0)
     293            {
     294                MythContext::DBError("music insert album", query);
     295                return;
     296            }
     297            m_albumid = query.lastInsertId().toInt();
     298        }
    338299    }
    339300
    340     // Genres
    341     unsigned int genreId;
    342     query.prepare("SELECT genre_id FROM music_genres "
    343                   "WHERE genre = :GENRE ;");
    344     query.bindValue(":GENRE", m_genre.utf8());
    345     if (!query.exec() || !query.isActive())
     301    if (m_genreid < 0)
    346302    {
    347         MythContext::DBError("music select genre id", query);
    348         return;
    349     }
    350     if (query.next())
    351     {
    352         genreId = query.value(0).toInt();
    353     }
    354     else
    355     {
    356         query.prepare("INSERT INTO music_genres (genre) VALUES (:GENRE);");
     303        // Genres
     304        query.prepare("SELECT genre_id FROM music_genres "
     305                    "WHERE genre = :GENRE ;");
    357306        query.bindValue(":GENRE", m_genre.utf8());
    358 
    359         if (!query.exec() || !query.isActive() || query.numRowsAffected() <= 0)
     307        if (!query.exec() || !query.isActive())
    360308        {
    361             MythContext::DBError("music insert genre", query);
     309            MythContext::DBError("music select genre id", query);
    362310            return;
    363311        }
    364         genreId = query.lastInsertId().toInt();
     312        if (query.next())
     313        {
     314            m_genreid = query.value(0).toInt();
     315        }
     316        else
     317        {
     318            query.prepare("INSERT INTO music_genres (genre) VALUES (:GENRE);");
     319            query.bindValue(":GENRE", m_genre.utf8());
     320
     321            if (!query.exec() || !query.isActive() || query.numRowsAffected() <= 0)
     322            {
     323                MythContext::DBError("music insert genre", query);
     324                return;
     325            }
     326            m_genreid = query.lastInsertId().toInt();
     327        }
    365328    }
    366329
    367330    // We have all the id's now. We can insert it.
     
    397360    }
    398361
    399362    query.prepare(strQuery);
    400     /*
    401     query.prepare("INSERT INTO music_songs "
    402                   "  (artist_id, album_id,     name,"
    403                   "   genre_id,  year,         track,                length,"
    404                   "   filename,  date_entered, date_modified,"
    405                   "   format,    size,         bitrate) "
    406                   "VALUES "
    407                   "  (:ARTIST,   :ALBUM,       :TITLE,"
    408                   "   :GENRE,    :YEAR,        :TRACKNUM,            :LENGTH,"
    409                   "   :FILENAME, :DATE_ADD,  :DATE_MOD,"
    410                   "   :FORMAT,   :FILESIZE,    :BITRATE)"
    411                   );
    412     */
    413     query.bindValue(":DIRECTORY", directoryId);
    414     query.bindValue(":ARTIST", artistId);
    415     query.bindValue(":ALBUM", albumId);
     363
     364    query.bindValue(":DIRECTORY", m_directoryid);
     365    query.bindValue(":ARTIST", m_artistid);
     366    query.bindValue(":ALBUM", m_albumid);
    416367    query.bindValue(":TITLE", m_title.utf8());
    417     query.bindValue(":GENRE", genreId);
     368    query.bindValue(":GENRE", m_genreid);
    418369    query.bindValue(":YEAR", m_year);
    419370    query.bindValue(":TRACKNUM", m_tracknum);
    420371    query.bindValue(":LENGTH", m_length);
  • mythmusic/mythmusic/metadata.h

     
    4040                m_changed = false;
    4141                m_show = true;
    4242                m_format = lformat;
     43
     44                m_directoryid = -1;
     45                m_artistid = -1;
     46                m_albumid = -1;
     47                m_genreid = -1;
    4348            }
    4449
    4550    Metadata(const Metadata &other)
     
    8994    QString Genre() { return m_genre; }
    9095    void setGenre(const QString &lgenre) { m_genre = lgenre; }
    9196
     97    void setDirectoryId(int ldirectoryid) { m_directoryid = ldirectoryid; }
     98    int getDirectoryId() { return m_directoryid; }
     99    void setArtistId(int lartistid) { m_artistid = lartistid; }
     100    int getArtistId() { return m_artistid; }
     101    void setAlbumId(int lalbumid) { m_albumid = lalbumid; }
     102    int getAlbumId() { return m_albumid; }
     103    void setGenreId(int lgenreid) { m_genreid = lgenreid; }
     104    int getGenreId() { return m_genreid; }
     105
    92106    int Year() { return m_year; }
    93107    void setYear(int lyear) { m_year = lyear; }
    94  
     108
    95109    int Track() { return m_tracknum; }
    96110    void setTrack(int ltrack) { m_tracknum = ltrack; }
    97111
     
    159173    int m_tracknum;
    160174    int m_length;
    161175    int m_rating;
     176    int m_directoryid;
     177    int m_artistid;
     178    int m_compartistid;
     179    int m_albumid;
     180    int m_genreid;
    162181    QString m_lastplay;
    163182    int m_playcount;
    164183    bool m_compilation;
    165      
     184
    166185    unsigned int m_id;
    167186    QString m_filename;
    168187    bool    m_changed;
  • mythmusic/mythmusic/filescanner.h

     
    1313};
    1414
    1515typedef QMap <QString, MusicFileLocation> MusicLoadedMap;
     16typedef QMap<QString, int> IdCache;
    1617
    1718class FileScanner
    1819{
     
    2425
    2526    private:
    2627        void BuildFileList(QString &directory, MusicLoadedMap &music_files, int parentid);
     28        int  GetDirectoryId(const QString &directory, const int &parentid);
    2729        bool HasFileChanged(const QString &filename, const QString &date_modified);
    2830        void AddFileToDB(const QString &filename);
    2931        void RemoveFileFromDB (const QString &directory, const QString &filename);
    3032        void UpdateFileInDB(const QString &filename);
    3133
    3234        QString         m_startdir;
     35        IdCache         m_directoryid;
     36        IdCache         m_artistid;
     37        IdCache         m_genreid;
     38        IdCache         m_albumid;
     39
     40        Decoder             *m_decoder;
    3341};
    3442
    3543#endif // _FILESCANNER_H_
  • mythmusic/mythmusic/metaio.cpp

     
    4343
    4444    static QString regext = mFileExtension + "$";
    4545    int part_num = 0;
     46    filename.replace(QRegExp(QString("_")), QString(" "));
     47    filename.replace(QRegExp(regext, FALSE), QString(""));
    4648    QStringList fmt_list = QStringList::split("/", mFilenameFormat);
    4749    QStringList::iterator fmt_it = fmt_list.begin();
    4850
     
    5456    for(; fmt_it != fmt_list.end(); fmt_it++, part_num++)
    5557    {
    5658        QString part_str = filename.section( "/", part_num, part_num);
    57         part_str.replace(QRegExp(QString("_")), QString(" "));
    58         part_str.replace(QRegExp(regext, FALSE), QString(""));
    5959
    6060        if ( *fmt_it == "GENRE" )
    6161            genre = part_str;
  • mythmusic/mythmusic/filescanner.cpp

     
    1818
    1919FileScanner::FileScanner ()
    2020{
     21    MSqlQuery query(MSqlQuery::InitCon());
    2122
     23    // Cache the directory ids from the database
     24    query.prepare("SELECT directory_id, LOWER(path) FROM music_directories");
     25    if (query.exec() || query.isActive())
     26    {
     27        while(query.next())
     28        {
     29            m_directoryid[query.value(1).toString()] = query.value(0).toInt();
     30        }
     31    }
     32
     33    // Cache the genre ids from the database
     34    query.prepare("SELECT genre_id, LOWER(genre) FROM music_genres");
     35    if (query.exec() || query.isActive())
     36    {
     37        while(query.next())
     38        {
     39            m_genreid[query.value(1).toString()] = query.value(0).toInt();
     40        }
     41    }
     42
     43    // Cache the artist ids from the database
     44    query.prepare("SELECT artist_id, LOWER(artist_name) FROM music_artists");
     45    if (query.exec() || query.isActive())
     46    {
     47        while(query.next())
     48        {
     49            m_artistid[query.value(1).toString()] = query.value(0).toInt();
     50        }
     51    }
     52
     53    // Cache the album ids from the database
     54    query.prepare("SELECT album_id, artist_id, LOWER(album_name) FROM music_albums");
     55    if (query.exec() || query.isActive())
     56    {
     57        while(query.next())
     58        {
     59            m_albumid[query.value(1).toString() + "#" + query.value(2).toString()] = query.value(0).toInt();
     60        }
     61    }
    2262}
    2363
    2464FileScanner::~FileScanner ()
     
    4080    QFileInfoListIterator it(*list);
    4181    QFileInfo *fi;
    4282
    43     MSqlQuery query(MSqlQuery::InitCon());
    44 
    4583    /* Recursively traverse directory, calling QApplication::processEvents()
    4684       every now and then to ensure the UI updates */
    4785    int update_interval = 0;
     
    5593        if (fi->isDir())
    5694        {
    5795
    58             QString directory(filename);
    59             directory.remove(0, m_startdir.length());
    60             // Load the directory id or insert it and get the id
    61             query.prepare("SELECT directory_id FROM music_directories "
    62                         "WHERE path = :DIRECTORY ;");
    63             query.bindValue(":DIRECTORY", directory.utf8());
     96            QString dir(filename);
     97            dir.remove(0, m_startdir.length());
    6498
    65             if (query.exec() || query.isActive())
    66             {
    67                 if (query.size() == 0)
     99            newparentid = m_directoryid[dir.utf8().lower()];
     100
     101            if (newparentid == 0) {
     102                if ((m_directoryid[dir.utf8().lower()] = GetDirectoryId(dir, parentid)) > 0)
    68103                {
    69                     query.prepare("INSERT INTO music_directories (path, parent_id) "
    70                                   "VALUES (:DIRECTORY, :PARENTID);");
    71                     query.bindValue(":DIRECTORY", directory.utf8());
    72                     query.bindValue(":PARENTID", parentid);
    73 
    74                     if (!query.exec() || !query.isActive()
    75                     || query.numRowsAffected() <= 0)
    76                     {
    77                         MythContext::DBError("music insert directory", query);
    78                         return;
    79                     }
    80                     newparentid = query.lastInsertId().toInt();
     104                    newparentid = m_directoryid[dir.utf8().lower()];
    81105                }
    82106                else
    83107                {
    84                     query.next();
    85                     newparentid = query.value(0).toInt();
     108                    VERBOSE(VB_IMPORTANT, QString("Failed to get directory id for path %1").arg(dir).arg(m_directoryid[dir.utf8().lower()]));
    86109                }
    87110            }
    88             else
    89             {
    90                 MythContext::DBError("music select directory id", query);
    91                 return;
    92             }
    93111
    94112            BuildFileList(filename, music_files, newparentid);
     113
    95114            qApp->processEvents ();
    96115        }
    97116        else
     
    101120                qApp->processEvents();
    102121                update_interval = 0;
    103122            }
     123
    104124            music_files[filename] = kFileSystem;
    105125        }
    106126    }
    107127}
    108128
     129int FileScanner::GetDirectoryId(const QString &directory, const int &parentid)
     130{
     131    MSqlQuery query(MSqlQuery::InitCon());
     132
     133    // Load the directory id or insert it and get the id
     134    query.prepare("SELECT directory_id FROM music_directories "
     135                "WHERE path = :DIRECTORY ;");
     136    query.bindValue(":DIRECTORY", directory.utf8());
     137
     138    if (query.exec() || query.isActive())
     139    {
     140        if (query.next())
     141        {
     142            return query.value(0).toInt();
     143        }
     144        else
     145        {
     146            query.prepare("INSERT INTO music_directories (path, parent_id) "
     147                        "VALUES (:DIRECTORY, :PARENTID);");
     148            query.bindValue(":DIRECTORY", directory.utf8());
     149            query.bindValue(":PARENTID", parentid);
     150
     151            if (!query.exec() || !query.isActive()
     152            || query.numRowsAffected() <= 0)
     153            {
     154                MythContext::DBError("music insert directory", query);
     155                return -1;
     156            }
     157            return query.lastInsertId().toInt();
     158        }
     159    }
     160    else
     161    {
     162        MythContext::DBError("music select directory id", query);
     163        return -1;
     164    }
     165
     166    return -1;
     167}
     168
    109169bool FileScanner::HasFileChanged(const QString &filename, const QString &date_modified)
    110170{
    111171    struct stat stbuf;
    112172
    113     if (stat(filename.ascii(), &stbuf) == 0)
     173    if (stat(filename.local8Bit(), &stbuf) == 0)
    114174    {
    115175        if (date_modified.isEmpty() ||
    116176            stbuf.st_mtime >
     
    121181        }
    122182    }
    123183    else {
    124         VERBOSE(VB_IMPORTANT, QString("Failed to stat file: %1").arg(filename.ascii()));
     184        VERBOSE(VB_IMPORTANT, QString("Failed to stat file: %1")
     185            .arg(filename.local8Bit()));
    125186    }
    126187    return false;
    127188}
     
    132193
    133194    if (decoder)
    134195    {
    135         Metadata *data = decoder->getMetadata();
     196        Metadata *data = decoder->readMetadata();
    136197        if (data) {
     198            QString directory = filename;
     199            directory.remove(0, m_startdir.length());
     200            directory = directory.section( '/', 0, -2);
     201
     202            QString album_cache_string;
     203
     204            // Set values from cache
     205            if (m_directoryid[directory.utf8().lower()] > 0)
     206                data->setDirectoryId(m_directoryid[directory.utf8().lower()]);
     207            if (m_artistid[data->Artist().utf8().lower()] > 0)
     208            {
     209                data->setArtistId(m_artistid[data->Artist().utf8().lower()]);
     210
     211                // The album cache depends on the artist id
     212                album_cache_string = data->getArtistId() + "#"
     213                    + data->Album().utf8().lower();
     214
     215                if (m_albumid[album_cache_string] > 0)
     216                    data->setAlbumId(m_albumid[album_cache_string]);
     217            }
     218            if (m_genreid[data->Genre().utf8().lower()] > 0)
     219                data->setGenreId(m_genreid[data->Genre().utf8().lower()]);
     220
     221            // Commit track info to database
    137222            data->dumpToDatabase();
     223
     224            // Update the cache
     225            m_artistid[data->Artist().utf8().lower()] = data->getArtistId();
     226            m_genreid[data->Genre().utf8().lower()] = data->getGenreId();
     227            album_cache_string = data->getArtistId() + "#"
     228                + data->Album().utf8().lower();
     229            m_albumid[album_cache_string] = data->getAlbumId();
    138230            delete data;
    139231        }
    140232
     
    143235}
    144236
    145237// Remove a file from the database
    146 void FileScanner::RemoveFileFromDB (const QString &directory, const QString &filename)
     238void FileScanner::RemoveFileFromDB (const QString &directory,
     239const QString &filename)
    147240{
    148241    QString sqlfilename(filename);
    149242    // We know that the filename will not contain :// as the SQL limits this
    150243    sqlfilename.remove(0, directory.length());
     244    sqlfilename = sqlfilename.section( '/', -1 ) ;
    151245    MSqlQuery query(MSqlQuery::InitCon());
    152246    query.prepare("DELETE FROM music_songs WHERE "
    153247                  "filename = :NAME ;");
     
    166260
    167261        if (db_meta && disk_meta)
    168262        {
     263            QString directory = filename;
     264            directory.remove(0, m_startdir.length());
     265            directory = directory.section( '/', 0, -2);
     266
    169267            disk_meta->setID(db_meta->ID());
    170268            disk_meta->setRating(db_meta->Rating());
     269
     270            QString album_cache_string;
     271
     272            // Set values from cache
     273            if (m_directoryid[directory.utf8().lower()] > 0)
     274                disk_meta->setDirectoryId(m_directoryid[directory
     275                    .utf8().lower()]);
     276            if (m_artistid[disk_meta->Artist().utf8().lower()] > 0)
     277            {
     278                disk_meta->setArtistId(m_artistid[disk_meta->Artist()
     279                    .utf8().lower()]);
     280
     281                // The album cache depends on the artist id
     282                album_cache_string = disk_meta->getArtistId() + "#"
     283                    + disk_meta->Album().utf8().lower();
     284
     285                if (m_albumid[album_cache_string] > 0)
     286                    disk_meta->setAlbumId(m_albumid[album_cache_string]);
     287            }
     288            if (m_genreid[disk_meta->Genre().utf8().lower()] > 0)
     289                disk_meta->setGenreId(m_genreid[disk_meta->Genre()
     290                    .utf8().lower()]);
     291
     292            // Commit track info to database
    171293            disk_meta->dumpToDatabase();
     294
     295            // Update the cache
     296            m_artistid[disk_meta->Artist().utf8().lower()]
     297                = disk_meta->getArtistId();
     298            m_genreid[disk_meta->Genre().utf8().lower()]
     299                = disk_meta->getGenreId();
     300            album_cache_string = disk_meta->getArtistId() + "#"
     301                + disk_meta->Album().utf8().lower();
     302            m_albumid[album_cache_string] = disk_meta->getAlbumId();
    172303        }
    173304
    174305        if (disk_meta)
     
    269400    file_checking->Close();
    270401    delete file_checking;
    271402}
    272