diff --git a/src/audit/log.cpp b/src/audit/log.cpp index b5ce9ff68..2102f2862 100644 --- a/src/audit/log.cpp +++ b/src/audit/log.cpp @@ -38,8 +38,7 @@ inline nlohmann::json PropertyValueToJson(const storage::PropertyValue &pv) { case storage::PropertyValue::Type::Map: { ret = nlohmann::json::object(); for (const auto &item : pv.ValueMap()) { - ret.push_back(nlohmann::json::object_t::value_type( - item.first, PropertyValueToJson(item.second))); + ret.push_back(nlohmann::json::object_t::value_type(item.first, PropertyValueToJson(item.second))); } break; } @@ -47,8 +46,7 @@ inline nlohmann::json PropertyValueToJson(const storage::PropertyValue &pv) { return ret; } -Log::Log(const std::filesystem::path &storage_directory, int32_t buffer_size, - int32_t buffer_flush_interval_millis) +Log::Log(const std::filesystem::path &storage_directory, int32_t buffer_size, int32_t buffer_flush_interval_millis) : storage_directory_(storage_directory), buffer_size_(buffer_size), buffer_flush_interval_millis_(buffer_flush_interval_millis), @@ -63,9 +61,7 @@ void Log::Start() { started_ = true; ReopenLog(); - scheduler_.Run("Audit", - std::chrono::milliseconds(buffer_flush_interval_millis_), - [&] { Flush(); }); + scheduler_.Run("Audit", std::chrono::milliseconds(buffer_flush_interval_millis_), [&] { Flush(); }); } Log::~Log() { @@ -78,13 +74,12 @@ Log::~Log() { Flush(); } -void Log::Record(const std::string &address, const std::string &username, - const std::string &query, +void Log::Record(const std::string &address, const std::string &username, const std::string &query, const storage::PropertyValue ¶ms) { if (!started_.load(std::memory_order_relaxed)) return; - auto timestamp = std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()) - .count(); + auto timestamp = + std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()) + .count(); buffer_->emplace(Item{timestamp, address, username, query, params}); } @@ -92,8 +87,7 @@ void Log::ReopenLog() { if (!started_.load(std::memory_order_relaxed)) return; std::lock_guard guard(lock_); if (log_.IsOpen()) log_.Close(); - log_.Open(storage_directory_ / "audit.log", - utils::OutputFile::Mode::APPEND_TO_EXISTING); + log_.Open(storage_directory_ / "audit.log", utils::OutputFile::Mode::APPEND_TO_EXISTING); } void Log::Flush() { @@ -101,11 +95,9 @@ void Log::Flush() { for (uint64_t i = 0; i < buffer_size_; ++i) { auto item = buffer_->pop(); if (!item) break; - log_.Write( - fmt::format("{}.{:06d},{},{},{},{}\n", item->timestamp / 1000000, - item->timestamp % 1000000, item->address, item->username, - utils::Escape(item->query), - utils::Escape(PropertyValueToJson(item->params).dump()))); + log_.Write(fmt::format("{}.{:06d},{},{},{},{}\n", item->timestamp / 1000000, item->timestamp % 1000000, + item->address, item->username, utils::Escape(item->query), + utils::Escape(PropertyValueToJson(item->params).dump()))); } log_.Sync(); } diff --git a/src/audit/log.hpp b/src/audit/log.hpp index 6e3e88721..5640ef22e 100644 --- a/src/audit/log.hpp +++ b/src/audit/log.hpp @@ -27,8 +27,7 @@ class Log { }; public: - Log(const std::filesystem::path &storage_directory, int32_t buffer_size, - int32_t buffer_flush_interval_millis); + Log(const std::filesystem::path &storage_directory, int32_t buffer_size, int32_t buffer_flush_interval_millis); ~Log(); @@ -43,8 +42,8 @@ class Log { void Start(); /// Adds an entry to the audit log. Thread-safe. - void Record(const std::string &address, const std::string &username, - const std::string &query, const storage::PropertyValue ¶ms); + void Record(const std::string &address, const std::string &username, const std::string &query, + const storage::PropertyValue ¶ms); /// Reopens the log file. Used for log file rotation. Thread-safe. void ReopenLog(); diff --git a/src/auth/auth.cpp b/src/auth/auth.cpp index e12d743ae..759d5abba 100644 --- a/src/auth/auth.cpp +++ b/src/auth/auth.cpp @@ -12,26 +12,20 @@ #include "utils/logging.hpp" #include "utils/string.hpp" -DEFINE_VALIDATED_string( - auth_module_executable, "", - "Absolute path to the auth module executable that should be used.", { - if (value.empty()) return true; - // Check the file status, following symlinks. - auto status = std::filesystem::status(value); - if (!std::filesystem::is_regular_file(status)) { - std::cerr << "The auth module path doesn't exist or isn't a file!" - << std::endl; - return false; - } - return true; - }); -DEFINE_bool(auth_module_create_missing_user, true, - "Set to false to disable creation of missing users."); -DEFINE_bool(auth_module_create_missing_role, true, - "Set to false to disable creation of missing roles."); -DEFINE_bool( - auth_module_manage_roles, true, - "Set to false to disable management of roles through the auth module."); +DEFINE_VALIDATED_string(auth_module_executable, "", "Absolute path to the auth module executable that should be used.", + { + if (value.empty()) return true; + // Check the file status, following symlinks. + auto status = std::filesystem::status(value); + if (!std::filesystem::is_regular_file(status)) { + std::cerr << "The auth module path doesn't exist or isn't a file!" << std::endl; + return false; + } + return true; + }); +DEFINE_bool(auth_module_create_missing_user, true, "Set to false to disable creation of missing users."); +DEFINE_bool(auth_module_create_missing_role, true, "Set to false to disable creation of missing roles."); +DEFINE_bool(auth_module_manage_roles, true, "Set to false to disable management of roles through the auth module."); DEFINE_VALIDATED_int32(auth_module_timeout_ms, 10000, "Timeout (in milliseconds) used when waiting for a " "response from the auth module.", @@ -60,11 +54,9 @@ const std::string kLinkPrefix = "link:"; * key="link:", value="" */ -Auth::Auth(const std::string &storage_directory) - : storage_(storage_directory), module_(FLAGS_auth_module_executable) {} +Auth::Auth(const std::string &storage_directory) : storage_(storage_directory), module_(FLAGS_auth_module_executable) {} -std::optional Auth::Authenticate(const std::string &username, - const std::string &password) { +std::optional Auth::Authenticate(const std::string &username, const std::string &password) { if (module_.IsUsed()) { nlohmann::json params = nlohmann::json::object(); params["username"] = username; @@ -73,8 +65,7 @@ std::optional Auth::Authenticate(const std::string &username, auto ret = module_.Call(params, FLAGS_auth_module_timeout_ms); // Verify response integrity. - if (!ret.is_object() || ret.find("authenticated") == ret.end() || - ret.find("role") == ret.end()) { + if (!ret.is_object() || ret.find("authenticated") == ret.end() || ret.find("role") == ret.end()) { return std::nullopt; } const auto &ret_authenticated = ret.at("authenticated"); @@ -142,9 +133,7 @@ std::optional Auth::Authenticate(const std::string &username, } else { auto user = GetUser(username); if (!user) { - spdlog::warn( - "Couldn't authenticate user '{}' because the user doesn't exist", - username); + spdlog::warn("Couldn't authenticate user '{}' because the user doesn't exist", username); return std::nullopt; } if (!user->CheckPassword(password)) { @@ -182,21 +171,18 @@ std::optional Auth::GetUser(const std::string &username_orig) { void Auth::SaveUser(const User &user) { bool success = false; if (user.role()) { - success = storage_.PutMultiple( - {{kUserPrefix + user.username(), user.Serialize().dump()}, - {kLinkPrefix + user.username(), user.role()->rolename()}}); + success = storage_.PutMultiple({{kUserPrefix + user.username(), user.Serialize().dump()}, + {kLinkPrefix + user.username(), user.role()->rolename()}}); } else { - success = storage_.PutAndDeleteMultiple( - {{kUserPrefix + user.username(), user.Serialize().dump()}}, - {kLinkPrefix + user.username()}); + success = storage_.PutAndDeleteMultiple({{kUserPrefix + user.username(), user.Serialize().dump()}}, + {kLinkPrefix + user.username()}); } if (!success) { throw AuthException("Couldn't save user '{}'!", user.username()); } } -std::optional Auth::AddUser(const std::string &username, - const std::optional &password) { +std::optional Auth::AddUser(const std::string &username, const std::optional &password) { auto existing_user = GetUser(username); if (existing_user) return std::nullopt; auto existing_role = GetRole(username); @@ -210,8 +196,7 @@ std::optional Auth::AddUser(const std::string &username, bool Auth::RemoveUser(const std::string &username_orig) { auto username = utils::ToLowerCase(username_orig); if (!storage_.Get(kUserPrefix + username)) return false; - std::vector keys( - {kLinkPrefix + username, kUserPrefix + username}); + std::vector keys({kLinkPrefix + username, kUserPrefix + username}); if (!storage_.DeleteMultiple(keys)) { throw AuthException("Couldn't remove user '{}'!", username); } @@ -220,8 +205,7 @@ bool Auth::RemoveUser(const std::string &username_orig) { std::vector Auth::AllUsers() { std::vector ret; - for (auto it = storage_.begin(kUserPrefix); it != storage_.end(kUserPrefix); - ++it) { + for (auto it = storage_.begin(kUserPrefix); it != storage_.end(kUserPrefix); ++it) { auto username = it->first.substr(kUserPrefix.size()); if (username != utils::ToLowerCase(username)) continue; auto user = GetUser(username); @@ -232,9 +216,7 @@ std::vector Auth::AllUsers() { return ret; } -bool Auth::HasUsers() { - return storage_.begin(kUserPrefix) != storage_.end(kUserPrefix); -} +bool Auth::HasUsers() { return storage_.begin(kUserPrefix) != storage_.end(kUserPrefix); } std::optional Auth::GetRole(const std::string &rolename_orig) { auto rolename = utils::ToLowerCase(rolename_orig); @@ -271,8 +253,7 @@ bool Auth::RemoveRole(const std::string &rolename_orig) { auto rolename = utils::ToLowerCase(rolename_orig); if (!storage_.Get(kRolePrefix + rolename)) return false; std::vector keys; - for (auto it = storage_.begin(kLinkPrefix); it != storage_.end(kLinkPrefix); - ++it) { + for (auto it = storage_.begin(kLinkPrefix); it != storage_.end(kLinkPrefix); ++it) { if (utils::ToLowerCase(it->second) == rolename) { keys.push_back(it->first); } @@ -286,8 +267,7 @@ bool Auth::RemoveRole(const std::string &rolename_orig) { std::vector Auth::AllRoles() { std::vector ret; - for (auto it = storage_.begin(kRolePrefix); it != storage_.end(kRolePrefix); - ++it) { + for (auto it = storage_.begin(kRolePrefix); it != storage_.end(kRolePrefix); ++it) { auto rolename = it->first.substr(kRolePrefix.size()); if (rolename != utils::ToLowerCase(rolename)) continue; auto role = GetRole(rolename); @@ -300,12 +280,10 @@ std::vector Auth::AllRoles() { return ret; } -std::vector Auth::AllUsersForRole( - const std::string &rolename_orig) { +std::vector Auth::AllUsersForRole(const std::string &rolename_orig) { auto rolename = utils::ToLowerCase(rolename_orig); std::vector ret; - for (auto it = storage_.begin(kLinkPrefix); it != storage_.end(kLinkPrefix); - ++it) { + for (auto it = storage_.begin(kLinkPrefix); it != storage_.end(kLinkPrefix); ++it) { auto username = it->first.substr(kLinkPrefix.size()); if (username != utils::ToLowerCase(username)) continue; if (it->second != utils::ToLowerCase(it->second)) continue; diff --git a/src/auth/auth.hpp b/src/auth/auth.hpp index 1c56851db..9d87dfab7 100644 --- a/src/auth/auth.hpp +++ b/src/auth/auth.hpp @@ -32,8 +32,7 @@ class Auth final { * @return a user when the username and password match, nullopt otherwise * @throw AuthException if unable to authenticate for whatever reason. */ - std::optional Authenticate(const std::string &username, - const std::string &password); + std::optional Authenticate(const std::string &username, const std::string &password); /** * Gets a user from the storage. @@ -63,9 +62,7 @@ class Auth final { * @return a user when the user is created, nullopt if the user exists * @throw AuthException if unable to save the user. */ - std::optional AddUser( - const std::string &username, - const std::optional &password = std::nullopt); + std::optional AddUser(const std::string &username, const std::optional &password = std::nullopt); /** * Removes a user from the storage. diff --git a/src/auth/models.cpp b/src/auth/models.cpp index 92e19f841..2f5bb210f 100644 --- a/src/auth/models.cpp +++ b/src/auth/models.cpp @@ -9,8 +9,7 @@ #include "utils/cast.hpp" #include "utils/string.hpp" -DEFINE_bool(auth_password_permit_null, true, - "Set to false to disable null passwords."); +DEFINE_bool(auth_password_permit_null, true, "Set to false to disable null passwords."); DEFINE_string(auth_password_strength_regex, ".+", "The regular expression that should be used to match the entire " @@ -129,8 +128,7 @@ Permissions Permissions::Deserialize(const nlohmann::json &data) { if (!data.is_object()) { throw AuthException("Couldn't load permissions data!"); } - if (!data["grants"].is_number_unsigned() || - !data["denies"].is_number_unsigned()) { + if (!data["grants"].is_number_unsigned() || !data["denies"].is_number_unsigned()) { throw AuthException("Couldn't load permissions data!"); } return {data["grants"], data["denies"]}; @@ -143,12 +141,9 @@ bool operator==(const Permissions &first, const Permissions &second) { return first.grants() == second.grants() && first.denies() == second.denies(); } -bool operator!=(const Permissions &first, const Permissions &second) { - return !(first == second); -} +bool operator!=(const Permissions &first, const Permissions &second) { return !(first == second); } -Role::Role(const std::string &rolename) - : rolename_(utils::ToLowerCase(rolename)) {} +Role::Role(const std::string &rolename) : rolename_(utils::ToLowerCase(rolename)) {} Role::Role(const std::string &rolename, const Permissions &permissions) : rolename_(utils::ToLowerCase(rolename)), permissions_(permissions) {} @@ -176,18 +171,13 @@ Role Role::Deserialize(const nlohmann::json &data) { } bool operator==(const Role &first, const Role &second) { - return first.rolename_ == second.rolename_ && - first.permissions_ == second.permissions_; + return first.rolename_ == second.rolename_ && first.permissions_ == second.permissions_; } -User::User(const std::string &username) - : username_(utils::ToLowerCase(username)) {} +User::User(const std::string &username) : username_(utils::ToLowerCase(username)) {} -User::User(const std::string &username, const std::string &password_hash, - const Permissions &permissions) - : username_(utils::ToLowerCase(username)), - password_hash_(password_hash), - permissions_(permissions) {} +User::User(const std::string &username, const std::string &password_hash, const Permissions &permissions) + : username_(utils::ToLowerCase(username)), password_hash_(password_hash), permissions_(permissions) {} bool User::CheckPassword(const std::string &password) { if (password_hash_ == "") return true; @@ -244,8 +234,7 @@ User User::Deserialize(const nlohmann::json &data) { if (!data.is_object()) { throw AuthException("Couldn't load user data!"); } - if (!data["username"].is_string() || !data["password_hash"].is_string() || - !data["permissions"].is_object()) { + if (!data["username"].is_string() || !data["password_hash"].is_string() || !data["permissions"].is_object()) { throw AuthException("Couldn't load user data!"); } auto permissions = Permissions::Deserialize(data["permissions"]); @@ -253,9 +242,7 @@ User User::Deserialize(const nlohmann::json &data) { } bool operator==(const User &first, const User &second) { - return first.username_ == second.username_ && - first.password_hash_ == second.password_hash_ && - first.permissions_ == second.permissions_ && - first.role_ == second.role_; + return first.username_ == second.username_ && first.password_hash_ == second.password_hash_ && + first.permissions_ == second.permissions_ && first.role_ == second.role_; } } // namespace auth diff --git a/src/auth/models.hpp b/src/auth/models.hpp index fbc1df6f9..cb627b052 100644 --- a/src/auth/models.hpp +++ b/src/auth/models.hpp @@ -29,11 +29,9 @@ enum class Permission : uint64_t { // Constant list of all available permissions. const std::vector kPermissionsAll = { - Permission::MATCH, Permission::CREATE, Permission::MERGE, - Permission::DELETE, Permission::SET, Permission::REMOVE, - Permission::INDEX, Permission::STATS, Permission::CONSTRAINT, - Permission::DUMP, Permission::AUTH, Permission::REPLICATION, - Permission::LOCK_PATH}; + Permission::MATCH, Permission::CREATE, Permission::MERGE, Permission::DELETE, Permission::SET, + Permission::REMOVE, Permission::INDEX, Permission::STATS, Permission::CONSTRAINT, Permission::DUMP, + Permission::AUTH, Permission::REPLICATION, Permission::LOCK_PATH}; // Function that converts a permission to its string representation. std::string PermissionToString(Permission permission); @@ -110,15 +108,13 @@ class User final { public: User(const std::string &username); - User(const std::string &username, const std::string &password_hash, - const Permissions &permissions); + User(const std::string &username, const std::string &password_hash, const Permissions &permissions); /// @throw AuthException if unable to verify the password. bool CheckPassword(const std::string &password); /// @throw AuthException if unable to set the password. - void UpdatePassword( - const std::optional &password = std::nullopt); + void UpdatePassword(const std::optional &password = std::nullopt); void SetRole(const Role &role); diff --git a/src/auth/module.cpp b/src/auth/module.cpp index eeee3ec1a..86c599329 100644 --- a/src/auth/module.cpp +++ b/src/auth/module.cpp @@ -86,54 +86,22 @@ class CharPP final { //////////////////////////////////// const std::vector kSeccompSyscallsBlacklist = { - SCMP_SYS(mknod), - SCMP_SYS(mount), - SCMP_SYS(setuid), - SCMP_SYS(stime), - SCMP_SYS(ptrace), - SCMP_SYS(setgid), - SCMP_SYS(acct), - SCMP_SYS(umount), - SCMP_SYS(setpgid), - SCMP_SYS(chroot), - SCMP_SYS(setreuid), - SCMP_SYS(setregid), - SCMP_SYS(sethostname), - SCMP_SYS(settimeofday), - SCMP_SYS(setgroups), - SCMP_SYS(swapon), - SCMP_SYS(reboot), - SCMP_SYS(setpriority), - SCMP_SYS(ioperm), - SCMP_SYS(syslog), - SCMP_SYS(iopl), - SCMP_SYS(vhangup), - SCMP_SYS(vm86old), - SCMP_SYS(swapoff), - SCMP_SYS(setdomainname), - SCMP_SYS(adjtimex), - SCMP_SYS(init_module), - SCMP_SYS(delete_module), - SCMP_SYS(setfsuid), - SCMP_SYS(setfsgid), - SCMP_SYS(setresuid), - SCMP_SYS(vm86), - SCMP_SYS(setresgid), - SCMP_SYS(capset), - SCMP_SYS(setreuid), - SCMP_SYS(setregid), - SCMP_SYS(setgroups), - SCMP_SYS(setresuid), - SCMP_SYS(setresgid), - SCMP_SYS(setuid), - SCMP_SYS(setgid), - SCMP_SYS(setfsuid), - SCMP_SYS(setfsgid), - SCMP_SYS(pivot_root), - SCMP_SYS(sched_setaffinity), - SCMP_SYS(clock_settime), - SCMP_SYS(kexec_load), - SCMP_SYS(mknodat), + SCMP_SYS(mknod), SCMP_SYS(mount), SCMP_SYS(setuid), + SCMP_SYS(stime), SCMP_SYS(ptrace), SCMP_SYS(setgid), + SCMP_SYS(acct), SCMP_SYS(umount), SCMP_SYS(setpgid), + SCMP_SYS(chroot), SCMP_SYS(setreuid), SCMP_SYS(setregid), + SCMP_SYS(sethostname), SCMP_SYS(settimeofday), SCMP_SYS(setgroups), + SCMP_SYS(swapon), SCMP_SYS(reboot), SCMP_SYS(setpriority), + SCMP_SYS(ioperm), SCMP_SYS(syslog), SCMP_SYS(iopl), + SCMP_SYS(vhangup), SCMP_SYS(vm86old), SCMP_SYS(swapoff), + SCMP_SYS(setdomainname), SCMP_SYS(adjtimex), SCMP_SYS(init_module), + SCMP_SYS(delete_module), SCMP_SYS(setfsuid), SCMP_SYS(setfsgid), + SCMP_SYS(setresuid), SCMP_SYS(vm86), SCMP_SYS(setresgid), + SCMP_SYS(capset), SCMP_SYS(setreuid), SCMP_SYS(setregid), + SCMP_SYS(setgroups), SCMP_SYS(setresuid), SCMP_SYS(setresgid), + SCMP_SYS(setuid), SCMP_SYS(setgid), SCMP_SYS(setfsuid), + SCMP_SYS(setfsgid), SCMP_SYS(pivot_root), SCMP_SYS(sched_setaffinity), + SCMP_SYS(clock_settime), SCMP_SYS(kexec_load), SCMP_SYS(mknodat), SCMP_SYS(unshare), #ifdef SYS_seccomp SCMP_SYS(seccomp), @@ -182,24 +150,20 @@ int Target(void *arg) { // Redirect `stdin` to `/dev/null`. int fd = open("/dev/null", O_RDONLY | O_CLOEXEC); if (fd == -1) { - std::cerr - << "Couldn't open \"/dev/null\" for auth module stdin because of: " - << strerror(errno) << " (" << errno << ")!" << std::endl; + std::cerr << "Couldn't open \"/dev/null\" for auth module stdin because of: " << strerror(errno) << " (" << errno + << ")!" << std::endl; return EXIT_FAILURE; } if (dup2(fd, STDIN_FILENO) != STDIN_FILENO) { - std::cerr - << "Couldn't attach \"/dev/null\" to auth module stdin because of: " - << strerror(errno) << " (" << errno << ")!" << std::endl; + std::cerr << "Couldn't attach \"/dev/null\" to auth module stdin because of: " << strerror(errno) << " (" << errno + << ")!" << std::endl; return EXIT_FAILURE; } // Change the current directory to the module directory. if (chdir(ta->module_executable_path.parent_path().c_str()) != 0) { - std::cerr << "Couldn't change directory to " - << ta->module_executable_path.parent_path() - << " for auth module stdin because of: " << strerror(errno) - << " (" << errno << ")!" << std::endl; + std::cerr << "Couldn't change directory to " << ta->module_executable_path.parent_path() + << " for auth module stdin because of: " << strerror(errno) << " (" << errno << ")!" << std::endl; return EXIT_FAILURE; } @@ -214,8 +178,7 @@ int Target(void *arg) { } // Connect the communication input pipe. - if (dup2(ta->pipe_to_module, kCommunicationToModuleFd) != - kCommunicationToModuleFd) { + if (dup2(ta->pipe_to_module, kCommunicationToModuleFd) != kCommunicationToModuleFd) { std::cerr << "Couldn't attach communication to module pipe to auth module " "because of: " << strerror(errno) << " (" << errno << ")!" << std::endl; @@ -223,8 +186,7 @@ int Target(void *arg) { } // Connect the communication output pipe. - if (dup2(ta->pipe_from_module, kCommunicationFromModuleFd) != - kCommunicationFromModuleFd) { + if (dup2(ta->pipe_from_module, kCommunicationFromModuleFd) != kCommunicationFromModuleFd) { std::cerr << "Couldn't attach communication from module pipe to auth " "module because of: " << strerror(errno) << " (" << errno << ")!" << std::endl; @@ -246,8 +208,8 @@ int Target(void *arg) { sigemptyset(&action.sa_mask); action.sa_flags = 0; if (sigaction(SIGINT, &action, nullptr) != 0) { - std::cerr << "Couldn't ignore SIGINT for auth module because of: " - << strerror(errno) << " (" << errno << ")!" << std::endl; + std::cerr << "Couldn't ignore SIGINT for auth module because of: " << strerror(errno) << " (" << errno << ")!" + << std::endl; return EXIT_FAILURE; } @@ -261,8 +223,7 @@ int Target(void *arg) { // If the `execve` call succeeded then the process will exit from that call // and won't reach this piece of code ever. - std::cerr << "Couldn't start auth module because of: " << strerror(errno) - << " (" << errno << ")!" << std::endl; + std::cerr << "Couldn't start auth module because of: " << strerror(errno) << " (" << errno << ")!" << std::endl; return EXIT_FAILURE; } @@ -408,8 +369,7 @@ bool Module::Startup() { return true; } -nlohmann::json Module::Call(const nlohmann::json ¶ms, - int timeout_millisec) { +nlohmann::json Module::Call(const nlohmann::json ¶ms, int timeout_millisec) { std::lock_guard guard(lock_); if (!params.is_object()) return {}; diff --git a/src/communication/bolt/client.hpp b/src/communication/bolt/client.hpp index 098f0714a..b28a9b54f 100644 --- a/src/communication/bolt/client.hpp +++ b/src/communication/bolt/client.hpp @@ -43,16 +43,14 @@ class ClientFatalException : public utils::BasicException { // only handle the `ClientFatalException`. class ServerCommunicationException : public ClientFatalException { public: - ServerCommunicationException() - : ClientFatalException("Couldn't communicate with the server!") {} + ServerCommunicationException() : ClientFatalException("Couldn't communicate with the server!") {} }; // Internal exception used whenever a malformed data error occurs. You should // only handle the `ClientFatalException`. class ServerMalformedDataException : public ClientFatalException { public: - ServerMalformedDataException() - : ClientFatalException("The server sent malformed data!") {} + ServerMalformedDataException() : ClientFatalException("The server sent malformed data!") {} }; /// Structure that is used to return results from an executed query. @@ -79,8 +77,7 @@ class Client final { /// connection is set-up, multiple queries may be executed through a single /// established connection. /// @throws ClientFatalException when we couldn't connect to the server - void Connect(const io::network::Endpoint &endpoint, - const std::string &username, const std::string &password, + void Connect(const io::network::Endpoint &endpoint, const std::string &username, const std::string &password, const std::string &client_name = "memgraph-bolt") { if (!client_.Connect(endpoint)) { throw ClientFatalException("Couldn't connect to {}!", endpoint); @@ -103,14 +100,11 @@ class Client final { } if (memcmp(kProtocol, client_.GetData(), sizeof(kProtocol)) != 0) { SPDLOG_ERROR("Server negotiated unsupported protocol version!"); - throw ClientFatalException( - "The server negotiated an usupported protocol version!"); + throw ClientFatalException("The server negotiated an usupported protocol version!"); } client_.ShiftData(sizeof(kProtocol)); - if (!encoder_.MessageInit(client_name, {{"scheme", "basic"}, - {"principal", username}, - {"credentials", password}})) { + if (!encoder_.MessageInit(client_name, {{"scheme", "basic"}, {"principal", username}, {"credentials", password}})) { SPDLOG_ERROR("Couldn't send init message!"); throw ServerCommunicationException(); } @@ -135,15 +129,12 @@ class Client final { /// executing the query (eg. mistyped query, /// etc.) /// @throws ClientFatalException when we couldn't communicate with the server - QueryData Execute(const std::string &query, - const std::map ¶meters) { + QueryData Execute(const std::string &query, const std::map ¶meters) { if (!client_.IsConnected()) { - throw ClientFatalException( - "You must first connect to the server before using the client!"); + throw ClientFatalException("You must first connect to the server before using the client!"); } - SPDLOG_INFO("Sending run message with statement: '{}'; parameters: {}", - query, parameters); + SPDLOG_INFO("Sending run message with statement: '{}'; parameters: {}", query, parameters); encoder_.MessageRun(query, parameters); encoder_.MessagePullAll(); @@ -165,8 +156,7 @@ class Client final { if (it != tmp.end()) { auto it_code = tmp.find("code"); if (it_code != tmp.end()) { - throw ClientQueryException(it_code->second.ValueString(), - it->second.ValueString()); + throw ClientQueryException(it_code->second.ValueString(), it->second.ValueString()); } else { throw ClientQueryException("", it->second.ValueString()); } @@ -209,8 +199,7 @@ class Client final { if (it != tmp.end()) { auto it_code = tmp.find("code"); if (it_code != tmp.end()) { - throw ClientQueryException(it_code->second.ValueString(), - it->second.ValueString()); + throw ClientQueryException(it_code->second.ValueString(), it->second.ValueString()); } else { throw ClientQueryException("", it->second.ValueString()); } @@ -308,15 +297,11 @@ class Client final { communication::ClientOutputStream output_stream_{client_}; // decoder objects - ChunkedDecoderBuffer decoder_buffer_{ - input_stream_}; - Decoder> decoder_{ - decoder_buffer_}; + ChunkedDecoderBuffer decoder_buffer_{input_stream_}; + Decoder> decoder_{decoder_buffer_}; // encoder objects - ChunkedEncoderBuffer encoder_buffer_{ - output_stream_}; - ClientEncoder> - encoder_{encoder_buffer_}; + ChunkedEncoderBuffer encoder_buffer_{output_stream_}; + ClientEncoder> encoder_{encoder_buffer_}; }; } // namespace communication::bolt diff --git a/src/communication/bolt/v1/codes.hpp b/src/communication/bolt/v1/codes.hpp index 9c6227c2f..6d7b1f627 100644 --- a/src/communication/bolt/v1/codes.hpp +++ b/src/communication/bolt/v1/codes.hpp @@ -78,12 +78,8 @@ enum class Marker : uint8_t { }; static constexpr uint8_t MarkerString = 0, MarkerList = 1, MarkerMap = 2; -static constexpr Marker MarkerTiny[3] = {Marker::TinyString, Marker::TinyList, - Marker::TinyMap}; -static constexpr Marker Marker8[3] = {Marker::String8, Marker::List8, - Marker::Map8}; -static constexpr Marker Marker16[3] = {Marker::String16, Marker::List16, - Marker::Map16}; -static constexpr Marker Marker32[3] = {Marker::String32, Marker::List32, - Marker::Map32}; +static constexpr Marker MarkerTiny[3] = {Marker::TinyString, Marker::TinyList, Marker::TinyMap}; +static constexpr Marker Marker8[3] = {Marker::String8, Marker::List8, Marker::Map8}; +static constexpr Marker Marker16[3] = {Marker::String16, Marker::List16, Marker::Map16}; +static constexpr Marker Marker32[3] = {Marker::String32, Marker::List32, Marker::Map32}; } // namespace communication::bolt diff --git a/src/communication/bolt/v1/decoder/chunked_decoder_buffer.hpp b/src/communication/bolt/v1/decoder/chunked_decoder_buffer.hpp index bce34445a..d857d27c8 100644 --- a/src/communication/bolt/v1/decoder/chunked_decoder_buffer.hpp +++ b/src/communication/bolt/v1/decoder/chunked_decoder_buffer.hpp @@ -40,9 +40,7 @@ enum class ChunkState : uint8_t { template class ChunkedDecoderBuffer { public: - ChunkedDecoderBuffer(TBuffer &buffer) : buffer_(buffer) { - data_.reserve(kChunkMaxDataSize); - } + ChunkedDecoderBuffer(TBuffer &buffer) : buffer_(buffer) { data_.reserve(kChunkMaxDataSize); } /** * Reads data from the internal buffer. diff --git a/src/communication/bolt/v1/decoder/decoder.hpp b/src/communication/bolt/v1/decoder/decoder.hpp index 3dc1e735a..83fa72130 100644 --- a/src/communication/bolt/v1/decoder/decoder.hpp +++ b/src/communication/bolt/v1/decoder/decoder.hpp @@ -158,8 +158,7 @@ class Decoder { } bool ReadBool(const Marker &marker, Value *data) { - DMG_ASSERT(marker == Marker::False || marker == Marker::True, - "Received invalid marker!"); + DMG_ASSERT(marker == Marker::False || marker == Marker::True, "Received invalid marker!"); if (marker == Marker::False) { *data = Value(false); } else { diff --git a/src/communication/bolt/v1/encoder/base_encoder.hpp b/src/communication/bolt/v1/encoder/base_encoder.hpp index 76d4dd474..543a3d451 100644 --- a/src/communication/bolt/v1/encoder/base_encoder.hpp +++ b/src/communication/bolt/v1/encoder/base_encoder.hpp @@ -7,8 +7,7 @@ #include "utils/cast.hpp" #include "utils/endian.hpp" -static_assert(std::is_same_v || - std::is_same_v, +static_assert(std::is_same_v || std::is_same_v, "communication::bolt::Encoder requires uint8_t to be " "implemented as char or unsigned char."); @@ -29,9 +28,7 @@ class BaseEncoder { void WriteRAW(const uint8_t *data, uint64_t len) { buffer_.Write(data, len); } - void WriteRAW(const char *data, uint64_t len) { - WriteRAW((const uint8_t *)data, len); - } + void WriteRAW(const char *data, uint64_t len) { WriteRAW((const uint8_t *)data, len); } void WriteRAW(const uint8_t data) { WriteRAW(&data, 1); } @@ -126,8 +123,7 @@ class BaseEncoder { void WriteEdge(const Edge &edge, bool unbound = false) { WriteRAW(utils::UnderlyingCast(Marker::TinyStruct) + (unbound ? 3 : 5)); - WriteRAW(utils::UnderlyingCast(unbound ? Signature::UnboundRelationship - : Signature::Relationship)); + WriteRAW(utils::UnderlyingCast(unbound ? Signature::UnboundRelationship : Signature::Relationship)); WriteInt(edge.id.AsInt()); if (!unbound) { diff --git a/src/communication/bolt/v1/encoder/chunked_encoder_buffer.hpp b/src/communication/bolt/v1/encoder/chunked_encoder_buffer.hpp index 5bd4abbb6..3948e6dda 100644 --- a/src/communication/bolt/v1/encoder/chunked_encoder_buffer.hpp +++ b/src/communication/bolt/v1/encoder/chunked_encoder_buffer.hpp @@ -37,8 +37,7 @@ namespace communication::bolt { template class ChunkedEncoderBuffer { public: - ChunkedEncoderBuffer(TOutputStream &output_stream) - : output_stream_(output_stream) {} + ChunkedEncoderBuffer(TOutputStream &output_stream) : output_stream_(output_stream) {} /** * Writes n values into the buffer. If n is bigger than whole chunk size @@ -53,12 +52,10 @@ class ChunkedEncoderBuffer { while (n > 0) { // Define the number of bytes which will be copied into the chunk because // the internal storage is a fixed length array. - size_t size = - n < kChunkMaxDataSize - have_ ? n : kChunkMaxDataSize - have_; + size_t size = n < kChunkMaxDataSize - have_ ? n : kChunkMaxDataSize - have_; // Copy `size` values to the chunk array. - std::memcpy(chunk_.data() + kChunkHeaderSize + have_, values + written, - size); + std::memcpy(chunk_.data() + kChunkHeaderSize + have_, values + written, size); // Update positions. The position pointer and incoming size have to be // updated because all incoming values have to be processed. @@ -87,8 +84,7 @@ class ChunkedEncoderBuffer { chunk_[1] = have_ & 0xFF; // Write the data to the stream. - auto ret = output_stream_.Write(chunk_.data(), kChunkHeaderSize + have_, - have_more); + auto ret = output_stream_.Write(chunk_.data(), kChunkHeaderSize + have_, have_more); // Cleanup. Clear(); diff --git a/src/communication/bolt/v1/encoder/client_encoder.hpp b/src/communication/bolt/v1/encoder/client_encoder.hpp index adc2a61eb..ceed77a7b 100644 --- a/src/communication/bolt/v1/encoder/client_encoder.hpp +++ b/src/communication/bolt/v1/encoder/client_encoder.hpp @@ -38,8 +38,7 @@ class ClientEncoder : private BaseEncoder { * @returns true if the data was successfully sent to the client * when flushing, false otherwise */ - bool MessageInit(const std::string client_name, - const std::map &auth_token) { + bool MessageInit(const std::string client_name, const std::map &auth_token) { WriteRAW(utils::UnderlyingCast(Marker::TinyStruct2)); WriteRAW(utils::UnderlyingCast(Signature::Init)); WriteString(client_name); @@ -65,9 +64,7 @@ class ClientEncoder : private BaseEncoder { * @returns true if the data was successfully sent to the client * when flushing, false otherwise */ - bool MessageRun(const std::string &statement, - const std::map ¶meters, - bool have_more = true) { + bool MessageRun(const std::string &statement, const std::map ¶meters, bool have_more = true) { WriteRAW(utils::UnderlyingCast(Marker::TinyStruct2)); WriteRAW(utils::UnderlyingCast(Signature::Run)); WriteString(statement); diff --git a/src/communication/bolt/v1/exceptions.hpp b/src/communication/bolt/v1/exceptions.hpp index a6d630d1b..119d9d085 100644 --- a/src/communication/bolt/v1/exceptions.hpp +++ b/src/communication/bolt/v1/exceptions.hpp @@ -50,13 +50,10 @@ class VerboseError : public utils::BasicException { }; template - VerboseError(Classification classification, const std::string &category, - const std::string &title, const std::string &format, - Args &&... args) + VerboseError(Classification classification, const std::string &category, const std::string &title, + const std::string &format, Args &&...args) : BasicException(format, std::forward(args)...), - code_(fmt::format("Memgraph.{}.{}.{}", - ClassificationToString(classification), category, - title)) {} + code_(fmt::format("Memgraph.{}.{}.{}", ClassificationToString(classification), category, title)) {} const std::string &code() const noexcept { return code_; } diff --git a/src/communication/bolt/v1/session.hpp b/src/communication/bolt/v1/session.hpp index 90b10dbcf..86bb710c0 100644 --- a/src/communication/bolt/v1/session.hpp +++ b/src/communication/bolt/v1/session.hpp @@ -62,9 +62,7 @@ class Session { * @param q If set, defines from which query to pull the results, * otherwise the last query is used. */ - virtual std::map Pull(TEncoder *encoder, - std::optional n, - std::optional qid) = 0; + virtual std::map Pull(TEncoder *encoder, std::optional n, std::optional qid) = 0; /** * Discard results of the processed query. @@ -74,8 +72,7 @@ class Session { * @param q If set, defines from which query to discard the results, * otherwise the last query is used. */ - virtual std::map Discard(std::optional n, - std::optional qid) = 0; + virtual std::map Discard(std::optional n, std::optional qid) = 0; virtual void BeginTransaction() = 0; virtual void CommitTransaction() = 0; @@ -85,8 +82,7 @@ class Session { virtual void Abort() = 0; /** Return `true` if the user was successfully authenticated. */ - virtual bool Authenticate(const std::string &username, - const std::string &password) = 0; + virtual bool Authenticate(const std::string &username, const std::string &password) = 0; /** Return the name of the server that should be used for the Bolt INIT * message. */ @@ -104,8 +100,7 @@ class Session { // Receive the handshake. if (input_stream_.size() < kHandshakeSize) { - spdlog::trace("Received partial handshake of size {}", - input_stream_.size()); + spdlog::trace("Received partial handshake of size {}", input_stream_.size()); return; } state_ = StateHandshakeRun(*this); diff --git a/src/communication/bolt/v1/state.hpp b/src/communication/bolt/v1/state.hpp index b29b506f6..e6a12a0ad 100644 --- a/src/communication/bolt/v1/state.hpp +++ b/src/communication/bolt/v1/state.hpp @@ -44,4 +44,4 @@ enum class State : uint8_t { */ Close }; -} +} // namespace communication::bolt diff --git a/src/communication/bolt/v1/states/error.hpp b/src/communication/bolt/v1/states/error.hpp index 2f032be97..9d003e9f1 100644 --- a/src/communication/bolt/v1/states/error.hpp +++ b/src/communication/bolt/v1/states/error.hpp @@ -26,8 +26,7 @@ State StateErrorRun(TSession &session, State state) { return State::Close; } - if (UNLIKELY(signature == Signature::Noop && session.version_.major == 4 && - session.version_.minor == 1)) { + if (UNLIKELY(signature == Signature::Noop && session.version_.major == 4 && session.version_.minor == 1)) { spdlog::trace("Received NOOP message"); return state; } @@ -35,8 +34,7 @@ State StateErrorRun(TSession &session, State state) { // Clear the data buffer if it has any leftover data. session.encoder_buffer_.Clear(); - if ((session.version_.major == 1 && signature == Signature::AckFailure) || - signature == Signature::Reset) { + if ((session.version_.major == 1 && signature == Signature::AckFailure) || signature == Signature::Reset) { if (signature == Signature::AckFailure) { spdlog::trace("AckFailure received"); } else { @@ -62,8 +60,7 @@ State StateErrorRun(TSession &session, State state) { // All bolt client messages have less than 15 parameters so if we receive // anything than a TinyStruct it's an error. if ((value & 0xF0) != utils::UnderlyingCast(Marker::TinyStruct)) { - spdlog::trace("Expected TinyStruct marker, but received 0x{:02X}!", - value); + spdlog::trace("Expected TinyStruct marker, but received 0x{:02X}!", value); return State::Close; } diff --git a/src/communication/bolt/v1/states/executing.hpp b/src/communication/bolt/v1/states/executing.hpp index 4c1083f4e..d771f92c5 100644 --- a/src/communication/bolt/v1/states/executing.hpp +++ b/src/communication/bolt/v1/states/executing.hpp @@ -16,8 +16,7 @@ namespace communication::bolt { // TODO (mferencevic): revise these error messages -inline std::pair ExceptionToErrorMessage( - const std::exception &e) { +inline std::pair ExceptionToErrorMessage(const std::exception &e) { if (auto *verbose = dynamic_cast(&e)) { return {verbose->code(), verbose->what()}; } @@ -54,8 +53,7 @@ inline std::pair ExceptionToErrorMessage( // All exceptions used in memgraph are derived from BasicException. Since // we caught some other exception we don't know what is going on. Return // DatabaseError, log real message and return generic string. - spdlog::error("Unknown exception occurred during query execution {}", - e.what()); + spdlog::error("Unknown exception occurred during query execution {}", e.what()); return {"Memgraph.DatabaseError.MemgraphError.MemgraphError", "An unknown exception occurred, this is unexpected. Real message " "should be in database logs."}; @@ -69,8 +67,7 @@ inline State HandleFailure(TSession &session, const std::exception &e) { } session.encoder_buffer_.Clear(); auto code_message = ExceptionToErrorMessage(e); - bool fail_sent = session.encoder_.MessageFailure( - {{"code", code_message.first}, {"message", code_message.second}}); + bool fail_sent = session.encoder_.MessageFailure({{"code", code_message.first}, {"message", code_message.second}}); if (!fail_sent) { spdlog::trace("Couldn't send failure message!"); return State::Close; @@ -80,15 +77,12 @@ inline State HandleFailure(TSession &session, const std::exception &e) { template State HandleRun(TSession &session, State state, Marker marker) { - const std::map kEmptyFields = { - {"fields", std::vector{}}}; + const std::map kEmptyFields = {{"fields", std::vector{}}}; - const auto expected_marker = - session.version_.major == 1 ? Marker::TinyStruct2 : Marker::TinyStruct3; + const auto expected_marker = session.version_.major == 1 ? Marker::TinyStruct2 : Marker::TinyStruct3; if (marker != expected_marker) { spdlog::trace("Expected {} marker, but received 0x{:02X}!", - session.version_.major == 1 ? "TinyStruct2" : "TinyStruct3", - utils::UnderlyingCast(marker)); + session.version_.major == 1 ? "TinyStruct2" : "TinyStruct3", utils::UnderlyingCast(marker)); return State::Close; } @@ -117,15 +111,13 @@ State HandleRun(TSession &session, State state, Marker marker) { return State::Close; } - DMG_ASSERT(!session.encoder_buffer_.HasData(), - "There should be no data to write in this state"); + DMG_ASSERT(!session.encoder_buffer_.HasData(), "There should be no data to write in this state"); spdlog::debug("[Run] '{}'", query.ValueString()); try { // Interpret can throw. - auto [header, qid] = - session.Interpret(query.ValueString(), params.ValueMap()); + auto [header, qid] = session.Interpret(query.ValueString(), params.ValueMap()); // Convert std::string to Value std::vector vec; std::map data; @@ -146,12 +138,10 @@ State HandleRun(TSession &session, State state, Marker marker) { namespace detail { template State HandlePullDiscard(TSession &session, State state, Marker marker) { - const auto expected_marker = - session.version_.major == 1 ? Marker::TinyStruct : Marker::TinyStruct1; + const auto expected_marker = session.version_.major == 1 ? Marker::TinyStruct : Marker::TinyStruct1; if (marker != expected_marker) { spdlog::trace("Expected {} marker, but received 0x{:02X}!", - session.version_.major == 1 ? "TinyStruct" : "TinyStruct1", - utils::UnderlyingCast(marker)); + session.version_.major == 1 ? "TinyStruct" : "TinyStruct1", utils::UnderlyingCast(marker)); return State::Close; } @@ -176,15 +166,13 @@ State HandlePullDiscard(TSession &session, State state, Marker marker) { } const auto &extra_map = extra.ValueMap(); if (extra_map.count("n")) { - if (const auto n_value = extra_map.at("n").ValueInt(); - n_value != kPullAll) { + if (const auto n_value = extra_map.at("n").ValueInt(); n_value != kPullAll) { n = n_value; } } if (extra_map.count("qid")) { - if (const auto qid_value = extra_map.at("qid").ValueInt(); - qid_value != kPullLast) { + if (const auto qid_value = extra_map.at("qid").ValueInt(); qid_value != kPullLast) { qid = qid_value; } } @@ -236,8 +224,7 @@ State HandleReset(Session &session, State, Marker marker) { // now this command only resets the session to a clean state. It // does not IGNORE running and pending commands as it should. if (marker != Marker::TinyStruct) { - spdlog::trace("Expected TinyStruct marker, but received 0x{:02X}!", - utils::UnderlyingCast(marker)); + spdlog::trace("Expected TinyStruct marker, but received 0x{:02X}!", utils::UnderlyingCast(marker)); return State::Close; } @@ -262,8 +249,7 @@ State HandleBegin(Session &session, State state, Marker marker) { } if (marker != Marker::TinyStruct1) { - spdlog::trace("Expected TinyStruct1 marker, but received 0x{:02x}!", - utils::UnderlyingCast(marker)); + spdlog::trace("Expected TinyStruct1 marker, but received 0x{:02x}!", utils::UnderlyingCast(marker)); return State::Close; } @@ -278,8 +264,7 @@ State HandleBegin(Session &session, State state, Marker marker) { return State::Close; } - DMG_ASSERT(!session.encoder_buffer_.HasData(), - "There should be no data to write in this state"); + DMG_ASSERT(!session.encoder_buffer_.HasData(), "There should be no data to write in this state"); if (!session.encoder_.MessageSuccess({})) { spdlog::trace("Couldn't send success message!"); @@ -303,8 +288,7 @@ State HandleCommit(Session &session, State state, Marker marker) { } if (marker != Marker::TinyStruct) { - spdlog::trace("Expected TinyStruct marker, but received 0x{:02x}!", - utils::UnderlyingCast(marker)); + spdlog::trace("Expected TinyStruct marker, but received 0x{:02x}!", utils::UnderlyingCast(marker)); return State::Close; } @@ -313,8 +297,7 @@ State HandleCommit(Session &session, State state, Marker marker) { return State::Close; } - DMG_ASSERT(!session.encoder_buffer_.HasData(), - "There should be no data to write in this state"); + DMG_ASSERT(!session.encoder_buffer_.HasData(), "There should be no data to write in this state"); try { if (!session.encoder_.MessageSuccess({})) { @@ -336,8 +319,7 @@ State HandleRollback(Session &session, State state, Marker marker) { } if (marker != Marker::TinyStruct) { - spdlog::trace("Expected TinyStruct marker, but received 0x{:02x}!", - utils::UnderlyingCast(marker)); + spdlog::trace("Expected TinyStruct marker, but received 0x{:02x}!", utils::UnderlyingCast(marker)); return State::Close; } @@ -346,8 +328,7 @@ State HandleRollback(Session &session, State state, Marker marker) { return State::Close; } - DMG_ASSERT(!session.encoder_buffer_.HasData(), - "There should be no data to write in this state"); + DMG_ASSERT(!session.encoder_buffer_.HasData(), "There should be no data to write in this state"); try { if (!session.encoder_.MessageSuccess({})) { @@ -376,8 +357,7 @@ State StateExecutingRun(Session &session, State state) { return State::Close; } - if (UNLIKELY(signature == Signature::Noop && session.version_.major == 4 && - session.version_.minor == 1)) { + if (UNLIKELY(signature == Signature::Noop && session.version_.major == 4 && session.version_.minor == 1)) { spdlog::trace("Received NOOP message"); return state; } @@ -399,8 +379,7 @@ State StateExecutingRun(Session &session, State state) { } else if (signature == Signature::Goodbye && session.version_.major != 1) { throw SessionClosedException("Closing connection."); } else { - spdlog::trace("Unrecognized signature received (0x{:02X})!", - utils::UnderlyingCast(signature)); + spdlog::trace("Unrecognized signature received (0x{:02X})!", utils::UnderlyingCast(signature)); return State::Close; } } diff --git a/src/communication/bolt/v1/states/handshake.hpp b/src/communication/bolt/v1/states/handshake.hpp index 4c11b8166..dfdaca89c 100644 --- a/src/communication/bolt/v1/states/handshake.hpp +++ b/src/communication/bolt/v1/states/handshake.hpp @@ -17,15 +17,13 @@ namespace communication::bolt { */ template State StateHandshakeRun(TSession &session) { - auto precmp = - std::memcmp(session.input_stream_.data(), kPreamble, sizeof(kPreamble)); + auto precmp = std::memcmp(session.input_stream_.data(), kPreamble, sizeof(kPreamble)); if (UNLIKELY(precmp != 0)) { spdlog::trace("Received a wrong preamble!"); return State::Close; } - DMG_ASSERT(session.input_stream_.size() >= kHandshakeSize, - "Wrong size of the handshake data!"); + DMG_ASSERT(session.input_stream_.size() >= kHandshakeSize, "Wrong size of the handshake data!"); auto dataPosition = session.input_stream_.data() + sizeof(kPreamble); @@ -61,8 +59,7 @@ State StateHandshakeRun(TSession &session) { return State::Close; } - spdlog::info("Using version {}.{} of protocol", session.version_.major, - session.version_.minor); + spdlog::info("Using version {}.{} of protocol", session.version_.major, session.version_.minor); // Delete data from the input stream. It is guaranteed that there will more // than, or equal to 20 bytes (kHandshakeSize) in the buffer. diff --git a/src/communication/bolt/v1/states/init.hpp b/src/communication/bolt/v1/states/init.hpp index 336703fb4..a537c4816 100644 --- a/src/communication/bolt/v1/states/init.hpp +++ b/src/communication/bolt/v1/states/init.hpp @@ -15,8 +15,7 @@ namespace detail { template std::optional StateInitRunV1(TSession &session, const Marker marker) { if (UNLIKELY(marker != Marker::TinyStruct2)) { - spdlog::trace("Expected TinyStruct2 marker, but received 0x{:02X}!", - utils::UnderlyingCast(marker)); + spdlog::trace("Expected TinyStruct2 marker, but received 0x{:02X}!", utils::UnderlyingCast(marker)); spdlog::trace( "The client sent malformed data, but we are continuing " "because the official Neo4j Java driver sends malformed " @@ -45,8 +44,7 @@ std::optional StateInitRunV1(TSession &session, const Marker marker) { template std::optional StateInitRunV4(TSession &session, const Marker marker) { if (UNLIKELY(marker != Marker::TinyStruct1)) { - spdlog::trace("Expected TinyStruct1 marker, but received 0x{:02X}!", - utils::UnderlyingCast(marker)); + spdlog::trace("Expected TinyStruct1 marker, but received 0x{:02X}!", utils::UnderlyingCast(marker)); spdlog::trace( "The client sent malformed data, but we are continuing " "because the official Neo4j Java driver sends malformed " @@ -80,8 +78,7 @@ std::optional StateInitRunV4(TSession &session, const Marker marker) { */ template State StateInitRun(Session &session) { - DMG_ASSERT(!session.encoder_buffer_.HasData(), - "There should be no data to write in this state"); + DMG_ASSERT(!session.encoder_buffer_.HasData(), "There should be no data to write in this state"); Marker marker; Signature signature; @@ -90,21 +87,18 @@ State StateInitRun(Session &session) { return State::Close; } - if (UNLIKELY(signature == Signature::Noop && session.version_.major == 4 && - session.version_.minor == 1)) { + if (UNLIKELY(signature == Signature::Noop && session.version_.major == 4 && session.version_.minor == 1)) { SPDLOG_DEBUG("Received NOOP message"); return State::Init; } if (UNLIKELY(signature != Signature::Init)) { - spdlog::trace("Expected Init signature, but received 0x{:02X}!", - utils::UnderlyingCast(signature)); + spdlog::trace("Expected Init signature, but received 0x{:02X}!", utils::UnderlyingCast(signature)); return State::Close; } - auto maybeMetadata = session.version_.major == 1 - ? detail::StateInitRunV1(session, marker) - : detail::StateInitRunV4(session, marker); + auto maybeMetadata = + session.version_.major == 1 ? detail::StateInitRunV1(session, marker) : detail::StateInitRunV4(session, marker); if (!maybeMetadata) { return State::Close; @@ -126,16 +120,14 @@ State StateInitRun(Session &session) { username = data["principal"].ValueString(); password = data["credentials"].ValueString(); } else if (data["scheme"].ValueString() != "none") { - spdlog::warn("Unsupported authentication scheme: {}", - data["scheme"].ValueString()); + spdlog::warn("Unsupported authentication scheme: {}", data["scheme"].ValueString()); return State::Close; } // Authenticate the user. if (!session.Authenticate(username, password)) { if (!session.encoder_.MessageFailure( - {{"code", "Memgraph.ClientError.Security.Unauthenticated"}, - {"message", "Authentication failure"}})) { + {{"code", "Memgraph.ClientError.Security.Unauthenticated"}, {"message", "Authentication failure"}})) { spdlog::trace("Couldn't send failure message to the client!"); } // Throw an exception to indicate to the network stack that the session diff --git a/src/communication/bolt/v1/value.cpp b/src/communication/bolt/v1/value.cpp index f626d99ca..8babac83a 100644 --- a/src/communication/bolt/v1/value.cpp +++ b/src/communication/bolt/v1/value.cpp @@ -198,8 +198,7 @@ Value &Value::operator=(Value &&other) noexcept { new (&edge_v) Edge(std::move(other.edge_v)); break; case Type::UnboundedEdge: - new (&unbounded_edge_v) - UnboundedEdge(std::move(other.unbounded_edge_v)); + new (&unbounded_edge_v) UnboundedEdge(std::move(other.unbounded_edge_v)); break; case Type::Path: new (&path_v) Path(std::move(other.path_v)); @@ -258,17 +257,14 @@ std::ostream &operator<<(std::ostream &os, const Vertex &vertex) { if (vertex.labels.size() > 0) { os << ":"; } - utils::PrintIterable(os, vertex.labels, ":", - [&](auto &stream, auto label) { stream << label; }); + utils::PrintIterable(os, vertex.labels, ":", [&](auto &stream, auto label) { stream << label; }); if (vertex.labels.size() > 0 && vertex.properties.size() > 0) { os << " "; } if (vertex.properties.size() > 0) { os << "{"; utils::PrintIterable(os, vertex.properties, ", ", - [&](auto &stream, const auto &pair) { - stream << pair.first << ": " << pair.second; - }); + [&](auto &stream, const auto &pair) { stream << pair.first << ": " << pair.second; }); os << "}"; } return os << ")"; @@ -279,9 +275,7 @@ std::ostream &operator<<(std::ostream &os, const Edge &edge) { if (edge.properties.size() > 0) { os << " {"; utils::PrintIterable(os, edge.properties, ", ", - [&](auto &stream, const auto &pair) { - stream << pair.first << ": " << pair.second; - }); + [&](auto &stream, const auto &pair) { stream << pair.first << ": " << pair.second; }); os << "}"; } return os << "]"; @@ -292,9 +286,7 @@ std::ostream &operator<<(std::ostream &os, const UnboundedEdge &edge) { if (edge.properties.size() > 0) { os << " {"; utils::PrintIterable(os, edge.properties, ", ", - [&](auto &stream, const auto &pair) { - stream << pair.first << ": " << pair.second; - }); + [&](auto &stream, const auto &pair) { stream << pair.first << ": " << pair.second; }); os << "}"; } return os << "]"; @@ -339,9 +331,7 @@ std::ostream &operator<<(std::ostream &os, const Value &value) { case Value::Type::Map: os << "{"; utils::PrintIterable(os, value.ValueMap(), ", ", - [](auto &stream, const auto &pair) { - stream << pair.first << ": " << pair.second; - }); + [](auto &stream, const auto &pair) { stream << pair.first << ": " << pair.second; }); return os << "}"; case Value::Type::Vertex: return os << value.ValueVertex(); diff --git a/src/communication/bolt/v1/value.hpp b/src/communication/bolt/v1/value.hpp index 80dfc797b..543370e47 100644 --- a/src/communication/bolt/v1/value.hpp +++ b/src/communication/bolt/v1/value.hpp @@ -33,9 +33,7 @@ class Id { int64_t id_; }; -inline bool operator==(const Id &id1, const Id &id2) { - return id1.AsInt() == id2.AsInt(); -} +inline bool operator==(const Id &id1, const Id &id2) { return id1.AsInt() == id2.AsInt(); } inline bool operator!=(const Id &id1, const Id &id2) { return !(id1 == id2); } @@ -84,13 +82,10 @@ struct Path { // into the collection and puts that index into `indices`. A multiplier is // added to switch between positive and negative indices (that define edge // direction). - auto add_element = [this](auto &collection, const auto &element, - int multiplier, int offset) { + auto add_element = [this](auto &collection, const auto &element, int multiplier, int offset) { auto found = - std::find_if(collection.begin(), collection.end(), - [&](const auto &e) { return e.id == element.id; }); - indices.emplace_back(multiplier * - (std::distance(collection.begin(), found) + offset)); + std::find_if(collection.begin(), collection.end(), [&](const auto &e) { return e.id == element.id; }); + indices.emplace_back(multiplier * (std::distance(collection.begin(), found) + offset)); if (found == collection.end()) collection.push_back(element); }; @@ -125,19 +120,7 @@ class Value { Value() : type_(Type::Null) {} /** Types that can be stored in a Value. */ - enum class Type : unsigned { - Null, - Bool, - Int, - Double, - String, - List, - Map, - Vertex, - Edge, - UnboundedEdge, - Path - }; + enum class Type : unsigned { Null, Bool, Int, Double, String, List, Map, Vertex, Edge, UnboundedEdge, Path }; // constructors for primitive types Value(bool value) : type_(Type::Bool) { bool_v = value; } @@ -146,47 +129,29 @@ class Value { Value(double value) : type_(Type::Double) { double_v = value; } // constructors for non-primitive types - Value(const std::string &value) : type_(Type::String) { - new (&string_v) std::string(value); - } + Value(const std::string &value) : type_(Type::String) { new (&string_v) std::string(value); } Value(const char *value) : Value(std::string(value)) {} - Value(const std::vector &value) : type_(Type::List) { - new (&list_v) std::vector(value); - } + Value(const std::vector &value) : type_(Type::List) { new (&list_v) std::vector(value); } Value(const std::map &value) : type_(Type::Map) { new (&map_v) std::map(value); } - Value(const Vertex &value) : type_(Type::Vertex) { - new (&vertex_v) Vertex(value); - } + Value(const Vertex &value) : type_(Type::Vertex) { new (&vertex_v) Vertex(value); } Value(const Edge &value) : type_(Type::Edge) { new (&edge_v) Edge(value); } - Value(const UnboundedEdge &value) : type_(Type::UnboundedEdge) { - new (&unbounded_edge_v) UnboundedEdge(value); - } + Value(const UnboundedEdge &value) : type_(Type::UnboundedEdge) { new (&unbounded_edge_v) UnboundedEdge(value); } Value(const Path &value) : type_(Type::Path) { new (&path_v) Path(value); } // move constructors for non-primitive values - Value(std::string &&value) noexcept : type_(Type::String) { - new (&string_v) std::string(std::move(value)); - } - Value(std::vector &&value) noexcept : type_(Type::List) { - new (&list_v) std::vector(std::move(value)); - } + Value(std::string &&value) noexcept : type_(Type::String) { new (&string_v) std::string(std::move(value)); } + Value(std::vector &&value) noexcept : type_(Type::List) { new (&list_v) std::vector(std::move(value)); } Value(std::map &&value) noexcept : type_(Type::Map) { new (&map_v) std::map(std::move(value)); } - Value(Vertex &&value) noexcept : type_(Type::Vertex) { - new (&vertex_v) Vertex(std::move(value)); - } - Value(Edge &&value) noexcept : type_(Type::Edge) { - new (&edge_v) Edge(std::move(value)); - } + Value(Vertex &&value) noexcept : type_(Type::Vertex) { new (&vertex_v) Vertex(std::move(value)); } + Value(Edge &&value) noexcept : type_(Type::Edge) { new (&edge_v) Edge(std::move(value)); } Value(UnboundedEdge &&value) noexcept : type_(Type::UnboundedEdge) { new (&unbounded_edge_v) UnboundedEdge(std::move(value)); } - Value(Path &&value) noexcept : type_(Type::Path) { - new (&path_v) Path(std::move(value)); - } + Value(Path &&value) noexcept : type_(Type::Path) { new (&path_v) Path(std::move(value)); } Value &operator=(const Value &other); Value &operator=(Value &&other) noexcept; diff --git a/src/communication/buffer.cpp b/src/communication/buffer.cpp index 9f29391f1..930653359 100644 --- a/src/communication/buffer.cpp +++ b/src/communication/buffer.cpp @@ -4,8 +4,7 @@ namespace communication { -Buffer::Buffer() - : data_(kBufferInitialSize, 0), read_end_(this), write_end_(this) {} +Buffer::Buffer() : data_(kBufferInitialSize, 0), read_end_(this), write_end_(this) {} Buffer::ReadEnd::ReadEnd(Buffer *buffer) : buffer_(buffer) {} @@ -21,9 +20,7 @@ void Buffer::ReadEnd::Clear() { buffer_->Clear(); } Buffer::WriteEnd::WriteEnd(Buffer *buffer) : buffer_(buffer) {} -io::network::StreamBuffer Buffer::WriteEnd::Allocate() { - return buffer_->Allocate(); -} +io::network::StreamBuffer Buffer::WriteEnd::Allocate() { return buffer_->Allocate(); } void Buffer::WriteEnd::Written(size_t len) { buffer_->Written(len); } diff --git a/src/communication/client.cpp b/src/communication/client.cpp index ba2f079a5..0483a86f9 100644 --- a/src/communication/client.cpp +++ b/src/communication/client.cpp @@ -195,8 +195,7 @@ bool Client::Write(const uint8_t *data, size_t len, bool have_more) { } bool Client::Write(const std::string &str, bool have_more) { - return Write(reinterpret_cast(str.data()), str.size(), - have_more); + return Write(reinterpret_cast(str.data()), str.size(), have_more); } const io::network::Endpoint &Client::endpoint() { return socket_.endpoint(); } @@ -224,12 +223,9 @@ void ClientInputStream::Clear() { client_.ClearData(); } ClientOutputStream::ClientOutputStream(Client &client) : client_(client) {} -bool ClientOutputStream::Write(const uint8_t *data, size_t len, - bool have_more) { +bool ClientOutputStream::Write(const uint8_t *data, size_t len, bool have_more) { return client_.Write(data, len, have_more); } -bool ClientOutputStream::Write(const std::string &str, bool have_more) { - return client_.Write(str, have_more); -} +bool ClientOutputStream::Write(const std::string &str, bool have_more) { return client_.Write(str, have_more); } } // namespace communication diff --git a/src/communication/context.cpp b/src/communication/context.cpp index 88ba1be71..248bef969 100644 --- a/src/communication/context.cpp +++ b/src/communication/context.cpp @@ -19,21 +19,16 @@ ClientContext::ClientContext(bool use_ssl) : use_ssl_(use_ssl), ctx_(nullptr) { } } -ClientContext::ClientContext(const std::string &key_file, - const std::string &cert_file) - : ClientContext(true) { +ClientContext::ClientContext(const std::string &key_file, const std::string &cert_file) : ClientContext(true) { if (key_file != "" && cert_file != "") { - MG_ASSERT(SSL_CTX_use_certificate_file(ctx_, cert_file.c_str(), - SSL_FILETYPE_PEM) == 1, + MG_ASSERT(SSL_CTX_use_certificate_file(ctx_, cert_file.c_str(), SSL_FILETYPE_PEM) == 1, "Couldn't load client certificate from file: {}", cert_file); - MG_ASSERT(SSL_CTX_use_PrivateKey_file(ctx_, key_file.c_str(), - SSL_FILETYPE_PEM) == 1, + MG_ASSERT(SSL_CTX_use_PrivateKey_file(ctx_, key_file.c_str(), SSL_FILETYPE_PEM) == 1, "Couldn't load client private key from file: ", key_file); } } -ClientContext::ClientContext(ClientContext &&other) noexcept - : use_ssl_(other.use_ssl_), ctx_(other.ctx_) { +ClientContext::ClientContext(ClientContext &&other) noexcept : use_ssl_(other.use_ssl_), ctx_(other.ctx_) { other.use_ssl_ = false; other.ctx_ = nullptr; } @@ -69,9 +64,8 @@ bool ClientContext::use_ssl() { return use_ssl_; } ServerContext::ServerContext() : use_ssl_(false), ctx_(nullptr) {} -ServerContext::ServerContext(const std::string &key_file, - const std::string &cert_file, - const std::string &ca_file, bool verify_peer) +ServerContext::ServerContext(const std::string &key_file, const std::string &cert_file, const std::string &ca_file, + bool verify_peer) : use_ssl_(true), #if OPENSSL_VERSION_NUMBER < 0x10100000L ctx_(SSL_CTX_new(SSLv23_server_method())) @@ -81,11 +75,9 @@ ServerContext::ServerContext(const std::string &key_file, { // TODO (mferencevic): add support for encrypted private keys // TODO (mferencevic): add certificate revocation list (CRL) - MG_ASSERT(SSL_CTX_use_certificate_file(ctx_, cert_file.c_str(), - SSL_FILETYPE_PEM) == 1, + MG_ASSERT(SSL_CTX_use_certificate_file(ctx_, cert_file.c_str(), SSL_FILETYPE_PEM) == 1, "Couldn't load server certificate from file: {}", cert_file); - MG_ASSERT(SSL_CTX_use_PrivateKey_file(ctx_, key_file.c_str(), - SSL_FILETYPE_PEM) == 1, + MG_ASSERT(SSL_CTX_use_PrivateKey_file(ctx_, key_file.c_str(), SSL_FILETYPE_PEM) == 1, "Couldn't load server private key from file: {}", key_file); // Disable legacy SSL support. Other options can be seen here: @@ -94,29 +86,25 @@ ServerContext::ServerContext(const std::string &key_file, if (ca_file != "") { // Load the certificate authority file. - MG_ASSERT( - SSL_CTX_load_verify_locations(ctx_, ca_file.c_str(), nullptr) == 1, - "Couldn't load certificate authority from file: {}", ca_file); + MG_ASSERT(SSL_CTX_load_verify_locations(ctx_, ca_file.c_str(), nullptr) == 1, + "Couldn't load certificate authority from file: {}", ca_file); if (verify_peer) { // Add the CA to list of accepted CAs that is sent to the client. STACK_OF(X509_NAME) *ca_names = SSL_load_client_CA_file(ca_file.c_str()); - MG_ASSERT(ca_names != nullptr, - "Couldn't load certificate authority from file: {}", ca_file); + MG_ASSERT(ca_names != nullptr, "Couldn't load certificate authority from file: {}", ca_file); // `ca_names` doesn' need to be free'd because we pass it to // `SSL_CTX_set_client_CA_list`: // https://mta.openssl.org/pipermail/openssl-users/2015-May/001363.html SSL_CTX_set_client_CA_list(ctx_, ca_names); // Enable verification of the client certificate. - SSL_CTX_set_verify( - ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); + SSL_CTX_set_verify(ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); } } } -ServerContext::ServerContext(ServerContext &&other) noexcept - : use_ssl_(other.use_ssl_), ctx_(other.ctx_) { +ServerContext::ServerContext(ServerContext &&other) noexcept : use_ssl_(other.use_ssl_), ctx_(other.ctx_) { other.use_ssl_ = false; other.ctx_ = nullptr; } diff --git a/src/communication/context.hpp b/src/communication/context.hpp index a91712849..9d6184df8 100644 --- a/src/communication/context.hpp +++ b/src/communication/context.hpp @@ -72,8 +72,8 @@ class ServerContext final { * to check that the client certificate is valid, then you need to supply a * valid `ca_file` as well. */ - ServerContext(const std::string &key_file, const std::string &cert_file, - const std::string &ca_file = "", bool verify_peer = false); + ServerContext(const std::string &key_file, const std::string &cert_file, const std::string &ca_file = "", + bool verify_peer = false); // This object can't be copied because the underlying SSL implementation is // messy and ownership can't be handled correctly. diff --git a/src/communication/init.cpp b/src/communication/init.cpp index 5498e1737..44fc7a654 100644 --- a/src/communication/init.cpp +++ b/src/communication/init.cpp @@ -28,10 +28,7 @@ void LockingFunction(int mode, int n, const char *file, int line) { } } -unsigned long IdFunction() { - return (unsigned long)std::hash()( - std::this_thread::get_id()); -} +unsigned long IdFunction() { return (unsigned long)std::hash()(std::this_thread::get_id()); } void SetupThreading() { crypto_locks.resize(CRYPTO_num_locks()); @@ -58,8 +55,7 @@ SSLInit::SSLInit() { ERR_load_crypto_strings(); // Ignore SIGPIPE. - MG_ASSERT(utils::SignalIgnore(utils::Signal::Pipe), - "Couldn't ignore SIGPIPE!"); + MG_ASSERT(utils::SignalIgnore(utils::Signal::Pipe), "Couldn't ignore SIGPIPE!"); SetupThreading(); } diff --git a/src/communication/listener.hpp b/src/communication/listener.hpp index 29ff5cb11..14d892d48 100644 --- a/src/communication/listener.hpp +++ b/src/communication/listener.hpp @@ -40,8 +40,7 @@ class Listener final { using SessionHandler = Session; public: - Listener(TSessionData *data, ServerContext *context, - int inactivity_timeout_sec, const std::string &service_name, + Listener(TSessionData *data, ServerContext *context, int inactivity_timeout_sec, const std::string &service_name, size_t workers_count) : data_(data), alive_(false), @@ -77,8 +76,8 @@ class Listener final { int fd = connection.fd(); // Create a new Session for the connection. - sessions_.push_back(std::make_unique( - std::move(connection), data_, context_, inactivity_timeout_sec_)); + sessions_.push_back( + std::make_unique(std::move(connection), data_, context_, inactivity_timeout_sec_)); // Register the connection in Epoll. // We want to listen to an incoming event which is edge triggered and @@ -86,8 +85,7 @@ class Listener final { // concurrently and that is why we use `EPOLLONESHOT`, for a detailed // description what are the problems and why this is correct see: // https://idea.popcount.org/2017-02-20-epoll-is-fundamentally-broken-12/ - epoll_.Add(fd, EPOLLIN | EPOLLET | EPOLLRDHUP | EPOLLONESHOT, - sessions_.back().get()); + epoll_.Add(fd, EPOLLIN | EPOLLET | EPOLLRDHUP | EPOLLONESHOT, sessions_.back().get()); } /** @@ -117,8 +115,7 @@ class Listener final { std::lock_guard guard(lock_); for (auto &session : sessions_) { if (session->TimedOut()) { - spdlog::warn("{} session associated with {} timed out", - service_name, session->socket().endpoint()); + spdlog::warn("{} session associated with {} timed out", service_name, session->socket().endpoint()); // Here we shutdown the socket to terminate any leftover // blocking `Write` calls and to signal an event that the // session is closed. Session cleanup will be done in the event @@ -178,8 +175,7 @@ class Listener final { // dereference it here. It is safe to dereference the pointer because // this design guarantees that there will never be an event that has // a stale Session pointer. - SessionHandler &session = - *reinterpret_cast(event.data.ptr); + SessionHandler &session = *reinterpret_cast(event.data.ptr); // Process epoll events. We use epoll in edge-triggered mode so we process // all events here. Only one of the `if` statements must be executed @@ -192,20 +188,16 @@ class Listener final { ; } else if (event.events & EPOLLRDHUP) { // The client closed the connection. - spdlog::info("{} client {} closed the connection.", service_name_, - session.socket().endpoint()); + spdlog::info("{} client {} closed the connection.", service_name_, session.socket().endpoint()); CloseSession(session); - } else if (!(event.events & EPOLLIN) || - event.events & (EPOLLHUP | EPOLLERR)) { + } else if (!(event.events & EPOLLIN) || event.events & (EPOLLHUP | EPOLLERR)) { // There was an error on the server side. - spdlog::error("Error occured in {} session associated with {}", - service_name_, session.socket().endpoint()); + spdlog::error("Error occured in {} session associated with {}", service_name_, session.socket().endpoint()); CloseSession(session); } else { // Unhandled epoll event. - spdlog::error( - "Unhandled event occured in {} session associated with {} events: {}", - service_name_, session.socket().endpoint(), event.events); + spdlog::error("Unhandled event occured in {} session associated with {} events: {}", service_name_, + session.socket().endpoint(), event.events); CloseSession(session); } } @@ -215,13 +207,11 @@ class Listener final { if (session.Execute()) { // Session execution done, rearm epoll to send events for this // socket. - epoll_.Modify(session.socket().fd(), - EPOLLIN | EPOLLET | EPOLLRDHUP | EPOLLONESHOT, &session); + epoll_.Modify(session.socket().fd(), EPOLLIN | EPOLLET | EPOLLRDHUP | EPOLLONESHOT, &session); return false; } } catch (const SessionClosedException &e) { - spdlog::info("{} client {} closed the connection.", service_name_, - session.socket().endpoint()); + spdlog::info("{} client {} closed the connection.", service_name_, session.socket().endpoint()); CloseSession(session); return false; } catch (const std::exception &e) { @@ -245,11 +235,9 @@ class Listener final { epoll_.Delete(session.socket().fd()); std::lock_guard guard(lock_); - auto it = std::find_if(sessions_.begin(), sessions_.end(), - [&](const auto &l) { return l.get() == &session; }); + auto it = std::find_if(sessions_.begin(), sessions_.end(), [&](const auto &l) { return l.get() == &session; }); - MG_ASSERT(it != sessions_.end(), - "Trying to remove session that is not found in sessions!"); + MG_ASSERT(it != sessions_.end(), "Trying to remove session that is not found in sessions!"); int i = it - sessions_.begin(); swap(sessions_[i], sessions_.back()); diff --git a/src/communication/result_stream_faker.hpp b/src/communication/result_stream_faker.hpp index 539be990c..cda3e8ecf 100644 --- a/src/communication/result_stream_faker.hpp +++ b/src/communication/result_stream_faker.hpp @@ -27,9 +27,7 @@ class ResultStreamFaker { void Header(const std::vector &fields) { header_ = fields; } - void Result(const std::vector &values) { - results_.push_back(values); - } + void Result(const std::vector &values) { results_.push_back(values); } void Result(const std::vector &values) { std::vector bvalues; @@ -42,16 +40,12 @@ class ResultStreamFaker { results_.push_back(std::move(bvalues)); } - void Summary( - const std::map &summary) { - summary_ = summary; - } + void Summary(const std::map &summary) { summary_ = summary; } void Summary(const std::map &summary) { std::map bsummary; for (const auto &item : summary) { - auto maybe_value = - glue::ToBoltValue(item.second, *store_, storage::View::NEW); + auto maybe_value = glue::ToBoltValue(item.second, *store_, storage::View::NEW); MG_ASSERT(maybe_value.HasValue()); bsummary.insert({item.first, std::move(*maybe_value)}); } @@ -64,8 +58,7 @@ class ResultStreamFaker { const auto &GetSummary() const { return summary_; } - friend std::ostream &operator<<(std::ostream &os, - const ResultStreamFaker &results) { + friend std::ostream &operator<<(std::ostream &os, const ResultStreamFaker &results) { auto decoded_value_to_string = [](const auto &value) { std::stringstream ss; ss << value; @@ -73,21 +66,16 @@ class ResultStreamFaker { }; const std::vector &header = results.GetHeader(); std::vector column_widths(header.size()); - std::transform(header.begin(), header.end(), column_widths.begin(), - [](const auto &s) { return s.size(); }); + std::transform(header.begin(), header.end(), column_widths.begin(), [](const auto &s) { return s.size(); }); // convert all the results into strings, and track max column width auto &results_data = results.GetResults(); - std::vector> result_strings( - results_data.size(), std::vector(column_widths.size())); - for (int row_ind = 0; row_ind < static_cast(results_data.size()); - ++row_ind) { - for (int col_ind = 0; col_ind < static_cast(column_widths.size()); - ++col_ind) { - std::string string_val = - decoded_value_to_string(results_data[row_ind][col_ind]); - column_widths[col_ind] = - std::max(column_widths[col_ind], (int)string_val.size()); + std::vector> result_strings(results_data.size(), + std::vector(column_widths.size())); + for (int row_ind = 0; row_ind < static_cast(results_data.size()); ++row_ind) { + for (int col_ind = 0; col_ind < static_cast(column_widths.size()); ++col_ind) { + std::string string_val = decoded_value_to_string(results_data[row_ind][col_ind]); + column_widths[col_ind] = std::max(column_widths[col_ind], (int)string_val.size()); result_strings[row_ind][col_ind] = string_val; } } @@ -96,15 +84,13 @@ class ResultStreamFaker { // first define some helper functions auto emit_horizontal_line = [&]() { os << "+"; - for (auto col_width : column_widths) - os << std::string((unsigned long)col_width + 2, '-') << "+"; + for (auto col_width : column_widths) os << std::string((unsigned long)col_width + 2, '-') << "+"; os << std::endl; }; auto emit_result_vec = [&](const std::vector result_vec) { os << "| "; - for (int col_ind = 0; col_ind < static_cast(column_widths.size()); - ++col_ind) { + for (int col_ind = 0; col_ind < static_cast(column_widths.size()); ++col_ind) { const std::string &res = result_vec[col_ind]; os << res << std::string(column_widths[col_ind] - res.size(), ' '); os << " | "; @@ -123,9 +109,7 @@ class ResultStreamFaker { // output the summary os << "Query summary: {"; utils::PrintIterable(os, results.GetSummary(), ", ", - [&](auto &stream, const auto &kv) { - stream << kv.first << ": " << kv.second; - }); + [&](auto &stream, const auto &kv) { stream << kv.first << ": " << kv.second; }); os << "}" << std::endl; return os; diff --git a/src/communication/server.hpp b/src/communication/server.hpp index 290cf3465..c5ec3d296 100644 --- a/src/communication/server.hpp +++ b/src/communication/server.hpp @@ -46,14 +46,12 @@ class Server final { * Constructs and binds server to endpoint, operates on session data and * invokes workers_count workers */ - Server(const io::network::Endpoint &endpoint, TSessionData *session_data, - ServerContext *context, int inactivity_timeout_sec, - const std::string &service_name, + Server(const io::network::Endpoint &endpoint, TSessionData *session_data, ServerContext *context, + int inactivity_timeout_sec, const std::string &service_name, size_t workers_count = std::thread::hardware_concurrency()) : alive_(false), endpoint_(endpoint), - listener_(session_data, context, inactivity_timeout_sec, service_name, - workers_count), + listener_(session_data, context, inactivity_timeout_sec, service_name, workers_count), service_name_(service_name) {} ~Server() { @@ -69,8 +67,7 @@ class Server final { Server &operator=(Server &&) = delete; const auto &endpoint() const { - MG_ASSERT(alive_, - "You can't get the server endpoint when it's not running!"); + MG_ASSERT(alive_, "You can't get the server endpoint when it's not running!"); return socket_.endpoint(); } @@ -138,8 +135,7 @@ class Server final { // Connection is not available anymore or configuration failed. return; } - spdlog::info("Accepted a {} connection from {}", service_name_, - s->endpoint()); + spdlog::info("Accepted a {} connection from {}", service_name_, s->endpoint()); listener_.AddConnection(std::move(*s)); } diff --git a/src/communication/session.hpp b/src/communication/session.hpp index c662a0fe5..e24d20751 100644 --- a/src/communication/session.hpp +++ b/src/communication/session.hpp @@ -35,22 +35,17 @@ using InputStream = Buffer::ReadEnd; */ class OutputStream final { public: - OutputStream( - std::function write_function) - : write_function_(write_function) {} + OutputStream(std::function write_function) : write_function_(write_function) {} OutputStream(const OutputStream &) = delete; OutputStream(OutputStream &&) = delete; OutputStream &operator=(const OutputStream &) = delete; OutputStream &operator=(OutputStream &&) = delete; - bool Write(const uint8_t *data, size_t len, bool have_more = false) { - return write_function_(data, len, have_more); - } + bool Write(const uint8_t *data, size_t len, bool have_more = false) { return write_function_(data, len, have_more); } bool Write(const std::string &str, bool have_more = false) { - return Write(reinterpret_cast(str.data()), str.size(), - have_more); + return Write(reinterpret_cast(str.data()), str.size(), have_more); } private: @@ -65,14 +60,10 @@ class OutputStream final { template class Session final { public: - Session(io::network::Socket &&socket, TSessionData *data, - ServerContext *context, int inactivity_timeout_sec) + Session(io::network::Socket &&socket, TSessionData *data, ServerContext *context, int inactivity_timeout_sec) : socket_(std::move(socket)), - output_stream_([this](const uint8_t *data, size_t len, bool have_more) { - return Write(data, len, have_more); - }), - session_(data, socket_.endpoint(), input_buffer_.read_end(), - &output_stream_), + output_stream_([this](const uint8_t *data, size_t len, bool have_more) { return Write(data, len, have_more); }), + session_(data, socket_.endpoint(), input_buffer_.read_end(), &output_stream_), inactivity_timeout_sec_(inactivity_timeout_sec) { // Set socket options. // The socket is set to be a non-blocking socket. We use the socket in a @@ -243,8 +234,7 @@ class Session final { bool TimedOut() { std::unique_lock guard(lock_); if (execution_active_) return false; - return last_event_time_ + std::chrono::seconds(inactivity_timeout_sec_) < - std::chrono::steady_clock::now(); + return last_event_time_ + std::chrono::seconds(inactivity_timeout_sec_) < std::chrono::steady_clock::now(); } /** @@ -316,8 +306,7 @@ class Session final { TSession session_; // Time of the last event and associated lock. - std::chrono::time_point last_event_time_{ - std::chrono::steady_clock::now()}; + std::chrono::time_point last_event_time_{std::chrono::steady_clock::now()}; bool execution_active_{false}; utils::SpinLock lock_; const int inactivity_timeout_sec_; diff --git a/src/data_structures/ring_buffer.hpp b/src/data_structures/ring_buffer.hpp index 0127d28d8..44caddd8c 100644 --- a/src/data_structures/ring_buffer.hpp +++ b/src/data_structures/ring_buffer.hpp @@ -20,9 +20,7 @@ template class RingBuffer { public: - explicit RingBuffer(int capacity) : capacity_(capacity) { - buffer_ = std::make_unique(capacity_); - } + explicit RingBuffer(int capacity) : capacity_(capacity) { buffer_ = std::make_unique(capacity_); } RingBuffer(const RingBuffer &) = delete; RingBuffer(RingBuffer &&) = delete; diff --git a/src/glue/communication.cpp b/src/glue/communication.cpp index e817cd927..464b5237c 100644 --- a/src/glue/communication.cpp +++ b/src/glue/communication.cpp @@ -32,34 +32,28 @@ query::TypedValue ToTypedValue(const Value &value) { } case Value::Type::Map: { std::map map; - for (const auto &kv : value.ValueMap()) - map.emplace(kv.first, ToTypedValue(kv.second)); + for (const auto &kv : value.ValueMap()) map.emplace(kv.first, ToTypedValue(kv.second)); return query::TypedValue(std::move(map)); } case Value::Type::Vertex: case Value::Type::Edge: case Value::Type::UnboundedEdge: case Value::Type::Path: - throw communication::bolt::ValueException( - "Unsupported conversion from Value to TypedValue"); + throw communication::bolt::ValueException("Unsupported conversion from Value to TypedValue"); } } -storage::Result ToBoltVertex( - const query::VertexAccessor &vertex, const storage::Storage &db, - storage::View view) { +storage::Result ToBoltVertex(const query::VertexAccessor &vertex, + const storage::Storage &db, storage::View view) { return ToBoltVertex(vertex.impl_, db, view); } -storage::Result ToBoltEdge( - const query::EdgeAccessor &edge, const storage::Storage &db, - storage::View view) { +storage::Result ToBoltEdge(const query::EdgeAccessor &edge, const storage::Storage &db, + storage::View view) { return ToBoltEdge(edge.impl_, db, view); } -storage::Result ToBoltValue(const query::TypedValue &value, - const storage::Storage &db, - storage::View view) { +storage::Result ToBoltValue(const query::TypedValue &value, const storage::Storage &db, storage::View view) { switch (value.type()) { case query::TypedValue::Type::Null: return Value(); @@ -90,20 +84,17 @@ storage::Result ToBoltValue(const query::TypedValue &value, } return Value(std::move(map)); } - case query::TypedValue::Type::Vertex: - { + case query::TypedValue::Type::Vertex: { auto maybe_vertex = ToBoltVertex(value.ValueVertex(), db, view); if (maybe_vertex.HasError()) return maybe_vertex.GetError(); return Value(std::move(*maybe_vertex)); } - case query::TypedValue::Type::Edge: - { + case query::TypedValue::Type::Edge: { auto maybe_edge = ToBoltEdge(value.ValueEdge(), db, view); if (maybe_edge.HasError()) return maybe_edge.GetError(); return Value(std::move(*maybe_edge)); } - case query::TypedValue::Type::Path: - { + case query::TypedValue::Type::Path: { auto maybe_path = ToBoltPath(value.ValuePath(), db, view); if (maybe_path.HasError()) return maybe_path.GetError(); return Value(std::move(*maybe_path)); @@ -111,9 +102,8 @@ storage::Result ToBoltValue(const query::TypedValue &value, } } -storage::Result ToBoltVertex( - const storage::VertexAccessor &vertex, const storage::Storage &db, - storage::View view) { +storage::Result ToBoltVertex(const storage::VertexAccessor &vertex, + const storage::Storage &db, storage::View view) { auto id = communication::bolt::Id::FromUint(vertex.Gid().AsUint()); auto maybe_labels = vertex.Labels(view); if (maybe_labels.HasError()) return maybe_labels.GetError(); @@ -131,12 +121,10 @@ storage::Result ToBoltVertex( return communication::bolt::Vertex{id, labels, properties}; } -storage::Result ToBoltEdge( - const storage::EdgeAccessor &edge, const storage::Storage &db, - storage::View view) { +storage::Result ToBoltEdge(const storage::EdgeAccessor &edge, const storage::Storage &db, + storage::View view) { auto id = communication::bolt::Id::FromUint(edge.Gid().AsUint()); - auto from = - communication::bolt::Id::FromUint(edge.FromVertex().Gid().AsUint()); + auto from = communication::bolt::Id::FromUint(edge.FromVertex().Gid().AsUint()); auto to = communication::bolt::Id::FromUint(edge.ToVertex().Gid().AsUint()); auto type = db.EdgeTypeToName(edge.EdgeType()); auto maybe_properties = edge.Properties(view); @@ -148,8 +136,8 @@ storage::Result ToBoltEdge( return communication::bolt::Edge{id, from, to, type, properties}; } -storage::Result ToBoltPath( - const query::Path &path, const storage::Storage &db, storage::View view) { +storage::Result ToBoltPath(const query::Path &path, const storage::Storage &db, + storage::View view) { std::vector vertices; vertices.reserve(path.vertices().size()); for (const auto &v : path.vertices()) { @@ -182,22 +170,19 @@ storage::PropertyValue ToPropertyValue(const Value &value) { case Value::Type::List: { std::vector vec; vec.reserve(value.ValueList().size()); - for (const auto &value : value.ValueList()) - vec.emplace_back(ToPropertyValue(value)); + for (const auto &value : value.ValueList()) vec.emplace_back(ToPropertyValue(value)); return storage::PropertyValue(std::move(vec)); } case Value::Type::Map: { std::map map; - for (const auto &kv : value.ValueMap()) - map.emplace(kv.first, ToPropertyValue(kv.second)); + for (const auto &kv : value.ValueMap()) map.emplace(kv.first, ToPropertyValue(kv.second)); return storage::PropertyValue(std::move(map)); } case Value::Type::Vertex: case Value::Type::Edge: case Value::Type::UnboundedEdge: case Value::Type::Path: - throw communication::bolt::ValueException( - "Unsupported conversion from Value to PropertyValue"); + throw communication::bolt::ValueException("Unsupported conversion from Value to PropertyValue"); } } diff --git a/src/glue/communication.hpp b/src/glue/communication.hpp index 80fd39cc8..6fb91aa83 100644 --- a/src/glue/communication.hpp +++ b/src/glue/communication.hpp @@ -21,35 +21,32 @@ namespace glue { /// @param storage::View for deciding which vertex attributes are visible. /// /// @throw std::bad_alloc -storage::Result ToBoltVertex( - const storage::VertexAccessor &vertex, const storage::Storage &db, - storage::View view); +storage::Result ToBoltVertex(const storage::VertexAccessor &vertex, + const storage::Storage &db, storage::View view); /// @param storage::EdgeAccessor for converting to communication::bolt::Edge. /// @param storage::Storage for getting edge type and property names. /// @param storage::View for deciding which edge attributes are visible. /// /// @throw std::bad_alloc -storage::Result ToBoltEdge( - const storage::EdgeAccessor &edge, const storage::Storage &db, - storage::View view); +storage::Result ToBoltEdge(const storage::EdgeAccessor &edge, const storage::Storage &db, + storage::View view); /// @param query::Path for converting to communication::bolt::Path. /// @param storage::Storage for ToBoltVertex and ToBoltEdge. /// @param storage::View for ToBoltVertex and ToBoltEdge. /// /// @throw std::bad_alloc -storage::Result ToBoltPath( - const query::Path &path, const storage::Storage &db, storage::View view); +storage::Result ToBoltPath(const query::Path &path, const storage::Storage &db, + storage::View view); /// @param query::TypedValue for converting to communication::bolt::Value. /// @param storage::Storage for ToBoltVertex and ToBoltEdge. /// @param storage::View for ToBoltVertex and ToBoltEdge. /// /// @throw std::bad_alloc -storage::Result ToBoltValue( - const query::TypedValue &value, const storage::Storage &db, - storage::View view); +storage::Result ToBoltValue(const query::TypedValue &value, const storage::Storage &db, + storage::View view); query::TypedValue ToTypedValue(const communication::bolt::Value &value); diff --git a/src/helpers.hpp b/src/helpers.hpp index fe6bf0d36..64b5fd71b 100644 --- a/src/helpers.hpp +++ b/src/helpers.hpp @@ -17,17 +17,13 @@ inline void LoadConfig(const std::string &product_name) { namespace fs = std::filesystem; std::vector configs = {fs::path("/etc/memgraph/memgraph.conf")}; - if (getenv("HOME") != nullptr) - configs.emplace_back(fs::path(getenv("HOME")) / - fs::path(".memgraph/config")); + if (getenv("HOME") != nullptr) configs.emplace_back(fs::path(getenv("HOME")) / fs::path(".memgraph/config")); { auto memgraph_config = getenv("MEMGRAPH_CONFIG"); if (memgraph_config != nullptr) { auto path = fs::path(memgraph_config); - MG_ASSERT( - fs::exists(path), - "MEMGRAPH_CONFIG environment variable set to nonexisting path: {}", - path.generic_string()); + MG_ASSERT(fs::exists(path), "MEMGRAPH_CONFIG environment variable set to nonexisting path: {}", + path.generic_string()); configs.emplace_back(path); } } @@ -35,8 +31,7 @@ inline void LoadConfig(const std::string &product_name) { std::vector flagfile_arguments; for (const auto &config : configs) if (fs::exists(config)) { - flagfile_arguments.emplace_back( - std::string("--flag-file=" + config.generic_string())); + flagfile_arguments.emplace_back(std::string("--flag-file=" + config.generic_string())); } int custom_argc = static_cast(flagfile_arguments.size()) + 1; diff --git a/src/io/network/endpoint.cpp b/src/io/network/endpoint.cpp index 962b50d80..b18f389fa 100644 --- a/src/io/network/endpoint.cpp +++ b/src/io/network/endpoint.cpp @@ -25,10 +25,8 @@ Endpoint::IpFamily Endpoint::GetIpFamily(const std::string &ip_address) { } } -std::optional> -Endpoint::ParseSocketOrIpAddress( - const std::string &address, - const std::optional default_port = {}) { +std::optional> Endpoint::ParseSocketOrIpAddress( + const std::string &address, const std::optional default_port = {}) { /// expected address format: /// - "ip_address:port_number" /// - "ip_address" @@ -80,8 +78,7 @@ std::string Endpoint::SocketAddress() const { } Endpoint::Endpoint() {} -Endpoint::Endpoint(std::string ip_address, uint16_t port) - : address(std::move(ip_address)), port(port) { +Endpoint::Endpoint(std::string ip_address, uint16_t port) : address(std::move(ip_address)), port(port) { IpFamily ip_family = GetIpFamily(address); if (ip_family == IpFamily::NONE) { throw NetworkError("Not a valid IPv4 or IPv6 address: {}", ip_address); diff --git a/src/io/network/epoll.hpp b/src/io/network/epoll.hpp index 70477e8ed..1da6278f9 100644 --- a/src/io/network/epoll.hpp +++ b/src/io/network/epoll.hpp @@ -21,13 +21,11 @@ class Epoll { public: using Event = struct epoll_event; - Epoll(bool set_cloexec = false) - : epoll_fd_(epoll_create1(set_cloexec ? EPOLL_CLOEXEC : 0)) { + Epoll(bool set_cloexec = false) : epoll_fd_(epoll_create1(set_cloexec ? EPOLL_CLOEXEC : 0)) { // epoll_create1 returns an error if there is a logical error in our code // (for example invalid flags) or if there is irrecoverable error. In both // cases it is best to terminate. - MG_ASSERT(epoll_fd_ != -1, "Error on epoll create: ({}) {}", errno, - strerror(errno)); + MG_ASSERT(epoll_fd_ != -1, "Error on epoll create: ({}) {}", errno, strerror(errno)); } /** @@ -42,15 +40,13 @@ class Epoll { Event event; event.events = events; event.data.ptr = ptr; - int status = epoll_ctl(epoll_fd_, (modify ? EPOLL_CTL_MOD : EPOLL_CTL_ADD), - fd, &event); + int status = epoll_ctl(epoll_fd_, (modify ? EPOLL_CTL_MOD : EPOLL_CTL_ADD), fd, &event); // epoll_ctl can return an error on our logical error or on irrecoverable // error. There is a third possibility that some system limit is reached. In // that case we could return an erorr and close connection. Chances of // reaching system limit in normally working memgraph is extremely unlikely, // so it is correct to terminate even in that case. - MG_ASSERT(!status, "Error on epoll {}: ({}) {}", - (modify ? "modify" : "add"), errno, strerror(errno)); + MG_ASSERT(!status, "Error on epoll {}: ({}) {}", (modify ? "modify" : "add"), errno, strerror(errno)); } /** @@ -60,9 +56,7 @@ class Epoll { * @param events epoll events mask * @param ptr pointer to the associated event handler */ - void Modify(int fd, uint32_t events, void *ptr) { - Add(fd, events, ptr, true); - } + void Modify(int fd, uint32_t events, void *ptr) { Add(fd, events, ptr, true); } /** * This function deletes a file descriptor that is listened for events. @@ -76,8 +70,7 @@ class Epoll { // that case we could return an erorr and close connection. Chances of // reaching system limit in normally working memgraph is extremely unlikely, // so it is correct to terminate even in that case. - MG_ASSERT(!status, "Error on epoll delete: ({}) {}", errno, - strerror(errno)); + MG_ASSERT(!status, "Error on epoll delete: ({}) {}", errno, strerror(errno)); } /** @@ -91,8 +84,7 @@ class Epoll { int Wait(Event *events, int max_events, int timeout) { auto num_events = epoll_wait(epoll_fd_, events, max_events, timeout); // If this check fails there was logical error in our code. - MG_ASSERT(num_events != -1 || errno == EINTR, - "Error on epoll wait: ({}) {}", errno, strerror(errno)); + MG_ASSERT(num_events != -1 || errno == EINTR, "Error on epoll wait: ({}) {}", errno, strerror(errno)); // num_events can be -1 if errno was EINTR (epoll_wait interrupted by signal // handler). We treat that as no events, so we return 0. return num_events == -1 ? 0 : num_events; diff --git a/src/io/network/network_error.hpp b/src/io/network/network_error.hpp index 640a65b01..1225d3403 100644 --- a/src/io/network/network_error.hpp +++ b/src/io/network/network_error.hpp @@ -8,4 +8,4 @@ class NetworkError : public utils::StacktraceException { public: using utils::StacktraceException::StacktraceException; }; -} +} // namespace io::network diff --git a/src/io/network/socket.cpp b/src/io/network/socket.cpp index f0656d9ea..db5376c33 100644 --- a/src/io/network/socket.cpp +++ b/src/io/network/socket.cpp @@ -59,8 +59,7 @@ bool Socket::IsOpen() const { return socket_ != -1; } bool Socket::Connect(const Endpoint &endpoint) { if (socket_ != -1) return false; - auto info = AddrInfo::Get(endpoint.address.c_str(), - std::to_string(endpoint.port).c_str()); + auto info = AddrInfo::Get(endpoint.address.c_str(), std::to_string(endpoint.port).c_str()); for (struct addrinfo *it = info; it != nullptr; it = it->ai_next) { int sfd = socket(it->ai_family, it->ai_socktype, it->ai_protocol); @@ -83,8 +82,7 @@ bool Socket::Connect(const Endpoint &endpoint) { bool Socket::Bind(const Endpoint &endpoint) { if (socket_ != -1) return false; - auto info = AddrInfo::Get(endpoint.address.c_str(), - std::to_string(endpoint.port).c_str()); + auto info = AddrInfo::Get(endpoint.address.c_str(), std::to_string(endpoint.port).c_str()); for (struct addrinfo *it = info; it != nullptr; it = it->ai_next) { int sfd = socket(it->ai_family, it->ai_socktype, it->ai_protocol); @@ -130,38 +128,30 @@ void Socket::SetNonBlocking() { int flags = fcntl(socket_, F_GETFL, 0); MG_ASSERT(flags != -1, "Can't get socket mode"); flags |= O_NONBLOCK; - MG_ASSERT(fcntl(socket_, F_SETFL, flags) != -1, - "Can't set socket nonblocking"); + MG_ASSERT(fcntl(socket_, F_SETFL, flags) != -1, "Can't set socket nonblocking"); } void Socket::SetKeepAlive() { int optval = 1; socklen_t optlen = sizeof(optval); - MG_ASSERT(!setsockopt(socket_, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen), - "Can't set socket keep alive"); + MG_ASSERT(!setsockopt(socket_, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen), "Can't set socket keep alive"); optval = 20; // wait 20s before sending keep-alive packets - MG_ASSERT( - !setsockopt(socket_, SOL_TCP, TCP_KEEPIDLE, (void *)&optval, optlen), - "Can't set socket keep alive"); + MG_ASSERT(!setsockopt(socket_, SOL_TCP, TCP_KEEPIDLE, (void *)&optval, optlen), "Can't set socket keep alive"); optval = 4; // 4 keep-alive packets must fail to close - MG_ASSERT(!setsockopt(socket_, SOL_TCP, TCP_KEEPCNT, (void *)&optval, optlen), - "Can't set socket keep alive"); + MG_ASSERT(!setsockopt(socket_, SOL_TCP, TCP_KEEPCNT, (void *)&optval, optlen), "Can't set socket keep alive"); optval = 15; // send keep-alive packets every 15s - MG_ASSERT( - !setsockopt(socket_, SOL_TCP, TCP_KEEPINTVL, (void *)&optval, optlen), - "Can't set socket keep alive"); + MG_ASSERT(!setsockopt(socket_, SOL_TCP, TCP_KEEPINTVL, (void *)&optval, optlen), "Can't set socket keep alive"); } void Socket::SetNoDelay() { int optval = 1; socklen_t optlen = sizeof(optval); - MG_ASSERT(!setsockopt(socket_, SOL_TCP, TCP_NODELAY, (void *)&optval, optlen), - "Can't set socket no delay"); + MG_ASSERT(!setsockopt(socket_, SOL_TCP, TCP_NODELAY, (void *)&optval, optlen), "Can't set socket no delay"); } void Socket::SetTimeout(long sec, long usec) { @@ -169,11 +159,9 @@ void Socket::SetTimeout(long sec, long usec) { tv.tv_sec = sec; tv.tv_usec = usec; - MG_ASSERT(!setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)), - "Can't set socket timeout"); + MG_ASSERT(!setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)), "Can't set socket timeout"); - MG_ASSERT(!setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)), - "Can't set socket timeout"); + MG_ASSERT(!setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)), "Can't set socket timeout"); } int Socket::ErrorStatus() const { @@ -238,8 +226,7 @@ bool Socket::Write(const uint8_t *data, size_t len, bool have_more) { } bool Socket::Write(const std::string &s, bool have_more) { - return Write(reinterpret_cast(s.data()), s.size(), - have_more); + return Write(reinterpret_cast(s.data()), s.size(), have_more); } ssize_t Socket::Read(void *buffer, size_t len, bool nonblock) { diff --git a/src/io/network/stream_buffer.hpp b/src/io/network/stream_buffer.hpp index 95bdd64fa..95e0e4244 100644 --- a/src/io/network/stream_buffer.hpp +++ b/src/io/network/stream_buffer.hpp @@ -14,4 +14,4 @@ struct StreamBuffer { uint8_t *data; size_t len; }; -} +} // namespace io::network diff --git a/src/io/network/utils.cpp b/src/io/network/utils.cpp index 7de0c06e2..43f1ec1fc 100644 --- a/src/io/network/utils.cpp +++ b/src/io/network/utils.cpp @@ -23,9 +23,8 @@ std::string ResolveHostname(std::string hostname) { int addr_result; addrinfo *servinfo; - MG_ASSERT((addr_result = - getaddrinfo(hostname.c_str(), NULL, &hints, &servinfo)) == 0, - "Error with getaddrinfo: {}", gai_strerror(addr_result)); + MG_ASSERT((addr_result = getaddrinfo(hostname.c_str(), NULL, &hints, &servinfo)) == 0, "Error with getaddrinfo: {}", + gai_strerror(addr_result)); MG_ASSERT(servinfo, "Could not resolve address: {}", hostname); std::string address; diff --git a/src/kvstore/kvstore.cpp b/src/kvstore/kvstore.cpp index d350bef09..e5b63310b 100644 --- a/src/kvstore/kvstore.cpp +++ b/src/kvstore/kvstore.cpp @@ -12,18 +12,16 @@ struct KVStore::impl { rocksdb::Options options; }; -KVStore::KVStore(std::filesystem::path storage) - : pimpl_(std::make_unique()) { +KVStore::KVStore(std::filesystem::path storage) : pimpl_(std::make_unique()) { pimpl_->storage = storage; if (!utils::EnsureDir(pimpl_->storage)) - throw KVStoreError("Folder for the key-value store " + - pimpl_->storage.string() + " couldn't be initialized!"); + throw KVStoreError("Folder for the key-value store " + pimpl_->storage.string() + " couldn't be initialized!"); pimpl_->options.create_if_missing = true; rocksdb::DB *db = nullptr; auto s = rocksdb::DB::Open(pimpl_->options, storage.c_str(), &db); if (!s.ok()) - throw KVStoreError("RocksDB couldn't be initialized inside " + - storage.string() + " -- " + std::string(s.ToString())); + throw KVStoreError("RocksDB couldn't be initialized inside " + storage.string() + " -- " + + std::string(s.ToString())); pimpl_->db.reset(db); } @@ -72,19 +70,16 @@ bool KVStore::DeleteMultiple(const std::vector &keys) { } bool KVStore::DeletePrefix(const std::string &prefix) { - std::unique_ptr iter = std::unique_ptr( - pimpl_->db->NewIterator(rocksdb::ReadOptions())); - for (iter->Seek(prefix); iter->Valid() && iter->key().starts_with(prefix); - iter->Next()) { - if (!pimpl_->db->Delete(rocksdb::WriteOptions(), iter->key()).ok()) - return false; + std::unique_ptr iter = + std::unique_ptr(pimpl_->db->NewIterator(rocksdb::ReadOptions())); + for (iter->Seek(prefix); iter->Valid() && iter->key().starts_with(prefix); iter->Next()) { + if (!pimpl_->db->Delete(rocksdb::WriteOptions(), iter->key()).ok()) return false; } return true; } -bool KVStore::PutAndDeleteMultiple( - const std::map &items, - const std::vector &keys) { +bool KVStore::PutAndDeleteMultiple(const std::map &items, + const std::vector &keys) { rocksdb::WriteBatch batch; for (const auto &item : items) { batch.Put(item.first, item.second); @@ -105,22 +100,16 @@ struct KVStore::iterator::impl { std::pair disk_prop; }; -KVStore::iterator::iterator(const KVStore *kvstore, const std::string &prefix, - bool at_end) +KVStore::iterator::iterator(const KVStore *kvstore, const std::string &prefix, bool at_end) : pimpl_(std::make_unique()) { pimpl_->kvstore = kvstore; pimpl_->prefix = prefix; - pimpl_->it = std::unique_ptr( - pimpl_->kvstore->pimpl_->db->NewIterator(rocksdb::ReadOptions())); + pimpl_->it = std::unique_ptr(pimpl_->kvstore->pimpl_->db->NewIterator(rocksdb::ReadOptions())); pimpl_->it->Seek(pimpl_->prefix); - if (!pimpl_->it->Valid() || !pimpl_->it->key().starts_with(pimpl_->prefix) || - at_end) - pimpl_->it = nullptr; + if (!pimpl_->it->Valid() || !pimpl_->it->key().starts_with(pimpl_->prefix) || at_end) pimpl_->it = nullptr; } -KVStore::iterator::iterator(KVStore::iterator &&other) { - pimpl_ = std::move(other.pimpl_); -} +KVStore::iterator::iterator(KVStore::iterator &&other) { pimpl_ = std::move(other.pimpl_); } KVStore::iterator::~iterator() {} @@ -131,24 +120,19 @@ KVStore::iterator &KVStore::iterator::operator=(KVStore::iterator &&other) { KVStore::iterator &KVStore::iterator::operator++() { pimpl_->it->Next(); - if (!pimpl_->it->Valid() || !pimpl_->it->key().starts_with(pimpl_->prefix)) - pimpl_->it = nullptr; + if (!pimpl_->it->Valid() || !pimpl_->it->key().starts_with(pimpl_->prefix)) pimpl_->it = nullptr; return *this; } bool KVStore::iterator::operator==(const iterator &other) const { - return pimpl_->kvstore == other.pimpl_->kvstore && - pimpl_->prefix == other.pimpl_->prefix && + return pimpl_->kvstore == other.pimpl_->kvstore && pimpl_->prefix == other.pimpl_->prefix && pimpl_->it == other.pimpl_->it; } -bool KVStore::iterator::operator!=(const iterator &other) const { - return !(*this == other); -} +bool KVStore::iterator::operator!=(const iterator &other) const { return !(*this == other); } KVStore::iterator::reference KVStore::iterator::operator*() { - pimpl_->disk_prop = {pimpl_->it->key().ToString(), - pimpl_->it->value().ToString()}; + pimpl_->disk_prop = {pimpl_->it->key().ToString(), pimpl_->it->value().ToString()}; return pimpl_->disk_prop; } @@ -166,8 +150,7 @@ size_t KVStore::Size(const std::string &prefix) { return size; } -bool KVStore::CompactRange(const std::string &begin_prefix, - const std::string &end_prefix) { +bool KVStore::CompactRange(const std::string &begin_prefix, const std::string &end_prefix) { rocksdb::CompactRangeOptions options; rocksdb::Slice begin(begin_prefix); rocksdb::Slice end(end_prefix); diff --git a/src/kvstore/kvstore.hpp b/src/kvstore/kvstore.hpp index c2453d4e7..f51460a96 100644 --- a/src/kvstore/kvstore.hpp +++ b/src/kvstore/kvstore.hpp @@ -114,8 +114,7 @@ class KVStore final { * @return true if the items have been successfully stored and deleted. * In case of any error false is going to be returned. */ - bool PutAndDeleteMultiple(const std::map &items, - const std::vector &keys); + bool PutAndDeleteMultiple(const std::map &items, const std::vector &keys); /** * Returns total number of stored (key, value) pairs. The function takes an @@ -140,8 +139,7 @@ class KVStore final { * * @return - true if the compaction finished successfully, false otherwise. */ - bool CompactRange(const std::string &begin_prefix, - const std::string &end_prefix); + bool CompactRange(const std::string &begin_prefix, const std::string &end_prefix); /** * Custom prefix-based iterator over kvstore. @@ -150,17 +148,14 @@ class KVStore final { * and behaves as if all of those pairs are stored in a single iterable * collection of std::pair. */ - class iterator final - : public std::iterator< - std::input_iterator_tag, // iterator_category - std::pair, // value_type - long, // difference_type - const std::pair *, // pointer - const std::pair & // reference - > { + class iterator final : public std::iterator, // value_type + long, // difference_type + const std::pair *, // pointer + const std::pair & // reference + > { public: - explicit iterator(const KVStore *kvstore, const std::string &prefix = "", - bool at_end = false); + explicit iterator(const KVStore *kvstore, const std::string &prefix = "", bool at_end = false); iterator(const iterator &other) = delete; @@ -191,13 +186,9 @@ class KVStore final { std::unique_ptr pimpl_; }; - iterator begin(const std::string &prefix = "") { - return iterator(this, prefix); - } + iterator begin(const std::string &prefix = "") { return iterator(this, prefix); } - iterator end(const std::string &prefix = "") { - return iterator(this, prefix, true); - } + iterator end(const std::string &prefix = "") { return iterator(this, prefix, true); } private: struct impl; diff --git a/src/kvstore/kvstore_dummy.cpp b/src/kvstore/kvstore_dummy.cpp index 6a17ab455..6acb3cbe2 100644 --- a/src/kvstore/kvstore_dummy.cpp +++ b/src/kvstore/kvstore_dummy.cpp @@ -26,8 +26,7 @@ std::optional KVStore::Get(const std::string &key) const noexcept { } bool KVStore::Delete(const std::string &key) { - LOG_FATAL( - "Unsupported operation (KVStore::Delete) -- this is a dummy kvstore"); + LOG_FATAL("Unsupported operation (KVStore::Delete) -- this is a dummy kvstore"); } bool KVStore::DeleteMultiple(const std::vector &keys) { @@ -42,9 +41,8 @@ bool KVStore::DeletePrefix(const std::string &prefix) { "dummy kvstore"); } -bool KVStore::PutAndDeleteMultiple( - const std::map &items, - const std::vector &keys) { +bool KVStore::PutAndDeleteMultiple(const std::map &items, + const std::vector &keys) { LOG_FATAL( "Unsupported operation (KVStore::PutAndDeleteMultiple) -- this is a " "dummy kvstore"); @@ -54,13 +52,9 @@ bool KVStore::PutAndDeleteMultiple( struct KVStore::iterator::impl {}; -KVStore::iterator::iterator(const KVStore *kvstore, const std::string &prefix, - bool at_end) - : pimpl_(new impl()) {} +KVStore::iterator::iterator(const KVStore *kvstore, const std::string &prefix, bool at_end) : pimpl_(new impl()) {} -KVStore::iterator::iterator(KVStore::iterator &&other) { - pimpl_ = std::move(other.pimpl_); -} +KVStore::iterator::iterator(KVStore::iterator &&other) { pimpl_ = std::move(other.pimpl_); } KVStore::iterator::~iterator() {} @@ -77,9 +71,7 @@ KVStore::iterator &KVStore::iterator::operator++() { bool KVStore::iterator::operator==(const iterator &other) const { return true; } -bool KVStore::iterator::operator!=(const iterator &other) const { - return false; -} +bool KVStore::iterator::operator!=(const iterator &other) const { return false; } KVStore::iterator::reference KVStore::iterator::operator*() { LOG_FATAL( @@ -99,8 +91,7 @@ bool KVStore::iterator::IsValid() { return false; } size_t KVStore::Size(const std::string &prefix) { return 0; } -bool KVStore::CompactRange(const std::string &begin_prefix, - const std::string &end_prefix) { +bool KVStore::CompactRange(const std::string &begin_prefix, const std::string &end_prefix) { LOG_FATAL( "Unsupported operation (KVStore::Compact) -- this is a " "dummy kvstore"); diff --git a/src/memgraph.cpp b/src/memgraph.cpp index a32eb3390..657678e16 100644 --- a/src/memgraph.cpp +++ b/src/memgraph.cpp @@ -63,25 +63,19 @@ #endif // Bolt server flags. -DEFINE_string(bolt_address, "0.0.0.0", - "IP address on which the Bolt server should listen."); -DEFINE_VALIDATED_int32(bolt_port, 7687, - "Port on which the Bolt server should listen.", +DEFINE_string(bolt_address, "0.0.0.0", "IP address on which the Bolt server should listen."); +DEFINE_VALIDATED_int32(bolt_port, 7687, "Port on which the Bolt server should listen.", FLAG_IN_RANGE(0, std::numeric_limits::max())); -DEFINE_VALIDATED_int32( - bolt_num_workers, std::max(std::thread::hardware_concurrency(), 1U), - "Number of workers used by the Bolt server. By default, this will be the " - "number of processing units available on the machine.", - FLAG_IN_RANGE(1, INT32_MAX)); -DEFINE_VALIDATED_int32( - bolt_session_inactivity_timeout, 1800, - "Time in seconds after which inactive Bolt sessions will be " - "closed.", - FLAG_IN_RANGE(1, INT32_MAX)); -DEFINE_string(bolt_cert_file, "", - "Certificate file which should be used for the Bolt server."); -DEFINE_string(bolt_key_file, "", - "Key file which should be used for the Bolt server."); +DEFINE_VALIDATED_int32(bolt_num_workers, std::max(std::thread::hardware_concurrency(), 1U), + "Number of workers used by the Bolt server. By default, this will be the " + "number of processing units available on the machine.", + FLAG_IN_RANGE(1, INT32_MAX)); +DEFINE_VALIDATED_int32(bolt_session_inactivity_timeout, 1800, + "Time in seconds after which inactive Bolt sessions will be " + "closed.", + FLAG_IN_RANGE(1, INT32_MAX)); +DEFINE_string(bolt_cert_file, "", "Certificate file which should be used for the Bolt server."); +DEFINE_string(bolt_key_file, "", "Key file which should be used for the Bolt server."); DEFINE_string(bolt_server_name_for_init, "", "Server name which the database should send to the client in the " "Bolt INIT message."); @@ -89,26 +83,20 @@ DEFINE_string(bolt_server_name_for_init, "", // General purpose flags. // NOTE: The `data_directory` flag must be the same here and in // `mg_import_csv`. If you change it, make sure to change it there as well. -DEFINE_string(data_directory, "mg_data", - "Path to directory in which to save all permanent data."); -DEFINE_HIDDEN_string( - log_link_basename, "", - "Basename used for symlink creation to the last log file."); +DEFINE_string(data_directory, "mg_data", "Path to directory in which to save all permanent data."); +DEFINE_HIDDEN_string(log_link_basename, "", "Basename used for symlink creation to the last log file."); DEFINE_uint64(memory_warning_threshold, 1024, "Memory warning threshold, in MB. If Memgraph detects there is " "less available RAM it will log a warning. Set to 0 to " "disable."); // Storage flags. -DEFINE_VALIDATED_uint64(storage_gc_cycle_sec, 30, - "Storage garbage collector interval (in seconds).", +DEFINE_VALIDATED_uint64(storage_gc_cycle_sec, 30, "Storage garbage collector interval (in seconds).", FLAG_IN_RANGE(1, 24 * 3600)); // NOTE: The `storage_properties_on_edges` flag must be the same here and in // `mg_import_csv`. If you change it, make sure to change it there as well. -DEFINE_bool(storage_properties_on_edges, false, - "Controls whether edges have properties."); -DEFINE_bool(storage_recover_on_startup, false, - "Controls whether the storage recovers persisted data on startup."); +DEFINE_bool(storage_properties_on_edges, false, "Controls whether edges have properties."); +DEFINE_bool(storage_recover_on_startup, false, "Controls whether the storage recovers persisted data on startup."); DEFINE_VALIDATED_uint64(storage_snapshot_interval_sec, 0, "Storage snapshot creation interval (in seconds). Set " "to 0 to disable periodic snapshot creation.", @@ -116,21 +104,15 @@ DEFINE_VALIDATED_uint64(storage_snapshot_interval_sec, 0, DEFINE_bool(storage_wal_enabled, false, "Controls whether the storage uses write-ahead-logging. To enable " "WAL periodic snapshots must be enabled."); -DEFINE_VALIDATED_uint64(storage_snapshot_retention_count, 3, - "The number of snapshots that should always be kept.", +DEFINE_VALIDATED_uint64(storage_snapshot_retention_count, 3, "The number of snapshots that should always be kept.", FLAG_IN_RANGE(1, 1000000)); -DEFINE_VALIDATED_uint64(storage_wal_file_size_kib, - storage::Config::Durability().wal_file_size_kibibytes, - "Minimum file size of each WAL file.", - FLAG_IN_RANGE(1, 1000 * 1024)); -DEFINE_VALIDATED_uint64( - storage_wal_file_flush_every_n_tx, - storage::Config::Durability().wal_file_flush_every_n_tx, - "Issue a 'fsync' call after this amount of transactions are written to the " - "WAL file. Set to 1 for fully synchronous operation.", - FLAG_IN_RANGE(1, 1000000)); -DEFINE_bool(storage_snapshot_on_exit, false, - "Controls whether the storage creates another snapshot on exit."); +DEFINE_VALIDATED_uint64(storage_wal_file_size_kib, storage::Config::Durability().wal_file_size_kibibytes, + "Minimum file size of each WAL file.", FLAG_IN_RANGE(1, 1000 * 1024)); +DEFINE_VALIDATED_uint64(storage_wal_file_flush_every_n_tx, storage::Config::Durability().wal_file_flush_every_n_tx, + "Issue a 'fsync' call after this amount of transactions are written to the " + "WAL file. Set to 1 for fully synchronous operation.", + FLAG_IN_RANGE(1, 1000000)); +DEFINE_bool(storage_snapshot_on_exit, false, "Controls whether the storage creates another snapshot on exit."); DEFINE_bool(telemetry_enabled, false, "Set to true to enable telemetry. We collect information about the " @@ -141,13 +123,11 @@ DEFINE_bool(telemetry_enabled, false, // Audit logging flags. #ifdef MG_ENTERPRISE DEFINE_bool(audit_enabled, false, "Set to true to enable audit logging."); -DEFINE_VALIDATED_int32(audit_buffer_size, audit::kBufferSizeDefault, - "Maximum number of items in the audit log buffer.", +DEFINE_VALIDATED_int32(audit_buffer_size, audit::kBufferSizeDefault, "Maximum number of items in the audit log buffer.", FLAG_IN_RANGE(1, INT32_MAX)); -DEFINE_VALIDATED_int32( - audit_buffer_flush_interval_ms, audit::kBufferFlushIntervalMillisDefault, - "Interval (in milliseconds) used for flushing the audit log buffer.", - FLAG_IN_RANGE(10, INT32_MAX)); +DEFINE_VALIDATED_int32(audit_buffer_flush_interval_ms, audit::kBufferFlushIntervalMillisDefault, + "Interval (in milliseconds) used for flushing the audit log buffer.", + FLAG_IN_RANGE(10, INT32_MAX)); #endif // Query flags. @@ -155,41 +135,34 @@ DEFINE_uint64(query_execution_timeout_sec, 180, "Maximum allowed query execution time. Queries exceeding this " "limit will be aborted. Value of 0 means no limit."); -DEFINE_VALIDATED_string( - query_modules_directory, "", - "Directory where modules with custom query procedures are stored.", { - if (value.empty()) return true; - if (utils::DirExists(value)) return true; - std::cout << "Expected --" << flagname << " to point to a directory." - << std::endl; - return false; - }); +DEFINE_VALIDATED_string(query_modules_directory, "", "Directory where modules with custom query procedures are stored.", + { + if (value.empty()) return true; + if (utils::DirExists(value)) return true; + std::cout << "Expected --" << flagname << " to point to a directory." << std::endl; + return false; + }); // Logging flags -DEFINE_bool(also_log_to_stderr, false, - "Log messages go to stderr in addition to logfiles"); +DEFINE_bool(also_log_to_stderr, false, "Log messages go to stderr in addition to logfiles"); DEFINE_string(log_file, "", "Path to where the log should be stored."); namespace { constexpr std::array log_level_mappings{ - std::pair{"TRACE", spdlog::level::trace}, - std::pair{"DEBUG", spdlog::level::debug}, - std::pair{"INFO", spdlog::level::info}, - std::pair{"WARNING", spdlog::level::warn}, - std::pair{"ERROR", spdlog::level::err}, - std::pair{"CRITICAL", spdlog::level::critical}}; + std::pair{"TRACE", spdlog::level::trace}, std::pair{"DEBUG", spdlog::level::debug}, + std::pair{"INFO", spdlog::level::info}, std::pair{"WARNING", spdlog::level::warn}, + std::pair{"ERROR", spdlog::level::err}, std::pair{"CRITICAL", spdlog::level::critical}}; std::string GetAllowedLogLevelsString() { std::vector allowed_log_levels; allowed_log_levels.reserve(log_level_mappings.size()); - std::transform(log_level_mappings.cbegin(), log_level_mappings.cend(), - std::back_inserter(allowed_log_levels), + std::transform(log_level_mappings.cbegin(), log_level_mappings.cend(), std::back_inserter(allowed_log_levels), [](const auto &mapping) { return mapping.first; }); return utils::Join(allowed_log_levels, ", "); } -const std::string log_level_help_string = fmt::format( - "Minimum log level. Allowed values: {}", GetAllowedLogLevelsString()); +const std::string log_level_help_string = + fmt::format("Minimum log level. Allowed values: {}", GetAllowedLogLevelsString()); } // namespace DEFINE_VALIDATED_string(log_level, "WARNING", log_level_help_string.c_str(), { @@ -199,11 +172,8 @@ DEFINE_VALIDATED_string(log_level, "WARNING", log_level_help_string.c_str(), { } if (std::find_if(log_level_mappings.cbegin(), log_level_mappings.cend(), - [&](const auto &mapping) { - return mapping.first == value; - }) == log_level_mappings.cend()) { - std::cout << "Invalid value for log level. Allowed values: " - << GetAllowedLogLevelsString() << std::endl; + [&](const auto &mapping) { return mapping.first == value; }) == log_level_mappings.cend()) { + std::cout << "Invalid value for log level. Allowed values: " << GetAllowedLogLevelsString() << std::endl; return false; } @@ -212,9 +182,8 @@ DEFINE_VALIDATED_string(log_level, "WARNING", log_level_help_string.c_str(), { namespace { void ParseLogLevel() { - const auto mapping_iter = std::find_if( - log_level_mappings.cbegin(), log_level_mappings.cend(), - [](const auto &mapping) { return mapping.first == FLAGS_log_level; }); + const auto mapping_iter = std::find_if(log_level_mappings.cbegin(), log_level_mappings.cend(), + [](const auto &mapping) { return mapping.first == FLAGS_log_level; }); MG_ASSERT(mapping_iter != log_level_mappings.cend(), "Invalid log level"); spdlog::set_level(mapping_iter->second); @@ -227,8 +196,7 @@ void ConfigureLogging() { std::vector loggers; if (FLAGS_also_log_to_stderr) { - loggers.emplace_back( - std::make_shared()); + loggers.emplace_back(std::make_shared()); } if (!FLAGS_log_file.empty()) { @@ -240,12 +208,10 @@ void ConfigureLogging() { local_time = localtime(¤t_time); loggers.emplace_back(std::make_shared( - FLAGS_log_file, local_time->tm_hour, local_time->tm_min, false, - log_retention_count)); + FLAGS_log_file, local_time->tm_hour, local_time->tm_min, false, log_retention_count)); } - spdlog::set_default_logger(std::make_shared( - "memgraph_log", loggers.begin(), loggers.end())); + spdlog::set_default_logger(std::make_shared("memgraph_log", loggers.begin(), loggers.end())); spdlog::flush_on(spdlog::level::trace); ParseLogLevel(); @@ -258,13 +224,9 @@ void ConfigureLogging() { struct SessionData { // Explicit constructor here to ensure that pointers to all objects are // supplied. - SessionData(storage::Storage *db, - query::InterpreterContext *interpreter_context, auth::Auth *auth, + SessionData(storage::Storage *db, query::InterpreterContext *interpreter_context, auth::Auth *auth, audit::Log *audit_log) - : db(db), - interpreter_context(interpreter_context), - auth(auth), - audit_log(audit_log) {} + : db(db), interpreter_context(interpreter_context), auth(auth), audit_log(audit_log) {} storage::Storage *db; query::InterpreterContext *interpreter_context; auth::Auth *auth; @@ -274,24 +236,19 @@ struct SessionData { struct SessionData { // Explicit constructor here to ensure that pointers to all objects are // supplied. - SessionData(storage::Storage *db, - query::InterpreterContext *interpreter_context) + SessionData(storage::Storage *db, query::InterpreterContext *interpreter_context) : db(db), interpreter_context(interpreter_context) {} storage::Storage *db; query::InterpreterContext *interpreter_context; }; #endif -class BoltSession final - : public communication::bolt::Session { +class BoltSession final : public communication::bolt::Session { public: - BoltSession(SessionData *data, const io::network::Endpoint &endpoint, - communication::InputStream *input_stream, + BoltSession(SessionData *data, const io::network::Endpoint &endpoint, communication::InputStream *input_stream, communication::OutputStream *output_stream) - : communication::bolt::Session( - input_stream, output_stream), + : communication::bolt::Session(input_stream, + output_stream), db_(data->db), interpreter_(data->interpreter_context), #ifdef MG_ENTERPRISE @@ -301,8 +258,7 @@ class BoltSession final endpoint_(endpoint) { } - using communication::bolt::Session::TEncoder; + using communication::bolt::Session::TEncoder; void BeginTransaction() override { interpreter_.BeginTransaction(); } @@ -311,15 +267,11 @@ class BoltSession final void RollbackTransaction() override { interpreter_.RollbackTransaction(); } std::pair, std::optional> Interpret( - const std::string &query, - const std::map ¶ms) - override { + const std::string &query, const std::map ¶ms) override { std::map params_pv; - for (const auto &kv : params) - params_pv.emplace(kv.first, glue::ToPropertyValue(kv.second)); + for (const auto &kv : params) params_pv.emplace(kv.first, glue::ToPropertyValue(kv.second)); #ifdef MG_ENTERPRISE - audit_log_->Record(endpoint_.address, user_ ? user_->username() : "", query, - storage::PropertyValue(params_pv)); + audit_log_->Record(endpoint_.address, user_ ? user_->username() : "", query, storage::PropertyValue(params_pv)); #endif try { auto result = interpreter_.Prepare(query, params_pv); @@ -327,8 +279,7 @@ class BoltSession final if (user_) { const auto &permissions = user_->GetPermissions(); for (const auto &privilege : result.privileges) { - if (permissions.Has(glue::PrivilegeToPermission(privilege)) != - auth::PermissionLevel::GRANT) { + if (permissions.Has(glue::PrivilegeToPermission(privilege)) != auth::PermissionLevel::GRANT) { interpreter_.Abort(); throw communication::bolt::ClientError( "You are not authorized to execute this query! Please contact " @@ -346,23 +297,20 @@ class BoltSession final } } - std::map Pull( - TEncoder *encoder, std::optional n, - std::optional qid) override { + std::map Pull(TEncoder *encoder, std::optional n, + std::optional qid) override { TypedValueResultStream stream(encoder, db_); return PullResults(stream, n, qid); } - std::map Discard( - std::optional n, std::optional qid) override { + std::map Discard(std::optional n, std::optional qid) override { DiscardValueResultStream stream; return PullResults(stream, n, qid); } void Abort() override { interpreter_.Abort(); } - bool Authenticate(const std::string &username, - const std::string &password) override { + bool Authenticate(const std::string &username, const std::string &password) override { #ifdef MG_ENTERPRISE if (!auth_->HasUsers()) return true; user_ = auth_->Authenticate(username, password); @@ -379,14 +327,13 @@ class BoltSession final private: template - std::map PullResults( - TStream &stream, std::optional n, std::optional qid) { + std::map PullResults(TStream &stream, std::optional n, + std::optional qid) { try { const auto &summary = interpreter_.Pull(&stream, n, qid); std::map decoded_summary; for (const auto &kv : summary) { - auto maybe_value = - glue::ToBoltValue(kv.second, *db_, storage::View::NEW); + auto maybe_value = glue::ToBoltValue(kv.second, *db_, storage::View::NEW); if (maybe_value.HasError()) { switch (maybe_value.GetError()) { case storage::Error::DELETED_OBJECT: @@ -394,8 +341,7 @@ class BoltSession final case storage::Error::VERTEX_HAS_EDGES: case storage::Error::PROPERTIES_DISABLED: case storage::Error::NONEXISTENT_OBJECT: - throw communication::bolt::ClientError( - "Unexpected storage error when streaming summary."); + throw communication::bolt::ClientError("Unexpected storage error when streaming summary."); } } decoded_summary.emplace(kv.first, std::move(*maybe_value)); @@ -412,8 +358,7 @@ class BoltSession final /// before forwarding the calls to original TEncoder. class TypedValueResultStream { public: - TypedValueResultStream(TEncoder *encoder, const storage::Storage *db) - : encoder_(encoder), db_(db) {} + TypedValueResultStream(TEncoder *encoder, const storage::Storage *db) : encoder_(encoder), db_(db) {} void Result(const std::vector &values) { std::vector decoded_values; @@ -423,16 +368,13 @@ class BoltSession final if (maybe_value.HasError()) { switch (maybe_value.GetError()) { case storage::Error::DELETED_OBJECT: - throw communication::bolt::ClientError( - "Returning a deleted object as a result."); + throw communication::bolt::ClientError("Returning a deleted object as a result."); case storage::Error::NONEXISTENT_OBJECT: - throw communication::bolt::ClientError( - "Returning a nonexistent object as a result."); + throw communication::bolt::ClientError("Returning a nonexistent object as a result."); case storage::Error::VERTEX_HAS_EDGES: case storage::Error::SERIALIZATION_ERROR: case storage::Error::PROPERTIES_DISABLED: - throw communication::bolt::ClientError( - "Unexpected storage error when streaming results."); + throw communication::bolt::ClientError("Unexpected storage error when streaming results."); } } decoded_values.emplace_back(std::move(*maybe_value)); @@ -467,20 +409,17 @@ using ServerT = communication::Server; using communication::ServerContext; #ifdef MG_ENTERPRISE -DEFINE_string( - auth_user_or_role_name_regex, "[a-zA-Z0-9_.+-@]+", - "Set to the regular expression that each user or role name must fulfill."); +DEFINE_string(auth_user_or_role_name_regex, "[a-zA-Z0-9_.+-@]+", + "Set to the regular expression that each user or role name must fulfill."); class AuthQueryHandler final : public query::AuthQueryHandler { auth::Auth *auth_; std::regex name_regex_; public: - AuthQueryHandler(auth::Auth *auth, const std::regex &name_regex) - : auth_(auth), name_regex_(name_regex) {} + AuthQueryHandler(auth::Auth *auth, const std::regex &name_regex) : auth_(auth), name_regex_(name_regex) {} - bool CreateUser(const std::string &username, - const std::optional &password) override { + bool CreateUser(const std::string &username, const std::optional &password) override { if (!std::regex_match(username, name_regex_)) { throw query::QueryRuntimeException("Invalid user name."); } @@ -506,8 +445,7 @@ class AuthQueryHandler final : public query::AuthQueryHandler { } } - void SetPassword(const std::string &username, - const std::optional &password) override { + void SetPassword(const std::string &username, const std::optional &password) override { if (!std::regex_match(username, name_regex_)) { throw query::QueryRuntimeException("Invalid user name."); } @@ -515,8 +453,7 @@ class AuthQueryHandler final : public query::AuthQueryHandler { std::lock_guard lock(auth_->WithLock()); auto user = auth_->GetUser(username); if (!user) { - throw query::QueryRuntimeException("User '{}' doesn't exist.", - username); + throw query::QueryRuntimeException("User '{}' doesn't exist.", username); } user->UpdatePassword(password); auth_->SaveUser(*user); @@ -581,8 +518,7 @@ class AuthQueryHandler final : public query::AuthQueryHandler { } } - std::optional GetRolenameForUser( - const std::string &username) override { + std::optional GetRolenameForUser(const std::string &username) override { if (!std::regex_match(username, name_regex_)) { throw query::QueryRuntimeException("Invalid user name."); } @@ -590,8 +526,7 @@ class AuthQueryHandler final : public query::AuthQueryHandler { std::lock_guard lock(auth_->WithLock()); auto user = auth_->GetUser(username); if (!user) { - throw query::QueryRuntimeException("User '{}' doesn't exist .", - username); + throw query::QueryRuntimeException("User '{}' doesn't exist .", username); } if (user->role()) return user->role()->rolename(); return std::nullopt; @@ -600,8 +535,7 @@ class AuthQueryHandler final : public query::AuthQueryHandler { } } - std::vector GetUsernamesForRole( - const std::string &rolename) override { + std::vector GetUsernamesForRole(const std::string &rolename) override { if (!std::regex_match(rolename, name_regex_)) { throw query::QueryRuntimeException("Invalid role name."); } @@ -609,8 +543,7 @@ class AuthQueryHandler final : public query::AuthQueryHandler { std::lock_guard lock(auth_->WithLock()); auto role = auth_->GetRole(rolename); if (!role) { - throw query::QueryRuntimeException("Role '{}' doesn't exist.", - rolename); + throw query::QueryRuntimeException("Role '{}' doesn't exist.", rolename); } std::vector usernames; const auto &users = auth_->AllUsersForRole(rolename); @@ -624,8 +557,7 @@ class AuthQueryHandler final : public query::AuthQueryHandler { } } - void SetRole(const std::string &username, - const std::string &rolename) override { + void SetRole(const std::string &username, const std::string &rolename) override { if (!std::regex_match(username, name_regex_)) { throw query::QueryRuntimeException("Invalid user name."); } @@ -636,18 +568,15 @@ class AuthQueryHandler final : public query::AuthQueryHandler { std::lock_guard lock(auth_->WithLock()); auto user = auth_->GetUser(username); if (!user) { - throw query::QueryRuntimeException("User '{}' doesn't exist .", - username); + throw query::QueryRuntimeException("User '{}' doesn't exist .", username); } auto role = auth_->GetRole(rolename); if (!role) { - throw query::QueryRuntimeException("Role '{}' doesn't exist .", - rolename); + throw query::QueryRuntimeException("Role '{}' doesn't exist .", rolename); } if (user->role()) { - throw query::QueryRuntimeException( - "User '{}' is already a member of role '{}'.", username, - user->role()->rolename()); + throw query::QueryRuntimeException("User '{}' is already a member of role '{}'.", username, + user->role()->rolename()); } user->SetRole(*role); auth_->SaveUser(*user); @@ -664,8 +593,7 @@ class AuthQueryHandler final : public query::AuthQueryHandler { std::lock_guard lock(auth_->WithLock()); auto user = auth_->GetUser(username); if (!user) { - throw query::QueryRuntimeException("User '{}' doesn't exist .", - username); + throw query::QueryRuntimeException("User '{}' doesn't exist .", username); } user->ClearRole(); auth_->SaveUser(*user); @@ -674,8 +602,7 @@ class AuthQueryHandler final : public query::AuthQueryHandler { } } - std::vector> GetPrivileges( - const std::string &user_or_role) override { + std::vector> GetPrivileges(const std::string &user_or_role) override { if (!std::regex_match(user_or_role, name_regex_)) { throw query::QueryRuntimeException("Invalid user or role name."); } @@ -685,8 +612,7 @@ class AuthQueryHandler final : public query::AuthQueryHandler { auto user = auth_->GetUser(user_or_role); auto role = auth_->GetRole(user_or_role); if (!user && !role) { - throw query::QueryRuntimeException("User or role '{}' doesn't exist.", - user_or_role); + throw query::QueryRuntimeException("User or role '{}' doesn't exist.", user_or_role); } if (user) { const auto &permissions = user->GetPermissions(); @@ -709,10 +635,9 @@ class AuthQueryHandler final : public query::AuthQueryHandler { description.emplace_back("DENIED TO ROLE"); } } - grants.push_back( - {query::TypedValue(auth::PermissionToString(permission)), - query::TypedValue(auth::PermissionLevelToString(effective)), - query::TypedValue(utils::Join(description, ", "))}); + grants.push_back({query::TypedValue(auth::PermissionToString(permission)), + query::TypedValue(auth::PermissionLevelToString(effective)), + query::TypedValue(utils::Join(description, ", "))}); } } } else { @@ -727,10 +652,9 @@ class AuthQueryHandler final : public query::AuthQueryHandler { } else if (effective == auth::PermissionLevel::DENY) { description = "DENIED TO ROLE"; } - grants.push_back( - {query::TypedValue(auth::PermissionToString(permission)), - query::TypedValue(auth::PermissionLevelToString(effective)), - query::TypedValue(description)}); + grants.push_back({query::TypedValue(auth::PermissionToString(permission)), + query::TypedValue(auth::PermissionLevelToString(effective)), + query::TypedValue(description)}); } } } @@ -740,48 +664,40 @@ class AuthQueryHandler final : public query::AuthQueryHandler { } } - void GrantPrivilege( - const std::string &user_or_role, - const std::vector &privileges) override { - EditPermissions(user_or_role, privileges, - [](auto *permissions, const auto &permission) { - // TODO (mferencevic): should we first check that the - // privilege is granted/denied/revoked before - // unconditionally granting/denying/revoking it? - permissions->Grant(permission); - }); + void GrantPrivilege(const std::string &user_or_role, + const std::vector &privileges) override { + EditPermissions(user_or_role, privileges, [](auto *permissions, const auto &permission) { + // TODO (mferencevic): should we first check that the + // privilege is granted/denied/revoked before + // unconditionally granting/denying/revoking it? + permissions->Grant(permission); + }); } - void DenyPrivilege( - const std::string &user_or_role, - const std::vector &privileges) override { - EditPermissions(user_or_role, privileges, - [](auto *permissions, const auto &permission) { - // TODO (mferencevic): should we first check that the - // privilege is granted/denied/revoked before - // unconditionally granting/denying/revoking it? - permissions->Deny(permission); - }); + void DenyPrivilege(const std::string &user_or_role, + const std::vector &privileges) override { + EditPermissions(user_or_role, privileges, [](auto *permissions, const auto &permission) { + // TODO (mferencevic): should we first check that the + // privilege is granted/denied/revoked before + // unconditionally granting/denying/revoking it? + permissions->Deny(permission); + }); } - void RevokePrivilege( - const std::string &user_or_role, - const std::vector &privileges) override { - EditPermissions(user_or_role, privileges, - [](auto *permissions, const auto &permission) { - // TODO (mferencevic): should we first check that the - // privilege is granted/denied/revoked before - // unconditionally granting/denying/revoking it? - permissions->Revoke(permission); - }); + void RevokePrivilege(const std::string &user_or_role, + const std::vector &privileges) override { + EditPermissions(user_or_role, privileges, [](auto *permissions, const auto &permission) { + // TODO (mferencevic): should we first check that the + // privilege is granted/denied/revoked before + // unconditionally granting/denying/revoking it? + permissions->Revoke(permission); + }); } private: template - void EditPermissions( - const std::string &user_or_role, - const std::vector &privileges, - const TEditFun &edit_fun) { + void EditPermissions(const std::string &user_or_role, const std::vector &privileges, + const TEditFun &edit_fun) { if (!std::regex_match(user_or_role, name_regex_)) { throw query::QueryRuntimeException("Invalid user or role name."); } @@ -795,8 +711,7 @@ class AuthQueryHandler final : public query::AuthQueryHandler { auto user = auth_->GetUser(user_or_role); auto role = auth_->GetRole(user_or_role); if (!user && !role) { - throw query::QueryRuntimeException("User or role '{}' doesn't exist.", - user_or_role); + throw query::QueryRuntimeException("User or role '{}' doesn't exist.", user_or_role); } if (user) { for (const auto &permission : permissions) { @@ -818,71 +733,44 @@ class AuthQueryHandler final : public query::AuthQueryHandler { class NoAuthInCommunity : public query::QueryRuntimeException { public: NoAuthInCommunity() - : query::QueryRuntimeException::QueryRuntimeException( - "Auth is not supported in Memgraph Community!") {} + : query::QueryRuntimeException::QueryRuntimeException("Auth is not supported in Memgraph Community!") {} }; class AuthQueryHandler final : public query::AuthQueryHandler { public: - bool CreateUser(const std::string &, - const std::optional &) override { - throw NoAuthInCommunity(); - } + bool CreateUser(const std::string &, const std::optional &) override { throw NoAuthInCommunity(); } bool DropUser(const std::string &) override { throw NoAuthInCommunity(); } - void SetPassword(const std::string &, - const std::optional &) override { - throw NoAuthInCommunity(); - } + void SetPassword(const std::string &, const std::optional &) override { throw NoAuthInCommunity(); } bool CreateRole(const std::string &) override { throw NoAuthInCommunity(); } bool DropRole(const std::string &) override { throw NoAuthInCommunity(); } - std::vector GetUsernames() override { - throw NoAuthInCommunity(); - } + std::vector GetUsernames() override { throw NoAuthInCommunity(); } - std::vector GetRolenames() override { - throw NoAuthInCommunity(); - } + std::vector GetRolenames() override { throw NoAuthInCommunity(); } - std::optional GetRolenameForUser(const std::string &) override { - throw NoAuthInCommunity(); - } + std::optional GetRolenameForUser(const std::string &) override { throw NoAuthInCommunity(); } - std::vector GetUsernamesForRole( - const std::string &) override { - throw NoAuthInCommunity(); - } + std::vector GetUsernamesForRole(const std::string &) override { throw NoAuthInCommunity(); } - void SetRole(const std::string &, const std::string &) override { - throw NoAuthInCommunity(); - } + void SetRole(const std::string &, const std::string &) override { throw NoAuthInCommunity(); } void ClearRole(const std::string &) override { throw NoAuthInCommunity(); } - std::vector> GetPrivileges( - const std::string &) override { + std::vector> GetPrivileges(const std::string &) override { throw NoAuthInCommunity(); } + + void GrantPrivilege(const std::string &, const std::vector &) override { throw NoAuthInCommunity(); } - void GrantPrivilege( - const std::string &, - const std::vector &) override { + void DenyPrivilege(const std::string &, const std::vector &) override { throw NoAuthInCommunity(); } - void DenyPrivilege( - const std::string &, - const std::vector &) override { - throw NoAuthInCommunity(); - } - - void RevokePrivilege( - const std::string &, - const std::vector &) override { + void RevokePrivilege(const std::string &, const std::vector &) override { throw NoAuthInCommunity(); } }; @@ -911,11 +799,9 @@ void InitSignalHandlers(const std::function &shutdown_fun) { shutdown_fun(); }; - MG_ASSERT(utils::SignalHandler::RegisterHandler( - utils::Signal::Terminate, shutdown, block_shutdown_signals), + MG_ASSERT(utils::SignalHandler::RegisterHandler(utils::Signal::Terminate, shutdown, block_shutdown_signals), "Unable to register SIGTERM handler!"); - MG_ASSERT(utils::SignalHandler::RegisterHandler( - utils::Signal::Interupt, shutdown, block_shutdown_signals), + MG_ASSERT(utils::SignalHandler::RegisterHandler(utils::Signal::Interupt, shutdown, block_shutdown_signals), "Unable to register SIGINT handler!"); } @@ -952,13 +838,10 @@ int main(int argc, char **argv) { auto gil = py::EnsureGIL(); auto maybe_exc = py::AppendToSysPath(py_support_dir.c_str()); if (maybe_exc) { - spdlog::error("Unable to load support for embedded Python: {}", - *maybe_exc); + spdlog::error("Unable to load support for embedded Python: {}", *maybe_exc); } } else { - spdlog::error( - "Unable to load support for embedded Python: missing directory {}", - py_support_dir); + spdlog::error("Unable to load support for embedded Python: missing directory {}", py_support_dir); } } catch (const std::filesystem::filesystem_error &e) { spdlog::error("Unable to load support for embedded Python: {}", e.what()); @@ -978,8 +861,7 @@ int main(int argc, char **argv) { mem_log_scheduler.Run("Memory warning", std::chrono::seconds(3), [] { auto free_ram = utils::sysinfo::AvailableMemoryKilobytes(); if (free_ram && *free_ram / 1024 < FLAGS_memory_warning_threshold) - spdlog::warn("Running out of available RAM, only {} MB left", - *free_ram / 1024); + spdlog::warn("Running out of available RAM, only {} MB left", *free_ram / 1024); }); } else { // Kernel version for the `MemAvailable` value is from: man procfs @@ -990,8 +872,7 @@ int main(int argc, char **argv) { } } - std::cout << "You are running Memgraph v" << gflags::VersionString() - << std::endl; + std::cout << "You are running Memgraph v" << gflags::VersionString() << std::endl; auto data_directory = std::filesystem::path(FLAGS_data_directory); @@ -1011,18 +892,15 @@ int main(int argc, char **argv) { auth::Auth auth{data_directory / "auth"}; // Audit log - audit::Log audit_log{data_directory / "audit", FLAGS_audit_buffer_size, - FLAGS_audit_buffer_flush_interval_ms}; + audit::Log audit_log{data_directory / "audit", FLAGS_audit_buffer_size, FLAGS_audit_buffer_flush_interval_ms}; // Start the log if enabled. if (FLAGS_audit_enabled) { audit_log.Start(); } // Setup SIGUSR2 to be used for reopening audit log files, when e.g. logrotate // rotates our audit logs. - MG_ASSERT( - utils::SignalHandler::RegisterHandler( - utils::Signal::User2, [&audit_log]() { audit_log.ReopenLog(); }), - "Unable to register SIGUSR2 handler!"); + MG_ASSERT(utils::SignalHandler::RegisterHandler(utils::Signal::User2, [&audit_log]() { audit_log.ReopenLog(); }), + "Unable to register SIGUSR2 handler!"); // End enterprise features initialization #endif @@ -1030,54 +908,45 @@ int main(int argc, char **argv) { // Main storage and execution engines initialization storage::Config db_config{ - .gc = {.type = storage::Config::Gc::Type::PERIODIC, - .interval = std::chrono::seconds(FLAGS_storage_gc_cycle_sec)}, + .gc = {.type = storage::Config::Gc::Type::PERIODIC, .interval = std::chrono::seconds(FLAGS_storage_gc_cycle_sec)}, .items = {.properties_on_edges = FLAGS_storage_properties_on_edges}, - .durability = { - .storage_directory = FLAGS_data_directory, - .recover_on_startup = FLAGS_storage_recover_on_startup, - .snapshot_retention_count = FLAGS_storage_snapshot_retention_count, - .wal_file_size_kibibytes = FLAGS_storage_wal_file_size_kib, - .wal_file_flush_every_n_tx = FLAGS_storage_wal_file_flush_every_n_tx, - .snapshot_on_exit = FLAGS_storage_snapshot_on_exit}}; + .durability = {.storage_directory = FLAGS_data_directory, + .recover_on_startup = FLAGS_storage_recover_on_startup, + .snapshot_retention_count = FLAGS_storage_snapshot_retention_count, + .wal_file_size_kibibytes = FLAGS_storage_wal_file_size_kib, + .wal_file_flush_every_n_tx = FLAGS_storage_wal_file_flush_every_n_tx, + .snapshot_on_exit = FLAGS_storage_snapshot_on_exit}}; if (FLAGS_storage_snapshot_interval_sec == 0) { if (FLAGS_storage_wal_enabled) { LOG_FATAL( "In order to use write-ahead-logging you must enable " "periodic snapshots by setting the snapshot interval to a " "value larger than 0!"); - db_config.durability.snapshot_wal_mode = - storage::Config::Durability::SnapshotWalMode::DISABLED; + db_config.durability.snapshot_wal_mode = storage::Config::Durability::SnapshotWalMode::DISABLED; } } else { if (FLAGS_storage_wal_enabled) { - db_config.durability.snapshot_wal_mode = storage::Config::Durability:: - SnapshotWalMode::PERIODIC_SNAPSHOT_WITH_WAL; + db_config.durability.snapshot_wal_mode = storage::Config::Durability::SnapshotWalMode::PERIODIC_SNAPSHOT_WITH_WAL; } else { - db_config.durability.snapshot_wal_mode = - storage::Config::Durability::SnapshotWalMode::PERIODIC_SNAPSHOT; + db_config.durability.snapshot_wal_mode = storage::Config::Durability::SnapshotWalMode::PERIODIC_SNAPSHOT; } - db_config.durability.snapshot_interval = - std::chrono::seconds(FLAGS_storage_snapshot_interval_sec); + db_config.durability.snapshot_interval = std::chrono::seconds(FLAGS_storage_snapshot_interval_sec); } storage::Storage db(db_config); query::InterpreterContext interpreter_context{&db}; - query::SetExecutionTimeout(&interpreter_context, - FLAGS_query_execution_timeout_sec); + query::SetExecutionTimeout(&interpreter_context, FLAGS_query_execution_timeout_sec); #ifdef MG_ENTERPRISE SessionData session_data{&db, &interpreter_context, &auth, &audit_log}; #else SessionData session_data{&db, &interpreter_context}; #endif - query::procedure::gModuleRegistry.SetModulesDirectory( - FLAGS_query_modules_directory); + query::procedure::gModuleRegistry.SetModulesDirectory(FLAGS_query_modules_directory); query::procedure::gModuleRegistry.UnloadAndLoadModulesFromDirectory(); #ifdef MG_ENTERPRISE - AuthQueryHandler auth_handler(&auth, - std::regex(FLAGS_auth_user_or_role_name_regex)); + AuthQueryHandler auth_handler(&auth, std::regex(FLAGS_auth_user_or_role_name_regex)); #else AuthQueryHandler auth_handler; #endif @@ -1093,16 +962,14 @@ int main(int argc, char **argv) { spdlog::warn("Using non-secure Bolt connection (without SSL)"); } - ServerT server({FLAGS_bolt_address, static_cast(FLAGS_bolt_port)}, - &session_data, &context, FLAGS_bolt_session_inactivity_timeout, - service_name, FLAGS_bolt_num_workers); + ServerT server({FLAGS_bolt_address, static_cast(FLAGS_bolt_port)}, &session_data, &context, + FLAGS_bolt_session_inactivity_timeout, service_name, FLAGS_bolt_num_workers); // Setup telemetry std::optional telemetry; if (FLAGS_telemetry_enabled) { - telemetry.emplace( - "https://telemetry.memgraph.com/88b5e7e8-746a-11e8-9f85-538a9e9690cc/", - data_directory / "telemetry", std::chrono::minutes(10)); + telemetry.emplace("https://telemetry.memgraph.com/88b5e7e8-746a-11e8-9f85-538a9e9690cc/", + data_directory / "telemetry", std::chrono::minutes(10)); telemetry->AddCollector("db", [&db]() -> nlohmann::json { auto info = db.GetInfo(); return {{"vertices", info.vertex_count}, {"edges", info.edge_count}}; diff --git a/src/mg_import_csv.cpp b/src/mg_import_csv.cpp index 6e64a46ea..2e3b08869 100644 --- a/src/mg_import_csv.cpp +++ b/src/mg_import_csv.cpp @@ -42,30 +42,22 @@ bool ValidateIdTypeOptions(const char *flagname, const std::string &value) { // They are used to automatically load the same configuration as the main // Memgraph binary so that the flags don't need to be specified when importing a // CSV file on a correctly set-up Memgraph installation. -DEFINE_string(data_directory, "mg_data", - "Path to directory in which to save all permanent data."); -DEFINE_bool(storage_properties_on_edges, false, - "Controls whether relationships have properties."); +DEFINE_string(data_directory, "mg_data", "Path to directory in which to save all permanent data."); +DEFINE_bool(storage_properties_on_edges, false, "Controls whether relationships have properties."); // CSV import flags. -DEFINE_string(array_delimiter, ";", - "Delimiter between elements of array values."); +DEFINE_string(array_delimiter, ";", "Delimiter between elements of array values."); DEFINE_validator(array_delimiter, &ValidateControlCharacter); DEFINE_string(delimiter, ",", "Delimiter between each field in the CSV."); DEFINE_validator(delimiter, &ValidateControlCharacter); -DEFINE_string(quote, "\"", - "Quotation character for data in the CSV. Cannot contain '\n'"); +DEFINE_string(quote, "\"", "Quotation character for data in the CSV. Cannot contain '\n'"); DEFINE_validator(quote, &ValidateControlCharacter); -DEFINE_bool(skip_duplicate_nodes, false, - "Set to true to skip duplicate nodes instead of raising an error."); +DEFINE_bool(skip_duplicate_nodes, false, "Set to true to skip duplicate nodes instead of raising an error."); DEFINE_bool(skip_bad_relationships, false, "Set to true to skip relationships that connect nodes that don't " "exist instead of raising an error."); -DEFINE_bool(ignore_empty_strings, false, - "Set to true to treat empty strings as null values."); -DEFINE_bool( - ignore_extra_columns, false, - "Set to true to ignore columns that aren't specified in the header."); +DEFINE_bool(ignore_empty_strings, false, "Set to true to treat empty strings as null values."); +DEFINE_bool(ignore_extra_columns, false, "Set to true to ignore columns that aren't specified in the header."); DEFINE_bool(trim_strings, false, "Set to true to trim leading/trailing whitespace from all fields " "that are loaded from the CSV file."); @@ -75,25 +67,22 @@ DEFINE_string(id_type, "STRING", DEFINE_validator(id_type, &ValidateIdTypeOptions); // Arguments `--nodes` and `--relationships` can be input multiple times and are // handled with custom parsing. -DEFINE_string( - nodes, "", - "Files that should be parsed for nodes. The CSV header will be loaded from " - "the first supplied file, all other files supplied in a single flag will " - "be treated as data files. Additional labels can be specified for the node " - "files. The flag can be specified multiple times (useful for differently " - "formatted node files). The format of this argument is: " - "[