DatabasePlugin: add method VisitUniqueTags()
Optimize the ProxyDatabase by invoking "list" on the peer, instead of visiting all songs.
This commit is contained in:
parent
4e1eb03287
commit
a6ac0f8965
@ -438,6 +438,7 @@ endif
|
|||||||
|
|
||||||
libdb_plugins_a_SOURCES = \
|
libdb_plugins_a_SOURCES = \
|
||||||
src/DatabaseRegistry.cxx src/DatabaseRegistry.hxx \
|
src/DatabaseRegistry.cxx src/DatabaseRegistry.hxx \
|
||||||
|
src/DatabaseHelpers.cxx src/DatabaseHelpers.hxx \
|
||||||
src/db/SimpleDatabasePlugin.cxx src/db/SimpleDatabasePlugin.hxx
|
src/db/SimpleDatabasePlugin.cxx src/db/SimpleDatabasePlugin.hxx
|
||||||
|
|
||||||
if HAVE_LIBMPDCLIENT
|
if HAVE_LIBMPDCLIENT
|
||||||
|
78
src/DatabaseHelpers.cxx
Normal file
78
src/DatabaseHelpers.cxx
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2003-2012 The Music Player Daemon Project
|
||||||
|
* http://www.musicpd.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "DatabaseHelpers.hxx"
|
||||||
|
#include "DatabasePlugin.hxx"
|
||||||
|
#include "song.h"
|
||||||
|
#include "tag.h"
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
struct StringLess {
|
||||||
|
gcc_pure
|
||||||
|
bool operator()(const char *a, const char *b) const {
|
||||||
|
return strcmp(a, b) < 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef std::set<const char *, StringLess> StringSet;
|
||||||
|
|
||||||
|
static bool
|
||||||
|
CollectTags(StringSet &set, enum tag_type tag_type, song &song)
|
||||||
|
{
|
||||||
|
struct tag *tag = song.tag;
|
||||||
|
if (tag == nullptr)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
bool found = false;
|
||||||
|
for (unsigned i = 0; i < tag->num_items; ++i) {
|
||||||
|
if (tag->items[i]->type == tag_type) {
|
||||||
|
set.insert(tag->items[i]->value);
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!found)
|
||||||
|
set.insert("");
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
VisitUniqueTags(const Database &db, const DatabaseSelection &selection,
|
||||||
|
enum tag_type tag_type,
|
||||||
|
VisitString visit_string,
|
||||||
|
GError **error_r)
|
||||||
|
{
|
||||||
|
StringSet set;
|
||||||
|
|
||||||
|
using namespace std::placeholders;
|
||||||
|
const auto f = std::bind(CollectTags, std::ref(set), tag_type, _1);
|
||||||
|
if (!db.Visit(selection, f, error_r))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (auto value : set)
|
||||||
|
if (!visit_string(value, error_r))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
36
src/DatabaseHelpers.hxx
Normal file
36
src/DatabaseHelpers.hxx
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2003-2012 The Music Player Daemon Project
|
||||||
|
* http://www.musicpd.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MPD_MEMORY_DATABASE_PLUGIN_HXX
|
||||||
|
#define MPD_MEMORY_DATABASE_PLUGIN_HXX
|
||||||
|
|
||||||
|
#include "DatabaseVisitor.hxx"
|
||||||
|
#include "tag.h"
|
||||||
|
#include "gcc.h"
|
||||||
|
|
||||||
|
class Database;
|
||||||
|
struct DatabaseSelection;
|
||||||
|
|
||||||
|
bool
|
||||||
|
VisitUniqueTags(const Database &db, const DatabaseSelection &selection,
|
||||||
|
enum tag_type tag_type,
|
||||||
|
VisitString visit_string,
|
||||||
|
GError **error_r);
|
||||||
|
|
||||||
|
#endif
|
@ -29,6 +29,10 @@
|
|||||||
#include "DatabaseVisitor.hxx"
|
#include "DatabaseVisitor.hxx"
|
||||||
#include "gcc.h"
|
#include "gcc.h"
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include "tag.h"
|
||||||
|
}
|
||||||
|
|
||||||
struct config_param;
|
struct config_param;
|
||||||
struct DatabaseSelection;
|
struct DatabaseSelection;
|
||||||
struct db_visitor;
|
struct db_visitor;
|
||||||
@ -82,6 +86,14 @@ public:
|
|||||||
GError **error_r) const {
|
GError **error_r) const {
|
||||||
return Visit(selection, VisitDirectory(), visit_song, error_r);
|
return Visit(selection, VisitDirectory(), visit_song, error_r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Visit all unique tag values.
|
||||||
|
*/
|
||||||
|
virtual bool VisitUniqueTags(const DatabaseSelection &selection,
|
||||||
|
enum tag_type tag_type,
|
||||||
|
VisitString visit_string,
|
||||||
|
GError **error_r) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct DatabasePlugin {
|
struct DatabasePlugin {
|
||||||
|
@ -38,7 +38,6 @@ extern "C" {
|
|||||||
#include "DatabasePlugin.hxx"
|
#include "DatabasePlugin.hxx"
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <set>
|
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
PrintDirectory(struct client *client, const directory &directory)
|
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);
|
return db_selection_print(client, selection, true, error_r);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct StringLess {
|
static bool
|
||||||
gcc_pure
|
PrintSongURIVisitor(struct client *client, song &song)
|
||||||
bool operator()(const char *a, const char *b) const {
|
|
||||||
return strcmp(a, b) < 0;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef std::set<const char *, StringLess> StringSet;
|
|
||||||
|
|
||||||
static void
|
|
||||||
visitTag(struct client *client, StringSet &set,
|
|
||||||
song &song, enum tag_type tagType)
|
|
||||||
{
|
{
|
||||||
struct tag *tag = song.tag;
|
song_print_uri(client, &song);
|
||||||
bool found = false;
|
|
||||||
|
|
||||||
if (tagType == LOCATE_TAG_FILE_TYPE) {
|
return true;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!found)
|
|
||||||
set.insert("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
unique_tags_visitor_song(struct client *client,
|
PrintUniqueTag(struct client *client, enum tag_type tag_type,
|
||||||
enum tag_type tag_type,
|
const char *value)
|
||||||
StringSet &set, song &song)
|
|
||||||
{
|
{
|
||||||
visitTag(client, set, song, tag_type);
|
client_printf(client, "%s: %s\n", tag_item_names[tag_type], value);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -238,20 +208,16 @@ listAllUniqueTags(struct client *client, int type,
|
|||||||
{
|
{
|
||||||
const DatabaseSelection selection("", true, criteria);
|
const DatabaseSelection selection("", true, criteria);
|
||||||
|
|
||||||
StringSet set;
|
if (type == LOCATE_TAG_FILE_TYPE) {
|
||||||
|
using namespace std::placeholders;
|
||||||
using namespace std::placeholders;
|
const auto f = std::bind(PrintSongURIVisitor, client, _1);
|
||||||
const auto f = std::bind(unique_tags_visitor_song, client,
|
return GetDatabase()->Visit(selection, f, error_r);
|
||||||
(enum tag_type)type, std::ref(set),
|
} else {
|
||||||
_1);
|
using namespace std::placeholders;
|
||||||
if (!GetDatabase()->Visit(selection, f, error_r))
|
const auto f = std::bind(PrintUniqueTag, client,
|
||||||
return false;
|
(enum tag_type)type, _1);
|
||||||
|
return GetDatabase()->VisitUniqueTags(selection,
|
||||||
if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES)
|
(enum tag_type)type,
|
||||||
for (auto value : set)
|
f, error_r);
|
||||||
client_printf(client, "%s: %s\n",
|
}
|
||||||
tag_item_names[type],
|
|
||||||
value);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
@ -33,4 +33,6 @@ typedef std::function<bool(struct song &, GError **)> VisitSong;
|
|||||||
typedef std::function<bool(const playlist_metadata &, const directory &,
|
typedef std::function<bool(const playlist_metadata &, const directory &,
|
||||||
GError **)> VisitPlaylist;
|
GError **)> VisitPlaylist;
|
||||||
|
|
||||||
|
typedef std::function<bool(const char *, GError **)> VisitString;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -62,6 +62,11 @@ public:
|
|||||||
VisitPlaylist visit_playlist,
|
VisitPlaylist visit_playlist,
|
||||||
GError **error_r) const override;
|
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:
|
protected:
|
||||||
bool Configure(const struct config_param *param, GError **error_r);
|
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 }
|
{ 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
|
static bool
|
||||||
CheckError(const struct mpd_connection *connection, GError **error_r)
|
CheckError(const struct mpd_connection *connection, GError **error_r)
|
||||||
{
|
{
|
||||||
@ -368,6 +384,42 @@ ProxyDatabase::Visit(const DatabaseSelection &selection,
|
|||||||
return success;
|
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 = {
|
const DatabasePlugin proxy_db_plugin = {
|
||||||
"proxy",
|
"proxy",
|
||||||
ProxyDatabase::Create,
|
ProxyDatabase::Create,
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "SimpleDatabasePlugin.hxx"
|
#include "SimpleDatabasePlugin.hxx"
|
||||||
#include "DatabaseSelection.hxx"
|
#include "DatabaseSelection.hxx"
|
||||||
|
#include "DatabaseHelpers.hxx"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "db_error.h"
|
#include "db_error.h"
|
||||||
@ -269,6 +270,16 @@ SimpleDatabase::Visit(const DatabaseSelection &selection,
|
|||||||
return ret;
|
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
|
bool
|
||||||
SimpleDatabase::Save(GError **error_r)
|
SimpleDatabase::Save(GError **error_r)
|
||||||
{
|
{
|
||||||
|
@ -66,6 +66,11 @@ public:
|
|||||||
VisitPlaylist visit_playlist,
|
VisitPlaylist visit_playlist,
|
||||||
GError **error_r) const override;
|
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:
|
protected:
|
||||||
bool Configure(const struct config_param *param, GError **error_r);
|
bool Configure(const struct config_param *param, GError **error_r);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user