From 05e1b6b04e0bcbe9d94b831d1f63a4f8b8e35f10 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Thu, 15 Feb 2024 12:01:57 +0100 Subject: [PATCH] Add a `getBestTargetBookId` directly taking bookName, flavour and date. --- include/library.h | 14 ++++++++++++++ src/library.cpp | 16 ++++++++++++++++ test/library.cpp | 8 ++++++++ 3 files changed, 38 insertions(+) diff --git a/include/library.h b/include/library.h index 13dd6fe0d..d0528d3d9 100644 --- a/include/library.h +++ b/include/library.h @@ -325,6 +325,20 @@ class Library: public std::enable_shared_from_this */ 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 const Book& getBookById(const std::string& id) const; // XXX: This is a non-thread-safe operation diff --git a/src/library.cpp b/src/library.cpp index 8a713ace3..6e4c68889 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -230,6 +230,22 @@ std::string remove_quote(std::string 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::lock_guard lock(m_mutex); // Search for a existing book with the same name diff --git a/test/library.cpp b/test/library.cpp index 2be7b5130..4f9c234a5 100644 --- a/test/library.cpp +++ b/test/library.cpp @@ -698,6 +698,14 @@ TEST_F(LibraryTest, GetBestTargetBookIdInvalidNewer) 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) { EXPECT_EQ(lib->getBookCount(true, true), 16U);