您的位置:首页 > 其它

handlersocket使用 第一章 基本语法和一个简单示例

2013-02-05 15:30 447 查看
注: 本文档主要根据原作者的英文文档protocol.en.txt写成,做了一些翻译工作和添加了一些例子以及一些需要注意的地方。如果本文档对你有所帮助,欢迎关注我的新浪微博:http://weibo.com/u/1857063732 如果有建议,欢迎发送到我的邮箱xiaoxuye1988@163.com 目前就职于 欢聚时代(YY), 从事于后台开发工作。

一.Basic syntax

英文原文如下

Basic syntax

- TheHandlerSocket protocol is line-based. Each line ends with LF(0x0a).

- Each lineconsists a concatenation of tokens separated by HT(0x09).

- A token iseither NULL or an encoded string. Note that you need to

  distinguish NULL from an empty string, asmost DBMs does so.

- NULL isexpressed as a single NUL(0x00).

- An encodedstring is a string with the following encoding rules.

        - Characters in the range [0x10 - 0xff]are encoded as itselves.

        - A character in the range [0x00 -0x0f] is prefixed by 0x01 and

          shifted by 0x40. For example, 0x03 isencoded as 0x01 0x43.

- Note that astring can be empty. A continuation of 0x09 0x09 means that

  there is an empty string between them. Acontinuation of 0x09 0x0a means

  that there is an empty string at the end ofthe line.

 

-Handlersocket协议是基于行的。每一行一’\n’结束。

-每一行由一系列记号组成,他们之间用'\t'分隔。

-一个记号要不是NULL,要不是一个已经编码的string。提醒你需要区分NULL和一个空的string,就像大部分DBMs那样做的。

-NULL由一个单独的空字符表示(0x00)

-一个已编码的string遵从以下编码规则。

      --范围为[0x10-0xff]的字符将会编码为其本身。

      --如果一个字符的范围为[0x00-0x0f],那么这个字符的编码规则为:用0x01为前缀,        然后偏移0x40。例如0x03会被编码为0x01 0x43 

-提醒,一个string可以是空的。一个连续的0x090x09意思是由一个空的string在中间。一个连续的0x09 0x0a意思是,有一个空的string在这一行的最后。

 

Handlersocket是MySql的一个NoSQL插件。它在mysqld进程中以后台形式工作,接受TCP连接并且处理来自客户端的请求。

Handlersocket不支持SQL语法。他支持简单的create,retrieve, update, delete这写在表上的操作。

由于以下的原因,handersocket在一些情况下会比一般的mysqld/libmysql快:

  -HandlerSocket 不用解析SQL语句,直接操作数据,节省了CPU的开销。

 -HandlerSocket 读很多从客户端读很多请求,并且批量处理这些请求,节省了CPU和磁    盘的开销。

 -HandlerSocket 的client/server协议比 mysql/libmysql的协议致密,节省了网络传输的开销。

目前版本的HandlerSocket只能在GNU/Linux运行工作。

先看一个简单的用例:

#include <cstdlib>

#include <iostream>

#include <handlersocket/hstcpcli.hpp>

#include <boost/shared_ptr.hpp>

#include <vector>

 

using namespace std;

using namespace dena;

/*

 *

 */

int main(int argc, char** argv) {

   config conf;

   conf["host"] = "127.0.0.1";

   conf["port"] = "9998";

 

   socket_args sock_args;

   sock_args.set(conf);

   hstcpcli_ptr cli = hstcpcli_i::create(sock_args);

     /////多线程情况下,应该开启多个client,每个线程对应独立的client

 //  cli->request_buf_auth();

 

/*******通过索引打开连接 ************/

   int code = 0;

   size_t numflds = 0;

 

 //写请求buffer,column之间除了逗号不允许空格,通过下面的request_send发送请求

                                                 //db name // table // index

   cli->request_buf_open_index(1, "test", "test1","PRIMARY", "col1,col2,col3");  

   do {           //发送request_buf_open_index请求

       if(cli->request_send() != 0) {

            fprintf(stderr, "request_send:%s\n", cli->get_error().c_str());

              break;

       }

       if ((code = cli->response_recv(numflds)) != 0) {

                     fprintf(stderr,"response_recv: %s\n", cli->get_error().c_str());

                     break;

              }

    } while (false);

    cli->response_buf_remove();

 

    vector<string_ref> vec;

    

    char key_buf[32] = { 0 };

    uint32_t uid = 5;

    snprintf(key_buf, 32, "%u", uid);

    const string key(key_buf);

    const string_ref keyref(key.data(), key.size());

    vec.push_back(keyref1);

        

    const string kTestEqualOp("=");

    const string_ref kTestEqualOpRef(kTestEqualOp.data(),kTestEqualOp.size());

 

    cli->request_buf_exec_generic(1, kTestEqualOpRef, &vec[0], 1, 1,0, 0, 0, 0);

    

    do {//发送request_buf_exec_generic

        if (cli->request_send() != 0) {

           // cout<<"send err"<<endl;

                     fprintf(stderr,"request_send: %s\n", cli->get_error().c_str());

                     break;

        }

        if ((code = cli->response_recv(numflds)) !=0) {

                     fprintf(stderr,"response_recv: %s\n", cli->get_error().c_str());

                     break;

        }

         //输出结果

        while (true) {

                     conststring_ref * const row = cli->get_next_row();

                     if(row == 0) {

                            break;

                     }

 

                     for(size_t i = 0; i < numflds; ++i) {

                            conststring val(row[i].begin(), row[i].size());

                            printf("%s", val.c_str());

                     }

                     printf("\n");

              }

     } while (false);

 

       cli->response_buf_remove();

       cli->close();

   return 0;

}

 

基本的代码形式如上,看完上述例子,你已经有了一个大概的了解,下来将根据其原有的英文协议说明,进行一个比较详细的介绍。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