mirror of https://github.com/kiwix/libkiwix.git
Use a map to store the Library's books.
Having the books sorted is useless. We handle books by id not by index.
This commit is contained in:
parent
541fb0cfd1
commit
57ac6f0305
|
@ -24,7 +24,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <map>
|
||||||
|
|
||||||
#include "common/regexTools.h"
|
#include "common/regexTools.h"
|
||||||
#include "common/stringTools.h"
|
#include "common/stringTools.h"
|
||||||
|
@ -88,7 +88,7 @@ class Book
|
||||||
*/
|
*/
|
||||||
class Library
|
class Library
|
||||||
{
|
{
|
||||||
std::vector<kiwix::Book> books;
|
std::map<std::string, kiwix::Book> books;
|
||||||
public:
|
public:
|
||||||
Library();
|
Library();
|
||||||
~Library();
|
~Library();
|
||||||
|
@ -108,7 +108,6 @@ class Library
|
||||||
|
|
||||||
Book& getBookById(const std::string& id);
|
Book& getBookById(const std::string& id);
|
||||||
|
|
||||||
bool removeBookByIndex(const unsigned int bookIndex);
|
|
||||||
/**
|
/**
|
||||||
* Remove a book from the library.
|
* Remove a book from the library.
|
||||||
*
|
*
|
||||||
|
|
|
@ -99,14 +99,6 @@ class Manager
|
||||||
*/
|
*/
|
||||||
bool readOpds(const string& content, const std::string& urlHost);
|
bool readOpds(const string& content, const std::string& urlHost);
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove a book from the library.
|
|
||||||
*
|
|
||||||
* @param bookIndex the index of the book to remove
|
|
||||||
* @return True
|
|
||||||
*/
|
|
||||||
bool removeBookByIndex(const unsigned int bookIndex);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a book from the library.
|
* Remove a book from the library.
|
||||||
*
|
*
|
||||||
|
|
|
@ -144,55 +144,33 @@ bool Library::addBook(const Book& book)
|
||||||
{
|
{
|
||||||
/* Try to find it */
|
/* Try to find it */
|
||||||
std::vector<kiwix::Book>::iterator itr;
|
std::vector<kiwix::Book>::iterator itr;
|
||||||
for (itr = this->books.begin(); itr != this->books.end(); ++itr) {
|
try {
|
||||||
if (itr->id == book.id) {
|
auto& oldbook = books.at(book.id);
|
||||||
itr->update(book);
|
oldbook.update(book);
|
||||||
return false;
|
return false;
|
||||||
}
|
} catch (std::out_of_range&) {
|
||||||
}
|
books[book.id] = book;
|
||||||
|
|
||||||
/* otherwise */
|
|
||||||
this->books.push_back(book);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Library::removeBookByIndex(const unsigned int bookIndex)
|
|
||||||
{
|
|
||||||
books.erase(books.begin() + bookIndex);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Library::removeBookById(const std::string& id)
|
bool Library::removeBookById(const std::string& id)
|
||||||
{
|
{
|
||||||
auto itr = books.begin();
|
return books.erase(id) == 1;
|
||||||
for(; itr != books.end(); itr ++) {
|
|
||||||
if (itr->id == id) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (itr != books.end()) {
|
|
||||||
books.erase(itr);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Book& Library::getBookById(const std::string& id)
|
Book& Library::getBookById(const std::string& id)
|
||||||
{
|
{
|
||||||
for(auto& book: books) {
|
return books.at(id);
|
||||||
if(book.id == id) {
|
|
||||||
return book;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw std::runtime_error("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int Library::getBookCount(const bool localBooks,
|
unsigned int Library::getBookCount(const bool localBooks,
|
||||||
const bool remoteBooks)
|
const bool remoteBooks)
|
||||||
{
|
{
|
||||||
unsigned int result = 0;
|
unsigned int result = 0;
|
||||||
for (auto& book: books) {
|
for (auto& pair: books) {
|
||||||
|
auto& book = pair.second;
|
||||||
if ((!book.path.empty() && localBooks)
|
if ((!book.path.empty() && localBooks)
|
||||||
|| (book.path.empty() && remoteBooks)) {
|
|| (book.path.empty() && remoteBooks)) {
|
||||||
result++;
|
result++;
|
||||||
|
@ -208,7 +186,8 @@ Library Library::filter(const std::string& search) {
|
||||||
return library;
|
return library;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto& book:books) {
|
for(auto& pair:books) {
|
||||||
|
auto& book = pair.second;
|
||||||
if (matchRegex(book.title, "\\Q" + search + "\\E")
|
if (matchRegex(book.title, "\\Q" + search + "\\E")
|
||||||
|| matchRegex(book.description, "\\Q" + search + "\\E")) {
|
|| matchRegex(book.description, "\\Q" + search + "\\E")) {
|
||||||
library.addBook(book);
|
library.addBook(book);
|
||||||
|
@ -228,7 +207,8 @@ bool Library::writeToFile(const std::string& path) {
|
||||||
libraryNode.append_attribute("version") = version.c_str();
|
libraryNode.append_attribute("version") = version.c_str();
|
||||||
|
|
||||||
/* Add each book */
|
/* Add each book */
|
||||||
for (auto& book: books) {
|
for (auto& pair: books) {
|
||||||
|
auto& book = pair.second;
|
||||||
if (!book.readOnly) {
|
if (!book.readOnly) {
|
||||||
pugi::xml_node bookNode = libraryNode.append_child("book");
|
pugi::xml_node bookNode = libraryNode.append_child("book");
|
||||||
bookNode.append_attribute("id") = book.id.c_str();
|
bookNode.append_attribute("id") = book.id.c_str();
|
||||||
|
@ -312,7 +292,8 @@ std::vector<std::string> Library::getBooksLanguages()
|
||||||
std::vector<std::string> booksLanguages;
|
std::vector<std::string> booksLanguages;
|
||||||
std::map<std::string, bool> booksLanguagesMap;
|
std::map<std::string, bool> booksLanguagesMap;
|
||||||
|
|
||||||
for (auto& book: books) {
|
for (auto& pair: books) {
|
||||||
|
auto& book = pair.second;
|
||||||
if (booksLanguagesMap.find(book.language) == booksLanguagesMap.end()) {
|
if (booksLanguagesMap.find(book.language) == booksLanguagesMap.end()) {
|
||||||
if (book.origId.empty()) {
|
if (book.origId.empty()) {
|
||||||
booksLanguagesMap[book.language] = true;
|
booksLanguagesMap[book.language] = true;
|
||||||
|
@ -329,7 +310,8 @@ std::vector<std::string> Library::getBooksCreators()
|
||||||
std::vector<std::string> booksCreators;
|
std::vector<std::string> booksCreators;
|
||||||
std::map<std::string, bool> booksCreatorsMap;
|
std::map<std::string, bool> booksCreatorsMap;
|
||||||
|
|
||||||
for (auto& book: books) {
|
for (auto& pair: books) {
|
||||||
|
auto& book = pair.second;
|
||||||
if (booksCreatorsMap.find(book.creator) == booksCreatorsMap.end()) {
|
if (booksCreatorsMap.find(book.creator) == booksCreatorsMap.end()) {
|
||||||
if (book.origId.empty()) {
|
if (book.origId.empty()) {
|
||||||
booksCreatorsMap[book.creator] = true;
|
booksCreatorsMap[book.creator] = true;
|
||||||
|
@ -345,8 +327,8 @@ std::vector<std::string> Library::getBooksIds()
|
||||||
{
|
{
|
||||||
std::vector<std::string> booksIds;
|
std::vector<std::string> booksIds;
|
||||||
|
|
||||||
for (auto& book: books) {
|
for (auto& pair: books) {
|
||||||
booksIds.push_back(book.id);
|
booksIds.push_back(pair.first);
|
||||||
}
|
}
|
||||||
|
|
||||||
return booksIds;
|
return booksIds;
|
||||||
|
@ -357,7 +339,8 @@ std::vector<std::string> Library::getBooksPublishers()
|
||||||
std::vector<std::string> booksPublishers;
|
std::vector<std::string> booksPublishers;
|
||||||
std::map<std::string, bool> booksPublishersMap;
|
std::map<std::string, bool> booksPublishersMap;
|
||||||
|
|
||||||
for (auto& book:books) {
|
for (auto& pair:books) {
|
||||||
|
auto& book = pair.second;
|
||||||
if (booksPublishersMap.find(book.publisher) == booksPublishersMap.end()) {
|
if (booksPublishersMap.find(book.publisher) == booksPublishersMap.end()) {
|
||||||
if (book.origId.empty()) {
|
if (book.origId.empty()) {
|
||||||
booksPublishersMap[book.publisher] = true;
|
booksPublishersMap[book.publisher] = true;
|
||||||
|
|
|
@ -285,11 +285,6 @@ bool Manager::readBookFromPath(const string path, kiwix::Book* book)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Manager::removeBookByIndex(const unsigned int bookIndex)
|
|
||||||
{
|
|
||||||
return this->library.removeBookByIndex(bookIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Manager::removeBookById(const string id)
|
bool Manager::removeBookById(const string id)
|
||||||
{
|
{
|
||||||
return library.removeBookById(id);
|
return library.removeBookById(id);
|
||||||
|
|
|
@ -110,8 +110,8 @@ string OPDSDumper::dumpOPDSFeed()
|
||||||
search_link.append_attribute("href") = searchDescriptionUrl.c_str();
|
search_link.append_attribute("href") = searchDescriptionUrl.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto book: library.books) {
|
for (auto& pair: library.books) {
|
||||||
handleBook(book, root_node);
|
handleBook(pair.second, root_node);
|
||||||
}
|
}
|
||||||
|
|
||||||
return nodeToString(root_node);
|
return nodeToString(root_node);
|
||||||
|
|
Loading…
Reference in New Issue