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 fhash_int64 map and adding test

krunald1aef324a9d3
diff --git a/include/facet_index.h b/include/facet_index.h
index 651b15a9..20bdab66 100644
--- a/include/facet_index.h
+++ b/include/facet_index.h
@@ -81,6 +81,7 @@ private:
std::map<std::string, facet_id_seq_ids_t> fvalue_seq_ids;
std::list<facet_count_t> counts;
posting_list_t* seq_id_hashes = nullptr;
+ spp::sparse_hash_map<uint32_t, int64_t> fhash_to_int64_map;
bool has_value_index = true;
bool has_hash_index = true;
@@ -109,7 +110,6 @@ private:
// field -> facet_index
std::unordered_map<std::string, facet_doc_ids_list_t> facet_field_map;
- spp::sparse_hash_map<uint32_t, int64_t> fhash_to_int64_map;
// auto incrementing ID that is assigned to each unique facet value string
uint32_t next_facet_id = 0;
@@ -153,5 +153,9 @@ public:
posting_list_t* get_facet_hash_index(const std::string& field_name);
//get int64 val from index for stats
- int64_t get_facet_val(uint32_t index);
+ int64_t get_facet_val(const std::string& field_name, uint32_t index);
+
+#ifdef TEST_BUILD
+ size_t get_fhash_int64_map_count(const std::string& field_name);
+#endif
};
\ No newline at end of file
diff --git a/src/facet_index.cpp b/src/facet_index.cpp
index 37508c6d..c28d7326 100644
--- a/src/facet_index.cpp
+++ b/src/facet_index.cpp
@@ -40,7 +40,7 @@ void facet_index_t::insert(const std::string& field_name,std::unordered_map<face
if(!is_string_field) {
int64_t val = std::stoll(fvalue.facet_value);
- fhash_to_int64_map[facet_id] = val;
+ facet_index.fhash_to_int64_map[facet_id] = val;
}
}
@@ -117,8 +117,6 @@ void facet_index_t::remove(const std::string& field_name, const uint32_t seq_id)
if(ids && ids_t::contains(ids, seq_id)) {
ids_t::erase(ids, seq_id);
auto& count_list = facet_field_it->second.counts;
- auto key = facet_ids_seq_ids->first;
- auto& facet_id_seq_ids = facet_ids_seq_ids->second;
facet_ids_seq_ids->second.facet_count_it->count--;
if(ids_t::num_ids(ids) == 0) {
@@ -129,6 +127,14 @@ void facet_index_t::remove(const std::string& field_name, const uint32_t seq_id)
auto node = facet_index_map.extract(facet_ids_seq_ids->first);
node.key() = dead_fvalue;
facet_index_map.insert(std::move(node));
+
+ //remove from int64 lookup map
+ auto& fhash_int64_map = facet_field_it->second.fhash_to_int64_map;
+ uint32_t fhash = facet_ids_seq_ids->second.facet_id;
+
+ if(fhash_int64_map.find(fhash) != fhash_int64_map.end()) {
+ fhash_int64_map.erase(fhash);
+ }
}
}
}
@@ -340,12 +346,30 @@ posting_list_t* facet_index_t::get_facet_hash_index(const std::string &field_nam
return nullptr;
}
-int64_t facet_index_t::get_facet_val(uint32_t index) {
- auto it = fhash_to_int64_map.find(index);
+int64_t facet_index_t::get_facet_val(const std::string& field_name, uint32_t index) {
+ const auto facet_field_map_it = facet_field_map.find(field_name);
+ if(facet_field_map_it == facet_field_map.end()) {
+ return INT64_MAX; // field is not initialized or dropped
+ }
+
+ auto& facet_index = facet_field_map_it->second;
+
+ auto it = facet_index.fhash_to_int64_map.find(index);
- if(it != fhash_to_int64_map.end()) {
+ if(it != facet_index.fhash_to_int64_map.end()) {
return it->second;
}
return INT64_MAX;
-}
\ No newline at end of file
+}
+
+#ifdef TEST_BUILD
+size_t facet_index_t::get_fhash_int64_map_count(const std::string& field_name) {
+ const auto facet_field_map_it = facet_field_map.find(field_name);
+ if(facet_field_map_it == facet_field_map.end()) {
+ return 0; // field is not initialized or dropped
+ }
+
+ return facet_field_map_it->second.fhash_to_int64_map.size();
+}
+#endif
\ No newline at end of file
diff --git a/src/index.cpp b/src/index.cpp
index 416b816c..3903d357 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1386,7 +1386,7 @@ void Index::do_facets(std::vector<facet> & facets, facet_query_t & facet_query,
if(should_compute_stats) {
int64_t val = fhash;
if(facet_field.is_int64()) {
- val = facet_index_v4->get_facet_val(fhash);
+ val = facet_index_v4->get_facet_val(a_facet.field_name,fhash);
}
compute_facet_stats(a_facet, val, facet_field.type);
diff --git a/test/collection_faceting_test.cpp b/test/collection_faceting_test.cpp
index 43cccb66..75d07e53 100644
--- a/test/collection_faceting_test.cpp
+++ b/test/collection_faceting_test.cpp
@@ -2403,4 +2403,39 @@ TEST_F(CollectionFacetingTest, FacetSortValidation) {
ASSERT_EQ("Fusion Plus", results["facet_counts"][0]["counts"][0]["value"]);
ASSERT_EQ("Oneplus 11R", results["facet_counts"][0]["counts"][1]["value"]);
ASSERT_EQ("S22 Ultra", results["facet_counts"][0]["counts"][2]["value"]);
+}
+
+TEST_F(CollectionFacetingTest, FhashInt64MapTest) {
+ std::vector<int64_t> visitors = {227489798,124098972,180247624};
+ facet_index_t facet_index_v4;
+ std::unordered_map<facet_value_id_t, std::vector<uint32_t>, facet_value_id_t::Hash> fvalue_to_seq_ids;
+ std::unordered_map<uint32_t, std::vector<facet_value_id_t>> seq_id_to_fvalues;
+
+ facet_index_v4.initialize("visitors");
+
+ //insert timestamps
+ int seq_id = 0;
+ for(auto it = visitors.begin(); it != visitors.end(); ++it) {
+ auto val = std::to_string(*it);
+ facet_value_id_t facet_value_id(val);
+ fvalue_to_seq_ids[facet_value_id].push_back(seq_id);
+ seq_id_to_fvalues[seq_id].push_back(facet_value_id);
+ ++seq_id;
+ }
+
+ facet_index_v4.insert("visitors", fvalue_to_seq_ids, seq_id_to_fvalues);
+ ASSERT_EQ(3, facet_index_v4.get_fhash_int64_map_count("visitors"));
+
+ facet_index_v4.remove("visitors", 0);
+ ASSERT_EQ(2, facet_index_v4.get_fhash_int64_map_count("visitors"));
+
+ fvalue_to_seq_ids.clear();
+ seq_id_to_fvalues.clear();
+
+ facet_value_id_t facet_value_id("124798721");
+ fvalue_to_seq_ids[facet_value_id].push_back(seq_id);
+ seq_id_to_fvalues[seq_id].push_back(facet_value_id);
+
+ facet_index_v4.insert("visitors", fvalue_to_seq_ids, seq_id_to_fvalues);
+ ASSERT_EQ(3, facet_index_v4.get_fhash_int64_map_count("visitors"));
}
\ No newline at end of file