song/OptimizeFilter: optimization stage for filters
This commit is contained in:
parent
2d2120338b
commit
027e562f65
|
@ -1039,6 +1039,7 @@ libsong_a_SOURCES = \
|
||||||
src/song/AudioFormatSongFilter.cxx src/song/AudioFormatSongFilter.hxx \
|
src/song/AudioFormatSongFilter.cxx src/song/AudioFormatSongFilter.hxx \
|
||||||
src/song/AndSongFilter.cxx src/song/AndSongFilter.hxx \
|
src/song/AndSongFilter.cxx src/song/AndSongFilter.hxx \
|
||||||
src/song/NotSongFilter.hxx \
|
src/song/NotSongFilter.hxx \
|
||||||
|
src/song/OptimizeFilter.cxx src/song/OptimizeFilter.hxx \
|
||||||
src/song/Filter.cxx src/song/Filter.hxx \
|
src/song/Filter.cxx src/song/Filter.hxx \
|
||||||
src/song/LightSong.cxx src/song/LightSong.hxx
|
src/song/LightSong.cxx src/song/LightSong.hxx
|
||||||
|
|
||||||
|
|
|
@ -105,6 +105,7 @@ handle_match(Client &client, Request args, Response &r, bool fold_case)
|
||||||
GetFullMessage(std::current_exception()).c_str());
|
GetFullMessage(std::current_exception()).c_str());
|
||||||
return CommandResult::ERROR;
|
return CommandResult::ERROR;
|
||||||
}
|
}
|
||||||
|
filter.Optimize();
|
||||||
|
|
||||||
const DatabaseSelection selection("", true, &filter);
|
const DatabaseSelection selection("", true, &filter);
|
||||||
|
|
||||||
|
@ -138,6 +139,7 @@ handle_match_add(Client &client, Request args, Response &r, bool fold_case)
|
||||||
GetFullMessage(std::current_exception()).c_str());
|
GetFullMessage(std::current_exception()).c_str());
|
||||||
return CommandResult::ERROR;
|
return CommandResult::ERROR;
|
||||||
}
|
}
|
||||||
|
filter.Optimize();
|
||||||
|
|
||||||
auto &partition = client.GetPartition();
|
auto &partition = client.GetPartition();
|
||||||
const ScopeBulkEdit bulk_edit(partition);
|
const ScopeBulkEdit bulk_edit(partition);
|
||||||
|
@ -172,6 +174,7 @@ handle_searchaddpl(Client &client, Request args, Response &r)
|
||||||
GetFullMessage(std::current_exception()).c_str());
|
GetFullMessage(std::current_exception()).c_str());
|
||||||
return CommandResult::ERROR;
|
return CommandResult::ERROR;
|
||||||
}
|
}
|
||||||
|
filter.Optimize();
|
||||||
|
|
||||||
const Database &db = client.GetDatabaseOrThrow();
|
const Database &db = client.GetDatabaseOrThrow();
|
||||||
|
|
||||||
|
@ -206,6 +209,8 @@ handle_count(Client &client, Request args, Response &r)
|
||||||
GetFullMessage(std::current_exception()).c_str());
|
GetFullMessage(std::current_exception()).c_str());
|
||||||
return CommandResult::ERROR;
|
return CommandResult::ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filter.Optimize();
|
||||||
}
|
}
|
||||||
|
|
||||||
PrintSongCount(r, client.GetPartition(), "", &filter, group);
|
PrintSongCount(r, client.GetPartition(), "", &filter, group);
|
||||||
|
@ -238,6 +243,7 @@ handle_list_file(Client &client, Request args, Response &r)
|
||||||
GetFullMessage(std::current_exception()).c_str());
|
GetFullMessage(std::current_exception()).c_str());
|
||||||
return CommandResult::ERROR;
|
return CommandResult::ERROR;
|
||||||
}
|
}
|
||||||
|
filter->Optimize();
|
||||||
}
|
}
|
||||||
|
|
||||||
PrintSongUris(r, client.GetPartition(), filter.get());
|
PrintSongUris(r, client.GetPartition(), filter.get());
|
||||||
|
@ -300,6 +306,7 @@ handle_list(Client &client, Request args, Response &r)
|
||||||
GetFullMessage(std::current_exception()).c_str());
|
GetFullMessage(std::current_exception()).c_str());
|
||||||
return CommandResult::ERROR;
|
return CommandResult::ERROR;
|
||||||
}
|
}
|
||||||
|
filter->Optimize();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (group_mask.Test(tagType)) {
|
if (group_mask.Test(tagType)) {
|
||||||
|
|
|
@ -272,6 +272,7 @@ handle_playlist_match(Client &client, Request args, Response &r,
|
||||||
GetFullMessage(std::current_exception()).c_str());
|
GetFullMessage(std::current_exception()).c_str());
|
||||||
return CommandResult::ERROR;
|
return CommandResult::ERROR;
|
||||||
}
|
}
|
||||||
|
filter.Optimize();
|
||||||
|
|
||||||
playlist_print_find(r, client.GetPlaylist(), filter);
|
playlist_print_find(r, client.GetPlaylist(), filter);
|
||||||
return CommandResult::OK;
|
return CommandResult::OK;
|
||||||
|
|
|
@ -31,6 +31,9 @@
|
||||||
class AndSongFilter final : public ISongFilter {
|
class AndSongFilter final : public ISongFilter {
|
||||||
std::list<ISongFilterPtr> items;
|
std::list<ISongFilterPtr> items;
|
||||||
|
|
||||||
|
friend void OptimizeSongFilter(AndSongFilter &) noexcept;
|
||||||
|
friend ISongFilterPtr OptimizeSongFilter(ISongFilterPtr) noexcept;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
const auto &GetItems() const noexcept {
|
const auto &GetItems() const noexcept {
|
||||||
return items;
|
return items;
|
||||||
|
|
|
@ -352,6 +352,12 @@ SongFilter::Parse(ConstBuffer<const char *> args, bool fold_case)
|
||||||
} while (!args.empty());
|
} while (!args.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
SongFilter::Optimize() noexcept
|
||||||
|
{
|
||||||
|
OptimizeSongFilter(and_filter);
|
||||||
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
SongFilter::Match(const LightSong &song) const noexcept
|
SongFilter::Match(const LightSong &song) const noexcept
|
||||||
{
|
{
|
||||||
|
|
|
@ -68,6 +68,8 @@ public:
|
||||||
*/
|
*/
|
||||||
void Parse(ConstBuffer<const char *> args, bool fold_case=false);
|
void Parse(ConstBuffer<const char *> args, bool fold_case=false);
|
||||||
|
|
||||||
|
void Optimize() noexcept;
|
||||||
|
|
||||||
gcc_pure
|
gcc_pure
|
||||||
bool Match(const LightSong &song) const noexcept;
|
bool Match(const LightSong &song) const noexcept;
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,8 @@
|
||||||
class NotSongFilter final : public ISongFilter {
|
class NotSongFilter final : public ISongFilter {
|
||||||
ISongFilterPtr child;
|
ISongFilterPtr child;
|
||||||
|
|
||||||
|
friend ISongFilterPtr OptimizeSongFilter(ISongFilterPtr) noexcept;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template<typename C>
|
template<typename C>
|
||||||
explicit NotSongFilter(C &&_child) noexcept
|
explicit NotSongFilter(C &&_child) noexcept
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2003-2018 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 "config.h"
|
||||||
|
#include "OptimizeFilter.hxx"
|
||||||
|
#include "AndSongFilter.hxx"
|
||||||
|
#include "NotSongFilter.hxx"
|
||||||
|
#include "TagSongFilter.hxx"
|
||||||
|
#include "UriSongFilter.hxx"
|
||||||
|
|
||||||
|
void
|
||||||
|
OptimizeSongFilter(AndSongFilter &af) noexcept
|
||||||
|
{
|
||||||
|
for (auto i = af.items.begin(); i != af.items.end();) {
|
||||||
|
auto f = OptimizeSongFilter(std::move(*i));
|
||||||
|
if (auto *nested = dynamic_cast<AndSongFilter *>(f.get())) {
|
||||||
|
/* collapse nested #AndSongFilter instances */
|
||||||
|
af.items.splice(i, std::move(nested->items));
|
||||||
|
i = af.items.erase(i);
|
||||||
|
} else {
|
||||||
|
*i = std::move(f);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ISongFilterPtr
|
||||||
|
OptimizeSongFilter(ISongFilterPtr f) noexcept
|
||||||
|
{
|
||||||
|
if (auto *af = dynamic_cast<AndSongFilter *>(f.get())) {
|
||||||
|
/* first optimize all items */
|
||||||
|
OptimizeSongFilter(*af);
|
||||||
|
|
||||||
|
if (!af->items.empty() &&
|
||||||
|
std::next(af->items.begin()) == af->items.end())
|
||||||
|
/* only one item: the containing
|
||||||
|
#AndSongFilter can be removed */
|
||||||
|
return std::move(af->items.front());
|
||||||
|
} else if (auto *nf = dynamic_cast<NotSongFilter *>(f.get())) {
|
||||||
|
auto child = OptimizeSongFilter(std::move(nf->child));
|
||||||
|
if (auto *tf = dynamic_cast<TagSongFilter *>(child.get())) {
|
||||||
|
/* #TagSongFilter has its own "negated" flag,
|
||||||
|
so we can drop the #NotSongFilter
|
||||||
|
container */
|
||||||
|
tf->negated = !tf->negated;
|
||||||
|
return child;
|
||||||
|
} else if (auto *uf = dynamic_cast<UriSongFilter *>(child.get())) {
|
||||||
|
/* same for #UriSongFilter */
|
||||||
|
uf->negated = !uf->negated;
|
||||||
|
return child;
|
||||||
|
}
|
||||||
|
|
||||||
|
nf->child = std::move(child);
|
||||||
|
}
|
||||||
|
|
||||||
|
return f;
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2003-2018 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_SONG_OPTIMIZE_FILTER_HXX
|
||||||
|
#define MPD_SONG_OPTIMIZE_FILTER_HXX
|
||||||
|
|
||||||
|
#include "ISongFilter.hxx"
|
||||||
|
|
||||||
|
class AndSongFilter;
|
||||||
|
|
||||||
|
void
|
||||||
|
OptimizeSongFilter(AndSongFilter &af) noexcept;
|
||||||
|
|
||||||
|
ISongFilterPtr
|
||||||
|
OptimizeSongFilter(ISongFilterPtr f) noexcept;
|
||||||
|
|
||||||
|
#endif
|
|
@ -38,6 +38,8 @@ class TagSongFilter final : public ISongFilter {
|
||||||
|
|
||||||
StringFilter filter;
|
StringFilter filter;
|
||||||
|
|
||||||
|
friend ISongFilterPtr OptimizeSongFilter(ISongFilterPtr) noexcept;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template<typename V>
|
template<typename V>
|
||||||
TagSongFilter(TagType _type, V &&_value, bool fold_case, bool _negated)
|
TagSongFilter(TagType _type, V &&_value, bool fold_case, bool _negated)
|
||||||
|
|
|
@ -28,6 +28,8 @@ class UriSongFilter final : public ISongFilter {
|
||||||
|
|
||||||
bool negated;
|
bool negated;
|
||||||
|
|
||||||
|
friend ISongFilterPtr OptimizeSongFilter(ISongFilterPtr) noexcept;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template<typename V>
|
template<typename V>
|
||||||
UriSongFilter(V &&_value, bool fold_case, bool _negated)
|
UriSongFilter(V &&_value, bool fold_case, bool _negated)
|
||||||
|
|
|
@ -41,6 +41,7 @@ try {
|
||||||
|
|
||||||
SongFilter filter;
|
SongFilter filter;
|
||||||
filter.Parse(ConstBuffer<const char *>(argv + 1, argc - 1));
|
filter.Parse(ConstBuffer<const char *>(argv + 1, argc - 1));
|
||||||
|
filter.Optimize();
|
||||||
|
|
||||||
puts(filter.ToExpression().c_str());
|
puts(filter.ToExpression().c_str());
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|
Loading…
Reference in New Issue