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
Basic test for topster.
Kishore Nallan473aa6d5f683
diff --git a/CMakeLists.txt b/CMakeLists.txtindex d1e6e243..88d2fbb4 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -34,7 +34,8 @@ link_directories(${CMAKE_SOURCE_DIR}/external/${H2O_NAME}/build)link_directories(${CMAKE_SOURCE_DIR}/external/${H2O_NAME}/build/libressl-build/lib)link_directories(${CMAKE_SOURCE_DIR}/external/${ROCKSDB_NAME})-add_executable(typesense_test test/forarray_test.cpp test/art_test.cpp test/collection_test.cpp ${SRC_FILES})+add_executable(typesense_test test/forarray_test.cpp test/art_test.cpp test/collection_test.cpp test/topster_test.cpp+ ${SRC_FILES})add_executable(search ${HEADER_FILES} ${SRC_FILES} src/main/main.cpp)add_executable(typesense-server ${HEADER_FILES} ${SRC_FILES} src/main/server.cpp)diff --git a/include/topster.h b/include/topster.hindex 8257f390..54e19c84 100644--- a/include/topster.h+++ b/include/topster.h@@ -31,8 +31,7 @@ struct Topster {void add(const uint64_t &key, const uint64_t &match_score, const int64_t &primary_attr, const int64_t &secondary_attr){if (size >= MAX_SIZE) {- if(match_score <= data[0].match_score && primary_attr <= data[0].primary_attr &&- secondary_attr <= data[0].secondary_attr) {+ if(!is_greater(data[0], match_score, primary_attr, secondary_attr)) {// when incoming value is less than the smallest in the heap, ignorereturn;}@@ -46,14 +45,11 @@ struct Topster {// sift to maintain heap propertywhile ((2*i+1) < MAX_SIZE) {uint32_t next = (uint32_t) (2 * i + 1);- if (next+1 < MAX_SIZE && data[next].match_score > data[next+1].match_score &&- data[next].primary_attr > data[next+1].primary_attr &&- data[next].secondary_attr > data[next+1].secondary_attr) {+ if (next+1 < MAX_SIZE && is_greater_kv(data[next], data[next+1])) {next++;}- if (data[i].match_score > data[next].match_score && data[i].primary_attr > data[next].primary_attr &&- data[i].secondary_attr > data[next].secondary_attr) {+ if (is_greater_kv(data[i], data[next])) {swapMe(data[i], data[next]);} else {break;@@ -70,8 +66,7 @@ struct Topster {for (uint32_t i = size - 1; i > 0;) {uint32_t parent = (i-1)/2;- if (data[parent].match_score > data[i].match_score && data[parent].primary_attr > data[i].primary_attr &&- data[parent].secondary_attr > data[i].secondary_attr) {+ if (is_greater_kv(data[parent], data[i])) {swapMe(data[i], data[parent]);i = parent;} else {@@ -81,7 +76,13 @@ struct Topster {}}- static bool compare_values(const struct KV& i, const struct KV& j) {+ static bool is_greater(const struct KV& i, uint64_t match_score, int64_t primary_attr, int64_t secondary_attr) {+ if(i.match_score != match_score) return match_score > i.match_score;+ if(i.primary_attr != primary_attr) return primary_attr > i.primary_attr;+ return secondary_attr > i.secondary_attr;+ }++ static bool is_greater_kv(const struct KV &i, const struct KV &j) {if(i.match_score != j.match_score) return i.match_score > j.match_score;if(i.primary_attr != j.primary_attr) return i.primary_attr > j.primary_attr;if(i.secondary_attr != j.secondary_attr) return i.secondary_attr > j.secondary_attr;@@ -89,7 +90,7 @@ struct Topster {}void sort() {- std::stable_sort(std::begin(data), std::begin(data)+size, compare_values);+ std::stable_sort(std::begin(data), std::begin(data) + size, is_greater_kv);}void clear(){diff --git a/test/topster_test.cpp b/test/topster_test.cppnew file mode 100644index 00000000..cddd3174--- /dev/null+++ b/test/topster_test.cpp@@ -0,0 +1,36 @@+#include <gtest/gtest.h>+#include "topster.h"++TEST(TopsterTest, StoreMaxValuesWithoutRepetition) {+ Topster<5> topster;++ struct {+ uint64_t key;+ uint64_t match_score;+ int64_t primary_attr;+ int64_t secondary_attr;+ } data[10] = {+ {1, 10, 20, 30},+ {2, 4, 20, 30},+ {3, 7, 20, 30},+ {4, 11, 20, 30},+ {5, 9, 20, 30},+ {6, 6, 20, 30},+ {7, 6, 22, 30},+ {8, 9, 20, 30},+ {9, 8, 20, 30},+ {10, 5, 20, 30},+ };++ for(int i = 0; i < 10; i++) {+ topster.add(data[i].key, data[i].match_score, data[i].primary_attr, data[i].secondary_attr);+ }++ topster.sort();++ std::vector<uint64_t> ids = {4, 1, 8, 5, 9};++ for(int i = 0; i < topster.size; i++) {+ EXPECT_EQ(ids[i], topster.getKeyAt(i));+ }+}\ No newline at end of file