mirror of https://github.com/kiwix/libkiwix.git
Add a `getBestTargetBookId` directly taking bookName, flavour and date.
This commit is contained in:
parent
eaca7010bc
commit
73b855ce6b
|
@ -325,6 +325,20 @@ class Library: public std::enable_shared_from_this<Library>
|
||||||
*/
|
*/
|
||||||
std::string getBestTargetBookId(const Bookmark& bookmark, MigrationMode migrationMode) const;
|
std::string getBestTargetBookId(const Bookmark& bookmark, MigrationMode migrationMode) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the best bookId for a combination of book's name, flavour and date.
|
||||||
|
*
|
||||||
|
* Given a bookName (mandatory), try to find the best book.
|
||||||
|
* If preferedFlavour is given, will try to find a book with the same flavour. If not found, return a book with a different flavour.
|
||||||
|
* If minDate is given, return a book newer than minDate. If not found, return a empty bookId.
|
||||||
|
*
|
||||||
|
* @param bookName The name of the book
|
||||||
|
* @param preferedFlavour The prefered flavour.
|
||||||
|
* @param minDate the minimal book date acceptable. Must be a string in the format "YYYY-MM-DD".
|
||||||
|
* @return A bookId corresponding to the query, or empty string if not found.
|
||||||
|
*/
|
||||||
|
std::string getBestTargetBookId(const std::string& bookName, const std::string& preferedFlavour="", const std::string& minDate="") const;
|
||||||
|
|
||||||
// XXX: This is a non-thread-safe operation
|
// XXX: This is a non-thread-safe operation
|
||||||
const Book& getBookById(const std::string& id) const;
|
const Book& getBookById(const std::string& id) const;
|
||||||
// XXX: This is a non-thread-safe operation
|
// XXX: This is a non-thread-safe operation
|
||||||
|
|
|
@ -230,6 +230,22 @@ std::string remove_quote(std::string input) {
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Library::getBestTargetBookId(const std::string& bookName, const std::string& preferedFlavour, const std::string& minDate) const {
|
||||||
|
// Let's reuse our algorithm based on bookmark.
|
||||||
|
MigrationMode migrationMode = UPGRADE_ONLY;
|
||||||
|
auto bookmark = Bookmark();
|
||||||
|
bookmark.setBookName(bookName);
|
||||||
|
bookmark.setBookFlavour(preferedFlavour);
|
||||||
|
|
||||||
|
if (minDate.empty()) {
|
||||||
|
migrationMode = ALLOW_DOWNGRADE;
|
||||||
|
} else {
|
||||||
|
bookmark.setDate(minDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
return getBestTargetBookId(bookmark, migrationMode);
|
||||||
|
}
|
||||||
|
|
||||||
std::string Library::getBestTargetBookId(const Bookmark& bookmark, MigrationMode migrationMode) const {
|
std::string Library::getBestTargetBookId(const Bookmark& bookmark, MigrationMode migrationMode) const {
|
||||||
std::lock_guard<std::recursive_mutex> lock(m_mutex);
|
std::lock_guard<std::recursive_mutex> lock(m_mutex);
|
||||||
// Search for a existing book with the same name
|
// Search for a existing book with the same name
|
||||||
|
|
|
@ -698,6 +698,14 @@ TEST_F(LibraryTest, GetBestTargetBookIdInvalidNewer)
|
||||||
ASSERT_EQ(lib->getBestTargetBookId(invalidBookmark, kiwix::ALLOW_DOWNGRADE), bookId+"_updated1yearlater");
|
ASSERT_EQ(lib->getBestTargetBookId(invalidBookmark, kiwix::ALLOW_DOWNGRADE), bookId+"_updated1yearlater");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(LibraryTest, GetBestTargetBookIdName)
|
||||||
|
{
|
||||||
|
ASSERT_EQ(lib->getBestTargetBookId("wikipedia_fr_tunisie"), "0c45160e-f917-760a-9159-dfe3c53cdcdd_updated1yearlater");
|
||||||
|
ASSERT_EQ(lib->getBestTargetBookId("wikipedia_fr_tunisie", "novid"), "0c45160e-f917-760a-9159-dfe3c53cdcdd_updated1yearlater");
|
||||||
|
ASSERT_EQ(lib->getBestTargetBookId("wikipedia_fr_tunisie", "other_flavour"), "0c45160e-f917-760a-9159-dfe3c53cdcdd_updated1yearlater_flavour");
|
||||||
|
ASSERT_EQ(lib->getBestTargetBookId("wikipedia_fr_tunisie", "other_flavour", "2020-12-12"), "");
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(LibraryTest, sanityCheck)
|
TEST_F(LibraryTest, sanityCheck)
|
||||||
{
|
{
|
||||||
EXPECT_EQ(lib->getBookCount(true, true), 16U);
|
EXPECT_EQ(lib->getBookCount(true, true), 16U);
|
||||||
|
|
Loading…
Reference in New Issue