diff --git a/include/collection.h b/include/collection.h index 38e91d1f..f0c16236 100644 --- a/include/collection.h +++ b/include/collection.h @@ -463,6 +463,8 @@ public: Option validate_reference_filter(const std::string& filter_query) const; + Option get_reference_field(const std::string & collection_name) const; + Option get_reference_filter_ids(const std::string & filter_query, const std::string & collection_name, std::pair& reference_index_ids) const; diff --git a/src/collection.cpp b/src/collection.cpp index c3ef5a49..6c61a24c 100644 --- a/src/collection.cpp +++ b/src/collection.cpp @@ -2580,9 +2580,7 @@ Option Collection::validate_reference_filter(const std::string& filter_que return Option(true); } -Option Collection::get_reference_filter_ids(const std::string & filter_query, - const std::string & collection_name, - std::pair& reference_index_ids) const { +Option Collection::get_reference_field(const std::string & collection_name) const { std::shared_lock lock(mutex); std::string reference_field_name; @@ -2595,10 +2593,23 @@ Option Collection::get_reference_filter_ids(const std::string & filter_que } if (reference_field_name.empty()) { - return Option(400, "Could not find any field in `" + name + "` referencing the collection `" - + collection_name + "`."); + return Option(400, "Could not find any field in `" + name + "` referencing the collection `" + + collection_name + "`."); } + return Option(reference_field_name); +} + +Option Collection::get_reference_filter_ids(const std::string & filter_query, + const std::string & collection_name, + std::pair& reference_index_ids) const { + auto reference_field_op = get_reference_field(collection_name); + if (!reference_field_op.ok()) { + return Option(reference_field_op.code(), reference_field_op.error()); + } + + std::shared_lock lock(mutex); + const std::string doc_id_prefix = std::to_string(collection_id) + "_" + DOC_ID_PREFIX + "_"; filter_node_t* filter_tree_root = nullptr; Option filter_op = filter::parse_filter_query(filter_query, search_schema, @@ -2608,8 +2619,8 @@ Option Collection::get_reference_filter_ids(const std::string & filter_que } // Reference helper field has the sequence id of other collection's documents. - reference_field_name += REFERENCE_HELPER_FIELD_SUFFIX; - index->do_reference_filtering_with_lock(reference_index_ids, filter_tree_root, reference_field_name); + auto field_name = reference_field_op.get() + REFERENCE_HELPER_FIELD_SUFFIX; + index->do_reference_filtering_with_lock(reference_index_ids, filter_tree_root, field_name); delete filter_tree_root; return Option(true);