Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPDS name mapper #830

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ namespace kiwix
class LibraryManipulator
{
public: // functions
explicit LibraryManipulator(Library* library);
explicit LibraryManipulator(std::shared_ptr<Library> library);
virtual ~LibraryManipulator();

Library& getLibrary() const { return library; }
Library& getLibrary() const { return *library.get(); }

bool addBookToLibrary(const Book& book);
void addBookmarkToLibrary(const Bookmark& bookmark);
Expand All @@ -52,7 +52,7 @@ class LibraryManipulator
virtual void booksWereRemovedFromLibrary();

private: // data
kiwix::Library& library;
std::shared_ptr<kiwix::Library> library;
};

/**
Expand All @@ -65,7 +65,7 @@ class Manager

public: // functions
explicit Manager(LibraryManipulator* manipulator);
explicit Manager(Library* library);
explicit Manager(std::shared_ptr<Library> library);

/**
* Read a `library.xml` and add book in the file to the library.
Expand Down
6 changes: 3 additions & 3 deletions include/name_mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class HumanReadableNameMapper : public NameMapper {
std::map<std::string, std::string> m_nameToId;

public:
HumanReadableNameMapper(kiwix::Library& library, bool withAlias);
HumanReadableNameMapper(const kiwix::Library& library, bool withAlias);
virtual ~HumanReadableNameMapper() = default;
virtual std::string getNameForId(const std::string& id) const;
virtual std::string getIdForName(const std::string& name) const;
Expand All @@ -59,7 +59,7 @@ class HumanReadableNameMapper : public NameMapper {
class UpdatableNameMapper : public NameMapper {
typedef std::shared_ptr<NameMapper> NameMapperHandle;
public:
UpdatableNameMapper(Library& library, bool withAlias);
UpdatableNameMapper(std::shared_ptr<Library> library, bool withAlias);

virtual std::string getNameForId(const std::string& id) const;
virtual std::string getIdForName(const std::string& name) const;
Expand All @@ -71,7 +71,7 @@ class UpdatableNameMapper : public NameMapper {

private:
mutable std::mutex mutex;
Library& library;
std::shared_ptr<Library> library;
NameMapperHandle nameMapper;
const bool withAlias;
};
Expand Down
16 changes: 4 additions & 12 deletions include/opds_dumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
#include <time.h>
#include <sstream>
#include <string>
#include <vector>

#include <pugixml.hpp>

#include "library.h"
#include "server.h"

using namespace std;

Expand All @@ -40,8 +41,7 @@ namespace kiwix
class OPDSDumper
{
public:
OPDSDumper() = default;
OPDSDumper(Library* library);
OPDSDumper(Server::Configuration configuration);
~OPDSDumper();

/**
Expand Down Expand Up @@ -92,13 +92,6 @@ class OPDSDumper
*/
void setLibraryId(const std::string& id) { this->libraryId = id;}

/**
* Set the root location used when generating url.
*
* @param rootLocation the root location to use.
*/
void setRootLocation(const std::string& rootLocation) { this->rootLocation = rootLocation; }

