Playlist track sort order cleanup, part two.

This commit is contained in:
casey langen 2017-11-30 10:41:48 -08:00
parent 5fdffc292d
commit 153cc9c708
3 changed files with 48 additions and 36 deletions

View File

@ -53,7 +53,7 @@ namespace musik { namespace core {
enum class SyncType{
All,
Local,
Sources,
Sources
};
virtual ~IIndexer() { }

View File

@ -695,46 +695,57 @@ void Indexer::SyncCleanup() {
}
}
/* make sure playlist sort orders are always sequential without holes. we
do this anyway, as playlists are updated, but there's no way to guarantee
it stays this way -- plugins, external processes, etc can cause problems */
{
db::Statement playlists("SELECT DISTINCT id FROM playlists", this->dbConnection);
while (playlists.Step() == db::Row) {
db::Statement tracks(
"SELECT track_external_id, sort_order "
"FROM playlist_tracks WHERE playlist_id=? "
"ORDER BY sort_order",
this->dbConnection);
int64_t playlistId = playlists.ColumnInt64(0);
tracks.BindInt64(0, playlistId);
db::Statement update(
"UPDATE playlist_tracks "
"SET sort_order=? "
"WHERE track_external_id=? AND sort_order=?",
this->dbConnection);
int order = 0;
while (tracks.Step() == db::Row) {
std::string externalId = tracks.ColumnText(0);
int sortOrder = tracks.ColumnInt32(1);
update.ResetAndUnbind();
update.BindInt32(0, order++);
update.BindText(1, externalId);
update.BindInt32(2, sortOrder);
update.Step();
}
}
}
this->SyncPlaylistTracksOrder();
/* optimize and shrink */
this->dbConnection.Execute("VACUUM");
}
void Indexer::SyncPlaylistTracksOrder() {
/* make sure playlist sort orders are always sequential without holes. we
do this anyway, as playlists are updated, but there's no way to guarantee
it stays this way -- plugins, external processes, etc can cause problems */
db::Statement playlists(
"SELECT DISTINCT id FROM playlists",
this->dbConnection);
db::Statement tracks(
"SELECT track_external_id, sort_order "
"FROM playlist_tracks WHERE playlist_id=? "
"ORDER BY sort_order",
this->dbConnection);
db::Statement update(
"UPDATE playlist_tracks "
"SET sort_order=? "
"WHERE track_external_id=? AND sort_order=?",
this->dbConnection);
struct Record { std::string id; int order; };
while (playlists.Step() == db::Row) {
tracks.ResetAndUnbind();
tracks.BindInt64(0, playlists.ColumnInt64(0));
/* gotta cache these in memory because we can't update the
table at the same time we're iterating */
std::vector<Record> records;
while (tracks.Step() == db::Row) {
records.push_back({ tracks.ColumnText(0), tracks.ColumnInt32(1) });
}
int order = 0;
for (auto& r : records) {
update.ResetAndUnbind();
update.BindInt32(0, order++);
update.BindText(1, r.id);
update.BindInt32(2, r.order);
update.Step();
}
}
}
void Indexer::GetPaths(std::vector<std::string>& paths) {
boost::mutex::scoped_lock lock(this->stateMutex);
std::copy(this->paths.begin(), this->paths.end(), std::back_inserter(paths));

View File

@ -120,6 +120,7 @@ namespace musik { namespace core {
void FinalizeSync(const SyncContext& context);
void SyncDelete();
void SyncCleanup();
void SyncPlaylistTracksOrder();
musik::core::sdk::ScanResult SyncSource(musik::core::sdk::IIndexerSource* source);
void ProcessAddRemoveQueue();
void SyncOptimize();