@@ -290,7 +290,10 @@ class QueryInfo
290290 std::string m_querySuffix;
291291};
292292
293- using namespace suggestions ;
293+ } // unnamed namespace
294+
295+ namespace suggestions
296+ {
294297
295298#if defined(LIBZIM_WITH_XAPIAN) && ! defined(_WIN32)
296299#define ENABLE_SPELLINGSDB
@@ -361,6 +364,15 @@ std::vector<std::string> SpellingsDB::getSpellingCorrections(const std::string&
361364}
362365#endif // ENABLE_SPELLINGSDB
363366
367+ } // namespace suggestions
368+
369+ SuggestionDataBase::~SuggestionDataBase () = default ;
370+
371+ namespace
372+ {
373+
374+ using namespace suggestions ;
375+
364376bool isXapianTermPrefix (unsigned char c) {
365377 return ' A' <= c && c <= ' Z' ;
366378}
@@ -407,23 +419,26 @@ TermCollection getTermCompletions(const SuggestionDataBase& db,
407419 return result;
408420}
409421
410- std::vector<std::string> getSpellingCorrections (const SuggestionDataBase& db,
422+ } // unnamed namespace
423+
424+ std::vector<std::string> SuggestionDataBase::getSpellingCorrections (
411425 const std::string& word,
412- uint32_t maxCount)
426+ uint32_t maxCount) const
413427{
414428#ifdef ENABLE_SPELLINGSDB
415- if ( db.hasDatabase () ) {
416- const TermCollection& allTerms = db.getAllSuggestionTerms ();
417- const SpellingsDB sdb (allTerms);
418- return sdb.getSpellingCorrections (word, maxCount);
429+ if ( this ->hasDatabase () ) {
430+ std::lock_guard<std::mutex> locker (m_spellingsDBMutex);
431+ if ( !m_spellingsDB ) {
432+ const TermCollection& allTerms = this ->getAllSuggestionTerms ();
433+ m_spellingsDB.reset (new SpellingsDB (allTerms));
434+ }
435+ return m_spellingsDB->getSpellingCorrections (word, maxCount);
419436 }
420437#endif // ENABLE_SPELLINGSDB
421438
422439 return {};
423440}
424441
425- } // unnamed namespace
426-
427442const TermCollection& SuggestionDataBase::getAllSuggestionTerms () const
428443{
429444 std::lock_guard<std::mutex> locker (m_suggestionTermsMutex);
@@ -459,7 +474,7 @@ SuggestionSearch::Results SuggestionSearch::getSpellingSuggestions(uint32_t maxC
459474
460475 SuggestionSearch::Results r;
461476 if ( !queryInfo.wordBeingEdited ().empty () ) {
462- const auto terms = getSpellingCorrections (*mp_internalDb, queryInfo.wordBeingEdited (), maxCount);
477+ const auto terms = mp_internalDb-> getSpellingCorrections (queryInfo.wordBeingEdited (), maxCount);
463478
464479 for (const auto & t : terms) {
465480 const auto suggestion = queryInfo.spellingSuggestion (t);
0 commit comments