Skip to content

Commit

Permalink
Simplify MatchConfig
Browse files Browse the repository at this point in the history
Hardcode error_tolerance_divisor to 4

Close #1412
  • Loading branch information
ManuelSchneid3r committed Aug 2, 2024
1 parent 6e45801 commit 61a5061
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
3 changes: 2 additions & 1 deletion include/albert/matchconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ struct MatchConfig
bool ignore_case = true;
bool ignore_diacritics = true;
bool ignore_word_order = true;
uint error_tolerance_divisor = 0;
bool fuzzy = false;
static const uint error_tolerance_divisor = 4;
};

}
27 changes: 16 additions & 11 deletions src/util/indexqueryhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,46 @@ using namespace std;
class IndexQueryHandler::Private
{
public:
ItemIndex index;
unique_ptr<ItemIndex> index;
std::shared_mutex index_mutex;
};

IndexQueryHandler::IndexQueryHandler() : d(new Private) {}
IndexQueryHandler::IndexQueryHandler() : d(new Private()) {}

IndexQueryHandler::~IndexQueryHandler() = default;

void IndexQueryHandler::setIndexItems(vector<IndexItem> &&index_items)
{
// Pointer check not necessary since never called before setFuzzyMatching
unique_lock l(d->index_mutex);
d->index.setItems(::move(index_items));
d->index->setItems(::move(index_items));
}

vector<RankItem> IndexQueryHandler::handleGlobalQuery(const Query *query) const
{
// Pointer check not necessary since never called before setFuzzyMatching
shared_lock l(d->index_mutex);
return d->index.search(query->string(), query->isValid());
return d->index->search(query->string(), query->isValid());
}

bool IndexQueryHandler::supportsFuzzyMatching() const { return true; }

void IndexQueryHandler::setFuzzyMatching(bool fuzzy)
{
if ((bool)d->index.config().error_tolerance_divisor == fuzzy)
if (!d->index)
{
auto c = d->index.config();

if (!fuzzy)
c.error_tolerance_divisor = 0;
auto c = MatchConfig{.fuzzy = fuzzy};
d->index = make_unique<ItemIndex>(c);
updateIndexItems();
}
else if ((bool)d->index->config().fuzzy != fuzzy)
{
auto c = d->index->config();
c.fuzzy = fuzzy;

d->index_mutex.lock();
d->index = ItemIndex(c);
d->index = make_unique<ItemIndex>(c);
d->index_mutex.unlock();

updateIndexItems();
}
}
4 changes: 2 additions & 2 deletions src/util/itemindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ vector<WordMatch> ItemIndex::Private::getWordMatches(const QString &word, const
matches.emplace_back(*it, word_length);

// Get the (fuzzy) prefix matches
if (config.error_tolerance_divisor)
if (config.fuzzy)
{
// Exclusion range for already collected prefix matches
Index exclude_begin = eq_begin - index.words.begin(); // Ignore interval. closed begin [
Expand Down Expand Up @@ -305,7 +305,7 @@ void ItemIndex::setItems(vector<albert::IndexItem> &&index_items)
}
new_index.words.shrink_to_fit();

if (d->config.error_tolerance_divisor)
if (d->config.fuzzy)
{
// Build n_gram_index
for (Index word_index = 0; word_index < (Index)new_index.words.size(); ++word_index)
Expand Down
2 changes: 1 addition & 1 deletion src/util/matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class MatcherPrivate
if ((it->size() <= oit->size()))
{
// check if the query word is a prefix of the matched word
if(config.error_tolerance_divisor) // fuzzy
if(config.fuzzy)
{
uint allowed_errors = it->size() / config.error_tolerance_divisor;
auto edit_distance = levenshtein.computePrefixEditDistanceWithLimit(
Expand Down

0 comments on commit 61a5061

Please sign in to comment.