Helper function Library::getBookPropValueSet()

Introduced a helper function `Library::getBookPropValueSet()` and
deduplicated Library::getBooks{Languages,Creators,Publishers}() methods.
This commit is contained in:
Veloman Yunkan 2021-06-10 12:27:51 +04:00 committed by Matthieu Gautier
parent b2027b397c
commit 18871b4b15
3 changed files with 37 additions and 45 deletions

View File

@ -341,7 +341,11 @@ class Library
friend class OPDSDumper; friend class OPDSDumper;
friend class libXMLDumper; friend class libXMLDumper;
private: // types
typedef const std::string& (Book::*BookStrPropMemFn)() const;
private: // functions private: // functions
std::vector<std::string> getBookPropValueSet(BookStrPropMemFn p) const;
BookIdCollection filterViaBookDB(const Filter& filter) const; BookIdCollection filterViaBookDB(const Filter& filter) const;
void updateBookDB(const Book& book); void updateBookDB(const Book& book);
}; };

View File

@ -208,23 +208,23 @@ bool Library::writeBookmarksToFile(const std::string& path) const
return writeTextFile(path, dumper.dumpLibXMLBookmark()); return writeTextFile(path, dumper.dumpLibXMLBookmark());
} }
std::vector<std::string> Library::getBooksLanguages() const std::vector<std::string> Library::getBookPropValueSet(BookStrPropMemFn p) const
{ {
std::vector<std::string> booksLanguages; std::set<std::string> propValues;
std::map<std::string, bool> booksLanguagesMap;
for (auto& pair: m_books) { for (const auto& pair: m_books) {
auto& book = pair.second; const auto& book = pair.second;
auto& language = book.getLanguage(); if (book.getOrigId().empty()) {
if (booksLanguagesMap.find(language) == booksLanguagesMap.end()) { propValues.insert((book.*p)());
if (book.getOrigId().empty()) {
booksLanguagesMap[language] = true;
booksLanguages.push_back(language);
}
} }
} }
return booksLanguages; return std::vector<std::string>(propValues.begin(), propValues.end());
}
std::vector<std::string> Library::getBooksLanguages() const
{
return getBookPropValueSet(&Book::getLanguage);
} }
std::vector<std::string> Library::getBooksCategories() const std::vector<std::string> Library::getBooksCategories() const
@ -244,40 +244,12 @@ std::vector<std::string> Library::getBooksCategories() const
std::vector<std::string> Library::getBooksCreators() const std::vector<std::string> Library::getBooksCreators() const
{ {
std::vector<std::string> booksCreators; return getBookPropValueSet(&Book::getCreator);
std::map<std::string, bool> booksCreatorsMap;
for (auto& pair: m_books) {
auto& book = pair.second;
auto& creator = book.getCreator();
if (booksCreatorsMap.find(creator) == booksCreatorsMap.end()) {
if (book.getOrigId().empty()) {
booksCreatorsMap[creator] = true;
booksCreators.push_back(creator);
}
}
}
return booksCreators;
} }
std::vector<std::string> Library::getBooksPublishers() const std::vector<std::string> Library::getBooksPublishers() const
{ {
std::vector<std::string> booksPublishers; return getBookPropValueSet(&Book::getPublisher);
std::map<std::string, bool> booksPublishersMap;
for (auto& pair:m_books) {
auto& book = pair.second;
auto& publisher = book.getPublisher();
if (booksPublishersMap.find(publisher) == booksPublishersMap.end()) {
if (book.getOrigId().empty()) {
booksPublishersMap[publisher] = true;
booksPublishers.push_back(publisher);
}
}
}
return booksPublishers;
} }
const std::vector<kiwix::Bookmark> Library::getBookmarks(bool onlyValidBookmarks) const const std::vector<kiwix::Bookmark> Library::getBookmarks(bool onlyValidBookmarks) const

View File

@ -275,9 +275,25 @@ TEST_F(LibraryTest, getBookMarksTest)
TEST_F(LibraryTest, sanityCheck) TEST_F(LibraryTest, sanityCheck)
{ {
EXPECT_EQ(lib.getBookCount(true, true), 12U); EXPECT_EQ(lib.getBookCount(true, true), 12U);
EXPECT_EQ(lib.getBooksLanguages().size(), 3U); EXPECT_EQ(lib.getBooksLanguages(),
EXPECT_EQ(lib.getBooksCreators().size(), 9U); std::vector<std::string>({"deu", "eng", "fra"})
EXPECT_EQ(lib.getBooksPublishers().size(), 3U); );
EXPECT_EQ(lib.getBooksCreators(), std::vector<std::string>({
"Islam Stack Exchange",
"Movies & TV Stack Exchange",
"Mythology & Folklore Stack Exchange",
"TED",
"Tania Louis",
"Wiki",
"Wikibooks",
"Wikipedia",
"Wikiquote"
}));
EXPECT_EQ(lib.getBooksPublishers(), std::vector<std::string>({
"",
"Kiwix",
"Kiwix & Some Enthusiasts"
}));
} }
TEST_F(LibraryTest, categoryHandling) TEST_F(LibraryTest, categoryHandling)