command/Database: add "sort" parameter to "find" and "search"
Implement the second part of https://bugs.musicpd.org/view.php?id=3990
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
#include "Selection.hxx"
|
||||
#include "SongFilter.hxx"
|
||||
#include "SongPrint.hxx"
|
||||
#include "DetachedSong.hxx"
|
||||
#include "TimePrint.hxx"
|
||||
#include "TagPrint.hxx"
|
||||
#include "client/Response.hxx"
|
||||
@@ -139,10 +140,36 @@ PrintPlaylistFull(Response &r, bool base,
|
||||
time_print(r, "Last-Modified", playlist.mtime);
|
||||
}
|
||||
|
||||
static bool
|
||||
CompareNumeric(const char *a, const char *b)
|
||||
{
|
||||
long a_value = strtol(a, nullptr, 10);
|
||||
long b_value = strtol(b, nullptr, 10);
|
||||
|
||||
return a_value < b_value;
|
||||
}
|
||||
|
||||
static bool
|
||||
CompareTags(TagType type, const Tag &a, const Tag &b)
|
||||
{
|
||||
const char *a_value = a.GetSortValue(type);
|
||||
const char *b_value = b.GetSortValue(type);
|
||||
|
||||
switch (type) {
|
||||
case TAG_DISC:
|
||||
case TAG_TRACK:
|
||||
return CompareNumeric(a_value, b_value);
|
||||
|
||||
default:
|
||||
return strcmp(a_value, b_value) < 0;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
db_selection_print(Response &r, Partition &partition,
|
||||
const DatabaseSelection &selection,
|
||||
bool full, bool base,
|
||||
TagType sort,
|
||||
unsigned window_start, unsigned window_end)
|
||||
{
|
||||
const Database &db = partition.GetDatabaseOrThrow();
|
||||
@@ -161,16 +188,53 @@ db_selection_print(Response &r, Partition &partition,
|
||||
std::ref(r), base, _1, _2)
|
||||
: VisitPlaylist();
|
||||
|
||||
if (window_start > 0 ||
|
||||
window_end < (unsigned)std::numeric_limits<int>::max())
|
||||
s = [s, window_start, window_end, &i](const LightSong &song){
|
||||
const bool in_window = i >= window_start && i < window_end;
|
||||
++i;
|
||||
if (in_window)
|
||||
s(song);
|
||||
};
|
||||
if (sort == TAG_NUM_OF_ITEM_TYPES) {
|
||||
if (window_start > 0 ||
|
||||
window_end < (unsigned)std::numeric_limits<int>::max())
|
||||
s = [s, window_start, window_end, &i](const LightSong &song){
|
||||
const bool in_window = i >= window_start && i < window_end;
|
||||
++i;
|
||||
if (in_window)
|
||||
s(song);
|
||||
};
|
||||
|
||||
db.Visit(selection, d, s, p);
|
||||
db.Visit(selection, d, s, p);
|
||||
} else {
|
||||
// TODO: allow the database plugin to sort internally
|
||||
|
||||
/* the client has asked us to sort the result; this is
|
||||
pretty expensive, because instead of streaming the
|
||||
result to the client, we need to copy it all into
|
||||
this std::vector, and then sort it */
|
||||
std::vector<DetachedSong> songs;
|
||||
|
||||
{
|
||||
auto collect_songs = [&songs](const LightSong &song){
|
||||
songs.emplace_back(song);
|
||||
};
|
||||
|
||||
db.Visit(selection, d, collect_songs, p);
|
||||
}
|
||||
|
||||
std::stable_sort(songs.begin(), songs.end(),
|
||||
[sort](const DetachedSong &a, const DetachedSong &b){
|
||||
return CompareTags(sort, a.GetTag(),
|
||||
b.GetTag());
|
||||
});
|
||||
|
||||
if (window_end < songs.size())
|
||||
songs.erase(std::next(songs.begin(), window_end),
|
||||
songs.end());
|
||||
|
||||
if (window_start >= songs.size())
|
||||
return;
|
||||
|
||||
songs.erase(songs.begin(),
|
||||
std::next(songs.begin(), window_start));
|
||||
|
||||
for (const auto &song : songs)
|
||||
s((LightSong)song);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
@@ -179,6 +243,7 @@ db_selection_print(Response &r, Partition &partition,
|
||||
bool full, bool base)
|
||||
{
|
||||
db_selection_print(r, partition, selection, full, base,
|
||||
TAG_NUM_OF_ITEM_TYPES,
|
||||
0, std::numeric_limits<int>::max());
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user