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

No special trailing character is required when int is serialized since all strings of equal length.

Kishore Nallanaf8e8ba4601a
diff --git a/src/art.cpp b/src/art.cpp
index 4a785b49..86735eaf 100644
--- a/src/art.cpp
+++ b/src/art.cpp
@@ -34,7 +34,7 @@ static void art_fuzzy_recurse(char p, char c, const art_node *n, int depth, cons
const int term_len, const int* irow, const int* jrow, const int min_cost,
const int max_cost, const bool prefix, std::vector<const art_node *> &results);
-void art_int_fuzzy_recurse(art_node *n, int depth, unsigned char* int_str, int int_str_len,
+void art_int_fuzzy_recurse(art_node *n, int depth, const unsigned char* int_str, int int_str_len,
int32_t compare, std::vector<const art_leaf *> &results);
bool compare_art_leaf_frequency(const art_leaf *a, const art_leaf *b) {
@@ -1373,15 +1373,10 @@ void encode_int32(int32_t n, unsigned char *chars) {
chars[2*i] = symbols[((bytes[i] >> 4) & 0x0F)];
chars[2*i+1] = symbols[(bytes[i] & 0x0F)];
}
-
- // Terminate the string with a "character" that does not ever appear in regular text since an inserted string
- // should not be a substring of another string in this ART implementation. We choose 46 (.) instead of '\0' which is
- // actually ZERO and is a valid character that can appear in the encoded string.
- chars[8] = 46;
}
// Implements ==, <= and >=
-recurse_progress matches(char a, char b, int compare) {
+recurse_progress matches(unsigned char a, unsigned char b, int compare) {
switch(compare) {
case -1:
if (a == b) return RECURSE;
@@ -1445,7 +1440,7 @@ static void art_iter(const art_node *n, std::vector<const art_leaf *> &results)
return ;
}
-static inline void art_int_fuzzy_children(const art_node *n, int depth, unsigned char* int_str, int int_str_len,
+static inline void art_int_fuzzy_children(const art_node *n, int depth, const unsigned char* int_str, int int_str_len,
int32_t compare, std::vector<const art_leaf *> &results) {
char child_char;
art_node* child;
@@ -1515,7 +1510,7 @@ static inline void art_int_fuzzy_children(const art_node *n, int depth, unsigned
}
}
-void art_int_fuzzy_recurse(art_node *n, int depth, unsigned char* int_str, int int_str_len,
+void art_int_fuzzy_recurse(art_node *n, int depth, const unsigned char* int_str, int int_str_len,
int32_t compare, std::vector<const art_leaf*> &results) {
if (!n) return ;
@@ -1561,8 +1556,8 @@ void art_int_fuzzy_recurse(art_node *n, int depth, unsigned char* int_str, int i
}
int art_int32_search(art_tree *t, int32_t value, int compare, std::vector<const art_leaf *> &results) {
- unsigned char chars[9];
+ unsigned char chars[8];
encode_int32(value, chars);
- art_int_fuzzy_recurse(t->root, 0, chars, 9, compare, results);
+ art_int_fuzzy_recurse(t->root, 0, chars, 8, compare, results);
return 0;
}
diff --git a/test/art_test.cpp b/test/art_test.cpp
index 79e09bf1..432e0a8f 100644
--- a/test/art_test.cpp
+++ b/test/art_test.cpp
@@ -686,40 +686,40 @@ TEST(ArtTest, test_art_fuzzy_search) {
}
TEST(ArtTest, test_encode_int) {
- unsigned char chars[9];
+ unsigned char chars[8];
// 175 => 0000,0000,0000,0000,0000,0000,1010,1111,\0
- unsigned char chars_175[9] = {0, 0, 0, 0, 0, 0, 10, 15, 46};
+ unsigned char chars_175[8] = {0, 0, 0, 0, 0, 0, 10, 15};
encode_int32(175, chars);
- for(uint32_t i = 0; i < 9; i++) {
+ for(uint32_t i = 0; i < 8; i++) {
ASSERT_EQ(chars_175[i], chars[i]);
}
// 0 => 0000,0000,0000,0000,0000,0000,0000,0000,\0
- unsigned char chars_0[9] = {0, 0, 0, 0, 0, 0, 0, 0, 46};
+ unsigned char chars_0[8] = {0, 0, 0, 0, 0, 0, 0, 0};
encode_int32(0, chars);
- for(uint32_t i = 0; i < 9; i++) {
+ for(uint32_t i = 0; i < 8; i++) {
ASSERT_EQ(chars_0[i], chars[i]);
}
// 255 => 0000,0000,0000,0000,0000,0000,1111,1111,\0
- unsigned char chars_255[9] = {0, 0, 0, 0, 0, 0, 15, 15, 46};
+ unsigned char chars_255[8] = {0, 0, 0, 0, 0, 0, 15, 15};
encode_int32(255, chars);
- for(uint32_t i = 0; i < 9; i++) {
+ for(uint32_t i = 0; i < 8; i++) {
ASSERT_EQ(chars_255[i], chars[i]);
}
// 4531 => 0000,0000,0000,0000,0001,0001,1011,0011,\0
- unsigned char chars_4531[9] = {0, 0, 0, 0, 1, 1, 11, 3, 46};
+ unsigned char chars_4531[8] = {0, 0, 0, 0, 1, 1, 11, 3};
encode_int32(4531, chars);
- for(uint32_t i = 0; i < 9; i++) {
+ for(uint32_t i = 0; i < 8; i++) {
ASSERT_EQ(chars_4531[i], chars[i]);
}
// 1200000 => 0000,0000,0001,0010,0100,1111,1000,0000,\0
- unsigned char chars_1M[9] = {0, 0, 1, 2, 4, 15, 8, 0, 46};
+ unsigned char chars_1M[8] = {0, 0, 1, 2, 4, 15, 8, 0};
encode_int32(1200000, chars);
- for(uint32_t i = 0; i < 9; i++) {
+ for(uint32_t i = 0; i < 8; i++) {
ASSERT_EQ(chars_1M[i], chars[i]);
}
}
@@ -729,7 +729,7 @@ TEST(ArtTest, test_int_range_hundreds) {
art_tree_init(&t);
art_document doc = get_document(1);
- const int CHAR_LEN = 9;
+ const int CHAR_LEN = 8;
unsigned char chars[CHAR_LEN];
for(uint32_t i = 100; i < 110; i++) {
@@ -764,7 +764,7 @@ TEST(ArtTest, test_int_negative_ints) {
art_tree_init(&t);
art_document doc = get_document(1);
- const int CHAR_LEN = 9;
+ const int CHAR_LEN = 8;
unsigned char chars[CHAR_LEN];
for(int32_t i = -100; i < 0; i++) {
@@ -800,7 +800,7 @@ TEST(ArtTest, test_int_range_millions) {
art_document doc = get_document(1);
- const int CHAR_LEN = 9;
+ const int CHAR_LEN = 8;
unsigned char chars[CHAR_LEN];
for(uint32_t i = 0; i < 1000000; i++) {
@@ -873,7 +873,7 @@ TEST(ArtTest, test_int_range_byte_boundary) {
art_document doc = get_document(1);
- const int CHAR_LEN = 9;
+ const int CHAR_LEN = 8;
unsigned char chars[CHAR_LEN];
for(uint32_t i = 200; i < 300; i++) {