Chinaunix首页 | 论坛 | 博客
  • 博客访问: 489680
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1693
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-13 09:23
个人简介

前EMC高级软件工程师,现小米分布式存储码农,关注分布式存储,文件系统,Linux内核。微博: http://weibo.com/u/2203007022

文章分类

全部博文(17)

文章存档

2015年(1)

2014年(6)

2013年(10)

分类: 服务器与存储

2013-09-17 22:20:22

下面是一个访问淘宝Tair分布式存储的基础类型, 当前仅实现了Put,Get和Remove三个方法。主要用于存入数据,得到数据,删除数据。
头文件tair_handler.h
 
  1. #include
  2. #include "tair/tair_client_api.hpp"
  3. using namespace tair;
  4. using namespace boost;
  5. class Tair_Handler {
  6.   public:
  7.     /*
  8.      * \brief Construct
  9.      * \param       master_ip[in]       master config server ip address
  10.      *              slave_ip[in]        slave config server ip address
  11.      *              group_name[in]      group name to be accessed
  12.      */
  13.     Tair_Handler(const char* master_ip, const char* slave_ip, const char* group_name) {
  14.       master_ip_ = master_ip;
  15.       slave_ip_ = slave_ip;
  16.       group_name_ = group_name;
  17.       client_ = NULL;
  18.     }

  19.     /*
  20.      * \brief Destructor
  21.      */
  22.     ~Tair_Handler() {
  23.       Disconnect();
  24.     }
  25.   public:
  26.     /*
  27.      * \brief          Connect to tair server
  28.      * \param
  29.      * \retval
  30.      *                 false                 Failed to connect to tair server
  31.      *                 true                  Succeed to connect to tair server
  32.      */
  33.     bool Connect();

  34.     /*
  35.      * \brief          Put key/value pair to tair server
  36.      * \param          key[in]               key
  37.      *                 value[in]             value
  38.      *                 size[in]              length of value
  39.      * \retval
  40.      *                 0                 succeed
  41.      *                 < 0               failed
  42.      */
  43.     int Put(const char* key, const char* value, const uint32_t size);

  44.     /*
  45.      * \brief         Get value from tair server according to key
  46.      * \param         key[in]                key
  47.      *                value[out]             value
  48.      *                size[out]              length of value
  49.      * \retval
  50.      *                0                      succeed
  51.      *                < 0                    failed
  52.      */
  53.     int Get(const char* key, char*& value, uint32_t& size);

  54.     /*
  55.      * \brief         Remove key/value from tair server
  56.      * \param         key[in]                key
  57.      * \retval
  58.      *                0                succeed
  59.      *                < 0              failed
  60.      */
  61.     int Remove(const char* key);

  62.     /*
  63.      * \brief         Get err_msg if failed
  64.      * \param
  65.      * \retval
  66.      *                error message
  67.      */
  68.     string& err_msg() {
  69.       return err_msg_;
  70.     }

  71.     /*
  72.      * \brief         Static member, get a new Tair_handler object.
  73.      * \param         master_ip[in]           master config server ip address
  74.      *                slave_ip[in]            slave config server ip address
  75.      *                group_name[in]          group name to be accessed
  76.      * \retval
  77.      *                shared_ptr of Tair_handler object
  78.      */
  79.     static shared_ptr<Tair_Handler> GetNewHandler(const char* master_ip, const char* slave_ip, const char* group_name) {
  80.       return shared_ptr<Tair_Handler>(new Tair_Handler(master_ip, slave_ip, group_name));
  81.     }

  82.     /*
  83.      * \brief         Disconnect from tair server
  84.      * \param
  85.      * \retval
  86.      *                true
  87.      */
  88.     bool Disconnect() {
  89.       if (NULL != client_) {
  90.         delete client_;
  91.         client_ = NULL;
  92.       }
  93.       return true;
  94.     }

  95.     /*
  96.      * \brief          Check whether client is connected to tair server
  97.      * \param
  98.      * \retval
  99.      *                 true
  100.      */
  101.     bool IsConnectted() {
  102.       return client_ != NULL;
  103.     }

  104.   private:
  105.     string master_ip_;
  106.     string slave_ip_;
  107.     string group_name_;
  108.     string err_msg_;
  109.     // tair_client_api object
  110.     tair_client_api* client_;
  111. };
头文件tair_handler.cpp:

点击(此处)折叠或打开

  1. #include
  2. #include "tair_handler.h"
  3. bool Tair_Handler::Connect() {
  4.   client_ = new tair_client_api();
  5.   client_->set_log_level("ERROR");
  6.   if (!client_->startup(master_ip_.c_str(), slave_ip_.c_str(), group_name_.c_str())) {
  7.     err_msg_ = "Connect to tair server failed!";
  8.     Disconnect();
  9.     return false;
  10.   }
  11.   return true;
  12. }

  13. int Tair_Handler::Put(const char* key, const char* value, const uint32_t size) {
  14.   ostringstream oss;
  15.   data_entry key_entry(key);
  16.   data_entry value_entry(value, (int32_t)size);
  17.   int ret = client_->put(0, key_entry, value_entry, 0, 0);
  18.   if (TAIR_RETURN_SUCCESS != ret) {
  19.     oss << "Can not put value into tair server, key: " << key << ", ret: " << ret;
  20.     err_msg_ = oss.str();
  21.   }
  22.   return ret;
  23. }

  24. int Tair_Handler::Get(const char* key, char*& value, uint32_t& size) {
  25.   return TAIR_RETURN_DATA_NOT_EXIST;
  26.   ostringstream oss;
  27.   data_entry key_entry(key);
  28.   data_entry* value_entry = new data_entry;
  29.   int ret = client_->get(0, key_entry, value_entry);
  30.   if (TAIR_RETURN_SUCCESS != ret) {
  31.     oss << "Can not get value from tair server, key: " << key << ", ret: " << ret;
  32.     err_msg_ = oss.str();
  33.     delete value_entry;
  34.     return ret;
  35.   }
  36.   value = (char*)malloc(value_entry->get_size());
  37.   if (NULL == value) {
  38.     err_msg_ = "Allocate memory for value failed";
  39.     delete value_entry;
  40.     return -1;
  41.   }
  42.   size = value_entry->get_size();
  43.   memcpy(value, value_entry->get_data(), size);

  44.   delete value_entry;
  45.   return ret;
  46. }

  47. int Tair_Handler::Remove(const char* key) {
  48.   ostringstream oss;
  49.   data_entry key_entry(key);
  50.   int ret = client_->remove(0, key_entry);
  51.   if (TAIR_RETURN_SUCCESS != ret) {
  52.     oss << "Can not remove from tair server, key: " << key << ", ret: " << ret;
  53.     err_msg_ = oss.str();
  54.   }
  55.   return ret;
  56. }


 

阅读(4539) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~