下面是一个访问淘宝Tair分布式存储的基础类型, 当前仅实现了Put,Get和Remove三个方法。主要用于存入数据,得到数据,删除数据。
头文件tair_handler.h
头文件tair_handler.cpp:
阅读(621) | 评论(0) | 转发(0) |
<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
头文件tair_handler.h
- #include <boost/shared_ptr.hpp>
- #include "tair/tair_client_api.hpp"
- using namespace tair;
- using namespace boost;
- class Tair_Handler {
- public:
- /*
- * \brief Construct
- * \param master_ip[in] master config server ip address
- * slave_ip[in] slave config server ip address
- * group_name[in] group name to be accessed
- */
- Tair_Handler(const char* master_ip, const char* slave_ip, const char* group_name) {
- master_ip_ = master_ip;
- slave_ip_ = slave_ip;
- group_name_ = group_name;
- client_ = NULL;
- }
-
- /*
- * \brief Destructor
- */
- ~Tair_Handler() {
- Disconnect();
- }
- public:
- /*
- * \brief Connect to tair server
- * \param
- * \retval
- * false Failed to connect to tair server
- * true Succeed to connect to tair server
- */
- bool Connect();
-
- /*
- * \brief Put key/value pair to tair server
- * \param key[in] key
- * value[in] value
- * size[in] length of value
- * \retval
- * 0 succeed
- * < 0 failed
- */
- int Put(const char* key, const char* value, const uint32_t size);
-
- /*
- * \brief Get value from tair server according to key
- * \param key[in] key
- * value[out] value
- * size[out] length of value
- * \retval
- * 0 succeed
- * < 0 failed
- */
- int Get(const char* key, char*& value, uint32_t& size);
-
- /*
- * \brief Remove key/value from tair server
- * \param key[in] key
- * \retval
- * 0 succeed
- * < 0 failed
- */
- int Remove(const char* key);
-
- /*
- * \brief Get err_msg if failed
- * \param
- * \retval
- * error message
- */
- string& err_msg() {
- return err_msg_;
- }
-
- /*
- * \brief Static member, get a new Tair_handler object.
- * \param master_ip[in] master config server ip address
- * slave_ip[in] slave config server ip address
- * group_name[in] group name to be accessed
- * \retval
- * shared_ptr of Tair_handler object
- */
- static shared_ptr<Tair_Handler> GetNewHandler(const char* master_ip, const char* slave_ip, const char* group_name) {
- return shared_ptr<Tair_Handler>(new Tair_Handler(master_ip, slave_ip, group_name));
- }
-
- /*
- * \brief Disconnect from tair server
- * \param
- * \retval
- * true
- */
- bool Disconnect() {
- if (NULL != client_) {
- delete client_;
- client_ = NULL;
- }
- return true;
- }
-
- /*
- * \brief Check whether client is connected to tair server
- * \param
- * \retval
- * true
- */
- bool IsConnectted() {
- return client_ != NULL;
- }
-
- private:
- string master_ip_;
- string slave_ip_;
- string group_name_;
- string err_msg_;
- // tair_client_api object
- tair_client_api* client_;
- };
点击(此处)折叠或打开
- #include <sstream>
- #include "tair_handler.h"
- bool Tair_Handler::Connect() {
- client_ = new tair_client_api();
- client_->set_log_level("ERROR");
- if (!client_->startup(master_ip_.c_str(), slave_ip_.c_str(), group_name_.c_str())) {
- err_msg_ = "Connect to tair server failed!";
- Disconnect();
- return false;
- }
- return true;
- }
-
- int Tair_Handler::Put(const char* key, const char* value, const uint32_t size) {
- ostringstream oss;
- data_entry key_entry(key);
- data_entry value_entry(value, (int32_t)size);
- int ret = client_->put(0, key_entry, value_entry, 0, 0);
- if (TAIR_RETURN_SUCCESS != ret) {
- oss << "Can not put value into tair server, key: " << key << ", ret: " << ret;
- err_msg_ = oss.str();
- }
- return ret;
- }
-
- int Tair_Handler::Get(const char* key, char*& value, uint32_t& size) {
- return TAIR_RETURN_DATA_NOT_EXIST;
- ostringstream oss;
- data_entry key_entry(key);
- data_entry* value_entry = new data_entry;
- int ret = client_->get(0, key_entry, value_entry);
- if (TAIR_RETURN_SUCCESS != ret) {
- oss << "Can not get value from tair server, key: " << key << ", ret: " << ret;
- err_msg_ = oss.str();
- delete value_entry;
- return ret;
- }
- value = (char*)malloc(value_entry->get_size());
- if (NULL == value) {
- err_msg_ = "Allocate memory for value failed";
- delete value_entry;
- return -1;
- }
- size = value_entry->get_size();
- memcpy(value, value_entry->get_data(), size);
-
- delete value_entry;
- return ret;
- }
-
- int Tair_Handler::Remove(const char* key) {
- ostringstream oss;
- data_entry key_entry(key);
- int ret = client_->remove(0, key_entry);
- if (TAIR_RETURN_SUCCESS != ret) {
- oss << "Can not remove from tair server, key: " << key << ", ret: " << ret;
- err_msg_ = oss.str();
- }
- return ret;
- }
相关热门文章
给主人留下些什么吧!~~
评论热议