[CXX-995] Add mongocxx::pool examples Created: 17/Aug/16  Updated: 15/Mar/17  Resolved: 08/Mar/17

Status: Closed
Project: C++ Driver
Component/s: Documentation
Affects Version/s: None
Fix Version/s: 3.2.0-rc0

Type: New Feature Priority: Minor - P4
Reporter: David Golden Assignee: Samuel Rossi (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CXX-994 Fix missing or misleading docs about ... Closed

 Description   

We have no examples of pool usage for multi-threaded operation and we should have some. At a minimum, it should show:

  • pool initialization and configuration (pool size)
  • RAII acquisition of a client object within a thread for an operation


 Comments   
Comment by Githook User [ 15/Mar/17 ]

Author:

{u'username': u'xdg', u'name': u'David Golden', u'email': u'xdg@xdg.me'}

Message: CXX-995 CXX-1256 Fix pool example URI

Newer libmongoc has stricter connection string parsing.
Branch: master
https://github.com/mongodb/mongo-cxx-driver/commit/29a4a590c6234ab44b869df1e5de659fa15b8e52

Comment by Githook User [ 08/Mar/17 ]

Author:

{u'username': u'saghm', u'name': u'Saghm Rossi', u'email': u'saghmrossi@gmail.com'}

Message: CXX-995 Add mongocxx::pool examples
Branch: master
https://github.com/mongodb/mongo-cxx-driver/commit/d474ccd57342f3f12d78a65b70c34f0d5d11ee8b

Comment by chen shu [ 22/Nov/16 ]

My code,
1. Create a singleton class MyApp to hold the pool object as a member
my_app.h file

#ifndef INCLUDE_MY_APP_H_
#define INCLUDE_MY_APP_H_
 
#include "config.h"
#include <memory>
#include <string>
#include <mongocxx/pool.hpp>
 
using namespace std;
 
class MyApp {
public:
  static MyApp &Instance();
 
  void config(Configuration *configuration);
  Configuration &config() const;
 
  void SetPool(mongocxx::pool* pool);
  mongocxx::pool &GetPool() const;
 
private:
  MyApp();
  MyApp(MyApp const &);
  MyApp &operator=(MyApp const &);
 
  unique_ptr<Configuration> config_;
  unique_ptr<mongocxx::pool> pool_;
};
 
#endif

my_app.cc file

#include "my_app.h"
 
MyApp::MyApp() {}
 
MyApp &MyApp::Instance() {
  static MyApp app;
  return app;
}
 
void MyApp::config(Configuration *configuration) {
  config_.reset(configuration);
}
 
Configuration &MyApp::config() const { return *config_; }
 
void MyApp::SetPool(mongocxx::pool *pool) {
  pool_.reset(pool);
}
 
mongocxx::pool &MyApp::GetPool() const {
  return *pool_;
}

2. create the MyApp object and set the pool to its member in main function(main.cc file)

int main(int argc, char **argv) {
  try {
    cppcms::json::value config_json;
    LoadSettings(argc, argv, config_json);
    MyApp &app = MyApp::Instance();
 
    Configuration *config = new Configuration();
    config->mongo_uri = config_json.get<string>("mongo.uri");
    config->db_name = config_json.get<string>("mongo.db_name");
    config->task_db = config_json.get<string>("mongo.task_db");
    config->sent_db = config_json.get<string>("mongo.sent_db");
    config->cache_db = config_json.get<string>("mongo.cache_db");
    config->gsql_uri = config_json.get<string>("gsql.uri");
    config->default_locale = config_json.get<string>("default_locale.language");
    app.config(config);
 
    mongocxx::instance inst{};
    mongocxx::pool *p = new mongocxx::pool(mongocxx::uri{config->mongo_uri});
    app.SetPool(p);
 
    cppcms::service srv(argc, argv);
    srv.applications_pool().mount(cppcms::applications_factory<Site>());
    srv.run();
  } catch (std::exception const &e) {
    std::cerr << e.what() << std::endl;
  }
}

3. In any thread function, use the following to get connection from pool

void ReportService::AddBlackDetail(string const &phone, int32_t found_time,
                                   string const &type, string const &rule,
                                   stringstream &stream) {
  Configuration &config = MyApp::Instance().config();
  auto conn = MyApp::Instance().GetPool().acquire(); // conn here is an unique_ptr which has real conn object, it will be destroyed when this function is returned and get back to pool(I guess that)
  auto db = (*conn)[config.cache_db];
  auto coll = db["black_detail"];
  mongocxx::options::update upsert_option;
  upsert_option.upsert(true);
 
  bsoncxx::builder::stream::document filter_builder, update_builder;
  filter_builder << "phone" << phone;
  update_builder << "$set" << open_document << "phone" << phone << "found_time"
                 << found_time << "type" << type;
  if (rule != "") {
    update_builder << "rule" << rule;
  }
  update_builder << close_document;
  mongocxx::stdx::optional<mongocxx::result::update> result = coll.update_one(
      filter_builder.view(), update_builder.view(), upsert_option);
  stream << "{\"result\":\"" << result->result().upserted_count()
         << " phones inserted\"}";
}

Comment by David Golden [ 17/Aug/16 ]

Sorry, I meant that the example should have a comment to the effect that when the object goes out of scope, it will be returned to the pool automatically. There's no 'return to pool' call. I assume that's expected by experienced C++ programmers, but the examples ought to be newbie friendly.

Comment by Andrew Morrow (Inactive) [ 17/Aug/16 ]

You don't actually need to demo RAII since the objects handed back from the mongocxx::pool are unique_ptrs.

Generated at Wed Feb 07 22:01:02 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.