基于C语言的ssdb笔记 ----hashmap的简单实例
ssdb支持 zset, map/hash, list, kv 数据结构,同redis差不多。下面是关于ssdb hsahmap的使用笔记
1.ssdb hashmap的命令
1.hset name key value
设置hashmap中指定key的值
2.hget name key
获取hashmap中指定key的值
3.hdel name key
删除hashmap中指定的key
4.hincr name key [num]
使hashmap中key对应的值增加num
5.hexists name key
判断指定的key是否存在于hashmap中
6.hsize name
返回hashmap中元素的个数
7.hlist name_start name_end limit
列出名字处于区间 (name_start, name_end] 的 hashmap.
8.hrlist name_start name_end limit
逆序
9.hkeys name key_start key_end
列出 hashmap 中处于区间 (key_start, key_end] 的 key 列表.
10.hgetall name
返回整个 hashmap.
11.hscan name key_start key_end limit
列出 hashmap 中处于区间 (key_start, key_end] 的 key-value 列表.
12.hrscan name key_start key_end limit
像 hscan, 逆序.
13.hclear name
删除 hashmap 中的所有 key.
14.multi_hset name key1 value1 key2 value2 ...
批量设置 hashmap 中的 key-value.
15.multi_hget name key1 key2 ...
批量获取 hashmap 中多个 key 对应的权重值.
16.multi_hdel name key1 key2 ...
指删除 hashmap 中的 key.
2.ssdb使用hashmap 存储获取的代码
这里因为ssdb是完全兼容redis的,所以完全可以用redis的库的接口函数。因为这样的特性,使得使用redis的项目很容易的能迁移到ssdb,作者的原博客也有完整的redis迁移到ssdb的完整教程。
ssdb的连接
redisContext *conn = redisConnect(host,port);
host:连接ssdb的主机ip
port:连接端口 返回值 redisContext结构体 定义如下
/* Context for a connection to Redis */
2 typedef struct redisContext {
3 int err; /* Error flags, 0 when there is no error */
4 char errstr[128]; /* String representation of error when applicable */
5 int fd;
6 int flags;
7 char *obuf; /* Write buffer */
8 redisReader *reader; /* Protocol reader */
9
10 enum redisConnectionType connection_type;
11 struct timeval *timeout;
12
13 struct {
14 char *host;
15 char *source_addr;
16 int port;
17 } tcp;
18
19 struct {
20 char *path;
21 } unix_sock;
22
23 } redisContext; ssdb的命令执行
redisReply* reply = redisCommand(conn,cmd)
conn:redis的连接句柄
cmd:执行的命令
返回值:redisReply结构体 结构体如下:
1 /* This is the reply object returned by redisCommand() */
2 typedef struct redisReply {
3 int type; /* REDIS_REPLY_* */
4 long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
5 size_t len; /* Length of string */
6 char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
7 size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
8 struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
9 } redisReply; 其中type元素的类型可用来判断放回结果的状态:
REDIS_REPLY_STRING 1 //返回字符串,查看str,len字段
REDIS_REPLY_ARRAY 2 //返回一个数组,查看elements的值(数组个数),通过element[index]的方式访问数组元素,每个数组元素是一个redisReply对象的指针
REDIS_REPLY_INTEGER 3 //返回整数,从integer字段获取值
REDIS_REPLY_NIL 4 //没有数据返回
REDIS_REPLY_STATUS 5 //表示状态,内容通过str字段查看,字符串长度是len字段
REDIS_REPLY_ERROR 6 //表示出错,查看出错信息,如上的str,len字段 redisReply的释放
freeReplyObject(reply);
用来释放执行命令后所占用的内存 ssdb连接的释放
redisFree(conn);
完整代码如下:
redisContext* ssdb_init(const char* host,uint16_t port) {
redisContext *conn = redisConnect(host,port);
if(conn->err) {
printf("connection error:%s\n",conn->errstr);
redisFree(conn);
return NULL;
}
return conn;
} void ssdb_finit(redisContext *conn) {
if (conn)
redisFree(conn);
} redisReply* ssdb_command(redisContext *conn,char *cmd) {
redisReply* reply = NULL;
if(NULL != conn && cmd != NULL) {
reply = redisCommand(conn,cmd);
} else {
printf("redis err\n");
return NULL;
}
return reply;
} int main(){
char buf[] = {};
char name[] = {};
char key[] = {};
char value[] = {};
redisReply* reply = NULL;
redisContext *ssdb_conn = ssdb_init("127.0.0.1",);
if(ssdb_conn == NULL){
printf("ssdb conn err...\n");
return ;
}
memcpy(name,"mymap",);
memcpy(key,"name",);
memcpy(value,"wangwu",);
snprintf(buf,sizeof(buf),"hset %s %s %s",name,key,value);
reply = ssdb_command(ssdb_conn,buf);
if(reply != NULL){
if(reply->type == REDIS_REPLY_ERROR)
printf("commd err:%s\n",reply->str);
freeReplyObject(reply);
} //根据指定key获取value值
memset(buf,,sizeof(buf));
memcpy(buf,"hget mymap name",sizeof(buf));
reply = ssdb_command(ssdb_conn,buf);
if(reply == NULL)
return ;
if(reply->type == REDIS_REPLY_ERROR){
printf("commd err:%s\n",reply->str);
freeReplyObject(reply);
return -;
}
printf("the value this is:%s\n",reply->str);
freeReplyObject(reply); //获取mymap里所有的key-value值
memset(buf,,sizeof(buf));
memcpy(buf,"hgetall mymap",sizeof(buf));
reply = ssdb_command(ssdb_conn,buf);
if(reply == NULL)
return ;
int i = ;
/*此处对于获取的hashmap来说这个返回的数组每个key-value是占有两个元素的第一个元素为key 后一个元素为前一个元素的值*/
for(i = ;i < reply->elements;i+=){
printf("key:%s values:%s\n",reply->element[i]->str,reply->element[i + ]->str);
} freeReplyObject(reply);
return ;
}
执行效果图如下:
基于C语言的ssdb笔记 ----hashmap的简单实例的更多相关文章
- 第一单元总结:基于基础语言、继承和接口的简单OOP
前情提要 到目前为止,OO课程已经完成了前三次的作业,分别为: 第一次作业:简单多项式的构造和求导.[正则表达式][数据结构][排序] 第二次作业:含三角函数因子的复杂多项式的构造.求导和化简.[递归 ...
- R语言入门视频笔记--2--一些简单的命令
一.对象 1.列举当前内存中的对象 ls() 2.删除不需要的对象 rm(某对象名称) 3.查看向量长度 length(某向量名称) 4.查看向量类型 mode(某向量名称) 二.函数 1.seq函数 ...
- Hibernate笔记——第一个简单实例
1. 首先进行框架配置 导包 hibernate配置xml文件 ======================= 2. 创建并配置POJO类 Hibernate是操作持久化层的框架,和数据库打交道,其 ...
- 关于《selenium2自动测试实战--基于Python语言》
关于本书的类型: 首先在我看来技术书分为两类,一类是“思想”,一类是“操作手册”. 对于思想类的书,一般作者有很多年经验积累,这类书需要细读与品位.高手读了会深有体会,豁然开朗.新手读了不止所云,甚至 ...
- 《Selenium2自动化测试实战--基于Python语言》 --即将面市
发展历程: <selenium_webdriver(python)第一版> 将本博客中的这个系列整理为pdf文档,免费. <selenium_webdriver(python)第 ...
- Gogs - 基于 Go 语言的自助 Git 服务
Gogs(Go Git Service) 是一个基于 Go 语言的自助 Git 服务.Gogs 的目标是打造一个最简单.最快速和最轻松的方式搭建自助 Git 服务.使用 Go 语言开发使得 Gogs ...
- 基于ruby的watir自动化测试 笔记二
基于ruby的watir自动化测试 笔记一的补充版,新增加了些特殊的控件捕获方法.还在更新中.... attribute_value 获取当前控件的属性 Value = ie.link(:id=> ...
- Ggoogle Protocol Buffer的使用 (基于C++语言)
首先说明的是Protocol Buffle是灵活高效的.它的一个很好的优点(很重要的,我认为)就是后向兼容性--当我们扩展了了.proto文件后,我们照样可以用它来读取之前生成的文件. 之前已经写了关 ...
- C语言细节总结笔记
C语言细节总结笔记 */--> C语言细节总结笔记 Table of Contents 1. 三步异或法交换数字 2. 做差法交换数字 3. 按n位置位 4. 求余求商求积 5. 辗除法求最大公 ...
随机推荐
- IE盒模型和标准盒模型
标准盒模型和ie盒模型(怪异盒模型) w3c标准盒模型 width和height不包括padding和border ie盒模型 width和height包含padding和border ie8以上都是 ...
- NFS指定端口,NFS缓存
nfs服务端: #编辑/etc/nfsmount.conf,在末尾添加: #RQUOTAD_PORT=30001#LOCKD_TCPPORT=30002#LOCKD_UDPPORT=30002#MOU ...
- Ansible playbook练习
示例1:创建用户的Playbook --- - name: create user hosts: openstack gather_facts: false tasks: - name: create ...
- 012_流式计算系统(Mahout协同过滤)
课程介绍 课程内容 1.Mahout是什么 l Mahout是一个算法库,集成了很多算法. l Apache Mahout 是 Apache Software Foundation(ASF)旗下的 ...
- Oracle学习笔记_05_分组函数
组函数:avg sum max min count group by having group by 增强:rollup cube grouping groupi ...
- 05 - Django应用第二步
知识点 1) 数据库的配置 涉及到INSTALL_APPS的配置等 初次创建数据的命令 2) 模型的编写 模型的创建方式, 写一个类继承自models.Model 模型的数据类型 外键 自动创建ID列 ...
- 【leetcode刷题笔记】ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- UDEV管理RAC共享存储
背景:操作系统 centos 6.7 数据库:11.2.0.1 操作流程: 1. 确认在所有RAC节点上已经安装了必要的UDEV包[root@11gnode1 ~]# rpm -qa|grep ude ...
- debian服务查询
1.查询 用root身份执行service --status-all查看所有服务的状态 "+" started "-" stopped "?" ...
- Poj_1004_FinancialManagement
一.Description Larry graduated this year and finally has a job. He's making a lot of money, but someh ...