Merge branch 'v0.20.x'

This commit is contained in:
Max Kellermann
2018-10-22 18:19:04 +02:00
22 changed files with 319 additions and 375 deletions

View File

@@ -25,6 +25,7 @@
#include "client/Response.hxx"
#include "song/LightSong.hxx"
#include "tag/Tag.hxx"
#include "tag/VisitFallback.hxx"
#include "TagPrint.hxx"
#include <functional>
@@ -73,24 +74,15 @@ stats_visitor_song(SearchStats &stats, const LightSong &song) noexcept
stats.total_duration += duration;
}
static bool
CollectGroupCounts(TagCountMap &map, TagType group, const Tag &tag) noexcept
static void
CollectGroupCounts(TagCountMap &map, const Tag &tag,
const char *value) noexcept
{
bool found = false;
for (const auto &item : tag) {
if (item.type == group) {
auto r = map.insert(std::make_pair(item.value,
SearchStats()));
SearchStats &s = r.first->second;
++s.n_songs;
if (!tag.duration.IsNegative())
s.total_duration += tag.duration;
found = true;
}
}
return found;
auto r = map.insert(std::make_pair(value, SearchStats()));
SearchStats &s = r.first->second;
++s.n_songs;
if (!tag.duration.IsNegative())
s.total_duration += tag.duration;
}
static void
@@ -98,9 +90,10 @@ GroupCountVisitor(TagCountMap &map, TagType group,
const LightSong &song) noexcept
{
const Tag &tag = song.tag;
if (!CollectGroupCounts(map, group, tag) && group == TAG_ALBUM_ARTIST)
/* fall back to "Artist" if no "AlbumArtist" was found */
CollectGroupCounts(map, TAG_ARTIST, tag);
VisitTagWithFallbackOrEmpty(tag, group,
std::bind(CollectGroupCounts, std::ref(map),
std::cref(tag),
std::placeholders::_1));
}
void

View File

