Skip to content

Commit

Permalink
Merge pull request #991 from kiwix/no_raw_pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr authored Oct 5, 2023
2 parents e13324f + 9166b67 commit e49abc1
Show file tree
Hide file tree
Showing 22 changed files with 249 additions and 279 deletions.
46 changes: 37 additions & 9 deletions include/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@

#define KIWIX_LIBRARY_VERSION "20110515"

namespace Xapian {
class WritableDatabase;
};

namespace kiwix
{

Expand Down Expand Up @@ -173,31 +177,42 @@ class ZimSearcher : public zim::Searcher
std::mutex m_mutex;
};

template<typename, typename>
class ConcurrentCache;

template<typename, typename>
class MultiKeyCache;

using LibraryPtr = std::shared_ptr<Library>;
using ConstLibraryPtr = std::shared_ptr<const Library>;

/**
* A Library store several books.
*/
class Library
class Library: public std::enable_shared_from_this<Library>
{
// all data fields must be added in LibraryBase
mutable std::mutex m_mutex;

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

public:
private:
Library();

public:
[[nodiscard]] static LibraryPtr create() {
return LibraryPtr(new Library());
}
~Library();

/**
* Library is not a copiable object. However it can be moved.
*/
Library(const Library& ) = delete;
Library(Library&& );
Library(Library&& ) = delete;
void operator=(const Library& ) = delete;
Library& operator=(Library&& );
Library& operator=(Library&& ) = delete;

/**
* Add a book to the library.
Expand Down Expand Up @@ -368,19 +383,32 @@ class Library

private: // types
typedef const std::string& (Book::*BookStrPropMemFn)() const;
struct Impl;
struct Entry : Book
{
Library::Revision lastUpdatedRevision = 0;
};

private: // functions
AttributeCounts getBookAttributeCounts(BookStrPropMemFn p) const;
std::vector<std::string> getBookPropValueSet(BookStrPropMemFn p) const;
BookIdCollection filterViaBookDB(const Filter& filter) const;
unsigned int getBookCount_not_protected(const bool localBooks, const bool remoteBooks) const;
void updateBookDB(const Book& book);
void dropCache(const std::string& bookId);

private: //data
std::unique_ptr<Impl> mp_impl;
mutable std::mutex m_mutex;
Library::Revision m_revision;
std::map<std::string, Entry> m_books;
using ArchiveCache = ConcurrentCache<std::string, std::shared_ptr<zim::Archive>>;
std::unique_ptr<ArchiveCache> mp_archiveCache;
using SearcherCache = MultiKeyCache<std::string, std::shared_ptr<ZimSearcher>>;
std::unique_ptr<SearcherCache> mp_searcherCache;
std::vector<kiwix::Bookmark> m_bookmarks;
std::unique_ptr<Xapian::WritableDatabase> m_bookDB;
};


}

#endif
12 changes: 6 additions & 6 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(LibraryPtr library);
virtual ~LibraryManipulator();

Library& getLibrary() const { return library; }
LibraryPtr getLibrary() const { return library; }

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;
LibraryPtr library;
};

/**
Expand All @@ -64,8 +64,8 @@ class Manager
typedef std::vector<std::string> Paths;

public: // functions
explicit Manager(LibraryManipulator* manipulator);
explicit Manager(Library* library);
explicit Manager(LibraryManipulator manipulator);
explicit Manager(LibraryPtr library);

/**
* Read a `library.xml` and add book in the file to the library.
Expand Down Expand Up @@ -163,7 +163,7 @@ class Manager
uint64_t m_itemsPerPage = 0;

protected:
std::shared_ptr<kiwix::LibraryManipulator> manipulator;
kiwix::LibraryManipulator manipulator;

bool readBookFromPath(const std::string& path, Book* book);
bool parseXmlDom(const pugi::xml_document& doc,
Expand Down
4 changes: 0 additions & 4 deletions include/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ headers = [
'common.h',
'library.h',
'manager.h',
'libxml_dumper.h',
'opds_dumper.h',
'library_dumper.h',
'html_dumper.h',
'downloader.h',
'search_renderer.h',
'server.h',
Expand Down
4 changes: 2 additions & 2 deletions include/name_mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 16 additions & 26 deletions include/search_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,14 @@ class NameMapper;
class SearchRenderer
{
public:
/**
* Construct a SearchRenderer from a SearchResultSet.
*
* The constructed version of the SearchRenderer will not introduce
* the book name for each result. It is better to use the other constructor
* with a Library pointer to have a better html page.
*
* @param srs The `SearchResultSet` to render.
* @param mapper The `NameMapper` to use to do the rendering.
* @param start The start offset used for the srs.
* @param estimatedResultCount The estimatedResultCount of the whole search
*/
SearchRenderer(zim::SearchResultSet srs, NameMapper* mapper,
unsigned int start, unsigned int estimatedResultCount);

/**
* Construct a SearchRenderer from a SearchResultSet.
*
* @param srs The `SearchResultSet` to render.
* @param mapper The `NameMapper` to use to do the rendering.
* @param library The `Library` to use to look up book details for search results.
* @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,
unsigned int start, unsigned int estimatedResultCount);
SearchRenderer(zim::SearchResultSet srs, unsigned int start, unsigned int estimatedResultCount);

~SearchRenderer();

Expand Down Expand Up @@ -90,24 +72,32 @@ class SearchRenderer
this->pageLength = pageLength;
}

std::string renderTemplate(const std::string& tmpl_str);

/**
* Generate the html page with the resutls of the search.
*
* @param mapper The `NameMapper` to use to do the rendering.
* @param library The `Library` to use to look up book details for search results.
May be nullptr. In this case, bookName is not set in the rendered string.
* @return The html string
*/
std::string getHtml();
std::string getHtml(const NameMapper& mapper, const Library* library);

/**
/**
* Generate the xml page with the resutls of the search.
*
* @param mapper The `NameMapper` to use to do the rendering.
* @param library The `Library` to use to look up book details for search results.
May be nullptr. In this case, bookName is not set in the rendered string.
* @return The xml string
*/
std::string getXml();
std::string getXml(const NameMapper& mapper, const Library* library);

protected: // function
std::string renderTemplate(const std::string& tmpl_str, const NameMapper& mapper, const Library *library);

protected:
std::string beautifyInteger(const unsigned int number);
zim::SearchResultSet m_srs;
NameMapper* mp_nameMapper;
Library* mp_library;
std::string searchBookQuery;
std::string searchPattern;
std::string protocolPrefix;
Expand Down
6 changes: 3 additions & 3 deletions include/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace kiwix
*
* @param library The library to serve.
*/
Server(Library* library, NameMapper* nameMapper=nullptr);
Server(std::shared_ptr<Library> library, std::shared_ptr<NameMapper> nameMapper=nullptr);

virtual ~Server();

Expand Down Expand Up @@ -66,8 +66,8 @@ namespace kiwix
std::string getAddress();

protected:
Library* mp_library;
NameMapper* mp_nameMapper;
std::shared_ptr<Library> mp_library;
std::shared_ptr<NameMapper> mp_nameMapper;
std::string m_root = "";
std::string m_addr = "";
std::string m_indexTemplateString = "";
Expand Down
File renamed without changes.
Loading

0 comments on commit e49abc1

Please sign in to comment.