enh(MongoDB) Use string literals and char constants where possible.

This commit is contained in:
Matej Kenda
2025-11-28 11:44:52 +01:00
parent b3dc88bc5c
commit 21db602f67
10 changed files with 81 additions and 65 deletions

View File

@@ -42,7 +42,7 @@ std::string Array::toString(int indent) const
{ {
std::ostringstream oss; std::ostringstream oss;
oss << "["; oss << '[';
if (indent > 0) oss << std::endl; if (indent > 0) oss << std::endl;
@@ -52,7 +52,7 @@ std::string Array::toString(int indent) const
{ {
if (it != elems.begin()) if (it != elems.begin())
{ {
oss << ","; oss << ',';
if (indent > 0) oss << std::endl; 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 << ' '; for (int i = 0; i < indent; ++i) oss << ' ';
} }
oss << "]"; oss << ']';
return oss.str(); return oss.str();
} }

View File

@@ -18,6 +18,8 @@
#include "Poco/MemoryStream.h" #include "Poco/MemoryStream.h"
#include <sstream> #include <sstream>
using namespace std::string_literals;
namespace Poco { namespace Poco {
namespace MongoDB { namespace MongoDB {
@@ -74,7 +76,7 @@ std::string Binary::toString(int indent) const
{ {
try try
{ {
return "UUID(\"" + uuid().toString() + "\")"; return "UUID(\""s + uuid().toString() + "\")"s;
} }
catch (...) catch (...)
{ {

View File

@@ -20,6 +20,8 @@
#include "Poco/NumberParser.h" #include "Poco/NumberParser.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
using namespace std::string_literals;
namespace Poco { namespace Poco {
namespace MongoDB { namespace MongoDB {
@@ -165,19 +167,19 @@ void Connection::connect(const std::string& uri, SocketFactory& socketFactory)
Poco::URI::QueryParameters params = theURI.getQueryParameters(); Poco::URI::QueryParameters params = theURI.getQueryParameters();
for (Poco::URI::QueryParameters::const_iterator it = params.begin(); it != params.end(); ++it) 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<Poco::Timespan::TimeDiff>(1000)*Poco::NumberParser::parse(it->second); connectTimeout = static_cast<Poco::Timespan::TimeDiff>(1000)*Poco::NumberParser::parse(it->second);
} }
else if (it->first == "socketTimeoutMS") else if (it->first == "socketTimeoutMS"s)
{ {
socketTimeout = static_cast<Poco::Timespan::TimeDiff>(1000)*Poco::NumberParser::parse(it->second); socketTimeout = static_cast<Poco::Timespan::TimeDiff>(1000)*Poco::NumberParser::parse(it->second);
} }
else if (it->first == "authMechanism") else if (it->first == "authMechanism"s)
{ {
authMechanism = it->second; authMechanism = it->second;
} }

View File

@@ -31,6 +31,8 @@
#include <sstream> #include <sstream>
#include <map> #include <map>
using namespace std::string_literals;
namespace Poco { namespace Poco {
namespace MongoDB { namespace MongoDB {
@@ -337,26 +339,26 @@ Poco::MongoDB::Document::Ptr Database::createIndex(
MongoDB::Document::Ptr index = new MongoDB::Document(); MongoDB::Document::Ptr index = new MongoDB::Document();
if (!indexName.empty()) if (!indexName.empty())
{ {
index->add("name", indexName); index->add("name"s, indexName);
} }
index->add("key", keys); index->add("key"s, keys);
index->add("ns", _dbname + "." + collection); index->add("ns"s, _dbname + '.' + collection);
index->add("name", indexName); index->add("name"s, indexName);
if (options & INDEX_UNIQUE) { if (options & INDEX_UNIQUE) {
index->add("unique", true); index->add("unique"s, true);
} }
if (options & INDEX_BACKGROUND) { if (options & INDEX_BACKGROUND) {
index->add("background", true); index->add("background"s, true);
} }
if (options & INDEX_SPARSE) { if (options & INDEX_SPARSE) {
index->add("sparse", true); index->add("sparse"s, true);
} }
if (expirationSeconds > 0) { if (expirationSeconds > 0) {
index->add("expireAfterSeconds", static_cast<Poco::Int32>(expirationSeconds)); index->add("expireAfterSeconds"s, static_cast<Poco::Int32>(expirationSeconds));
} }
if (version > 0) { if (version > 0) {
index->add("version", static_cast<Poco::Int32>(version)); index->add("version"s, static_cast<Poco::Int32>(version));
} }
MongoDB::Array::Ptr indexes = new MongoDB::Array(); MongoDB::Array::Ptr indexes = new MongoDB::Array();

View File

@@ -39,6 +39,8 @@
#define _MONGODB_EXHAUST_ALLOWED_WORKS false #define _MONGODB_EXHAUST_ALLOWED_WORKS false
using namespace std::string_literals;
namespace Poco { namespace Poco {
namespace MongoDB { namespace MongoDB {
@@ -184,7 +186,7 @@ void OpMsgCursor::kill(Connection& connection)
const auto killed = _response.body().get<MongoDB::Array::Ptr>(keyCursorsKilled, nullptr); const auto killed = _response.body().get<MongoDB::Array::Ptr>(keyCursorsKilled, nullptr);
if (!killed || killed->size() != 1 || killed->get<Poco::Int64>(0, -1) != _cursorID) if (!killed || killed->size() != 1 || killed->get<Poco::Int64>(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; _cursorID = 0;

View File

@@ -296,8 +296,8 @@ void OpMsgMessage::read(std::istream& istr)
#if POCO_MONGODB_DUMP #if POCO_MONGODB_DUMP
std::cout std::cout
<< "Message hdr: " << _header.getMessageLength() << " " << remainingSize << " " << "Message hdr: " << _header.getMessageLength() << ' ' << remainingSize << ' '
<< _header.opCode() << " " << _header.getRequestID() << " " << _header.responseTo() << _header.opCode() << ' ' << _header.getRequestID() << ' ' << _header.responseTo()
<< std::endl; << std::endl;
#endif #endif
@@ -357,7 +357,7 @@ void OpMsgMessage::read(std::istream& istr)
while (msgss.tellg() < endOfSection) while (msgss.tellg() < endOfSection)
{ {
#if POCO_MONGODB_DUMP #if POCO_MONGODB_DUMP
std::cout << "section doc: " << msgss.tellg() << " " << endOfSection << std::endl; std::cout << "section doc: " << msgss.tellg() << ' ' << endOfSection << std::endl;
#endif #endif
Document::Ptr doc = new Document(); Document::Ptr doc = new Document();
doc->read(reader); doc->read(reader);

View File

@@ -17,6 +17,8 @@
#include "Poco/Format.h" #include "Poco/Format.h"
#include <limits> #include <limits>
using namespace std::string_literals;
namespace Poco { namespace Poco {
namespace MongoDB { namespace MongoDB {
@@ -179,7 +181,7 @@ std::string ReadPreference::toString() const
if (!_tags.empty()) if (!_tags.empty())
{ {
result += " (tags: " + _tags.toString() + ")"; result += " (tags: "s + _tags.toString() + ')';
} }
if (_maxStalenessSeconds != NO_MAX_STALENESS) if (_maxStalenessSeconds != NO_MAX_STALENESS)

View File

@@ -20,6 +20,8 @@
#include "Poco/NumberParser.h" #include "Poco/NumberParser.h"
#include <chrono> #include <chrono>
using namespace std::string_literals;
namespace Poco { namespace Poco {
namespace MongoDB { namespace MongoDB {
@@ -234,13 +236,13 @@ Connection::Ptr ReplicaSet::isMaster(const Net::SocketAddress& address)
if (response.responseOk()) if (response.responseOk())
{ {
const Document& doc = response.body(); const Document& doc = response.body();
if (doc.get<bool>("isWritablePrimary", false) || doc.get<bool>("ismaster", false)) if (doc.get<bool>("isWritablePrimary"s, false) || doc.get<bool>("ismaster"s, false))
{ {
return conn; return conn;
} }
else if (doc.exists("primary")) else if (doc.exists("primary"s))
{ {
return isMaster(Net::SocketAddress(doc.get<std::string>("primary"))); return isMaster(Net::SocketAddress(doc.get<std::string>("primary"s)));
} }
} }
} }
@@ -411,9 +413,9 @@ void ReplicaSet::updateTopologyFromHello(const Net::SocketAddress& address)
_topology.updateServer(address, doc, rttMicros); _topology.updateServer(address, doc, rttMicros);
// Update replica set name if not set // 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<std::string>("setName"); _config.setName = doc.get<std::string>("setName"s);
_topology.setName(_config.setName); _topology.setName(_config.setName);
} }
} }
@@ -532,45 +534,45 @@ void ReplicaSet::parseURI(const std::string& uri)
Poco::URI::QueryParameters params = theURI.getQueryParameters(); Poco::URI::QueryParameters params = theURI.getQueryParameters();
for (const auto& param : params) for (const auto& param : params)
{ {
if (param.first == "replicaSet") if (param.first == "replicaSet"s)
{ {
_config.setName = param.second; _config.setName = param.second;
} }
else if (param.first == "readPreference") else if (param.first == "readPreference"s)
{ {
// Parse read preference mode // Parse read preference mode
if (param.second == "primary") if (param.second == "primary"s)
{ {
_config.readPreference = ReadPreference(ReadPreference::Primary); _config.readPreference = ReadPreference(ReadPreference::Primary);
} }
else if (param.second == "primaryPreferred") else if (param.second == "primaryPreferred"s)
{ {
_config.readPreference = ReadPreference(ReadPreference::PrimaryPreferred); _config.readPreference = ReadPreference(ReadPreference::PrimaryPreferred);
} }
else if (param.second == "secondary") else if (param.second == "secondary"s)
{ {
_config.readPreference = ReadPreference(ReadPreference::Secondary); _config.readPreference = ReadPreference(ReadPreference::Secondary);
} }
else if (param.second == "secondaryPreferred") else if (param.second == "secondaryPreferred"s)
{ {
_config.readPreference = ReadPreference(ReadPreference::SecondaryPreferred); _config.readPreference = ReadPreference(ReadPreference::SecondaryPreferred);
} }
else if (param.second == "nearest") else if (param.second == "nearest"s)
{ {
_config.readPreference = ReadPreference(ReadPreference::Nearest); _config.readPreference = ReadPreference(ReadPreference::Nearest);
} }
} }
else if (param.first == "connectTimeoutMS") else if (param.first == "connectTimeoutMS"s)
{ {
Poco::Int64 timeoutMs = Poco::NumberParser::parse64(param.second); Poco::Int64 timeoutMs = Poco::NumberParser::parse64(param.second);
_config.connectTimeout = Poco::Timespan(timeoutMs * 1000); // Convert ms to microseconds _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); Poco::Int64 timeoutMs = Poco::NumberParser::parse64(param.second);
_config.socketTimeout = Poco::Timespan(timeoutMs * 1000); // Convert ms to microseconds _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); Poco::Int64 freqMs = Poco::NumberParser::parse64(param.second);
_config.heartbeatFrequency = Poco::Timespan(freqMs * 1000); // Convert ms to microseconds _config.heartbeatFrequency = Poco::Timespan(freqMs * 1000); // Convert ms to microseconds

View File

@@ -17,6 +17,8 @@
#include "Poco/Net/NetException.h" #include "Poco/Net/NetException.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
using namespace std::string_literals;
namespace Poco { namespace Poco {
namespace MongoDB { namespace MongoDB {
@@ -59,7 +61,7 @@ void ReplicaSetConnection::sendRequest(OpMsgMessage& request, OpMsgMessage& resp
if (!response.responseOk() && isRetriableMongoDBError(response)) if (!response.responseOk() && isRetriableMongoDBError(response))
{ {
markServerFailed(); 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(); const auto& msg = ioEx->message();
// Check for specific retriable error messages // Check for specific retriable error messages
if (msg.find("not master") != std::string::npos || if (msg.find("not master"s) != std::string::npos ||
msg.find("NotMaster") != std::string::npos || msg.find("NotMaster"s) != std::string::npos ||
msg.find("Connection") != std::string::npos || msg.find("Connection"s) != std::string::npos ||
msg.find("connection") != std::string::npos) msg.find("connection"s) != std::string::npos)
{ {
return true; return true;
} }
@@ -264,9 +266,9 @@ bool ReplicaSetConnection::isRetriableMongoDBError(const OpMsgMessage& response)
const Document& body = response.body(); const Document& body = response.body();
// Check for error code // Check for error code
if (body.exists("code")) if (body.exists("code"s))
{ {
ErrorCode code = static_cast<ErrorCode>(body.get<int>("code")); ErrorCode code = static_cast<ErrorCode>(body.get<int>("code"s));
switch (code) switch (code)
{ {
@@ -288,11 +290,11 @@ bool ReplicaSetConnection::isRetriableMongoDBError(const OpMsgMessage& response)
} }
// Check for error message patterns // Check for error message patterns
if (body.exists("errmsg")) if (body.exists("errmsg"s))
{ {
const auto& errmsg = body.get<std::string>("errmsg"); const auto& errmsg = body.get<std::string>("errmsg"s);
if (errmsg.find("not master") != std::string::npos || if (errmsg.find("not master"s) != std::string::npos ||
errmsg.find("NotMaster") != std::string::npos) errmsg.find("NotMaster"s) != std::string::npos)
{ {
return true; return true;
} }

View File

@@ -15,6 +15,8 @@
#include "Poco/MongoDB/ServerDescription.h" #include "Poco/MongoDB/ServerDescription.h"
#include "Poco/MongoDB/Array.h" #include "Poco/MongoDB/Array.h"
using namespace std::string_literals;
namespace Poco { namespace Poco {
namespace MongoDB { namespace MongoDB {
@@ -55,9 +57,9 @@ void ServerDescription::updateFromHelloResponse(const Document& helloResponse, P
parseServerType(helloResponse); parseServerType(helloResponse);
// Get replica set name // Get replica set name
if (helloResponse.exists("setName")) if (helloResponse.exists("setName"s))
{ {
_setName = helloResponse.get<std::string>("setName"); _setName = helloResponse.get<std::string>("setName"s);
} }
// Parse hosts list // Parse hosts list
@@ -93,10 +95,10 @@ void ServerDescription::reset()
void ServerDescription::parseServerType(const Document& doc) void ServerDescription::parseServerType(const Document& doc)
{ {
// Check for standalone // Check for standalone
if (!doc.exists("setName")) if (!doc.exists("setName"s))
{ {
// Check if it's a mongos // Check if it's a mongos
if (doc.get<std::string>("msg", "") == "isdbgrid") if (doc.get<std::string>("msg"s, ""s) == "isdbgrid")
{ {
_type = Mongos; _type = Mongos;
return; return;
@@ -106,19 +108,19 @@ void ServerDescription::parseServerType(const Document& doc)
} }
// It's part of a replica set - determine the role // It's part of a replica set - determine the role
if (doc.get<bool>("isWritablePrimary", false) || doc.get<bool>("ismaster", false)) if (doc.get<bool>("isWritablePrimary"s, false) || doc.get<bool>("ismaster"s, false))
{ {
_type = RsPrimary; _type = RsPrimary;
} }
else if (doc.get<bool>("secondary", false)) else if (doc.get<bool>("secondary"s, false))
{ {
_type = RsSecondary; _type = RsSecondary;
} }
else if (doc.get<bool>("arbiterOnly", false)) else if (doc.get<bool>("arbiterOnly"s, false))
{ {
_type = RsArbiter; _type = RsArbiter;
} }
else if (doc.get<bool>("hidden", false) || doc.get<bool>("passive", false)) else if (doc.get<bool>("hidden"s, false) || doc.get<bool>("passive"s, false))
{ {
_type = RsOther; _type = RsOther;
} }
@@ -135,9 +137,9 @@ void ServerDescription::parseHosts(const Document& doc)
_hosts.clear(); _hosts.clear();
// Parse hosts array // Parse hosts array
if (doc.exists("hosts")) if (doc.exists("hosts"s))
{ {
Array::Ptr hostsArray = doc.get<Array::Ptr>("hosts"); Array::Ptr hostsArray = doc.get<Array::Ptr>("hosts"s);
_hosts.reserve(hostsArray->size()); _hosts.reserve(hostsArray->size());
for (std::size_t i = 0; i < hostsArray->size(); ++i) 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) // Parse passives array (hidden/passive members)
if (doc.exists("passives")) if (doc.exists("passives"s))
{ {
Array::Ptr passivesArray = doc.get<Array::Ptr>("passives"); Array::Ptr passivesArray = doc.get<Array::Ptr>("passives"s);
for (std::size_t i = 0; i < passivesArray->size(); ++i) for (std::size_t i = 0; i < passivesArray->size(); ++i)
{ {
try try
@@ -172,9 +174,9 @@ void ServerDescription::parseHosts(const Document& doc)
} }
// Parse arbiters array // Parse arbiters array
if (doc.exists("arbiters")) if (doc.exists("arbiters"s))
{ {
Array::Ptr arbitersArray = doc.get<Array::Ptr>("arbiters"); Array::Ptr arbitersArray = doc.get<Array::Ptr>("arbiters"s);
for (std::size_t i = 0; i < arbitersArray->size(); ++i) for (std::size_t i = 0; i < arbitersArray->size(); ++i)
{ {
try try
@@ -195,9 +197,9 @@ void ServerDescription::parseTags(const Document& doc)
{ {
_tags.clear(); _tags.clear();
if (doc.exists("tags")) if (doc.exists("tags"s))
{ {
Document::Ptr tagsDoc = doc.get<Document::Ptr>("tags"); Document::Ptr tagsDoc = doc.get<Document::Ptr>("tags"s);
if (!tagsDoc.isNull()) if (!tagsDoc.isNull())
{ {
_tags = *tagsDoc; _tags = *tagsDoc;