Skip to content

Commit

Permalink
fixup! Preventing confusion of tongues in multizim search
Browse files Browse the repository at this point in the history
  • Loading branch information
veloman-yunkan committed Oct 25, 2022
1 parent c2201fb commit f0f31f6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
17 changes: 11 additions & 6 deletions include/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,18 +263,23 @@ class Library
unsigned int getBookCount(const bool localBooks, const bool remoteBooks) const;

/**
* Get all languagues of the books in the library.
* Get all languagues of all or a subset of books in the library.
*
* @param books An optional list of book ids. By default all books in the
* library are used.
* @return A list of languages.
*/
std::vector<std::string> getBooksLanguages() const;
std::vector<std::string> getBooksLanguages(const BookIdCollection* books = nullptr) const;

/**
* Get all languagues of the books in the library with counts.
* Get all languagues of all or a subset of books in the library with counts.
*
* @param books An optional list of book ids. By default all books in the
* library are used.
*
* @return A list of languages with the count of books in each language.
*/
AttributeCounts getBooksLanguagesWithCounts() const;
AttributeCounts getBooksLanguagesWithCounts(const BookIdCollection* books = nullptr) const;

/**
* Get all categories of the books in the library.
Expand Down Expand Up @@ -355,8 +360,8 @@ class Library
struct Impl;

private: // functions
AttributeCounts getBookAttributeCounts(BookStrPropMemFn p) const;
std::vector<std::string> getBookPropValueSet(BookStrPropMemFn p) const;
AttributeCounts getBookAttributeCounts(BookStrPropMemFn p, const BookIdCollection& books) const;
std::vector<std::string> getBookPropValueSet(BookStrPropMemFn p, const BookIdCollection& books) const;
BookIdCollection filterViaBookDB(const Filter& filter) const;
void updateBookDB(const Book& book);
void dropCache(const std::string& bookId);
Expand Down
22 changes: 11 additions & 11 deletions src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,37 +348,37 @@ bool Library::writeBookmarksToFile(const std::string& path) const
return writeTextFile(path, xml);
}

Library::AttributeCounts Library::getBookAttributeCounts(BookStrPropMemFn p) const
Library::AttributeCounts Library::getBookAttributeCounts(BookStrPropMemFn p, const BookIdCollection& books) const
{
std::lock_guard<std::mutex> lock(m_mutex);
AttributeCounts propValueCounts;

for (const auto& pair: mp_impl->m_books) {
const auto& book = pair.second;
for (const auto& b: books) {
const auto& book = getBookById(b);
if (book.getOrigId().empty()) {
propValueCounts[(book.*p)()] += 1;
}
}
return propValueCounts;
}

std::vector<std::string> Library::getBookPropValueSet(BookStrPropMemFn p) const
std::vector<std::string> Library::getBookPropValueSet(BookStrPropMemFn p, const BookIdCollection& books) const
{
std::vector<std::string> result;
for ( const auto& kv : getBookAttributeCounts(p) ) {
for ( const auto& kv : getBookAttributeCounts(p, books) ) {
result.push_back(kv.first);
}
return result;
}

std::vector<std::string> Library::getBooksLanguages() const
std::vector<std::string> Library::getBooksLanguages(const BookIdCollection* books) const
{
return getBookPropValueSet(&Book::getLanguage);
return getBookPropValueSet(&Book::getLanguage, books ? *books : getBooksIds());
}

Library::AttributeCounts Library::getBooksLanguagesWithCounts() const
Library::AttributeCounts Library::getBooksLanguagesWithCounts(const BookIdCollection* books) const
{
return getBookAttributeCounts(&Book::getLanguage);
return getBookAttributeCounts(&Book::getLanguage, books ? *books : getBooksIds());
}

std::vector<std::string> Library::getBooksCategories() const
Expand All @@ -399,12 +399,12 @@ std::vector<std::string> Library::getBooksCategories() const

std::vector<std::string> Library::getBooksCreators() const
{
return getBookPropValueSet(&Book::getCreator);
return getBookPropValueSet(&Book::getCreator, getBooksIds());
}

std::vector<std::string> Library::getBooksPublishers() const
{
return getBookPropValueSet(&Book::getPublisher);
return getBookPropValueSet(&Book::getPublisher, getBooksIds());
}

const std::vector<kiwix::Bookmark> Library::getBookmarks(bool onlyValidBookmarks) const
Expand Down
13 changes: 2 additions & 11 deletions src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,6 @@ void checkBookNumber(const Library::BookIdSet& bookIds, size_t limit) {
}
}

typedef std::set<std::string> Languages;

Languages getLanguages(const Library& lib, const Library::BookIdSet& bookIds) {
Languages langs;
for ( const auto& b : bookIds ) {
langs.insert(lib.getBookById(b).getLanguage());
}
return langs;
}

struct CustomizedResourceData
{
std::string mimeType;
Expand Down Expand Up @@ -316,7 +306,8 @@ SearchInfo InternalServer::getSearchInfo(const RequestContext& request) const
{
auto bookIds = selectBooks(request);
checkBookNumber(bookIds.second, m_multizimSearchLimit);
if ( getLanguages(*mp_library, bookIds.second).size() != 1 ) {
const Library::BookIdCollection bookIds2(bookIds.second.begin(), bookIds.second.end());
if ( mp_library->getBooksLanguages(&bookIds2).size() != 1 ) {
throw Error(nonParameterizedMessage("confusion-of-tongues"));
}

Expand Down

0 comments on commit f0f31f6

Please sign in to comment.