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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Replace callbacks with a result vector.
Kishore Nallan8ff75e481dd9
diff --git a/include/art.h b/include/art.hindex 3f9186d4..02f1ef39 100644--- a/include/art.h+++ b/include/art.h@@ -3,6 +3,7 @@#include <stdint.h>#include <stdbool.h>+#include <vector>#include "forarray.h"#define IGNORE_PRINTF 1@@ -233,11 +234,7 @@ int art_iter_prefix(art_tree *t, const unsigned char *prefix, int prefix_len, ar* @arg data Opaque handle passed to the callback* @return 0 on success, or the return of the callback.*/-int art_iter_fuzzy_prefix(art_tree *t, const unsigned char *term, int term_len, int max_cost, art_callback cb,- void *data);--static int art_iter_fuzzy_prefix_recurse(art_node *n, const unsigned char *term, int term_len, int max_cost,- int depth, int* previous_row, art_callback cb, void *data);+int art_iter_fuzzy_prefix(art_tree *t, const unsigned char *term, int term_len, int max_cost, std::vector<art_leaf*> & results);#ifdef __cplusplus}diff --git a/src/art.cpp b/src/art.cppindex bc7a6a01..2db5061e 100644--- a/src/art.cpp+++ b/src/art.cpp@@ -801,7 +801,7 @@ void* art_delete(art_tree *t, const unsigned char *key, int key_len) {return NULL;}-static int topk_iter(art_node *root) {+static int topk_iter(art_node *root, std::vector<art_leaf*> & results) {printf("INSIDE topk_iter: root->type: %d\n", root->type);// FIXME: try to avoid branching@@ -824,7 +824,6 @@ static int topk_iter(art_node *root) {return lscore < rscore;};std::priority_queue<art_node *, std::vector<art_node *>, decltype(cmp)> q(cmp);- std::vector<art_leaf*> results;q.push(root);std::cout << "ROOT SCORE: " << root->score << std::endl;@@ -889,11 +888,6 @@ static int topk_iter(art_node *root) {printf("OUTSIDE topk_iter: results size: %d\n", results.size());- for(auto leaf: results) {- std::cout << ">>>>!Key: " << leaf->key;- std::cout << ", Value: " << leaf->values->ids.at(0) << std::endl;- }-return 0;}@@ -1060,7 +1054,7 @@ int art_iter_prefix(art_tree *t, const unsigned char *key, int key_len, art_callreturn 0;}-#define fuzzy_recurse(child_char, term, term_len, depth, previous_row) {\+#define fuzzy_recurse(child_char, term, term_len, depth, previous_row, results) {\int new_current_row[term_len+1];\new_current_row[0] = previous_row[0] + 1;\row_min = levenshtein_score(child_char, term, term_len, previous_row, new_current_row);\@@ -1072,11 +1066,11 @@ int art_iter_prefix(art_tree *t, const unsigned char *key, int key_len, art_call/* reached end of term, and cost is below threshold, print children of this node as matches*/\if(new_current_row[term_len] <= max_cost) {\printf("START RECURSIVE ITER\n");\- topk_iter(child);\+ topk_iter(child, results);\}\} else if(row_min <= max_cost) {\int new_depth = (child_char != 0) ? depth+1 : depth;\- art_iter_fuzzy_prefix_recurse(child, term, term_len, max_cost, new_depth, new_current_row, cb, data);\+ art_iter_fuzzy_prefix_recurse(child, term, term_len, max_cost, new_depth, new_current_row, results);\}\}\@@ -1116,7 +1110,7 @@ void copyIntArray(int *dest, int *src, int len) {}static int art_iter_fuzzy_prefix_recurse(art_node *n, const unsigned char *term, int term_len, int max_cost,- int depth, int* previous_row, art_callback cb, void *data) {+ int depth, int* previous_row, std::vector<art_leaf*> & results) {if (IS_LEAF(n)) {printf("IS_LEAF\n");@@ -1145,7 +1139,7 @@ static int art_iter_fuzzy_prefix_recurse(art_node *n, const unsigned char *term,}if(current_row[term_len] <= max_cost) {- topk_iter(n);+ topk_iter(n, results);}return 0;@@ -1177,7 +1171,7 @@ static int art_iter_fuzzy_prefix_recurse(art_node *n, const unsigned char *term,if(depth == term_len) {if(current_row[term_len] <= max_cost) {printf("PARTIAL START RECURSIVE ITER\n");- return topk_iter(n);+ return topk_iter(n, results);}return 0;@@ -1193,7 +1187,7 @@ static int art_iter_fuzzy_prefix_recurse(art_node *n, const unsigned char *term,char child_char = ((art_node4*)n)->keys[i];printf("4!child_char: %c, %d, depth: %d", child_char, child_char, depth);art_node* child = ((art_node4*)n)->children[i];- fuzzy_recurse(child_char, term, term_len, depth, previous_row);+ fuzzy_recurse(child_char, term, term_len, depth, previous_row, results);}break;@@ -1203,7 +1197,7 @@ static int art_iter_fuzzy_prefix_recurse(art_node *n, const unsigned char *term,char child_char = ((art_node16*)n)->keys[i];printf("16!child_char: %c, depth: %d", child_char, depth);art_node* child = ((art_node16*)n)->children[i];- fuzzy_recurse(child_char, term, term_len, depth, previous_row);+ fuzzy_recurse(child_char, term, term_len, depth, previous_row, results);}break;@@ -1215,7 +1209,7 @@ static int art_iter_fuzzy_prefix_recurse(art_node *n, const unsigned char *term,art_node* child = ((art_node48*)n)->children[ix - 1];char child_char = (char)i;printf("48!child_char: %c, depth: %d, ix: %d", child_char, depth, ix);- fuzzy_recurse(child_char, term, term_len, depth, previous_row);+ fuzzy_recurse(child_char, term, term_len, depth, previous_row, results);}break;@@ -1226,7 +1220,7 @@ static int art_iter_fuzzy_prefix_recurse(art_node *n, const unsigned char *term,char child_char = (char) i;printf("256!child_char: %c, depth: %d", child_char, depth);art_node* child = ((art_node256*)n)->children[i];- fuzzy_recurse(child_char, term, term_len, depth, previous_row);+ fuzzy_recurse(child_char, term, term_len, depth, previous_row, results);}break;@@ -1251,7 +1245,7 @@ static int art_iter_fuzzy_prefix_recurse(art_node *n, const unsigned char *term,* @return 0 on success, or the return of the callback.*/int art_iter_fuzzy_prefix(art_tree *t, const unsigned char *term, int term_len,- int max_cost, art_callback cb, void *data) {+ int max_cost, std::vector<art_leaf*> & results) {int previous_row[term_len + 1];@@ -1259,5 +1253,5 @@ int art_iter_fuzzy_prefix(art_tree *t, const unsigned char *term, int term_len,previous_row[i] = i;}- return art_iter_fuzzy_prefix_recurse(t->root, term, term_len, max_cost, 0, previous_row, cb, data);+ return art_iter_fuzzy_prefix_recurse(t->root, term, term_len, max_cost, 0, previous_row, results);}\ No newline at end of filediff --git a/src/main.cpp b/src/main.cppindex 7763b26d..a6bb52d3 100644--- a/src/main.cpp+++ b/src/main.cpp@@ -16,7 +16,11 @@ using namespace std;static int test_prefix_cb(void *data, const unsigned char *k, uint32_t k_len, void *val) {cout << "#>>>>Key: ";printf("%.*s", k_len, k);- cout << ", ID: " << ((art_values*)val)->ids.at(0) << endl;+ cout << "LENGTH OF IDS: " << ((art_values*)val)->ids.getLength() << endl;++ for(uint32_t i=0; i<((art_values*)val)->ids.getLength(); i++) {+ cout << ", ID: " << ((art_values*)val)->ids.at(i) << endl;+ }return 0;}@@ -84,11 +88,12 @@ int main() {num++;}- const unsigned char *prefix = (const unsigned char *) "propellants";+ const unsigned char *prefix = (const unsigned char *) "launch";size_t prefix_len = strlen((const char *) prefix);+ std::vector<art_leaf*> results;auto begin = std::chrono::high_resolution_clock::now();- art_iter_fuzzy_prefix(&t, prefix, prefix_len, 2, test_prefix_cb, NULL);+ art_iter_fuzzy_prefix(&t, prefix, prefix_len, 0, results);long long int timeMillis = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - begin).count();// art_iter_prefix(&t, prefix, strlen((const char *) prefix), test_prefix_cb, NULL);@@ -96,6 +101,14 @@ int main() {cout << "Time taken: " << timeMillis << "us" << endl;+ for(auto leaf: results) {+ std::cout << ">>>>/Key: " << leaf->key << std::endl;+ for(uint32_t i=0; i<leaf->values->ids.getLength(); i++) {+ std::cout << ", ID: " << leaf->values->ids.at(i) << std::endl;+ }+ //std::cout << ", Value: " << leaf->values->ids.at(0) << std::endl;+ }+art_tree_destroy(&t);return 0;}\ No newline at end of file