Library: MongoDB
Package: MongoDB
Header: Poco/MongoDB/ReplicaSetURI.h
Description
Class for parsing and generating MongoDB replica set URIs.
This class handles parsing of MongoDB connection strings in the format:
mongodb://[username:password@]host1[:port1][,host2[:port2],...][/[database][?options]]
It also provides functionality to: - Access and modify the list of servers - Access and modify configuration options - Generate a URI string from the current state
Usage example:
ReplicaSetURI uri("mongodb://host1:27017,host2:27017/?replicaSet=rs0");
// Access parsed data
std::vector<Net::SocketAddress> servers = uri.servers();
std::string setName = uri.replicaSet();
// Modify and regenerate
uri.addServer(Net::SocketAddress("host3:27017"));
uri.setReadPreference("secondaryPreferred");
std::string newUri = uri.toString();
Member Summary
Member Functions: addServer, clearServers, connectTimeoutMS, database, heartbeatFrequencyMS, parse, password, readPreference, reconnectDelay, reconnectRetries, replicaSet, servers, setConnectTimeoutMS, setDatabase, setHeartbeatFrequencyMS, setPassword, setReadPreference, setReconnectDelay, setReconnectRetries, setReplicaSet, setServers, setSocketTimeoutMS, setUsername, socketTimeoutMS, toString, username
Constructors
ReplicaSetURI
Creates an empty ReplicaSetURI.
ReplicaSetURI
explicit ReplicaSetURI(
const std::string & uri
);
Creates a ReplicaSetURI by parsing the given MongoDB connection string.
Throws Poco::SyntaxException if the URI format is invalid. Throws Poco::UnknownURISchemeException if the scheme is not "mongodb".
Destructor
~ReplicaSetURI
~ReplicaSetURI();
Destroys the ReplicaSetURI.
Member Functions
addServer
void addServer(
const std::string & server
);
Adds a server to the list as a string (host:port format).
clearServers
void clearServers();
Clears the list of servers.
connectTimeoutMS
[[nodiscard]]
unsigned int connectTimeoutMS() const;
Returns the connection timeout in milliseconds.
database
[[nodiscard]]
std::string database() const;
Returns the database name from the URI path, or empty string if not set.
heartbeatFrequencyMS
[[nodiscard]]
unsigned int heartbeatFrequencyMS() const;
Returns the heartbeat frequency in milliseconds.
parse
void parse(
const std::string & uri
);
Parses a MongoDB connection string and updates the configuration.
Throws Poco::SyntaxException if the URI format is invalid. Throws Poco::UnknownURISchemeException if the scheme is not "mongodb".
password
[[nodiscard]]
std::string password() const;
Returns the password, or empty string if not set.
readPreference
[[nodiscard]]
ReadPreference readPreference() const;
Returns the read preference.
reconnectDelay
[[nodiscard]]
unsigned int reconnectDelay() const;
Returns the reconnection delay in seconds.
reconnectRetries
[[nodiscard]]
unsigned int reconnectRetries() const;
Returns the number of reconnection retries.
replicaSet
[[nodiscard]]
std::string replicaSet() const;
Returns the replica set name, or empty string if not set.
servers
[[nodiscard]]
const std::vector < std::string > & servers() const;
Returns the list of server addresses as strings (host:port format). Servers are NOT resolved - they remain as strings exactly as provided in the URI.
setConnectTimeoutMS
void setConnectTimeoutMS(
unsigned int timeoutMS
);
Sets the connection timeout in milliseconds.
setDatabase
void setDatabase(
const std::string & database
);
Sets the database name.
setHeartbeatFrequencyMS
void setHeartbeatFrequencyMS(
unsigned int milliseconds
);
Sets the heartbeat frequency in milliseconds. Throws Poco::InvalidArgumentException if milliseconds < MIN_HEARTBEAT_FREQUENCY_MS (500). Per MongoDB SDAM specification, minimum value is 500 milliseconds.
setPassword
void setPassword(
const std::string & password
);
Sets the password.
setReadPreference
void setReadPreference(
const ReadPreference & pref
);
Sets the read preference.
setReadPreference
void setReadPreference(
const std::string & mode
);
Sets the read preference from a string mode. Valid modes: primary, primaryPreferred, secondary, secondaryPreferred, nearest
setReconnectDelay
void setReconnectDelay(
unsigned int seconds
);
Sets the reconnection delay in seconds.
setReconnectRetries
void setReconnectRetries(
unsigned int retries
);
Sets the number of reconnection retries.
setReplicaSet
void setReplicaSet(
const std::string & name
);
Sets the replica set name.
setServers
void setServers(
const std::vector < std::string > & servers
);
Sets the list of server addresses as strings (host:port format).
setSocketTimeoutMS
void setSocketTimeoutMS(
unsigned int timeoutMS
);
Sets the socket timeout in milliseconds.
setUsername
void setUsername(
const std::string & username
);
Sets the username.
socketTimeoutMS
[[nodiscard]]
unsigned int socketTimeoutMS() const;
Returns the socket timeout in milliseconds.
toString
[[nodiscard]]
std::string toString() const;
Generates a MongoDB connection string from the current configuration. Format: mongodb://[username:password@]host1:port1[,host2:port2,...][/database][?options]
username
[[nodiscard]]
std::string username() const;
Returns the username, or empty string if not set.
Variables
DEFAULT_CONNECT_TIMEOUT_MS
static constexpr unsigned int DEFAULT_CONNECT_TIMEOUT_MS = 10000;
Default connection timeout: 10 seconds
DEFAULT_HEARTBEAT_FREQUENCY_MS
static constexpr unsigned int DEFAULT_HEARTBEAT_FREQUENCY_MS = 10000;
Default heartbeat frequency: 10 seconds
DEFAULT_RECONNECT_DELAY
static constexpr unsigned int DEFAULT_RECONNECT_DELAY = 1;
Default reconnect delay: 1 second
DEFAULT_RECONNECT_RETRIES
static constexpr unsigned int DEFAULT_RECONNECT_RETRIES = 10;
Default number of reconnect attempts
DEFAULT_SOCKET_TIMEOUT_MS
static constexpr unsigned int DEFAULT_SOCKET_TIMEOUT_MS = 30000;
Default socket timeout: 30 seconds
MIN_HEARTBEAT_FREQUENCY_MS
static constexpr unsigned int MIN_HEARTBEAT_FREQUENCY_MS = 500;
Minimum heartbeat frequency per MongoDB SDAM specification: 500 milliseconds