From 21db602f67d45c2d215a931903dffee8e3c6b34d Mon Sep 17 00:00:00 2001 From: Matej Kenda Date: Fri, 28 Nov 2025 11:44:52 +0100 Subject: [PATCH] enh(MongoDB) Use string literals and char constants where possible. --- MongoDB/src/Array.cpp | 6 ++--- MongoDB/src/Binary.cpp | 4 +++- MongoDB/src/Connection.cpp | 12 ++++++---- MongoDB/src/Database.cpp | 20 ++++++++-------- MongoDB/src/OpMsgCursor.cpp | 4 +++- MongoDB/src/OpMsgMessage.cpp | 6 ++--- MongoDB/src/ReadPreference.cpp | 4 +++- MongoDB/src/ReplicaSet.cpp | 32 ++++++++++++++------------ MongoDB/src/ReplicaSetConnection.cpp | 24 +++++++++++--------- MongoDB/src/ServerDescription.cpp | 34 +++++++++++++++------------- 10 files changed, 81 insertions(+), 65 deletions(-) diff --git a/MongoDB/src/Array.cpp b/MongoDB/src/Array.cpp index c6fe7aab0..be711e2f3 100644 --- a/MongoDB/src/Array.cpp +++ b/MongoDB/src/Array.cpp @@ -42,7 +42,7 @@ std::string Array::toString(int indent) const { std::ostringstream oss; - oss << "["; + oss << '['; if (indent > 0) oss << std::endl; @@ -52,7 +52,7 @@ std::string Array::toString(int indent) const { if (it != elems.begin()) { - oss << ","; + oss << ','; if (indent > 0) oss << std::endl; } @@ -68,7 +68,7 @@ std::string Array::toString(int indent) const for (int i = 0; i < indent; ++i) oss << ' '; } - oss << "]"; + oss << ']'; return oss.str(); } diff --git a/MongoDB/src/Binary.cpp b/MongoDB/src/Binary.cpp index 2bbbf943c..95bf8bc55 100644 --- a/MongoDB/src/Binary.cpp +++ b/MongoDB/src/Binary.cpp @@ -18,6 +18,8 @@ #include "Poco/MemoryStream.h" #include +using namespace std::string_literals; + namespace Poco { namespace MongoDB { @@ -74,7 +76,7 @@ std::string Binary::toString(int indent) const { try { - return "UUID(\"" + uuid().toString() + "\")"; + return "UUID(\""s + uuid().toString() + "\")"s; } catch (...) { diff --git a/MongoDB/src/Connection.cpp b/MongoDB/src/Connection.cpp index b07973420..e152bdc04 100644 --- a/MongoDB/src/Connection.cpp +++ b/MongoDB/src/Connection.cpp @@ -20,6 +20,8 @@ #include "Poco/NumberParser.h" #include "Poco/Exception.h" +using namespace std::string_literals; + namespace Poco { namespace MongoDB { @@ -165,19 +167,19 @@ void Connection::connect(const std::string& uri, SocketFactory& socketFactory) Poco::URI::QueryParameters params = theURI.getQueryParameters(); for (Poco::URI::QueryParameters::const_iterator it = params.begin(); it != params.end(); ++it) { - if (it->first == "ssl") + if (it->first == "ssl"s) { - ssl = (it->second == "true"); + ssl = (it->second == "true"s); } - else if (it->first == "connectTimeoutMS") + else if (it->first == "connectTimeoutMS"s) { connectTimeout = static_cast(1000)*Poco::NumberParser::parse(it->second); } - else if (it->first == "socketTimeoutMS") + else if (it->first == "socketTimeoutMS"s) { socketTimeout = static_cast(1000)*Poco::NumberParser::parse(it->second); } - else if (it->first == "authMechanism") + else if (it->first == "authMechanism"s) { authMechanism = it->second; } diff --git a/MongoDB/src/Database.cpp b/MongoDB/src/Database.cpp index 6ef58b370..3c03f63d4 100644 --- a/MongoDB/src/Database.cpp +++ b/MongoDB/src/Database.cpp @@ -31,6 +31,8 @@ #include #include +using namespace std::string_literals; + namespace Poco { namespace MongoDB { @@ -337,26 +339,26 @@ Poco::MongoDB::Document::Ptr Database::createIndex( MongoDB::Document::Ptr index = new MongoDB::Document(); if (!indexName.empty()) { - index->add("name", indexName); + index->add("name"s, indexName); } - index->add("key", keys); - index->add("ns", _dbname + "." + collection); - index->add("name", indexName); + index->add("key"s, keys); + index->add("ns"s, _dbname + '.' + collection); + index->add("name"s, indexName); if (options & INDEX_UNIQUE) { - index->add("unique", true); + index->add("unique"s, true); } if (options & INDEX_BACKGROUND) { - index->add("background", true); + index->add("background"s, true); } if (options & INDEX_SPARSE) { - index->add("sparse", true); + index->add("sparse"s, true); } if (expirationSeconds > 0) { - index->add("expireAfterSeconds", static_cast(expirationSeconds)); + index->add("expireAfterSeconds"s, static_cast(expirationSeconds)); } if (version > 0) { - index->add("version", static_cast(version)); + index->add("version"s, static_cast(version)); } MongoDB::Array::Ptr indexes = new MongoDB::Array(); diff --git a/MongoDB/src/OpMsgCursor.cpp b/MongoDB/src/OpMsgCursor.cpp index a440fd13b..b7402982e 100644 --- a/MongoDB/src/OpMsgCursor.cpp +++ b/MongoDB/src/OpMsgCursor.cpp @@ -39,6 +39,8 @@ #define _MONGODB_EXHAUST_ALLOWED_WORKS false +using namespace std::string_literals; + namespace Poco { namespace MongoDB { @@ -184,7 +186,7 @@ void OpMsgCursor::kill(Connection& connection) const auto killed = _response.body().get(keyCursorsKilled, nullptr); if (!killed || killed->size() != 1 || killed->get(0, -1) != _cursorID) { - throw Poco::ProtocolException("Cursor not killed as expected: " + std::to_string(_cursorID)); + throw Poco::ProtocolException("Cursor not killed as expected: "s + std::to_string(_cursorID)); } _cursorID = 0; diff --git a/MongoDB/src/OpMsgMessage.cpp b/MongoDB/src/OpMsgMessage.cpp index 3dc0c5946..b4755d7ac 100644 --- a/MongoDB/src/OpMsgMessage.cpp +++ b/MongoDB/src/OpMsgMessage.cpp @@ -296,8 +296,8 @@ void OpMsgMessage::read(std::istream& istr) #if POCO_MONGODB_DUMP std::cout - << "Message hdr: " << _header.getMessageLength() << " " << remainingSize << " " - << _header.opCode() << " " << _header.getRequestID() << " " << _header.responseTo() + << "Message hdr: " << _header.getMessageLength() << ' ' << remainingSize << ' ' + << _header.opCode() << ' ' << _header.getRequestID() << ' ' << _header.responseTo() << std::endl; #endif @@ -357,7 +357,7 @@ void OpMsgMessage::read(std::istream& istr) while (msgss.tellg() < endOfSection) { #if POCO_MONGODB_DUMP - std::cout << "section doc: " << msgss.tellg() << " " << endOfSection << std::endl; + std::cout << "section doc: " << msgss.tellg() << ' ' << endOfSection << std::endl; #endif Document::Ptr doc = new Document(); doc->read(reader); diff --git a/MongoDB/src/ReadPreference.cpp b/MongoDB/src/ReadPreference.cpp index 90019a73a..728813641 100644 --- a/MongoDB/src/ReadPreference.cpp +++ b/MongoDB/src/ReadPreference.cpp @@ -17,6 +17,8 @@ #include "Poco/Format.h" #include +using namespace std::string_literals; + namespace Poco { namespace MongoDB { @@ -179,7 +181,7 @@ std::string ReadPreference::toString() const if (!_tags.empty()) { - result += " (tags: " + _tags.toString() + ")"; + result += " (tags: "s + _tags.toString() + ')'; } if (_maxStalenessSeconds != NO_MAX_STALENESS) diff --git a/MongoDB/src/ReplicaSet.cpp b/MongoDB/src/ReplicaSet.cpp index 1231ec66d..3f38846a1 100644 --- a/MongoDB/src/ReplicaSet.cpp +++ b/MongoDB/src/ReplicaSet.cpp @@ -20,6 +20,8 @@ #include "Poco/NumberParser.h" #include +using namespace std::string_literals; + namespace Poco { namespace MongoDB { @@ -234,13 +236,13 @@ Connection::Ptr ReplicaSet::isMaster(const Net::SocketAddress& address) if (response.responseOk()) { const Document& doc = response.body(); - if (doc.get("isWritablePrimary", false) || doc.get("ismaster", false)) + if (doc.get("isWritablePrimary"s, false) || doc.get("ismaster"s, false)) { return conn; } - else if (doc.exists("primary")) + else if (doc.exists("primary"s)) { - return isMaster(Net::SocketAddress(doc.get("primary"))); + return isMaster(Net::SocketAddress(doc.get("primary"s))); } } } @@ -411,9 +413,9 @@ void ReplicaSet::updateTopologyFromHello(const Net::SocketAddress& address) _topology.updateServer(address, doc, rttMicros); // Update replica set name if not set - if (_config.setName.empty() && doc.exists("setName")) + if (_config.setName.empty() && doc.exists("setName"s)) { - _config.setName = doc.get("setName"); + _config.setName = doc.get("setName"s); _topology.setName(_config.setName); } } @@ -532,45 +534,45 @@ void ReplicaSet::parseURI(const std::string& uri) Poco::URI::QueryParameters params = theURI.getQueryParameters(); for (const auto& param : params) { - if (param.first == "replicaSet") + if (param.first == "replicaSet"s) { _config.setName = param.second; } - else if (param.first == "readPreference") + else if (param.first == "readPreference"s) { // Parse read preference mode - if (param.second == "primary") + if (param.second == "primary"s) { _config.readPreference = ReadPreference(ReadPreference::Primary); } - else if (param.second == "primaryPreferred") + else if (param.second == "primaryPreferred"s) { _config.readPreference = ReadPreference(ReadPreference::PrimaryPreferred); } - else if (param.second == "secondary") + else if (param.second == "secondary"s) { _config.readPreference = ReadPreference(ReadPreference::Secondary); } - else if (param.second == "secondaryPreferred") + else if (param.second == "secondaryPreferred"s) { _config.readPreference = ReadPreference(ReadPreference::SecondaryPreferred); } - else if (param.second == "nearest") + else if (param.second == "nearest"s) { _config.readPreference = ReadPreference(ReadPreference::Nearest); } } - else if (param.first == "connectTimeoutMS") + else if (param.first == "connectTimeoutMS"s) { Poco::Int64 timeoutMs = Poco::NumberParser::parse64(param.second); _config.connectTimeout = Poco::Timespan(timeoutMs * 1000); // Convert ms to microseconds } - else if (param.first == "socketTimeoutMS") + else if (param.first == "socketTimeoutMS"s) { Poco::Int64 timeoutMs = Poco::NumberParser::parse64(param.second); _config.socketTimeout = Poco::Timespan(timeoutMs * 1000); // Convert ms to microseconds } - else if (param.first == "heartbeatFrequencyMS") + else if (param.first == "heartbeatFrequencyMS"s) { Poco::Int64 freqMs = Poco::NumberParser::parse64(param.second); _config.heartbeatFrequency = Poco::Timespan(freqMs * 1000); // Convert ms to microseconds diff --git a/MongoDB/src/ReplicaSetConnection.cpp b/MongoDB/src/ReplicaSetConnection.cpp index 977c5b266..046e5c781 100644 --- a/MongoDB/src/ReplicaSetConnection.cpp +++ b/MongoDB/src/ReplicaSetConnection.cpp @@ -17,6 +17,8 @@ #include "Poco/Net/NetException.h" #include "Poco/Exception.h" +using namespace std::string_literals; + namespace Poco { namespace MongoDB { @@ -59,7 +61,7 @@ void ReplicaSetConnection::sendRequest(OpMsgMessage& request, OpMsgMessage& resp if (!response.responseOk() && isRetriableMongoDBError(response)) { markServerFailed(); - throw Poco::IOException("MongoDB retriable error: " + response.body().toString()); + throw Poco::IOException("MongoDB retriable error: "s + response.body().toString()); } }); } @@ -241,10 +243,10 @@ bool ReplicaSetConnection::isRetriableError(const std::exception& e) { const auto& msg = ioEx->message(); // Check for specific retriable error messages - if (msg.find("not master") != std::string::npos || - msg.find("NotMaster") != std::string::npos || - msg.find("Connection") != std::string::npos || - msg.find("connection") != std::string::npos) + if (msg.find("not master"s) != std::string::npos || + msg.find("NotMaster"s) != std::string::npos || + msg.find("Connection"s) != std::string::npos || + msg.find("connection"s) != std::string::npos) { return true; } @@ -264,9 +266,9 @@ bool ReplicaSetConnection::isRetriableMongoDBError(const OpMsgMessage& response) const Document& body = response.body(); // Check for error code - if (body.exists("code")) + if (body.exists("code"s)) { - ErrorCode code = static_cast(body.get("code")); + ErrorCode code = static_cast(body.get("code"s)); switch (code) { @@ -288,11 +290,11 @@ bool ReplicaSetConnection::isRetriableMongoDBError(const OpMsgMessage& response) } // Check for error message patterns - if (body.exists("errmsg")) + if (body.exists("errmsg"s)) { - const auto& errmsg = body.get("errmsg"); - if (errmsg.find("not master") != std::string::npos || - errmsg.find("NotMaster") != std::string::npos) + const auto& errmsg = body.get("errmsg"s); + if (errmsg.find("not master"s) != std::string::npos || + errmsg.find("NotMaster"s) != std::string::npos) { return true; } diff --git a/MongoDB/src/ServerDescription.cpp b/MongoDB/src/ServerDescription.cpp index eb8127e8f..415ef59b5 100644 --- a/MongoDB/src/ServerDescription.cpp +++ b/MongoDB/src/ServerDescription.cpp @@ -15,6 +15,8 @@ #include "Poco/MongoDB/ServerDescription.h" #include "Poco/MongoDB/Array.h" +using namespace std::string_literals; + namespace Poco { namespace MongoDB { @@ -55,9 +57,9 @@ void ServerDescription::updateFromHelloResponse(const Document& helloResponse, P parseServerType(helloResponse); // Get replica set name - if (helloResponse.exists("setName")) + if (helloResponse.exists("setName"s)) { - _setName = helloResponse.get("setName"); + _setName = helloResponse.get("setName"s); } // Parse hosts list @@ -93,10 +95,10 @@ void ServerDescription::reset() void ServerDescription::parseServerType(const Document& doc) { // Check for standalone - if (!doc.exists("setName")) + if (!doc.exists("setName"s)) { // Check if it's a mongos - if (doc.get("msg", "") == "isdbgrid") + if (doc.get("msg"s, ""s) == "isdbgrid") { _type = Mongos; return; @@ -106,19 +108,19 @@ void ServerDescription::parseServerType(const Document& doc) } // It's part of a replica set - determine the role - if (doc.get("isWritablePrimary", false) || doc.get("ismaster", false)) + if (doc.get("isWritablePrimary"s, false) || doc.get("ismaster"s, false)) { _type = RsPrimary; } - else if (doc.get("secondary", false)) + else if (doc.get("secondary"s, false)) { _type = RsSecondary; } - else if (doc.get("arbiterOnly", false)) + else if (doc.get("arbiterOnly"s, false)) { _type = RsArbiter; } - else if (doc.get("hidden", false) || doc.get("passive", false)) + else if (doc.get("hidden"s, false) || doc.get("passive"s, false)) { _type = RsOther; } @@ -135,9 +137,9 @@ void ServerDescription::parseHosts(const Document& doc) _hosts.clear(); // Parse hosts array - if (doc.exists("hosts")) + if (doc.exists("hosts"s)) { - Array::Ptr hostsArray = doc.get("hosts"); + Array::Ptr hostsArray = doc.get("hosts"s); _hosts.reserve(hostsArray->size()); for (std::size_t i = 0; i < hostsArray->size(); ++i) { @@ -154,9 +156,9 @@ void ServerDescription::parseHosts(const Document& doc) } // Parse passives array (hidden/passive members) - if (doc.exists("passives")) + if (doc.exists("passives"s)) { - Array::Ptr passivesArray = doc.get("passives"); + Array::Ptr passivesArray = doc.get("passives"s); for (std::size_t i = 0; i < passivesArray->size(); ++i) { try @@ -172,9 +174,9 @@ void ServerDescription::parseHosts(const Document& doc) } // Parse arbiters array - if (doc.exists("arbiters")) + if (doc.exists("arbiters"s)) { - Array::Ptr arbitersArray = doc.get("arbiters"); + Array::Ptr arbitersArray = doc.get("arbiters"s); for (std::size_t i = 0; i < arbitersArray->size(); ++i) { try @@ -195,9 +197,9 @@ void ServerDescription::parseTags(const Document& doc) { _tags.clear(); - if (doc.exists("tags")) + if (doc.exists("tags"s)) { - Document::Ptr tagsDoc = doc.get("tags"); + Document::Ptr tagsDoc = doc.get("tags"s); if (!tagsDoc.isNull()) { _tags = *tagsDoc;