@@ -187,22 +187,34 @@ PrintSongUris(Response &r, Partition &partition,
}
static void
PrintUniqueTag(Response &r, TagType tag_type,
const Tag &tag) noexcept
PrintUniqueTags(Response &r, TagType tag_type,
const std::set<std::string> &values)
{
const char *value = tag.GetValue(tag_type);
assert(value != nullptr);
tag_print(r, tag_type, value);
const char *const name = tag_item_names[tag_type];
for (const auto &i : values)
r.Format("%s: %s\n", name, i.c_str());
}
const auto tag_mask = r.GetTagMask();
for (const auto &item : tag)
if (item.type != tag_type && tag_mask.Test(item.type))
tag_print(r, item.type, item.value);
static void
PrintGroupedUniqueTags(Response &r, TagType tag_type, TagType group,
const std::map<std::string, std::set<std::string>> &groups)
{
if (group == TAG_NUM_OF_ITEM_TYPES) {
for (const auto &i : groups)
PrintUniqueTags(r, tag_type, i.second);
return;
}
const char *const group_name = tag_item_names[group];
for (const auto &i : groups) {
r.Format("%s: %s\n", group_name, i.first.c_str());
PrintUniqueTags(r, tag_type, i.second);
}
}
void
PrintUniqueTags(Response &r, Partition &partition,
TagType type, TagMask group_mask,
TagType type, TagType group,
const SongFilter *filter)
{
assert(type < TAG_NUM_OF_ITEM_TYPES);
@@ -211,7 +223,6 @@ PrintUniqueTags(Response &r, Partition &partition,
const DatabaseSelection selection("", true, filter);
using namespace std::placeholders;
const auto f = std::bind(PrintUniqueTag, std::ref(r), type, _1);
db.VisitUniqueTags(selection, type, group_mask, f);
PrintGroupedUniqueTags(r, type, group,
db.CollectUniqueTags(selection, type, group));
}

View File

@@ -45,7 +45,7 @@ PrintSongUris(Response &r, Partition &partition,
void
PrintUniqueTags(Response &r, Partition &partition,
TagType type, TagMask group_mask,
TagType type, TagType group,
const SongFilter *filter);
#endif

View File

@@ -25,6 +25,9 @@
#include "util/Compiler.h"
#include <chrono>
#include <map>
#include <set>
#include <string>
struct DatabasePlugin;
struct DatabaseStats;
@@ -105,14 +108,9 @@ public:
return Visit(selection, VisitDirectory(), visit_song);
}
/**
* Visit all unique tag values.
*
* Throws on error.
*/
virtual void VisitUniqueTags(const DatabaseSelection &selection,
TagType tag_type, TagMask group_mask,
VisitTag visit_tag) const = 0;
virtual std::map<std::string, std::set<std::string>> CollectUniqueTags(const DatabaseSelection &selection,
TagType tag_type,
TagType group=TAG_NUM_OF_ITEM_TYPES) const = 0;
/**
* Throws on error.

View File

@@ -20,34 +20,42 @@
#include "UniqueTags.hxx"
#include "Interface.hxx"
#include "song/LightSong.hxx"
#include "tag/Set.hxx"
#include "tag/Mask.hxx"
#include "tag/VisitFallback.hxx"
#include <functional>
#include <assert.h>
static void
CollectTags(TagSet &set, TagType tag_type, TagMask group_mask,
const LightSong &song)
CollectTags(std::set<std::string> &result,
const Tag &tag,
TagType tag_type) noexcept
{
const Tag &tag = song.tag;
set.InsertUnique(tag, tag_type, group_mask);
VisitTagWithFallbackOrEmpty(tag, tag_type, [&result](const char *value){
result.emplace(value);
});
}
void
VisitUniqueTags(const Database &db, const DatabaseSelection &selection,
TagType tag_type, TagMask group_mask,
VisitTag visit_tag)
static void
CollectGroupTags(std::map<std::string, std::set<std::string>> &result,
const Tag &tag,
TagType tag_type,
TagType group) noexcept
{
TagSet set;
using namespace std::placeholders;
const auto f = std::bind(CollectTags, std::ref(set),
tag_type, group_mask, _1);
db.Visit(selection, f);
for (const auto &value : set)
visit_tag(value);
VisitTagWithFallbackOrEmpty(tag, group, [&](const char *group_name){
CollectTags(result[group_name], tag, tag_type);
});
}
std::map<std::string, std::set<std::string>>
CollectUniqueTags(const Database &db, const DatabaseSelection &selection,
TagType tag_type, TagType group)
{
std::map<std::string, std::set<std::string>> result;
db.Visit(selection, [&result, tag_type, group](const LightSong &song){
CollectGroupTags(result, song.tag, tag_type, group);
});
return result;
}

View File

@@ -20,16 +20,20 @@
#ifndef MPD_DB_UNIQUE_TAGS_HXX
#define MPD_DB_UNIQUE_TAGS_HXX
#include "Visitor.hxx"
#include "tag/Type.h"
#include "util/Compiler.h"
#include <map>
#include <set>
#include <string>
class TagMask;
class Database;
struct DatabaseSelection;
void
VisitUniqueTags(const Database &db, const DatabaseSelection &selection,
TagType tag_type, TagMask group_mask,
VisitTag visit_tag);
gcc_pure
std::map<std::string, std::set<std::string>>
CollectUniqueTags(const Database &db, const DatabaseSelection &selection,
TagType tag_type, TagType group);
#endif

View File

@@ -128,9 +128,9 @@ public:
VisitSong visit_song,
VisitPlaylist visit_playlist) const override;
void VisitUniqueTags(const DatabaseSelection &selection,
TagType tag_type, TagMask group_mask,
VisitTag visit_tag) const override;
std::map<std::string, std::set<std::string>> CollectUniqueTags(const DatabaseSelection &selection,
TagType tag_type,
TagType group) const override;
DatabaseStats GetStats(const DatabaseSelection &selection) const override;
@@ -411,31 +411,21 @@ SendConstraints(mpd_connection *connection, const DatabaseSelection &selection)
}
static bool
SendGroupMask(mpd_connection *connection, TagMask mask)
SendGroup(mpd_connection *connection, TagType group)
{
if (group == TAG_NUM_OF_ITEM_TYPES)
return true;
#if LIBMPDCLIENT_CHECK_VERSION(2,12,0)
for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) {
const auto tag_type = TagType(i);
if (!mask.Test(tag_type))
continue;
const auto tag = Convert(group);
if (tag == MPD_TAG_COUNT)
throw std::runtime_error("Unsupported tag");
const auto tag = Convert(tag_type);
if (tag == MPD_TAG_COUNT)
throw std::runtime_error("Unsupported tag");
if (!mpd_search_add_group_tag(connection, tag))
return false;
}
return true;
return mpd_search_add_group_tag(connection, tag);
#else
(void)connection;
(void)mask;
if (mask.TestAny())
throw std::runtime_error("Grouping requires libmpdclient 2.12");
return true;
throw std::runtime_error("Grouping requires libmpdclient 2.12");
#endif
}
@@ -992,11 +982,9 @@ ProxyDatabase::Visit(const DatabaseSelection &selection,
helper.Commit();
}
void
ProxyDatabase::VisitUniqueTags(const DatabaseSelection &selection,
TagType tag_type,
TagMask group_mask,
VisitTag visit_tag) const
std::map<std::string, std::set<std::string>>
ProxyDatabase::CollectUniqueTags(const DatabaseSelection &selection,
TagType tag_type, TagType group) const
try {
// TODO: eliminate the const_cast
const_cast<ProxyDatabase *>(this)->EnsureConnected();
@@ -1007,54 +995,56 @@ try {
if (!mpd_search_db_tags(connection, tag_type2) ||
!SendConstraints(connection, selection) ||
!SendGroupMask(connection, group_mask))
!SendGroup(connection, group))
ThrowError(connection);
if (!mpd_search_commit(connection))
ThrowError(connection);
TagBuilder builder;
std::map<std::string, std::set<std::string>> result;
while (auto *pair = mpd_recv_pair(connection)) {
AtScopeExit(this, pair) {
mpd_return_pair(connection, pair);
};
if (group == TAG_NUM_OF_ITEM_TYPES) {
auto &values = result[std::string()];
const auto current_type = tag_name_parse_i(pair->name);
if (current_type == TAG_NUM_OF_ITEM_TYPES)
continue;
while (auto *pair = mpd_recv_pair(connection)) {
AtScopeExit(this, pair) {
mpd_return_pair(connection, pair);
};
if (current_type == tag_type && !builder.empty()) {
try {
visit_tag(builder.Commit());
} catch (...) {
mpd_response_finish(connection);
throw;
}
const auto current_type = tag_name_parse_i(pair->name);
if (current_type == TAG_NUM_OF_ITEM_TYPES)
continue;
if (current_type == tag_type)
values.emplace(pair->value);
}
} else {
std::set<std::string> *current_group = nullptr;
builder.AddItem(current_type, pair->value);
while (auto *pair = mpd_recv_pair(connection)) {
AtScopeExit(this, pair) {
mpd_return_pair(connection, pair);
};
if (!builder.HasType(current_type))
/* if no tag item has been added, then the
given value was not acceptable
(e.g. empty); forcefully insert an empty
tag in this case, as the caller expects the
given tag type to be present */
builder.AddEmptyItem(current_type);
}
const auto current_type = tag_name_parse_i(pair->name);
if (current_type == TAG_NUM_OF_ITEM_TYPES)
continue;
if (!builder.empty()) {
try {
visit_tag(builder.Commit());
} catch (...) {
mpd_response_finish(connection);
throw;
if (current_type == tag_type) {
if (current_group == nullptr)
current_group = &result[std::string()];
current_group->emplace(pair->value);
} else if (current_type == group) {
current_group = &result[pair->value];
}
}
}
if (!mpd_response_finish(connection))
ThrowError(connection);
return result;
} catch (...) {
if (connection != nullptr)
mpd_search_cancel(connection);

View File

@@ -329,12 +329,11 @@ SimpleDatabase::Visit(const DatabaseSelection &selection,
"No such directory");
}
void
SimpleDatabase::VisitUniqueTags(const DatabaseSelection &selection,
TagType tag_type, TagMask group_mask,
VisitTag visit_tag) const
std::map<std::string, std::set<std::string>>
SimpleDatabase::CollectUniqueTags(const DatabaseSelection &selection,
TagType tag_type, TagType group) const
{
::VisitUniqueTags(*this, selection, tag_type, group_mask, visit_tag);
return ::CollectUniqueTags(*this, selection, tag_type, group);
}
DatabaseStats

