Library::filter() works in two stages

1. Get the subset of books matching the q (title/description) parameter
   of the search

2. Filter out books not matching the other parameters of the search.

Stage 1. currently works in the old way, but will be replaced by Xapian
based search in subsequent commits.
This commit is contained in:
Veloman Yunkan 2021-03-06 20:03:41 +04:00 committed by Matthieu Gautier
parent f7c867f8a7
commit a20f9e2ce1
2 changed files with 28 additions and 3 deletions

View File

@ -105,7 +105,11 @@ class Filter {
Filter& query(std::string query);
Filter& name(std::string name);
bool hasQuery() const;
bool accept(const Book& book) const;
bool acceptByQueryOnly(const Book& book) const;
bool acceptByNonQueryCriteria(const Book& book) const;
};

View File

@ -236,12 +236,18 @@ std::vector<std::string> Library::filter(const Filter& filter)
{
std::vector<std::string> bookIds;
for(auto& pair:m_books) {
auto book = pair.second;
if(filter.accept(book)) {
if(filter.acceptByQueryOnly(pair.second)) {
bookIds.push_back(pair.first);
}
}
return bookIds;
std::vector<std::string> result;
for(auto id : bookIds) {
if(filter.acceptByNonQueryCriteria(m_books[id])) {
result.push_back(id);
}
}
return result;
}
template<supportedListSortBy SORT>
@ -495,7 +501,17 @@ Filter& Filter::name(std::string name)
#define ACTIVE(X) (activeFilters & (X))
#define FILTER(TAG, TEST) if (ACTIVE(TAG) && !(TEST)) { return false; }
bool Filter::hasQuery() const
{
return ACTIVE(QUERY);
}
bool Filter::accept(const Book& book) const
{
return acceptByNonQueryCriteria(book) && acceptByQueryOnly(book);
}
bool Filter::acceptByNonQueryCriteria(const Book& book) const
{
auto local = !book.getPath().empty();
FILTER(_LOCAL, local)
@ -538,6 +554,11 @@ bool Filter::accept(const Book& book) const
}
}
}
return true;
}
bool Filter::acceptByQueryOnly(const Book& book) const
{
if ( ACTIVE(QUERY)
&& !(matchRegex(book.getTitle(), "\\Q" + _query + "\\E")
|| matchRegex(book.getDescription(), "\\Q" + _query + "\\E")))