Move FindProcedure to module.hpp

Summary:
The function will also be used during AST construction in order to
support `CALL ... YIELD *` syntax.

Reviewers: mferencevic, ipaljak

Reviewed By: mferencevic

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D2586
This commit is contained in:
Teon Banek
2019-12-07 09:34:01 +01:00
parent 8da71873a7
commit d9abb7ccf1
3 changed files with 44 additions and 33 deletions

View File

@@ -6,6 +6,9 @@ extern "C" {
#include <optional>
#include "utils/pmr/vector.hpp"
#include "utils/string.hpp"
namespace query::procedure {
ModuleRegistry gModuleRegistry;
@@ -213,7 +216,7 @@ bool ModuleRegistry::LoadModuleLibrary(std::filesystem::path path) {
return true;
}
ModulePtr ModuleRegistry::GetModuleNamed(const std::string_view &name) {
ModulePtr ModuleRegistry::GetModuleNamed(const std::string_view &name) const {
std::shared_lock<utils::RWLock> guard(lock_);
auto found_it = modules_.find(name);
if (found_it == modules_.end()) return nullptr;
@@ -270,4 +273,23 @@ void ModuleRegistry::UnloadAllModules() {
modules_.clear();
}
std::optional<std::pair<procedure::ModulePtr, const mgp_proc *>> FindProcedure(
const ModuleRegistry &module_registry,
const std::string_view &fully_qualified_procedure_name,
utils::MemoryResource *memory) {
utils::pmr::vector<std::string_view> name_parts(memory);
utils::Split(&name_parts, fully_qualified_procedure_name, ".");
if (name_parts.size() == 1U) return std::nullopt;
auto last_dot_pos = fully_qualified_procedure_name.find_last_of('.');
CHECK(last_dot_pos != std::string_view::npos);
const auto &module_name =
fully_qualified_procedure_name.substr(0, last_dot_pos);
const auto &proc_name = name_parts.back();
auto module = module_registry.GetModuleNamed(module_name);
if (!module) return std::nullopt;
const auto &proc_it = module->procedures.find(proc_name);
if (proc_it == module->procedures.end()) return std::nullopt;
return std::make_pair(std::move(module), &proc_it->second);
}
} // namespace query::procedure