/**
* Set some informations about the search results.
*
Expand All @@ -109,9 +102,8 @@ class OPDSDumper
void setOpenSearchInfo(int totalResult, int startIndex, int count);

protected:
kiwix::Library* library;
Server::Configuration m_configuration;
std::string libraryId;
std::string rootLocation;
int m_totalResults;
int m_startIndex;
int m_count;
Expand Down
8 changes: 4 additions & 4 deletions include/search_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class SearchRenderer
* @param start The start offset used for the srs.
* @param estimatedResultCount The estimatedResultCount of the whole search
*/
SearchRenderer(zim::SearchResultSet srs, NameMapper* mapper,
SearchRenderer(zim::SearchResultSet srs, std::shared_ptr<NameMapper> mapper,
unsigned int start, unsigned int estimatedResultCount);

/**
Expand All @@ -58,7 +58,7 @@ class SearchRenderer
* @param start The start offset used for the srs.
* @param estimatedResultCount The estimatedResultCount of the whole search
*/
SearchRenderer(zim::SearchResultSet srs, NameMapper* mapper, Library* library,
SearchRenderer(zim::SearchResultSet srs, std::shared_ptr<NameMapper> mapper, std::shared_ptr<Library> library,
unsigned int start, unsigned int estimatedResultCount);

~SearchRenderer();
Expand Down Expand Up @@ -106,8 +106,8 @@ class SearchRenderer
protected:
std::string beautifyInteger(const unsigned int number);
zim::SearchResultSet m_srs;
NameMapper* mp_nameMapper;
Library* mp_library;
std::shared_ptr<NameMapper> mp_nameMapper;
std::shared_ptr<Library> mp_library;
std::string searchBookQuery;
std::string searchPattern;
std::string protocolPrefix;
Expand Down
109 changes: 81 additions & 28 deletions include/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,81 @@ namespace kiwix
class NameMapper;
class InternalServer;


class Server {
public:
public:
class Configuration {
public:
explicit Configuration(std::shared_ptr<Library> library, std::shared_ptr<NameMapper> nameMapper=nullptr);

Configuration& setRoot(const std::string& root);
Configuration& setAddress(const std::string& addr) {
m_addr = addr;
return *this;
}

Configuration& setPort(int port) {
m_port = port;
return *this;
}

Configuration& setNbThreads(int threads) {
m_nbThreads = threads;
return *this;
}

Configuration& setMultiZimSearchLimit(unsigned int limit) {
m_multizimSearchLimit = limit;
return *this;
}

Configuration& setIpConnectionLimit(int limit) {
m_ipConnectionLimit = limit;
return *this;
}

Configuration& setVerbose(bool verbose) {
m_verbose = verbose;
return *this;
}

Configuration& setIndexTemplateString(const std::string& indexTemplateString) {
m_indexTemplateString = indexTemplateString;
return *this;
}

Configuration& setTaskbar(bool withTaskbar, bool withLibraryButton) {
m_withTaskbar = withTaskbar;
m_withLibraryButton = withLibraryButton;
return *this;
}

Configuration& setBlockExternalLinks(bool blockExternalLinks) {
m_blockExternalLinks = blockExternalLinks;
return *this;
}

std::shared_ptr<Library> mp_library;
std::shared_ptr<NameMapper> mp_nameMapper;
std::string m_root = "";
std::string m_addr = "";
std::string m_indexTemplateString = "";
int m_port = 80;
int m_nbThreads = 1;
unsigned int m_multizimSearchLimit = 0;
bool m_verbose = false;
bool m_withTaskbar = true;
bool m_withLibraryButton = true;
bool m_blockExternalLinks = false;
int m_ipConnectionLimit = 0;
};

/**
* The default constructor.
*
* @param library The library to serve.
*/
Server(Library* library, NameMapper* nameMapper=nullptr);

explicit Server(const Configuration& configuration);
virtual ~Server();

/**
Expand All @@ -50,35 +116,22 @@ namespace kiwix
*/
void stop();

void setRoot(const std::string& root);
void setAddress(const std::string& addr) { m_addr = addr; }
void setPort(int port) { m_port = port; }
void setNbThreads(int threads) { m_nbThreads = threads; }
void setMultiZimSearchLimit(unsigned int limit) { m_multizimSearchLimit = limit; }
void setIpConnectionLimit(int limit) { m_ipConnectionLimit = limit; }
void setVerbose(bool verbose) { m_verbose = verbose; }
void setIndexTemplateString(const std::string& indexTemplateString) { m_indexTemplateString = indexTemplateString; }
void setTaskbar(bool withTaskbar, bool withLibraryButton)
{ m_withTaskbar = withTaskbar; m_withLibraryButton = withLibraryButton; }
void setBlockExternalLinks(bool blockExternalLinks)
{ m_blockExternalLinks = blockExternalLinks; }
/**
* Tell if the server is running or not.
*/
bool isRunning();

/**
* Get the port of the server
*/
int getPort();

/**
* Get the ipAddress of the server
*/
std::string getAddress();

protected:
Library* mp_library;
NameMapper* mp_nameMapper;
std::string m_root = "";
std::string m_addr = "";
std::string m_indexTemplateString = "";
int m_port = 80;
int m_nbThreads = 1;
unsigned int m_multizimSearchLimit = 0;
bool m_verbose = false;
bool m_withTaskbar = true;
bool m_withLibraryButton = true;
bool m_blockExternalLinks = false;
int m_ipConnectionLimit = 0;
std::unique_ptr<InternalServer> mp_server;
};
}
Expand Down
12 changes: 6 additions & 6 deletions src/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ struct NoDelete
// LibraryManipulator
////////////////////////////////////////////////////////////////////////////////

LibraryManipulator::LibraryManipulator(Library* library)
: library(*library)
LibraryManipulator::LibraryManipulator(std::shared_ptr<Library> library)
: library(library)
{}

LibraryManipulator::~LibraryManipulator()
{}

bool LibraryManipulator::addBookToLibrary(const Book& book)
{
const auto ret = library.addBook(book);
const auto ret = library->addBook(book);
if ( ret ) {
bookWasAddedToLibrary(book);
}
Expand All @@ -59,13 +59,13 @@ bool LibraryManipulator::addBookToLibrary(const Book& book)

void LibraryManipulator::addBookmarkToLibrary(const Bookmark& bookmark)
{
library.addBookmark(bookmark);
library->addBookmark(bookmark);
bookmarkWasAddedToLibrary(bookmark);
}

uint32_t LibraryManipulator::removeBooksNotUpdatedSince(Library::Revision rev)
{
const auto n = library.removeBooksNotUpdatedSince(rev);
const auto n = library->removeBooksNotUpdatedSince(rev);
if ( n != 0 ) {
booksWereRemovedFromLibrary();
}
Expand Down Expand Up @@ -95,7 +95,7 @@ Manager::Manager(LibraryManipulator* manipulator):
{
}

Manager::Manager(Library* library) :
Manager::Manager(std::shared_ptr<Library> library) :
writableLibraryPath(""),
manipulator(new LibraryManipulator(library))
{
Expand Down
6 changes: 3 additions & 3 deletions src/name_mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace kiwix {

HumanReadableNameMapper::HumanReadableNameMapper(kiwix::Library& library, bool withAlias) {
HumanReadableNameMapper::HumanReadableNameMapper(const kiwix::Library& library, bool withAlias) {
for (auto& bookId: library.filter(kiwix::Filter().local(true).valid(true))) {
auto& currentBook = library.getBookById(bookId);
auto bookName = currentBook.getHumanReadableIdFromPath();
Expand Down Expand Up @@ -63,7 +63,7 @@ std::string HumanReadableNameMapper::getIdForName(const std::string& name) const
// UpdatableNameMapper
////////////////////////////////////////////////////////////////////////////////

UpdatableNameMapper::UpdatableNameMapper(Library& lib, bool withAlias)
UpdatableNameMapper::UpdatableNameMapper(std::shared_ptr<Library> lib, bool withAlias)
: library(lib)
, withAlias(withAlias)
{
Expand All @@ -72,7 +72,7 @@ UpdatableNameMapper::UpdatableNameMapper(Library& lib, bool withAlias)

void UpdatableNameMapper::update()
{
const auto newNameMapper = new HumanReadableNameMapper(library, withAlias);
const auto newNameMapper = new HumanReadableNameMapper(*library, withAlias);
std::lock_guard<std::mutex> lock(mutex);
nameMapper.reset(newNameMapper);
}
Expand Down
Loading