创建Client实例

客户端工厂类

  1. namespace pegasus {
  2. class pegasus_client_factory
  3. {
  4. public:
  5. ///
  6. /// \brief initialize
  7. /// initialize pegasus client lib. must call this function before anything else.
  8. /// \param config_file
  9. /// the configuration file of client lib
  10. /// \return
  11. /// true indicate the initailize is success.
  12. ///
  13. static bool initialize(const char *config_file);
  14. ///
  15. /// \brief get_client
  16. /// get an instance for a given cluster and a given app name.
  17. /// \param cluster_name
  18. /// the pegasus cluster name.
  19. /// a cluster can have multiple apps.
  20. /// \param app_name
  21. /// an app is a logical isolated k-v store.
  22. /// a cluster can have multiple apps.
  23. /// \return
  24. /// the client instance. DO NOT delete this client even after usage.
  25. static pegasus_client *get_client(const char *cluster_name, const char *app_name);
  26. };
  27. } //end namespace

客户端的初始化,实际上是对rdsn框架初始化

  1. if (!pegasus::pegasus_client_factory::initialize("config.ini")) {
  2. fprintf(stderr, "ERROR: init pegasus failed\n");
  3. return -1;
  4. }
  5. /**succeed, continue**/

注:

  • 参数:config_file 见 配置文件 的介绍
  • 返回值:bool值,true代表初始化成功,false初始化失败
  • 该函数只需在一个进程生命周期内调用一次即可,并且非线程安全的获取客户端
  1. pegasus::pegasus_client* pg_client = pegasus::pegasus_client_factory::get_client("cluster_name", "table_name");
  2. if (pg_client == nullptr) {
  3. fprintf(stderr, "ERROR: get pegasus client failed\n");
  4. return -1;
  5. }
  6. /*** do what you want with pg_client ****/

注意: get_client返回值不能明确的调用delete,也不能用智能指针封装,框架在停止的时候自动释放(底层使用单例来保存)