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;
}
基本的代码形式如上,看完上述例子,你已经有了一个大概的了解,下来将根据其原有的英文协议说明,进行一个比较详细的介绍。
一.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;
}
基本的代码形式如上,看完上述例子,你已经有了一个大概的了解,下来将根据其原有的英文协议说明,进行一个比较详细的介绍。
相关文章推荐
- JAVA套接字(Socket)101七天系列—第四天【一个简单示例】
- 一个简单的、基本的、完整的使用biblatex包的例子
- Xqk.Data数据框架使用说明之:一个简单的示例
- (翻译)《Expert .NET 2.0 IL Assembler》 第一章 简单示例 1.1 CLR的基本概念
- 使用ServerSocket、Socket创建一个简单的服务聊天程序
- Lua的系统学习(杂)_语法使用的简单示例
- flex 整合 struts 的一个简单示例(使用Httpservice)
- Spring MVC中使用SimpleUrlHandlerMapping映射处理器的一个简单例子
- Android handler的使用简单示例
- android 一个小例子说明handler和AlertDialog的简单使用
- Spring MVC:使用SimpleUrlHandlerMapping的一个简单例子
- 由一个简单示例 引出java继承中父子类成员变量共享问题 以及super的使用
- 用类名做方法的返回值类型 在学习Java的初始阶段,很多同学使用基本数据类型定义变量和引用类型定义变量以及使用类名做方法的返回值类型常常不知所以。今天我以自己的解读方式和个人的理解作一个简单的解释,和
- 第一章 基本语法-6循环(for)练习 求一个数的所有约数
- Linux平台:一个简单的TCP6 server的示例代码(socket)
- Plone的一个简单工作流使用示例
- 使用socket.io做一个简单的WEB聊天室
- 一个简单的使用SDL播放YUV文件的示例
- 从使用Python开发一个Socket示例说到开发者的思维和习惯问题 推荐
- 使用Python的Bottle框架写一个简单的服务接口的示例