From a6ac0f89656b9ef374703d24bbb27316a705eadc Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 15 Aug 2012 21:32:34 +0200 Subject: DatabasePlugin: add method VisitUniqueTags() Optimize the ProxyDatabase by invoking "list" on the peer, instead of visiting all songs. --- src/db/ProxyDatabasePlugin.cxx | 52 +++++++++++++++++++++++++++++++++++++++++ src/db/SimpleDatabasePlugin.cxx | 11 +++++++++ src/db/SimpleDatabasePlugin.hxx | 5 ++++ 3 files changed, 68 insertions(+) (limited to 'src/db') diff --git a/src/db/ProxyDatabasePlugin.cxx b/src/db/ProxyDatabasePlugin.cxx index 68ba5a542..f06728f80 100644 --- a/src/db/ProxyDatabasePlugin.cxx +++ b/src/db/ProxyDatabasePlugin.cxx @@ -62,6 +62,11 @@ public: VisitPlaylist visit_playlist, GError **error_r) const override; + virtual bool VisitUniqueTags(const DatabaseSelection &selection, + enum tag_type tag_type, + VisitString visit_string, + GError **error_r) const override; + protected: bool Configure(const struct config_param *param, GError **error_r); }; @@ -97,6 +102,17 @@ static constexpr struct { { TAG_NUM_OF_ITEM_TYPES, MPD_TAG_COUNT } }; +G_GNUC_CONST +static enum mpd_tag_type +Convert(enum tag_type tag_type) +{ + for (auto i = tag_table; i->d != TAG_NUM_OF_ITEM_TYPES; ++i) + if (i->d == tag_type) + return i->s; + + return MPD_TAG_COUNT; +} + static bool CheckError(const struct mpd_connection *connection, GError **error_r) { @@ -368,6 +384,42 @@ ProxyDatabase::Visit(const DatabaseSelection &selection, return success; } +bool +ProxyDatabase::VisitUniqueTags(const DatabaseSelection &selection, + enum tag_type tag_type, + VisitString visit_string, + GError **error_r) const +{ + enum mpd_tag_type tag_type2 = Convert(tag_type); + if (tag_type2 == MPD_TAG_COUNT) { + g_set_error_literal(error_r, libmpdclient_quark(), 0, + "Unsupported tag"); + return false; + } + + if (!mpd_search_db_tags(connection, tag_type2)) + return CheckError(connection, error_r); + + // TODO: match + (void)selection; + + if (!mpd_search_commit(connection)) + return CheckError(connection, error_r); + + bool result = true; + + struct mpd_pair *pair; + while (result && + (pair = mpd_recv_pair_tag(connection, tag_type2)) != nullptr) { + result = visit_string(pair->value, error_r); + mpd_return_pair(connection, pair); + } + + return mpd_response_finish(connection) && + CheckError(connection, error_r) && + result; +} + const DatabasePlugin proxy_db_plugin = { "proxy", ProxyDatabase::Create, diff --git a/src/db/SimpleDatabasePlugin.cxx b/src/db/SimpleDatabasePlugin.cxx index a4077eb27..ed166de45 100644 --- a/src/db/SimpleDatabasePlugin.cxx +++ b/src/db/SimpleDatabasePlugin.cxx @@ -20,6 +20,7 @@ #include "config.h" #include "SimpleDatabasePlugin.hxx" #include "DatabaseSelection.hxx" +#include "DatabaseHelpers.hxx" extern "C" { #include "db_error.h" @@ -269,6 +270,16 @@ SimpleDatabase::Visit(const DatabaseSelection &selection, return ret; } +bool +SimpleDatabase::VisitUniqueTags(const DatabaseSelection &selection, + enum tag_type tag_type, + VisitString visit_string, + GError **error_r) const +{ + return ::VisitUniqueTags(*this, selection, tag_type, visit_string, + error_r); +} + bool SimpleDatabase::Save(GError **error_r) { diff --git a/src/db/SimpleDatabasePlugin.hxx b/src/db/SimpleDatabasePlugin.hxx index 1e990de13..0b7e838b5 100644 --- a/src/db/SimpleDatabasePlugin.hxx +++ b/src/db/SimpleDatabasePlugin.hxx @@ -66,6 +66,11 @@ public: VisitPlaylist visit_playlist, GError **error_r) const override; + virtual bool VisitUniqueTags(const DatabaseSelection &selection, + enum tag_type tag_type, + VisitString visit_string, + GError **error_r) const override; + protected: bool Configure(const struct config_param *param, GError **error_r); -- cgit v1.2.3