Skip to content

Commit 84ad890

Browse files
SuggestionDataBase::getAllSuggestionTerms()
The results of obtaining all suggestion terms are memoized in SuggestionDataBase.
1 parent 19cdd5a commit 84ad890

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

src/suggestion.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -290,21 +290,7 @@ class QueryInfo
290290
std::string m_querySuffix;
291291
};
292292

293-
struct TermWithFreq
294-
{
295-
std::string term;
296-
uint32_t freq;
297-
298-
static bool freqPred(const TermWithFreq& t1, const TermWithFreq& t2) {
299-
return t1.freq > t2.freq;
300-
}
301-
302-
static bool dictionaryPred(const TermWithFreq& t1, const TermWithFreq& t2) {
303-
return t1.term < t2.term;
304-
}
305-
};
306-
307-
typedef std::vector<TermWithFreq> TermCollection;
293+
using namespace suggestions;
308294

309295
#if defined(LIBZIM_WITH_XAPIAN) && ! defined(_WIN32)
310296
#define ENABLE_SPELLINGSDB
@@ -408,7 +394,7 @@ TermCollection getTermCompletions(const SuggestionDataBase& db,
408394
return TermCollection();
409395
}
410396

411-
const TermCollection allTerms = getAllTerms(db);
397+
const TermCollection& allTerms = db.getAllSuggestionTerms();
412398
auto it = std::lower_bound(allTerms.begin(), allTerms.end(),
413399
TermWithFreq{termPrefix, 0},
414400
TermWithFreq::dictionaryPred);
@@ -427,7 +413,7 @@ std::vector<std::string> getSpellingCorrections(const SuggestionDataBase& db,
427413
{
428414
#ifdef ENABLE_SPELLINGSDB
429415
if ( db.hasDatabase() ) {
430-
const TermCollection allTerms = getAllTerms(db);
416+
const TermCollection& allTerms = db.getAllSuggestionTerms();
431417
const SpellingsDB sdb(allTerms);
432418
return sdb.getSpellingCorrections(word, maxCount);
433419
}
@@ -438,6 +424,15 @@ std::vector<std::string> getSpellingCorrections(const SuggestionDataBase& db,
438424

439425
} // unnamed namespace
440426

427+
const TermCollection& SuggestionDataBase::getAllSuggestionTerms() const
428+
{
429+
std::lock_guard<std::mutex> locker(m_suggestionTermsMutex);
430+
if ( m_suggestionTerms.empty() ) {
431+
m_suggestionTerms = getAllTerms(*this);
432+
}
433+
return m_suggestionTerms;
434+
}
435+
441436
SuggestionSearch::Results SuggestionSearch::getAutocompletionSuggestions(uint32_t maxCount) const {
442437
QueryInfo queryInfo(removeAccents(m_query));
443438

src/suggestion_internal.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@
3434
namespace zim
3535
{
3636

37+
namespace suggestions
38+
{
39+
40+
struct TermWithFreq
41+
{
42+
std::string term;
43+
uint32_t freq;
44+
45+
static bool freqPred(const TermWithFreq& t1, const TermWithFreq& t2) {
46+
return t1.freq > t2.freq;
47+
}
48+
49+
static bool dictionaryPred(const TermWithFreq& t1, const TermWithFreq& t2) {
50+
return t1.term < t2.term;
51+
}
52+
};
53+
54+
typedef std::vector<TermWithFreq> TermCollection;
55+
56+
} // namespace suggestions
57+
3758
/**
3859
* A class to encapsulate a xapian title index and it's archive and all the
3960
* information we can gather from it.
@@ -42,6 +63,8 @@ class SuggestionDataBase {
4263
public: // methods
4364
SuggestionDataBase(const Archive& archive, bool verbose);
4465

66+
const suggestions::TermCollection& getAllSuggestionTerms() const;
67+
4568
public: // data
4669
// The archive to get suggestions from.
4770
Archive m_archive;
@@ -52,6 +75,9 @@ class SuggestionDataBase {
5275
private: // data
5376
std::mutex m_mutex;
5477

78+
mutable std::mutex m_suggestionTermsMutex;
79+
mutable suggestions::TermCollection m_suggestionTerms;
80+
5581
#if defined(LIBZIM_WITH_XAPIAN)
5682

5783
public: // xapian based methods

0 commit comments

Comments
 (0)