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/DatabasePrint.cxx | 72 ++++++++++++++------------------------------------- 1 file changed, 19 insertions(+), 53 deletions(-) (limited to 'src/DatabasePrint.cxx') diff --git a/src/DatabasePrint.cxx b/src/DatabasePrint.cxx index d22d4db8a..b58619a95 100644 --- a/src/DatabasePrint.cxx +++ b/src/DatabasePrint.cxx @@ -38,7 +38,6 @@ extern "C" { #include "DatabasePlugin.hxx" #include -#include static bool PrintDirectory(struct client *client, const directory &directory) @@ -186,48 +185,19 @@ printInfoForAllIn(struct client *client, const char *uri_utf8, return db_selection_print(client, selection, true, error_r); } -struct StringLess { - gcc_pure - bool operator()(const char *a, const char *b) const { - return strcmp(a, b) < 0; - } -}; - -typedef std::set StringSet; - -static void -visitTag(struct client *client, StringSet &set, - song &song, enum tag_type tagType) +static bool +PrintSongURIVisitor(struct client *client, song &song) { - struct tag *tag = song.tag; - bool found = false; - - if (tagType == LOCATE_TAG_FILE_TYPE) { - song_print_uri(client, &song); - return; - } - - if (!tag) - return; - - for (unsigned i = 0; i < tag->num_items; i++) { - if (tag->items[i]->type == tagType) { - set.insert(tag->items[i]->value); - found = true; - } - } + song_print_uri(client, &song); - if (!found) - set.insert(""); + return true; } static bool -unique_tags_visitor_song(struct client *client, - enum tag_type tag_type, - StringSet &set, song &song) +PrintUniqueTag(struct client *client, enum tag_type tag_type, + const char *value) { - visitTag(client, set, song, tag_type); - + client_printf(client, "%s: %s\n", tag_item_names[tag_type], value); return true; } @@ -238,20 +208,16 @@ listAllUniqueTags(struct client *client, int type, { const DatabaseSelection selection("", true, criteria); - StringSet set; - - using namespace std::placeholders; - const auto f = std::bind(unique_tags_visitor_song, client, - (enum tag_type)type, std::ref(set), - _1); - if (!GetDatabase()->Visit(selection, f, error_r)) - return false; - - if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) - for (auto value : set) - client_printf(client, "%s: %s\n", - tag_item_names[type], - value); - - return true; + if (type == LOCATE_TAG_FILE_TYPE) { + using namespace std::placeholders; + const auto f = std::bind(PrintSongURIVisitor, client, _1); + return GetDatabase()->Visit(selection, f, error_r); + } else { + using namespace std::placeholders; + const auto f = std::bind(PrintUniqueTag, client, + (enum tag_type)type, _1); + return GetDatabase()->VisitUniqueTags(selection, + (enum tag_type)type, + f, error_r); + } } -- cgit v1.2.3