From 5fdffc292d09bdc408ea33619d501a2b39176262 Mon Sep 17 00:00:00 2001 From: casey langen Date: Thu, 30 Nov 2017 10:10:09 -0800 Subject: [PATCH] Small update to Indexer to ensure playlist sort order is always sequential with no holes. --- src/core/library/Indexer.cpp | 36 ++++++++++++++++++++++++++++++++++ src/core/library/Indexer.h | 6 +++--- src/plugins/server/Constants.h | 1 + 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/core/library/Indexer.cpp b/src/core/library/Indexer.cpp index 4f50532be..dcf3526d7 100644 --- a/src/core/library/Indexer.cpp +++ b/src/core/library/Indexer.cpp @@ -695,6 +695,42 @@ 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(); + } + } + } + /* optimize and shrink */ this->dbConnection.Execute("VACUUM"); } diff --git a/src/core/library/Indexer.h b/src/core/library/Indexer.h index 8e552ee6a..f9e4ef7b0 100644 --- a/src/core/library/Indexer.h +++ b/src/core/library/Indexer.h @@ -103,13 +103,13 @@ namespace musik { namespace core { }; typedef std::vector > TagReaderList; + musik::core::sdk::ITagReader>> TagReaderList; typedef std::vector > DecoderList; + musik::core::sdk::IDecoderFactory>> DecoderList; typedef std::vector > IndexerSourceList; + musik::core::sdk::IIndexerSource>> IndexerSourceList; void ThreadLoop(); diff --git a/src/plugins/server/Constants.h b/src/plugins/server/Constants.h index 34fac2219..dfa36fb4f 100644 --- a/src/plugins/server/Constants.h +++ b/src/plugins/server/Constants.h @@ -117,6 +117,7 @@ namespace key { static const std::string playlist_name = "playlist_name"; static const std::string subquery = "subquery"; static const std::string type = "type"; + static const std::string sort_order = "sort_order"; static const std::string sort_orders = "sort_orders"; static const std::string predicate_category = "predicate_category"; static const std::string predicate_id = "predicate_id";