Make the Searcher owning the stored Reader

If we keep a reference to a `Reader` it is better to (share) owning
the reference. Else the reader may be deleted after we create the searcher.

This is especially the case now we are creating the `Reader` at demand
and we don't store it in the library's cache.
This commit is contained in:
Matthieu Gautier 2022-06-02 17:08:17 +02:00
parent 3704d8ab87
commit d196496802
2 changed files with 6 additions and 6 deletions

View File

@ -76,10 +76,10 @@ class Searcher
* @return true if the reader has been added. * @return true if the reader has been added.
* false if the reader cannot be added (no embedded fulltext index present) * false if the reader cannot be added (no embedded fulltext index present)
*/ */
bool add_reader(Reader* reader); bool add_reader(std::shared_ptr<Reader> reader);
Reader* get_reader(int index); std::shared_ptr<Reader> get_reader(int index);
/** /**
* Start a search on the zim associated to the Searcher. * Start a search on the zim associated to the Searcher.
@ -161,7 +161,7 @@ class Searcher
const unsigned int maxResultCount, const unsigned int maxResultCount,
const bool verbose = false); const bool verbose = false);
std::vector<Reader*> readers; std::vector<std::shared_ptr<Reader>> readers;
std::unique_ptr<SearcherInternal> internal; std::unique_ptr<SearcherInternal> internal;
std::unique_ptr<SuggestionInternal> suggestionInternal; std::unique_ptr<SuggestionInternal> suggestionInternal;
std::string searchPattern; std::string searchPattern;

View File

@ -89,13 +89,13 @@ Searcher::~Searcher()
{ {
} }
bool Searcher::add_reader(Reader* reader) bool Searcher::add_reader(std::shared_ptr<Reader> reader)
{ {
if (!reader->hasFulltextIndex()) { if (!reader->hasFulltextIndex()) {
return false; return false;
} }
for ( const Reader* const existing_reader : readers ) { for ( auto existing_reader : readers ) {
if ( existing_reader->getZimArchive()->getUuid() == reader->getZimArchive()->getUuid() ) if ( existing_reader->getZimArchive()->getUuid() == reader->getZimArchive()->getUuid() )
return false; return false;
} }
@ -105,7 +105,7 @@ bool Searcher::add_reader(Reader* reader)
} }
Reader* Searcher::get_reader(int readerIndex) std::shared_ptr<Reader> Searcher::get_reader(int readerIndex)
{ {
return readers.at(readerIndex); return readers.at(readerIndex);
} }