SWITCH_STANDARD_API(mongo_mapreduce_function)
{
switch_status_t status = SWITCH_STATUS_SUCCESS;
- DBClientConnection *conn = NULL;
+ DBClientBase *conn = NULL;
char *ns = NULL, *json_query = NULL;
ns = strdup(cmd);
if (!zstr(ns) && !zstr(json_query) && !zstr(json_fields)) {
- DBClientConnection *conn = NULL;
+ DBClientBase *conn = NULL;
try {
BSONObj query = fromjson(json_query);
const char *cf = "mongo.conf";
switch_xml_t cfg, xml, settings, param;
switch_status_t status = SWITCH_STATUS_SUCCESS;
- const char *host = "127.0.0.1";
+ const char *conn_str = "127.0.0.1";
switch_size_t min_connections = 1, max_connections = 1;
if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
int tmp;
if (!strcmp(var, "host")) {
- host = val;
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "'host' is deprecated. use 'connection-string'\n");
+ conn_str = val;
+ } else if (!strcmp(var, "connection-string")) {
+ conn_str = val;
} else if (!strcmp(var, "min-connections")) {
if ((tmp = atoi(val)) > 0) {
min_connections = tmp;
}
}
- if (mongo_connection_pool_create(&globals.conn_pool, min_connections, max_connections, host) != SWITCH_STATUS_SUCCESS) {
+ if (mongo_connection_pool_create(&globals.conn_pool, min_connections, max_connections, conn_str) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Can't create connection pool\n");
status = SWITCH_STATUS_GENERR;
} else {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Mongo connection pool created [%s %d/%d]\n", host, (int)min_connections, (int)max_connections);
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Mongo connection pool created [%s %d/%d]\n", conn_str, (int)min_connections, (int)max_connections);
}
switch_xml_free(xml);
#ifndef MOD_MONGO_H
#define MOD_MONGO_H
-
#include <client/dbclient.h>
#include <client/connpool.h>
#include <db/json.h>
using namespace mongo;
typedef struct {
- char *host;
+ char *conn_str;
switch_size_t min_connections;
switch_size_t max_connections;
} mongo_connection_pool_t;
-switch_status_t mongo_connection_create(DBClientConnection **connection, const char *host);
-void mongo_connection_destroy(DBClientConnection **conn);
+switch_status_t mongo_connection_create(DBClientBase **connection, const char *conn_str);
+void mongo_connection_destroy(DBClientBase **conn);
switch_status_t mongo_connection_pool_create(mongo_connection_pool_t **conn_pool, switch_size_t min_connections, switch_size_t max_connections,
- const char *host);
+ const char *conn_str);
void mongo_connection_pool_destroy(mongo_connection_pool_t **conn_pool);
-DBClientConnection *mongo_connection_pool_get(mongo_connection_pool_t *conn_pool);
-switch_status_t mongo_connection_pool_put(mongo_connection_pool_t *conn_pool, DBClientConnection *conn);
+DBClientBase *mongo_connection_pool_get(mongo_connection_pool_t *conn_pool);
+switch_status_t mongo_connection_pool_put(mongo_connection_pool_t *conn_pool, DBClientBase *conn);
#endif
scoped_conn.done();
*/
-switch_status_t mongo_connection_create(DBClientConnection **connection, const char *host)
+switch_status_t mongo_connection_create(DBClientBase **connection, const char *conn_str)
{
- DBClientConnection *conn = new DBClientConnection();
+ DBClientBase *conn = NULL;
+ string conn_string(conn_str), err_msg;
+ ConnectionString cs = ConnectionString::parse(conn_string, err_msg);
+
+ if (!cs.isValid()) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't parse url: %s\n", err_msg.c_str());
+ return SWITCH_STATUS_GENERR;
+ }
try {
- conn->connect(host);
+ conn = cs.connect(err_msg);
} catch (DBException &e) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't connect to mongo [%s]\n", host);
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't connect to mongo [%s]: %s\n", conn_str, err_msg.c_str());
return SWITCH_STATUS_GENERR;
}
*connection = conn;
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected to mongo [%s]\n", host);
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected to mongo [%s]\n", conn_str);
return SWITCH_STATUS_SUCCESS;
}
-void mongo_connection_destroy(DBClientConnection **conn)
+void mongo_connection_destroy(DBClientBase **conn)
{
switch_assert(*conn != NULL);
delete *conn;
}
switch_status_t mongo_connection_pool_create(mongo_connection_pool_t **conn_pool, switch_size_t min_connections, switch_size_t max_connections,
- const char *host)
+ const char *conn_str)
{
switch_memory_pool_t *pool = NULL;
switch_status_t status = SWITCH_STATUS_SUCCESS;
mongo_connection_pool_t *cpool = NULL;
- DBClientConnection *conn = NULL;
+ DBClientBase *conn = NULL;
if ((status = switch_core_new_memory_pool(&pool)) != SWITCH_STATUS_SUCCESS) {
return status;
cpool->min_connections = min_connections;
cpool->max_connections = max_connections;
- cpool->host = switch_core_strdup(pool, host);
+ cpool->conn_str = switch_core_strdup(pool, conn_str);
cpool->pool = pool;
for (cpool->size = 0; cpool->size < min_connections; cpool->size++) {
- if (mongo_connection_create(&conn, host) == SWITCH_STATUS_SUCCESS) {
+ if (mongo_connection_create(&conn, conn_str) == SWITCH_STATUS_SUCCESS) {
mongo_connection_pool_put(cpool, conn);
} else {
break;
switch_assert(cpool != NULL);
while (switch_queue_trypop(cpool->connections, &data) == SWITCH_STATUS_SUCCESS) {
- mongo_connection_destroy((DBClientConnection **)&data);
+ mongo_connection_destroy((DBClientBase **)&data);
}
switch_mutex_destroy(cpool->mutex);
}
-DBClientConnection *mongo_connection_pool_get(mongo_connection_pool_t *conn_pool)
+DBClientBase *mongo_connection_pool_get(mongo_connection_pool_t *conn_pool)
{
- DBClientConnection *conn = NULL;
+ DBClientBase *conn = NULL;
void *data = NULL;
switch_assert(conn_pool != NULL);
switch_mutex_lock(conn_pool->mutex);
if (switch_queue_trypop(conn_pool->connections, &data) == SWITCH_STATUS_SUCCESS) {
- conn = (DBClientConnection *) data;
- } else if (mongo_connection_create(&conn, conn_pool->host) == SWITCH_STATUS_SUCCESS) {
+ conn = (DBClientBase *) data;
+ } else if (mongo_connection_create(&conn, conn_pool->conn_str) == SWITCH_STATUS_SUCCESS) {
if (++conn_pool->size > conn_pool->max_connections) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Connection pool is empty. You may want to increase 'max-connections'\n");
}
return conn;
}
-switch_status_t mongo_connection_pool_put(mongo_connection_pool_t *conn_pool, DBClientConnection *conn)
+switch_status_t mongo_connection_pool_put(mongo_connection_pool_t *conn_pool, DBClientBase *conn)
{
switch_status_t status = SWITCH_STATUS_SUCCESS;