LCOV - code coverage report
Current view: top level - src - utils_mongodb.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 27 37 73.0 %
Date: 2025-11-04 01:35:31 Functions: 2 2 100.0 %

          Line data    Source code
       1             : #ifndef SOCIAL_NETWORK_MICROSERVICES_SRC_UTILS_MONGODB_H_
       2             : #define SOCIAL_NETWORK_MICROSERVICES_SRC_UTILS_MONGODB_H_
       3             : 
       4             : #include <mongoc.h>
       5             : #include <bson/bson.h>
       6             : 
       7             : #define SERVER_SELECTION_TIMEOUT_MS 300
       8             : 
       9             : namespace social_network {
      10             : 
      11           1 : mongoc_client_pool_t* init_mongodb_client_pool(
      12             :     const json &config_json,
      13             :     const std::string &service_name,
      14             :     uint32_t max_size
      15             : ) {
      16           2 :   std::string addr = config_json[service_name + "-mongodb"]["addr"];
      17           1 :   int port = config_json[service_name + "-mongodb"]["port"];
      18           2 :   std::string uri_str = "mongodb://" + addr + ":" +
      19           4 :       std::to_string(port) + "/?appname=" + service_name + "-service";
      20             :   uri_str += "&" MONGOC_URI_SERVERSELECTIONTIMEOUTMS "="
      21           1 :       + std::to_string(SERVER_SELECTION_TIMEOUT_MS);
      22             : 
      23           1 :   mongoc_init();
      24             :   bson_error_t error;
      25             :   mongoc_uri_t *mongodb_uri =
      26           1 :       mongoc_uri_new_with_error(uri_str.c_str(), &error);
      27             : 
      28           1 :   if (!mongodb_uri) {
      29           0 :     LOG(fatal) << "Error: failed to parse URI" << std::endl
      30           0 :               << "error message: " << std::endl
      31           0 :               << uri_str << std::endl
      32           0 :               << error.message<< std::endl;
      33           0 :     return nullptr;
      34             :   } else {
      35           1 :     if (config_json["ssl"]["enabled"]) {
      36           0 :       std::string ca_file = config_json["ssl"]["caPath"];
      37             : 
      38           0 :       mongoc_uri_set_option_as_bool(mongodb_uri, MONGOC_URI_TLS, true);
      39           0 :       mongoc_uri_set_option_as_utf8(mongodb_uri, MONGOC_URI_TLSCAFILE, ca_file.c_str());
      40           0 :       mongoc_uri_set_option_as_bool(mongodb_uri, MONGOC_URI_TLSALLOWINVALIDHOSTNAMES, true);
      41             :     }
      42             : 
      43           1 :     mongoc_client_pool_t *client_pool= mongoc_client_pool_new(mongodb_uri);
      44           1 :     mongoc_client_pool_max_size(client_pool, max_size);
      45           1 :     return client_pool;
      46             :   }
      47             : }
      48             : 
      49           1 : bool CreateIndex(
      50             :     mongoc_client_t *client,
      51             :     const std::string &db_name,
      52             :     const std::string &index,
      53             :     bool unique) {
      54             :   mongoc_database_t *db;
      55             :   bson_t keys;
      56             :   char *index_name;
      57             :   bson_t *create_indexes;
      58             :   bson_t reply;
      59             :   bson_error_t error;
      60             :   bool r;
      61             : 
      62           1 :   db = mongoc_client_get_database(client, db_name.c_str());
      63           1 :   bson_init (&keys);
      64           1 :   BSON_APPEND_INT32(&keys, index.c_str(), 1);
      65           1 :   index_name = mongoc_collection_keys_to_index_string(&keys);
      66           1 :   create_indexes = BCON_NEW (
      67             :       "createIndexes", BCON_UTF8(db_name.c_str()),
      68             :       "indexes", "[", "{",
      69             :           "key", BCON_DOCUMENT (&keys),
      70             :           "name", BCON_UTF8 (index_name),
      71             :           "unique", BCON_BOOL(unique),
      72           1 :       "}", "]");
      73             :   r = mongoc_database_write_command_with_opts (
      74           1 :       db, create_indexes, NULL, &reply, &error);
      75           1 :   if (!r) {
      76           0 :     LOG(error) << "Error in createIndexes: " << error.message;
      77             :   }
      78           1 :   bson_free (index_name);
      79           1 :   bson_destroy (&reply);
      80           1 :   bson_destroy (create_indexes);
      81           1 :   mongoc_database_destroy(db);
      82             : 
      83           1 :   return r;
      84             : }
      85             : 
      86             : } // namespace social_network
      87             : 
      88             : #endif //SOCIAL_NETWORK_MICROSERVICES_SRC_UTILS_MONGODB_H_

Generated by: LCOV version 1.12