Skip to content

Commit 5a90b35

Browse files
SuggestionDataBase::getSpellingCorrections()
Made spellings DB a member of SuggestionsDataBase so that it is not recreated on every call of getSpellingCorrections().
1 parent 84ad890 commit 5a90b35

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/suggestion.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
364376
bool 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-
427442
const 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);

src/suggestion_internal.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ struct TermWithFreq
5252
};
5353

5454
typedef std::vector<TermWithFreq> TermCollection;
55+
class SpellingsDB;
5556

5657
} // namespace suggestions
5758

@@ -62,8 +63,11 @@ typedef std::vector<TermWithFreq> TermCollection;
6263
class SuggestionDataBase {
6364
public: // methods
6465
SuggestionDataBase(const Archive& archive, bool verbose);
66+
~SuggestionDataBase();
6567

6668
const suggestions::TermCollection& getAllSuggestionTerms() const;
69+
std::vector<std::string> getSpellingCorrections(const std::string& word,
70+
uint32_t maxCount) const;
6771

6872
public: // data
6973
// The archive to get suggestions from.
@@ -103,6 +107,10 @@ class SuggestionDataBase {
103107

104108
private:
105109
void initXapianDb();
110+
111+
mutable std::mutex m_spellingsDBMutex;
112+
mutable std::unique_ptr<suggestions::SpellingsDB> m_spellingsDB;
113+
106114
#endif // LIBZIM_WITH_XAPIAN
107115
};
108116

0 commit comments

Comments
 (0)