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

Fix `referenced_in` not being initialized if referenced collection is created later on.

Harpreet Sangarf80184a25c05
diff --git a/include/collection.h b/include/collection.h
index f694a0fb..bcec8ba7 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -44,6 +44,10 @@ struct reference_pair {
std::string field;
reference_pair(std::string collection, std::string field) : collection(std::move(collection)), field(std::move(field)) {}
+
+ bool operator < (const reference_pair& pair) const {
+ return collection < pair.collection;
+ }
};
class Collection {
@@ -592,6 +596,8 @@ public:
void add_referenced_in(const reference_pair& pair);
+ void add_referenced_ins(const std::set<reference_pair>& pairs);
+
void add_referenced_in(const std::string& collection_name, const std::string& field_name);
Option<std::string> get_reference_field(const std::string& collection_name) const;
diff --git a/include/collection_manager.h b/include/collection_manager.h
index 99de5a8c..828421fc 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -80,6 +80,9 @@ private:
BatchedIndexer* batch_indexer;
+ // All the references to a particular collection are stored until it is created.
+ std::map<std::string, std::set<reference_pair>> referenced_in_backlog;
+
CollectionManager();
~CollectionManager() = default;
@@ -204,4 +207,8 @@ public:
static void _get_reference_collection_names(const std::string& filter_query,
std::set<std::string>& reference_collection_names);
+
+ void add_referenced_in_backlog(const std::string& collection_name, reference_pair&& pair);
+
+ std::map<std::string, std::set<reference_pair>> _get_referenced_in_backlog() const;
};
diff --git a/src/collection.cpp b/src/collection.cpp
index 472d72bb..bf9a2577 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -5068,6 +5068,10 @@ Index* Collection::init_index() {
if (ref_coll != nullptr) {
// Passing reference helper field helps perform operation on doc_id instead of field value.
ref_coll->add_referenced_in(name, field.name + REFERENCE_HELPER_FIELD_SUFFIX);
+ } else {
+ // Reference collection has not been created yet.
+ collectionManager.add_referenced_in_backlog(ref_coll_name,
+ reference_pair{name, field.name + REFERENCE_HELPER_FIELD_SUFFIX});
}
}
}
@@ -5444,7 +5448,14 @@ bool Collection::is_referenced_in(const std::string& collection_name) const {
}
void Collection::add_referenced_in(const reference_pair& pair) {
- add_referenced_in(pair.collection, pair.field);
+ return add_referenced_in(pair.collection, pair.field);
+}
+
+void Collection::add_referenced_ins(const std::set<reference_pair>& pairs) {
+ std::shared_lock lock(mutex);
+ for (const auto &pair: pairs) {
+ referenced_in.emplace(pair.collection, pair.field);
+ }
}
void Collection::add_referenced_in(const std::string& collection_name, const std::string& field_name) {
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index ddc52c35..7b4c97f6 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -220,7 +220,7 @@ Option<bool> CollectionManager::load(const size_t collection_batch_size, const s
size_t num_processed = 0;
// Collection name -> Referenced in
- std::map<std::string, std::vector<reference_pair>> referenced_ins = {};
+ std::map<std::string, std::set<reference_pair>> referenced_ins = {};
std::mutex m_process;
std::condition_variable cv_process;
@@ -263,7 +263,7 @@ Option<bool> CollectionManager::load(const size_t collection_batch_size, const s
referenced_ins[ref_coll_name] = {};
}
auto const field_name = item.first + Collection::REFERENCE_HELPER_FIELD_SUFFIX;
- referenced_ins.at(ref_coll_name).emplace_back(reference_pair{collection_name, field_name});
+ referenced_ins.at(ref_coll_name).insert(reference_pair{collection_name, field_name});
}
}
cv_process.notify_one();
@@ -470,6 +470,11 @@ Option<Collection*> CollectionManager::create_collection(const std::string& name
add_to_collections(new_collection);
+ if (referenced_in_backlog.count(name) > 0) {
+ new_collection->add_referenced_ins(referenced_in_backlog.at(name));
+ referenced_in_backlog.erase(name);
+ }
+
return Option<Collection*>(new_collection);
}
@@ -1769,3 +1774,13 @@ Option<Collection*> CollectionManager::clone_collection(const string& existing_n
return Option<Collection*>(new_coll);
}
+
+void CollectionManager::add_referenced_in_backlog(const std::string& collection_name, reference_pair&& pair) {
+ std::shared_lock lock(mutex);
+ referenced_in_backlog[collection_name].insert(pair);
+}
+
+std::map<std::string, std::set<reference_pair>> CollectionManager::_get_referenced_in_backlog() const {
+ std::shared_lock lock(mutex);
+ return referenced_in_backlog;
+}
\ No newline at end of file
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index af3263fa..e0f382fb 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -1335,7 +1335,7 @@ TEST_F(CollectionManagerTest, CloneCollection) {
ASSERT_EQ('?', coll2->get_token_separators().at(1));
}
-TEST(StringUtilsTest, GetReferenceCollectionNames) {
+TEST_F(CollectionManagerTest, GetReferenceCollectionNames) {
std::string filter_query = "";
std::set<std::string> reference_collection_names;
CollectionManager::_get_reference_collection_names(filter_query, reference_collection_names);
@@ -1364,3 +1364,36 @@ TEST(StringUtilsTest, GetReferenceCollectionNames) {
}
reference_collection_names.clear();
}
+
+TEST_F(CollectionManagerTest, ReferencedInBacklog) {
+ auto referenced_ins_backlog = collectionManager._get_referenced_in_backlog();
+ ASSERT_EQ(1, referenced_ins_backlog.count("Products"));
+
+ auto const& references = referenced_ins_backlog.at("Products");
+ ASSERT_EQ(1, references.size());
+ ASSERT_EQ("collection1", references.cbegin()->collection);
+ ASSERT_EQ("product_id_sequence_id", references.cbegin()->field);
+
+ auto schema_json =
+ R"({
+ "name": "Products",
+ "fields": [
+ {"name": "product_id", "type": "string"},
+ {"name": "name", "type": "string"}
+ ]
+ })"_json;
+
+ auto create_op = collectionManager.create_collection(schema_json);
+ ASSERT_TRUE(create_op.ok());
+
+ referenced_ins_backlog = collectionManager._get_referenced_in_backlog();
+ ASSERT_EQ(0, referenced_ins_backlog.count("Products"));
+
+ auto get_reference_field_op = create_op.get()->get_reference_field("collection1");
+ ASSERT_TRUE(get_reference_field_op.ok());
+ ASSERT_EQ("product_id_sequence_id", get_reference_field_op.get());
+
+ get_reference_field_op = create_op.get()->get_reference_field("foo");
+ ASSERT_FALSE(get_reference_field_op.ok());
+ ASSERT_EQ("Could not find any field in `Products` referencing the collection `foo`.", get_reference_field_op.error());
+}
\ No newline at end of file