View File

@@ -122,9 +122,9 @@ public:
VisitSong visit_song,
VisitPlaylist visit_playlist) const override;
void VisitUniqueTags(const DatabaseSelection &selection,
TagType tag_type, TagMask group_mask,
VisitTag visit_tag) const override;
std::map<std::string, std::set<std::string>> CollectUniqueTags(const DatabaseSelection &selection,
TagType tag_type,
TagType group) const override;
DatabaseStats GetStats(const DatabaseSelection &selection) const override;

View File

@@ -97,9 +97,9 @@ public:
VisitSong visit_song,
VisitPlaylist visit_playlist) const override;
void VisitUniqueTags(const DatabaseSelection &selection,
TagType tag_type, TagMask group_mask,
VisitTag visit_tag) const override;
std::map<std::string, std::set<std::string>> CollectUniqueTags(const DatabaseSelection &selection,
TagType tag_type,
TagType group) const override;
DatabaseStats GetStats(const DatabaseSelection &selection) const override;
@@ -624,17 +624,15 @@ UpnpDatabase::Visit(const DatabaseSelection &selection,
helper.Commit();
}
void
UpnpDatabase::VisitUniqueTags(const DatabaseSelection &selection,
TagType tag, gcc_unused TagMask group_mask,
VisitTag visit_tag) const
std::map<std::string, std::set<std::string>>
UpnpDatabase::CollectUniqueTags(const DatabaseSelection &selection,
TagType tag, TagType group) const
{
// TODO: use group_mask
(void)group; // TODO: use group
if (!visit_tag)
return;
std::map<std::string, std::set<std::string>> result;
auto &values = result[std::string()];
std::set<std::string> values;
for (auto& server : discovery->GetDirectories()) {
const auto dirbuf = SearchSongs(server, rootid, selection);
@@ -650,11 +648,7 @@ UpnpDatabase::VisitUniqueTags(const DatabaseSelection &selection,
}
}
for (const auto& value : values) {
TagBuilder builder;
builder.AddItem(tag, value.c_str());
visit_tag(builder.Commit());
}
return result;
}
DatabaseStats