Class Library

Nested Relationships

Nested Types

Inheritance Relationships

Base Type

  • public std::enable_shared_from_this< Library >

Class Documentation

class Library : public std::enable_shared_from_this<Library>

A Library store several books.

Public Types

typedef uint64_t Revision
typedef std::vector<std::string> BookIdCollection
typedef std::map<std::string, int> AttributeCounts
typedef std::set<std::string> BookIdSet

Public Functions

~Library()
Library(const Library&) = delete

Library is not a copiable object. However it can be moved.

Library(Library&&) = delete
void operator=(const Library&) = delete
Library &operator=(Library&&) = delete
bool addBook(const Book &book)

Add a book to the library.

If a book already exist in the library with the same id, update the existing book instead of adding a new one.

Parameters:

book – The book to add.

Returns:

True if the book has been added. False if a book has been updated.

inline bool addOrUpdateBook(const Book &book)

A self-explanatory alias for addBook()

void addBookmark(const Bookmark &bookmark)

Add a bookmark to the library.

Parameters:

bookmark – the book to add.

bool removeBookmark(const std::string &zimId, const std::string &url)

Remove a bookmark

Parameters:
  • zimId – The zimId of the bookmark.

  • url – The url of the bookmark.

Returns:

True if the bookmark has been removed.

std::tuple<int, int> migrateBookmarks(MigrationMode migrationMode = ALLOW_DOWNGRADE)

Migrate all invalid bookmarks.

All invalid bookmarks (ie pointing to unknown books, no check is made on bookmark pointing to invalid articles of valid book) will be migrated (if possible) to a better book. “Better book”, will be determined using method getBestTargetBookId.

Returns:

A tuple<int, int>: <The number of bookmarks updated>, <Number of invalid bookmarks before migration was performed>.

int migrateBookmarks(const std::string &sourceBookId, MigrationMode migrationMode = UPGRADE_ONLY)

Migrate all bookmarks associated to a specific book.

All bookmarks associated to sourceBookId book will be migrated to a better book. “Better book”, will be determined using method getBestTargetBookId.

Parameters:
  • sourceBookId – the source bookId of the bookmarks to migrate.

  • migrationMode – how we will find the best book.

Returns:

The number of bookmarks updated.

int migrateBookmarks(const std::string &sourceBookId, const std::string &targetBookId)

Migrate bookmarks

Migrate all bookmarks pointing to source to destination.

Parameters:
  • sourceBookId – the source bookId of the bookmarks to migrate.

  • targetBookId – the destination bookId to migrate the bookmarks to.

Returns:

The number of bookmarks updated.

std::string getBestTargetBookId(const Bookmark &bookmark, MigrationMode migrationMode) const

Get the best available bookId for a bookmark.

Given a bookmark, return the best available bookId. “best available bookId” is determined using heuristitcs based on book name, flavour and date.

Parameters:
  • bookmark – The bookmark to search the bookId for.

  • migrationMode – The migration mode to use.

Returns:

A bookId. Potentially empty string if no suitable book found.

std::string getBestTargetBookId(const std::string &bookName, const std::string &preferedFlavour = "", const std::string &minDate = "") 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.

Parameters:
  • bookName – The name of the book

  • preferedFlavour – The prefered flavour.

  • minDate – the minimal book date acceptable. Must be a string in the format “YYYY-MM-DD”.

Returns:

A bookId corresponding to the query, or empty string if not found.

const Book &getBookById(const std::string &id) const
const Book &getBookByPath(const std::string &path) const
Book getBookByIdThreadSafe(const std::string &id) const
std::shared_ptr<zim::Archive> getArchiveById(const std::string &id)
inline std::shared_ptr<ZimSearcher> getSearcherById(const std::string &id)
std::shared_ptr<ZimSearcher> getSearcherByIds(const BookIdSet &ids)
bool removeBookById(const std::string &id)

Remove a book from the library.

Parameters:

id – the id of the book to remove.

Returns:

True if the book were in the lirbrary and has been removed.

bool writeToFile(const std::string &path) const

Write the library to a file.

Parameters:

path – the path of the file to write to.

Returns:

True if the library has been correctly saved.

bool writeBookmarksToFile(const std::string &path) const

Write the library bookmarks to a file.

Parameters:

path – the path of the file to write to.

Returns:

True if the library has been correctly saved.

unsigned int getBookCount(const bool localBooks, const bool remoteBooks) const

Get the number of book in the library.

Parameters:
  • localBooks – If we must count local books (books with a path).

  • remoteBooks – If we must count remote books (books with an url)

Returns:

The number of books.

std::vector<std::string> getBooksLanguages() const

Get all languagues of the books in the library.

Returns:

A list of languages.

AttributeCounts getBooksLanguagesWithCounts() const

Get all languagues of the books in the library with counts.

Returns:

A list of languages with the count of books in each language.

std::vector<std::string> getBooksCategories() const

Get all categories of the books in the library.

Returns:

A list of categories.

std::vector<std::string> getBooksCreators() const

Get all book creators of the books in the library.

Returns:

A list of book creators.

std::vector<std::string> getBooksPublishers() const

Get all book publishers of the books in the library.

Returns:

A list of book publishers.

const std::vector<kiwix::Bookmark> getBookmarks(bool onlyValidBookmarks = true) const

Get all bookmarks.

Returns:

A list of bookmarks

BookIdCollection getBooksIds() const

Get all book ids of the books in the library.

Returns:

A list of book ids.

BookIdCollection filter(const Filter &filter) const

Filter the library and return the id of the keep elements.

Parameters:

filter – The filter to use.

Returns:

The list of bookIds corresponding to the filter.

void sort(BookIdCollection &bookIds, supportedListSortBy sortBy, bool ascending) const

Sort (in place) bookIds using the given arguments.

Parameters:
  • bookIds – the list of book Ids to sort

  • sortBy – how to sort the books (UNSORTED, TITLE, SIZE, DATE, CREATOR, PUBLISHER)

  • ascending – ascending or descending

Revision getRevision() const

Return the current revision of the library.

The revision of the library is updated (incremented by one) by the addBook() and removeBookById() operations.

Returns:

Current revision of the library.

uint32_t removeBooksNotUpdatedSince(Revision rev)

Remove books that have not been updated since the specified revision.

Parameters:

rev – the library revision to use

Returns:

Count of books that were removed by this operation.

Public Static Functions

static inline LibraryPtr create()

Friends

friend class OPDSDumper
friend class libXMLDumper