![]() |
Mixxx
|
00001 // trackcache.h 00002 // Created 7/3/2011 by RJ Ryan (rryan@mit.edu) 00003 00004 #ifndef BASETRACKCACHE_H 00005 #define BASETRACKCACHE_H 00006 00007 #include <QList> 00008 #include <QObject> 00009 #include <QSet> 00010 #include <QHash> 00011 #include <QString> 00012 #include <QStringList> 00013 #include <QSqlDatabase> 00014 #include <QVector> 00015 00016 #include "library/dao/trackdao.h" 00017 #include "trackinfoobject.h" 00018 #include "util.h" 00019 00020 class TrackCollection; 00021 00022 // BaseTrackCache is a cache of all of the values in certain table. It supports 00023 // searching and sorting of tracks by values within the table. The reasoning for 00024 // this is that previously there was a per-table-model cache which was largely a 00025 // waste of memory because all the table-models were caching the same data 00026 // (track properties). Furthermore, the base SQL tables of these table-models 00027 // involve complicated joins, which are very slow. 00028 class BaseTrackCache : public QObject { 00029 Q_OBJECT 00030 public: 00031 BaseTrackCache(TrackCollection* pTrackCollection, 00032 QString tableName, 00033 QString idColumn, 00034 QList<QString> columns, 00035 bool isCaching); 00036 virtual ~BaseTrackCache(); 00037 00038 // Rebuild the BaseTrackCache index from the SQL table. This can be 00039 // expensive on large tables. 00040 virtual void buildIndex(); 00041 00043 // Data access methods 00045 00046 virtual QVariant data(int trackId, int column) const; 00047 virtual const QStringList columns() const; 00048 virtual int columnCount() const; 00049 virtual int fieldIndex(const QString column) const; 00050 virtual void filterAndSort(const QSet<int>& trackIds, 00051 QString query, QString extraFilter, 00052 int sortColumn, Qt::SortOrder sortOrder, 00053 QHash<int, int>* trackToIndex); 00054 virtual bool isCached(int trackId) const; 00055 virtual void ensureCached(int trackId); 00056 virtual void ensureCached(QSet<int> trackIds); 00057 00058 signals: 00059 void tracksChanged(QSet<int> trackIds); 00060 00061 private slots: 00062 void slotTracksAdded(QSet<int> trackId); 00063 void slotTracksRemoved(QSet<int> trackId); 00064 void slotTrackDirty(int trackId); 00065 void slotTrackClean(int trackId); 00066 void slotTrackChanged(int trackId); 00067 00068 private: 00069 TrackPointer lookupCachedTrack(int trackId) const; 00070 bool updateIndexWithQuery(QString query); 00071 void updateTrackInIndex(int trackId); 00072 void updateTracksInIndex(QSet<int> trackIds); 00073 QVariant getTrackValueForColumn(TrackPointer pTrack, int column) const; 00074 00075 QString filterClause(QString query, QString extraFilter, 00076 QStringList idStrings) const; 00077 QString orderByClause(int sortColumn, Qt::SortOrder sortOrder) const; 00078 int findSortInsertionPoint(TrackPointer pTrack, 00079 const int sortColumn, 00080 const Qt::SortOrder sortOrder, 00081 const QVector<int> trackIds) const; 00082 int compareColumnValues(int sortColumn, Qt::SortOrder sortOrder, 00083 QVariant val1, QVariant val2) const; 00084 bool trackMatches(const TrackPointer& pTrack, 00085 const QRegExp& matcher) const; 00086 00087 const QString m_tableName; 00088 const QString m_idColumn; 00089 const QStringList m_columns; 00090 const QString m_columnsJoined; 00091 const QHash<QString, int> m_columnIndex; 00092 00093 QStringList m_searchColumns; 00094 QVector<int> m_searchColumnIndices; 00095 00096 // Temporary storage for filterAndSort() 00097 00098 QVector<int> m_trackOrder; 00099 00100 QSet<int> m_dirtyTracks; 00101 00102 bool m_bIndexBuilt; 00103 bool m_bIsCaching; 00104 QHash<int, QVector<QVariant> > m_trackInfo; 00105 TrackCollection* m_pTrackCollection; 00106 TrackDAO& m_trackDAO; 00107 QSqlDatabase m_database; 00108 00109 DISALLOW_COPY_AND_ASSIGN(BaseTrackCache); 00110 }; 00111 00112 #endif // BASETRACKCACHE_H