Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Refactor `filter_result_iterator_t` methods.

Harpreet Sangar91dd04add209
diff --git a/include/filter_result_iterator.h b/include/filter_result_iterator.h
index 0e06efe4..d74cb523 100644
--- a/include/filter_result_iterator.h
+++ b/include/filter_result_iterator.h
@@ -160,7 +160,7 @@ public:
/// Returns the status of the initialization of iterator tree.
Option<bool> init_status();
- /// Recursively computes the result of each node and stores the final result in root node.
+ /// Recursively computes the result of each node and stores the final result in the root node.
void compute_result();
/// Returns a tri-state:
diff --git a/src/filter_result_iterator.cpp b/src/filter_result_iterator.cpp
index 2355e871..25cfd2c3 100644
--- a/src/filter_result_iterator.cpp
+++ b/src/filter_result_iterator.cpp
@@ -401,6 +401,22 @@ void filter_result_iterator_t::next() {
return;
}
+ // No need to traverse iterator tree if there's only one filter or compute_result() has been called.
+ if (is_filter_result_initialized) {
+ if (++result_index >= filter_result.count) {
+ is_valid = false;
+ return;
+ }
+
+ seq_id = filter_result.docs[result_index];
+ reference.clear();
+ for (auto const& item: filter_result.reference_filter_results) {
+ reference[item.first] = item.second[result_index];
+ }
+
+ return;
+ }
+
if (filter_node->isOperator) {
// Advance the subtrees and then apply operators to arrive at the next valid doc.
if (filter_node->filter_operator == AND) {
@@ -423,21 +439,6 @@ void filter_result_iterator_t::next() {
return;
}
- if (is_filter_result_initialized) {
- if (++result_index >= filter_result.count) {
- is_valid = false;
- return;
- }
-
- seq_id = filter_result.docs[result_index];
- reference.clear();
- for (auto const& item: filter_result.reference_filter_results) {
- reference[item.first] = item.second[result_index];
- }
-
- return;
- }
-
const filter a_filter = filter_node->filter_exp;
if (!index->field_is_indexed(a_filter.field_name)) {
@@ -1024,20 +1025,7 @@ void filter_result_iterator_t::skip_to(uint32_t id) {
return;
}
- if (filter_node->isOperator) {
- // Skip the subtrees to id and then apply operators to arrive at the next valid doc.
- left_it->skip_to(id);
- right_it->skip_to(id);
-
- if (filter_node->filter_operator == AND) {
- and_filter_iterators();
- } else {
- or_filter_iterators();
- }
-
- return;
- }
-
+ // No need to traverse iterator tree if there's only one filter or compute_result() has been called.
if (is_filter_result_initialized) {
ArrayUtils::skip_index_to_id(result_index, filter_result.docs, filter_result.count, id);
@@ -1055,6 +1043,20 @@ void filter_result_iterator_t::skip_to(uint32_t id) {
return;
}
+ if (filter_node->isOperator) {
+ // Skip the subtrees to id and then apply operators to arrive at the next valid doc.
+ left_it->skip_to(id);
+ right_it->skip_to(id);
+
+ if (filter_node->filter_operator == AND) {
+ and_filter_iterators();
+ } else {
+ or_filter_iterators();
+ }
+
+ return;
+ }
+
const filter a_filter = filter_node->filter_exp;
if (!index->field_is_indexed(a_filter.field_name)) {
@@ -1137,6 +1139,12 @@ int filter_result_iterator_t::valid(uint32_t id) {
return -1;
}
+ // No need to traverse iterator tree if there's only one filter or compute_result() has been called.
+ if (is_filter_result_initialized) {
+ skip_to(id);
+ return is_valid ? (seq_id == id ? 1 : 0) : -1;
+ }
+
if (filter_node->isOperator) {
auto left_valid = left_it->valid(id), right_valid = right_it->valid(id);
@@ -1250,21 +1258,7 @@ void filter_result_iterator_t::reset() {
return;
}
- if (filter_node->isOperator) {
- // Reset the subtrees then apply operators to arrive at the first valid doc.
- left_it->reset();
- right_it->reset();
- is_valid = true;
-
- if (filter_node->filter_operator == AND) {
- and_filter_iterators();
- } else {
- or_filter_iterators();
- }
-
- return;
- }
-
+ // No need to traverse iterator tree if there's only one filter or compute_result() has been called.
if (is_filter_result_initialized) {
if (filter_result.count == 0) {
is_valid = false;
@@ -1283,6 +1277,21 @@ void filter_result_iterator_t::reset() {
return;
}
+ if (filter_node->isOperator) {
+ // Reset the subtrees then apply operators to arrive at the first valid doc.
+ left_it->reset();
+ right_it->reset();
+ is_valid = true;
+
+ if (filter_node->filter_operator == AND) {
+ and_filter_iterators();
+ } else {
+ or_filter_iterators();
+ }
+
+ return;
+ }
+
const filter a_filter = filter_node->filter_exp;
if (!index->field_is_indexed(a_filter.field_name)) {